| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /********************************************************************* |
| 2 | * |
||
| 3 | * AutoIP Client |
||
| 4 | * Module for Microchip TCP/IP Stack |
||
| 5 | * -Provides AutoIP Code for automatically allocating a link-layer |
||
| 6 | * address |
||
| 7 | * |
||
| 8 | * -Reference: RFC 3927 |
||
| 9 | ********************************************************************* |
||
| 10 | * FileName: AutoIP.c |
||
| 11 | * Dependencies: UDP, ARP |
||
| 12 | * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32 |
||
| 13 | * Compiler: Microchip C32 v1.05 or higher |
||
| 14 | * Microchip C30 v3.12 or higher |
||
| 15 | * Microchip C18 v3.30 or higher |
||
| 16 | * HI-TECH PICC-18 PRO 9.63PL2 or higher |
||
| 17 | * Company: Microchip Technology, Inc. |
||
| 18 | * |
||
| 19 | * Software License Agreement |
||
| 20 | * |
||
| 21 | * Copyright (C) 2009 Microchip Technology Inc. All rights |
||
| 22 | * reserved. |
||
| 23 | * |
||
| 24 | * Microchip licenses to you the right to use, modify, copy, and |
||
| 25 | * distribute: |
||
| 26 | * (i) the Software when embedded on a Microchip microcontroller or |
||
| 27 | * digital signal controller product ("Device") which is |
||
| 28 | * integrated into Licensee's product; or |
||
| 29 | * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h, |
||
| 30 | * ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device |
||
| 31 | * used in conjunction with a Microchip ethernet controller for |
||
| 32 | * the sole purpose of interfacing with the ethernet controller. |
||
| 33 | * |
||
| 34 | * You should refer to the license agreement accompanying this |
||
| 35 | * Software for additional information regarding your rights and |
||
| 36 | * obligations. |
||
| 37 | * |
||
| 38 | * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT |
||
| 39 | * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT |
||
| 40 | * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
||
| 41 | * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
||
| 42 | * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR |
||
| 43 | * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF |
||
| 44 | * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS |
||
| 45 | * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE |
||
| 46 | * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER |
||
| 47 | * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT |
||
| 48 | * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. |
||
| 49 | * |
||
| 50 | * Author Date Comment |
||
| 51 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 52 | * Peter Reen 7/10/09 Original (Rev 1.0) |
||
| 53 | ********************************************************************/ |
||
| 54 | |||
| 55 | #define __AUTOIP_C |
||
| 56 | |||
| 57 | #include "TCPIPConfig.h" |
||
| 58 | |||
| 59 | #if defined (STACK_USE_AUTO_IP) |
||
| 60 | |||
| 61 | #include "TCPIP Stack/TCPIP.h" |
||
| 62 | |||
| 63 | // Set of variables for each network interface |
||
| 64 | typedef struct |
||
| 65 | { |
||
| 66 | // Global state-tracker variable |
||
| 67 | SM_AUTOIP smAUTOIPState; |
||
| 68 | // Global backoff-time counter |
||
| 69 | DWORD gAutoIPConflictTimer; |
||
| 70 | // Records the last time at which an AutoIP event occured |
||
| 71 | DWORD eventTime; |
||
| 72 | // An ARP packet |
||
| 73 | ARP_PACKET packet; |
||
| 74 | // Holds the number of ticks needed for a random delay |
||
| 75 | DWORD randomDelay; |
||
| 76 | // The seed value for the RNG |
||
| 77 | DWORD wRandSeed; |
||
| 78 | |||
| 79 | union |
||
| 80 | { |
||
| 81 | struct |
||
| 82 | { |
||
| 83 | unsigned char gDisableAutoIP : 1; // Prevents AutoIP from initializing if the user wants a static address |
||
| 84 | unsigned char bConfigureAutoIP : 1; |
||
| 85 | unsigned char bLastLinkState : 1; |
||
| 86 | unsigned char checkAddress : 1; |
||
| 87 | unsigned char bCurrentLinkState : 1; |
||
| 88 | #if defined (STACK_USE_DHCP_CLIENT) |
||
| 89 | unsigned char bLastDHCPState : 1; |
||
| 90 | #endif |
||
| 91 | } bits; |
||
| 92 | BYTE val; |
||
| 93 | } flags; |
||
| 94 | |||
| 95 | |||
| 96 | BYTE conflicts; |
||
| 97 | } AUTOIP_CLIENT_VARS; |
||
| 98 | |||
| 99 | // Prototypes |
||
| 100 | |||
| 101 | void AutoIPRandSeed (DWORD seed, BYTE vInterface); |
||
| 102 | DWORD AutoIPRand (BYTE vInterface); |
||
| 103 | |||
| 104 | |||
| 105 | /***************************************************************************** |
||
| 106 | Function: |
||
| 107 | static void LoadState(BYTE vInterface) |
||
| 108 | |||
| 109 | Summary: |
||
| 110 | Saves the AutoIP client state information structure to the appropriate |
||
| 111 | location and loads AutoIP client with the state information for the specified |
||
| 112 | interface. |
||
| 113 | |||
| 114 | Description: |
||
| 115 | Saves the AutoIP client state information structure to the appropriate |
||
| 116 | location and loads AutoIP client with the state information for the specified |
||
| 117 | interface. |
||
| 118 | |||
| 119 | Precondition: |
||
| 120 | None |
||
| 121 | |||
| 122 | Parameters: |
||
| 123 | None |
||
| 124 | |||
| 125 | Returns: |
||
| 126 | None |
||
| 127 | |||
| 128 | Remarks: |
||
| 129 | This function does nothing when you only have one physical interface. |
||
| 130 | ***************************************************************************/ |
||
| 131 | #if NETWORK_INTERFACES > 1 |
||
| 132 | |||
| 133 | static AUTOIP_CLIENT_VARS AutoIPClients[NETWORK_INTERFACES]; |
||
| 134 | static AUTOIP_CLIENT_VARS *SelectedAutoIPClient; |
||
| 135 | #define AutoIPClient (*SelectedAutoIPClient) |
||
| 136 | #define LoadState(v) do(SelectedAutoIPClient = &AutoIPClients[v])while(0) |
||
| 137 | |||
| 138 | #else |
||
| 139 | |||
| 140 | static AUTOIP_CLIENT_VARS AutoIPClient; |
||
| 141 | #define LoadState(v) |
||
| 142 | |||
| 143 | #endif |
||
| 144 | |||
| 145 | |||
| 146 | /***************************************************************************** |
||
| 147 | Function: |
||
| 148 | void AutoIPInit(BYTE vInterface) |
||
| 149 | |||
| 150 | Summary: |
||
| 151 | Resets the AutoIP client module for the specified interface. |
||
| 152 | |||
| 153 | Description: |
||
| 154 | Resets the AutoIP client module |
||
| 155 | |||
| 156 | Precondition: |
||
| 157 | None |
||
| 158 | |||
| 159 | Parameters: |
||
| 160 | vInterface - Interface number to initialize AutoIP client state variables |
||
| 161 | for. If you only have one interface, specify 0x00. |
||
| 162 | |||
| 163 | Returns: |
||
| 164 | None |
||
| 165 | |||
| 166 | Remarks: |
||
| 167 | This function may be called multiple times throughout the life of the |
||
| 168 | application, if desired. |
||
| 169 | ***************************************************************************/ |
||
| 170 | |||
| 171 | void AutoIPInit (BYTE vInterface) |
||
| 172 | { |
||
| 173 | LoadState (vInterface); |
||
| 174 | |||
| 175 | AutoIPClient.flags.bits.bConfigureAutoIP = FALSE; |
||
| 176 | AutoIPClient.smAUTOIPState = SM_AUTOIP_DISABLED; |
||
| 177 | AutoIPClient.gAutoIPConflictTimer = 0; |
||
| 178 | AutoIPClient.flags.bits.bLastLinkState = FALSE; |
||
| 179 | AutoIPClient.flags.bits.checkAddress = FALSE; |
||
| 180 | AutoIPClient.conflicts = 0; |
||
| 181 | #if defined (STACK_USE_DHCP_CLIENT) |
||
| 182 | AutoIPClient.flags.bits.bLastDHCPState = FALSE; |
||
| 183 | #endif |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | /***************************************************************************** |
||
| 188 | Function: |
||
| 189 | BOOL AutoIPIsConfigured (BYTE vInterface) |
||
| 190 | |||
| 191 | Summary: |
||
| 192 | Determines if the AutoIP interface has successfully claimed an IP address |
||
| 193 | |||
| 194 | Description: |
||
| 195 | Determines if the AutoIP interface has successfully claimed an IP address |
||
| 196 | |||
| 197 | Precondition: |
||
| 198 | None |
||
| 199 | |||
| 200 | Parameters: |
||
| 201 | vInterface - Interface number to check AutoIP configuration for. If you |
||
| 202 | only have one interface, specify 0x00. |
||
| 203 | |||
| 204 | Returns: |
||
| 205 | TRUE - The AutoIP client has successfully claimed an IP address |
||
| 206 | FALSE - The AutoIP client has not claimed an IP address |
||
| 207 | |||
| 208 | Remarks: |
||
| 209 | None |
||
| 210 | ***************************************************************************/ |
||
| 211 | BOOL AutoIPIsConfigured (BYTE vInterface) |
||
| 212 | { |
||
| 213 | LoadState(vInterface); |
||
| 214 | return (AutoIPClient.smAUTOIPState == SM_AUTOIP_CONFIGURED) ? TRUE : FALSE; |
||
| 215 | } |
||
| 216 | |||
| 217 | /***************************************************************************** |
||
| 218 | Function: |
||
| 219 | BOOL AutoIPConfigIsInProgress (BYTE vInterface) |
||
| 220 | |||
| 221 | Summary: |
||
| 222 | Determines if the AutoIP address is being configured |
||
| 223 | |||
| 224 | Description: |
||
| 225 | Determines if the AutoIP address is being configured |
||
| 226 | |||
| 227 | Precondition: |
||
| 228 | None |
||
| 229 | |||
| 230 | Parameters: |
||
| 231 | vInterface - Interface number to check AutoIP configuration for. If you |
||
| 232 | only have one interface, specify 0x00. |
||
| 233 | |||
| 234 | Returns: |
||
| 235 | TRUE - The AutoIP client is being configured |
||
| 236 | FALSE - The AutoIP client is not in configuration mode |
||
| 237 | |||
| 238 | Remarks: |
||
| 239 | None |
||
| 240 | ***************************************************************************/ |
||
| 241 | BOOL AutoIPConfigIsInProgress (BYTE vInterface) |
||
| 242 | { |
||
| 243 | LoadState(vInterface); |
||
| 244 | return AutoIPClient.flags.bits.bConfigureAutoIP; |
||
| 245 | } |
||
| 246 | |||
| 247 | /***************************************************************************** |
||
| 248 | Function: |
||
| 249 | void AutoIPTasks(void) |
||
| 250 | |||
| 251 | Summary: |
||
| 252 | Performs periodic AutoIP tasks. |
||
| 253 | |||
| 254 | Description: |
||
| 255 | This function performs any periodic tasks requied by the AutoIP module, |
||
| 256 | such as re/transmitting gratuitous ARP packets and defending addresses |
||
| 257 | |||
| 258 | Precondition: |
||
| 259 | None |
||
| 260 | |||
| 261 | Parameters: |
||
| 262 | None |
||
| 263 | |||
| 264 | Returns: |
||
| 265 | None |
||
| 266 | ***************************************************************************/ |
||
| 267 | |||
| 268 | void AutoIPTasks(void) |
||
| 269 | { |
||
| 270 | BYTE i; |
||
| 271 | |||
| 272 | for (i = 0; i < NETWORK_INTERFACES; i++) |
||
| 273 | { |
||
| 274 | LoadState (i); |
||
| 275 | AutoIPClient.flags.bits.bCurrentLinkState = MACIsLinked(); |
||
| 276 | if(AutoIPClient.flags.bits.bCurrentLinkState != AutoIPClient.flags.bits.bLastLinkState) |
||
| 277 | { |
||
| 278 | AutoIPClient.flags.bits.bLastLinkState = AutoIPClient.flags.bits.bCurrentLinkState; |
||
| 279 | if(!AutoIPClient.flags.bits.bCurrentLinkState) |
||
| 280 | { |
||
| 281 | AutoIPClient.flags.bits.bConfigureAutoIP = FALSE; |
||
| 282 | AutoIPClient.smAUTOIPState = SM_AUTOIP_DISABLED; |
||
| 283 | AppConfig.MyIPAddr.Val = AppConfig.DefaultIPAddr.Val; |
||
| 284 | AppConfig.MyMask.Val = AppConfig.DefaultMask.Val; |
||
| 285 | } |
||
| 286 | else |
||
| 287 | { |
||
| 288 | AutoIPClient.smAUTOIPState = SM_AUTOIP_INIT_RNG; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | #if defined (STACK_USE_DHCP_CLIENT) |
||
| 293 | if (DHCPIsBound(i)) |
||
| 294 | { |
||
| 295 | AutoIPClient.flags.bits.bConfigureAutoIP = FALSE; |
||
| 296 | AutoIPClient.smAUTOIPState = SM_AUTOIP_DISABLED; |
||
| 297 | AutoIPClient.flags.bits.bLastDHCPState = TRUE; |
||
| 298 | } |
||
| 299 | else |
||
| 300 | { |
||
| 301 | if (AutoIPClient.flags.bits.bLastDHCPState == TRUE) |
||
| 302 | { |
||
| 303 | if (AutoIPClient.flags.bits.bCurrentLinkState) |
||
| 304 | AutoIPClient.smAUTOIPState = SM_AUTOIP_INIT_RNG; |
||
| 305 | } |
||
| 306 | AutoIPClient.flags.bits.bLastDHCPState = FALSE; |
||
| 307 | } |
||
| 308 | #endif |
||
| 309 | |||
| 310 | |||
| 311 | if (AutoIPClient.flags.bits.gDisableAutoIP == TRUE) |
||
| 312 | { |
||
| 313 | AutoIPClient.flags.bits.bConfigureAutoIP = FALSE; |
||
| 314 | AutoIPClient.smAUTOIPState = SM_AUTOIP_DISABLED; |
||
| 315 | } |
||
| 316 | |||
| 317 | |||
| 318 | switch (AutoIPClient.smAUTOIPState) |
||
| 319 | { |
||
| 320 | // Default no-AutoIP case |
||
| 321 | case SM_AUTOIP_DISABLED: |
||
| 322 | |||
| 323 | break; |
||
| 324 | |||
| 325 | // Initializes the random number generator with a seed based on the MAC address |
||
| 326 | case SM_AUTOIP_INIT_RNG: |
||
| 327 | AutoIPRandSeed (((DWORD)AppConfig.MyMACAddr.v[0] + ((DWORD)AppConfig.MyMACAddr.v[1] << 8) + \ |
||
| 328 | ((DWORD)AppConfig.MyMACAddr.v[2] << 16) + ((DWORD)AppConfig.MyMACAddr.v[3] << 24) + \ |
||
| 329 | ((DWORD)AppConfig.MyMACAddr.v[4]) + ((DWORD)AppConfig.MyMACAddr.v[5] << 8)), i); |
||
| 330 | |||
| 331 | AutoIPClient.smAUTOIPState = SM_AUTOIP_CHECK_ADDRESS; |
||
| 332 | |||
| 333 | // Check the address to see if it's in use before we write it into AppConfig |
||
| 334 | case SM_AUTOIP_CHECK_ADDRESS: |
||
| 335 | |||
| 336 | if (AutoIPClient.flags.bits.checkAddress == FALSE) |
||
| 337 | { |
||
| 338 | AutoIPClient.flags.bits.checkAddress = TRUE; |
||
| 339 | |||
| 340 | AppConfig.MyMask.Val = 0x00000000; |
||
| 341 | |||
| 342 | // Generate a random IP address (based on the MAC address) to try and claim. |
||
| 343 | // Dynamic link-local addresses can fall within the range: |
||
| 344 | // 169.254.1.0 - 169.254.254.255 |
||
| 345 | AutoIPClient.packet.TargetIPAddr.byte.MB = AutoIPRand(i) % 256; |
||
| 346 | AutoIPClient.packet.TargetIPAddr.byte.UB = (AutoIPRand(i) % 254) + 1; |
||
| 347 | AutoIPClient.packet.TargetIPAddr.word.LW = 0xFEA9; |
||
| 348 | |||
| 349 | ARPResolve (&AutoIPClient.packet.TargetIPAddr); |
||
| 350 | |||
| 351 | AutoIPClient.eventTime = TickGet(); |
||
| 352 | } |
||
| 353 | |||
| 354 | if (!ARPIsResolved (&AutoIPClient.packet.TargetIPAddr, &AutoIPClient.packet.TargetMACAddr)) |
||
| 355 | { |
||
| 356 | if (TickGet() - AutoIPClient.eventTime > TICK_SECOND) |
||
| 357 | { |
||
| 358 | AutoIPClient.smAUTOIPState = SM_AUTOIP_SETUP_MESSAGE; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | else |
||
| 362 | { |
||
| 363 | AutoIPClient.flags.bits.checkAddress = FALSE; |
||
| 364 | } |
||
| 365 | |||
| 366 | break; |
||
| 367 | |||
| 368 | // Set up an ARP packet |
||
| 369 | case SM_AUTOIP_SETUP_MESSAGE: |
||
| 370 | |||
| 371 | AutoIPClient.flags.bits.checkAddress = FALSE; |
||
| 372 | |||
| 373 | // Set the bConfigureAutoIP flag- This flag will cause an AutoIP conflict |
||
| 374 | // if a response packet is received from the address we're trying to claim. |
||
| 375 | AutoIPClient.flags.bits.bConfigureAutoIP = TRUE; |
||
| 376 | |||
| 377 | // Configure the fields for a gratuitous ARP packet |
||
| 378 | AutoIPClient.packet.Operation = ARP_OPERATION_REQ; |
||
| 379 | |||
| 380 | AutoIPClient.packet.TargetMACAddr.v[0] = 0xff; |
||
| 381 | AutoIPClient.packet.TargetMACAddr.v[1] = 0xff; |
||
| 382 | AutoIPClient.packet.TargetMACAddr.v[2] = 0xff; |
||
| 383 | AutoIPClient.packet.TargetMACAddr.v[3] = 0xff; |
||
| 384 | AutoIPClient.packet.TargetMACAddr.v[4] = 0xff; |
||
| 385 | AutoIPClient.packet.TargetMACAddr.v[5] = 0xff; |
||
| 386 | |||
| 387 | AppConfig.MyIPAddr = AutoIPClient.packet.TargetIPAddr; |
||
| 388 | AppConfig.MyMask.Val = 0x0000FFFF; |
||
| 389 | memcpy(&AutoIPClient.packet.SenderMACAddr, (void*)&AppConfig.MyMACAddr, sizeof(AutoIPClient.packet.SenderMACAddr)); |
||
| 390 | AutoIPClient.packet.HardwareType = HW_ETHERNET; |
||
| 391 | AutoIPClient.packet.Protocol = ARP_IP; |
||
| 392 | AutoIPClient.packet.MACAddrLen = sizeof(MAC_ADDR); |
||
| 393 | AutoIPClient.packet.ProtocolLen = sizeof(IP_ADDR); |
||
| 394 | AutoIPClient.packet.SenderIPAddr.Val = AutoIPClient.packet.TargetIPAddr.Val; |
||
| 395 | |||
| 396 | SwapARPPacket(&AutoIPClient.packet); |
||
| 397 | |||
| 398 | // Generate a random delay between 0 and 1 second |
||
| 399 | AutoIPClient.randomDelay = ((rand() % 20) * TICK_SECOND) / 20; |
||
| 400 | // Store the current time |
||
| 401 | AutoIPClient.eventTime = TickGet(); |
||
| 402 | |||
| 403 | // Set the state to send the ARP packet |
||
| 404 | AutoIPClient.smAUTOIPState = SM_AUTOIP_GRATUITOUS_ARP1; |
||
| 405 | |||
| 406 | break; |
||
| 407 | |||
| 408 | // Send a gratuitous ARP packet to try and claim our address |
||
| 409 | case SM_AUTOIP_GRATUITOUS_ARP1: |
||
| 410 | case SM_AUTOIP_GRATUITOUS_ARP2: |
||
| 411 | case SM_AUTOIP_GRATUITOUS_ARP3: |
||
| 412 | // Check to ensure we've passed the delay time |
||
| 413 | if (TickGet() - AutoIPClient.eventTime > AutoIPClient.randomDelay) |
||
| 414 | { |
||
| 415 | // Store the new event time |
||
| 416 | AutoIPClient.eventTime = TickGet(); |
||
| 417 | // Generate a new random delay between 1 and 2 seconds |
||
| 418 | AutoIPClient.randomDelay = TICK_SECOND + (((rand() % 20) * TICK_SECOND) / 20); |
||
| 419 | |||
| 420 | // Transmit the packet |
||
| 421 | while(!MACIsTxReady()); |
||
| 422 | MACSetWritePtr(BASE_TX_ADDR); |
||
| 423 | |||
| 424 | MACPutHeader(&AutoIPClient.packet.TargetMACAddr, MAC_ARP, sizeof(AutoIPClient.packet)); |
||
| 425 | MACPutArray((BYTE*)&AutoIPClient.packet, sizeof(AutoIPClient.packet)); |
||
| 426 | MACFlush(); |
||
| 427 | |||
| 428 | // Increment the probe iteration or increment to the delay state |
||
| 429 | AutoIPClient.smAUTOIPState++; |
||
| 430 | } |
||
| 431 | break; |
||
| 432 | |||
| 433 | // Delay for 1-2 seconds after sending the third ARP request before |
||
| 434 | // entering the configured state |
||
| 435 | case SM_AUTOIP_DELAY: |
||
| 436 | if (TickGet() - AutoIPClient.eventTime > AutoIPClient.randomDelay) |
||
| 437 | AutoIPClient.smAUTOIPState = SM_AUTOIP_CONFIGURED; |
||
| 438 | break; |
||
| 439 | |||
| 440 | // Configure the module to limit the rate at which packets are sent |
||
| 441 | case SM_AUTOIP_RATE_LIMIT_SET: |
||
| 442 | AutoIPClient.eventTime = TickGet(); |
||
| 443 | AppConfig.MyIPAddr.v[0] = MY_DEFAULT_IP_ADDR_BYTE1; |
||
| 444 | AppConfig.MyIPAddr.v[1] = MY_DEFAULT_IP_ADDR_BYTE2; |
||
| 445 | AppConfig.MyIPAddr.v[2] = MY_DEFAULT_IP_ADDR_BYTE3; |
||
| 446 | AppConfig.MyIPAddr.v[3] = MY_DEFAULT_IP_ADDR_BYTE4; |
||
| 447 | AutoIPClient.smAUTOIPState = SM_AUTOIP_RATE_LIMIT_WAIT; |
||
| 448 | break; |
||
| 449 | |||
| 450 | // Ensure that we don't try more than one address every 60 seconds |
||
| 451 | case SM_AUTOIP_RATE_LIMIT_WAIT: |
||
| 452 | if (TickGet() - AutoIPClient.eventTime > TICK_SECOND * 60) |
||
| 453 | AutoIPClient.smAUTOIPState = SM_AUTOIP_CHECK_ADDRESS; |
||
| 454 | break; |
||
| 455 | |||
| 456 | // Configured state |
||
| 457 | case SM_AUTOIP_CONFIGURED: |
||
| 458 | AutoIPClient.flags.bits.bConfigureAutoIP = FALSE; |
||
| 459 | break; |
||
| 460 | |||
| 461 | // Address defense state |
||
| 462 | case SM_AUTOIP_DEFEND: |
||
| 463 | // Prepare and send an ARP response |
||
| 464 | AutoIPClient.packet.Operation = ARP_OPERATION_RESP; |
||
| 465 | AutoIPClient.packet.HardwareType = HW_ETHERNET; |
||
| 466 | AutoIPClient.packet.Protocol = ARP_IP; |
||
| 467 | |||
| 468 | SwapARPPacket(&AutoIPClient.packet); |
||
| 469 | |||
| 470 | while(!MACIsTxReady()); |
||
| 471 | MACSetWritePtr(BASE_TX_ADDR); |
||
| 472 | |||
| 473 | MACPutHeader(&AutoIPClient.packet.TargetMACAddr, MAC_ARP, sizeof(AutoIPClient.packet)); |
||
| 474 | MACPutArray((BYTE*)&AutoIPClient.packet, sizeof(AutoIPClient.packet)); |
||
| 475 | MACFlush(); |
||
| 476 | |||
| 477 | AutoIPClient.smAUTOIPState = SM_AUTOIP_CONFIGURED; |
||
| 478 | break; |
||
| 479 | } |
||
| 480 | } |
||
| 481 | } |
||
| 482 | |||
| 483 | /***************************************************************************** |
||
| 484 | Function: |
||
| 485 | void AutoIPConflict(BYTE vInterface) |
||
| 486 | |||
| 487 | Summary: |
||
| 488 | Handles AutoIP address conflicts. |
||
| 489 | |||
| 490 | Description: |
||
| 491 | This function will change the state machine to handle AutoIP address |
||
| 492 | conflicts. |
||
| 493 | |||
| 494 | Precondition: |
||
| 495 | None |
||
| 496 | |||
| 497 | Parameters: |
||
| 498 | vInterface - Interface number to cause an AutoIP conflict for. If |
||
| 499 | you only have one interface, specify 0x00. |
||
| 500 | |||
| 501 | Returns: |
||
| 502 | None |
||
| 503 | ***************************************************************************/ |
||
| 504 | void AutoIPConflict (BYTE vInterface) |
||
| 505 | { |
||
| 506 | LoadState (vInterface); |
||
| 507 | |||
| 508 | AutoIPClient.conflicts++; |
||
| 509 | |||
| 510 | // State handler |
||
| 511 | switch (AutoIPClient.smAUTOIPState) |
||
| 512 | { |
||
| 513 | // During configuration, if there is a conflict, immediately give |
||
| 514 | // up the current address and select a new one. |
||
| 515 | // If more than 10 conflicts have occured, limit the rate of |
||
| 516 | // address retrys to 1 every 60 seconds. |
||
| 517 | case SM_AUTOIP_INIT_RNG: |
||
| 518 | case SM_AUTOIP_CHECK_ADDRESS: |
||
| 519 | case SM_AUTOIP_SETUP_MESSAGE: |
||
| 520 | case SM_AUTOIP_GRATUITOUS_ARP1: |
||
| 521 | case SM_AUTOIP_GRATUITOUS_ARP2: |
||
| 522 | case SM_AUTOIP_GRATUITOUS_ARP3: |
||
| 523 | case SM_AUTOIP_DELAY: |
||
| 524 | if (AutoIPClient.conflicts >= 10u) |
||
| 525 | AutoIPClient.smAUTOIPState = SM_AUTOIP_RATE_LIMIT_SET; |
||
| 526 | else |
||
| 527 | AutoIPClient.smAUTOIPState = SM_AUTOIP_CHECK_ADDRESS; |
||
| 528 | break; |
||
| 529 | case SM_AUTOIP_RATE_LIMIT_SET: |
||
| 530 | case SM_AUTOIP_RATE_LIMIT_WAIT: |
||
| 531 | case SM_AUTOIP_DISABLED: |
||
| 532 | AutoIPClient.conflicts--; |
||
| 533 | break; |
||
| 534 | // If there is a conflict while we have an address configured, |
||
| 535 | // send a defense packet. If more than one conflict occurs within |
||
| 536 | // 10 seconds, claim a new address. |
||
| 537 | case SM_AUTOIP_CONFIGURED: |
||
| 538 | case SM_AUTOIP_DEFEND: |
||
| 539 | if (AutoIPClient.gAutoIPConflictTimer != 0u) |
||
| 540 | { |
||
| 541 | if (TickGet() - AutoIPClient.gAutoIPConflictTimer < TICK_SECOND * 10) |
||
| 542 | { |
||
| 543 | AutoIPClient.smAUTOIPState = SM_AUTOIP_CHECK_ADDRESS; |
||
| 544 | return;; |
||
| 545 | } |
||
| 546 | } |
||
| 547 | AutoIPClient.gAutoIPConflictTimer = TickGet(); |
||
| 548 | AutoIPClient.smAUTOIPState = SM_AUTOIP_DEFEND; |
||
| 549 | break; |
||
| 550 | |||
| 551 | } |
||
| 552 | } |
||
| 553 | |||
| 554 | |||
| 555 | /***************************************************************************** |
||
| 556 | Function: |
||
| 557 | void AutoIPDisable (BYTE vInterface) |
||
| 558 | |||
| 559 | Summary: |
||
| 560 | Disables the AutoIP module. |
||
| 561 | |||
| 562 | Description: |
||
| 563 | Disables the AutoIP module. If DHCP is enabled, this function will |
||
| 564 | reset the IP address to the default address if no DHCP address has been |
||
| 565 | bound. If DHCP is disabled, this function will reset the IP address to |
||
| 566 | the default address. |
||
| 567 | |||
| 568 | Precondition: |
||
| 569 | None |
||
| 570 | |||
| 571 | Parameters: |
||
| 572 | vInterface - Interface number to disable AutoIP for. If you only |
||
| 573 | have one interface, specify 0x00. |
||
| 574 | |||
| 575 | Returns: |
||
| 576 | None |
||
| 577 | |||
| 578 | Remarks: |
||
| 579 | None |
||
| 580 | ***************************************************************************/ |
||
| 581 | |||
| 582 | void AutoIPDisable (BYTE vInterface) |
||
| 583 | { |
||
| 584 | LoadState (vInterface); |
||
| 585 | |||
| 586 | AutoIPClient.flags.bits.gDisableAutoIP = TRUE; |
||
| 587 | |||
| 588 | #if defined (STACK_USE_DHCP_CLIENT) |
||
| 589 | if (!DHCPIsBound(vInterface)) |
||
| 590 | { |
||
| 591 | AppConfig.MyIPAddr.Val = AppConfig.DefaultIPAddr.Val; |
||
| 592 | AppConfig.MyMask.Val = AppConfig.DefaultMask.Val; |
||
| 593 | } |
||
| 594 | #else |
||
| 595 | AppConfig.MyIPAddr.Val = AppConfig.DefaultIPAddr.Val; |
||
| 596 | AppConfig.MyMask.Val = AppConfig.DefaultMask.Val; |
||
| 597 | #endif |
||
| 598 | } |
||
| 599 | |||
| 600 | /***************************************************************************** |
||
| 601 | Function: |
||
| 602 | void AutoIPEnable (BYTE vInterface) |
||
| 603 | |||
| 604 | Summary: |
||
| 605 | Enables the AutoIP module. |
||
| 606 | |||
| 607 | Description: |
||
| 608 | Enables the AutoIP module. This function will end the manual-disable |
||
| 609 | condition for AutoIP, and reset the state machine to the beginning. |
||
| 610 | If a DHCP address is bound or the link is broken, the state will be |
||
| 611 | automatically set back to disabled. |
||
| 612 | |||
| 613 | Precondition: |
||
| 614 | None |
||
| 615 | |||
| 616 | Parameters: |
||
| 617 | vInterface - Interface number to enable AutoIP for. If you only have |
||
| 618 | one interface, specify 0x00. |
||
| 619 | |||
| 620 | Returns: |
||
| 621 | None |
||
| 622 | |||
| 623 | Remarks: |
||
| 624 | None |
||
| 625 | ***************************************************************************/ |
||
| 626 | |||
| 627 | void AutoIPEnable (BYTE vInterface) |
||
| 628 | { |
||
| 629 | LoadState (vInterface); |
||
| 630 | |||
| 631 | AutoIPClient.flags.bits.gDisableAutoIP = FALSE; |
||
| 632 | AutoIPClient.smAUTOIPState = SM_AUTOIP_INIT_RNG; |
||
| 633 | } |
||
| 634 | |||
| 635 | |||
| 636 | /***************************************************************************** |
||
| 637 | Function: |
||
| 638 | BOOL AutoIPIsDisabled (BYTE vInterface) |
||
| 639 | |||
| 640 | Summary: |
||
| 641 | Determines if the AutoIP state machine is in a disabled state. |
||
| 642 | |||
| 643 | Description: |
||
| 644 | Determines if the AutoIP state machine is in a disabled state. This |
||
| 645 | could occur because a DHCP address is bound, the link is broken, or |
||
| 646 | the user has manually disabled the AutoIP module. |
||
| 647 | |||
| 648 | Precondition: |
||
| 649 | None |
||
| 650 | |||
| 651 | Parameters: |
||
| 652 | vInterface - Interface number to check the AutoIP disable status for. |
||
| 653 | If you only have one interface, specify 0x00. |
||
| 654 | |||
| 655 | Returns: |
||
| 656 | TRUE - The AutoIP client is disabled |
||
| 657 | FALSE - The AutoIP client in active |
||
| 658 | |||
| 659 | Remarks: |
||
| 660 | None |
||
| 661 | ***************************************************************************/ |
||
| 662 | |||
| 663 | BOOL AutoIPIsDisabled (BYTE vInterface) |
||
| 664 | { |
||
| 665 | LoadState (vInterface); |
||
| 666 | |||
| 667 | return (AutoIPClient.smAUTOIPState == SM_AUTOIP_DISABLED)?TRUE:FALSE; |
||
| 668 | } |
||
| 669 | |||
| 670 | /***************************************************************************** |
||
| 671 | Function: |
||
| 672 | void AutoIPRandSeed (DWORD seed, BYTE vInterface) |
||
| 673 | |||
| 674 | Summary: |
||
| 675 | Seeds a random number generator used to generate a MAC |
||
| 676 | |||
| 677 | Description: |
||
| 678 | Seeds a random number generator used to generate a MAC |
||
| 679 | |||
| 680 | Precondition: |
||
| 681 | None |
||
| 682 | |||
| 683 | Parameters: |
||
| 684 | seed - The seed value for the RNG |
||
| 685 | vInterface - The interface to seed the generator for |
||
| 686 | |||
| 687 | Returns: |
||
| 688 | None |
||
| 689 | |||
| 690 | Remarks: |
||
| 691 | None |
||
| 692 | ***************************************************************************/ |
||
| 693 | |||
| 694 | void AutoIPRandSeed (DWORD seed, BYTE vInterface) |
||
| 695 | { |
||
| 696 | LoadState (vInterface); |
||
| 697 | AutoIPClient.wRandSeed = seed; |
||
| 698 | } |
||
| 699 | |||
| 700 | /***************************************************************************** |
||
| 701 | Function: |
||
| 702 | DWORD AutoIPRand (BYTE vInterface) |
||
| 703 | |||
| 704 | Summary: |
||
| 705 | Generates a random number |
||
| 706 | |||
| 707 | Description: |
||
| 708 | Generates a random number using a Linear Congruential Generator |
||
| 709 | |||
| 710 | Precondition: |
||
| 711 | None |
||
| 712 | |||
| 713 | Parameters: |
||
| 714 | vInterface - The interface to generate the RNG for |
||
| 715 | |||
| 716 | Returns: |
||
| 717 | A random number from 0- 2^31 |
||
| 718 | |||
| 719 | Remarks: |
||
| 720 | None |
||
| 721 | ***************************************************************************/ |
||
| 722 | |||
| 723 | DWORD AutoIPRand (BYTE vInterface) |
||
| 724 | { |
||
| 725 | LoadState (vInterface); |
||
| 726 | AutoIPClient.wRandSeed = (((AutoIPClient.wRandSeed * 1103515245) + 12345) % 0x80000000); |
||
| 727 | return AutoIPClient.wRandSeed; |
||
| 728 | } |
||
| 729 | |||
| 730 | #endif |
||
| 731 | |||
| 732 | |||
| 733 | |||
| 734 | |||
| 735 | |||
| 736 |
Powered by WebSVN v2.8.3