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