/********************************************************************* * 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: void DNSServerTask(NET_CONFIG* pNet) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: Sends dummy responses that point to ourself for DNS requests * * Note: None ********************************************************************/ void DNSServerTask(NET_CONFIG* pNet) { static UDP_SOCKET MySocket = INVALID_UDP_SOCKET; static struct { uint16_t wTransactionID; uint16_t wFlags; uint16_t wQuestions; uint16_t wAnswerRRs; uint16_t wAuthorityRRs; uint16_t wAdditionalRRs; } DNSHeader; switch(dnssState) { case DNSS_STATE_START: // Create a socket to listen on if this is the first time calling this function if(MySocket == INVALID_UDP_SOCKET) { MySocket = UDPOpen(0,UDP_OPEN_SERVER,DNS_PORT,0); if(MySocket == INVALID_UDP_SOCKET) break; } UDPSocketSetNet(MySocket, pNet); dnssState = DNSS_STATE_WAIT_REQUEST; break; case DNSS_STATE_WAIT_REQUEST: // See if a DNS query packet has arrived if(UDPIsGetReady(MySocket) < sizeof(DNSHeader)) break; // Read DNS header UDPGetArray(MySocket, (uint8_t*)&DNSHeader, sizeof(DNSHeader)); // Ignore this packet if it isn't a query if((DNSHeader.wFlags & 0x8000) == 0x8000u) break; // Ignore this packet if there are no questions in it if(DNSHeader.wQuestions == 0u) break; dnssState = DNSS_STATE_PUT_REQUEST; break; case DNSS_STATE_PUT_REQUEST: // check that we can transmit a DNS response packet if(!UDPIsPutReady(MySocket)) { break; } // Write DNS response packet UDPPutArray(MySocket, (uint8_t*)&DNSHeader.wTransactionID, 2); // 2 byte Transaction ID if(DNSHeader.wFlags & 0x0100) UDPPut(MySocket, 0x81); // Message is a response with recursion desired else UDPPut(MySocket, 0x80); // Message is a response without recursion desired flag set UDPPut(MySocket, 0x80); // Recursion available UDPPut(MySocket, 0x00); // 0x0000 Questions UDPPut(MySocket, 0x00); UDPPut(MySocket, 0x00); // 0x0001 Answers RRs UDPPut(MySocket, 0x01); UDPPut(MySocket, 0x00); // 0x0000 Authority RRs UDPPut(MySocket, 0x00); UDPPut(MySocket, 0x00); // 0x0000 Additional RRs UDPPut(MySocket, 0x00); DNSCopyRXNameToTX(MySocket, pNet); // Copy hostname of first question over to TX packet UDPPut(MySocket, 0x00); // Type A Host address UDPPut(MySocket, 0x01); UDPPut(MySocket, 0x00); // Class INternet UDPPut(MySocket, 0x01); UDPPut(MySocket, 0x00); // Time to Live 10 seconds UDPPut(MySocket, 0x00); UDPPut(MySocket, 0x00); UDPPut(MySocket, 0x0A); UDPPut(MySocket, 0x00); // Data Length 4 bytes UDPPut(MySocket, 0x04); UDPPutArray(MySocket, (uint8_t*)&pNet->MyIPAddr.Val, 4); // Our IP address UDPFlush(MySocket); dnssState = DNSS_STATE_DONE; break; case DNSS_STATE_DONE: break; } }