Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Thu Feb 23 21:38:39 2012 +0000
Revision:
0:616601bde9fb

        

Who changed what in which revision?

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