Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Sun Dec 27 17:26:21 2020 +0000
Revision:
177:2cd7fde8bfe5
Parent:
172:9bc3c7b2cca1
Child:
178:52714fef5ca1
Removed dns responses for AAAA addresses with no prefixes.

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 177:2cd7fde8bfe5 90 if (nodeIsName6 && recordType == DNS_RECORD_AAAA)
andrewboyson 177:2cd7fde8bfe5 91 {
andrewboyson 177:2cd7fde8bfe5 92 answers[answerCount++] = RECORD_AAAA_LINK_LOCAL;
andrewboyson 177:2cd7fde8bfe5 93 if (SlaacUniqueLocalIp[0]) answers[answerCount++] = RECORD_AAAA_UNIQUE_LOCAL;
andrewboyson 177:2cd7fde8bfe5 94 if (SlaacGlobalIp [0]) answers[answerCount++] = RECORD_AAAA_GLOBAL;
andrewboyson 177:2cd7fde8bfe5 95 }
andrewboyson 44:83ce5ace337b 96 if (nodeIsAddr4 && recordType == DNS_RECORD_PTR ) answers[answerCount++] = RECORD_PTR4;
andrewboyson 172:9bc3c7b2cca1 97 if (nodeIsLocl6 && recordType == DNS_RECORD_PTR ) answers[answerCount++] = RECORD_PTR6_LINK_LOCAL;
andrewboyson 172:9bc3c7b2cca1 98 if (nodeIsUniq6 && recordType == DNS_RECORD_PTR ) answers[answerCount++] = RECORD_PTR6_UNIQUE_LOCAL;
andrewboyson 44:83ce5ace337b 99 if (nodeIsGlob6 && recordType == DNS_RECORD_PTR ) answers[answerCount++] = RECORD_PTR6_GLOBAL;
andrewboyson 13:9cd54f7db57a 100 }
andrewboyson 32:679654f2d023 101 return 0;
andrewboyson 32:679654f2d023 102 }
andrewboyson 32:679654f2d023 103 static int addAnswers(int dnsProtocol)
andrewboyson 59:e0e556c8bd46 104 {
andrewboyson 44:83ce5ace337b 105 //Go through each answer
andrewboyson 33:714a0345e59b 106 DnsHdrAncount = 0;
andrewboyson 44:83ce5ace337b 107 for (int i = 0; i < answerCount; i++)
andrewboyson 44:83ce5ace337b 108 {
andrewboyson 37:793b39683406 109 //Bomb out if we are tending to overrun the buffer
andrewboyson 61:aad055f1b0d1 110 if (p - DnsHdrData > DnsHdrDataLength)
andrewboyson 32:679654f2d023 111 {
andrewboyson 37:793b39683406 112 if (DnsServerTrace) LogTimeF("DnsServer-addAnswers - reply is getting too big\r\n");
andrewboyson 32:679654f2d023 113 return -1;
andrewboyson 32:679654f2d023 114 }
andrewboyson 43:bc028d5a6424 115
andrewboyson 37:793b39683406 116 //Encode the node name
andrewboyson 44:83ce5ace337b 117 switch (answers[i])
andrewboyson 32:679654f2d023 118 {
andrewboyson 172:9bc3c7b2cca1 119 case RECORD_A: DnsNameEncodePtr(myFullName4, &p); break;
andrewboyson 172:9bc3c7b2cca1 120 case RECORD_AAAA_LINK_LOCAL: DnsNameEncodePtr(myFullName6, &p); break;
andrewboyson 172:9bc3c7b2cca1 121 case RECORD_AAAA_UNIQUE_LOCAL: DnsNameEncodePtr(myFullName6, &p); break;
andrewboyson 172:9bc3c7b2cca1 122 case RECORD_AAAA_GLOBAL: DnsNameEncodePtr(myFullName6, &p); break;
andrewboyson 172:9bc3c7b2cca1 123 case RECORD_PTR4: DnsNameEncodeIp4(DhcpLocalIp, &p); break;
andrewboyson 172:9bc3c7b2cca1 124 case RECORD_PTR6_LINK_LOCAL: DnsNameEncodeIp6(SlaacLinkLocalIp, &p); break;
andrewboyson 172:9bc3c7b2cca1 125 case RECORD_PTR6_UNIQUE_LOCAL: DnsNameEncodeIp6(SlaacUniqueLocalIp, &p); break;
andrewboyson 172:9bc3c7b2cca1 126 case RECORD_PTR6_GLOBAL: DnsNameEncodeIp6(SlaacGlobalIp, &p); break;
andrewboyson 13:9cd54f7db57a 127 }
andrewboyson 37:793b39683406 128
andrewboyson 44:83ce5ace337b 129 //Add the 16 bit type
andrewboyson 44:83ce5ace337b 130 *p++ = 0;
andrewboyson 44:83ce5ace337b 131 switch (answers[i])
andrewboyson 44:83ce5ace337b 132 {
andrewboyson 172:9bc3c7b2cca1 133 case RECORD_A: *p++ = DNS_RECORD_A; break;
andrewboyson 172:9bc3c7b2cca1 134 case RECORD_AAAA_LINK_LOCAL: *p++ = DNS_RECORD_AAAA; break;
andrewboyson 172:9bc3c7b2cca1 135 case RECORD_AAAA_UNIQUE_LOCAL: *p++ = DNS_RECORD_AAAA; break;
andrewboyson 172:9bc3c7b2cca1 136 case RECORD_AAAA_GLOBAL: *p++ = DNS_RECORD_AAAA; break;
andrewboyson 172:9bc3c7b2cca1 137 case RECORD_PTR4: *p++ = DNS_RECORD_PTR; break;
andrewboyson 172:9bc3c7b2cca1 138 case RECORD_PTR6_LINK_LOCAL: *p++ = DNS_RECORD_PTR; break;
andrewboyson 172:9bc3c7b2cca1 139 case RECORD_PTR6_UNIQUE_LOCAL: *p++ = DNS_RECORD_PTR; break;
andrewboyson 172:9bc3c7b2cca1 140 case RECORD_PTR6_GLOBAL: *p++ = DNS_RECORD_PTR; break;
andrewboyson 44:83ce5ace337b 141 }
andrewboyson 37:793b39683406 142
andrewboyson 37:793b39683406 143 //Add the class
andrewboyson 37:793b39683406 144 char mdns = dnsProtocol == DNS_PROTOCOL_MDNS ? 0x80 : 0; //Set the 15th bit (CACHE_FLUSH) of the class to 1 if MDNS
andrewboyson 32:679654f2d023 145 *p++ = mdns; *p++ = 1; //16 bit Class LSB QCLASS_IN = 1 - internet
andrewboyson 37:793b39683406 146
andrewboyson 37:793b39683406 147 //Add the TTL
andrewboyson 32:679654f2d023 148 *p++ = 0; *p++ = 0; *p++ = 4; *p++ = 0; //32 bit TTL seconds - 1024
andrewboyson 37:793b39683406 149
andrewboyson 37:793b39683406 150
andrewboyson 37:793b39683406 151 //Add the 16 bit payload length
andrewboyson 44:83ce5ace337b 152 *p++ = 0;
andrewboyson 44:83ce5ace337b 153 switch (answers[i])
andrewboyson 37:793b39683406 154 {
andrewboyson 172:9bc3c7b2cca1 155 case RECORD_A: *p++ = 4; break;
andrewboyson 172:9bc3c7b2cca1 156 case RECORD_AAAA_LINK_LOCAL: *p++ = 16; break;
andrewboyson 172:9bc3c7b2cca1 157 case RECORD_AAAA_UNIQUE_LOCAL: *p++ = 16; break;
andrewboyson 172:9bc3c7b2cca1 158 case RECORD_AAAA_GLOBAL: *p++ = 16; break;
andrewboyson 172:9bc3c7b2cca1 159 case RECORD_PTR4: *p++ = myFullName4Length + 2; break; //add a byte for the initial length and another for the terminating zero length
andrewboyson 172:9bc3c7b2cca1 160 case RECORD_PTR6_LINK_LOCAL: *p++ = myFullName6Length + 2; break;
andrewboyson 172:9bc3c7b2cca1 161 case RECORD_PTR6_UNIQUE_LOCAL: *p++ = myFullName6Length + 2; break;
andrewboyson 172:9bc3c7b2cca1 162 case RECORD_PTR6_GLOBAL: *p++ = myFullName6Length + 2; break;
andrewboyson 37:793b39683406 163 }
andrewboyson 37:793b39683406 164
andrewboyson 37:793b39683406 165 //Add the payload
andrewboyson 44:83ce5ace337b 166 switch (answers[i])
andrewboyson 37:793b39683406 167 {
andrewboyson 172:9bc3c7b2cca1 168 case RECORD_A: memcpy(p, &DhcpLocalIp, 4); p += 4; break;
andrewboyson 172:9bc3c7b2cca1 169 case RECORD_AAAA_LINK_LOCAL: memcpy(p, SlaacLinkLocalIp, 16); p += 16; break;
andrewboyson 172:9bc3c7b2cca1 170 case RECORD_AAAA_UNIQUE_LOCAL: memcpy(p, SlaacUniqueLocalIp, 16); p += 16; break;
andrewboyson 172:9bc3c7b2cca1 171 case RECORD_AAAA_GLOBAL: memcpy(p, SlaacGlobalIp, 16); p += 16; break;
andrewboyson 172:9bc3c7b2cca1 172 case RECORD_PTR4: DnsNameEncodePtr(myFullName4, &p); break;
andrewboyson 172:9bc3c7b2cca1 173 case RECORD_PTR6_LINK_LOCAL: DnsNameEncodePtr(myFullName6, &p); break;
andrewboyson 172:9bc3c7b2cca1 174 case RECORD_PTR6_UNIQUE_LOCAL: DnsNameEncodePtr(myFullName6, &p); break;
andrewboyson 172:9bc3c7b2cca1 175 case RECORD_PTR6_GLOBAL: DnsNameEncodePtr(myFullName6, &p); break;
andrewboyson 37:793b39683406 176 }
andrewboyson 44:83ce5ace337b 177 //Increment the number of good answers to send
andrewboyson 44:83ce5ace337b 178 DnsHdrAncount++;
andrewboyson 13:9cd54f7db57a 179 }
andrewboyson 32:679654f2d023 180 return 0;
andrewboyson 32:679654f2d023 181 }
andrewboyson 32:679654f2d023 182
andrewboyson 59:e0e556c8bd46 183 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 184 {
andrewboyson 128:79052cb4a41c 185 myFullName4Length = DnsLabelMakeFullNameFromName(dnsProtocol, NetName4, sizeof(myFullName4), myFullName4);
andrewboyson 128:79052cb4a41c 186 myFullName6Length = DnsLabelMakeFullNameFromName(dnsProtocol, NetName6, sizeof(myFullName6), myFullName6);
andrewboyson 32:679654f2d023 187
andrewboyson 37:793b39683406 188 if (readQuestions()) return DO_NOTHING;
andrewboyson 44:83ce5ace337b 189 if (!answerCount) return DO_NOTHING;
andrewboyson 37:793b39683406 190
andrewboyson 57:e0fb648acf48 191 if (DnsServerTrace || NetTraceHostGetMatched())
andrewboyson 37:793b39683406 192 {
andrewboyson 43:bc028d5a6424 193 if (NetTraceNewLine) Log("\r\n");
andrewboyson 37:793b39683406 194 LogTimeF("DnsServer received query\r\n");
andrewboyson 43:bc028d5a6424 195 if (NetTraceStack) traceback();
andrewboyson 37:793b39683406 196 DnsHdrLog(dnsProtocol);
andrewboyson 37:793b39683406 197 }
andrewboyson 37:793b39683406 198
andrewboyson 59:e0e556c8bd46 199 char* pRx = DnsHdrData;
andrewboyson 59:e0e556c8bd46 200 char* pEndRx = p;
andrewboyson 59:e0e556c8bd46 201 int qdcount = DnsHdrQdcount;
andrewboyson 59:e0e556c8bd46 202 int nscount = DnsHdrNscount;
andrewboyson 59:e0e556c8bd46 203 int arcount = DnsHdrArcount;
andrewboyson 59:e0e556c8bd46 204
andrewboyson 59:e0e556c8bd46 205 DnsHdrSetup(pPacketTx, *pSizeTx);
andrewboyson 59:e0e556c8bd46 206 p = DnsHdrData;
andrewboyson 59:e0e556c8bd46 207
andrewboyson 59:e0e556c8bd46 208 //Add the questions if this is not MDNS
andrewboyson 59:e0e556c8bd46 209 if (dnsProtocol == DNS_PROTOCOL_MDNS)
andrewboyson 59:e0e556c8bd46 210 {
andrewboyson 59:e0e556c8bd46 211 DnsHdrQdcount = 0;
andrewboyson 59:e0e556c8bd46 212 DnsHdrNscount = 0;
andrewboyson 59:e0e556c8bd46 213 DnsHdrArcount = 0;
andrewboyson 59:e0e556c8bd46 214 }
andrewboyson 59:e0e556c8bd46 215 else
andrewboyson 59:e0e556c8bd46 216 {
andrewboyson 59:e0e556c8bd46 217 DnsHdrQdcount = qdcount;
andrewboyson 59:e0e556c8bd46 218 DnsHdrNscount = nscount;
andrewboyson 59:e0e556c8bd46 219 DnsHdrArcount = arcount;
andrewboyson 59:e0e556c8bd46 220 while (pRx < pEndRx) *p++ = *pRx++;
andrewboyson 59:e0e556c8bd46 221 }
andrewboyson 59:e0e556c8bd46 222
andrewboyson 59:e0e556c8bd46 223
andrewboyson 37:793b39683406 224 if (addAnswers(dnsProtocol)) return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 225
andrewboyson 59:e0e556c8bd46 226 DnsHdrIsReply = true;
andrewboyson 59:e0e556c8bd46 227 DnsHdrIsAuthoritative = false;
andrewboyson 169:336d499dc560 228 if (dnsProtocol == DNS_PROTOCOL_MDNS) DnsHdrIsAuthoritative = true; //See rfc6762 18.4
andrewboyson 59:e0e556c8bd46 229 DnsHdrIsRecursiveQuery = false;
andrewboyson 59:e0e556c8bd46 230
andrewboyson 13:9cd54f7db57a 231 DnsHdrWrite();
andrewboyson 13:9cd54f7db57a 232
andrewboyson 59:e0e556c8bd46 233 *pSizeTx = p - DnsHdrPacket;
andrewboyson 13:9cd54f7db57a 234
andrewboyson 57:e0fb648acf48 235 if (DnsServerTrace || NetTraceHostGetMatched()) DnsHdrLog(dnsProtocol);
andrewboyson 13:9cd54f7db57a 236
andrewboyson 37:793b39683406 237 int dest;
andrewboyson 37:793b39683406 238 if (dnsProtocol == DNS_PROTOCOL_MDNS && !mdnsUnicastReply) dest = MULTICAST_MDNS;
andrewboyson 37:793b39683406 239 else dest = UNICAST;
andrewboyson 37:793b39683406 240
andrewboyson 160:6a1d1d368f80 241 return ActionMakeFromDestAndTrace(dest, (NetTraceStack && DnsServerTrace) || NetTraceHostGetMatched());
andrewboyson 13:9cd54f7db57a 242 }