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