/*** void UdpServer::resumeListening(void) ** ** Synopsis: ** Resumes listening on a UdpServer that did a StopListening ** ** Parameters: ** None ** ** Return Values: ** None ** ** Errors: ** None ** ** Notes: ** ** If StartListening was never called, this does nothing ** If it is already listening, it does nothing */ void UdpServer::resumeListening(void) { if(!_fStarted) { return; } // say we want to listen, may fail, but we will pick it up when we can. _fListening = true; // make sure we have room to to put our handle in if(_cPending >= _cPendingMax) { return; } // if we need to start listening on a socket if(_rghUDP[_cPending] >= INVALID_UDP_SOCKET) { // do not need to check to see if Ethernet is initialized because I can assign sockets before then // _rghUDP[_cPending] = UDPOpen(_localPort, NULL, 0); // this is the old MLA _rghUDP[_cPending] = UDPOpenEx(NULL, 0, _localPort, 0); // 2nd param is UDP_OPEN_SERVER = 0 if(_rghUDP[_cPending] < INVALID_UDP_SOCKET) { // As for the iBuff, when we get data on the socket, the last entry // will be where we put pending Client so just use this iBuff as the cache buffer ExchangeCacheBuffer(_rghUDP[_cPending], GetBufferLocation(_cPending), GetBufferSize()); } } }
/********************************************************************* * Function: void RebootTask(void) * * PreCondition: Stack is initialized() * * Input: None * * Output: None * * Side Effects: None * * Overview: Checks for incomming traffic on port 69. * Resets the PIC if a 'R' is received. * * Note: This module is primarily for use with the * Ethernet bootloader. By resetting, the Ethernet * bootloader can take control for a second and let * a firmware upgrade take place. ********************************************************************/ void RebootTask(void) { static UDP_SOCKET MySocket = INVALID_UDP_SOCKET; struct { BYTE vMACAddress[6]; DWORD dwIPAddress; WORD wChecksum; } BootloaderAddress; if(MySocket == INVALID_UDP_SOCKET) MySocket = UDPOpenEx(0,UDP_OPEN_SERVER,REBOOT_PORT,INVALID_UDP_PORT); // MySocket = UDPOpen(REBOOT_PORT, NULL, INVALID_UDP_PORT); if(MySocket == INVALID_UDP_SOCKET) return; // Do nothing if no data is waiting if(!UDPIsGetReady(MySocket)) return; #if defined(REBOOT_SAME_SUBNET_ONLY) // Respond only to name requests sent to us from nodes on the same subnet if((remoteNode.IPAddr.Val & AppConfig.MyMask.Val) != (AppConfig.MyIPAddr.Val & AppConfig.MyMask.Val)) { UDPDiscard(); return; } #endif // Get our MAC address, IP address, and compute a checksum of them memcpy((void*)&BootloaderAddress.vMACAddress[0], (void*)&AppConfig.MyMACAddr.v[0], sizeof(AppConfig.MyMACAddr)); BootloaderAddress.dwIPAddress = AppConfig.MyIPAddr.Val; BootloaderAddress.wChecksum = CalcIPChecksum((BYTE*)&BootloaderAddress, sizeof(BootloaderAddress) - sizeof(BootloaderAddress.wChecksum)); // To enter the bootloader, we need to clear the /POR bit in RCON. // Otherwise, the bootloader will immediately hand off execution // to us. #if defined(USE_LCD) strcpypgm2ram((char*)LCDText, "Bootloader Reset"); LCDUpdate(); #endif #if !defined(__STM32F10X__) RCONbits.POR = 0; #if defined(__18CXX) { WORD_VAL wvPROD; wvPROD.Val = ((WORD)&BootloaderAddress); PRODH = wvPROD.v[1]; PRODL = wvPROD.v[0]; } #endif #endif Reset(); }
/**************************************************************************************************** Function: void AnnounceIP(void) Summary: Transmits an Announce packet. Conditions: Stack is initialized() Return: None Side Effects: None Description: AnnounceIP opens a UDP socket and transmits a broadcast packet to port \30303. If a computer is on the same subnet and a utility is looking for packets on the UDP port, it will receive the broadcast. For this application, it is used to announce the change of this board's IP address. The messages can be viewed with the TCP/IP Discoverer software tool. Remarks: A UDP socket must be available before this function is called. It is freed at the end of the function. MAX_UDP_SOCKETS may need to be increased if other modules use UDP sockets. ****************************************************************************************************/ void AnnounceIP(void) { UDP_SOCKET MySocket; BYTE i; if(!MACIsLinked()) // Check for link before blindly opening and transmitting (similar to DHCP case) return; // Open a UDP socket for outbound broadcast transmission //MySocket = UDPOpen(2860, NULL, ANNOUNCE_PORT); MySocket = UDPOpenEx(0,UDP_OPEN_SERVER,2860, ANNOUNCE_PORT); LED1_IO = 0; // Abort operation if no UDP sockets are available // If this ever happens, incrementing MAX_UDP_SOCKETS in // StackTsk.h may help (at the expense of more global memory // resources). if(MySocket == INVALID_UDP_SOCKET) return; // Make certain the socket can be written to while(!UDPIsPutReady(MySocket)); // Begin sending our MAC address in human readable form. // The MAC address theoretically could be obtained from the // packet header when the computer receives our UDP packet, // however, in practice, the OS will abstract away the useful // information and it would be difficult to obtain. It also // would be lost if this broadcast packet were forwarded by a // router to a different portion of the network (note that // broadcasts are normally not forwarded by routers). UDPPutArray((BYTE*)AppConfig.NetBIOSName, sizeof(AppConfig.NetBIOSName)-1); UDPPut('\r'); UDPPut('\n'); // Convert the MAC address bytes to hex (text) and then send it i = 0; while(1) { UDPPut(btohexa_high(AppConfig.MyMACAddr.v[i])); UDPPut(btohexa_low(AppConfig.MyMACAddr.v[i])); if(++i == 6u) break; UDPPut('-'); } // Send some other human readable information. UDPPutROMString((ROM BYTE*)"\r\nDHCP/Power event occurred"); // Send the packet UDPFlush(); // Close the socket so it can be used by other modules UDPClose(MySocket); }
void EthernetInit(void) { // Initialize hardware InitBoard(); // Initialize stack-related hardware components that may be // required by the UART configuration routines TickInit(); // Initialize Stack and application related NV variables into AppConfig. InitAppConfig(); // Seed the LFSRRand() function. LFSRSeedRand(GenerateRandomDWORD()); // Initialize the MAC library. MACInit(); // Initialize the ARP library. ARPInit(); // Initialize UDP. UDPInit(); // Open up a socket for our UDP server. localServerSocket = UDPOpenEx(NULL, UDP_OPEN_SERVER, UDP_SERVER_PORT, UDP_SERVER_PORT); if (localServerSocket == INVALID_UDP_SOCKET) { FATAL_ERROR(); } // Open up a socket for our UDP client. remoteServerSocket = UDPOpenEx(NULL, UDP_OPEN_IP_ADDRESS, UDP_CLIENT_PORT, UDP_CLIENT_PORT); if (remoteServerSocket == INVALID_UDP_SOCKET) { FATAL_ERROR(); } }
bool microchip_udp_open(uint32_t localAddr, uint16_t* localPort, UDP_SOCKET* socketDescriptor) { UDP_SOCKET sd; sd = UDPOpenEx(0, UDP_OPEN_SERVER, *localPort, 0); NABTO_LOG_INFO(("create socket...")); if (sd == INVALID_UDP_SOCKET) { NABTO_LOG_INFO(("failed to create socket")); return false; } *localPort = UDPSocketInfo[sd].localPort; *socketDescriptor = sd; // NABTO_LOG_INFO(("init socket %i port %i", *socketDescriptor, *localPort)); return true; }
/********************************************************************* * Function: TFTP_RESULT TFTPIsOpened(void) * * Summary: Determines if the TFTP connection is open. * * PreCondition: TFTPOpen() is already called. * * Input: None * * Output: TFTP_OK if previous call to TFTPOpen is complete * * TFTP_TIMEOUT if remote host did not respond to * previous ARP request. * * TFTP_NOT_READY if remote has still not responded * and timeout has not expired. * * Side Effects: None * * Overview: Waits for ARP reply and opens a UDP socket * to perform further TFTP operations. * * Note: Once opened, application may keep TFTP socket * open and future TFTP operations. * If TFTPClose() is called to close the connection * TFTPOpen() must be called again before performing * any other TFTP operations. ********************************************************************/ TFTP_RESULT TFTPIsOpened(void) { switch(_tftpState) { default: _tftpState = SM_TFTP_READY; #if 0 DEBUG(printf("Resolving remote IP...\n")); // Check to see if adddress is resolved. if ( ARPIsResolved(&MutExVar.group1._hostInfo.IPAddr, &MutExVar.group1._hostInfo.MACAddr) ) { _tftpSocket = UDPOpenEx(TFTP_CLIENT_PORT, &MutExVar.group1._hostInfo, TFTP_SERVER_PORT); _tftpState = SM_TFTP_READY; } else break; #endif case SM_TFTP_READY: // Wait for UDP to be ready. Immediately after this user will // may TFTPGetFile or TFTPPutFile and we have to make sure that // UDP is read to transmit. These functions do not check for // UDP to get ready. if ( UDPIsPutReady(_tftpSocket) ) return TFTP_OK; } // Make sure that we do not do this forever. if ( TickGet() - _tftpStartTick >= TFTP_ARP_TIMEOUT_VAL ) { _tftpStartTick = TickGet(); // Forget about all previous attempts. _tftpRetries = 1; return TFTP_TIMEOUT; } return TFTP_NOT_READY; }
/***************************************************************************** Function: int bind( SOCKET s, const struct sockaddr* name, int namelen ) Summary: This function assigns a name to the socket descriptor. Description: The bind function assigns a name to an unnamed socket. The name represents the local address of the communication endpoint. For sockets of type SOCK_STREAM, the name of the remote endpoint is assigned when a connect or accept function is executed. Precondition: socket function should be called. Parameters: s - Socket descriptor returned from a previous call to socket. name - pointer to the sockaddr structure containing the local address of the socket. namelen - length of the sockaddr structure. Returns: If bind is successful, a value of 0 is returned. A return value of SOCKET_ERROR indicates an error. Remarks: None. ***************************************************************************/ int bind( SOCKET s, const struct sockaddr* name, int namelen ) { struct BSDSocket *socket; struct sockaddr_in *local_addr; WORD lPort; if( s >= BSD_SOCKET_COUNT ) return SOCKET_ERROR; socket = &BSDSocketArray[s]; if( socket->bsdState != SKT_CREATED ) //only work with recently created socket return SOCKET_ERROR; if( (unsigned int)namelen < sizeof(struct sockaddr_in) ) return SOCKET_ERROR; local_addr = (struct sockaddr_in *)name; lPort = local_addr->sin_port; if( lPort == 0u ) //pick a port { lPort = gAutoPortNumber++; if(gAutoPortNumber > 5000u) // reset the port numbers gAutoPortNumber = 1024; } if(socket->SocketType == SOCK_DGRAM) { //socket->SocketID = UDPOpen(lPort, NULL, 0); socket->SocketID = UDPOpenEx(0,UDP_OPEN_SERVER,lPort, 0); if(socket->SocketID == INVALID_UDP_SOCKET) return SOCKET_ERROR; } socket->localPort = lPort; socket->bsdState = SKT_BOUND; return 0; //success }
/***************************************************************************** Function: void SNTPClient(void) Summary: Periodically checks the current time from a pool of servers. Description: This function periodically checks a pool of time servers to obtain the current date/time. Precondition: UDP is initialized. Parameters: None Returns: None Remarks: This function requires once available UDP socket while processing, but frees that socket when the SNTP module is idle. ***************************************************************************/ void SNTPClient(void) { NTP_PACKET pkt; WORD w; // static NODE_INFO Server; static DWORD dwTimer; static UDP_SOCKET MySocket = INVALID_UDP_SOCKET; static enum { SM_HOME = 0, SM_UDP_IS_OPENED, //SM_NAME_RESOLVE, //SM_ARP_START_RESOLVE, //SM_ARP_RESOLVE, //SM_ARP_START_RESOLVE2, //SM_ARP_RESOLVE2, //SM_ARP_START_RESOLVE3, //SM_ARP_RESOLVE3, //SM_ARP_RESOLVE_FAIL, SM_UDP_SEND, SM_UDP_RECV, SM_SHORT_WAIT, SM_WAIT } SNTPState = SM_HOME; switch(SNTPState) { case SM_HOME: if(MySocket == INVALID_UDP_SOCKET) MySocket = UDPOpenEx((DWORD)(PTR_BASE)NTP_SERVER,UDP_OPEN_ROM_HOST,0,NTP_SERVER_PORT); SNTPState++; break; case SM_UDP_IS_OPENED: if(UDPIsOpened(MySocket) == TRUE) { SNTPState = SM_UDP_SEND; } /* else { UDPClose(MySocket); SNTPState = SM_HOME; MySocket = INVALID_UDP_SOCKET; } */ break; #if 0 // Obtain ownership of the DNS resolution module if(!DNSBeginUsage()) break; // Obtain the IP address associated with the server name DNSResolveROM((ROM BYTE*)NTP_SERVER, DNS_TYPE_A); dwTimer = TickGet(); SNTPState = SM_NAME_RESOLVE; break; case SM_NAME_RESOLVE: // Wait for DNS resolution to complete if(!DNSIsResolved(&Server.IPAddr)) { if((TickGet() - dwTimer) > (5 * TICK_SECOND)) { DNSEndUsage(); dwTimer = TickGetDiv64K(); SNTPState = SM_SHORT_WAIT; } break; } // Obtain DNS resolution result if(!DNSEndUsage()) { // No valid IP address was returned from the DNS // server. Quit and fail for a while if host is not valid. dwTimer = TickGetDiv64K(); SNTPState = SM_SHORT_WAIT; break; } SNTPState = SM_ARP_START_RESOLVE; // No need to break case SM_ARP_START_RESOLVE: case SM_ARP_START_RESOLVE2: case SM_ARP_START_RESOLVE3: // Obtain the MAC address associated with the server's IP address ARPResolve(&Server.IPAddr); dwTimer = TickGet(); SNTPState++; break; case SM_ARP_RESOLVE: case SM_ARP_RESOLVE2: case SM_ARP_RESOLVE3: // Wait for the MAC address to finish being obtained if(!ARPIsResolved(&Server.IPAddr, &Server.MACAddr)) { // Time out if too much time is spent in this state if(TickGet() - dwTimer > 1*TICK_SECOND) { // Retransmit ARP request by going to next SM_ARP_START_RESOLVE state or fail by going to SM_ARP_RESOLVE_FAIL state. SNTPState++; } break; } SNTPState = SM_UDP_SEND; break; case SM_ARP_RESOLVE_FAIL: // ARP failed after 3 tries, abort and wait for next time query dwTimer = TickGetDiv64K(); SNTPState = SM_SHORT_WAIT; break; #endif // case SM_UDP_IS_OPENED: case SM_UDP_SEND: // Open up the sending UDP socket //MySocket = UDPOpen(0, &Server, NTP_SERVER_PORT); #if 0 MySocket = UDPOpenEx(NTP_SERVER,UDP_OPEN_ROM_HOST,0,NTP_SERVER_PORT); if(MySocket == INVALID_UDP_SOCKET) break; #endif // Make certain the socket can be written to if(!UDPIsPutReady(MySocket)) { UDPClose(MySocket); SNTPState = SM_HOME; MySocket = INVALID_UDP_SOCKET; break; } // Transmit a time request packet memset(&pkt, 0, sizeof(pkt)); pkt.flags.versionNumber = 3; // NTP Version 3 pkt.flags.mode = 3; // NTP Client pkt.orig_ts_secs = swapl(NTP_EPOCH); UDPPutArray((BYTE*) &pkt, sizeof(pkt)); UDPFlush(); dwTimer = TickGet(); SNTPState = SM_UDP_RECV; break; case SM_UDP_RECV: // Look for a response time packet if(!UDPIsGetReady(MySocket)) { if((TickGet()) - dwTimer > NTP_REPLY_TIMEOUT) { // Abort the request and wait until the next timeout period UDPClose(MySocket); //dwTimer = TickGetDiv64K(); //SNTPState = SM_SHORT_WAIT; SNTPState = SM_HOME; MySocket = INVALID_UDP_SOCKET; break; } break; } // Get the response time packet w = UDPGetArray((BYTE*) &pkt, sizeof(pkt)); UDPClose(MySocket); dwTimer = TickGetDiv64K(); SNTPState = SM_WAIT; MySocket = INVALID_UDP_SOCKET; // Validate packet size if(w != sizeof(pkt)) { break; } // Set out local time to match the returned time dwLastUpdateTick = TickGet(); dwSNTPSeconds = swapl(pkt.tx_ts_secs) - NTP_EPOCH; // Do rounding. If the partial seconds is > 0.5 then add 1 to the seconds count. if(((BYTE*)&pkt.tx_ts_fraq)[0] & 0x80) dwSNTPSeconds++; #ifdef WIFI_NET_TEST wifi_net_test_print("SNTP: current time", dwSNTPSeconds); #endif break; case SM_SHORT_WAIT: // Attempt to requery the NTP server after a specified NTP_FAST_QUERY_INTERVAL time (ex: 8 seconds) has elapsed. if(TickGetDiv64K() - dwTimer > (NTP_FAST_QUERY_INTERVAL/65536ull)) { SNTPState = SM_HOME; MySocket = INVALID_UDP_SOCKET; } break; case SM_WAIT: // Requery the NTP server after a specified NTP_QUERY_INTERVAL time (ex: 10 minutes) has elapsed. if(TickGetDiv64K() - dwTimer > (NTP_QUERY_INTERVAL/65536ull)) { SNTPState = SM_HOME; MySocket = INVALID_UDP_SOCKET; } break; } }
/***************************************************************************** Function: void DHCPTask(void) Summary: Performs periodic DHCP tasks for all interfaces. Description: This function performs any periodic tasks requied by the DHCP module, such as sending and receiving messages involved with obtaining and maintaining a lease. Precondition: None Parameters: None Returns: None ***************************************************************************/ void DHCPTask(void) { BYTE i; for(i = 0; i < NETWORK_INTERFACES; i++) { LoadState(i); switch(DHCPClient.smState) { case SM_DHCP_DISABLED: // When the module is disabled, do absolutely nothing break; case SM_DHCP_GET_SOCKET: // Open a socket to send and receive broadcast messages on //DHCPClient.hDHCPSocket = UDPOpen(DHCP_CLIENT_PORT, NULL, DHCP_SERVER_PORT); DHCPClient.hDHCPSocket = UDPOpenEx(0,UDP_OPEN_SERVER,DHCP_CLIENT_PORT, DHCP_SERVER_PORT); if(DHCPClient.hDHCPSocket == INVALID_UDP_SOCKET) break; DHCPClient.smState = SM_DHCP_SEND_DISCOVERY; // No break case SM_DHCP_SEND_DISCOVERY: // Assume default IP Lease time of 60 seconds. // This should be minimum possible to make sure that if the // server did not specify lease time, we try again after this // minimum time. DHCPClient.dwLeaseTime = 60; DHCPClient.validValues.val = 0x00; DHCPClient.flags.bits.bIsBound = FALSE; DHCPClient.flags.bits.bOfferReceived = FALSE; // No point in wasting time transmitting a discovery if we are // unlinked. No one will see it. if(!MACIsLinked()) break; #if defined(WF_CS_IO) && defined(STACK_USE_UART) // Note: Use this debug message out to UART with caution. It can prevent slow down stack // performance and possibly prevent STA from properly receiving DHCP IP. // putrsUART("DHCP Send Discovery...\r\n"); #endif // Ensure transmitter is ready to accept data if(UDPIsPutReady(DHCPClient.hDHCPSocket) < 300u) break; // Toggle the BOOTP Broadcast flag to ensure compatibility with // bad DHCP servers that don't know how to handle broadcast // responses. This results in the next discovery attempt to be // made using the opposite mode. DHCPClient.flags.bits.bUseUnicastMode ^= 1; // Ensure that we transmit to the broadcast IP and MAC addresses // The UDP Socket remembers who it was last talking to memset((void*)&UDPSocketInfo[DHCPClient.hDHCPSocket].remote.remoteNode, 0xFF, sizeof(UDPSocketInfo[0].remote.remoteNode)); // Send the DHCP Discover broadcast _DHCPSend(DHCP_DISCOVER_MESSAGE, FALSE); // Start a timer and begin looking for a response DHCPClient.dwTimer = TickGet(); DHCPClient.smState = SM_DHCP_GET_OFFER; break; case SM_DHCP_GET_OFFER: // Check to see if a packet has arrived if(UDPIsGetReady(DHCPClient.hDHCPSocket) < 250u) { // Go back and transmit a new discovery if we didn't get an offer after 2 seconds if(TickGet() - DHCPClient.dwTimer >= DHCP_TIMEOUT) DHCPClient.smState = SM_DHCP_SEND_DISCOVERY; break; } // Let the DHCP server module know that there is a DHCP server // on this network DHCPClient.flags.bits.bDHCPServerDetected = TRUE; // Check to see if we received an offer if(_DHCPReceive() != DHCP_OFFER_MESSAGE) break; DHCPClient.smState = SM_DHCP_SEND_REQUEST; // No break case SM_DHCP_SEND_REQUEST: if(UDPIsPutReady(DHCPClient.hDHCPSocket) < 258u) break; // Ensure that we transmit to the broadcast IP and MAC addresses // The UDP Socket remembers who it was last talking to, so // we must set this back to the broadcast address since the // current socket values are the unicast addresses of the DHCP // server. memset((void*)&UDPSocketInfo[DHCPClient.hDHCPSocket].remote.remoteNode, 0xFF, sizeof(UDPSocketInfo[0].remote.remoteNode)); // Send the DHCP request message _DHCPSend(DHCP_REQUEST_MESSAGE, FALSE); // Start a timer and begin looking for a response DHCPClient.dwTimer = TickGet(); DHCPClient.smState = SM_DHCP_GET_REQUEST_ACK; break; case SM_DHCP_GET_REQUEST_ACK: // Check to see if a packet has arrived if(UDPIsGetReady(DHCPClient.hDHCPSocket) < 250u) { // Go back and transmit a new discovery if we didn't get an ACK after 2 seconds if(TickGet() - DHCPClient.dwTimer >= DHCP_TIMEOUT) DHCPClient.smState = SM_DHCP_SEND_DISCOVERY; break; } // Check to see if we received an offer switch(_DHCPReceive()) { case DHCP_ACK_MESSAGE: UDPClose(DHCPClient.hDHCPSocket); DHCPClient.hDHCPSocket = INVALID_UDP_SOCKET; DHCPClient.dwTimer = TickGet(); DHCPClient.smState = SM_DHCP_BOUND; DHCPClient.flags.bits.bEvent = 1; DHCPClient.flags.bits.bIsBound = TRUE; if(DHCPClient.validValues.bits.IPAddress) { AppConfig.MyIPAddr = DHCPClient.tempIPAddress; #if defined(WF_CS_IO) #if defined(STACK_USE_UART ) putrsUART("DHCP client successful\r\n"); #endif SignalDHCPSuccessful(); #endif } if(DHCPClient.validValues.bits.Mask) AppConfig.MyMask = DHCPClient.tempMask; if(DHCPClient.validValues.bits.Gateway) AppConfig.MyGateway = DHCPClient.tempGateway; #if defined(STACK_USE_DNS) if(DHCPClient.validValues.bits.DNS) AppConfig.PrimaryDNSServer.Val = DHCPClient.tempDNS.Val; AppConfig.SecondaryDNSServer.Val = 0x00000000ul; if(DHCPClient.validValues.bits.DNS2) AppConfig.SecondaryDNSServer.Val = DHCPClient.tempDNS2.Val; #endif //if(DHCPClient.validValues.bits.HostName) // memcpy(AppConfig.NetBIOSName, (void*)DHCPClient.tempHostName, sizeof(AppConfig.NetBIOSName)); break; case DHCP_NAK_MESSAGE: DHCPClient.smState = SM_DHCP_SEND_DISCOVERY; break; } break; case SM_DHCP_BOUND: if(TickGet() - DHCPClient.dwTimer < TICK_SECOND) break; // Check to see if our lease is still valid, if so, decrement lease // time if(DHCPClient.dwLeaseTime >= 2ul) { DHCPClient.dwTimer += TICK_SECOND; DHCPClient.dwLeaseTime--; break; } // Open a socket to send and receive DHCP messages on //DHCPClient.hDHCPSocket = UDPOpen(DHCP_CLIENT_PORT, NULL, DHCP_SERVER_PORT); DHCPClient.hDHCPSocket = UDPOpenEx(0,UDP_OPEN_SERVER,DHCP_CLIENT_PORT, DHCP_SERVER_PORT); if(DHCPClient.hDHCPSocket == INVALID_UDP_SOCKET) break; DHCPClient.smState = SM_DHCP_SEND_RENEW; // No break case SM_DHCP_SEND_RENEW: case SM_DHCP_SEND_RENEW2: case SM_DHCP_SEND_RENEW3: if(UDPIsPutReady(DHCPClient.hDHCPSocket) < 258u) break; // Send the DHCP request message _DHCPSend(DHCP_REQUEST_MESSAGE, TRUE); DHCPClient.flags.bits.bOfferReceived = FALSE; // Start a timer and begin looking for a response DHCPClient.dwTimer = TickGet(); DHCPClient.smState++; break; case SM_DHCP_GET_RENEW_ACK: case SM_DHCP_GET_RENEW_ACK2: case SM_DHCP_GET_RENEW_ACK3: // Check to see if a packet has arrived if(UDPIsGetReady(DHCPClient.hDHCPSocket) < 250u) { // Go back and transmit a new discovery if we didn't get an ACK after 2 seconds if(TickGet() - DHCPClient.dwTimer >= DHCP_TIMEOUT) { if(++DHCPClient.smState > SM_DHCP_GET_RENEW_ACK3) DHCPClient.smState = SM_DHCP_SEND_DISCOVERY; } break; } // Check to see if we received an offer switch(_DHCPReceive()) { case DHCP_ACK_MESSAGE: UDPClose(DHCPClient.hDHCPSocket); DHCPClient.hDHCPSocket = INVALID_UDP_SOCKET; DHCPClient.dwTimer = TickGet(); DHCPClient.smState = SM_DHCP_BOUND; DHCPClient.flags.bits.bEvent = 1; break; case DHCP_NAK_MESSAGE: DHCPClient.smState = SM_DHCP_SEND_DISCOVERY; break; } break; } } }
/********************************************************************* * Function: void DNSServerTask(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: Sends dummy responses that point to ourself for DNS requests * * Note: None ********************************************************************/ void DNSServerTask(void) { static UDP_SOCKET MySocket = INVALID_UDP_SOCKET; struct { WORD wTransactionID; WORD wFlags; WORD wQuestions; WORD wAnswerRRs; WORD wAuthorityRRs; WORD wAdditionalRRs; } DNSHeader; // Create a socket to listen on if this is the first time calling this function if(MySocket == INVALID_UDP_SOCKET) { //MySocket = UDPOpen(DNS_PORT, NULL, 0); MySocket = UDPOpenEx(0,UDP_OPEN_SERVER,DNS_PORT,0); return; } // See if a DNS query packet has arrived if(UDPIsGetReady(MySocket) < sizeof(DNSHeader)) return; // Read DNS header UDPGetArray((BYTE*)&DNSHeader, sizeof(DNSHeader)); // Ignore this packet if it isn't a query if((DNSHeader.wFlags & 0x8000) == 0x8000u) return; // Ignore this packet if there are no questions in it if(DNSHeader.wQuestions == 0u) return; // Block until we can transmit a DNS response packet while(!UDPIsPutReady(MySocket)); // Write DNS response packet UDPPutArray((BYTE*)&DNSHeader.wTransactionID, 2); // 2 byte Transaction ID if(DNSHeader.wFlags & 0x0100) UDPPut(0x81); // Message is a response with recursion desired else UDPPut(0x80); // Message is a response without recursion desired flag set UDPPut(0x80); // Recursion available UDPPut(0x00); // 0x0000 Questions UDPPut(0x00); UDPPut(0x00); // 0x0001 Answers RRs UDPPut(0x01); UDPPut(0x00); // 0x0000 Authority RRs UDPPut(0x00); UDPPut(0x00); // 0x0000 Additional RRs UDPPut(0x00); DNSCopyRXNameToTX(); // Copy hostname of first question over to TX packet UDPPut(0x00); // Type A Host address UDPPut(0x01); UDPPut(0x00); // Class INternet UDPPut(0x01); UDPPut(0x00); // Time to Live 10 seconds UDPPut(0x00); UDPPut(0x00); UDPPut(0x0A); UDPPut(0x00); // Data Length 4 bytes UDPPut(0x04); UDPPutArray((BYTE*)&AppConfig.MyIPAddr.Val, 4); // Our IP address #warning tim had added extra code here UDPFlush(); }
/***************************************************************************** Function: int connect( SOCKET s, struct sockaddr* name, int namelen ) Summary: This function connects to the peer communications end point. Description: The connect function assigns the address of the peer communications endpoint. For stream sockets, connection is established between the endpoints. For datagram sockets, an address filter is established between the endpoints until changed with another connect() function. Precondition: socket function should be called. Parameters: s - Socket descriptor returned from a previous call to socket. name - pointer to the sockaddr structure containing the peer address and port number. namelen - length of the sockaddr structure. Returns: If the connect() function succeeds, it returns 0. Otherwise, the value SOCKET_ERROR is returned to indicate an error condition. For stream based socket, if the connection is not established yet, connect returns SOCKET_CNXN_IN_PROGRESS. Remarks: None. ***************************************************************************/ int connect( SOCKET s, struct sockaddr* name, int namelen ) { struct BSDSocket *socket; struct sockaddr_in *addr; DWORD remoteIP; WORD remotePort; WORD localPort; if( s >= BSD_SOCKET_COUNT ) return SOCKET_ERROR; socket = &BSDSocketArray[s]; if( socket->bsdState < SKT_CREATED ) return SOCKET_ERROR; if( (unsigned int)namelen < sizeof(struct sockaddr_in)) return SOCKET_ERROR; addr = (struct sockaddr_in *)name; remotePort = addr->sin_port; remoteIP = addr->sin_addr.S_un.S_addr; if( remoteIP == 0u || remotePort == 0u ) return SOCKET_ERROR; if( socket->SocketType == SOCK_STREAM ) { switch(socket->bsdState) { case SKT_EST: return 0; // already established case SKT_IN_PROGRESS: if(HandlePossibleTCPDisconnection(s)) return SOCKET_ERROR; if(!TCPIsConnected(socket->SocketID)) return SOCKET_CNXN_IN_PROGRESS; socket->bsdState = SKT_EST; return 0; //success case SKT_CREATED: case SKT_BOUND: socket->SocketID = TCPOpen(remoteIP, TCP_OPEN_IP_ADDRESS, remotePort, TCP_PURPOSE_BERKELEY_CLIENT); if(socket->SocketID == INVALID_SOCKET) return SOCKET_ERROR; // Clear the first reset flag TCPWasReset(socket->SocketID); socket->isServer = FALSE; socket->bsdState = SKT_IN_PROGRESS; return SOCKET_CNXN_IN_PROGRESS; default: return SOCKET_ERROR; } } else { // If not explicitly bound to a local port, implicitly do the binding if(socket->bsdState == SKT_CREATED) { localPort = gAutoPortNumber++; if(gAutoPortNumber > 5000u) // reset the port numbers gAutoPortNumber = 1024; //socket->SocketID = UDPOpen(localPort, NULL, remotePort); socket->SocketID = UDPOpenEx(0,UDP_OPEN_SERVER,localPort, remotePort); if(socket->SocketID == INVALID_UDP_SOCKET) return SOCKET_ERROR; socket->bsdState = SKT_BOUND; } if(socket->bsdState != SKT_BOUND) return SOCKET_ERROR; // UDP: remote port is used as a filter. Need to call connect when using // send/recv calls. No need to call 'connect' if using sendto/recvfrom // calls. socket->remotePort = remotePort; socket->remoteIP = remoteIP; return 0; //success } return SOCKET_ERROR; }
/***************************************************************************** Function: BOOL DNSIsResolved(IP_ADDR* HostIP) Summary: Determines if the DNS resolution is complete and provides the IP. Description: Call this function to determine if the DNS resolution of an address has been completed. If so, the resolved address will be provided in HostIP. Precondition: DNSResolve or DNSResolveROM has been called. Parameters: HostIP - A pointer to an IP_ADDR structure in which to store the resolved IP address once resolution is complete. Return Values: TRUE - The DNS client has obtained an IP, or the DNS process has encountered an error. HostIP will be 0.0.0.0 on error. Possible errors include server timeout (i.e. DNS server not available), hostname not in the DNS, or DNS server errors. FALSE - The resolution process is still in progress. ***************************************************************************/ BOOL DNSIsResolved(IP_ADDR* HostIP) { static DWORD StartTime; static WORD_VAL SentTransactionID __attribute__((persistent)); static BYTE vARPAttemptCount; static BYTE vDNSAttemptCount; BYTE i; WORD_VAL w; DNS_HEADER DNSHeader; DNS_ANSWER_HEADER DNSAnswerHeader; switch(smDNS) { case DNS_START: vARPAttemptCount = 0; vDNSAttemptCount = 0; // No break; case DNS_ARP_START_RESOLVE: ARPResolve(&AppConfig.PrimaryDNSServer); vARPAttemptCount++; StartTime = TickGet(); smDNS = DNS_ARP_RESOLVE; break; case DNS_ARP_RESOLVE: if(!ARPIsResolved(&AppConfig.PrimaryDNSServer, &ResolvedInfo.MACAddr)) { if(TickGet() - StartTime > DNS_TIMEOUT) smDNS = (vARPAttemptCount >= 3u) ? DNS_FAIL : DNS_ARP_START_RESOLVE; break; } ResolvedInfo.IPAddr.Val = AppConfig.PrimaryDNSServer.Val; smDNS = DNS_OPEN_SOCKET; // No break: DNS_OPEN_SOCKET is the correct next state case DNS_OPEN_SOCKET: //MySocket = UDPOpen(0, &ResolvedInfo, DNS_PORT); MySocket = UDPOpenEx((DWORD)(PTR_BASE)&ResolvedInfo,UDP_OPEN_NODE_INFO,0, DNS_PORT); if(MySocket == INVALID_UDP_SOCKET) break; smDNS = DNS_QUERY; // No need to break, we can immediately start resolution case DNS_QUERY: if(!UDPIsPutReady(MySocket)) break; // Put DNS query here SentTransactionID.Val++; UDPPut(SentTransactionID.v[1]);// User chosen transaction ID UDPPut(SentTransactionID.v[0]); UDPPut(0x01); // Standard query with recursion UDPPut(0x00); UDPPut(0x00); // 0x0001 questions UDPPut(0x01); UDPPut(0x00); // 0x0000 answers UDPPut(0x00); UDPPut(0x00); // 0x0000 name server resource records UDPPut(0x00); UDPPut(0x00); // 0x0000 additional records UDPPut(0x00); // Put hostname string to resolve if(DNSHostName) DNSPutString(DNSHostName); else DNSPutROMString(DNSHostNameROM); UDPPut(0x00); // Type: DNS_TYPE_A A (host address) or DNS_TYPE_MX for mail exchange UDPPut(RecordType); UDPPut(0x00); // Class: IN (Internet) UDPPut(0x01); UDPFlush(); StartTime = TickGet(); smDNS = DNS_GET_RESULT; break; case DNS_GET_RESULT: if(!UDPIsGetReady(MySocket)) { if(TickGet() - StartTime > DNS_TIMEOUT) smDNS = DNS_FAIL; break; } // Retrieve the DNS header and de-big-endian it UDPGet(&DNSHeader.TransactionID.v[1]); UDPGet(&DNSHeader.TransactionID.v[0]); // Throw this packet away if it isn't in response to our last query if(DNSHeader.TransactionID.Val != SentTransactionID.Val) { UDPDiscard(); break; } UDPGet(&DNSHeader.Flags.v[1]); UDPGet(&DNSHeader.Flags.v[0]); UDPGet(&DNSHeader.Questions.v[1]); UDPGet(&DNSHeader.Questions.v[0]); UDPGet(&DNSHeader.Answers.v[1]); UDPGet(&DNSHeader.Answers.v[0]); UDPGet(&DNSHeader.AuthoritativeRecords.v[1]); UDPGet(&DNSHeader.AuthoritativeRecords.v[0]); UDPGet(&DNSHeader.AdditionalRecords.v[1]); UDPGet(&DNSHeader.AdditionalRecords.v[0]); // Remove all questions (queries) while(DNSHeader.Questions.Val--) { DNSDiscardName(); UDPGet(&w.v[1]); // Question type UDPGet(&w.v[0]); UDPGet(&w.v[1]); // Question class UDPGet(&w.v[0]); } // Scan through answers while(DNSHeader.Answers.Val--) { DNSDiscardName(); // Throw away response name UDPGet(&DNSAnswerHeader.ResponseType.v[1]); // Response type UDPGet(&DNSAnswerHeader.ResponseType.v[0]); UDPGet(&DNSAnswerHeader.ResponseClass.v[1]); // Response class UDPGet(&DNSAnswerHeader.ResponseClass.v[0]); UDPGet(&DNSAnswerHeader.ResponseTTL.v[3]); // Time to live UDPGet(&DNSAnswerHeader.ResponseTTL.v[2]); UDPGet(&DNSAnswerHeader.ResponseTTL.v[1]); UDPGet(&DNSAnswerHeader.ResponseTTL.v[0]); UDPGet(&DNSAnswerHeader.ResponseLen.v[1]); // Response length UDPGet(&DNSAnswerHeader.ResponseLen.v[0]); // Make sure that this is a 4 byte IP address, response type A or MX, class 1 // Check if this is Type A or MX if( DNSAnswerHeader.ResponseType.Val == 0x0001u && DNSAnswerHeader.ResponseClass.Val == 0x0001u && // Internet class DNSAnswerHeader.ResponseLen.Val == 0x0004u) { Flags.bits.AddressValid = TRUE; UDPGet(&ResolvedInfo.IPAddr.v[0]); UDPGet(&ResolvedInfo.IPAddr.v[1]); UDPGet(&ResolvedInfo.IPAddr.v[2]); UDPGet(&ResolvedInfo.IPAddr.v[3]); goto DoneSearchingRecords; } else { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(&i); } } } // Remove all Authoritative Records while(DNSHeader.AuthoritativeRecords.Val--) { DNSDiscardName(); // Throw away response name UDPGet(&DNSAnswerHeader.ResponseType.v[1]); // Response type UDPGet(&DNSAnswerHeader.ResponseType.v[0]); UDPGet(&DNSAnswerHeader.ResponseClass.v[1]); // Response class UDPGet(&DNSAnswerHeader.ResponseClass.v[0]); UDPGet(&DNSAnswerHeader.ResponseTTL.v[3]); // Time to live UDPGet(&DNSAnswerHeader.ResponseTTL.v[2]); UDPGet(&DNSAnswerHeader.ResponseTTL.v[1]); UDPGet(&DNSAnswerHeader.ResponseTTL.v[0]); UDPGet(&DNSAnswerHeader.ResponseLen.v[1]); // Response length UDPGet(&DNSAnswerHeader.ResponseLen.v[0]); // Make sure that this is a 4 byte IP address, response type A or MX, class 1 // Check if this is Type A if( DNSAnswerHeader.ResponseType.Val == 0x0001u && DNSAnswerHeader.ResponseClass.Val == 0x0001u && // Internet class DNSAnswerHeader.ResponseLen.Val == 0x0004u) { Flags.bits.AddressValid = TRUE; UDPGet(&ResolvedInfo.IPAddr.v[0]); UDPGet(&ResolvedInfo.IPAddr.v[1]); UDPGet(&ResolvedInfo.IPAddr.v[2]); UDPGet(&ResolvedInfo.IPAddr.v[3]); goto DoneSearchingRecords; } else { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(&i); } } } // Remove all Additional Records while(DNSHeader.AdditionalRecords.Val--) { DNSDiscardName(); // Throw away response name UDPGet(&DNSAnswerHeader.ResponseType.v[1]); // Response type UDPGet(&DNSAnswerHeader.ResponseType.v[0]); UDPGet(&DNSAnswerHeader.ResponseClass.v[1]); // Response class UDPGet(&DNSAnswerHeader.ResponseClass.v[0]); UDPGet(&DNSAnswerHeader.ResponseTTL.v[3]); // Time to live UDPGet(&DNSAnswerHeader.ResponseTTL.v[2]); UDPGet(&DNSAnswerHeader.ResponseTTL.v[1]); UDPGet(&DNSAnswerHeader.ResponseTTL.v[0]); UDPGet(&DNSAnswerHeader.ResponseLen.v[1]); // Response length UDPGet(&DNSAnswerHeader.ResponseLen.v[0]); // Make sure that this is a 4 byte IP address, response type A or MX, class 1 // Check if this is Type A if( DNSAnswerHeader.ResponseType.Val == 0x0001u && DNSAnswerHeader.ResponseClass.Val == 0x0001u && // Internet class DNSAnswerHeader.ResponseLen.Val == 0x0004u) { Flags.bits.AddressValid = TRUE; UDPGet(&ResolvedInfo.IPAddr.v[0]); UDPGet(&ResolvedInfo.IPAddr.v[1]); UDPGet(&ResolvedInfo.IPAddr.v[2]); UDPGet(&ResolvedInfo.IPAddr.v[3]); goto DoneSearchingRecords; } else { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(&i); } } } DoneSearchingRecords: UDPDiscard(); UDPClose(MySocket); MySocket = INVALID_UDP_SOCKET; smDNS = DNS_DONE; // No break, DNS_DONE is the correct step case DNS_DONE: // Return 0.0.0.0 if DNS resolution failed, otherwise return the // resolved IP address if(!Flags.bits.AddressValid) ResolvedInfo.IPAddr.Val = 0; HostIP->Val = ResolvedInfo.IPAddr.Val; return TRUE; case DNS_FAIL: // If 3 attempts or more, quit if(vDNSAttemptCount >= 2u) { // Return an invalid IP address 0.0.0.0 if we can't finish ARP or DNS query step HostIP->Val = 0x00000000; return TRUE; } vDNSAttemptCount++; // Swap primary and secondary DNS servers if there is a secondary DNS server programmed if(AppConfig.SecondaryDNSServer.Val) { AppConfig.PrimaryDNSServer.Val ^= AppConfig.SecondaryDNSServer.Val; AppConfig.SecondaryDNSServer.Val ^= AppConfig.PrimaryDNSServer.Val; AppConfig.PrimaryDNSServer.Val ^= AppConfig.SecondaryDNSServer.Val; // Start another ARP resolution for the secondary server (now primary) vARPAttemptCount = 0; if(MySocket != INVALID_UDP_SOCKET) { UDPClose(MySocket); MySocket = INVALID_UDP_SOCKET; } smDNS = DNS_ARP_START_RESOLVE; } break; } return FALSE; }
/***************************************************************************** Function: void DHCPServerTask(void) Summary: Performs periodic DHCP server tasks. Description: This function performs any periodic tasks requied by the DHCP server module, such as processing DHCP requests and distributing IP addresses. Precondition: None Parameters: None Returns: None ***************************************************************************/ void DHCPServerTask(void) { BYTE i; BYTE Option, Len; BOOTP_HEADER BOOTPHeader; DWORD dw; BOOL bAccept; static enum { DHCP_OPEN_SOCKET, DHCP_LISTEN } smDHCPServer = DHCP_OPEN_SOCKET; #if defined(STACK_USE_DHCP_CLIENT) // Make sure we don't clobber anyone else's DHCP server if(DHCPIsServerDetected(0)) return; #endif if(!bDHCPServerEnabled) return; switch(smDHCPServer) { case DHCP_OPEN_SOCKET: // Obtain a UDP socket to listen/transmit on //MySocket = UDPOpen(DHCP_SERVER_PORT, NULL, DHCP_CLIENT_PORT); MySocket = UDPOpenEx(0,UDP_OPEN_SERVER,DHCP_SERVER_PORT, DHCP_CLIENT_PORT); if(MySocket == INVALID_UDP_SOCKET) break; // Decide which address to lease out // Note that this needs to be changed if we are to // support more than one lease DHCPNextLease.Val = (AppConfig.MyIPAddr.Val & AppConfig.MyMask.Val) + 0x02000000; if(DHCPNextLease.v[3] == 255u) DHCPNextLease.v[3] += 0x03; if(DHCPNextLease.v[3] == 0u) DHCPNextLease.v[3] += 0x02; smDHCPServer++; case DHCP_LISTEN: // Check to see if a valid DHCP packet has arrived if(UDPIsGetReady(MySocket) < 241u) break; // Retrieve the BOOTP header UDPGetArray((BYTE*)&BOOTPHeader, sizeof(BOOTPHeader)); bAccept = (BOOTPHeader.ClientIP.Val == DHCPNextLease.Val) || (BOOTPHeader.ClientIP.Val == 0x00000000u); // Validate first three fields if(BOOTPHeader.MessageType != 1u) break; if(BOOTPHeader.HardwareType != 1u) break; if(BOOTPHeader.HardwareLen != 6u) break; // Throw away 10 unused bytes of hardware address, // server host name, and boot file name -- unsupported/not needed. for(i = 0; i < 64+128+(16-sizeof(MAC_ADDR)); i++) UDPGet(&Option); // Obtain Magic Cookie and verify UDPGetArray((BYTE*)&dw, sizeof(DWORD)); if(dw != 0x63538263ul) break; // Obtain options while(1) { // Get option type if(!UDPGet(&Option)) break; if(Option == DHCP_END_OPTION) break; // Get option length UDPGet(&Len); // Process option switch(Option) { case DHCP_MESSAGE_TYPE: UDPGet(&i); switch(i) { case DHCP_DISCOVER_MESSAGE: DHCPReplyToDiscovery(&BOOTPHeader); break; case DHCP_REQUEST_MESSAGE: DHCPReplyToRequest(&BOOTPHeader, bAccept); break; // Need to handle these if supporting more than one DHCP lease case DHCP_RELEASE_MESSAGE: case DHCP_DECLINE_MESSAGE: break; } break; case DHCP_PARAM_REQUEST_IP_ADDRESS: if(Len == 4u) { // Get the requested IP address and see if it is the one we have on offer. UDPGetArray((BYTE*)&dw, 4); Len -= 4; bAccept = (dw == DHCPNextLease.Val); } break; case DHCP_END_OPTION: UDPDiscard(); return; } // Remove any unprocessed bytes that we don't care about while(Len--) { UDPGet(&i); } } UDPDiscard(); break; } }
/***************************************************************************** Function: CHAR TFTPGetUploadStatus(void) Summary: Returns the TFTP file upload status started by calling the TFTPUploadRAMFileToHost() or TFTPUploadFragmentedRAMFileToHost() functions. Description: Returns the TFTP file upload status started by calling the TFTPUploadRAMFileToHost() or TFTPUploadFragmentedRAMFileToHost() functions. Precondition: None Parameters: None Returns: A status code. Negative results are fatal errors. Positive results indicate the TFTP upload operation is still being processed. A zero result indicates successful file upload completion (TFTP API is now idle and available for further calls). Specific return values are as follows: 0 (TFTP_UPLOAD_COMPLETE): Upload completed successfully 1 (TFTP_UPLOAD_GET_DNS): Attempting to obtain DNS client module 2 (TFTP_UPLOAD_RESOLVE_HOST): Attempting to resolve TFTP hostname 3 (TFTP_UPLOAD_CONNECT): Attempting to ARP and contact the TFTP server 4 (TFTP_UPLOAD_SEND_FILENAME): Attempting to send the filename and receive acknowledgement. 5 (TFTP_UPLOAD_SEND_DATA): Attempting to send the file contents and receive acknowledgement. 6 (TFTP_UPLOAD_WAIT_FOR_CLOSURE): Attempting to send the final packet of file contents and receive acknowledgement. -1 (TFTP_UPLOAD_HOST_RESOLVE_TIMEOUT): Couldn't resolve hostname -2 (TFTP_UPLOAD_CONNECT_TIMEOUT): Couldn't finish ARP and reach server -3 (TFTP_UPLOAD_SERVER_ERROR): TFTP server returned an error (ex: access denial) or file upload failed due to a timeout (partial file may have been uploaded). Remarks: The DNS client module must be enabled to use this function. i.e. STACK_USE_DNS must be defined in TCPIPConfig.h. ***************************************************************************/ CHAR TFTPGetUploadStatus(void) { TFTP_RESULT result; IP_ADDR ipRemote; WORD w, w2; BYTE *vData; if(UDPIsOpened(_tftpSocket)== FALSE) { _tftpSocket = UDPOpenEx((DWORD)(ROM_PTR_BASE)vUploadRemoteHost, UDP_OPEN_ROM_HOST,TFTP_CLIENT_PORT, TFTP_SERVER_PORT); } switch(smUpload) { case TFTP_UPLOAD_GET_DNS: if(!DNSBeginUsage()) break; DNSResolveROM(vUploadRemoteHost, DNS_TYPE_A); smUpload = TFTP_UPLOAD_RESOLVE_HOST; break; case TFTP_UPLOAD_RESOLVE_HOST: if(!DNSIsResolved(&ipRemote)) break; DNSEndUsage(); if(ipRemote.Val == 0u) { smUpload = TFTP_UPLOAD_HOST_RESOLVE_TIMEOUT; break; } TFTPOpen(&ipRemote); smUpload = TFTP_UPLOAD_CONNECT; break; case TFTP_UPLOAD_CONNECT: switch(TFTPIsOpened()) { case TFTP_OK: TFTPOpenROMFile(vUploadFilename, TFTP_FILE_MODE_WRITE); smUpload = TFTP_UPLOAD_SEND_FILENAME; break; case TFTP_TIMEOUT: smUpload = TFTP_UPLOAD_CONNECT_TIMEOUT; break; default: break; } break; case TFTP_UPLOAD_SEND_FILENAME: result = TFTPIsFileOpened(); switch(result) { case TFTP_OK: smUpload = TFTP_UPLOAD_SEND_DATA; break; case TFTP_RETRY: TFTPOpenROMFile(vUploadFilename, TFTP_FILE_MODE_WRITE); break; case TFTP_TIMEOUT: smUpload = TFTP_UPLOAD_CONNECT_TIMEOUT; break; case TFTP_ERROR: smUpload = TFTP_UPLOAD_SERVER_ERROR; break; default: break; } if(result != TFTP_OK) break; // No break when TFTPIsFileOpened() returns TFTP_OK -- we need to immediately start sending data case TFTP_UPLOAD_SEND_DATA: switch(TFTPIsPutReady()) { case TFTP_OK: // Write blocksize bytes of data uploadChunkDescriptorForRetransmit = uploadChunkDescriptor; wUploadChunkOffsetForRetransmit = wUploadChunkOffset; vData = uploadChunkDescriptor->vDataPointer + wUploadChunkOffset; w = TFTP_BLOCK_SIZE; while(w) { w2 = uploadChunkDescriptor->wDataLength - wUploadChunkOffset; if(w2 > w) w2 = w; w -= w2; wUploadChunkOffset += w2; if(vData == NULL) { TFTPCloseFile(); smUpload = TFTP_UPLOAD_WAIT_FOR_CLOSURE; break; } while(w2--) { TFTPPut(*vData++); } if(wUploadChunkOffset == uploadChunkDescriptor->wDataLength) { uploadChunkDescriptor++; wUploadChunkOffset = 0; vData = uploadChunkDescriptor->vDataPointer; } } break; case TFTP_RETRY: uploadChunkDescriptor = uploadChunkDescriptorForRetransmit; wUploadChunkOffset = wUploadChunkOffsetForRetransmit; break; case TFTP_TIMEOUT: case TFTP_ERROR: smUpload = TFTP_UPLOAD_SERVER_ERROR; break; default: break; } break; case TFTP_UPLOAD_WAIT_FOR_CLOSURE: switch(TFTPIsFileClosed()) { case TFTP_OK: smUpload = TFTP_UPLOAD_COMPLETE; UDPClose(_tftpSocket); break; case TFTP_RETRY: uploadChunkDescriptor = uploadChunkDescriptorForRetransmit; wUploadChunkOffset = wUploadChunkOffsetForRetransmit; smUpload = TFTP_UPLOAD_SEND_DATA; break; case TFTP_TIMEOUT: case TFTP_ERROR: smUpload = TFTP_UPLOAD_SERVER_ERROR; break; default: break; } break; default: break; } return smUpload; }
/********************************************************************* * Function: void DiscoveryTask(void) * * Summary: Announce callback task. * * PreCondition: Stack is initialized() * * Input: None * * Output: None * * Side Effects: None * * Overview: Recurring task used to listen for Discovery * messages on the specified ANNOUNCE_PORT. These * messages can be sent using the Microchip Device * Discoverer tool. If one is received, this * function will transmit a reply. * * Note: A UDP socket must be available before this * function is called. It is freed at the end of * the function. MAX_UDP_SOCKETS may need to be * increased if other modules use UDP sockets. ********************************************************************/ void DiscoveryTask(void) { static enum { DISCOVERY_HOME = 0, DISCOVERY_LISTEN, DISCOVERY_REQUEST_RECEIVED, DISCOVERY_DISABLED } DiscoverySM = DISCOVERY_HOME; static UDP_SOCKET MySocket; BYTE i; switch(DiscoverySM) { case DISCOVERY_HOME: // Open a UDP socket for inbound and outbound transmission // Since we expect to only receive broadcast packets and // only send unicast packets directly to the node we last // received from, the remote NodeInfo parameter can be anything //MySocket = UDPOpen(ANNOUNCE_PORT, NULL, ANNOUNCE_PORT); MySocket = UDPOpenEx(0,UDP_OPEN_SERVER,ANNOUNCE_PORT, ANNOUNCE_PORT); if(MySocket == INVALID_UDP_SOCKET) return; else DiscoverySM++; break; case DISCOVERY_LISTEN: // Do nothing if no data is waiting if(!UDPIsGetReady(MySocket)) return; // See if this is a discovery query or reply UDPGet(&i); UDPDiscard(); if(i != 'D') return; // We received a discovery request, reply when we can DiscoverySM++; // Change the destination to the unicast address of the last received packet memcpy((void*)&UDPSocketInfo[MySocket].remote.remoteNode, (const void*)&remoteNode, sizeof(remoteNode)); // No break needed. If we get down here, we are now ready for the DISCOVERY_REQUEST_RECEIVED state case DISCOVERY_REQUEST_RECEIVED: if(!UDPIsPutReady(MySocket)) return; // Begin sending our MAC address in human readable form. // The MAC address theoretically could be obtained from the // packet header when the computer receives our UDP packet, // however, in practice, the OS will abstract away the useful // information and it would be difficult to obtain. It also // would be lost if this broadcast packet were forwarded by a // router to a different portion of the network (note that // broadcasts are normally not forwarded by routers). UDPPutArray((BYTE*)AppConfig.NetBIOSName, sizeof(AppConfig.NetBIOSName)-1); UDPPut('\r'); UDPPut('\n'); // Convert the MAC address bytes to hex (text) and then send it i = 0; while(1) { UDPPut(btohexa_high(AppConfig.MyMACAddr.v[i])); UDPPut(btohexa_low(AppConfig.MyMACAddr.v[i])); if(++i == 6u) break; UDPPut('-'); } UDPPut('\r'); UDPPut('\n'); // Send the packet UDPFlush(); // Listen for other discovery requests DiscoverySM = DISCOVERY_LISTEN; break; case DISCOVERY_DISABLED: break; } }