Official mbed lwIP library (version 1.4.0)

Dependents:   LwIPNetworking NetServicesMin EthernetInterface EthernetInterface_RSF ... more

Legacy Networking Libraries

This is an mbed 2 networking library. For mbed OS 5, lwip has been integrated with built-in networking interfaces. The networking libraries have been revised to better support additional network stacks and thread safety here.

This library is based on the code of lwIP v1.4.0

Copyright (c) 2001, 2002 Swedish Institute of Computer Science.
All rights reserved. 

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
Committer:
mbed_official
Date:
Mon Mar 14 16:15:36 2016 +0000
Revision:
20:08f08bfc3f3d
Parent:
0:51ac1d130fd4
Synchronized with git revision fec574a5ed6db26aca1b13992ff271bf527d4a0d

Full URL: https://github.com/mbedmicro/mbed/commit/fec574a5ed6db26aca1b13992ff271bf527d4a0d/

Increased allocated netbufs to handle DTLS handshakes

Who changed what in which revision?

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