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 * AutoIP Automatic LinkLocal IP Configuration
andrewbonney 0:ec559500a63f 4 *
andrewbonney 0:ec559500a63f 5 */
andrewbonney 0:ec559500a63f 6
andrewbonney 0:ec559500a63f 7 /*
andrewbonney 0:ec559500a63f 8 *
andrewbonney 0:ec559500a63f 9 * Copyright (c) 2007 Dominik Spies <kontakt@dspies.de>
andrewbonney 0:ec559500a63f 10 * All rights reserved.
andrewbonney 0:ec559500a63f 11 *
andrewbonney 0:ec559500a63f 12 * Redistribution and use in source and binary forms, with or without modification,
andrewbonney 0:ec559500a63f 13 * are permitted provided that the following conditions are met:
andrewbonney 0:ec559500a63f 14 *
andrewbonney 0:ec559500a63f 15 * 1. Redistributions of source code must retain the above copyright notice,
andrewbonney 0:ec559500a63f 16 * this list of conditions and the following disclaimer.
andrewbonney 0:ec559500a63f 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
andrewbonney 0:ec559500a63f 18 * this list of conditions and the following disclaimer in the documentation
andrewbonney 0:ec559500a63f 19 * and/or other materials provided with the distribution.
andrewbonney 0:ec559500a63f 20 * 3. The name of the author may not be used to endorse or promote products
andrewbonney 0:ec559500a63f 21 * derived from this software without specific prior written permission.
andrewbonney 0:ec559500a63f 22 *
andrewbonney 0:ec559500a63f 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
andrewbonney 0:ec559500a63f 24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
andrewbonney 0:ec559500a63f 25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
andrewbonney 0:ec559500a63f 26 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
andrewbonney 0:ec559500a63f 27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
andrewbonney 0:ec559500a63f 28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
andrewbonney 0:ec559500a63f 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
andrewbonney 0:ec559500a63f 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
andrewbonney 0:ec559500a63f 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
andrewbonney 0:ec559500a63f 32 * OF SUCH DAMAGE.
andrewbonney 0:ec559500a63f 33 *
andrewbonney 0:ec559500a63f 34 * Author: Dominik Spies <kontakt@dspies.de>
andrewbonney 0:ec559500a63f 35 *
andrewbonney 0:ec559500a63f 36 * This is a AutoIP implementation for the lwIP TCP/IP stack. It aims to conform
andrewbonney 0:ec559500a63f 37 * with RFC 3927.
andrewbonney 0:ec559500a63f 38 *
andrewbonney 0:ec559500a63f 39 *
andrewbonney 0:ec559500a63f 40 * Please coordinate changes and requests with Dominik Spies
andrewbonney 0:ec559500a63f 41 * <kontakt@dspies.de>
andrewbonney 0:ec559500a63f 42 */
andrewbonney 0:ec559500a63f 43
andrewbonney 0:ec559500a63f 44 /*******************************************************************************
andrewbonney 0:ec559500a63f 45 * USAGE:
andrewbonney 0:ec559500a63f 46 *
andrewbonney 0:ec559500a63f 47 * define LWIP_AUTOIP 1 in your lwipopts.h
andrewbonney 0:ec559500a63f 48 *
andrewbonney 0:ec559500a63f 49 * If you don't use tcpip.c (so, don't call, you don't call tcpip_init):
andrewbonney 0:ec559500a63f 50 * - First, call autoip_init().
andrewbonney 0:ec559500a63f 51 * - call autoip_tmr() all AUTOIP_TMR_INTERVAL msces,
andrewbonney 0:ec559500a63f 52 * that should be defined in autoip.h.
andrewbonney 0:ec559500a63f 53 * I recommend a value of 100. The value must divide 1000 with a remainder almost 0.
andrewbonney 0:ec559500a63f 54 * Possible values are 1000, 500, 333, 250, 200, 166, 142, 125, 111, 100 ....
andrewbonney 0:ec559500a63f 55 *
andrewbonney 0:ec559500a63f 56 * Without DHCP:
andrewbonney 0:ec559500a63f 57 * - Call autoip_start() after netif_add().
andrewbonney 0:ec559500a63f 58 *
andrewbonney 0:ec559500a63f 59 * With DHCP:
andrewbonney 0:ec559500a63f 60 * - define LWIP_DHCP_AUTOIP_COOP 1 in your lwipopts.h.
andrewbonney 0:ec559500a63f 61 * - Configure your DHCP Client.
andrewbonney 0:ec559500a63f 62 *
andrewbonney 0:ec559500a63f 63 */
andrewbonney 0:ec559500a63f 64
andrewbonney 0:ec559500a63f 65 #include "lwip/opt.h"
andrewbonney 0:ec559500a63f 66
andrewbonney 0:ec559500a63f 67 #if LWIP_AUTOIP /* don't build if not configured for use in lwipopts.h */
andrewbonney 0:ec559500a63f 68
andrewbonney 0:ec559500a63f 69 #include "lwip/mem.h"
andrewbonney 0:ec559500a63f 70 #include "lwip/udp.h"
andrewbonney 0:ec559500a63f 71 #include "lwip/ip_addr.h"
andrewbonney 0:ec559500a63f 72 #include "lwip/netif.h"
andrewbonney 0:ec559500a63f 73 #include "lwip/autoip.h"
andrewbonney 0:ec559500a63f 74 #include "netif/etharp.h"
andrewbonney 0:ec559500a63f 75
andrewbonney 0:ec559500a63f 76 #include <stdlib.h>
andrewbonney 0:ec559500a63f 77 #include <string.h>
andrewbonney 0:ec559500a63f 78
andrewbonney 0:ec559500a63f 79 /* 169.254.0.0 */
andrewbonney 0:ec559500a63f 80 #define AUTOIP_NET 0xA9FE0000
andrewbonney 0:ec559500a63f 81 /* 169.254.1.0 */
andrewbonney 0:ec559500a63f 82 #define AUTOIP_RANGE_START (AUTOIP_NET | 0x0100)
andrewbonney 0:ec559500a63f 83 /* 169.254.254.255 */
andrewbonney 0:ec559500a63f 84 #define AUTOIP_RANGE_END (AUTOIP_NET | 0xFEFF)
andrewbonney 0:ec559500a63f 85
andrewbonney 0:ec559500a63f 86
andrewbonney 0:ec559500a63f 87 /** Pseudo random macro based on netif informations.
andrewbonney 0:ec559500a63f 88 * You could use "rand()" from the C Library if you define LWIP_AUTOIP_RAND in lwipopts.h */
andrewbonney 0:ec559500a63f 89 #ifndef LWIP_AUTOIP_RAND
andrewbonney 0:ec559500a63f 90 #define LWIP_AUTOIP_RAND(netif) ( (((u32_t)((netif->hwaddr[5]) & 0xff) << 24) | \
andrewbonney 0:ec559500a63f 91 ((u32_t)((netif->hwaddr[3]) & 0xff) << 16) | \
andrewbonney 0:ec559500a63f 92 ((u32_t)((netif->hwaddr[2]) & 0xff) << 8) | \
andrewbonney 0:ec559500a63f 93 ((u32_t)((netif->hwaddr[4]) & 0xff))) + \
andrewbonney 0:ec559500a63f 94 (netif->autoip?netif->autoip->tried_llipaddr:0))
andrewbonney 0:ec559500a63f 95 #endif /* LWIP_AUTOIP_RAND */
andrewbonney 0:ec559500a63f 96
andrewbonney 0:ec559500a63f 97 /**
andrewbonney 0:ec559500a63f 98 * Macro that generates the initial IP address to be tried by AUTOIP.
andrewbonney 0:ec559500a63f 99 * If you want to override this, define it to something else in lwipopts.h.
andrewbonney 0:ec559500a63f 100 */
andrewbonney 0:ec559500a63f 101 #ifndef LWIP_AUTOIP_CREATE_SEED_ADDR
andrewbonney 0:ec559500a63f 102 #define LWIP_AUTOIP_CREATE_SEED_ADDR(netif) \
andrewbonney 0:ec559500a63f 103 htonl(AUTOIP_RANGE_START + ((u32_t)(((u8_t)(netif->hwaddr[4])) | \
andrewbonney 0:ec559500a63f 104 ((u32_t)((u8_t)(netif->hwaddr[5]))) << 8)))
andrewbonney 0:ec559500a63f 105 #endif /* LWIP_AUTOIP_CREATE_SEED_ADDR */
andrewbonney 0:ec559500a63f 106
andrewbonney 0:ec559500a63f 107 /* static functions */
andrewbonney 0:ec559500a63f 108 static void autoip_handle_arp_conflict(struct netif *netif);
andrewbonney 0:ec559500a63f 109
andrewbonney 0:ec559500a63f 110 /* creates a pseudo random LL IP-Address for a network interface */
andrewbonney 0:ec559500a63f 111 static void autoip_create_addr(struct netif *netif, ip_addr_t *ipaddr);
andrewbonney 0:ec559500a63f 112
andrewbonney 0:ec559500a63f 113 /* sends an ARP probe */
andrewbonney 0:ec559500a63f 114 static err_t autoip_arp_probe(struct netif *netif);
andrewbonney 0:ec559500a63f 115
andrewbonney 0:ec559500a63f 116 /* sends an ARP announce */
andrewbonney 0:ec559500a63f 117 static err_t autoip_arp_announce(struct netif *netif);
andrewbonney 0:ec559500a63f 118
andrewbonney 0:ec559500a63f 119 /* configure interface for use with current LL IP-Address */
andrewbonney 0:ec559500a63f 120 static err_t autoip_bind(struct netif *netif);
andrewbonney 0:ec559500a63f 121
andrewbonney 0:ec559500a63f 122 /* start sending probes for llipaddr */
andrewbonney 0:ec559500a63f 123 static void autoip_start_probing(struct netif *netif);
andrewbonney 0:ec559500a63f 124
andrewbonney 0:ec559500a63f 125 /**
andrewbonney 0:ec559500a63f 126 * Initialize this module
andrewbonney 0:ec559500a63f 127 */
andrewbonney 0:ec559500a63f 128 void
andrewbonney 0:ec559500a63f 129 autoip_init(void)
andrewbonney 0:ec559500a63f 130 {
andrewbonney 0:ec559500a63f 131 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_init()\n"));
andrewbonney 0:ec559500a63f 132 }
andrewbonney 0:ec559500a63f 133
andrewbonney 0:ec559500a63f 134 /** Set a statically allocated struct autoip to work with.
andrewbonney 0:ec559500a63f 135 * Using this prevents autoip_start to allocate it using mem_malloc.
andrewbonney 0:ec559500a63f 136 *
andrewbonney 0:ec559500a63f 137 * @param netif the netif for which to set the struct autoip
andrewbonney 0:ec559500a63f 138 * @param dhcp (uninitialised) dhcp struct allocated by the application
andrewbonney 0:ec559500a63f 139 */
andrewbonney 0:ec559500a63f 140 void
andrewbonney 0:ec559500a63f 141 autoip_set_struct(struct netif *netif, struct autoip *autoip)
andrewbonney 0:ec559500a63f 142 {
andrewbonney 0:ec559500a63f 143 LWIP_ASSERT("netif != NULL", netif != NULL);
andrewbonney 0:ec559500a63f 144 LWIP_ASSERT("autoip != NULL", autoip != NULL);
andrewbonney 0:ec559500a63f 145 LWIP_ASSERT("netif already has a struct autoip set", netif->autoip == NULL);
andrewbonney 0:ec559500a63f 146
andrewbonney 0:ec559500a63f 147 /* clear data structure */
andrewbonney 0:ec559500a63f 148 memset(autoip, 0, sizeof(struct autoip));
andrewbonney 0:ec559500a63f 149 /* autoip->state = AUTOIP_STATE_OFF; */
andrewbonney 0:ec559500a63f 150 netif->autoip = autoip;
andrewbonney 0:ec559500a63f 151 }
andrewbonney 0:ec559500a63f 152
andrewbonney 0:ec559500a63f 153 /** Restart AutoIP client and check the next address (conflict detected)
andrewbonney 0:ec559500a63f 154 *
andrewbonney 0:ec559500a63f 155 * @param netif The netif under AutoIP control
andrewbonney 0:ec559500a63f 156 */
andrewbonney 0:ec559500a63f 157 static void
andrewbonney 0:ec559500a63f 158 autoip_restart(struct netif *netif)
andrewbonney 0:ec559500a63f 159 {
andrewbonney 0:ec559500a63f 160 netif->autoip->tried_llipaddr++;
andrewbonney 0:ec559500a63f 161 autoip_start(netif);
andrewbonney 0:ec559500a63f 162 }
andrewbonney 0:ec559500a63f 163
andrewbonney 0:ec559500a63f 164 /**
andrewbonney 0:ec559500a63f 165 * Handle a IP address conflict after an ARP conflict detection
andrewbonney 0:ec559500a63f 166 */
andrewbonney 0:ec559500a63f 167 static void
andrewbonney 0:ec559500a63f 168 autoip_handle_arp_conflict(struct netif *netif)
andrewbonney 0:ec559500a63f 169 {
andrewbonney 0:ec559500a63f 170 /* Somehow detect if we are defending or retreating */
andrewbonney 0:ec559500a63f 171 unsigned char defend = 1; /* tbd */
andrewbonney 0:ec559500a63f 172
andrewbonney 0:ec559500a63f 173 if(defend) {
andrewbonney 0:ec559500a63f 174 if(netif->autoip->lastconflict > 0) {
andrewbonney 0:ec559500a63f 175 /* retreat, there was a conflicting ARP in the last
andrewbonney 0:ec559500a63f 176 * DEFEND_INTERVAL seconds
andrewbonney 0:ec559500a63f 177 */
andrewbonney 0:ec559500a63f 178 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
andrewbonney 0:ec559500a63f 179 ("autoip_handle_arp_conflict(): we are defending, but in DEFEND_INTERVAL, retreating\n"));
andrewbonney 0:ec559500a63f 180
andrewbonney 0:ec559500a63f 181 /* TODO: close all TCP sessions */
andrewbonney 0:ec559500a63f 182 autoip_restart(netif);
andrewbonney 0:ec559500a63f 183 } else {
andrewbonney 0:ec559500a63f 184 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
andrewbonney 0:ec559500a63f 185 ("autoip_handle_arp_conflict(): we are defend, send ARP Announce\n"));
andrewbonney 0:ec559500a63f 186 autoip_arp_announce(netif);
andrewbonney 0:ec559500a63f 187 netif->autoip->lastconflict = DEFEND_INTERVAL * AUTOIP_TICKS_PER_SECOND;
andrewbonney 0:ec559500a63f 188 }
andrewbonney 0:ec559500a63f 189 } else {
andrewbonney 0:ec559500a63f 190 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
andrewbonney 0:ec559500a63f 191 ("autoip_handle_arp_conflict(): we do not defend, retreating\n"));
andrewbonney 0:ec559500a63f 192 /* TODO: close all TCP sessions */
andrewbonney 0:ec559500a63f 193 autoip_restart(netif);
andrewbonney 0:ec559500a63f 194 }
andrewbonney 0:ec559500a63f 195 }
andrewbonney 0:ec559500a63f 196
andrewbonney 0:ec559500a63f 197 /**
andrewbonney 0:ec559500a63f 198 * Create an IP-Address out of range 169.254.1.0 to 169.254.254.255
andrewbonney 0:ec559500a63f 199 *
andrewbonney 0:ec559500a63f 200 * @param netif network interface on which create the IP-Address
andrewbonney 0:ec559500a63f 201 * @param ipaddr ip address to initialize
andrewbonney 0:ec559500a63f 202 */
andrewbonney 0:ec559500a63f 203 static void
andrewbonney 0:ec559500a63f 204 autoip_create_addr(struct netif *netif, ip_addr_t *ipaddr)
andrewbonney 0:ec559500a63f 205 {
andrewbonney 0:ec559500a63f 206 /* Here we create an IP-Address out of range 169.254.1.0 to 169.254.254.255
andrewbonney 0:ec559500a63f 207 * compliant to RFC 3927 Section 2.1
andrewbonney 0:ec559500a63f 208 * We have 254 * 256 possibilities */
andrewbonney 0:ec559500a63f 209
andrewbonney 0:ec559500a63f 210 u32_t addr = ntohl(LWIP_AUTOIP_CREATE_SEED_ADDR(netif));
andrewbonney 0:ec559500a63f 211 addr += netif->autoip->tried_llipaddr;
andrewbonney 0:ec559500a63f 212 addr = AUTOIP_NET | (addr & 0xffff);
andrewbonney 0:ec559500a63f 213 /* Now, 169.254.0.0 <= addr <= 169.254.255.255 */
andrewbonney 0:ec559500a63f 214
andrewbonney 0:ec559500a63f 215 if (addr < AUTOIP_RANGE_START) {
andrewbonney 0:ec559500a63f 216 addr += AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
andrewbonney 0:ec559500a63f 217 }
andrewbonney 0:ec559500a63f 218 if (addr > AUTOIP_RANGE_END) {
andrewbonney 0:ec559500a63f 219 addr -= AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
andrewbonney 0:ec559500a63f 220 }
andrewbonney 0:ec559500a63f 221 LWIP_ASSERT("AUTOIP address not in range", (addr >= AUTOIP_RANGE_START) &&
andrewbonney 0:ec559500a63f 222 (addr <= AUTOIP_RANGE_END));
andrewbonney 0:ec559500a63f 223 ip4_addr_set_u32(ipaddr, htonl(addr));
andrewbonney 0:ec559500a63f 224
andrewbonney 0:ec559500a63f 225 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
andrewbonney 0:ec559500a63f 226 ("autoip_create_addr(): tried_llipaddr=%"U16_F", %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
andrewbonney 0:ec559500a63f 227 (u16_t)(netif->autoip->tried_llipaddr), ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr),
andrewbonney 0:ec559500a63f 228 ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
andrewbonney 0:ec559500a63f 229 }
andrewbonney 0:ec559500a63f 230
andrewbonney 0:ec559500a63f 231 /**
andrewbonney 0:ec559500a63f 232 * Sends an ARP probe from a network interface
andrewbonney 0:ec559500a63f 233 *
andrewbonney 0:ec559500a63f 234 * @param netif network interface used to send the probe
andrewbonney 0:ec559500a63f 235 */
andrewbonney 0:ec559500a63f 236 static err_t
andrewbonney 0:ec559500a63f 237 autoip_arp_probe(struct netif *netif)
andrewbonney 0:ec559500a63f 238 {
andrewbonney 0:ec559500a63f 239 return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, &ethbroadcast,
andrewbonney 0:ec559500a63f 240 (struct eth_addr *)netif->hwaddr, IP_ADDR_ANY, &ethzero,
andrewbonney 0:ec559500a63f 241 &netif->autoip->llipaddr, ARP_REQUEST);
andrewbonney 0:ec559500a63f 242 }
andrewbonney 0:ec559500a63f 243
andrewbonney 0:ec559500a63f 244 /**
andrewbonney 0:ec559500a63f 245 * Sends an ARP announce from a network interface
andrewbonney 0:ec559500a63f 246 *
andrewbonney 0:ec559500a63f 247 * @param netif network interface used to send the announce
andrewbonney 0:ec559500a63f 248 */
andrewbonney 0:ec559500a63f 249 static err_t
andrewbonney 0:ec559500a63f 250 autoip_arp_announce(struct netif *netif)
andrewbonney 0:ec559500a63f 251 {
andrewbonney 0:ec559500a63f 252 return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, &ethbroadcast,
andrewbonney 0:ec559500a63f 253 (struct eth_addr *)netif->hwaddr, &netif->autoip->llipaddr, &ethzero,
andrewbonney 0:ec559500a63f 254 &netif->autoip->llipaddr, ARP_REQUEST);
andrewbonney 0:ec559500a63f 255 }
andrewbonney 0:ec559500a63f 256
andrewbonney 0:ec559500a63f 257 /**
andrewbonney 0:ec559500a63f 258 * Configure interface for use with current LL IP-Address
andrewbonney 0:ec559500a63f 259 *
andrewbonney 0:ec559500a63f 260 * @param netif network interface to configure with current LL IP-Address
andrewbonney 0:ec559500a63f 261 */
andrewbonney 0:ec559500a63f 262 static err_t
andrewbonney 0:ec559500a63f 263 autoip_bind(struct netif *netif)
andrewbonney 0:ec559500a63f 264 {
andrewbonney 0:ec559500a63f 265 struct autoip *autoip = netif->autoip;
andrewbonney 0:ec559500a63f 266 ip_addr_t sn_mask, gw_addr;
andrewbonney 0:ec559500a63f 267
andrewbonney 0:ec559500a63f 268 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
andrewbonney 0:ec559500a63f 269 ("autoip_bind(netif=%p) %c%c%"U16_F" %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
andrewbonney 0:ec559500a63f 270 (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num,
andrewbonney 0:ec559500a63f 271 ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
andrewbonney 0:ec559500a63f 272 ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
andrewbonney 0:ec559500a63f 273
andrewbonney 0:ec559500a63f 274 IP4_ADDR(&sn_mask, 255, 255, 0, 0);
andrewbonney 0:ec559500a63f 275 IP4_ADDR(&gw_addr, 0, 0, 0, 0);
andrewbonney 0:ec559500a63f 276
andrewbonney 0:ec559500a63f 277 netif_set_ipaddr(netif, &autoip->llipaddr);
andrewbonney 0:ec559500a63f 278 netif_set_netmask(netif, &sn_mask);
andrewbonney 0:ec559500a63f 279 netif_set_gw(netif, &gw_addr);
andrewbonney 0:ec559500a63f 280
andrewbonney 0:ec559500a63f 281 /* bring the interface up */
andrewbonney 0:ec559500a63f 282 netif_set_up(netif);
andrewbonney 0:ec559500a63f 283
andrewbonney 0:ec559500a63f 284 return ERR_OK;
andrewbonney 0:ec559500a63f 285 }
andrewbonney 0:ec559500a63f 286
andrewbonney 0:ec559500a63f 287 /**
andrewbonney 0:ec559500a63f 288 * Start AutoIP client
andrewbonney 0:ec559500a63f 289 *
andrewbonney 0:ec559500a63f 290 * @param netif network interface on which start the AutoIP client
andrewbonney 0:ec559500a63f 291 */
andrewbonney 0:ec559500a63f 292 err_t
andrewbonney 0:ec559500a63f 293 autoip_start(struct netif *netif)
andrewbonney 0:ec559500a63f 294 {
andrewbonney 0:ec559500a63f 295 struct autoip *autoip = netif->autoip;
andrewbonney 0:ec559500a63f 296 err_t result = ERR_OK;
andrewbonney 0:ec559500a63f 297
andrewbonney 0:ec559500a63f 298 if(netif_is_up(netif)) {
andrewbonney 0:ec559500a63f 299 netif_set_down(netif);
andrewbonney 0:ec559500a63f 300 }
andrewbonney 0:ec559500a63f 301
andrewbonney 0:ec559500a63f 302 /* Set IP-Address, Netmask and Gateway to 0 to make sure that
andrewbonney 0:ec559500a63f 303 * ARP Packets are formed correctly
andrewbonney 0:ec559500a63f 304 */
andrewbonney 0:ec559500a63f 305 ip_addr_set_zero(&netif->ip_addr);
andrewbonney 0:ec559500a63f 306 ip_addr_set_zero(&netif->netmask);
andrewbonney 0:ec559500a63f 307 ip_addr_set_zero(&netif->gw);
andrewbonney 0:ec559500a63f 308
andrewbonney 0:ec559500a63f 309 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
andrewbonney 0:ec559500a63f 310 ("autoip_start(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0],
andrewbonney 0:ec559500a63f 311 netif->name[1], (u16_t)netif->num));
andrewbonney 0:ec559500a63f 312 if(autoip == NULL) {
andrewbonney 0:ec559500a63f 313 /* no AutoIP client attached yet? */
andrewbonney 0:ec559500a63f 314 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
andrewbonney 0:ec559500a63f 315 ("autoip_start(): starting new AUTOIP client\n"));
andrewbonney 0:ec559500a63f 316 autoip = (struct autoip *)mem_malloc(sizeof(struct autoip));
andrewbonney 0:ec559500a63f 317 if(autoip == NULL) {
andrewbonney 0:ec559500a63f 318 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
andrewbonney 0:ec559500a63f 319 ("autoip_start(): could not allocate autoip\n"));
andrewbonney 0:ec559500a63f 320 return ERR_MEM;
andrewbonney 0:ec559500a63f 321 }
andrewbonney 0:ec559500a63f 322 memset(autoip, 0, sizeof(struct autoip));
andrewbonney 0:ec559500a63f 323 /* store this AutoIP client in the netif */
andrewbonney 0:ec559500a63f 324 netif->autoip = autoip;
andrewbonney 0:ec559500a63f 325 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): allocated autoip"));
andrewbonney 0:ec559500a63f 326 } else {
andrewbonney 0:ec559500a63f 327 autoip->state = AUTOIP_STATE_OFF;
andrewbonney 0:ec559500a63f 328 autoip->ttw = 0;
andrewbonney 0:ec559500a63f 329 autoip->sent_num = 0;
andrewbonney 0:ec559500a63f 330 ip_addr_set_zero(&autoip->llipaddr);
andrewbonney 0:ec559500a63f 331 autoip->lastconflict = 0;
andrewbonney 0:ec559500a63f 332 }
andrewbonney 0:ec559500a63f 333
andrewbonney 0:ec559500a63f 334 autoip_create_addr(netif, &(autoip->llipaddr));
andrewbonney 0:ec559500a63f 335 autoip_start_probing(netif);
andrewbonney 0:ec559500a63f 336
andrewbonney 0:ec559500a63f 337 return result;
andrewbonney 0:ec559500a63f 338 }
andrewbonney 0:ec559500a63f 339
andrewbonney 0:ec559500a63f 340 static void
andrewbonney 0:ec559500a63f 341 autoip_start_probing(struct netif *netif)
andrewbonney 0:ec559500a63f 342 {
andrewbonney 0:ec559500a63f 343 struct autoip *autoip = netif->autoip;
andrewbonney 0:ec559500a63f 344
andrewbonney 0:ec559500a63f 345 autoip->state = AUTOIP_STATE_PROBING;
andrewbonney 0:ec559500a63f 346 autoip->sent_num = 0;
andrewbonney 0:ec559500a63f 347 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
andrewbonney 0:ec559500a63f 348 ("autoip_start_probing(): changing state to PROBING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
andrewbonney 0:ec559500a63f 349 ip4_addr1_16(&netif->autoip->llipaddr), ip4_addr2_16(&netif->autoip->llipaddr),
andrewbonney 0:ec559500a63f 350 ip4_addr3_16(&netif->autoip->llipaddr), ip4_addr4_16(&netif->autoip->llipaddr)));
andrewbonney 0:ec559500a63f 351
andrewbonney 0:ec559500a63f 352 /* time to wait to first probe, this is randomly
andrewbonney 0:ec559500a63f 353 * choosen out of 0 to PROBE_WAIT seconds.
andrewbonney 0:ec559500a63f 354 * compliant to RFC 3927 Section 2.2.1
andrewbonney 0:ec559500a63f 355 */
andrewbonney 0:ec559500a63f 356 autoip->ttw = (u16_t)(LWIP_AUTOIP_RAND(netif) % (PROBE_WAIT * AUTOIP_TICKS_PER_SECOND));
andrewbonney 0:ec559500a63f 357
andrewbonney 0:ec559500a63f 358 /*
andrewbonney 0:ec559500a63f 359 * if we tried more then MAX_CONFLICTS we must limit our rate for
andrewbonney 0:ec559500a63f 360 * accquiring and probing address
andrewbonney 0:ec559500a63f 361 * compliant to RFC 3927 Section 2.2.1
andrewbonney 0:ec559500a63f 362 */
andrewbonney 0:ec559500a63f 363 if(autoip->tried_llipaddr > MAX_CONFLICTS) {
andrewbonney 0:ec559500a63f 364 autoip->ttw = RATE_LIMIT_INTERVAL * AUTOIP_TICKS_PER_SECOND;
andrewbonney 0:ec559500a63f 365 }
andrewbonney 0:ec559500a63f 366 }
andrewbonney 0:ec559500a63f 367
andrewbonney 0:ec559500a63f 368 /**
andrewbonney 0:ec559500a63f 369 * Handle a possible change in the network configuration.
andrewbonney 0:ec559500a63f 370 *
andrewbonney 0:ec559500a63f 371 * If there is an AutoIP address configured, take the interface down
andrewbonney 0:ec559500a63f 372 * and begin probing with the same address.
andrewbonney 0:ec559500a63f 373 */
andrewbonney 0:ec559500a63f 374 void
andrewbonney 0:ec559500a63f 375 autoip_network_changed(struct netif *netif)
andrewbonney 0:ec559500a63f 376 {
andrewbonney 0:ec559500a63f 377 if (netif->autoip && netif->autoip->state != AUTOIP_STATE_OFF) {
andrewbonney 0:ec559500a63f 378 netif_set_down(netif);
andrewbonney 0:ec559500a63f 379 autoip_start_probing(netif);
andrewbonney 0:ec559500a63f 380 }
andrewbonney 0:ec559500a63f 381 }
andrewbonney 0:ec559500a63f 382
andrewbonney 0:ec559500a63f 383 /**
andrewbonney 0:ec559500a63f 384 * Stop AutoIP client
andrewbonney 0:ec559500a63f 385 *
andrewbonney 0:ec559500a63f 386 * @param netif network interface on which stop the AutoIP client
andrewbonney 0:ec559500a63f 387 */
andrewbonney 0:ec559500a63f 388 err_t
andrewbonney 0:ec559500a63f 389 autoip_stop(struct netif *netif)
andrewbonney 0:ec559500a63f 390 {
andrewbonney 0:ec559500a63f 391 netif->autoip->state = AUTOIP_STATE_OFF;
andrewbonney 0:ec559500a63f 392 netif_set_down(netif);
andrewbonney 0:ec559500a63f 393 return ERR_OK;
andrewbonney 0:ec559500a63f 394 }
andrewbonney 0:ec559500a63f 395
andrewbonney 0:ec559500a63f 396 /**
andrewbonney 0:ec559500a63f 397 * Has to be called in loop every AUTOIP_TMR_INTERVAL milliseconds
andrewbonney 0:ec559500a63f 398 */
andrewbonney 0:ec559500a63f 399 void
andrewbonney 0:ec559500a63f 400 autoip_tmr()
andrewbonney 0:ec559500a63f 401 {
andrewbonney 0:ec559500a63f 402 struct netif *netif = netif_list;
andrewbonney 0:ec559500a63f 403 /* loop through netif's */
andrewbonney 0:ec559500a63f 404 while (netif != NULL) {
andrewbonney 0:ec559500a63f 405 /* only act on AutoIP configured interfaces */
andrewbonney 0:ec559500a63f 406 if (netif->autoip != NULL) {
andrewbonney 0:ec559500a63f 407 if(netif->autoip->lastconflict > 0) {
andrewbonney 0:ec559500a63f 408 netif->autoip->lastconflict--;
andrewbonney 0:ec559500a63f 409 }
andrewbonney 0:ec559500a63f 410
andrewbonney 0:ec559500a63f 411 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
andrewbonney 0:ec559500a63f 412 ("autoip_tmr() AutoIP-State: %"U16_F", ttw=%"U16_F"\n",
andrewbonney 0:ec559500a63f 413 (u16_t)(netif->autoip->state), netif->autoip->ttw));
andrewbonney 0:ec559500a63f 414
andrewbonney 0:ec559500a63f 415 switch(netif->autoip->state) {
andrewbonney 0:ec559500a63f 416 case AUTOIP_STATE_PROBING:
andrewbonney 0:ec559500a63f 417 if(netif->autoip->ttw > 0) {
andrewbonney 0:ec559500a63f 418 netif->autoip->ttw--;
andrewbonney 0:ec559500a63f 419 } else {
andrewbonney 0:ec559500a63f 420 if(netif->autoip->sent_num >= PROBE_NUM) {
andrewbonney 0:ec559500a63f 421 netif->autoip->state = AUTOIP_STATE_ANNOUNCING;
andrewbonney 0:ec559500a63f 422 netif->autoip->sent_num = 0;
andrewbonney 0:ec559500a63f 423 netif->autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND;
andrewbonney 0:ec559500a63f 424 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
andrewbonney 0:ec559500a63f 425 ("autoip_tmr(): changing state to ANNOUNCING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
andrewbonney 0:ec559500a63f 426 ip4_addr1_16(&netif->autoip->llipaddr), ip4_addr2_16(&netif->autoip->llipaddr),
andrewbonney 0:ec559500a63f 427 ip4_addr3_16(&netif->autoip->llipaddr), ip4_addr4_16(&netif->autoip->llipaddr)));
andrewbonney 0:ec559500a63f 428 } else {
andrewbonney 0:ec559500a63f 429 autoip_arp_probe(netif);
andrewbonney 0:ec559500a63f 430 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
andrewbonney 0:ec559500a63f 431 ("autoip_tmr() PROBING Sent Probe\n"));
andrewbonney 0:ec559500a63f 432 netif->autoip->sent_num++;
andrewbonney 0:ec559500a63f 433 /* calculate time to wait to next probe */
andrewbonney 0:ec559500a63f 434 netif->autoip->ttw = (u16_t)((LWIP_AUTOIP_RAND(netif) %
andrewbonney 0:ec559500a63f 435 ((PROBE_MAX - PROBE_MIN) * AUTOIP_TICKS_PER_SECOND) ) +
andrewbonney 0:ec559500a63f 436 PROBE_MIN * AUTOIP_TICKS_PER_SECOND);
andrewbonney 0:ec559500a63f 437 }
andrewbonney 0:ec559500a63f 438 }
andrewbonney 0:ec559500a63f 439 break;
andrewbonney 0:ec559500a63f 440
andrewbonney 0:ec559500a63f 441 case AUTOIP_STATE_ANNOUNCING:
andrewbonney 0:ec559500a63f 442 if(netif->autoip->ttw > 0) {
andrewbonney 0:ec559500a63f 443 netif->autoip->ttw--;
andrewbonney 0:ec559500a63f 444 } else {
andrewbonney 0:ec559500a63f 445 if(netif->autoip->sent_num == 0) {
andrewbonney 0:ec559500a63f 446 /* We are here the first time, so we waited ANNOUNCE_WAIT seconds
andrewbonney 0:ec559500a63f 447 * Now we can bind to an IP address and use it.
andrewbonney 0:ec559500a63f 448 *
andrewbonney 0:ec559500a63f 449 * autoip_bind calls netif_set_up. This triggers a gratuitous ARP
andrewbonney 0:ec559500a63f 450 * which counts as an announcement.
andrewbonney 0:ec559500a63f 451 */
andrewbonney 0:ec559500a63f 452 autoip_bind(netif);
andrewbonney 0:ec559500a63f 453 } else {
andrewbonney 0:ec559500a63f 454 autoip_arp_announce(netif);
andrewbonney 0:ec559500a63f 455 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
andrewbonney 0:ec559500a63f 456 ("autoip_tmr() ANNOUNCING Sent Announce\n"));
andrewbonney 0:ec559500a63f 457 }
andrewbonney 0:ec559500a63f 458 netif->autoip->ttw = ANNOUNCE_INTERVAL * AUTOIP_TICKS_PER_SECOND;
andrewbonney 0:ec559500a63f 459 netif->autoip->sent_num++;
andrewbonney 0:ec559500a63f 460
andrewbonney 0:ec559500a63f 461 if(netif->autoip->sent_num >= ANNOUNCE_NUM) {
andrewbonney 0:ec559500a63f 462 netif->autoip->state = AUTOIP_STATE_BOUND;
andrewbonney 0:ec559500a63f 463 netif->autoip->sent_num = 0;
andrewbonney 0:ec559500a63f 464 netif->autoip->ttw = 0;
andrewbonney 0:ec559500a63f 465 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
andrewbonney 0:ec559500a63f 466 ("autoip_tmr(): changing state to BOUND: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
andrewbonney 0:ec559500a63f 467 ip4_addr1_16(&netif->autoip->llipaddr), ip4_addr2_16(&netif->autoip->llipaddr),
andrewbonney 0:ec559500a63f 468 ip4_addr3_16(&netif->autoip->llipaddr), ip4_addr4_16(&netif->autoip->llipaddr)));
andrewbonney 0:ec559500a63f 469 }
andrewbonney 0:ec559500a63f 470 }
andrewbonney 0:ec559500a63f 471 break;
andrewbonney 0:ec559500a63f 472 }
andrewbonney 0:ec559500a63f 473 }
andrewbonney 0:ec559500a63f 474 /* proceed to next network interface */
andrewbonney 0:ec559500a63f 475 netif = netif->next;
andrewbonney 0:ec559500a63f 476 }
andrewbonney 0:ec559500a63f 477 }
andrewbonney 0:ec559500a63f 478
andrewbonney 0:ec559500a63f 479 /**
andrewbonney 0:ec559500a63f 480 * Handles every incoming ARP Packet, called by etharp_arp_input.
andrewbonney 0:ec559500a63f 481 *
andrewbonney 0:ec559500a63f 482 * @param netif network interface to use for autoip processing
andrewbonney 0:ec559500a63f 483 * @param hdr Incoming ARP packet
andrewbonney 0:ec559500a63f 484 */
andrewbonney 0:ec559500a63f 485 void
andrewbonney 0:ec559500a63f 486 autoip_arp_reply(struct netif *netif, struct etharp_hdr *hdr)
andrewbonney 0:ec559500a63f 487 {
andrewbonney 0:ec559500a63f 488 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_arp_reply()\n"));
andrewbonney 0:ec559500a63f 489 if ((netif->autoip != NULL) && (netif->autoip->state != AUTOIP_STATE_OFF)) {
andrewbonney 0:ec559500a63f 490 /* when ip.src == llipaddr && hw.src != netif->hwaddr
andrewbonney 0:ec559500a63f 491 *
andrewbonney 0:ec559500a63f 492 * when probing ip.dst == llipaddr && hw.src != netif->hwaddr
andrewbonney 0:ec559500a63f 493 * we have a conflict and must solve it
andrewbonney 0:ec559500a63f 494 */
andrewbonney 0:ec559500a63f 495 ip_addr_t sipaddr, dipaddr;
andrewbonney 0:ec559500a63f 496 struct eth_addr netifaddr;
andrewbonney 0:ec559500a63f 497 netifaddr.addr[0] = netif->hwaddr[0];
andrewbonney 0:ec559500a63f 498 netifaddr.addr[1] = netif->hwaddr[1];
andrewbonney 0:ec559500a63f 499 netifaddr.addr[2] = netif->hwaddr[2];
andrewbonney 0:ec559500a63f 500 netifaddr.addr[3] = netif->hwaddr[3];
andrewbonney 0:ec559500a63f 501 netifaddr.addr[4] = netif->hwaddr[4];
andrewbonney 0:ec559500a63f 502 netifaddr.addr[5] = netif->hwaddr[5];
andrewbonney 0:ec559500a63f 503
andrewbonney 0:ec559500a63f 504 /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
andrewbonney 0:ec559500a63f 505 * structure packing (not using structure copy which breaks strict-aliasing rules).
andrewbonney 0:ec559500a63f 506 */
andrewbonney 0:ec559500a63f 507 IPADDR2_COPY(&sipaddr, &hdr->sipaddr);
andrewbonney 0:ec559500a63f 508 IPADDR2_COPY(&dipaddr, &hdr->dipaddr);
andrewbonney 0:ec559500a63f 509
andrewbonney 0:ec559500a63f 510 if ((netif->autoip->state == AUTOIP_STATE_PROBING) ||
andrewbonney 0:ec559500a63f 511 ((netif->autoip->state == AUTOIP_STATE_ANNOUNCING) &&
andrewbonney 0:ec559500a63f 512 (netif->autoip->sent_num == 0))) {
andrewbonney 0:ec559500a63f 513 /* RFC 3927 Section 2.2.1:
andrewbonney 0:ec559500a63f 514 * from beginning to after ANNOUNCE_WAIT
andrewbonney 0:ec559500a63f 515 * seconds we have a conflict if
andrewbonney 0:ec559500a63f 516 * ip.src == llipaddr OR
andrewbonney 0:ec559500a63f 517 * ip.dst == llipaddr && hw.src != own hwaddr
andrewbonney 0:ec559500a63f 518 */
andrewbonney 0:ec559500a63f 519 if ((ip_addr_cmp(&sipaddr, &netif->autoip->llipaddr)) ||
andrewbonney 0:ec559500a63f 520 (ip_addr_cmp(&dipaddr, &netif->autoip->llipaddr) &&
andrewbonney 0:ec559500a63f 521 !eth_addr_cmp(&netifaddr, &hdr->shwaddr))) {
andrewbonney 0:ec559500a63f 522 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING,
andrewbonney 0:ec559500a63f 523 ("autoip_arp_reply(): Probe Conflict detected\n"));
andrewbonney 0:ec559500a63f 524 autoip_restart(netif);
andrewbonney 0:ec559500a63f 525 }
andrewbonney 0:ec559500a63f 526 } else {
andrewbonney 0:ec559500a63f 527 /* RFC 3927 Section 2.5:
andrewbonney 0:ec559500a63f 528 * in any state we have a conflict if
andrewbonney 0:ec559500a63f 529 * ip.src == llipaddr && hw.src != own hwaddr
andrewbonney 0:ec559500a63f 530 */
andrewbonney 0:ec559500a63f 531 if (ip_addr_cmp(&sipaddr, &netif->autoip->llipaddr) &&
andrewbonney 0:ec559500a63f 532 !eth_addr_cmp(&netifaddr, &hdr->shwaddr)) {
andrewbonney 0:ec559500a63f 533 LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING,
andrewbonney 0:ec559500a63f 534 ("autoip_arp_reply(): Conflicting ARP-Packet detected\n"));
andrewbonney 0:ec559500a63f 535 autoip_handle_arp_conflict(netif);
andrewbonney 0:ec559500a63f 536 }
andrewbonney 0:ec559500a63f 537 }
andrewbonney 0:ec559500a63f 538 }
andrewbonney 0:ec559500a63f 539 }
andrewbonney 0:ec559500a63f 540
andrewbonney 0:ec559500a63f 541 #endif /* LWIP_AUTOIP */