Port of LwIP performed by Ralf in 2010. Not recommended for use with recent mbed libraries, but good demos of raw LwIP possible

Dependents:   LwIP_raw_API_serverExample tiny-dtls

Committer:
RodColeman
Date:
Tue Sep 18 14:41:24 2012 +0000
Revision:
0:0791c1fece8e
[mbed] converted /Eth_TCP_Wei_Server/lwip

Who changed what in which revision?

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