| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /********************************************************************* |
| 2 | * |
||
| 3 | * TCP/IP Stack Manager |
||
| 4 | * Module for Microchip TCP/IP Stack |
||
| 5 | * -Handles internal RX packet pre-processing prior to dispatching |
||
| 6 | * to upper application layers. |
||
| 7 | * -Reference: AN833 |
||
| 8 | * |
||
| 9 | ********************************************************************* |
||
| 10 | * FileName: StackTsk.c |
||
| 11 | * Dependencies: ARP, IP, Network layer interface (ENC28J60.c, |
||
| 12 | * ETH97J60.c, ENCX24J600.c, or WFMac.c) |
||
| 13 | * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32 |
||
| 14 | * Compiler: Microchip C32 v1.05 or higher |
||
| 15 | * Microchip C30 v3.12 or higher |
||
| 16 | * Microchip C18 v3.30 or higher |
||
| 17 | * HI-TECH PICC-18 PRO 9.63PL2 or higher |
||
| 18 | * Company: Microchip Technology, Inc. |
||
| 19 | * |
||
| 20 | * Software License Agreement |
||
| 21 | * |
||
| 22 | * Copyright (C) 2002-2009 Microchip Technology Inc. All rights |
||
| 23 | * reserved. |
||
| 24 | * |
||
| 25 | * Microchip licenses to you the right to use, modify, copy, and |
||
| 26 | * distribute: |
||
| 27 | * (i) the Software when embedded on a Microchip microcontroller or |
||
| 28 | * digital signal controller product ("Device") which is |
||
| 29 | * integrated into Licensee's product; or |
||
| 30 | * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h, |
||
| 31 | * ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device |
||
| 32 | * used in conjunction with a Microchip ethernet controller for |
||
| 33 | * the sole purpose of interfacing with the ethernet controller. |
||
| 34 | * |
||
| 35 | * You should refer to the license agreement accompanying this |
||
| 36 | * Software for additional information regarding your rights and |
||
| 37 | * obligations. |
||
| 38 | * |
||
| 39 | * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT |
||
| 40 | * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT |
||
| 41 | * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
||
| 42 | * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
||
| 43 | * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR |
||
| 44 | * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF |
||
| 45 | * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS |
||
| 46 | * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE |
||
| 47 | * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER |
||
| 48 | * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT |
||
| 49 | * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. |
||
| 50 | * |
||
| 51 | * |
||
| 52 | * Author Date Comment |
||
| 53 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 54 | * Nilesh Rajbharti 8/14/01 Original (Rev. 1.0) |
||
| 55 | * Nilesh Rajbharti 2/9/02 Cleanup |
||
| 56 | * Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail) |
||
| 57 | * Nilesh Rajbharti 12/5/02 Modified UDPProcess() and TCPProcess() |
||
| 58 | * to include localIP as third param. |
||
| 59 | * This was done to allow these functions |
||
| 60 | * to calculate checksum correctly. |
||
| 61 | * Nilesh Rajbharti 7/26/04 Added code in StackTask() to not |
||
| 62 | * clear statically IP address if link is |
||
| 63 | * removed and DHCP module is disabled |
||
| 64 | * at runtime. |
||
| 65 | * Howard Schlunder 03/16/07 Rewrote stack manager to be linear |
||
| 66 | ********************************************************************/ |
||
| 67 | #define __STACKTSK_C |
||
| 68 | |||
| 69 | #include "TCPIP Stack/TCPIP.h" |
||
| 70 | |||
| 71 | #if defined( WF_CS_TRIS ) |
||
| 72 | #if defined( WF_CONFIG_CONSOLE ) |
||
| 73 | #include "TCPIP Stack\WFConsole.h" |
||
| 74 | #endif |
||
| 75 | #if defined( STACK_USE_EZ_CONFIG ) || defined( EZ_CONFIG_SCAN ) |
||
| 76 | #include "TCPIP Stack\WFEasyConfig.h" |
||
| 77 | #endif |
||
| 78 | #include "TCPIP Stack\WFApi.h" |
||
| 79 | #endif |
||
| 80 | |||
| 81 | // Stack FSM states. |
||
| 82 | typedef enum |
||
| 83 | { |
||
| 84 | SM_STACK_IDLE, |
||
| 85 | SM_STACK_MAC, |
||
| 86 | SM_STACK_IP, |
||
| 87 | SM_STACK_ARP, |
||
| 88 | SM_STACK_TCP, |
||
| 89 | SM_STACK_UDP |
||
| 90 | } SM_STACK; |
||
| 91 | static SM_STACK smStack; |
||
| 92 | |||
| 93 | NODE_INFO remoteNode; |
||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | /********************************************************************* |
||
| 98 | * Function: void StackInit(void) |
||
| 99 | * |
||
| 100 | * PreCondition: None |
||
| 101 | * |
||
| 102 | * Input: None |
||
| 103 | * |
||
| 104 | * Output: Stack and its componets are initialized |
||
| 105 | * |
||
| 106 | * Side Effects: None |
||
| 107 | * |
||
| 108 | * Note: This function must be called before any of the |
||
| 109 | * stack or its component routines are used. |
||
| 110 | * |
||
| 111 | ********************************************************************/ |
||
| 112 | void StackInit(void) |
||
| 113 | { |
||
| 114 | smStack = SM_STACK_IDLE; |
||
| 115 | |||
| 116 | #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) |
||
| 117 | /* |
||
| 118 | * If DHCP or IP Gleaning is enabled, |
||
| 119 | * startup in Config Mode. |
||
| 120 | */ |
||
| 121 | AppConfig.Flags.bInConfigMode = TRUE; |
||
| 122 | |||
| 123 | #endif |
||
| 124 | |||
| 125 | // Seed the rand() function |
||
| 126 | srand(GenerateRandomDWORD()); |
||
| 127 | |||
| 128 | MACInit(); |
||
| 129 | |||
| 130 | #if defined(WF_CS_TRIS) && defined(STACK_USE_EZ_CONFIG) |
||
| 131 | WFEasyConfigInit(); |
||
| 132 | #endif |
||
| 133 | |||
| 134 | ARPInit(); |
||
| 135 | |||
| 136 | #if defined(STACK_USE_UDP) |
||
| 137 | UDPInit(); |
||
| 138 | #endif |
||
| 139 | |||
| 140 | #if defined(STACK_USE_TCP) |
||
| 141 | TCPInit(); |
||
| 142 | #endif |
||
| 143 | |||
| 144 | #if defined(STACK_USE_BERKELEY_API) |
||
| 145 | BerkeleySocketInit(); |
||
| 146 | #endif |
||
| 147 | |||
| 148 | #if defined(STACK_USE_HTTP_SERVER) || defined(STACK_USE_HTTP2_SERVER) |
||
| 149 | HTTPInit(); |
||
| 150 | #endif |
||
| 151 | |||
| 152 | #if defined(STACK_USE_RSA) |
||
| 153 | RSAInit(); |
||
| 154 | #endif |
||
| 155 | |||
| 156 | #if defined(STACK_USE_SSL) |
||
| 157 | SSLInit(); |
||
| 158 | #endif |
||
| 159 | |||
| 160 | #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS) |
||
| 161 | FTPInit(); |
||
| 162 | #endif |
||
| 163 | |||
| 164 | #if defined(STACK_USE_SNMP_SERVER) |
||
| 165 | SNMPInit(); |
||
| 166 | #endif |
||
| 167 | |||
| 168 | #if defined(STACK_USE_DHCP_CLIENT) |
||
| 169 | DHCPInit(0); |
||
| 170 | if(!AppConfig.Flags.bIsDHCPEnabled) |
||
| 171 | { |
||
| 172 | DHCPDisable(0); |
||
| 173 | } |
||
| 174 | #endif |
||
| 175 | |||
| 176 | #if defined(STACK_USE_AUTOIP) |
||
| 177 | AutoIPInit(0); |
||
| 178 | #endif |
||
| 179 | |||
| 180 | #if defined(STACK_USE_DYNAMICDNS_CLIENT) |
||
| 181 | DDNSInit(); |
||
| 182 | #endif |
||
| 183 | |||
| 184 | #if defined(STACK_USE_RANDOM) |
||
| 185 | RandomInit(); |
||
| 186 | #endif |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | /********************************************************************* |
||
| 191 | * Function: void StackTask(void) |
||
| 192 | * |
||
| 193 | * PreCondition: StackInit() is already called. |
||
| 194 | * |
||
| 195 | * Input: None |
||
| 196 | * |
||
| 197 | * Output: Stack FSM is executed. |
||
| 198 | * |
||
| 199 | * Side Effects: None |
||
| 200 | * |
||
| 201 | * Note: This FSM checks for new incoming packets, |
||
| 202 | * and routes it to appropriate stack components. |
||
| 203 | * It also performs timed operations. |
||
| 204 | * |
||
| 205 | * This function must be called periodically to |
||
| 206 | * ensure timely responses. |
||
| 207 | * |
||
| 208 | ********************************************************************/ |
||
| 209 | void StackTask(void) |
||
| 210 | { |
||
| 211 | WORD dataCount; |
||
| 212 | IP_ADDR tempLocalIP; |
||
| 213 | BYTE cFrameType; |
||
| 214 | BYTE cIPFrameType; |
||
| 215 | |||
| 216 | |||
| 217 | #if defined( WF_CS_TRIS ) |
||
| 218 | // This task performs low-level MAC processing specific to the MRF24WB0M |
||
| 219 | MACProcess(); |
||
| 220 | #if defined( STACK_USE_EZ_CONFIG ) |
||
| 221 | WFEasyConfigMgr(); |
||
| 222 | #endif |
||
| 223 | #endif |
||
| 224 | |||
| 225 | |||
| 226 | #if defined(STACK_USE_DHCP_CLIENT) |
||
| 227 | // Normally, an application would not include DHCP module |
||
| 228 | // if it is not enabled. But in case some one wants to disable |
||
| 229 | // DHCP module at run-time, remember to not clear our IP |
||
| 230 | // address if link is removed. |
||
| 231 | if(AppConfig.Flags.bIsDHCPEnabled) |
||
| 232 | { |
||
| 233 | static BOOL bLastLinkState = FALSE; |
||
| 234 | BOOL bCurrentLinkState; |
||
| 235 | |||
| 236 | bCurrentLinkState = MACIsLinked(); |
||
| 237 | if(bCurrentLinkState != bLastLinkState) |
||
| 238 | { |
||
| 239 | bLastLinkState = bCurrentLinkState; |
||
| 240 | if(!bCurrentLinkState) |
||
| 241 | { |
||
| 242 | AppConfig.MyIPAddr.Val = AppConfig.DefaultIPAddr.Val; |
||
| 243 | AppConfig.MyMask.Val = AppConfig.DefaultMask.Val; |
||
| 244 | AppConfig.Flags.bInConfigMode = TRUE; |
||
| 245 | DHCPInit(0); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | // DHCP must be called all the time even after IP configuration is |
||
| 250 | // discovered. |
||
| 251 | // DHCP has to account lease expiration time and renew the configuration |
||
| 252 | // time. |
||
| 253 | DHCPTask(); |
||
| 254 | |||
| 255 | if(DHCPIsBound(0)) |
||
| 256 | AppConfig.Flags.bInConfigMode = FALSE; |
||
| 257 | } |
||
| 258 | #endif |
||
| 259 | |||
| 260 | #if defined (STACK_USE_AUTO_IP) |
||
| 261 | AutoIPTasks(); |
||
| 262 | #endif |
||
| 263 | |||
| 264 | #if defined(STACK_USE_TCP) |
||
| 265 | // Perform all TCP time related tasks (retransmit, send acknowledge, close connection, etc) |
||
| 266 | TCPTick(); |
||
| 267 | #endif |
||
| 268 | |||
| 269 | |||
| 270 | #if defined(STACK_USE_UDP) |
||
| 271 | UDPTask(); |
||
| 272 | #endif |
||
| 273 | |||
| 274 | // Process as many incomming packets as we can |
||
| 275 | while(1) |
||
| 276 | { |
||
| 277 | //if using the random module, generate entropy |
||
| 278 | #if defined(STACK_USE_RANDOM) |
||
| 279 | RandomAdd(remoteNode.MACAddr.v[5]); |
||
| 280 | #endif |
||
| 281 | |||
| 282 | // We are about to fetch a new packet, make sure that the |
||
| 283 | // UDP module knows that any old RX data it has laying |
||
| 284 | // around will now be gone. |
||
| 285 | #if defined(STACK_USE_UDP) |
||
| 286 | UDPDiscard(); |
||
| 287 | #endif |
||
| 288 | |||
| 289 | // Fetch a packet (throws old one away, if not thrown away |
||
| 290 | // yet) |
||
| 291 | if(!MACGetHeader(&remoteNode.MACAddr, &cFrameType)) |
||
| 292 | break; |
||
| 293 | |||
| 294 | // When using a WiFi module, filter out all incoming packets that have |
||
| 295 | // the same source MAC address as our own MAC address. This is to |
||
| 296 | // prevent receiving and passing our own broadcast packets up to other |
||
| 297 | // layers and avoid, for example, having our own gratuitous ARPs get |
||
| 298 | // answered by ourself. |
||
| 299 | #if defined(WF_CS_TRIS) |
||
| 300 | if(memcmp((void*)&remoteNode.MACAddr, (void*)&AppConfig.MyMACAddr, 6) == 0u) |
||
| 301 | continue; |
||
| 302 | #endif |
||
| 303 | |||
| 304 | // Dispatch the packet to the appropriate handler |
||
| 305 | switch(cFrameType) |
||
| 306 | { |
||
| 307 | case MAC_ARP: |
||
| 308 | ARPProcess(); |
||
| 309 | break; |
||
| 310 | |||
| 311 | case MAC_IP: |
||
| 312 | if(!IPGetHeader(&tempLocalIP, &remoteNode, &cIPFrameType, &dataCount)) |
||
| 313 | break; |
||
| 314 | |||
| 315 | #if defined(STACK_USE_ICMP_SERVER) || defined(STACK_USE_ICMP_CLIENT) |
||
| 316 | if(cIPFrameType == IP_PROT_ICMP) |
||
| 317 | { |
||
| 318 | #if defined(STACK_USE_IP_GLEANING) |
||
| 319 | if(AppConfig.Flags.bInConfigMode && AppConfig.Flags.bIsDHCPEnabled) |
||
| 320 | { |
||
| 321 | // According to "IP Gleaning" procedure, |
||
| 322 | // when we receive an ICMP packet with a valid |
||
| 323 | // IP address while we are still in configuration |
||
| 324 | // mode, accept that address as ours and conclude |
||
| 325 | // configuration mode. |
||
| 326 | if(tempLocalIP.Val != 0xffffffff) |
||
| 327 | { |
||
| 328 | AppConfig.Flags.bInConfigMode = FALSE; |
||
| 329 | AppConfig.MyIPAddr = tempLocalIP; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | #endif |
||
| 333 | |||
| 334 | // Process this ICMP packet if it the destination IP address matches our address or one of the broadcast IP addressees |
||
| 335 | if( (tempLocalIP.Val == AppConfig.MyIPAddr.Val) || |
||
| 336 | (tempLocalIP.Val == 0xFFFFFFFF) || |
||
| 337 | #if defined(STACK_USE_ZEROCONF_LINK_LOCAL) || defined(STACK_USE_ZEROCONF_MDNS_SD) |
||
| 338 | (tempLocalIP.Val == 0xFB0000E0) || |
||
| 339 | #endif |
||
| 340 | (tempLocalIP.Val == ((AppConfig.MyIPAddr.Val & AppConfig.MyMask.Val) | ~AppConfig.MyMask.Val))) |
||
| 341 | { |
||
| 342 | ICMPProcess(&remoteNode, dataCount); |
||
| 343 | } |
||
| 344 | |||
| 345 | break; |
||
| 346 | } |
||
| 347 | #endif |
||
| 348 | |||
| 349 | #if defined(STACK_USE_TCP) |
||
| 350 | if(cIPFrameType == IP_PROT_TCP) |
||
| 351 | { |
||
| 352 | TCPProcess(&remoteNode, &tempLocalIP, dataCount); |
||
| 353 | break; |
||
| 354 | } |
||
| 355 | #endif |
||
| 356 | |||
| 357 | #if defined(STACK_USE_UDP) |
||
| 358 | if(cIPFrameType == IP_PROT_UDP) |
||
| 359 | { |
||
| 360 | // Stop processing packets if we came upon a UDP frame with application data in it |
||
| 361 | if(UDPProcess(&remoteNode, &tempLocalIP, dataCount)) |
||
| 362 | return; |
||
| 363 | } |
||
| 364 | #endif |
||
| 365 | |||
| 366 | break; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | /********************************************************************* |
||
| 372 | * Function: void StackApplications(void) |
||
| 373 | * |
||
| 374 | * PreCondition: StackInit() is already called. |
||
| 375 | * |
||
| 376 | * Input: None |
||
| 377 | * |
||
| 378 | * Output: Calls all loaded application modules. |
||
| 379 | * |
||
| 380 | * Side Effects: None |
||
| 381 | * |
||
| 382 | * Note: This function must be called periodically to |
||
| 383 | * ensure timely responses. |
||
| 384 | * |
||
| 385 | ********************************************************************/ |
||
| 386 | void StackApplications(void) |
||
| 387 | { |
||
| 388 | #if defined(STACK_USE_HTTP_SERVER) || defined(STACK_USE_HTTP2_SERVER) |
||
| 389 | HTTPServer(); |
||
| 390 | #endif |
||
| 391 | |||
| 392 | #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS) |
||
| 393 | FTPServer(); |
||
| 394 | #endif |
||
| 395 | |||
| 396 | #if defined(STACK_USE_SNMP_SERVER) |
||
| 397 | SNMPTask(); |
||
| 398 | #endif |
||
| 399 | |||
| 400 | #if defined(STACK_USE_ANNOUNCE) |
||
| 401 | DiscoveryTask(); |
||
| 402 | #endif |
||
| 403 | |||
| 404 | #if defined(STACK_USE_NBNS) |
||
| 405 | NBNSTask(); |
||
| 406 | #endif |
||
| 407 | |||
| 408 | #if defined(STACK_USE_DHCP_SERVER) |
||
| 409 | DHCPServerTask(); |
||
| 410 | #endif |
||
| 411 | |||
| 412 | #if defined(STACK_USE_DNS_SERVER) |
||
| 413 | DNSServerTask(); |
||
| 414 | #endif |
||
| 415 | |||
| 416 | #if defined (STACK_USE_DYNAMICDNS_CLIENT) |
||
| 417 | DDNSTask(); |
||
| 418 | #endif |
||
| 419 | |||
| 420 | #if defined(STACK_USE_TELNET_SERVER) |
||
| 421 | TelnetTask(); |
||
| 422 | #endif |
||
| 423 | |||
| 424 | #if defined(STACK_USE_REBOOT_SERVER) |
||
| 425 | RebootTask(); |
||
| 426 | #endif |
||
| 427 | |||
| 428 | #if defined(STACK_USE_SNTP_CLIENT) |
||
| 429 | SNTPClient(); |
||
| 430 | #endif |
||
| 431 | |||
| 432 | #if defined(STACK_USE_UDP_PERFORMANCE_TEST) |
||
| 433 | UDPPerformanceTask(); |
||
| 434 | #endif |
||
| 435 | |||
| 436 | #if defined(STACK_USE_TCP_PERFORMANCE_TEST) |
||
| 437 | TCPPerformanceTask(); |
||
| 438 | #endif |
||
| 439 | |||
| 440 | #if defined(STACK_USE_SMTP_CLIENT) |
||
| 441 | SMTPTask(); |
||
| 442 | #endif |
||
| 443 | |||
| 444 | #if defined(STACK_USE_UART2TCP_BRIDGE) |
||
| 445 | UART2TCPBridgeTask(); |
||
| 446 | #endif |
||
| 447 | } |
Powered by WebSVN v2.8.3