Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependents:   STM32F746_iothub_client_sample_mqtt DISCO-F746NG_Ethernet Nucleo_F746ZG_Ethernet thethingsiO-DISCO_F746NG-mqtt ... more

Committer:
DieterGraef
Date:
Thu Jun 23 09:04:23 2016 +0000
Revision:
1:28ba13dd96f7
Parent:
0:d26c1b55cfca
corrected MAC issue. The MAC is now 02:00:00:xx:xx:xx where xx is the sum over the unique device register

Who changed what in which revision?

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