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