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:
43:bc028d5a6424
Parent:
37:793b39683406
Child:
44:83ce5ace337b
--- a/udp/dns/dnshdr.cpp	Sun Oct 15 17:54:09 2017 +0000
+++ b/udp/dns/dnshdr.cpp	Thu Oct 19 20:56:58 2017 +0000
@@ -111,15 +111,11 @@
     //Get the answers
     for (int a = 0; a < DnsHdrAncount; a++)
     {
-        if (p > DnsHdrData + 500)
-        {
-            Log("Answers have overrun the buffer\r\n");
-            return;
-        }
+        if (p > DnsHdrData + 500) { Log("  Answers have overrun the buffer\r\n"); return; }
         
         iEncodedName = DnsNameIndexFromPointer(p);
         int nameLength = DnsNameLength(p);
-        if (!nameLength) { LogTimeF("DnsServer-readQuestions namelength is zero\r\n"); return; }
+        if (!nameLength) { Log("  Answer name length is zero\r\n"); return; }
         p += nameLength;               //Skip past the name
         
         p++;                           //Skip the high byte of the record type
@@ -138,32 +134,62 @@
         
         char text[256];
         DnsRecordTypeToString(recordType, sizeof(text), text);
-        LogF("  Answer %s type record of ", text);
+        Log("  Answer ");
+        Log(text);
+        Log(" type record of ");
         DnsNameDecodePtr(iEncodedName, sizeof(text), text);
-        LogF(text);
+        Log(text);
+        Log(" ==> ");
         
         switch (recordType)           //Log the payload if its type is known
         {
             case DNS_RECORD_A:
-                Ip4AddressToString(*(uint32_t*)p, sizeof(text), text);
-                LogF(" ==> %s\r\n", text);
+                if (len == 4)
+                {
+                    Ip4AddressToString(*(uint32_t*)p, sizeof(text), text);
+                    Log(text);
+                }
+                else
+                {
+                    LogF("expected 4 bytes but had %d", len);
+                }
                 break;
                 
             case DNS_RECORD_AAAA:
-                Ip6AddressToString(p, sizeof(text), text);
-                LogF(" ==> %s\r\n", text);
+                if (len == 16)
+                {
+                    Ip6AddressToString(p, sizeof(text), text);
+                    Log(text);
+                }
+                else
+                {
+                    LogF("expected 16 bytes but had %d", len);
+                }
                 break;
                 
             case DNS_RECORD_PTR:
-                iEncodedName = DnsNameIndexFromPointer(p);
-                DnsNameDecodePtr(iEncodedName, sizeof(text), text);
-                LogF(" ==> %s\r\n", text);
+                if (len <= DNS_MAX_LABEL_LENGTH)
+                {
+                    iEncodedName = DnsNameIndexFromPointer(p);
+                    DnsNameDecodePtr(iEncodedName, sizeof(text), text);
+                    Log(text);
+                }
+                else
+                {
+                    LogF("length %d is greater than max DNS label length of %d\r\n", len, DNS_MAX_LABEL_LENGTH);
+                }
                 break;
                 
             case DNS_RECORD_TXT:
-                LogF(" ==> %.*s\r\n", len, p);
+            case DNS_RECORD_SRV:
+                LogF("'%.*s'", len, p);
+                break;
+                
+            default:
+                LogF(" %d characters", len);
                 break;
         }
+        Log("\r\n");
         p += len; //Adjust the pointer to the next character after the payload
 
     }
@@ -172,20 +198,29 @@
 void DnsHdrLog(int protocol)
 {
     char text[100];
-    DnsProtocolToString(protocol, sizeof(text), text);
-    LogF("%s header\r\n", text);
-    LogF("  Ident    %hd\r\n", DnsHdrId);
-    if (DnsHdrIsReply)
+    if (NetTraceVerbose)
     {
-        if (DnsHdrIsAuthoritative)  LogF("  Authoritative reply\r\n");
-        else                        LogF("  Non authoritative reply\r\n");
+        DnsProtocolToString(protocol, sizeof(text), text);
+        LogF("%s header\r\n", text);
+        LogF("  Ident    %hd\r\n", DnsHdrId);
+        if (DnsHdrIsReply)
+        {
+            if (DnsHdrIsAuthoritative)  LogF("  Authoritative reply\r\n");
+            else                        LogF("  Non authoritative reply\r\n");
+        }
+        else
+        {
+            if (DnsHdrIsRecursiveQuery) LogF("  Recursive query\r\n");
+            else                        LogF("  Non recursive query\r\n");
+        }
+        LogF("  qd, an, ns, ar  %hu, %hu, %hu, %hu\r\n", DnsHdrQdcount, DnsHdrAncount, DnsHdrNscount, DnsHdrArcount);
     }
     else
     {
-        if (DnsHdrIsRecursiveQuery) LogF("  Recursive query\r\n");
-        else                        LogF("  Non recursive query\r\n");
+        DnsProtocolToString(protocol, sizeof(text), text);
+        Log(text);
+        LogF(" header qd, an, ns, ar  %hu, %hu, %hu, %hu\r\n", DnsHdrQdcount, DnsHdrAncount, DnsHdrNscount, DnsHdrArcount);
     }
-    LogF("  qd, an, ns, ar  %hu, %hu, %hu, %hu\r\n", DnsHdrQdcount, DnsHdrAncount, DnsHdrNscount, DnsHdrArcount);
     logContent();
 }