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