Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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