| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /********************************************************************* |
| 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 | |||
| 56 | #include "TCPIPConfig.h" |
||
| 57 | |||
| 58 | #if defined(STACK_USE_DNS_SERVER) |
||
| 59 | |||
| 60 | #include "TCPIP Stack/TCPIP.h" |
||
| 61 | |||
| 62 | // Default port for the DNS server to listen on |
||
| 63 | #define DNS_PORT 53u |
||
| 64 | |||
| 65 | |||
| 66 | static void DNSCopyRXNameToTX(void); |
||
| 67 | |||
| 68 | /********************************************************************* |
||
| 69 | * Function: void DNSServerTask(void) |
||
| 70 | * |
||
| 71 | * PreCondition: None |
||
| 72 | * |
||
| 73 | * Input: None |
||
| 74 | * |
||
| 75 | * Output: None |
||
| 76 | * |
||
| 77 | * Side Effects: None |
||
| 78 | * |
||
| 79 | * Overview: Sends dummy responses that point to ourself for DNS requests |
||
| 80 | * |
||
| 81 | * Note: None |
||
| 82 | ********************************************************************/ |
||
| 83 | void DNSServerTask(void) |
||
| 84 | { |
||
| 85 | static UDP_SOCKET MySocket = INVALID_UDP_SOCKET; |
||
| 86 | struct |
||
| 87 | { |
||
| 88 | WORD wTransactionID; |
||
| 89 | WORD wFlags; |
||
| 90 | WORD wQuestions; |
||
| 91 | WORD wAnswerRRs; |
||
| 92 | WORD wAuthorityRRs; |
||
| 93 | WORD wAdditionalRRs; |
||
| 94 | } DNSHeader; |
||
| 95 | |||
| 96 | |||
| 97 | // Create a socket to listen on if this is the first time calling this function |
||
| 98 | if(MySocket == INVALID_UDP_SOCKET) |
||
| 99 | { |
||
| 100 | MySocket = UDPOpen(DNS_PORT, NULL, 0); |
||
| 101 | return; |
||
| 102 | } |
||
| 103 | |||
| 104 | // See if a DNS query packet has arrived |
||
| 105 | if(UDPIsGetReady(MySocket) < sizeof(DNSHeader)) |
||
| 106 | return; |
||
| 107 | |||
| 108 | // Read DNS header |
||
| 109 | UDPGetArray((BYTE*)&DNSHeader, sizeof(DNSHeader)); |
||
| 110 | |||
| 111 | // Ignore this packet if it isn't a query |
||
| 112 | if((DNSHeader.wFlags & 0x8000) == 0x8000u) |
||
| 113 | return; |
||
| 114 | |||
| 115 | // Ignore this packet if there are no questions in it |
||
| 116 | if(DNSHeader.wQuestions == 0u) |
||
| 117 | return; |
||
| 118 | |||
| 119 | // Block until we can transmit a DNS response packet |
||
| 120 | while(!UDPIsPutReady(MySocket)); |
||
| 121 | |||
| 122 | // Write DNS response packet |
||
| 123 | UDPPutArray((BYTE*)&DNSHeader.wTransactionID, 2); // 2 byte Transaction ID |
||
| 124 | if(DNSHeader.wFlags & 0x0100) |
||
| 125 | UDPPut(0x81); // Message is a response with recursion desired |
||
| 126 | else |
||
| 127 | UDPPut(0x80); // Message is a response without recursion desired flag set |
||
| 128 | UDPPut(0x80); // Recursion available |
||
| 129 | UDPPut(0x00); // 0x0000 Questions |
||
| 130 | UDPPut(0x00); |
||
| 131 | UDPPut(0x00); // 0x0001 Answers RRs |
||
| 132 | UDPPut(0x01); |
||
| 133 | UDPPut(0x00); // 0x0000 Authority RRs |
||
| 134 | UDPPut(0x00); |
||
| 135 | UDPPut(0x00); // 0x0000 Additional RRs |
||
| 136 | UDPPut(0x00); |
||
| 137 | DNSCopyRXNameToTX(); // Copy hostname of first question over to TX packet |
||
| 138 | UDPPut(0x00); // Type A Host address |
||
| 139 | UDPPut(0x01); |
||
| 140 | UDPPut(0x00); // Class INternet |
||
| 141 | UDPPut(0x01); |
||
| 142 | UDPPut(0x00); // Time to Live 10 seconds |
||
| 143 | UDPPut(0x00); |
||
| 144 | UDPPut(0x00); |
||
| 145 | UDPPut(0x0A); |
||
| 146 | UDPPut(0x00); // Data Length 4 bytes |
||
| 147 | UDPPut(0x04); |
||
| 148 | UDPPutArray((BYTE*)&AppConfig.MyIPAddr.Val, 4); // Our IP address |
||
| 149 | |||
| 150 | UDPFlush(); |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | /***************************************************************************** |
||
| 156 | Function: |
||
| 157 | static void DNSCopyRXNameToTX(void) |
||
| 158 | |||
| 159 | Summary: |
||
| 160 | Copies a DNS hostname, possibly including name compression, from the RX |
||
| 161 | packet to the TX packet (without name compression in TX case). |
||
| 162 | |||
| 163 | Description: |
||
| 164 | None |
||
| 165 | |||
| 166 | Precondition: |
||
| 167 | RX pointer is set to currently point to the DNS name to copy |
||
| 168 | |||
| 169 | Parameters: |
||
| 170 | None |
||
| 171 | |||
| 172 | Returns: |
||
| 173 | None |
||
| 174 | ***************************************************************************/ |
||
| 175 | static void DNSCopyRXNameToTX(void) |
||
| 176 | { |
||
| 177 | WORD w; |
||
| 178 | BYTE i; |
||
| 179 | BYTE len; |
||
| 180 | |||
| 181 | while(1) |
||
| 182 | { |
||
| 183 | // Get first byte which will tell us if this is a 16-bit pointer or the |
||
| 184 | // length of the first of a series of labels |
||
| 185 | if(!UDPGet(&i)) |
||
| 186 | return; |
||
| 187 | |||
| 188 | // Check if this is a pointer, if so, get the reminaing 8 bits and seek to the pointer value |
||
| 189 | if((i & 0xC0u) == 0xC0u) |
||
| 190 | { |
||
| 191 | ((BYTE*)&w)[1] = i & 0x3F; |
||
| 192 | UDPGet((BYTE*)&w); |
||
| 193 | IPSetRxBuffer(sizeof(UDP_HEADER) + w); |
||
| 194 | continue; |
||
| 195 | } |
||
| 196 | |||
| 197 | // Write the length byte |
||
| 198 | len = i; |
||
| 199 | UDPPut(len); |
||
| 200 | |||
| 201 | // Exit if we've reached a zero length label |
||
| 202 | if(len == 0u) |
||
| 203 | return; |
||
| 204 | |||
| 205 | // Copy all of the bytes in this label |
||
| 206 | while(len--) |
||
| 207 | { |
||
| 208 | UDPGet(&i); |
||
| 209 | UDPPut(i); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | #endif //#if defined(STACK_USE_DNS_SERVER) |
Powered by WebSVN v2.8.3