Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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