PageRenderTime 996ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Programms/dsPIC33/Microchip/TCPIP Stack/DNSs.c

http://astromonty.googlecode.com/
C | 214 lines | 87 code | 25 blank | 102 comment | 13 complexity | c0fdd1bc47214177d4160fb187282fa4 MD5 | raw file
  1. /*********************************************************************
  2. *
  3. * Domain Name System (DNS) Server dummy
  4. * Module for Microchip TCP/IP Stack
  5. * -Acts as a DNS server, but gives out the local IP address for all
  6. * queries to force web browsers to access the board.
  7. * -Reference: RFC 1034 and RFC 1035
  8. *
  9. *********************************************************************
  10. * FileName: DNSs.c
  11. * Dependencies: UDP
  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. * Company: Microchip Technology, Inc.
  17. *
  18. * Software License Agreement
  19. *
  20. * Copyright (C) 2002-2010 Microchip Technology Inc. All rights
  21. * reserved.
  22. *
  23. * Microchip licenses to you the right to use, modify, copy, and
  24. * distribute:
  25. * (i) the Software when embedded on a Microchip microcontroller or
  26. * digital signal controller product ("Device") which is
  27. * integrated into Licensee's product; or
  28. * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
  29. * ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device
  30. * used in conjunction with a Microchip ethernet controller for
  31. * the sole purpose of interfacing with the ethernet controller.
  32. *
  33. * You should refer to the license agreement accompanying this
  34. * Software for additional information regarding your rights and
  35. * obligations.
  36. *
  37. * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
  38. * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
  39. * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  40. * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  41. * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
  42. * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
  43. * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
  44. * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
  45. * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
  46. * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
  47. * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
  48. *
  49. *
  50. * Author Date Comment
  51. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  52. * Howard Schlunder 01/18/2010 Original
  53. ********************************************************************/
  54. #define DNSS_C
  55. #include "TCPIPConfig.h"
  56. #if defined(STACK_USE_DNS_SERVER)
  57. #include "TCPIP Stack/TCPIP.h"
  58. // Default port for the DNS server to listen on
  59. #define DNS_PORT 53u
  60. static void DNSCopyRXNameToTX(void);
  61. /*********************************************************************
  62. * Function: void DNSServerTask(void)
  63. *
  64. * PreCondition: None
  65. *
  66. * Input: None
  67. *
  68. * Output: None
  69. *
  70. * Side Effects: None
  71. *
  72. * Overview: Sends dummy responses that point to ourself for DNS requests
  73. *
  74. * Note: None
  75. ********************************************************************/
  76. void DNSServerTask(void)
  77. {
  78. static UDP_SOCKET MySocket = INVALID_UDP_SOCKET;
  79. struct
  80. {
  81. WORD wTransactionID;
  82. WORD wFlags;
  83. WORD wQuestions;
  84. WORD wAnswerRRs;
  85. WORD wAuthorityRRs;
  86. WORD wAdditionalRRs;
  87. } DNSHeader;
  88. // Create a socket to listen on if this is the first time calling this function
  89. if(MySocket == INVALID_UDP_SOCKET)
  90. {
  91. MySocket = UDPOpen(DNS_PORT, NULL, 0);
  92. return;
  93. }
  94. // See if a DNS query packet has arrived
  95. if(UDPIsGetReady(MySocket) < sizeof(DNSHeader))
  96. return;
  97. // Read DNS header
  98. UDPGetArray((BYTE*)&DNSHeader, sizeof(DNSHeader));
  99. // Ignore this packet if it isn't a query
  100. if((DNSHeader.wFlags & 0x8000) == 0x8000u)
  101. return;
  102. // Ignore this packet if there are no questions in it
  103. if(DNSHeader.wQuestions == 0u)
  104. return;
  105. // Block until we can transmit a DNS response packet
  106. while(!UDPIsPutReady(MySocket));
  107. // Write DNS response packet
  108. UDPPutArray((BYTE*)&DNSHeader.wTransactionID, 2); // 2 byte Transaction ID
  109. if(DNSHeader.wFlags & 0x0100)
  110. UDPPut(0x81); // Message is a response with recursion desired
  111. else
  112. UDPPut(0x80); // Message is a response without recursion desired flag set
  113. UDPPut(0x80); // Recursion available
  114. UDPPut(0x00); // 0x0000 Questions
  115. UDPPut(0x00);
  116. UDPPut(0x00); // 0x0001 Answers RRs
  117. UDPPut(0x01);
  118. UDPPut(0x00); // 0x0000 Authority RRs
  119. UDPPut(0x00);
  120. UDPPut(0x00); // 0x0000 Additional RRs
  121. UDPPut(0x00);
  122. DNSCopyRXNameToTX(); // Copy hostname of first question over to TX packet
  123. UDPPut(0x00); // Type A Host address
  124. UDPPut(0x01);
  125. UDPPut(0x00); // Class INternet
  126. UDPPut(0x01);
  127. UDPPut(0x00); // Time to Live 10 seconds
  128. UDPPut(0x00);
  129. UDPPut(0x00);
  130. UDPPut(0x0A);
  131. UDPPut(0x00); // Data Length 4 bytes
  132. UDPPut(0x04);
  133. UDPPutArray((BYTE*)&AppConfig.MyIPAddr.Val, 4); // Our IP address
  134. UDPFlush();
  135. }
  136. /*****************************************************************************
  137. Function:
  138. static void DNSCopyRXNameToTX(void)
  139. Summary:
  140. Copies a DNS hostname, possibly including name compression, from the RX
  141. packet to the TX packet (without name compression in TX case).
  142. Description:
  143. None
  144. Precondition:
  145. RX pointer is set to currently point to the DNS name to copy
  146. Parameters:
  147. None
  148. Returns:
  149. None
  150. ***************************************************************************/
  151. static void DNSCopyRXNameToTX(void)
  152. {
  153. WORD w;
  154. BYTE i;
  155. BYTE len;
  156. while(1)
  157. {
  158. // Get first byte which will tell us if this is a 16-bit pointer or the
  159. // length of the first of a series of labels
  160. if(!UDPGet(&i))
  161. return;
  162. // Check if this is a pointer, if so, get the reminaing 8 bits and seek to the pointer value
  163. if((i & 0xC0u) == 0xC0u)
  164. {
  165. ((BYTE*)&w)[1] = i & 0x3F;
  166. UDPGet((BYTE*)&w);
  167. IPSetRxBuffer(sizeof(UDP_HEADER) + w);
  168. continue;
  169. }
  170. // Write the length byte
  171. len = i;
  172. UDPPut(len);
  173. // Exit if we've reached a zero length label
  174. if(len == 0u)
  175. return;
  176. // Copy all of the bytes in this label
  177. while(len--)
  178. {
  179. UDPGet(&i);
  180. UDPPut(i);
  181. }
  182. }
  183. }
  184. #endif //#if defined(STACK_USE_DNS_SERVER)