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