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:
171:f708d6776752
Parent:
133:a37eb35a03f1
Child:
172:9bc3c7b2cca1
--- a/udp/dns/dnsquery.c	Wed Dec 09 18:11:05 2020 +0000
+++ b/udp/dns/dnsquery.c	Sat Dec 12 20:10:02 2020 +0000
@@ -24,17 +24,18 @@
 char     DnsQueryName[DNS_MAX_LABEL_LENGTH+1];
 uint32_t DnsQueryIp4 = 0;
 char     DnsQueryIp6[16];
-
-char     DnsQueryRecordType = DNS_RECORD_NONE;
-int      DnsQueryProtocol   = DNS_PROTOCOL_NONE;
 bool     DnsQueryIsBusy     = false;
 
-static uint32_t startedMs = 0;
+static char     _RecordType  = DNS_RECORD_NONE;
+static int      _DnsProtocol = DNS_PROTOCOL_NONE;
+static int      _IpProtocol  = 0;
+static uint32_t _StartedMs   = 0;
+
 static void reap()
 {
     if (!DnsQueryIsBusy) return;
     
-    if (MsTimerRelative(startedMs, TIME_OUT_SENT_MS))
+    if (MsTimerRelative(_StartedMs, TIME_OUT_SENT_MS))
     {
         LogTimeF("DNS reaped ongoing request for ");
         if (DnsQueryName[0]) LogF("name %s", DnsQueryName);
@@ -52,62 +53,67 @@
         DnsQueryIp4        = 0;
         DnsQueryIp6[0]     = 0;
         DnsQueryIsBusy     = false;
-        startedMs          = MsTimerCount;
-        DnsQueryProtocol   = DNS_PROTOCOL_NONE;
-        DnsQueryRecordType = DNS_RECORD_NONE;
+        _StartedMs         = MsTimerCount;
+        _DnsProtocol       = DNS_PROTOCOL_NONE;
+        _IpProtocol        = 0;
+        _RecordType        = DNS_RECORD_NONE;
     }
 }
 void DnsQueryMain()
 {
     reap();
 }
-void DnsQueryIp4FromName(char * name, int protocol)
+void DnsQueryIp4FromName(char * name, int dnsProtocol, int ipProtocol)
 {
-    DnsLabelMakeFullNameFromName(protocol, name, sizeof(DnsQueryName), DnsQueryName);
-    DnsQueryIp4        = 0;
-    DnsQueryIp6[0]     = 0;
-    DnsQueryIsBusy     = true;
-    startedMs          = MsTimerCount;
-    DnsQueryProtocol   = protocol;
-    DnsQueryRecordType = DNS_RECORD_A;
+    DnsLabelMakeFullNameFromName(dnsProtocol, name, sizeof(DnsQueryName), DnsQueryName);
+    DnsQueryIp4     = 0;
+    DnsQueryIp6[0]  = 0;
+    DnsQueryIsBusy  = true;
+    _StartedMs      = MsTimerCount;
+    _DnsProtocol    = dnsProtocol;
+    _IpProtocol     = ipProtocol;
+    _RecordType     = DNS_RECORD_A;
 }
-void DnsQueryIp6FromName(char * name, int protocol)
+void DnsQueryIp6FromName(char * name, int dnsProtocol, int ipProtocol)
 {
-    DnsLabelMakeFullNameFromName(protocol, name, sizeof(DnsQueryName), DnsQueryName);
-    DnsQueryIp4        = 0;
-    DnsQueryIp6[0]     = 0;
-    DnsQueryIsBusy     = true;
-    startedMs          = MsTimerCount;
-    DnsQueryProtocol   = protocol;
-    DnsQueryRecordType = DNS_RECORD_AAAA;
+    DnsLabelMakeFullNameFromName(dnsProtocol, name, sizeof(DnsQueryName), DnsQueryName);
+    DnsQueryIp4     = 0;
+    DnsQueryIp6[0]  = 0;
+    DnsQueryIsBusy  = true;
+    _StartedMs      = MsTimerCount;
+    _DnsProtocol    = dnsProtocol;
+    _IpProtocol     = ipProtocol;
+    _RecordType     = DNS_RECORD_AAAA;
 }
-void DnsQueryNameFromIp4(uint32_t ip, int protocol)
+void DnsQueryNameFromIp4(uint32_t ip, int dnsProtocol, int ipProtocol)
 {
-    DnsQueryName[0]    = 0;
-    DnsQueryIp4        = ip;
-    DnsQueryIp6[0]     = 0;
-    DnsQueryIsBusy     = true;
-    startedMs          = MsTimerCount;
-    DnsQueryProtocol   = protocol;
-    DnsQueryRecordType = DNS_RECORD_PTR;
+    DnsQueryName[0] = 0;
+    DnsQueryIp4     = ip;
+    DnsQueryIp6[0]  = 0;
+    DnsQueryIsBusy  = true;
+    _StartedMs      = MsTimerCount;
+    _DnsProtocol    = dnsProtocol;
+    _IpProtocol     = ipProtocol;
+    _RecordType     = DNS_RECORD_PTR;
 }
-void DnsQueryNameFromIp6(char* ip, int protocol)
+void DnsQueryNameFromIp6(char* ip, int dnsProtocol, int ipProtocol)
 {
-    DnsQueryName[0]    = 0;
-    DnsQueryIp4        = 0;
+    DnsQueryName[0] = 0;
+    DnsQueryIp4     = 0;
     Ip6AddressCopy(DnsQueryIp6, ip);
-    DnsQueryIsBusy     = true;
-    startedMs          = MsTimerCount;
-    DnsQueryProtocol   = protocol;
-    DnsQueryRecordType = DNS_RECORD_PTR;
+    DnsQueryIsBusy  = true;
+    _StartedMs      = MsTimerCount;
+    _DnsProtocol    = dnsProtocol;
+    _IpProtocol     = ipProtocol;
+    _RecordType     = DNS_RECORD_PTR;
 }
 static void logQuery()
 {
     if (NetTraceNewLine) Log("\r\n");
     LogTimeF("DnsQuery sent ");
-    DnsProtocolLog(DnsQueryProtocol);
+    DnsProtocolLog(_DnsProtocol);
     Log(" request for ");
-    DnsRecordTypeLog(DnsQueryRecordType);
+    DnsRecordTypeLog(_RecordType);
     Log(" ");
     if (DnsQueryIp4) //Reverse
     {
@@ -123,12 +129,12 @@
     }
     Log("\r\n");
 }
-int DnsQueryPoll(void* pPacket, int* pSize)
+int DnsQueryPoll(int ipType, void* pPacket, int* pSize)
 {   
     DnsHdrSetup(pPacket, *pSize);
 
-    if (!DnsQueryIsBusy)                                           return DO_NOTHING;
-    if (DnsQueryProtocol == DNS_PROTOCOL_UDNS && DhcpLocalIp == 0) return DO_NOTHING;
+    if (!DnsQueryIsBusy)                                       return DO_NOTHING;
+    if (_DnsProtocol == DNS_PROTOCOL_UDNS && DhcpLocalIp == 0) return DO_NOTHING;
     
     NetTraceHostCheckIp6(DnsQueryIp6);
     
@@ -137,6 +143,7 @@
     static uint16_t id = 0;
     DnsHdrId = ++id;
     DnsHdrIsReply          = false;
+    DnsHdrIsAuthoritative  = false; //Added 12/12/2020
     DnsHdrIsRecursiveQuery = false;
     
     DnsHdrQdcount = 1;
@@ -152,25 +159,25 @@
     else                     DnsNameEncodePtr(DnsQueryName, &p);
     
     *p++ = 0;
-    *p++ = DnsQueryRecordType;
-    *p++ = DnsQueryProtocol == DNS_PROTOCOL_MDNS && MDNS_UNICAST ? 0x80 : 0; //Set the 15th bit (UNICAST_RESPONSE) to 1 if MDNS
+    *p++ = _RecordType;
+    *p++ = _DnsProtocol == DNS_PROTOCOL_MDNS && MDNS_UNICAST ? 0x80 : 0; //Set the 15th bit (UNICAST_RESPONSE) to 1 if MDNS
     *p++ = 1;  //QCLASS_IN = 1 - internet
     
     *pSize = p - DnsHdrPacket;
     
     DnsQueryIsBusy = false;
     
-    if (DnsQueryTrace || NetTraceHostGetMatched()) DnsHdrLog(DnsQueryProtocol);
+    if (DnsQueryTrace || NetTraceHostGetMatched()) DnsHdrLog(_DnsProtocol);
 
     int dest = DO_NOTHING;
 
-    switch (DnsQueryProtocol)
+    switch (_DnsProtocol)
     {
         case DNS_PROTOCOL_UDNS:  dest =   UNICAST_DNS;   break;   //IPv6 ==> NdpDnsServer; IPv4 ==> DhcpDnsServer
         case DNS_PROTOCOL_MDNS:  dest = MULTICAST_MDNS;  break;
         case DNS_PROTOCOL_LLMNR: dest = MULTICAST_LLMNR; break;
         default:
-            LogTimeF("DNS unknown query protocol %d\r\n", DnsQueryProtocol);
+            LogTimeF("DNS unknown query protocol %d\r\n", _DnsProtocol);
             return DO_NOTHING;
     }