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

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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