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