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