Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Wed Dec 16 17:33:22 2020 +0000
Revision:
172:9bc3c7b2cca1
Parent:
169:336d499dc560
Child:
177:2cd7fde8bfe5
Modified name resolution to work with both IPv4 and IPv6. Before there were two independent modules.

Who changed what in which revision?

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