Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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