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