Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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