Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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