Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
udp/dns/dnsserver.cpp@33:714a0345e59b, 2017-08-23 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Aug 23 18:02:42 2017 +0000
- Revision:
- 33:714a0345e59b
- Parent:
- 32:679654f2d023
- Child:
- 36:900e24b27bfb
Made DNS names case insensitive
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 13:9cd54f7db57a | 1 | #include "mbed.h" |
andrewboyson | 13:9cd54f7db57a | 2 | #include "dnshdr.h" |
andrewboyson | 13:9cd54f7db57a | 3 | #include "dnsname.h" |
andrewboyson | 13:9cd54f7db57a | 4 | #include "net.h" |
andrewboyson | 13:9cd54f7db57a | 5 | #include "dns.h" |
andrewboyson | 13:9cd54f7db57a | 6 | #include "log.h" |
andrewboyson | 13:9cd54f7db57a | 7 | #include "dhcp.h" |
andrewboyson | 13:9cd54f7db57a | 8 | #include "slaac.h" |
andrewboyson | 13:9cd54f7db57a | 9 | #include "io.h" |
andrewboyson | 13:9cd54f7db57a | 10 | |
andrewboyson | 13:9cd54f7db57a | 11 | #define DEBUG false |
andrewboyson | 13:9cd54f7db57a | 12 | |
andrewboyson | 32:679654f2d023 | 13 | //Set by 'initialise' |
andrewboyson | 32:679654f2d023 | 14 | char* p; //Position relative to DnsHdrData and is updated while both reading questions and writing answers |
andrewboyson | 32:679654f2d023 | 15 | char fullname[100]; //The name, adjusted to include the domain if needed by the protocol, used when reading and when writing |
andrewboyson | 32:679654f2d023 | 16 | int fullnamelength; |
andrewboyson | 32:679654f2d023 | 17 | |
andrewboyson | 32:679654f2d023 | 18 | //Set by readQuestions and used by answerQuestions |
andrewboyson | 32:679654f2d023 | 19 | int questionTypes[4]; |
andrewboyson | 33:714a0345e59b | 20 | int nodeNameTypes[4]; |
andrewboyson | 32:679654f2d023 | 21 | bool mdnsUnicastReply; |
andrewboyson | 32:679654f2d023 | 22 | |
andrewboyson | 32:679654f2d023 | 23 | static int initialise(int dnsProtocol, int size) |
andrewboyson | 13:9cd54f7db57a | 24 | { |
andrewboyson | 32:679654f2d023 | 25 | if ( size > 512) { if (DEBUG) LogTimeF("DnsServer-initialise length %d too long\r\n", size ); return -1; } |
andrewboyson | 32:679654f2d023 | 26 | if (DnsHdrQdcount > 4) { if (DEBUG) LogTimeF("DnsServer-initialise too many queries %d\r\n", DnsHdrQdcount); return -1; } |
andrewboyson | 13:9cd54f7db57a | 27 | |
andrewboyson | 32:679654f2d023 | 28 | p = DnsHdrData; |
andrewboyson | 32:679654f2d023 | 29 | |
andrewboyson | 32:679654f2d023 | 30 | fullnamelength = DnsMakeFullNameFromName(dnsProtocol, NetName, sizeof(fullname), fullname); |
andrewboyson | 13:9cd54f7db57a | 31 | |
andrewboyson | 32:679654f2d023 | 32 | return 0; |
andrewboyson | 32:679654f2d023 | 33 | } |
andrewboyson | 32:679654f2d023 | 34 | static int readQuestions() |
andrewboyson | 32:679654f2d023 | 35 | { |
andrewboyson | 13:9cd54f7db57a | 36 | char iEncodedName; |
andrewboyson | 32:679654f2d023 | 37 | mdnsUnicastReply = false; |
andrewboyson | 13:9cd54f7db57a | 38 | |
andrewboyson | 13:9cd54f7db57a | 39 | //Get the questions |
andrewboyson | 13:9cd54f7db57a | 40 | for (int q = 0; q < DnsHdrQdcount; q++) |
andrewboyson | 13:9cd54f7db57a | 41 | { |
andrewboyson | 13:9cd54f7db57a | 42 | iEncodedName = DnsNameIndexFromPointer(p); |
andrewboyson | 13:9cd54f7db57a | 43 | int nameLength = DnsNameLength(p); |
andrewboyson | 13:9cd54f7db57a | 44 | if (!nameLength) return DO_NOTHING; |
andrewboyson | 33:714a0345e59b | 45 | nodeNameTypes[q] = DNS_RECORD_NONE; |
andrewboyson | 33:714a0345e59b | 46 | if (!nodeNameTypes[q] && DnsNameCompare (p, fullname) ) nodeNameTypes[q] = DNS_RECORD_PTR; //rtc.local |
andrewboyson | 33:714a0345e59b | 47 | if (!nodeNameTypes[q] && DnsNameCompareIp4(p, DhcpLocalIp) ) nodeNameTypes[q] = DNS_RECORD_A; //3.1.168.192.inaddr.arpa |
andrewboyson | 33:714a0345e59b | 48 | if (!nodeNameTypes[q] && DnsNameCompareIp6(p, SlaacLinkLocalIp)) nodeNameTypes[q] = DNS_RECORD_AAAA; //5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 32:679654f2d023 | 49 | p += nameLength; //Skip past the name |
andrewboyson | 32:679654f2d023 | 50 | p++ ; //skip the first byte of the type |
andrewboyson | 32:679654f2d023 | 51 | char recordType = *p++; //read the record type |
andrewboyson | 32:679654f2d023 | 52 | if (*p++ & 0x80) mdnsUnicastReply = true; //Check the 15th bit (UNICAST-RESPONSE) |
andrewboyson | 32:679654f2d023 | 53 | p += 1; //skip the class |
andrewboyson | 13:9cd54f7db57a | 54 | |
andrewboyson | 32:679654f2d023 | 55 | questionTypes[q] = recordType; |
andrewboyson | 13:9cd54f7db57a | 56 | |
andrewboyson | 13:9cd54f7db57a | 57 | if (DEBUG) |
andrewboyson | 13:9cd54f7db57a | 58 | { |
andrewboyson | 13:9cd54f7db57a | 59 | switch (recordType) |
andrewboyson | 13:9cd54f7db57a | 60 | { |
andrewboyson | 13:9cd54f7db57a | 61 | case DNS_RECORD_A: LogF(" for IP4 address\r\n"); break; |
andrewboyson | 13:9cd54f7db57a | 62 | case DNS_RECORD_PTR: LogF(" for name\r\n"); break; |
andrewboyson | 13:9cd54f7db57a | 63 | case DNS_RECORD_AAAA: LogF(" for IP6 address\r\n"); break; |
andrewboyson | 13:9cd54f7db57a | 64 | default: LogF(" for unrecognised record type %d\r\n", recordType); break; |
andrewboyson | 13:9cd54f7db57a | 65 | } |
andrewboyson | 13:9cd54f7db57a | 66 | char text[256]; |
andrewboyson | 13:9cd54f7db57a | 67 | DnsNameDecode(iEncodedName, sizeof(text), text); |
andrewboyson | 13:9cd54f7db57a | 68 | LogF(" Name %s\r\n", text); |
andrewboyson | 13:9cd54f7db57a | 69 | } |
andrewboyson | 13:9cd54f7db57a | 70 | } |
andrewboyson | 32:679654f2d023 | 71 | return 0; |
andrewboyson | 32:679654f2d023 | 72 | } |
andrewboyson | 32:679654f2d023 | 73 | static int addAnswers(int dnsProtocol) |
andrewboyson | 32:679654f2d023 | 74 | { |
andrewboyson | 33:714a0345e59b | 75 | DnsHdrAncount = 0; |
andrewboyson | 13:9cd54f7db57a | 76 | for (int q = 0; q < DnsHdrQdcount; q++) |
andrewboyson | 13:9cd54f7db57a | 77 | { |
andrewboyson | 32:679654f2d023 | 78 | //Skip unwanted record types |
andrewboyson | 32:679654f2d023 | 79 | switch (questionTypes[q]) |
andrewboyson | 13:9cd54f7db57a | 80 | { |
andrewboyson | 13:9cd54f7db57a | 81 | case DNS_RECORD_A: |
andrewboyson | 32:679654f2d023 | 82 | case DNS_RECORD_AAAA: |
andrewboyson | 33:714a0345e59b | 83 | case DNS_RECORD_PTR: break; |
andrewboyson | 33:714a0345e59b | 84 | default: continue; |
andrewboyson | 32:679654f2d023 | 85 | } |
andrewboyson | 33:714a0345e59b | 86 | if (!nodeNameTypes[q]) continue; //Skip queries which are not addressed to me |
andrewboyson | 32:679654f2d023 | 87 | if (p - DnsHdrPacket > 500) |
andrewboyson | 32:679654f2d023 | 88 | { |
andrewboyson | 32:679654f2d023 | 89 | LogTimeF("DnsServer-addAnswers Ip4 query reply is getting too big\r\n"); |
andrewboyson | 32:679654f2d023 | 90 | return -1; |
andrewboyson | 32:679654f2d023 | 91 | } |
andrewboyson | 32:679654f2d023 | 92 | DnsHdrAncount++; |
andrewboyson | 32:679654f2d023 | 93 | int lenPayload = 0; |
andrewboyson | 32:679654f2d023 | 94 | char* pPayload = 0; |
andrewboyson | 32:679654f2d023 | 95 | switch (questionTypes[q]) |
andrewboyson | 32:679654f2d023 | 96 | { |
andrewboyson | 32:679654f2d023 | 97 | case DNS_RECORD_A: |
andrewboyson | 32:679654f2d023 | 98 | DnsNameEncode(fullname, &p); |
andrewboyson | 32:679654f2d023 | 99 | lenPayload = 4; |
andrewboyson | 32:679654f2d023 | 100 | pPayload = (char*)&DhcpLocalIp; |
andrewboyson | 13:9cd54f7db57a | 101 | break; |
andrewboyson | 13:9cd54f7db57a | 102 | |
andrewboyson | 13:9cd54f7db57a | 103 | case DNS_RECORD_AAAA: |
andrewboyson | 32:679654f2d023 | 104 | DnsNameEncode(fullname, &p); |
andrewboyson | 32:679654f2d023 | 105 | lenPayload = 16; |
andrewboyson | 32:679654f2d023 | 106 | pPayload = SlaacLinkLocalIp; |
andrewboyson | 32:679654f2d023 | 107 | break; |
andrewboyson | 13:9cd54f7db57a | 108 | |
andrewboyson | 32:679654f2d023 | 109 | case DNS_RECORD_PTR: |
andrewboyson | 33:714a0345e59b | 110 | if (nodeNameTypes[q] == DNS_RECORD_A ) DnsNameEncodeIp4(DhcpLocalIp, &p); |
andrewboyson | 33:714a0345e59b | 111 | if (nodeNameTypes[q] == DNS_RECORD_AAAA) DnsNameEncodeIp6(SlaacLinkLocalIp, &p); |
andrewboyson | 32:679654f2d023 | 112 | lenPayload = fullnamelength; |
andrewboyson | 32:679654f2d023 | 113 | pPayload = fullname; |
andrewboyson | 13:9cd54f7db57a | 114 | break; |
andrewboyson | 13:9cd54f7db57a | 115 | } |
andrewboyson | 32:679654f2d023 | 116 | char mdns = dnsProtocol == DNS_PROTOCOL_MDNS ? 0x80 : 0; //Set the 15th bit (CACHE_FLUSH) of the class to 1 if MDNS |
andrewboyson | 32:679654f2d023 | 117 | *p++ = 0; *p++ = questionTypes[q]; //16 bit Type |
andrewboyson | 32:679654f2d023 | 118 | *p++ = mdns; *p++ = 1; //16 bit Class LSB QCLASS_IN = 1 - internet |
andrewboyson | 32:679654f2d023 | 119 | *p++ = 0; *p++ = 0; *p++ = 4; *p++ = 0; //32 bit TTL seconds - 1024 |
andrewboyson | 32:679654f2d023 | 120 | *p++ = 0; *p++ = lenPayload; //16 bit length in bytes |
andrewboyson | 32:679654f2d023 | 121 | memcpy(p, pPayload, lenPayload); //Copy the payload |
andrewboyson | 32:679654f2d023 | 122 | p += lenPayload; //Adjust the pointer to the next character afetr the payload |
andrewboyson | 32:679654f2d023 | 123 | |
andrewboyson | 13:9cd54f7db57a | 124 | } |
andrewboyson | 32:679654f2d023 | 125 | return 0; |
andrewboyson | 32:679654f2d023 | 126 | } |
andrewboyson | 32:679654f2d023 | 127 | |
andrewboyson | 32:679654f2d023 | 128 | int DnsServerHandleQuery(int dnsProtocol, int *pSize) //Received an mdns or llmnr query on port 5353 or 5355 |
andrewboyson | 32:679654f2d023 | 129 | { |
andrewboyson | 32:679654f2d023 | 130 | if (DEBUG) DnsHdrLog("DnsServer received query", dnsProtocol); |
andrewboyson | 32:679654f2d023 | 131 | |
andrewboyson | 32:679654f2d023 | 132 | if (initialise(dnsProtocol, *pSize)) return DO_NOTHING; |
andrewboyson | 32:679654f2d023 | 133 | if (readQuestions() ) return DO_NOTHING; |
andrewboyson | 32:679654f2d023 | 134 | if (addAnswers(dnsProtocol) ) return DO_NOTHING; |
andrewboyson | 32:679654f2d023 | 135 | if (!DnsHdrAncount) return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 136 | |
andrewboyson | 13:9cd54f7db57a | 137 | DnsHdrIsReply = true; |
andrewboyson | 13:9cd54f7db57a | 138 | DnsHdrIsAuthoritative = true; |
andrewboyson | 13:9cd54f7db57a | 139 | DnsHdrWrite(); |
andrewboyson | 13:9cd54f7db57a | 140 | |
andrewboyson | 13:9cd54f7db57a | 141 | *pSize = p - DnsHdrPacket; |
andrewboyson | 13:9cd54f7db57a | 142 | |
andrewboyson | 32:679654f2d023 | 143 | if (DEBUG) DnsHdrLog("DnsServer sending reply", dnsProtocol); |
andrewboyson | 13:9cd54f7db57a | 144 | |
andrewboyson | 13:9cd54f7db57a | 145 | if (dnsProtocol == DNS_PROTOCOL_MDNS && !mdnsUnicastReply) return MULTICAST_MDNS; |
andrewboyson | 13:9cd54f7db57a | 146 | return UNICAST; |
andrewboyson | 13:9cd54f7db57a | 147 | } |