Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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