Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Revision:
36:900e24b27bfb
Parent:
33:714a0345e59b
Child:
37:793b39683406
--- a/udp/dns/dnsserver.cpp	Fri Sep 22 13:55:56 2017 +0000
+++ b/udp/dns/dnsserver.cpp	Mon Sep 25 07:09:32 2017 +0000
@@ -8,17 +8,17 @@
 #include   "slaac.h"
 #include      "io.h"
 
-#define DEBUG false
+#define DEBUG true
 
 //Set by 'initialise'
-char* p;             //Position relative to DnsHdrData and is updated while both reading questions and writing answers
-char  fullname[100]; //The name, adjusted to include the domain if needed by the protocol, used when reading and when writing
-int   fullnamelength;
+static char* p;               //Position relative to DnsHdrData and is updated while both reading questions and writing answers
+static char  myFullName[100]; //The name, adjusted to include the domain if needed by the protocol, used when reading and when writing
+static int   myFullNameLength;
 
 //Set by readQuestions and used by answerQuestions
-int   questionTypes[4];
-int   nodeNameTypes[4];
-bool  mdnsUnicastReply;
+static int   questionTypes[4];
+static int   nodeNameTypes[4];
+static bool  mdnsUnicastReply;
 
 static int initialise(int dnsProtocol, int size)
 {    
@@ -27,7 +27,7 @@
     
     p = DnsHdrData;
     
-    fullnamelength = DnsMakeFullNameFromName(dnsProtocol, NetName, sizeof(fullname), fullname);
+    myFullNameLength = DnsMakeFullNameFromName(dnsProtocol, NetName, sizeof(myFullName), myFullName);
     
     return 0;
 }
@@ -41,9 +41,9 @@
     {
         iEncodedName = DnsNameIndexFromPointer(p);
         int nameLength = DnsNameLength(p);
-        if (!nameLength) return DO_NOTHING;
+        if (!nameLength) { if (DEBUG) LogTimeF("DnsServer-readQuestions namelength is zero\r\n"); return -1; }
         nodeNameTypes[q] = DNS_RECORD_NONE;
-        if (!nodeNameTypes[q] && DnsNameCompare   (p, fullname)        ) nodeNameTypes[q] = DNS_RECORD_PTR;  //rtc.local
+        if (!nodeNameTypes[q] && DnsNameCompare   (p, myFullName)      ) nodeNameTypes[q] = DNS_RECORD_PTR;  //rtc.local
         if (!nodeNameTypes[q] && DnsNameCompareIp4(p, DhcpLocalIp)     ) nodeNameTypes[q] = DNS_RECORD_A;    //3.1.168.192.inaddr.arpa
         if (!nodeNameTypes[q] && DnsNameCompareIp6(p, SlaacLinkLocalIp)) nodeNameTypes[q] = 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
         p += nameLength;                            //Skip past the name
@@ -58,14 +58,14 @@
         {
             switch (recordType)
             {
-                case DNS_RECORD_A:    LogF("  for IP4 address\r\n"); break;
-                case DNS_RECORD_PTR:  LogF("  for name\r\n");        break;
-                case DNS_RECORD_AAAA: LogF("  for IP6 address\r\n"); break;
-                default:              LogF("  for unrecognised record type %d\r\n", recordType); break;
+                case DNS_RECORD_A:    LogF("  for IP4 address of "); break;
+                case DNS_RECORD_PTR:  LogF("  for name of ");        break;
+                case DNS_RECORD_AAAA: LogF("  for IP6 address of "); break;
+                default:              LogF("  for unrecognised record type %d of ", recordType); break;
             }
             char text[256];
             DnsNameDecode(iEncodedName, sizeof(text), text);
-            LogF("  Name     %s\r\n", text);
+            LogF("%s\r\n", text);
         }    
     }
     return 0;
@@ -75,6 +75,8 @@
     DnsHdrAncount = 0;
     for (int q = 0; q < DnsHdrQdcount; q++)
     {
+        if (DEBUG) LogF("  deal with question %d, answer %d, question %d, node %d\r\n", q, DnsHdrAncount, questionTypes[q], nodeNameTypes[q]);
+        
         //Skip unwanted record types
         switch (questionTypes[q])
         {
@@ -95,22 +97,25 @@
         switch (questionTypes[q])
         {
             case DNS_RECORD_A:
-                DnsNameEncode(fullname, &p);
+                if (DEBUG) Log("  replied with my IPv4 address\r\n");
+                DnsNameEncode(myFullName, &p);
+                  pPayload = (char*)&DhcpLocalIp;
                 lenPayload = 4;
-                pPayload = (char*)&DhcpLocalIp;
                 break;
                 
             case DNS_RECORD_AAAA:
-                DnsNameEncode(fullname, &p);
+                if (DEBUG) Log("  replied with my IPv6 address\r\n");
+                DnsNameEncode(myFullName, &p);
+                  pPayload = SlaacLinkLocalIp;
                 lenPayload = 16;
-                pPayload = SlaacLinkLocalIp;
                 break;
                 
             case DNS_RECORD_PTR:
+                if (DEBUG) Log("  replied with my name\r\n");
                 if (nodeNameTypes[q] == DNS_RECORD_A   ) DnsNameEncodeIp4(DhcpLocalIp,      &p);
                 if (nodeNameTypes[q] == DNS_RECORD_AAAA) DnsNameEncodeIp6(SlaacLinkLocalIp, &p);
-                lenPayload = fullnamelength;
-                pPayload = fullname;
+                  pPayload = myFullName;
+                lenPayload = myFullNameLength;
                 break;
         }
         char mdns = dnsProtocol == DNS_PROTOCOL_MDNS ? 0x80 : 0; //Set the 15th bit (CACHE_FLUSH) of the class to 1 if MDNS
@@ -119,7 +124,7 @@
         *p++ =    0; *p++ = 0; *p++ = 4; *p++ = 0;               //32 bit TTL seconds - 1024
         *p++ =    0; *p++ = lenPayload;                          //16 bit length in bytes
         memcpy(p, pPayload, lenPayload);                         //Copy the payload
-        p += lenPayload;                                         //Adjust the pointer to the next character afetr the payload
+        p += lenPayload;                                         //Adjust the pointer to the next character after the payload
 
     }
     return 0;