A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.

Dependents:   oldheating gps motorhome heating

Revision:
37:793b39683406
Parent:
36:900e24b27bfb
Child:
47:73af5c0b0dc2
--- a/udp/dns/dnsname.cpp	Mon Sep 25 07:09:32 2017 +0000
+++ b/udp/dns/dnsname.cpp	Wed Oct 04 07:51:02 2017 +0000
@@ -3,7 +3,7 @@
 #include  "ctype.h"
 #include    "log.h"
 
-#define DEBUG false
+bool DnsNameTrace = false;
 
 #define MAX_PACKET_SIZE 512
 #define MAX_DEPTH 10
@@ -25,7 +25,7 @@
     {
         if (p > DnsHdrPacket + MAX_PACKET_SIZE)
         {
-            if (DEBUG) LogTimeF("DnsNameLength strayed out of the packet\r\n");
+            if (DnsNameTrace) LogTimeF("DnsNameLength strayed out of the packet\r\n");
             return 0;
         }
         
@@ -54,7 +54,7 @@
         depth++;
         if (depth > MAX_DEPTH)
         {
-            if (DEBUG) LogTimeF("DnsNameIndexFromPointer exceeded depth limit\r\n");
+            if (DnsNameTrace) LogTimeF("DnsNameIndexFromPointer exceeded depth limit\r\n");
             return -1;
         }
         p = derefOffset(p);
@@ -62,7 +62,7 @@
     return p - DnsHdrPacket;
 }
 
-bool DnsNameCompare(char* pStart, char* pTarget)
+bool DnsNameComparePtr(char* pStart, char* pTarget)
 {
     char* p = pStart;
     
@@ -72,7 +72,7 @@
     {
         if (p > DnsHdrPacket + MAX_PACKET_SIZE)
         {
-            if (DEBUG) LogTimeF("DnsNameCompare strayed out of the packet\r\n");
+            if (DnsNameTrace) LogTimeF("DnsNameCompare strayed out of the packet\r\n");
             return false;
         }
 
@@ -87,7 +87,7 @@
             depth++;
             if (depth > MAX_DEPTH)
             {
-                if (DEBUG) LogTimeF("DnsNameCompare exceeded depth limit\r\n");
+                if (DnsNameTrace) LogTimeF("DnsNameCompare exceeded depth limit\r\n");
                 return false;
             }
             pStart = derefOffset(p);
@@ -207,16 +207,11 @@
                 if      (*p >= '0' && *p <= '9') nibble = *p - '0';
                 else if (*p >= 'a' && *p <= 'f') nibble = *p - 'a' + 10;
                 else if (*p >= 'A' && *p <= 'F') nibble = *p - 'A' + 10;
-                else return false;                                   // expect 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
-                if ((field & 1) == 0)
-                {
-                    if (pIp[15 - (field >> 1)] & 0x0F != nibble) return false;
-                }
-                else
-                {
-                    nibble <<= 4;
-                    if (pIp[15 - (field >> 1)] & 0xF0 != nibble) return false;
-                }
+                else return false;                                   // expect 7.2.d.7.2.f.e.f.f.f.7.f.2.0.2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa
+                int target = pIp[15 - (field >> 1)];
+                if ((field & 1) == 0) { target &= 0x0F; }
+                else                  { target &= 0xF0; target >>=4; }
+                if (target != nibble) return false;
                 p += length;
             }
             else if (field == 32)
@@ -238,7 +233,7 @@
     }
 }
 
-void DnsNameDecode(int offset, int lenName, char* pName)
+void DnsNameDecodePtr(int offset, int lenName, char* pName)
 {   
     bool nameStarted = false;
     int depth = 0;
@@ -248,7 +243,7 @@
         if (p > DnsHdrPacket + MAX_PACKET_SIZE)
         {
             *pName = 0;
-            if (DEBUG) LogTimeF("DnsNameDecode could not find name end\r\n");
+            if (DnsNameTrace) LogTimeF("DnsNameDecodePtr could not find name end\r\n");
             return;
         }
 
@@ -263,7 +258,7 @@
             depth++;
             if (depth > MAX_DEPTH)
             {
-                LogTimeF("DnsNameDecode exceeded depth limit\r\n");
+                LogTimeF("DnsNameDecodePtr exceeded depth limit\r\n");
                 *pName = 0;
                 return;
             }
@@ -276,7 +271,7 @@
                 if (length + 1 > lenName) //Leave room to terminate the string
                 {
                     *pName = 0;
-                    LogTimeF("DnsNameDecode overran name buffer\r\n");
+                    LogTimeF("DnsNameDecodePtr overran name buffer\r\n");
                     return;
                 }
                 lenName -= length + 1; //name chunk plus a '.'
@@ -510,7 +505,7 @@
     
 }
 
-void DnsNameEncode(char* pName, char** pp)
+void DnsNameEncodePtr(char* pName, char** pp)
 {
     char* p = *pp; //Get a convenient pointer to the next byte