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@57:e0fb648acf48, 2017-11-28 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Nov 28 17:05:46 2017 +0000
- Revision:
- 57:e0fb648acf48
- Parent:
- 44:83ce5ace337b
- Child:
- 59:e0e556c8bd46
Added TFTP
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 | 37:793b39683406 | 5 | #include "action.h" |
andrewboyson | 13:9cd54f7db57a | 6 | #include "dns.h" |
andrewboyson | 13:9cd54f7db57a | 7 | #include "log.h" |
andrewboyson | 13:9cd54f7db57a | 8 | #include "dhcp.h" |
andrewboyson | 13:9cd54f7db57a | 9 | #include "slaac.h" |
andrewboyson | 13:9cd54f7db57a | 10 | #include "io.h" |
andrewboyson | 37:793b39683406 | 11 | #include "ip4.h" |
andrewboyson | 37:793b39683406 | 12 | #include "ip6.h" |
andrewboyson | 13:9cd54f7db57a | 13 | |
andrewboyson | 37:793b39683406 | 14 | bool DnsServerTrace = false; |
andrewboyson | 37:793b39683406 | 15 | |
andrewboyson | 44:83ce5ace337b | 16 | #define RECORD_NONE 0 |
andrewboyson | 44:83ce5ace337b | 17 | #define RECORD_PTR4 1 |
andrewboyson | 44:83ce5ace337b | 18 | #define RECORD_PTR6_LOCAL 2 |
andrewboyson | 44:83ce5ace337b | 19 | #define RECORD_PTR6_GLOBAL 3 |
andrewboyson | 44:83ce5ace337b | 20 | #define RECORD_A 4 |
andrewboyson | 44:83ce5ace337b | 21 | #define RECORD_AAAA_LOCAL 5 |
andrewboyson | 44:83ce5ace337b | 22 | #define RECORD_AAAA_GLOBAL 6 |
andrewboyson | 44:83ce5ace337b | 23 | |
andrewboyson | 44:83ce5ace337b | 24 | #define MAX_ANSWERS 4 |
andrewboyson | 13:9cd54f7db57a | 25 | |
andrewboyson | 32:679654f2d023 | 26 | //Set by 'initialise' |
andrewboyson | 36:900e24b27bfb | 27 | static char* p; //Position relative to DnsHdrData and is updated while both reading questions and writing answers |
andrewboyson | 44:83ce5ace337b | 28 | static char myFullName4[100]; //The name, adjusted to include the domain if needed by the protocol, used when reading and when writing |
andrewboyson | 44:83ce5ace337b | 29 | static int myFullName4Length; |
andrewboyson | 44:83ce5ace337b | 30 | static char myFullName6[100]; //The name, adjusted to include the domain if needed by the protocol, used when reading and when writing |
andrewboyson | 44:83ce5ace337b | 31 | static int myFullName6Length; |
andrewboyson | 32:679654f2d023 | 32 | |
andrewboyson | 32:679654f2d023 | 33 | //Set by readQuestions and used by answerQuestions |
andrewboyson | 44:83ce5ace337b | 34 | static int answers[MAX_ANSWERS]; |
andrewboyson | 44:83ce5ace337b | 35 | static int answerCount = 0; |
andrewboyson | 36:900e24b27bfb | 36 | static bool mdnsUnicastReply; |
andrewboyson | 32:679654f2d023 | 37 | |
andrewboyson | 32:679654f2d023 | 38 | static int readQuestions() |
andrewboyson | 32:679654f2d023 | 39 | { |
andrewboyson | 37:793b39683406 | 40 | p = DnsHdrData; |
andrewboyson | 32:679654f2d023 | 41 | mdnsUnicastReply = false; |
andrewboyson | 13:9cd54f7db57a | 42 | |
andrewboyson | 13:9cd54f7db57a | 43 | //Get the questions |
andrewboyson | 44:83ce5ace337b | 44 | answerCount = 0; |
andrewboyson | 44:83ce5ace337b | 45 | for (int i = 0; i < DnsHdrQdcount; i++) |
andrewboyson | 13:9cd54f7db57a | 46 | { |
andrewboyson | 44:83ce5ace337b | 47 | //Bomb out if there are too many answers |
andrewboyson | 44:83ce5ace337b | 48 | if (answerCount >= MAX_ANSWERS) |
andrewboyson | 37:793b39683406 | 49 | { |
andrewboyson | 44:83ce5ace337b | 50 | if (DnsServerTrace) LogTimeF("DnsServer-readQuestions - exceeded %d answers\r\n", MAX_ANSWERS); |
andrewboyson | 37:793b39683406 | 51 | break; |
andrewboyson | 37:793b39683406 | 52 | } |
andrewboyson | 37:793b39683406 | 53 | |
andrewboyson | 37:793b39683406 | 54 | //Bomb out if we are tending to overrun the buffer |
andrewboyson | 37:793b39683406 | 55 | if (p - DnsHdrPacket > 512) |
andrewboyson | 37:793b39683406 | 56 | { |
andrewboyson | 37:793b39683406 | 57 | if (DnsServerTrace) LogTimeF("DnsServer-readQuestions - overrunning the buffer\r\n"); |
andrewboyson | 37:793b39683406 | 58 | return -1; |
andrewboyson | 37:793b39683406 | 59 | } |
andrewboyson | 37:793b39683406 | 60 | |
andrewboyson | 37:793b39683406 | 61 | //Node name |
andrewboyson | 13:9cd54f7db57a | 62 | int nameLength = DnsNameLength(p); |
andrewboyson | 37:793b39683406 | 63 | if (!nameLength) |
andrewboyson | 37:793b39683406 | 64 | { |
andrewboyson | 37:793b39683406 | 65 | if (DnsServerTrace) LogTimeF("DnsServer-readQuestions namelength is zero\r\n"); |
andrewboyson | 37:793b39683406 | 66 | return -1; |
andrewboyson | 37:793b39683406 | 67 | } |
andrewboyson | 44:83ce5ace337b | 68 | bool nodeIsName4 = DnsNameComparePtr(p, myFullName4); |
andrewboyson | 44:83ce5ace337b | 69 | bool nodeIsName6 = DnsNameComparePtr(p, myFullName6); |
andrewboyson | 44:83ce5ace337b | 70 | bool nodeIsAddr4 = DnsNameCompareIp4(p, DhcpLocalIp); |
andrewboyson | 44:83ce5ace337b | 71 | bool nodeIsLocl6 = DnsNameCompareIp6(p, SlaacLinkLocalIp); |
andrewboyson | 44:83ce5ace337b | 72 | bool nodeIsGlob6 = DnsNameCompareIp6(p, SlaacGlobalIp); |
andrewboyson | 44:83ce5ace337b | 73 | p += nameLength; //Skip past the name |
andrewboyson | 44:83ce5ace337b | 74 | |
andrewboyson | 37:793b39683406 | 75 | //Type |
andrewboyson | 44:83ce5ace337b | 76 | p++ ; //skip the first byte of the type |
andrewboyson | 44:83ce5ace337b | 77 | char recordType = *p++; //read the record type |
andrewboyson | 37:793b39683406 | 78 | |
andrewboyson | 37:793b39683406 | 79 | //Class |
andrewboyson | 44:83ce5ace337b | 80 | if (*p++ & 0x80) mdnsUnicastReply = true; //check the class 15th bit (UNICAST-RESPONSE) |
andrewboyson | 44:83ce5ace337b | 81 | p++; //skip the class |
andrewboyson | 44:83ce5ace337b | 82 | |
andrewboyson | 44:83ce5ace337b | 83 | //Handle the questions |
andrewboyson | 44:83ce5ace337b | 84 | if (nodeIsName4 && recordType == DNS_RECORD_A ) answers[answerCount++] = RECORD_A; |
andrewboyson | 44:83ce5ace337b | 85 | if (nodeIsName6 && recordType == DNS_RECORD_AAAA) { answers[answerCount++] = RECORD_AAAA_LOCAL; answers[answerCount++] = RECORD_AAAA_GLOBAL; } |
andrewboyson | 44:83ce5ace337b | 86 | if (nodeIsAddr4 && recordType == DNS_RECORD_PTR ) answers[answerCount++] = RECORD_PTR4; |
andrewboyson | 44:83ce5ace337b | 87 | if (nodeIsLocl6 && recordType == DNS_RECORD_PTR ) answers[answerCount++] = RECORD_PTR6_LOCAL; |
andrewboyson | 44:83ce5ace337b | 88 | if (nodeIsGlob6 && recordType == DNS_RECORD_PTR ) answers[answerCount++] = RECORD_PTR6_GLOBAL; |
andrewboyson | 13:9cd54f7db57a | 89 | } |
andrewboyson | 32:679654f2d023 | 90 | return 0; |
andrewboyson | 32:679654f2d023 | 91 | } |
andrewboyson | 32:679654f2d023 | 92 | static int addAnswers(int dnsProtocol) |
andrewboyson | 32:679654f2d023 | 93 | { |
andrewboyson | 37:793b39683406 | 94 | //Strip the questions if this is MDNS |
andrewboyson | 37:793b39683406 | 95 | //if (dnsProtocol == DNS_PROTOCOL_MDNS) p = DnsHdrData; |
andrewboyson | 37:793b39683406 | 96 | |
andrewboyson | 44:83ce5ace337b | 97 | //Go through each answer |
andrewboyson | 33:714a0345e59b | 98 | DnsHdrAncount = 0; |
andrewboyson | 44:83ce5ace337b | 99 | for (int i = 0; i < answerCount; i++) |
andrewboyson | 44:83ce5ace337b | 100 | { |
andrewboyson | 37:793b39683406 | 101 | //Bomb out if we are tending to overrun the buffer |
andrewboyson | 37:793b39683406 | 102 | if (p - DnsHdrPacket > 512) |
andrewboyson | 32:679654f2d023 | 103 | { |
andrewboyson | 37:793b39683406 | 104 | if (DnsServerTrace) LogTimeF("DnsServer-addAnswers - reply is getting too big\r\n"); |
andrewboyson | 32:679654f2d023 | 105 | return -1; |
andrewboyson | 32:679654f2d023 | 106 | } |
andrewboyson | 43:bc028d5a6424 | 107 | |
andrewboyson | 37:793b39683406 | 108 | //Encode the node name |
andrewboyson | 44:83ce5ace337b | 109 | switch (answers[i]) |
andrewboyson | 32:679654f2d023 | 110 | { |
andrewboyson | 44:83ce5ace337b | 111 | case RECORD_A: DnsNameEncodePtr(myFullName4, &p); break; |
andrewboyson | 44:83ce5ace337b | 112 | case RECORD_AAAA_LOCAL: DnsNameEncodePtr(myFullName6, &p); break; |
andrewboyson | 44:83ce5ace337b | 113 | case RECORD_AAAA_GLOBAL: DnsNameEncodePtr(myFullName6, &p); break; |
andrewboyson | 44:83ce5ace337b | 114 | case RECORD_PTR4: DnsNameEncodeIp4(DhcpLocalIp, &p); break; |
andrewboyson | 44:83ce5ace337b | 115 | case RECORD_PTR6_LOCAL: DnsNameEncodeIp6(SlaacLinkLocalIp, &p); break; |
andrewboyson | 44:83ce5ace337b | 116 | case RECORD_PTR6_GLOBAL: DnsNameEncodeIp6(SlaacGlobalIp, &p); break; |
andrewboyson | 13:9cd54f7db57a | 117 | } |
andrewboyson | 37:793b39683406 | 118 | |
andrewboyson | 44:83ce5ace337b | 119 | //Add the 16 bit type |
andrewboyson | 44:83ce5ace337b | 120 | *p++ = 0; |
andrewboyson | 44:83ce5ace337b | 121 | switch (answers[i]) |
andrewboyson | 44:83ce5ace337b | 122 | { |
andrewboyson | 44:83ce5ace337b | 123 | case RECORD_A: *p++ = DNS_RECORD_A; break; |
andrewboyson | 44:83ce5ace337b | 124 | case RECORD_AAAA_LOCAL: *p++ = DNS_RECORD_AAAA; break; |
andrewboyson | 44:83ce5ace337b | 125 | case RECORD_AAAA_GLOBAL: *p++ = DNS_RECORD_AAAA; break; |
andrewboyson | 44:83ce5ace337b | 126 | case RECORD_PTR4: *p++ = DNS_RECORD_PTR; break; |
andrewboyson | 44:83ce5ace337b | 127 | case RECORD_PTR6_LOCAL: *p++ = DNS_RECORD_PTR; break; |
andrewboyson | 44:83ce5ace337b | 128 | case RECORD_PTR6_GLOBAL: *p++ = DNS_RECORD_PTR; break; |
andrewboyson | 44:83ce5ace337b | 129 | } |
andrewboyson | 37:793b39683406 | 130 | |
andrewboyson | 37:793b39683406 | 131 | //Add the class |
andrewboyson | 37:793b39683406 | 132 | char mdns = dnsProtocol == DNS_PROTOCOL_MDNS ? 0x80 : 0; //Set the 15th bit (CACHE_FLUSH) of the class to 1 if MDNS |
andrewboyson | 32:679654f2d023 | 133 | *p++ = mdns; *p++ = 1; //16 bit Class LSB QCLASS_IN = 1 - internet |
andrewboyson | 37:793b39683406 | 134 | |
andrewboyson | 37:793b39683406 | 135 | //Add the TTL |
andrewboyson | 32:679654f2d023 | 136 | *p++ = 0; *p++ = 0; *p++ = 4; *p++ = 0; //32 bit TTL seconds - 1024 |
andrewboyson | 37:793b39683406 | 137 | |
andrewboyson | 37:793b39683406 | 138 | |
andrewboyson | 37:793b39683406 | 139 | //Add the 16 bit payload length |
andrewboyson | 44:83ce5ace337b | 140 | *p++ = 0; |
andrewboyson | 44:83ce5ace337b | 141 | switch (answers[i]) |
andrewboyson | 37:793b39683406 | 142 | { |
andrewboyson | 44:83ce5ace337b | 143 | case RECORD_A: *p++ = 4; break; |
andrewboyson | 44:83ce5ace337b | 144 | case RECORD_AAAA_LOCAL: *p++ = 16; break; |
andrewboyson | 44:83ce5ace337b | 145 | case RECORD_AAAA_GLOBAL: *p++ = 16; break; |
andrewboyson | 44:83ce5ace337b | 146 | case RECORD_PTR4: *p++ = myFullName4Length + 2; break; //add a byte for the initial length and another for the terminating zero length |
andrewboyson | 44:83ce5ace337b | 147 | case RECORD_PTR6_LOCAL: *p++ = myFullName6Length + 2; break; |
andrewboyson | 44:83ce5ace337b | 148 | case RECORD_PTR6_GLOBAL: *p++ = myFullName6Length + 2; break; |
andrewboyson | 37:793b39683406 | 149 | } |
andrewboyson | 37:793b39683406 | 150 | |
andrewboyson | 37:793b39683406 | 151 | //Add the payload |
andrewboyson | 44:83ce5ace337b | 152 | switch (answers[i]) |
andrewboyson | 37:793b39683406 | 153 | { |
andrewboyson | 44:83ce5ace337b | 154 | case RECORD_A: memcpy(p, &DhcpLocalIp, 4); p += 4; break; |
andrewboyson | 44:83ce5ace337b | 155 | case RECORD_AAAA_LOCAL: memcpy(p, SlaacLinkLocalIp, 16); p += 16; break; |
andrewboyson | 44:83ce5ace337b | 156 | case RECORD_AAAA_GLOBAL: memcpy(p, SlaacGlobalIp, 16); p += 16; break; |
andrewboyson | 44:83ce5ace337b | 157 | case RECORD_PTR4: DnsNameEncodePtr(myFullName4, &p); break; |
andrewboyson | 44:83ce5ace337b | 158 | case RECORD_PTR6_LOCAL: DnsNameEncodePtr(myFullName6, &p); break; |
andrewboyson | 44:83ce5ace337b | 159 | case RECORD_PTR6_GLOBAL: DnsNameEncodePtr(myFullName6, &p); break; |
andrewboyson | 37:793b39683406 | 160 | } |
andrewboyson | 44:83ce5ace337b | 161 | //Increment the number of good answers to send |
andrewboyson | 44:83ce5ace337b | 162 | DnsHdrAncount++; |
andrewboyson | 13:9cd54f7db57a | 163 | } |
andrewboyson | 32:679654f2d023 | 164 | return 0; |
andrewboyson | 32:679654f2d023 | 165 | } |
andrewboyson | 32:679654f2d023 | 166 | |
andrewboyson | 37:793b39683406 | 167 | int DnsServerHandleQuery(void (*traceback)(void), int dnsProtocol, int *pSize) //Received an mdns or llmnr query on port 5353 or 5355 |
andrewboyson | 37:793b39683406 | 168 | { |
andrewboyson | 37:793b39683406 | 169 | if (*pSize > 512) |
andrewboyson | 37:793b39683406 | 170 | { |
andrewboyson | 57:e0fb648acf48 | 171 | if (DnsServerTrace || NetTraceHostGetMatched()) |
andrewboyson | 43:bc028d5a6424 | 172 | { |
andrewboyson | 43:bc028d5a6424 | 173 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 174 | LogTimeF("DnsServer length %d too long\r\n", *pSize ); |
andrewboyson | 43:bc028d5a6424 | 175 | if (NetTraceStack) traceback(); |
andrewboyson | 43:bc028d5a6424 | 176 | } |
andrewboyson | 37:793b39683406 | 177 | return DO_NOTHING; |
andrewboyson | 37:793b39683406 | 178 | } |
andrewboyson | 37:793b39683406 | 179 | |
andrewboyson | 44:83ce5ace337b | 180 | myFullName4Length = DnsMakeFullNameFromName(dnsProtocol, NetName4, sizeof(myFullName4), myFullName4); |
andrewboyson | 44:83ce5ace337b | 181 | myFullName6Length = DnsMakeFullNameFromName(dnsProtocol, NetName6, sizeof(myFullName6), myFullName6); |
andrewboyson | 32:679654f2d023 | 182 | |
andrewboyson | 37:793b39683406 | 183 | if (readQuestions()) return DO_NOTHING; |
andrewboyson | 44:83ce5ace337b | 184 | if (!answerCount) return DO_NOTHING; |
andrewboyson | 37:793b39683406 | 185 | |
andrewboyson | 57:e0fb648acf48 | 186 | if (DnsServerTrace || NetTraceHostGetMatched()) |
andrewboyson | 37:793b39683406 | 187 | { |
andrewboyson | 43:bc028d5a6424 | 188 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 37:793b39683406 | 189 | LogTimeF("DnsServer received query\r\n"); |
andrewboyson | 43:bc028d5a6424 | 190 | if (NetTraceStack) traceback(); |
andrewboyson | 37:793b39683406 | 191 | DnsHdrLog(dnsProtocol); |
andrewboyson | 37:793b39683406 | 192 | } |
andrewboyson | 37:793b39683406 | 193 | |
andrewboyson | 37:793b39683406 | 194 | if (addAnswers(dnsProtocol)) return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 195 | |
andrewboyson | 13:9cd54f7db57a | 196 | DnsHdrIsReply = true; |
andrewboyson | 13:9cd54f7db57a | 197 | DnsHdrIsAuthoritative = true; |
andrewboyson | 13:9cd54f7db57a | 198 | DnsHdrWrite(); |
andrewboyson | 13:9cd54f7db57a | 199 | |
andrewboyson | 13:9cd54f7db57a | 200 | *pSize = p - DnsHdrPacket; |
andrewboyson | 13:9cd54f7db57a | 201 | |
andrewboyson | 57:e0fb648acf48 | 202 | if (DnsServerTrace || NetTraceHostGetMatched()) DnsHdrLog(dnsProtocol); |
andrewboyson | 13:9cd54f7db57a | 203 | |
andrewboyson | 37:793b39683406 | 204 | int dest; |
andrewboyson | 37:793b39683406 | 205 | if (dnsProtocol == DNS_PROTOCOL_MDNS && !mdnsUnicastReply) dest = MULTICAST_MDNS; |
andrewboyson | 37:793b39683406 | 206 | else dest = UNICAST; |
andrewboyson | 37:793b39683406 | 207 | |
andrewboyson | 57:e0fb648acf48 | 208 | return ActionMakeFromDestAndTrace(dest, NetTraceStack && DnsServerTrace || NetTraceHostGetMatched()); |
andrewboyson | 13:9cd54f7db57a | 209 | } |