Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Sat Feb 25 03:28:05 2012 +0000
Revision:
1:c7958aa34fa1
Parent:
0:616601bde9fb
Use published libraries where possible.

Who changed what in which revision?

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