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@32:679654f2d023, 2017-08-17 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Aug 17 14:21:02 2017 +0000
- Revision:
- 32:679654f2d023
- Parent:
- 22:914b970356f0
- Child:
- 33:714a0345e59b
Corrected issues with .local in llmnr and mdns
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 | 32:679654f2d023 | 20 | bool mdnsUnicastReply; |
andrewboyson | 32:679654f2d023 | 21 | int nodeNameType; |
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 | nodeNameType = DNS_RECORD_NONE; |
andrewboyson | 32:679654f2d023 | 38 | mdnsUnicastReply = false; |
andrewboyson | 13:9cd54f7db57a | 39 | |
andrewboyson | 13:9cd54f7db57a | 40 | //Get the questions |
andrewboyson | 13:9cd54f7db57a | 41 | DnsHdrAncount = 0; |
andrewboyson | 13:9cd54f7db57a | 42 | for (int q = 0; q < DnsHdrQdcount; q++) |
andrewboyson | 13:9cd54f7db57a | 43 | { |
andrewboyson | 13:9cd54f7db57a | 44 | iEncodedName = DnsNameIndexFromPointer(p); |
andrewboyson | 13:9cd54f7db57a | 45 | int nameLength = DnsNameLength(p); |
andrewboyson | 13:9cd54f7db57a | 46 | if (!nameLength) return DO_NOTHING; |
andrewboyson | 32:679654f2d023 | 47 | if (!q) |
andrewboyson | 32:679654f2d023 | 48 | { |
andrewboyson | 32:679654f2d023 | 49 | if (!nodeNameType && DnsNameCompare (p, fullname) ) nodeNameType = DNS_RECORD_PTR; //rtc.local |
andrewboyson | 32:679654f2d023 | 50 | if (!nodeNameType && DnsNameCompareIp4(p, DhcpLocalIp) ) nodeNameType = DNS_RECORD_A; //3.1.168.192.inaddr.arpa |
andrewboyson | 32:679654f2d023 | 51 | if (!nodeNameType && DnsNameCompareIp6(p, SlaacLinkLocalIp)) nodeNameType = 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 | 52 | } |
andrewboyson | 32:679654f2d023 | 53 | p += nameLength; //Skip past the name |
andrewboyson | 32:679654f2d023 | 54 | p++ ; //skip the first byte of the type |
andrewboyson | 32:679654f2d023 | 55 | char recordType = *p++; //read the record type |
andrewboyson | 32:679654f2d023 | 56 | if (*p++ & 0x80) mdnsUnicastReply = true; //Check the 15th bit (UNICAST-RESPONSE) |
andrewboyson | 32:679654f2d023 | 57 | p += 1; //skip the class |
andrewboyson | 13:9cd54f7db57a | 58 | |
andrewboyson | 32:679654f2d023 | 59 | questionTypes[q] = recordType; |
andrewboyson | 13:9cd54f7db57a | 60 | |
andrewboyson | 13:9cd54f7db57a | 61 | if (DEBUG) |
andrewboyson | 13:9cd54f7db57a | 62 | { |
andrewboyson | 13:9cd54f7db57a | 63 | switch (recordType) |
andrewboyson | 13:9cd54f7db57a | 64 | { |
andrewboyson | 13:9cd54f7db57a | 65 | case DNS_RECORD_A: LogF(" for IP4 address\r\n"); break; |
andrewboyson | 13:9cd54f7db57a | 66 | case DNS_RECORD_PTR: LogF(" for name\r\n"); break; |
andrewboyson | 13:9cd54f7db57a | 67 | case DNS_RECORD_AAAA: LogF(" for IP6 address\r\n"); break; |
andrewboyson | 13:9cd54f7db57a | 68 | default: LogF(" for unrecognised record type %d\r\n", recordType); break; |
andrewboyson | 13:9cd54f7db57a | 69 | } |
andrewboyson | 13:9cd54f7db57a | 70 | char text[256]; |
andrewboyson | 13:9cd54f7db57a | 71 | DnsNameDecode(iEncodedName, sizeof(text), text); |
andrewboyson | 13:9cd54f7db57a | 72 | LogF(" Name %s\r\n", text); |
andrewboyson | 13:9cd54f7db57a | 73 | } |
andrewboyson | 13:9cd54f7db57a | 74 | } |
andrewboyson | 32:679654f2d023 | 75 | return 0; |
andrewboyson | 32:679654f2d023 | 76 | } |
andrewboyson | 32:679654f2d023 | 77 | static int addAnswers(int dnsProtocol) |
andrewboyson | 32:679654f2d023 | 78 | { |
andrewboyson | 13:9cd54f7db57a | 79 | for (int q = 0; q < DnsHdrQdcount; q++) |
andrewboyson | 13:9cd54f7db57a | 80 | { |
andrewboyson | 32:679654f2d023 | 81 | //Skip unwanted record types |
andrewboyson | 32:679654f2d023 | 82 | switch (questionTypes[q]) |
andrewboyson | 13:9cd54f7db57a | 83 | { |
andrewboyson | 13:9cd54f7db57a | 84 | case DNS_RECORD_A: |
andrewboyson | 32:679654f2d023 | 85 | case DNS_RECORD_AAAA: |
andrewboyson | 32:679654f2d023 | 86 | case DNS_RECORD_PTR: continue; |
andrewboyson | 32:679654f2d023 | 87 | } |
andrewboyson | 32:679654f2d023 | 88 | if (p - DnsHdrPacket > 500) |
andrewboyson | 32:679654f2d023 | 89 | { |
andrewboyson | 32:679654f2d023 | 90 | LogTimeF("DnsServer-addAnswers Ip4 query reply is getting too big\r\n"); |
andrewboyson | 32:679654f2d023 | 91 | return -1; |
andrewboyson | 32:679654f2d023 | 92 | } |
andrewboyson | 32:679654f2d023 | 93 | DnsHdrAncount++; |
andrewboyson | 32:679654f2d023 | 94 | int lenPayload = 0; |
andrewboyson | 32:679654f2d023 | 95 | char* pPayload = 0; |
andrewboyson | 32:679654f2d023 | 96 | switch (questionTypes[q]) |
andrewboyson | 32:679654f2d023 | 97 | { |
andrewboyson | 32:679654f2d023 | 98 | case DNS_RECORD_A: |
andrewboyson | 32:679654f2d023 | 99 | DnsNameEncode(fullname, &p); |
andrewboyson | 32:679654f2d023 | 100 | lenPayload = 4; |
andrewboyson | 32:679654f2d023 | 101 | pPayload = (char*)&DhcpLocalIp; |
andrewboyson | 13:9cd54f7db57a | 102 | break; |
andrewboyson | 13:9cd54f7db57a | 103 | |
andrewboyson | 13:9cd54f7db57a | 104 | case DNS_RECORD_AAAA: |
andrewboyson | 32:679654f2d023 | 105 | DnsNameEncode(fullname, &p); |
andrewboyson | 32:679654f2d023 | 106 | lenPayload = 16; |
andrewboyson | 32:679654f2d023 | 107 | pPayload = SlaacLinkLocalIp; |
andrewboyson | 32:679654f2d023 | 108 | break; |
andrewboyson | 13:9cd54f7db57a | 109 | |
andrewboyson | 32:679654f2d023 | 110 | case DNS_RECORD_PTR: |
andrewboyson | 32:679654f2d023 | 111 | if (nodeNameType == DNS_RECORD_A ) DnsNameEncodeIp4(DhcpLocalIp, &p); |
andrewboyson | 32:679654f2d023 | 112 | if (nodeNameType == DNS_RECORD_AAAA) DnsNameEncodeIp6(SlaacLinkLocalIp, &p); |
andrewboyson | 32:679654f2d023 | 113 | lenPayload = fullnamelength; |
andrewboyson | 32:679654f2d023 | 114 | pPayload = fullname; |
andrewboyson | 13:9cd54f7db57a | 115 | break; |
andrewboyson | 13:9cd54f7db57a | 116 | } |
andrewboyson | 32:679654f2d023 | 117 | char mdns = dnsProtocol == DNS_PROTOCOL_MDNS ? 0x80 : 0; //Set the 15th bit (CACHE_FLUSH) of the class to 1 if MDNS |
andrewboyson | 32:679654f2d023 | 118 | *p++ = 0; *p++ = questionTypes[q]; //16 bit Type |
andrewboyson | 32:679654f2d023 | 119 | *p++ = mdns; *p++ = 1; //16 bit Class LSB QCLASS_IN = 1 - internet |
andrewboyson | 32:679654f2d023 | 120 | *p++ = 0; *p++ = 0; *p++ = 4; *p++ = 0; //32 bit TTL seconds - 1024 |
andrewboyson | 32:679654f2d023 | 121 | *p++ = 0; *p++ = lenPayload; //16 bit length in bytes |
andrewboyson | 32:679654f2d023 | 122 | memcpy(p, pPayload, lenPayload); //Copy the payload |
andrewboyson | 32:679654f2d023 | 123 | p += lenPayload; //Adjust the pointer to the next character afetr the payload |
andrewboyson | 32:679654f2d023 | 124 | |
andrewboyson | 13:9cd54f7db57a | 125 | } |
andrewboyson | 32:679654f2d023 | 126 | return 0; |
andrewboyson | 32:679654f2d023 | 127 | } |
andrewboyson | 32:679654f2d023 | 128 | |
andrewboyson | 32:679654f2d023 | 129 | int DnsServerHandleQuery(int dnsProtocol, int *pSize) //Received an mdns or llmnr query on port 5353 or 5355 |
andrewboyson | 32:679654f2d023 | 130 | { |
andrewboyson | 32:679654f2d023 | 131 | if (DEBUG) DnsHdrLog("DnsServer received query", dnsProtocol); |
andrewboyson | 32:679654f2d023 | 132 | |
andrewboyson | 32:679654f2d023 | 133 | if (initialise(dnsProtocol, *pSize)) return DO_NOTHING; |
andrewboyson | 32:679654f2d023 | 134 | if (readQuestions() ) return DO_NOTHING; |
andrewboyson | 32:679654f2d023 | 135 | if (!nodeNameType) return DO_NOTHING; |
andrewboyson | 32:679654f2d023 | 136 | if (addAnswers(dnsProtocol) ) return DO_NOTHING; |
andrewboyson | 32:679654f2d023 | 137 | if (!DnsHdrAncount) return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 138 | |
andrewboyson | 13:9cd54f7db57a | 139 | DnsHdrIsReply = true; |
andrewboyson | 13:9cd54f7db57a | 140 | DnsHdrIsAuthoritative = true; |
andrewboyson | 13:9cd54f7db57a | 141 | DnsHdrWrite(); |
andrewboyson | 13:9cd54f7db57a | 142 | |
andrewboyson | 13:9cd54f7db57a | 143 | *pSize = p - DnsHdrPacket; |
andrewboyson | 13:9cd54f7db57a | 144 | |
andrewboyson | 32:679654f2d023 | 145 | if (DEBUG) DnsHdrLog("DnsServer sending reply", dnsProtocol); |
andrewboyson | 13:9cd54f7db57a | 146 | |
andrewboyson | 13:9cd54f7db57a | 147 | if (dnsProtocol == DNS_PROTOCOL_MDNS && !mdnsUnicastReply) return MULTICAST_MDNS; |
andrewboyson | 13:9cd54f7db57a | 148 | return UNICAST; |
andrewboyson | 13:9cd54f7db57a | 149 | } |