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