Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tuxitheone 0:ecaf3e593122 1 /**
Tuxitheone 0:ecaf3e593122 2 * @file
Tuxitheone 0:ecaf3e593122 3 * Implementation of raw protocol PCBs for low-level handling of
Tuxitheone 0:ecaf3e593122 4 * different types of protocols besides (or overriding) those
Tuxitheone 0:ecaf3e593122 5 * already available in lwIP.
Tuxitheone 0:ecaf3e593122 6 *
Tuxitheone 0:ecaf3e593122 7 */
Tuxitheone 0:ecaf3e593122 8
Tuxitheone 0:ecaf3e593122 9 /*
Tuxitheone 0:ecaf3e593122 10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
Tuxitheone 0:ecaf3e593122 11 * All rights reserved.
Tuxitheone 0:ecaf3e593122 12 *
Tuxitheone 0:ecaf3e593122 13 * Redistribution and use in source and binary forms, with or without modification,
Tuxitheone 0:ecaf3e593122 14 * are permitted provided that the following conditions are met:
Tuxitheone 0:ecaf3e593122 15 *
Tuxitheone 0:ecaf3e593122 16 * 1. Redistributions of source code must retain the above copyright notice,
Tuxitheone 0:ecaf3e593122 17 * this list of conditions and the following disclaimer.
Tuxitheone 0:ecaf3e593122 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Tuxitheone 0:ecaf3e593122 19 * this list of conditions and the following disclaimer in the documentation
Tuxitheone 0:ecaf3e593122 20 * and/or other materials provided with the distribution.
Tuxitheone 0:ecaf3e593122 21 * 3. The name of the author may not be used to endorse or promote products
Tuxitheone 0:ecaf3e593122 22 * derived from this software without specific prior written permission.
Tuxitheone 0:ecaf3e593122 23 *
Tuxitheone 0:ecaf3e593122 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
Tuxitheone 0:ecaf3e593122 25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Tuxitheone 0:ecaf3e593122 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
Tuxitheone 0:ecaf3e593122 27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Tuxitheone 0:ecaf3e593122 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
Tuxitheone 0:ecaf3e593122 29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Tuxitheone 0:ecaf3e593122 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Tuxitheone 0:ecaf3e593122 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
Tuxitheone 0:ecaf3e593122 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
Tuxitheone 0:ecaf3e593122 33 * OF SUCH DAMAGE.
Tuxitheone 0:ecaf3e593122 34 *
Tuxitheone 0:ecaf3e593122 35 * This file is part of the lwIP TCP/IP stack.
Tuxitheone 0:ecaf3e593122 36 *
Tuxitheone 0:ecaf3e593122 37 * Author: Adam Dunkels <adam@sics.se>
Tuxitheone 0:ecaf3e593122 38 *
Tuxitheone 0:ecaf3e593122 39 */
Tuxitheone 0:ecaf3e593122 40
Tuxitheone 0:ecaf3e593122 41 #include "lwip/opt.h"
Tuxitheone 0:ecaf3e593122 42
Tuxitheone 0:ecaf3e593122 43 #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
Tuxitheone 0:ecaf3e593122 44
Tuxitheone 0:ecaf3e593122 45 #include "lwip/def.h"
Tuxitheone 0:ecaf3e593122 46 #include "lwip/memp.h"
Tuxitheone 0:ecaf3e593122 47 #include "lwip/ip_addr.h"
Tuxitheone 0:ecaf3e593122 48 #include "lwip/netif.h"
Tuxitheone 0:ecaf3e593122 49 #include "lwip/raw.h"
Tuxitheone 0:ecaf3e593122 50 #include "lwip/stats.h"
Tuxitheone 0:ecaf3e593122 51 #include "arch/perf.h"
Tuxitheone 0:ecaf3e593122 52
Tuxitheone 0:ecaf3e593122 53 #include <string.h>
Tuxitheone 0:ecaf3e593122 54
Tuxitheone 0:ecaf3e593122 55 /** The list of RAW PCBs */
Tuxitheone 0:ecaf3e593122 56 static struct raw_pcb *raw_pcbs;
Tuxitheone 0:ecaf3e593122 57
Tuxitheone 0:ecaf3e593122 58 /**
Tuxitheone 0:ecaf3e593122 59 * Determine if in incoming IP packet is covered by a RAW PCB
Tuxitheone 0:ecaf3e593122 60 * and if so, pass it to a user-provided receive callback function.
Tuxitheone 0:ecaf3e593122 61 *
Tuxitheone 0:ecaf3e593122 62 * Given an incoming IP datagram (as a chain of pbufs) this function
Tuxitheone 0:ecaf3e593122 63 * finds a corresponding RAW PCB and calls the corresponding receive
Tuxitheone 0:ecaf3e593122 64 * callback function.
Tuxitheone 0:ecaf3e593122 65 *
Tuxitheone 0:ecaf3e593122 66 * @param p pbuf to be demultiplexed to a RAW PCB.
Tuxitheone 0:ecaf3e593122 67 * @param inp network interface on which the datagram was received.
Tuxitheone 0:ecaf3e593122 68 * @return - 1 if the packet has been eaten by a RAW PCB receive
Tuxitheone 0:ecaf3e593122 69 * callback function. The caller MAY NOT not reference the
Tuxitheone 0:ecaf3e593122 70 * packet any longer, and MAY NOT call pbuf_free().
Tuxitheone 0:ecaf3e593122 71 * @return - 0 if packet is not eaten (pbuf is still referenced by the
Tuxitheone 0:ecaf3e593122 72 * caller).
Tuxitheone 0:ecaf3e593122 73 *
Tuxitheone 0:ecaf3e593122 74 */
Tuxitheone 0:ecaf3e593122 75 u8_t
Tuxitheone 0:ecaf3e593122 76 raw_input(struct pbuf *p, struct netif *inp)
Tuxitheone 0:ecaf3e593122 77 {
Tuxitheone 0:ecaf3e593122 78 struct raw_pcb *pcb, *prev;
Tuxitheone 0:ecaf3e593122 79 struct ip_hdr *iphdr;
Tuxitheone 0:ecaf3e593122 80 s16_t proto;
Tuxitheone 0:ecaf3e593122 81 u8_t eaten = 0;
Tuxitheone 0:ecaf3e593122 82
Tuxitheone 0:ecaf3e593122 83 LWIP_UNUSED_ARG(inp);
Tuxitheone 0:ecaf3e593122 84
Tuxitheone 0:ecaf3e593122 85 iphdr = (struct ip_hdr *)p->payload;
Tuxitheone 0:ecaf3e593122 86 proto = IPH_PROTO(iphdr);
Tuxitheone 0:ecaf3e593122 87
Tuxitheone 0:ecaf3e593122 88 prev = NULL;
Tuxitheone 0:ecaf3e593122 89 pcb = raw_pcbs;
Tuxitheone 0:ecaf3e593122 90 /* loop through all raw pcbs until the packet is eaten by one */
Tuxitheone 0:ecaf3e593122 91 /* this allows multiple pcbs to match against the packet by design */
Tuxitheone 0:ecaf3e593122 92 while ((eaten == 0) && (pcb != NULL)) {
Tuxitheone 0:ecaf3e593122 93 if ((pcb->protocol == proto) &&
Tuxitheone 0:ecaf3e593122 94 (ip_addr_isany(&pcb->local_ip) ||
Tuxitheone 0:ecaf3e593122 95 ip_addr_cmp(&(pcb->local_ip), &current_iphdr_dest))) {
Tuxitheone 0:ecaf3e593122 96 #if IP_SOF_BROADCAST_RECV
Tuxitheone 0:ecaf3e593122 97 /* broadcast filter? */
Tuxitheone 0:ecaf3e593122 98 if ((pcb->so_options & SOF_BROADCAST) || !ip_addr_isbroadcast(&current_iphdr_dest, inp))
Tuxitheone 0:ecaf3e593122 99 #endif /* IP_SOF_BROADCAST_RECV */
Tuxitheone 0:ecaf3e593122 100 {
Tuxitheone 0:ecaf3e593122 101 /* receive callback function available? */
Tuxitheone 0:ecaf3e593122 102 if (pcb->recv != NULL) {
Tuxitheone 0:ecaf3e593122 103 /* the receive callback function did not eat the packet? */
Tuxitheone 0:ecaf3e593122 104 if (pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr()) != 0) {
Tuxitheone 0:ecaf3e593122 105 /* receive function ate the packet */
Tuxitheone 0:ecaf3e593122 106 p = NULL;
Tuxitheone 0:ecaf3e593122 107 eaten = 1;
Tuxitheone 0:ecaf3e593122 108 if (prev != NULL) {
Tuxitheone 0:ecaf3e593122 109 /* move the pcb to the front of raw_pcbs so that is
Tuxitheone 0:ecaf3e593122 110 found faster next time */
Tuxitheone 0:ecaf3e593122 111 prev->next = pcb->next;
Tuxitheone 0:ecaf3e593122 112 pcb->next = raw_pcbs;
Tuxitheone 0:ecaf3e593122 113 raw_pcbs = pcb;
Tuxitheone 0:ecaf3e593122 114 }
Tuxitheone 0:ecaf3e593122 115 }
Tuxitheone 0:ecaf3e593122 116 }
Tuxitheone 0:ecaf3e593122 117 /* no receive callback function was set for this raw PCB */
Tuxitheone 0:ecaf3e593122 118 }
Tuxitheone 0:ecaf3e593122 119 /* drop the packet */
Tuxitheone 0:ecaf3e593122 120 }
Tuxitheone 0:ecaf3e593122 121 prev = pcb;
Tuxitheone 0:ecaf3e593122 122 pcb = pcb->next;
Tuxitheone 0:ecaf3e593122 123 }
Tuxitheone 0:ecaf3e593122 124 return eaten;
Tuxitheone 0:ecaf3e593122 125 }
Tuxitheone 0:ecaf3e593122 126
Tuxitheone 0:ecaf3e593122 127 /**
Tuxitheone 0:ecaf3e593122 128 * Bind a RAW PCB.
Tuxitheone 0:ecaf3e593122 129 *
Tuxitheone 0:ecaf3e593122 130 * @param pcb RAW PCB to be bound with a local address ipaddr.
Tuxitheone 0:ecaf3e593122 131 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
Tuxitheone 0:ecaf3e593122 132 * bind to all local interfaces.
Tuxitheone 0:ecaf3e593122 133 *
Tuxitheone 0:ecaf3e593122 134 * @return lwIP error code.
Tuxitheone 0:ecaf3e593122 135 * - ERR_OK. Successful. No error occured.
Tuxitheone 0:ecaf3e593122 136 * - ERR_USE. The specified IP address is already bound to by
Tuxitheone 0:ecaf3e593122 137 * another RAW PCB.
Tuxitheone 0:ecaf3e593122 138 *
Tuxitheone 0:ecaf3e593122 139 * @see raw_disconnect()
Tuxitheone 0:ecaf3e593122 140 */
Tuxitheone 0:ecaf3e593122 141 err_t
Tuxitheone 0:ecaf3e593122 142 raw_bind(struct raw_pcb *pcb, ip_addr_t *ipaddr)
Tuxitheone 0:ecaf3e593122 143 {
Tuxitheone 0:ecaf3e593122 144 ip_addr_set(&pcb->local_ip, ipaddr);
Tuxitheone 0:ecaf3e593122 145 return ERR_OK;
Tuxitheone 0:ecaf3e593122 146 }
Tuxitheone 0:ecaf3e593122 147
Tuxitheone 0:ecaf3e593122 148 /**
Tuxitheone 0:ecaf3e593122 149 * Connect an RAW PCB. This function is required by upper layers
Tuxitheone 0:ecaf3e593122 150 * of lwip. Using the raw api you could use raw_sendto() instead
Tuxitheone 0:ecaf3e593122 151 *
Tuxitheone 0:ecaf3e593122 152 * This will associate the RAW PCB with the remote address.
Tuxitheone 0:ecaf3e593122 153 *
Tuxitheone 0:ecaf3e593122 154 * @param pcb RAW PCB to be connected with remote address ipaddr and port.
Tuxitheone 0:ecaf3e593122 155 * @param ipaddr remote IP address to connect with.
Tuxitheone 0:ecaf3e593122 156 *
Tuxitheone 0:ecaf3e593122 157 * @return lwIP error code
Tuxitheone 0:ecaf3e593122 158 *
Tuxitheone 0:ecaf3e593122 159 * @see raw_disconnect() and raw_sendto()
Tuxitheone 0:ecaf3e593122 160 */
Tuxitheone 0:ecaf3e593122 161 err_t
Tuxitheone 0:ecaf3e593122 162 raw_connect(struct raw_pcb *pcb, ip_addr_t *ipaddr)
Tuxitheone 0:ecaf3e593122 163 {
Tuxitheone 0:ecaf3e593122 164 ip_addr_set(&pcb->remote_ip, ipaddr);
Tuxitheone 0:ecaf3e593122 165 return ERR_OK;
Tuxitheone 0:ecaf3e593122 166 }
Tuxitheone 0:ecaf3e593122 167
Tuxitheone 0:ecaf3e593122 168
Tuxitheone 0:ecaf3e593122 169 /**
Tuxitheone 0:ecaf3e593122 170 * Set the callback function for received packets that match the
Tuxitheone 0:ecaf3e593122 171 * raw PCB's protocol and binding.
Tuxitheone 0:ecaf3e593122 172 *
Tuxitheone 0:ecaf3e593122 173 * The callback function MUST either
Tuxitheone 0:ecaf3e593122 174 * - eat the packet by calling pbuf_free() and returning non-zero. The
Tuxitheone 0:ecaf3e593122 175 * packet will not be passed to other raw PCBs or other protocol layers.
Tuxitheone 0:ecaf3e593122 176 * - not free the packet, and return zero. The packet will be matched
Tuxitheone 0:ecaf3e593122 177 * against further PCBs and/or forwarded to another protocol layers.
Tuxitheone 0:ecaf3e593122 178 *
Tuxitheone 0:ecaf3e593122 179 * @return non-zero if the packet was free()d, zero if the packet remains
Tuxitheone 0:ecaf3e593122 180 * available for others.
Tuxitheone 0:ecaf3e593122 181 */
Tuxitheone 0:ecaf3e593122 182 void
Tuxitheone 0:ecaf3e593122 183 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
Tuxitheone 0:ecaf3e593122 184 {
Tuxitheone 0:ecaf3e593122 185 /* remember recv() callback and user data */
Tuxitheone 0:ecaf3e593122 186 pcb->recv = recv;
Tuxitheone 0:ecaf3e593122 187 pcb->recv_arg = recv_arg;
Tuxitheone 0:ecaf3e593122 188 }
Tuxitheone 0:ecaf3e593122 189
Tuxitheone 0:ecaf3e593122 190 /**
Tuxitheone 0:ecaf3e593122 191 * Send the raw IP packet to the given address. Note that actually you cannot
Tuxitheone 0:ecaf3e593122 192 * modify the IP headers (this is inconsistent with the receive callback where
Tuxitheone 0:ecaf3e593122 193 * you actually get the IP headers), you can only specify the IP payload here.
Tuxitheone 0:ecaf3e593122 194 * It requires some more changes in lwIP. (there will be a raw_send() function
Tuxitheone 0:ecaf3e593122 195 * then.)
Tuxitheone 0:ecaf3e593122 196 *
Tuxitheone 0:ecaf3e593122 197 * @param pcb the raw pcb which to send
Tuxitheone 0:ecaf3e593122 198 * @param p the IP payload to send
Tuxitheone 0:ecaf3e593122 199 * @param ipaddr the destination address of the IP packet
Tuxitheone 0:ecaf3e593122 200 *
Tuxitheone 0:ecaf3e593122 201 */
Tuxitheone 0:ecaf3e593122 202 err_t
Tuxitheone 0:ecaf3e593122 203 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *ipaddr)
Tuxitheone 0:ecaf3e593122 204 {
Tuxitheone 0:ecaf3e593122 205 err_t err;
Tuxitheone 0:ecaf3e593122 206 struct netif *netif;
Tuxitheone 0:ecaf3e593122 207 ip_addr_t *src_ip;
Tuxitheone 0:ecaf3e593122 208 struct pbuf *q; /* q will be sent down the stack */
Tuxitheone 0:ecaf3e593122 209
Tuxitheone 0:ecaf3e593122 210 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
Tuxitheone 0:ecaf3e593122 211
Tuxitheone 0:ecaf3e593122 212 /* not enough space to add an IP header to first pbuf in given p chain? */
Tuxitheone 0:ecaf3e593122 213 if (pbuf_header(p, IP_HLEN)) {
Tuxitheone 0:ecaf3e593122 214 /* allocate header in new pbuf */
Tuxitheone 0:ecaf3e593122 215 q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
Tuxitheone 0:ecaf3e593122 216 /* new header pbuf could not be allocated? */
Tuxitheone 0:ecaf3e593122 217 if (q == NULL) {
Tuxitheone 0:ecaf3e593122 218 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
Tuxitheone 0:ecaf3e593122 219 return ERR_MEM;
Tuxitheone 0:ecaf3e593122 220 }
Tuxitheone 0:ecaf3e593122 221 if (p->tot_len != 0) {
Tuxitheone 0:ecaf3e593122 222 /* chain header q in front of given pbuf p */
Tuxitheone 0:ecaf3e593122 223 pbuf_chain(q, p);
Tuxitheone 0:ecaf3e593122 224 }
Tuxitheone 0:ecaf3e593122 225 /* { first pbuf q points to header pbuf } */
Tuxitheone 0:ecaf3e593122 226 LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
Tuxitheone 0:ecaf3e593122 227 } else {
Tuxitheone 0:ecaf3e593122 228 /* first pbuf q equals given pbuf */
Tuxitheone 0:ecaf3e593122 229 q = p;
Tuxitheone 0:ecaf3e593122 230 if(pbuf_header(q, -IP_HLEN)) {
Tuxitheone 0:ecaf3e593122 231 LWIP_ASSERT("Can't restore header we just removed!", 0);
Tuxitheone 0:ecaf3e593122 232 return ERR_MEM;
Tuxitheone 0:ecaf3e593122 233 }
Tuxitheone 0:ecaf3e593122 234 }
Tuxitheone 0:ecaf3e593122 235
Tuxitheone 0:ecaf3e593122 236 if ((netif = ip_route(ipaddr)) == NULL) {
Tuxitheone 0:ecaf3e593122 237 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
Tuxitheone 0:ecaf3e593122 238 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
Tuxitheone 0:ecaf3e593122 239 /* free any temporary header pbuf allocated by pbuf_header() */
Tuxitheone 0:ecaf3e593122 240 if (q != p) {
Tuxitheone 0:ecaf3e593122 241 pbuf_free(q);
Tuxitheone 0:ecaf3e593122 242 }
Tuxitheone 0:ecaf3e593122 243 return ERR_RTE;
Tuxitheone 0:ecaf3e593122 244 }
Tuxitheone 0:ecaf3e593122 245
Tuxitheone 0:ecaf3e593122 246 #if IP_SOF_BROADCAST
Tuxitheone 0:ecaf3e593122 247 /* broadcast filter? */
Tuxitheone 0:ecaf3e593122 248 if (((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(ipaddr, netif)) {
Tuxitheone 0:ecaf3e593122 249 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
Tuxitheone 0:ecaf3e593122 250 /* free any temporary header pbuf allocated by pbuf_header() */
Tuxitheone 0:ecaf3e593122 251 if (q != p) {
Tuxitheone 0:ecaf3e593122 252 pbuf_free(q);
Tuxitheone 0:ecaf3e593122 253 }
Tuxitheone 0:ecaf3e593122 254 return ERR_VAL;
Tuxitheone 0:ecaf3e593122 255 }
Tuxitheone 0:ecaf3e593122 256 #endif /* IP_SOF_BROADCAST */
Tuxitheone 0:ecaf3e593122 257
Tuxitheone 0:ecaf3e593122 258 if (ip_addr_isany(&pcb->local_ip)) {
Tuxitheone 0:ecaf3e593122 259 /* use outgoing network interface IP address as source address */
Tuxitheone 0:ecaf3e593122 260 src_ip = &(netif->ip_addr);
Tuxitheone 0:ecaf3e593122 261 } else {
Tuxitheone 0:ecaf3e593122 262 /* use RAW PCB local IP address as source address */
Tuxitheone 0:ecaf3e593122 263 src_ip = &(pcb->local_ip);
Tuxitheone 0:ecaf3e593122 264 }
Tuxitheone 0:ecaf3e593122 265
Tuxitheone 0:ecaf3e593122 266 #if LWIP_NETIF_HWADDRHINT
Tuxitheone 0:ecaf3e593122 267 netif->addr_hint = &(pcb->addr_hint);
Tuxitheone 0:ecaf3e593122 268 #endif /* LWIP_NETIF_HWADDRHINT*/
Tuxitheone 0:ecaf3e593122 269 err = ip_output_if (q, src_ip, ipaddr, pcb->ttl, pcb->tos, pcb->protocol, netif);
Tuxitheone 0:ecaf3e593122 270 #if LWIP_NETIF_HWADDRHINT
Tuxitheone 0:ecaf3e593122 271 netif->addr_hint = NULL;
Tuxitheone 0:ecaf3e593122 272 #endif /* LWIP_NETIF_HWADDRHINT*/
Tuxitheone 0:ecaf3e593122 273
Tuxitheone 0:ecaf3e593122 274 /* did we chain a header earlier? */
Tuxitheone 0:ecaf3e593122 275 if (q != p) {
Tuxitheone 0:ecaf3e593122 276 /* free the header */
Tuxitheone 0:ecaf3e593122 277 pbuf_free(q);
Tuxitheone 0:ecaf3e593122 278 }
Tuxitheone 0:ecaf3e593122 279 return err;
Tuxitheone 0:ecaf3e593122 280 }
Tuxitheone 0:ecaf3e593122 281
Tuxitheone 0:ecaf3e593122 282 /**
Tuxitheone 0:ecaf3e593122 283 * Send the raw IP packet to the address given by raw_connect()
Tuxitheone 0:ecaf3e593122 284 *
Tuxitheone 0:ecaf3e593122 285 * @param pcb the raw pcb which to send
Tuxitheone 0:ecaf3e593122 286 * @param p the IP payload to send
Tuxitheone 0:ecaf3e593122 287 *
Tuxitheone 0:ecaf3e593122 288 */
Tuxitheone 0:ecaf3e593122 289 err_t
Tuxitheone 0:ecaf3e593122 290 raw_send(struct raw_pcb *pcb, struct pbuf *p)
Tuxitheone 0:ecaf3e593122 291 {
Tuxitheone 0:ecaf3e593122 292 return raw_sendto(pcb, p, &pcb->remote_ip);
Tuxitheone 0:ecaf3e593122 293 }
Tuxitheone 0:ecaf3e593122 294
Tuxitheone 0:ecaf3e593122 295 /**
Tuxitheone 0:ecaf3e593122 296 * Remove an RAW PCB.
Tuxitheone 0:ecaf3e593122 297 *
Tuxitheone 0:ecaf3e593122 298 * @param pcb RAW PCB to be removed. The PCB is removed from the list of
Tuxitheone 0:ecaf3e593122 299 * RAW PCB's and the data structure is freed from memory.
Tuxitheone 0:ecaf3e593122 300 *
Tuxitheone 0:ecaf3e593122 301 * @see raw_new()
Tuxitheone 0:ecaf3e593122 302 */
Tuxitheone 0:ecaf3e593122 303 void
Tuxitheone 0:ecaf3e593122 304 raw_remove(struct raw_pcb *pcb)
Tuxitheone 0:ecaf3e593122 305 {
Tuxitheone 0:ecaf3e593122 306 struct raw_pcb *pcb2;
Tuxitheone 0:ecaf3e593122 307 /* pcb to be removed is first in list? */
Tuxitheone 0:ecaf3e593122 308 if (raw_pcbs == pcb) {
Tuxitheone 0:ecaf3e593122 309 /* make list start at 2nd pcb */
Tuxitheone 0:ecaf3e593122 310 raw_pcbs = raw_pcbs->next;
Tuxitheone 0:ecaf3e593122 311 /* pcb not 1st in list */
Tuxitheone 0:ecaf3e593122 312 } else {
Tuxitheone 0:ecaf3e593122 313 for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
Tuxitheone 0:ecaf3e593122 314 /* find pcb in raw_pcbs list */
Tuxitheone 0:ecaf3e593122 315 if (pcb2->next != NULL && pcb2->next == pcb) {
Tuxitheone 0:ecaf3e593122 316 /* remove pcb from list */
Tuxitheone 0:ecaf3e593122 317 pcb2->next = pcb->next;
Tuxitheone 0:ecaf3e593122 318 }
Tuxitheone 0:ecaf3e593122 319 }
Tuxitheone 0:ecaf3e593122 320 }
Tuxitheone 0:ecaf3e593122 321 memp_free(MEMP_RAW_PCB, pcb);
Tuxitheone 0:ecaf3e593122 322 }
Tuxitheone 0:ecaf3e593122 323
Tuxitheone 0:ecaf3e593122 324 /**
Tuxitheone 0:ecaf3e593122 325 * Create a RAW PCB.
Tuxitheone 0:ecaf3e593122 326 *
Tuxitheone 0:ecaf3e593122 327 * @return The RAW PCB which was created. NULL if the PCB data structure
Tuxitheone 0:ecaf3e593122 328 * could not be allocated.
Tuxitheone 0:ecaf3e593122 329 *
Tuxitheone 0:ecaf3e593122 330 * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
Tuxitheone 0:ecaf3e593122 331 *
Tuxitheone 0:ecaf3e593122 332 * @see raw_remove()
Tuxitheone 0:ecaf3e593122 333 */
Tuxitheone 0:ecaf3e593122 334 struct raw_pcb *
Tuxitheone 0:ecaf3e593122 335 raw_new(u8_t proto)
Tuxitheone 0:ecaf3e593122 336 {
Tuxitheone 0:ecaf3e593122 337 struct raw_pcb *pcb;
Tuxitheone 0:ecaf3e593122 338
Tuxitheone 0:ecaf3e593122 339 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
Tuxitheone 0:ecaf3e593122 340
Tuxitheone 0:ecaf3e593122 341 pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
Tuxitheone 0:ecaf3e593122 342 /* could allocate RAW PCB? */
Tuxitheone 0:ecaf3e593122 343 if (pcb != NULL) {
Tuxitheone 0:ecaf3e593122 344 /* initialize PCB to all zeroes */
Tuxitheone 0:ecaf3e593122 345 memset(pcb, 0, sizeof(struct raw_pcb));
Tuxitheone 0:ecaf3e593122 346 pcb->protocol = proto;
Tuxitheone 0:ecaf3e593122 347 pcb->ttl = RAW_TTL;
Tuxitheone 0:ecaf3e593122 348 pcb->next = raw_pcbs;
Tuxitheone 0:ecaf3e593122 349 raw_pcbs = pcb;
Tuxitheone 0:ecaf3e593122 350 }
Tuxitheone 0:ecaf3e593122 351 return pcb;
Tuxitheone 0:ecaf3e593122 352 }
Tuxitheone 0:ecaf3e593122 353
Tuxitheone 0:ecaf3e593122 354 #endif /* LWIP_RAW */