PageRenderTime 71ms CodeModel.GetById 60ms RepoModel.GetById 1ms app.codeStats 0ms

/CCS_5 C code/TCPIP/DNSs.c

https://gitlab.com/ammar.rajab1/ENC28J60-LAN
C | 216 lines | 88 code | 25 blank | 103 comment | 13 complexity | 7e1882b7b1624d4effb2e8ab3f2be137 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. MySocket = UDPOpenEx(0,UDP_OPEN_SERVER,DNS_PORT,0);
  93. return;
  94. }
  95. // See if a DNS query packet has arrived
  96. if(UDPIsGetReady(MySocket) < sizeof(DNSHeader))
  97. return;
  98. // Read DNS header
  99. UDPGetArray((BYTE*)&DNSHeader, sizeof(DNSHeader));
  100. // Ignore this packet if it isn't a query
  101. if((DNSHeader.wFlags & 0x8000) == 0x8000u)
  102. return;
  103. // Ignore this packet if there are no questions in it
  104. if(DNSHeader.wQuestions == 0u)
  105. return;
  106. // Block until we can transmit a DNS response packet
  107. while(!UDPIsPutReady(MySocket));
  108. // Write DNS response packet
  109. UDPPutArray((BYTE*)&DNSHeader.wTransactionID, 2); // 2 byte Transaction ID
  110. if(DNSHeader.wFlags & 0x0100)
  111. UDPPut(0x81); // Message is a response with recursion desired
  112. else
  113. UDPPut(0x80); // Message is a response without recursion desired flag set
  114. UDPPut(0x80); // Recursion available
  115. UDPPut(0x00); // 0x0000 Questions
  116. UDPPut(0x00);
  117. UDPPut(0x00); // 0x0001 Answers RRs
  118. UDPPut(0x01);
  119. UDPPut(0x00); // 0x0000 Authority RRs
  120. UDPPut(0x00);
  121. UDPPut(0x00); // 0x0000 Additional RRs
  122. UDPPut(0x00);
  123. DNSCopyRXNameToTX(); // Copy hostname of first question over to TX packet
  124. UDPPut(0x00); // Type A Host address
  125. UDPPut(0x01);
  126. UDPPut(0x00); // Class INternet
  127. UDPPut(0x01);
  128. UDPPut(0x00); // Time to Live 10 seconds
  129. UDPPut(0x00);
  130. UDPPut(0x00);
  131. UDPPut(0x0A);
  132. UDPPut(0x00); // Data Length 4 bytes
  133. UDPPut(0x04);
  134. UDPPutArray((BYTE*)&AppConfig.MyIPAddr.Val, 4); // Our IP address
  135. #warning tim had added extra code here
  136. UDPFlush();
  137. }
  138. /*****************************************************************************
  139. Function:
  140. static void DNSCopyRXNameToTX(void)
  141. Summary:
  142. Copies a DNS hostname, possibly including name compression, from the RX
  143. packet to the TX packet (without name compression in TX case).
  144. Description:
  145. None
  146. Precondition:
  147. RX pointer is set to currently point to the DNS name to copy
  148. Parameters:
  149. None
  150. Returns:
  151. None
  152. ***************************************************************************/
  153. static void DNSCopyRXNameToTX(void)
  154. {
  155. WORD w;
  156. BYTE i;
  157. BYTE len;
  158. while(1)
  159. {
  160. // Get first byte which will tell us if this is a 16-bit pointer or the
  161. // length of the first of a series of labels
  162. if(!UDPGet(&i))
  163. return;
  164. // Check if this is a pointer, if so, get the reminaing 8 bits and seek to the pointer value
  165. if((i & 0xC0u) == 0xC0u)
  166. {
  167. ((BYTE*)&w)[1] = i & 0x3F;
  168. UDPGet((BYTE*)&w);
  169. IPSetRxBuffer(sizeof(UDP_HEADER) + w);
  170. continue;
  171. }
  172. // Write the length byte
  173. len = i;
  174. UDPPut(len);
  175. // Exit if we've reached a zero length label
  176. if(len == 0u)
  177. return;
  178. // Copy all of the bytes in this label
  179. while(len--)
  180. {
  181. UDPGet(&i);
  182. UDPPut(i);
  183. }
  184. }
  185. }
  186. #endif //#if defined(STACK_USE_DNS_SERVER)