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:
32:679654f2d023
Parent:
15:6ca6778168b1
Child:
33:714a0345e59b
--- a/udp/dns/dns.cpp	Fri Aug 11 17:41:52 2017 +0000
+++ b/udp/dns/dns.cpp	Thu Aug 17 14:21:02 2017 +0000
@@ -4,6 +4,8 @@
 #include "dnsreply.h"
 #include "dnsserver.h"
 #include "io.h"
+#include "log.h"
+#include "dhcp.h"
 
 void DnsProtocolToString(uint8_t protocol, int size, char* text)
 {
@@ -28,6 +30,86 @@
         default:              snprintf(text, size, "%d", recordtype); break;
     }
 }
+int DnsGetNextProtocol(int protocol)
+{
+    switch(protocol)
+    {
+        case DNS_PROTOCOL_NONE:  return DNS_PROTOCOL_UDNS;
+        case DNS_PROTOCOL_UDNS:  return DNS_PROTOCOL_MDNS;
+        case DNS_PROTOCOL_MDNS:  return DNS_PROTOCOL_LLMNR;
+        case DNS_PROTOCOL_LLMNR: return DNS_PROTOCOL_NONE;
+        default: LogTimeF("DNS invalid protocol %d\r\n", protocol); return DNS_PROTOCOL_NONE;
+    }
+}
+int DnsMakeFullNameFromName(int protocol, char* p, int size, char* result)
+{
+    int i = 0;
+    char c;
+    
+    while (i < size - 1)
+    {
+        c = *p++;
+        if (!c) break;
+        *result++ = c;
+        i++;
+    }
+    if (protocol == DNS_PROTOCOL_MDNS)
+    {
+        p = ".local";
+        while (i < size - 1)
+        {
+            c = *p++;
+            if (!c) break;
+            *result++ = c;
+            i++;
+        }
+    }
+    if (protocol == DNS_PROTOCOL_UDNS && DhcpDomainName[0]) //Shouldn't do this in IPv6 as DHCP is IPv4 only
+    {
+        if (i < size - 1)
+        {
+            *result++ = '.';
+            i++;
+        }
+        p = DhcpDomainName;
+        while (i < size - 1)
+        {
+            c = *p++;
+            if (!c) break;
+            *result++ = c;
+            i++;
+        }
+    }
+    *result = 0; //Terminate the resulting string
+    return i;
+}
+int DnsStripNameFromFullName(int protocol, char* p, int size, char* result)
+{
+    int i = 0;
+    char c;
+    
+    while (i < size - 1)
+    {
+        c = *p++;
+        if (c >= 'A' && c <= 'Z') c |= 0x20;               //Make lower case
+        if (c == 0)   break;                               //End of the fqdn so stop
+        if (c == '.')
+        {
+            if (protocol == DNS_PROTOCOL_UDNS)
+            {
+                if (strcmp(p, DhcpDomainName) == 0) break; //Strip the domain from a UDNS fqdn if, and only if, it matches the domain given in DHCP. IPv4 only.
+            }
+            else
+            {
+                break;                                     //Strip the domain from an LLMNR (there shouldn't be one) or MDNS (it should always be '.local') fqdn
+            }
+        }
+        *result++ = c;
+        i++;
+    }
+    *result = 0;           //Terminate the copied string
+    return i;
+}
 
 void DnsTick()
 {