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