Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samdanbury 6:37b6d0d56190 1 /**
samdanbury 6:37b6d0d56190 2 * @file
samdanbury 6:37b6d0d56190 3 * lwIP network interface abstraction
samdanbury 6:37b6d0d56190 4 *
samdanbury 6:37b6d0d56190 5 */
samdanbury 6:37b6d0d56190 6
samdanbury 6:37b6d0d56190 7 /*
samdanbury 6:37b6d0d56190 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
samdanbury 6:37b6d0d56190 9 * All rights reserved.
samdanbury 6:37b6d0d56190 10 *
samdanbury 6:37b6d0d56190 11 * Redistribution and use in source and binary forms, with or without modification,
samdanbury 6:37b6d0d56190 12 * are permitted provided that the following conditions are met:
samdanbury 6:37b6d0d56190 13 *
samdanbury 6:37b6d0d56190 14 * 1. Redistributions of source code must retain the above copyright notice,
samdanbury 6:37b6d0d56190 15 * this list of conditions and the following disclaimer.
samdanbury 6:37b6d0d56190 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
samdanbury 6:37b6d0d56190 17 * this list of conditions and the following disclaimer in the documentation
samdanbury 6:37b6d0d56190 18 * and/or other materials provided with the distribution.
samdanbury 6:37b6d0d56190 19 * 3. The name of the author may not be used to endorse or promote products
samdanbury 6:37b6d0d56190 20 * derived from this software without specific prior written permission.
samdanbury 6:37b6d0d56190 21 *
samdanbury 6:37b6d0d56190 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
samdanbury 6:37b6d0d56190 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
samdanbury 6:37b6d0d56190 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
samdanbury 6:37b6d0d56190 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
samdanbury 6:37b6d0d56190 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
samdanbury 6:37b6d0d56190 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
samdanbury 6:37b6d0d56190 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
samdanbury 6:37b6d0d56190 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
samdanbury 6:37b6d0d56190 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
samdanbury 6:37b6d0d56190 31 * OF SUCH DAMAGE.
samdanbury 6:37b6d0d56190 32 *
samdanbury 6:37b6d0d56190 33 * This file is part of the lwIP TCP/IP stack.
samdanbury 6:37b6d0d56190 34 *
samdanbury 6:37b6d0d56190 35 * Author: Adam Dunkels <adam@sics.se>
samdanbury 6:37b6d0d56190 36 *
samdanbury 6:37b6d0d56190 37 */
samdanbury 6:37b6d0d56190 38
samdanbury 6:37b6d0d56190 39 #include "lwip/opt.h"
samdanbury 6:37b6d0d56190 40
samdanbury 6:37b6d0d56190 41 #include "lwip/def.h"
samdanbury 6:37b6d0d56190 42 #include "lwip/ip_addr.h"
samdanbury 6:37b6d0d56190 43 #include "lwip/netif.h"
samdanbury 6:37b6d0d56190 44 #include "lwip/tcp_impl.h"
samdanbury 6:37b6d0d56190 45 #include "lwip/snmp.h"
samdanbury 6:37b6d0d56190 46 #include "lwip/igmp.h"
samdanbury 6:37b6d0d56190 47 #include "netif/etharp.h"
samdanbury 6:37b6d0d56190 48 #include "lwip/stats.h"
samdanbury 6:37b6d0d56190 49 #if ENABLE_LOOPBACK
samdanbury 6:37b6d0d56190 50 #include "lwip/sys.h"
samdanbury 6:37b6d0d56190 51 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
samdanbury 6:37b6d0d56190 52 #include "lwip/tcpip.h"
samdanbury 6:37b6d0d56190 53 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
samdanbury 6:37b6d0d56190 54 #endif /* ENABLE_LOOPBACK */
samdanbury 6:37b6d0d56190 55
samdanbury 6:37b6d0d56190 56 #if LWIP_AUTOIP
samdanbury 6:37b6d0d56190 57 #include "lwip/autoip.h"
samdanbury 6:37b6d0d56190 58 #endif /* LWIP_AUTOIP */
samdanbury 6:37b6d0d56190 59 #if LWIP_DHCP
samdanbury 6:37b6d0d56190 60 #include "lwip/dhcp.h"
samdanbury 6:37b6d0d56190 61 #endif /* LWIP_DHCP */
samdanbury 6:37b6d0d56190 62
samdanbury 6:37b6d0d56190 63 #if LWIP_NETIF_STATUS_CALLBACK
samdanbury 6:37b6d0d56190 64 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
samdanbury 6:37b6d0d56190 65 #else
samdanbury 6:37b6d0d56190 66 #define NETIF_STATUS_CALLBACK(n)
samdanbury 6:37b6d0d56190 67 #endif /* LWIP_NETIF_STATUS_CALLBACK */
samdanbury 6:37b6d0d56190 68
samdanbury 6:37b6d0d56190 69 #if LWIP_NETIF_LINK_CALLBACK
samdanbury 6:37b6d0d56190 70 #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
samdanbury 6:37b6d0d56190 71 #else
samdanbury 6:37b6d0d56190 72 #define NETIF_LINK_CALLBACK(n)
samdanbury 6:37b6d0d56190 73 #endif /* LWIP_NETIF_LINK_CALLBACK */
samdanbury 6:37b6d0d56190 74
samdanbury 6:37b6d0d56190 75 struct netif *netif_list;
samdanbury 6:37b6d0d56190 76 struct netif *netif_default;
samdanbury 6:37b6d0d56190 77
samdanbury 6:37b6d0d56190 78 #if LWIP_HAVE_LOOPIF
samdanbury 6:37b6d0d56190 79 static struct netif loop_netif;
samdanbury 6:37b6d0d56190 80
samdanbury 6:37b6d0d56190 81 /**
samdanbury 6:37b6d0d56190 82 * Initialize a lwip network interface structure for a loopback interface
samdanbury 6:37b6d0d56190 83 *
samdanbury 6:37b6d0d56190 84 * @param netif the lwip network interface structure for this loopif
samdanbury 6:37b6d0d56190 85 * @return ERR_OK if the loopif is initialized
samdanbury 6:37b6d0d56190 86 * ERR_MEM if private data couldn't be allocated
samdanbury 6:37b6d0d56190 87 */
samdanbury 6:37b6d0d56190 88 static err_t
samdanbury 6:37b6d0d56190 89 netif_loopif_init(struct netif *netif)
samdanbury 6:37b6d0d56190 90 {
samdanbury 6:37b6d0d56190 91 /* initialize the snmp variables and counters inside the struct netif
samdanbury 6:37b6d0d56190 92 * ifSpeed: no assumption can be made!
samdanbury 6:37b6d0d56190 93 */
samdanbury 6:37b6d0d56190 94 NETIF_INIT_SNMP(netif, snmp_ifType_softwareLoopback, 0);
samdanbury 6:37b6d0d56190 95
samdanbury 6:37b6d0d56190 96 netif->name[0] = 'l';
samdanbury 6:37b6d0d56190 97 netif->name[1] = 'o';
samdanbury 6:37b6d0d56190 98 netif->output = netif_loop_output;
samdanbury 6:37b6d0d56190 99 return ERR_OK;
samdanbury 6:37b6d0d56190 100 }
samdanbury 6:37b6d0d56190 101 #endif /* LWIP_HAVE_LOOPIF */
samdanbury 6:37b6d0d56190 102
samdanbury 6:37b6d0d56190 103 void
samdanbury 6:37b6d0d56190 104 netif_init(void)
samdanbury 6:37b6d0d56190 105 {
samdanbury 6:37b6d0d56190 106 #if LWIP_HAVE_LOOPIF
samdanbury 6:37b6d0d56190 107 ip_addr_t loop_ipaddr, loop_netmask, loop_gw;
samdanbury 6:37b6d0d56190 108 IP4_ADDR(&loop_gw, 127,0,0,1);
samdanbury 6:37b6d0d56190 109 IP4_ADDR(&loop_ipaddr, 127,0,0,1);
samdanbury 6:37b6d0d56190 110 IP4_ADDR(&loop_netmask, 255,0,0,0);
samdanbury 6:37b6d0d56190 111
samdanbury 6:37b6d0d56190 112 #if NO_SYS
samdanbury 6:37b6d0d56190 113 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input);
samdanbury 6:37b6d0d56190 114 #else /* NO_SYS */
samdanbury 6:37b6d0d56190 115 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input);
samdanbury 6:37b6d0d56190 116 #endif /* NO_SYS */
samdanbury 6:37b6d0d56190 117 netif_set_up(&loop_netif);
samdanbury 6:37b6d0d56190 118
samdanbury 6:37b6d0d56190 119 #endif /* LWIP_HAVE_LOOPIF */
samdanbury 6:37b6d0d56190 120 }
samdanbury 6:37b6d0d56190 121
samdanbury 6:37b6d0d56190 122 /**
samdanbury 6:37b6d0d56190 123 * Add a network interface to the list of lwIP netifs.
samdanbury 6:37b6d0d56190 124 *
samdanbury 6:37b6d0d56190 125 * @param netif a pre-allocated netif structure
samdanbury 6:37b6d0d56190 126 * @param ipaddr IP address for the new netif
samdanbury 6:37b6d0d56190 127 * @param netmask network mask for the new netif
samdanbury 6:37b6d0d56190 128 * @param gw default gateway IP address for the new netif
samdanbury 6:37b6d0d56190 129 * @param state opaque data passed to the new netif
samdanbury 6:37b6d0d56190 130 * @param init callback function that initializes the interface
samdanbury 6:37b6d0d56190 131 * @param input callback function that is called to pass
samdanbury 6:37b6d0d56190 132 * ingress packets up in the protocol layer stack.
samdanbury 6:37b6d0d56190 133 *
samdanbury 6:37b6d0d56190 134 * @return netif, or NULL if failed.
samdanbury 6:37b6d0d56190 135 */
samdanbury 6:37b6d0d56190 136 struct netif *
samdanbury 6:37b6d0d56190 137 netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
samdanbury 6:37b6d0d56190 138 ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
samdanbury 6:37b6d0d56190 139 {
samdanbury 6:37b6d0d56190 140 static u8_t netifnum = 0;
samdanbury 6:37b6d0d56190 141
samdanbury 6:37b6d0d56190 142 LWIP_ASSERT("No init function given", init != NULL);
samdanbury 6:37b6d0d56190 143
samdanbury 6:37b6d0d56190 144 /* reset new interface configuration state */
samdanbury 6:37b6d0d56190 145 ip_addr_set_zero(&netif->ip_addr);
samdanbury 6:37b6d0d56190 146 ip_addr_set_zero(&netif->netmask);
samdanbury 6:37b6d0d56190 147 ip_addr_set_zero(&netif->gw);
samdanbury 6:37b6d0d56190 148 netif->flags = 0;
samdanbury 6:37b6d0d56190 149 #if LWIP_DHCP
samdanbury 6:37b6d0d56190 150 /* netif not under DHCP control by default */
samdanbury 6:37b6d0d56190 151 netif->dhcp = NULL;
samdanbury 6:37b6d0d56190 152 #endif /* LWIP_DHCP */
samdanbury 6:37b6d0d56190 153 #if LWIP_AUTOIP
samdanbury 6:37b6d0d56190 154 /* netif not under AutoIP control by default */
samdanbury 6:37b6d0d56190 155 netif->autoip = NULL;
samdanbury 6:37b6d0d56190 156 #endif /* LWIP_AUTOIP */
samdanbury 6:37b6d0d56190 157 #if LWIP_NETIF_STATUS_CALLBACK
samdanbury 6:37b6d0d56190 158 netif->status_callback = NULL;
samdanbury 6:37b6d0d56190 159 #endif /* LWIP_NETIF_STATUS_CALLBACK */
samdanbury 6:37b6d0d56190 160 #if LWIP_NETIF_LINK_CALLBACK
samdanbury 6:37b6d0d56190 161 netif->link_callback = NULL;
samdanbury 6:37b6d0d56190 162 #endif /* LWIP_NETIF_LINK_CALLBACK */
samdanbury 6:37b6d0d56190 163 #if LWIP_IGMP
samdanbury 6:37b6d0d56190 164 netif->igmp_mac_filter = NULL;
samdanbury 6:37b6d0d56190 165 #endif /* LWIP_IGMP */
samdanbury 6:37b6d0d56190 166 #if ENABLE_LOOPBACK
samdanbury 6:37b6d0d56190 167 netif->loop_first = NULL;
samdanbury 6:37b6d0d56190 168 netif->loop_last = NULL;
samdanbury 6:37b6d0d56190 169 #endif /* ENABLE_LOOPBACK */
samdanbury 6:37b6d0d56190 170
samdanbury 6:37b6d0d56190 171 /* remember netif specific state information data */
samdanbury 6:37b6d0d56190 172 netif->state = state;
samdanbury 6:37b6d0d56190 173 netif->num = netifnum++;
samdanbury 6:37b6d0d56190 174 netif->input = input;
samdanbury 6:37b6d0d56190 175 #if LWIP_NETIF_HWADDRHINT
samdanbury 6:37b6d0d56190 176 netif->addr_hint = NULL;
samdanbury 6:37b6d0d56190 177 #endif /* LWIP_NETIF_HWADDRHINT*/
samdanbury 6:37b6d0d56190 178 #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
samdanbury 6:37b6d0d56190 179 netif->loop_cnt_current = 0;
samdanbury 6:37b6d0d56190 180 #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
samdanbury 6:37b6d0d56190 181
samdanbury 6:37b6d0d56190 182 netif_set_addr(netif, ipaddr, netmask, gw);
samdanbury 6:37b6d0d56190 183
samdanbury 6:37b6d0d56190 184 /* call user specified initialization function for netif */
samdanbury 6:37b6d0d56190 185 if (init(netif) != ERR_OK) {
samdanbury 6:37b6d0d56190 186 return NULL;
samdanbury 6:37b6d0d56190 187 }
samdanbury 6:37b6d0d56190 188
samdanbury 6:37b6d0d56190 189 /* add this netif to the list */
samdanbury 6:37b6d0d56190 190 netif->next = netif_list;
samdanbury 6:37b6d0d56190 191 netif_list = netif;
samdanbury 6:37b6d0d56190 192 snmp_inc_iflist();
samdanbury 6:37b6d0d56190 193
samdanbury 6:37b6d0d56190 194 #if LWIP_IGMP
samdanbury 6:37b6d0d56190 195 /* start IGMP processing */
samdanbury 6:37b6d0d56190 196 if (netif->flags & NETIF_FLAG_IGMP) {
samdanbury 6:37b6d0d56190 197 igmp_start(netif);
samdanbury 6:37b6d0d56190 198 }
samdanbury 6:37b6d0d56190 199 #endif /* LWIP_IGMP */
samdanbury 6:37b6d0d56190 200
samdanbury 6:37b6d0d56190 201 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
samdanbury 6:37b6d0d56190 202 netif->name[0], netif->name[1]));
samdanbury 6:37b6d0d56190 203 ip_addr_debug_print(NETIF_DEBUG, ipaddr);
samdanbury 6:37b6d0d56190 204 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
samdanbury 6:37b6d0d56190 205 ip_addr_debug_print(NETIF_DEBUG, netmask);
samdanbury 6:37b6d0d56190 206 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
samdanbury 6:37b6d0d56190 207 ip_addr_debug_print(NETIF_DEBUG, gw);
samdanbury 6:37b6d0d56190 208 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
samdanbury 6:37b6d0d56190 209 return netif;
samdanbury 6:37b6d0d56190 210 }
samdanbury 6:37b6d0d56190 211
samdanbury 6:37b6d0d56190 212 /**
samdanbury 6:37b6d0d56190 213 * Change IP address configuration for a network interface (including netmask
samdanbury 6:37b6d0d56190 214 * and default gateway).
samdanbury 6:37b6d0d56190 215 *
samdanbury 6:37b6d0d56190 216 * @param netif the network interface to change
samdanbury 6:37b6d0d56190 217 * @param ipaddr the new IP address
samdanbury 6:37b6d0d56190 218 * @param netmask the new netmask
samdanbury 6:37b6d0d56190 219 * @param gw the new default gateway
samdanbury 6:37b6d0d56190 220 */
samdanbury 6:37b6d0d56190 221 void
samdanbury 6:37b6d0d56190 222 netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
samdanbury 6:37b6d0d56190 223 ip_addr_t *gw)
samdanbury 6:37b6d0d56190 224 {
samdanbury 6:37b6d0d56190 225 netif_set_ipaddr(netif, ipaddr);
samdanbury 6:37b6d0d56190 226 netif_set_netmask(netif, netmask);
samdanbury 6:37b6d0d56190 227 netif_set_gw(netif, gw);
samdanbury 6:37b6d0d56190 228 }
samdanbury 6:37b6d0d56190 229
samdanbury 6:37b6d0d56190 230 /**
samdanbury 6:37b6d0d56190 231 * Remove a network interface from the list of lwIP netifs.
samdanbury 6:37b6d0d56190 232 *
samdanbury 6:37b6d0d56190 233 * @param netif the network interface to remove
samdanbury 6:37b6d0d56190 234 */
samdanbury 6:37b6d0d56190 235 void
samdanbury 6:37b6d0d56190 236 netif_remove(struct netif *netif)
samdanbury 6:37b6d0d56190 237 {
samdanbury 6:37b6d0d56190 238 if (netif == NULL) {
samdanbury 6:37b6d0d56190 239 return;
samdanbury 6:37b6d0d56190 240 }
samdanbury 6:37b6d0d56190 241
samdanbury 6:37b6d0d56190 242 #if LWIP_IGMP
samdanbury 6:37b6d0d56190 243 /* stop IGMP processing */
samdanbury 6:37b6d0d56190 244 if (netif->flags & NETIF_FLAG_IGMP) {
samdanbury 6:37b6d0d56190 245 igmp_stop(netif);
samdanbury 6:37b6d0d56190 246 }
samdanbury 6:37b6d0d56190 247 #endif /* LWIP_IGMP */
samdanbury 6:37b6d0d56190 248 if (netif_is_up(netif)) {
samdanbury 6:37b6d0d56190 249 /* set netif down before removing (call callback function) */
samdanbury 6:37b6d0d56190 250 netif_set_down(netif);
samdanbury 6:37b6d0d56190 251 }
samdanbury 6:37b6d0d56190 252
samdanbury 6:37b6d0d56190 253 snmp_delete_ipaddridx_tree(netif);
samdanbury 6:37b6d0d56190 254
samdanbury 6:37b6d0d56190 255 /* is it the first netif? */
samdanbury 6:37b6d0d56190 256 if (netif_list == netif) {
samdanbury 6:37b6d0d56190 257 netif_list = netif->next;
samdanbury 6:37b6d0d56190 258 } else {
samdanbury 6:37b6d0d56190 259 /* look for netif further down the list */
samdanbury 6:37b6d0d56190 260 struct netif * tmpNetif;
samdanbury 6:37b6d0d56190 261 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
samdanbury 6:37b6d0d56190 262 if (tmpNetif->next == netif) {
samdanbury 6:37b6d0d56190 263 tmpNetif->next = netif->next;
samdanbury 6:37b6d0d56190 264 break;
samdanbury 6:37b6d0d56190 265 }
samdanbury 6:37b6d0d56190 266 }
samdanbury 6:37b6d0d56190 267 if (tmpNetif == NULL)
samdanbury 6:37b6d0d56190 268 return; /* we didn't find any netif today */
samdanbury 6:37b6d0d56190 269 }
samdanbury 6:37b6d0d56190 270 snmp_dec_iflist();
samdanbury 6:37b6d0d56190 271 /* this netif is default? */
samdanbury 6:37b6d0d56190 272 if (netif_default == netif) {
samdanbury 6:37b6d0d56190 273 /* reset default netif */
samdanbury 6:37b6d0d56190 274 netif_set_default(NULL);
samdanbury 6:37b6d0d56190 275 }
samdanbury 6:37b6d0d56190 276 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
samdanbury 6:37b6d0d56190 277 }
samdanbury 6:37b6d0d56190 278
samdanbury 6:37b6d0d56190 279 /**
samdanbury 6:37b6d0d56190 280 * Find a network interface by searching for its name
samdanbury 6:37b6d0d56190 281 *
samdanbury 6:37b6d0d56190 282 * @param name the name of the netif (like netif->name) plus concatenated number
samdanbury 6:37b6d0d56190 283 * in ascii representation (e.g. 'en0')
samdanbury 6:37b6d0d56190 284 */
samdanbury 6:37b6d0d56190 285 struct netif *
samdanbury 6:37b6d0d56190 286 netif_find(char *name)
samdanbury 6:37b6d0d56190 287 {
samdanbury 6:37b6d0d56190 288 struct netif *netif;
samdanbury 6:37b6d0d56190 289 u8_t num;
samdanbury 6:37b6d0d56190 290
samdanbury 6:37b6d0d56190 291 if (name == NULL) {
samdanbury 6:37b6d0d56190 292 return NULL;
samdanbury 6:37b6d0d56190 293 }
samdanbury 6:37b6d0d56190 294
samdanbury 6:37b6d0d56190 295 num = name[2] - '0';
samdanbury 6:37b6d0d56190 296
samdanbury 6:37b6d0d56190 297 for(netif = netif_list; netif != NULL; netif = netif->next) {
samdanbury 6:37b6d0d56190 298 if (num == netif->num &&
samdanbury 6:37b6d0d56190 299 name[0] == netif->name[0] &&
samdanbury 6:37b6d0d56190 300 name[1] == netif->name[1]) {
samdanbury 6:37b6d0d56190 301 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
samdanbury 6:37b6d0d56190 302 return netif;
samdanbury 6:37b6d0d56190 303 }
samdanbury 6:37b6d0d56190 304 }
samdanbury 6:37b6d0d56190 305 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
samdanbury 6:37b6d0d56190 306 return NULL;
samdanbury 6:37b6d0d56190 307 }
samdanbury 6:37b6d0d56190 308
samdanbury 6:37b6d0d56190 309 /**
samdanbury 6:37b6d0d56190 310 * Change the IP address of a network interface
samdanbury 6:37b6d0d56190 311 *
samdanbury 6:37b6d0d56190 312 * @param netif the network interface to change
samdanbury 6:37b6d0d56190 313 * @param ipaddr the new IP address
samdanbury 6:37b6d0d56190 314 *
samdanbury 6:37b6d0d56190 315 * @note call netif_set_addr() if you also want to change netmask and
samdanbury 6:37b6d0d56190 316 * default gateway
samdanbury 6:37b6d0d56190 317 */
samdanbury 6:37b6d0d56190 318 void
samdanbury 6:37b6d0d56190 319 netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
samdanbury 6:37b6d0d56190 320 {
samdanbury 6:37b6d0d56190 321 /* Protect against dereferencing NULL pointers by
samdanbury 6:37b6d0d56190 322 treating like ANY, as does ip_addr_set() */
samdanbury 6:37b6d0d56190 323 if (!ipaddr) {
samdanbury 6:37b6d0d56190 324 ipaddr = IP_ADDR_ANY;
samdanbury 6:37b6d0d56190 325 }
samdanbury 6:37b6d0d56190 326
samdanbury 6:37b6d0d56190 327 /* TODO: Handling of obsolete pcbs */
samdanbury 6:37b6d0d56190 328 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
samdanbury 6:37b6d0d56190 329 #if LWIP_TCP
samdanbury 6:37b6d0d56190 330 struct tcp_pcb *pcb;
samdanbury 6:37b6d0d56190 331 struct tcp_pcb_listen *lpcb;
samdanbury 6:37b6d0d56190 332
samdanbury 6:37b6d0d56190 333 /* address is actually being changed? */
samdanbury 6:37b6d0d56190 334 if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) {
samdanbury 6:37b6d0d56190 335 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
samdanbury 6:37b6d0d56190 336 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
samdanbury 6:37b6d0d56190 337 pcb = tcp_active_pcbs;
samdanbury 6:37b6d0d56190 338 while (pcb != NULL) {
samdanbury 6:37b6d0d56190 339 /* PCB bound to current local interface address? */
samdanbury 6:37b6d0d56190 340 if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))
samdanbury 6:37b6d0d56190 341 #if LWIP_AUTOIP
samdanbury 6:37b6d0d56190 342 /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
samdanbury 6:37b6d0d56190 343 && !ip_addr_islinklocal(&(pcb->local_ip))
samdanbury 6:37b6d0d56190 344 #endif /* LWIP_AUTOIP */
samdanbury 6:37b6d0d56190 345 ) {
samdanbury 6:37b6d0d56190 346 /* this connection must be aborted */
samdanbury 6:37b6d0d56190 347 struct tcp_pcb *next = pcb->next;
samdanbury 6:37b6d0d56190 348 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
samdanbury 6:37b6d0d56190 349 tcp_abort(pcb);
samdanbury 6:37b6d0d56190 350 pcb = next;
samdanbury 6:37b6d0d56190 351 } else {
samdanbury 6:37b6d0d56190 352 pcb = pcb->next;
samdanbury 6:37b6d0d56190 353 }
samdanbury 6:37b6d0d56190 354 }
samdanbury 6:37b6d0d56190 355 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
samdanbury 6:37b6d0d56190 356 /* PCB bound to current local interface address? */
samdanbury 6:37b6d0d56190 357 if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
samdanbury 6:37b6d0d56190 358 (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
samdanbury 6:37b6d0d56190 359 /* The PCB is listening to the old ipaddr and
samdanbury 6:37b6d0d56190 360 * is set to listen to the new one instead */
samdanbury 6:37b6d0d56190 361 ip_addr_set(&(lpcb->local_ip), ipaddr);
samdanbury 6:37b6d0d56190 362 }
samdanbury 6:37b6d0d56190 363 }
samdanbury 6:37b6d0d56190 364 }
samdanbury 6:37b6d0d56190 365 #endif
samdanbury 6:37b6d0d56190 366 snmp_delete_ipaddridx_tree(netif);
samdanbury 6:37b6d0d56190 367 snmp_delete_iprteidx_tree(0,netif);
samdanbury 6:37b6d0d56190 368 /* set new IP address to netif */
samdanbury 6:37b6d0d56190 369 ip_addr_set(&(netif->ip_addr), ipaddr);
samdanbury 6:37b6d0d56190 370 snmp_insert_ipaddridx_tree(netif);
samdanbury 6:37b6d0d56190 371 snmp_insert_iprteidx_tree(0,netif);
samdanbury 6:37b6d0d56190 372
samdanbury 6:37b6d0d56190 373 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
samdanbury 6:37b6d0d56190 374 netif->name[0], netif->name[1],
samdanbury 6:37b6d0d56190 375 ip4_addr1_16(&netif->ip_addr),
samdanbury 6:37b6d0d56190 376 ip4_addr2_16(&netif->ip_addr),
samdanbury 6:37b6d0d56190 377 ip4_addr3_16(&netif->ip_addr),
samdanbury 6:37b6d0d56190 378 ip4_addr4_16(&netif->ip_addr)));
samdanbury 6:37b6d0d56190 379 }
samdanbury 6:37b6d0d56190 380
samdanbury 6:37b6d0d56190 381 /**
samdanbury 6:37b6d0d56190 382 * Change the default gateway for a network interface
samdanbury 6:37b6d0d56190 383 *
samdanbury 6:37b6d0d56190 384 * @param netif the network interface to change
samdanbury 6:37b6d0d56190 385 * @param gw the new default gateway
samdanbury 6:37b6d0d56190 386 *
samdanbury 6:37b6d0d56190 387 * @note call netif_set_addr() if you also want to change ip address and netmask
samdanbury 6:37b6d0d56190 388 */
samdanbury 6:37b6d0d56190 389 void
samdanbury 6:37b6d0d56190 390 netif_set_gw(struct netif *netif, ip_addr_t *gw)
samdanbury 6:37b6d0d56190 391 {
samdanbury 6:37b6d0d56190 392 ip_addr_set(&(netif->gw), gw);
samdanbury 6:37b6d0d56190 393 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
samdanbury 6:37b6d0d56190 394 netif->name[0], netif->name[1],
samdanbury 6:37b6d0d56190 395 ip4_addr1_16(&netif->gw),
samdanbury 6:37b6d0d56190 396 ip4_addr2_16(&netif->gw),
samdanbury 6:37b6d0d56190 397 ip4_addr3_16(&netif->gw),
samdanbury 6:37b6d0d56190 398 ip4_addr4_16(&netif->gw)));
samdanbury 6:37b6d0d56190 399 }
samdanbury 6:37b6d0d56190 400
samdanbury 6:37b6d0d56190 401 /**
samdanbury 6:37b6d0d56190 402 * Change the netmask of a network interface
samdanbury 6:37b6d0d56190 403 *
samdanbury 6:37b6d0d56190 404 * @param netif the network interface to change
samdanbury 6:37b6d0d56190 405 * @param netmask the new netmask
samdanbury 6:37b6d0d56190 406 *
samdanbury 6:37b6d0d56190 407 * @note call netif_set_addr() if you also want to change ip address and
samdanbury 6:37b6d0d56190 408 * default gateway
samdanbury 6:37b6d0d56190 409 */
samdanbury 6:37b6d0d56190 410 void
samdanbury 6:37b6d0d56190 411 netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
samdanbury 6:37b6d0d56190 412 {
samdanbury 6:37b6d0d56190 413 snmp_delete_iprteidx_tree(0, netif);
samdanbury 6:37b6d0d56190 414 /* set new netmask to netif */
samdanbury 6:37b6d0d56190 415 ip_addr_set(&(netif->netmask), netmask);
samdanbury 6:37b6d0d56190 416 snmp_insert_iprteidx_tree(0, netif);
samdanbury 6:37b6d0d56190 417 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
samdanbury 6:37b6d0d56190 418 netif->name[0], netif->name[1],
samdanbury 6:37b6d0d56190 419 ip4_addr1_16(&netif->netmask),
samdanbury 6:37b6d0d56190 420 ip4_addr2_16(&netif->netmask),
samdanbury 6:37b6d0d56190 421 ip4_addr3_16(&netif->netmask),
samdanbury 6:37b6d0d56190 422 ip4_addr4_16(&netif->netmask)));
samdanbury 6:37b6d0d56190 423 }
samdanbury 6:37b6d0d56190 424
samdanbury 6:37b6d0d56190 425 /**
samdanbury 6:37b6d0d56190 426 * Set a network interface as the default network interface
samdanbury 6:37b6d0d56190 427 * (used to output all packets for which no specific route is found)
samdanbury 6:37b6d0d56190 428 *
samdanbury 6:37b6d0d56190 429 * @param netif the default network interface
samdanbury 6:37b6d0d56190 430 */
samdanbury 6:37b6d0d56190 431 void
samdanbury 6:37b6d0d56190 432 netif_set_default(struct netif *netif)
samdanbury 6:37b6d0d56190 433 {
samdanbury 6:37b6d0d56190 434 if (netif == NULL) {
samdanbury 6:37b6d0d56190 435 /* remove default route */
samdanbury 6:37b6d0d56190 436 snmp_delete_iprteidx_tree(1, netif);
samdanbury 6:37b6d0d56190 437 } else {
samdanbury 6:37b6d0d56190 438 /* install default route */
samdanbury 6:37b6d0d56190 439 snmp_insert_iprteidx_tree(1, netif);
samdanbury 6:37b6d0d56190 440 }
samdanbury 6:37b6d0d56190 441 netif_default = netif;
samdanbury 6:37b6d0d56190 442 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
samdanbury 6:37b6d0d56190 443 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
samdanbury 6:37b6d0d56190 444 }
samdanbury 6:37b6d0d56190 445
samdanbury 6:37b6d0d56190 446 /**
samdanbury 6:37b6d0d56190 447 * Bring an interface up, available for processing
samdanbury 6:37b6d0d56190 448 * traffic.
samdanbury 6:37b6d0d56190 449 *
samdanbury 6:37b6d0d56190 450 * @note: Enabling DHCP on a down interface will make it come
samdanbury 6:37b6d0d56190 451 * up once configured.
samdanbury 6:37b6d0d56190 452 *
samdanbury 6:37b6d0d56190 453 * @see dhcp_start()
samdanbury 6:37b6d0d56190 454 */
samdanbury 6:37b6d0d56190 455 void netif_set_up(struct netif *netif)
samdanbury 6:37b6d0d56190 456 {
samdanbury 6:37b6d0d56190 457 if (!(netif->flags & NETIF_FLAG_UP)) {
samdanbury 6:37b6d0d56190 458 netif->flags |= NETIF_FLAG_UP;
samdanbury 6:37b6d0d56190 459
samdanbury 6:37b6d0d56190 460 #if LWIP_SNMP
samdanbury 6:37b6d0d56190 461 snmp_get_sysuptime(&netif->ts);
samdanbury 6:37b6d0d56190 462 #endif /* LWIP_SNMP */
samdanbury 6:37b6d0d56190 463
samdanbury 6:37b6d0d56190 464 NETIF_STATUS_CALLBACK(netif);
samdanbury 6:37b6d0d56190 465
samdanbury 6:37b6d0d56190 466 if (netif->flags & NETIF_FLAG_LINK_UP) {
samdanbury 6:37b6d0d56190 467 #if LWIP_ARP
samdanbury 6:37b6d0d56190 468 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
samdanbury 6:37b6d0d56190 469 if (netif->flags & (NETIF_FLAG_ETHARP)) {
samdanbury 6:37b6d0d56190 470 etharp_gratuitous(netif);
samdanbury 6:37b6d0d56190 471 }
samdanbury 6:37b6d0d56190 472 #endif /* LWIP_ARP */
samdanbury 6:37b6d0d56190 473
samdanbury 6:37b6d0d56190 474 #if LWIP_IGMP
samdanbury 6:37b6d0d56190 475 /* resend IGMP memberships */
samdanbury 6:37b6d0d56190 476 if (netif->flags & NETIF_FLAG_IGMP) {
samdanbury 6:37b6d0d56190 477 igmp_report_groups( netif);
samdanbury 6:37b6d0d56190 478 }
samdanbury 6:37b6d0d56190 479 #endif /* LWIP_IGMP */
samdanbury 6:37b6d0d56190 480 }
samdanbury 6:37b6d0d56190 481 }
samdanbury 6:37b6d0d56190 482 }
samdanbury 6:37b6d0d56190 483
samdanbury 6:37b6d0d56190 484 /**
samdanbury 6:37b6d0d56190 485 * Bring an interface down, disabling any traffic processing.
samdanbury 6:37b6d0d56190 486 *
samdanbury 6:37b6d0d56190 487 * @note: Enabling DHCP on a down interface will make it come
samdanbury 6:37b6d0d56190 488 * up once configured.
samdanbury 6:37b6d0d56190 489 *
samdanbury 6:37b6d0d56190 490 * @see dhcp_start()
samdanbury 6:37b6d0d56190 491 */
samdanbury 6:37b6d0d56190 492 void netif_set_down(struct netif *netif)
samdanbury 6:37b6d0d56190 493 {
samdanbury 6:37b6d0d56190 494 if (netif->flags & NETIF_FLAG_UP) {
samdanbury 6:37b6d0d56190 495 netif->flags &= ~NETIF_FLAG_UP;
samdanbury 6:37b6d0d56190 496 #if LWIP_SNMP
samdanbury 6:37b6d0d56190 497 snmp_get_sysuptime(&netif->ts);
samdanbury 6:37b6d0d56190 498 #endif
samdanbury 6:37b6d0d56190 499
samdanbury 6:37b6d0d56190 500 NETIF_STATUS_CALLBACK(netif);
samdanbury 6:37b6d0d56190 501 }
samdanbury 6:37b6d0d56190 502 }
samdanbury 6:37b6d0d56190 503
samdanbury 6:37b6d0d56190 504 #if LWIP_NETIF_STATUS_CALLBACK
samdanbury 6:37b6d0d56190 505 /**
samdanbury 6:37b6d0d56190 506 * Set callback to be called when interface is brought up/down
samdanbury 6:37b6d0d56190 507 */
samdanbury 6:37b6d0d56190 508 void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
samdanbury 6:37b6d0d56190 509 {
samdanbury 6:37b6d0d56190 510 if (netif) {
samdanbury 6:37b6d0d56190 511 netif->status_callback = status_callback;
samdanbury 6:37b6d0d56190 512 }
samdanbury 6:37b6d0d56190 513 }
samdanbury 6:37b6d0d56190 514 #endif /* LWIP_NETIF_STATUS_CALLBACK */
samdanbury 6:37b6d0d56190 515
samdanbury 6:37b6d0d56190 516 /**
samdanbury 6:37b6d0d56190 517 * Called by a driver when its link goes up
samdanbury 6:37b6d0d56190 518 */
samdanbury 6:37b6d0d56190 519 void netif_set_link_up(struct netif *netif )
samdanbury 6:37b6d0d56190 520 {
samdanbury 6:37b6d0d56190 521 if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
samdanbury 6:37b6d0d56190 522 netif->flags |= NETIF_FLAG_LINK_UP;
samdanbury 6:37b6d0d56190 523
samdanbury 6:37b6d0d56190 524 #if LWIP_DHCP
samdanbury 6:37b6d0d56190 525 if (netif->dhcp) {
samdanbury 6:37b6d0d56190 526 dhcp_network_changed(netif);
samdanbury 6:37b6d0d56190 527 }
samdanbury 6:37b6d0d56190 528 #endif /* LWIP_DHCP */
samdanbury 6:37b6d0d56190 529
samdanbury 6:37b6d0d56190 530 #if LWIP_AUTOIP
samdanbury 6:37b6d0d56190 531 if (netif->autoip) {
samdanbury 6:37b6d0d56190 532 autoip_network_changed(netif);
samdanbury 6:37b6d0d56190 533 }
samdanbury 6:37b6d0d56190 534 #endif /* LWIP_AUTOIP */
samdanbury 6:37b6d0d56190 535
samdanbury 6:37b6d0d56190 536 if (netif->flags & NETIF_FLAG_UP) {
samdanbury 6:37b6d0d56190 537 #if LWIP_ARP
samdanbury 6:37b6d0d56190 538 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
samdanbury 6:37b6d0d56190 539 if (netif->flags & NETIF_FLAG_ETHARP) {
samdanbury 6:37b6d0d56190 540 etharp_gratuitous(netif);
samdanbury 6:37b6d0d56190 541 }
samdanbury 6:37b6d0d56190 542 #endif /* LWIP_ARP */
samdanbury 6:37b6d0d56190 543
samdanbury 6:37b6d0d56190 544 #if LWIP_IGMP
samdanbury 6:37b6d0d56190 545 /* resend IGMP memberships */
samdanbury 6:37b6d0d56190 546 if (netif->flags & NETIF_FLAG_IGMP) {
samdanbury 6:37b6d0d56190 547 igmp_report_groups( netif);
samdanbury 6:37b6d0d56190 548 }
samdanbury 6:37b6d0d56190 549 #endif /* LWIP_IGMP */
samdanbury 6:37b6d0d56190 550 }
samdanbury 6:37b6d0d56190 551 NETIF_LINK_CALLBACK(netif);
samdanbury 6:37b6d0d56190 552 }
samdanbury 6:37b6d0d56190 553 }
samdanbury 6:37b6d0d56190 554
samdanbury 6:37b6d0d56190 555 /**
samdanbury 6:37b6d0d56190 556 * Called by a driver when its link goes down
samdanbury 6:37b6d0d56190 557 */
samdanbury 6:37b6d0d56190 558 void netif_set_link_down(struct netif *netif )
samdanbury 6:37b6d0d56190 559 {
samdanbury 6:37b6d0d56190 560 if (netif->flags & NETIF_FLAG_LINK_UP) {
samdanbury 6:37b6d0d56190 561 netif->flags &= ~NETIF_FLAG_LINK_UP;
samdanbury 6:37b6d0d56190 562 NETIF_LINK_CALLBACK(netif);
samdanbury 6:37b6d0d56190 563 }
samdanbury 6:37b6d0d56190 564 }
samdanbury 6:37b6d0d56190 565
samdanbury 6:37b6d0d56190 566 #if LWIP_NETIF_LINK_CALLBACK
samdanbury 6:37b6d0d56190 567 /**
samdanbury 6:37b6d0d56190 568 * Set callback to be called when link is brought up/down
samdanbury 6:37b6d0d56190 569 */
samdanbury 6:37b6d0d56190 570 void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
samdanbury 6:37b6d0d56190 571 {
samdanbury 6:37b6d0d56190 572 if (netif) {
samdanbury 6:37b6d0d56190 573 netif->link_callback = link_callback;
samdanbury 6:37b6d0d56190 574 }
samdanbury 6:37b6d0d56190 575 }
samdanbury 6:37b6d0d56190 576 #endif /* LWIP_NETIF_LINK_CALLBACK */
samdanbury 6:37b6d0d56190 577
samdanbury 6:37b6d0d56190 578 #if ENABLE_LOOPBACK
samdanbury 6:37b6d0d56190 579 /**
samdanbury 6:37b6d0d56190 580 * Send an IP packet to be received on the same netif (loopif-like).
samdanbury 6:37b6d0d56190 581 * The pbuf is simply copied and handed back to netif->input.
samdanbury 6:37b6d0d56190 582 * In multithreaded mode, this is done directly since netif->input must put
samdanbury 6:37b6d0d56190 583 * the packet on a queue.
samdanbury 6:37b6d0d56190 584 * In callback mode, the packet is put on an internal queue and is fed to
samdanbury 6:37b6d0d56190 585 * netif->input by netif_poll().
samdanbury 6:37b6d0d56190 586 *
samdanbury 6:37b6d0d56190 587 * @param netif the lwip network interface structure
samdanbury 6:37b6d0d56190 588 * @param p the (IP) packet to 'send'
samdanbury 6:37b6d0d56190 589 * @param ipaddr the ip address to send the packet to (not used)
samdanbury 6:37b6d0d56190 590 * @return ERR_OK if the packet has been sent
samdanbury 6:37b6d0d56190 591 * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
samdanbury 6:37b6d0d56190 592 */
samdanbury 6:37b6d0d56190 593 err_t
samdanbury 6:37b6d0d56190 594 netif_loop_output(struct netif *netif, struct pbuf *p,
samdanbury 6:37b6d0d56190 595 ip_addr_t *ipaddr)
samdanbury 6:37b6d0d56190 596 {
samdanbury 6:37b6d0d56190 597 struct pbuf *r;
samdanbury 6:37b6d0d56190 598 err_t err;
samdanbury 6:37b6d0d56190 599 struct pbuf *last;
samdanbury 6:37b6d0d56190 600 #if LWIP_LOOPBACK_MAX_PBUFS
samdanbury 6:37b6d0d56190 601 u8_t clen = 0;
samdanbury 6:37b6d0d56190 602 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
samdanbury 6:37b6d0d56190 603 /* If we have a loopif, SNMP counters are adjusted for it,
samdanbury 6:37b6d0d56190 604 * if not they are adjusted for 'netif'. */
samdanbury 6:37b6d0d56190 605 #if LWIP_SNMP
samdanbury 6:37b6d0d56190 606 #if LWIP_HAVE_LOOPIF
samdanbury 6:37b6d0d56190 607 struct netif *stats_if = &loop_netif;
samdanbury 6:37b6d0d56190 608 #else /* LWIP_HAVE_LOOPIF */
samdanbury 6:37b6d0d56190 609 struct netif *stats_if = netif;
samdanbury 6:37b6d0d56190 610 #endif /* LWIP_HAVE_LOOPIF */
samdanbury 6:37b6d0d56190 611 #endif /* LWIP_SNMP */
samdanbury 6:37b6d0d56190 612 SYS_ARCH_DECL_PROTECT(lev);
samdanbury 6:37b6d0d56190 613 LWIP_UNUSED_ARG(ipaddr);
samdanbury 6:37b6d0d56190 614
samdanbury 6:37b6d0d56190 615 /* Allocate a new pbuf */
samdanbury 6:37b6d0d56190 616 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
samdanbury 6:37b6d0d56190 617 if (r == NULL) {
samdanbury 6:37b6d0d56190 618 LINK_STATS_INC(link.memerr);
samdanbury 6:37b6d0d56190 619 LINK_STATS_INC(link.drop);
samdanbury 6:37b6d0d56190 620 snmp_inc_ifoutdiscards(stats_if);
samdanbury 6:37b6d0d56190 621 return ERR_MEM;
samdanbury 6:37b6d0d56190 622 }
samdanbury 6:37b6d0d56190 623 #if LWIP_LOOPBACK_MAX_PBUFS
samdanbury 6:37b6d0d56190 624 clen = pbuf_clen(r);
samdanbury 6:37b6d0d56190 625 /* check for overflow or too many pbuf on queue */
samdanbury 6:37b6d0d56190 626 if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
samdanbury 6:37b6d0d56190 627 ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
samdanbury 6:37b6d0d56190 628 pbuf_free(r);
samdanbury 6:37b6d0d56190 629 LINK_STATS_INC(link.memerr);
samdanbury 6:37b6d0d56190 630 LINK_STATS_INC(link.drop);
samdanbury 6:37b6d0d56190 631 snmp_inc_ifoutdiscards(stats_if);
samdanbury 6:37b6d0d56190 632 return ERR_MEM;
samdanbury 6:37b6d0d56190 633 }
samdanbury 6:37b6d0d56190 634 netif->loop_cnt_current += clen;
samdanbury 6:37b6d0d56190 635 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
samdanbury 6:37b6d0d56190 636
samdanbury 6:37b6d0d56190 637 /* Copy the whole pbuf queue p into the single pbuf r */
samdanbury 6:37b6d0d56190 638 if ((err = pbuf_copy(r, p)) != ERR_OK) {
samdanbury 6:37b6d0d56190 639 pbuf_free(r);
samdanbury 6:37b6d0d56190 640 LINK_STATS_INC(link.memerr);
samdanbury 6:37b6d0d56190 641 LINK_STATS_INC(link.drop);
samdanbury 6:37b6d0d56190 642 snmp_inc_ifoutdiscards(stats_if);
samdanbury 6:37b6d0d56190 643 return err;
samdanbury 6:37b6d0d56190 644 }
samdanbury 6:37b6d0d56190 645
samdanbury 6:37b6d0d56190 646 /* Put the packet on a linked list which gets emptied through calling
samdanbury 6:37b6d0d56190 647 netif_poll(). */
samdanbury 6:37b6d0d56190 648
samdanbury 6:37b6d0d56190 649 /* let last point to the last pbuf in chain r */
samdanbury 6:37b6d0d56190 650 for (last = r; last->next != NULL; last = last->next);
samdanbury 6:37b6d0d56190 651
samdanbury 6:37b6d0d56190 652 SYS_ARCH_PROTECT(lev);
samdanbury 6:37b6d0d56190 653 if(netif->loop_first != NULL) {
samdanbury 6:37b6d0d56190 654 LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
samdanbury 6:37b6d0d56190 655 netif->loop_last->next = r;
samdanbury 6:37b6d0d56190 656 netif->loop_last = last;
samdanbury 6:37b6d0d56190 657 } else {
samdanbury 6:37b6d0d56190 658 netif->loop_first = r;
samdanbury 6:37b6d0d56190 659 netif->loop_last = last;
samdanbury 6:37b6d0d56190 660 }
samdanbury 6:37b6d0d56190 661 SYS_ARCH_UNPROTECT(lev);
samdanbury 6:37b6d0d56190 662
samdanbury 6:37b6d0d56190 663 LINK_STATS_INC(link.xmit);
samdanbury 6:37b6d0d56190 664 snmp_add_ifoutoctets(stats_if, p->tot_len);
samdanbury 6:37b6d0d56190 665 snmp_inc_ifoutucastpkts(stats_if);
samdanbury 6:37b6d0d56190 666
samdanbury 6:37b6d0d56190 667 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
samdanbury 6:37b6d0d56190 668 /* For multithreading environment, schedule a call to netif_poll */
samdanbury 6:37b6d0d56190 669 tcpip_callback((tcpip_callback_fn)netif_poll, netif);
samdanbury 6:37b6d0d56190 670 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
samdanbury 6:37b6d0d56190 671
samdanbury 6:37b6d0d56190 672 return ERR_OK;
samdanbury 6:37b6d0d56190 673 }
samdanbury 6:37b6d0d56190 674
samdanbury 6:37b6d0d56190 675 /**
samdanbury 6:37b6d0d56190 676 * Call netif_poll() in the main loop of your application. This is to prevent
samdanbury 6:37b6d0d56190 677 * reentering non-reentrant functions like tcp_input(). Packets passed to
samdanbury 6:37b6d0d56190 678 * netif_loop_output() are put on a list that is passed to netif->input() by
samdanbury 6:37b6d0d56190 679 * netif_poll().
samdanbury 6:37b6d0d56190 680 */
samdanbury 6:37b6d0d56190 681 void
samdanbury 6:37b6d0d56190 682 netif_poll(struct netif *netif)
samdanbury 6:37b6d0d56190 683 {
samdanbury 6:37b6d0d56190 684 struct pbuf *in;
samdanbury 6:37b6d0d56190 685 /* If we have a loopif, SNMP counters are adjusted for it,
samdanbury 6:37b6d0d56190 686 * if not they are adjusted for 'netif'. */
samdanbury 6:37b6d0d56190 687 #if LWIP_SNMP
samdanbury 6:37b6d0d56190 688 #if LWIP_HAVE_LOOPIF
samdanbury 6:37b6d0d56190 689 struct netif *stats_if = &loop_netif;
samdanbury 6:37b6d0d56190 690 #else /* LWIP_HAVE_LOOPIF */
samdanbury 6:37b6d0d56190 691 struct netif *stats_if = netif;
samdanbury 6:37b6d0d56190 692 #endif /* LWIP_HAVE_LOOPIF */
samdanbury 6:37b6d0d56190 693 #endif /* LWIP_SNMP */
samdanbury 6:37b6d0d56190 694 SYS_ARCH_DECL_PROTECT(lev);
samdanbury 6:37b6d0d56190 695
samdanbury 6:37b6d0d56190 696 do {
samdanbury 6:37b6d0d56190 697 /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
samdanbury 6:37b6d0d56190 698 SYS_ARCH_PROTECT(lev);
samdanbury 6:37b6d0d56190 699 in = netif->loop_first;
samdanbury 6:37b6d0d56190 700 if (in != NULL) {
samdanbury 6:37b6d0d56190 701 struct pbuf *in_end = in;
samdanbury 6:37b6d0d56190 702 #if LWIP_LOOPBACK_MAX_PBUFS
samdanbury 6:37b6d0d56190 703 u8_t clen = pbuf_clen(in);
samdanbury 6:37b6d0d56190 704 /* adjust the number of pbufs on queue */
samdanbury 6:37b6d0d56190 705 LWIP_ASSERT("netif->loop_cnt_current underflow",
samdanbury 6:37b6d0d56190 706 ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
samdanbury 6:37b6d0d56190 707 netif->loop_cnt_current -= clen;
samdanbury 6:37b6d0d56190 708 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
samdanbury 6:37b6d0d56190 709 while (in_end->len != in_end->tot_len) {
samdanbury 6:37b6d0d56190 710 LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
samdanbury 6:37b6d0d56190 711 in_end = in_end->next;
samdanbury 6:37b6d0d56190 712 }
samdanbury 6:37b6d0d56190 713 /* 'in_end' now points to the last pbuf from 'in' */
samdanbury 6:37b6d0d56190 714 if (in_end == netif->loop_last) {
samdanbury 6:37b6d0d56190 715 /* this was the last pbuf in the list */
samdanbury 6:37b6d0d56190 716 netif->loop_first = netif->loop_last = NULL;
samdanbury 6:37b6d0d56190 717 } else {
samdanbury 6:37b6d0d56190 718 /* pop the pbuf off the list */
samdanbury 6:37b6d0d56190 719 netif->loop_first = in_end->next;
samdanbury 6:37b6d0d56190 720 LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
samdanbury 6:37b6d0d56190 721 }
samdanbury 6:37b6d0d56190 722 /* De-queue the pbuf from its successors on the 'loop_' list. */
samdanbury 6:37b6d0d56190 723 in_end->next = NULL;
samdanbury 6:37b6d0d56190 724 }
samdanbury 6:37b6d0d56190 725 SYS_ARCH_UNPROTECT(lev);
samdanbury 6:37b6d0d56190 726
samdanbury 6:37b6d0d56190 727 if (in != NULL) {
samdanbury 6:37b6d0d56190 728 LINK_STATS_INC(link.recv);
samdanbury 6:37b6d0d56190 729 snmp_add_ifinoctets(stats_if, in->tot_len);
samdanbury 6:37b6d0d56190 730 snmp_inc_ifinucastpkts(stats_if);
samdanbury 6:37b6d0d56190 731 /* loopback packets are always IP packets! */
samdanbury 6:37b6d0d56190 732 if (ip_input(in, netif) != ERR_OK) {
samdanbury 6:37b6d0d56190 733 pbuf_free(in);
samdanbury 6:37b6d0d56190 734 }
samdanbury 6:37b6d0d56190 735 /* Don't reference the packet any more! */
samdanbury 6:37b6d0d56190 736 in = NULL;
samdanbury 6:37b6d0d56190 737 }
samdanbury 6:37b6d0d56190 738 /* go on while there is a packet on the list */
samdanbury 6:37b6d0d56190 739 } while (netif->loop_first != NULL);
samdanbury 6:37b6d0d56190 740 }
samdanbury 6:37b6d0d56190 741
samdanbury 6:37b6d0d56190 742 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
samdanbury 6:37b6d0d56190 743 /**
samdanbury 6:37b6d0d56190 744 * Calls netif_poll() for every netif on the netif_list.
samdanbury 6:37b6d0d56190 745 */
samdanbury 6:37b6d0d56190 746 void
samdanbury 6:37b6d0d56190 747 netif_poll_all(void)
samdanbury 6:37b6d0d56190 748 {
samdanbury 6:37b6d0d56190 749 struct netif *netif = netif_list;
samdanbury 6:37b6d0d56190 750 /* loop through netifs */
samdanbury 6:37b6d0d56190 751 while (netif != NULL) {
samdanbury 6:37b6d0d56190 752 netif_poll(netif);
samdanbury 6:37b6d0d56190 753 /* proceed to next network interface */
samdanbury 6:37b6d0d56190 754 netif = netif->next;
samdanbury 6:37b6d0d56190 755 }
samdanbury 6:37b6d0d56190 756 }
samdanbury 6:37b6d0d56190 757 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
samdanbury 6:37b6d0d56190 758 #endif /* ENABLE_LOOPBACK */