Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Wed Dec 09 18:11:05 2020 +0000
Parent:
169:336d499dc560
Child:
171:f708d6776752
Commit message:
Tidied up mdns messages

Changed in this revision

resolve/nr4.c Show annotated file Show diff for this revision Revisions of this file
resolve/nr6.c Show annotated file Show diff for this revision Revisions of this file
udp/dns/dns.c Show annotated file Show diff for this revision Revisions of this file
udp/dns/dns.h Show annotated file Show diff for this revision Revisions of this file
udp/dns/dnsreply.c Show annotated file Show diff for this revision Revisions of this file
udp/tftp/tftp.c Show annotated file Show diff for this revision Revisions of this file
--- a/resolve/nr4.c	Tue Dec 01 12:56:34 2020 +0000
+++ b/resolve/nr4.c	Wed Dec 09 18:11:05 2020 +0000
@@ -16,9 +16,11 @@
 bool Nr4Trace = false;
 
 #define NAME_MAX_LENGTH      20
-#define  CACHE_TIMEOUT_MS  3600 * 1000
-#define FREEZE_TIMEOUT_MS  1800 * 1000
-#define  REPLY_TIMEOUT_MS     1 * 1000
+
+#define CACHE_TIMEOUT_MS  3600 * 1000
+#define STALE_TIMEOUT_MS  1800 * 1000
+#define EMPTY_TIMEOUT_MS     5 * 1000
+#define REPLY_TIMEOUT_MS     1 * 1000
 
 #define RECORDS_COUNT 20
 
@@ -70,6 +72,16 @@
     }
     return -1;
 }
+static int getIpOnly(uint32_t ip)
+{
+    for (int i = 0; i < RECORDS_COUNT; i++)
+    {
+        if (records[i].state == STATE_EMPTY) continue;
+        if (records[i].name[0] != 0) continue;
+        if (records[i].ip == ip) return i;
+    }
+    return -1;
+}
 static int getOldest()
 {
     int        iOldest = 0;
@@ -96,7 +108,14 @@
     i = getExistingIp(ip);
     if (i > -1)
     {
-        if (!MsTimerRelative(records[i].elapsed, FREEZE_TIMEOUT_MS)) return;
+        if (records[i].name[0] == 0)
+        {
+            if (!MsTimerRelative(records[i].elapsed, EMPTY_TIMEOUT_MS)) return;
+        }
+        else
+        {
+            if (!MsTimerRelative(records[i].elapsed, STALE_TIMEOUT_MS)) return;
+        }
         if (Nr4Trace)
         {
             LogTimeF("NR - renew name of ");
@@ -105,7 +124,7 @@
         }
         records[i].todo     = TODO_NAME_FROM_IP;
         records[i].state    = STATE_WANT;
-        records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE);
+        records[i].protocol = DnsGetNextProtocol(DNS_PROTOCOL_NONE);
         records[i].elapsed  = MsTimerCount;
         return;
     }
@@ -121,7 +140,7 @@
     records[i].ip       = ip;
     records[i].todo     = TODO_NAME_FROM_IP;
     records[i].state    = STATE_WANT;
-    records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE);
+    records[i].protocol = DnsGetNextProtocol(DNS_PROTOCOL_NONE);
     records[i].elapsed  = MsTimerCount;
     records[i].name[0]  = 0;
 }
@@ -135,14 +154,21 @@
     i = getExistingName(name);
     if (i > -1)
     {
-        if (!MsTimerRelative(records[i].elapsed, FREEZE_TIMEOUT_MS)) return;
+        if (records[i].ip == 0)
+        {
+            if (!MsTimerRelative(records[i].elapsed, EMPTY_TIMEOUT_MS)) return;
+        }
+        else
+        {
+            if (!MsTimerRelative(records[i].elapsed, STALE_TIMEOUT_MS)) return;
+        }
         if (Nr4Trace)
         {
             LogTimeF("NR - renew IPv4 of %s\r\n", name);
         }
         records[i].todo     = TODO_IP_FROM_NAME;
         records[i].state    = STATE_WANT;
-        records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE);
+        records[i].protocol = DnsGetNextProtocol(DNS_PROTOCOL_NONE);
         records[i].elapsed  = MsTimerCount;
         return;
     }
@@ -156,7 +182,7 @@
     records[i].ip       = 0;
     records[i].todo     = TODO_IP_FROM_NAME;
     records[i].state    = STATE_WANT;
-    records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE);
+    records[i].protocol = DnsGetNextProtocol(DNS_PROTOCOL_NONE);
     records[i].elapsed  = MsTimerCount;
     strncpy(records[i].name, name, NAME_MAX_LENGTH);
     records[i].name[NAME_MAX_LENGTH - 1] = 0;
@@ -175,43 +201,94 @@
 {    
     int i;
     
-    //Get existing ip and, if found, add it then clear any name only entries
+    //Print what is being handled
+    if (Nr4Trace)
+    {
+        LogTimeF("NR - received response ");
+        Ip4AddressLog(ip);
+        Log(" == '");
+        Log(name);
+        Log("'\r\n");
+    }
+
+    //Ignore records which do not have both ip and name    
+    if (ip == 0 || name == 0 || name[0] == 0)
+    {
+        if (Nr4Trace) LogTimeF("NR - ignoring unresolved response\r\n");
+        return;
+    }
+    
+    //Get existing ip and, if found, add the name then clear any name only entries
     i = getExistingIp(ip);
     if (i >= 0)
     {
         if (Nr4Trace)
         {
-            if (DnsLabelIsSame(name, records[i].name)) LogTimeF("NR - confirm existing ");
-            else                                       LogTimeF("NR - replace name for existing ip ");
-            Ip4AddressLog(ip);
-            Log(" == '");
-            Log(name);
-            Log("'\r\n");
+            if (DnsLabelIsSame(name, records[i].name))
+            {
+                LogTimeF("NR record %d - confirm existing name for ip ", i);
+                Ip4AddressLog(ip);
+                Log(" is '");
+                Log(name);
+                Log("'\r\n");
+            }
+            else
+            {
+                LogTimeF("NR record %d - update name for ip ", i);
+                Ip4AddressLog(ip);
+                Log(" from '");
+                Log(records[i].name);
+                Log("' to '");
+                Log(name);
+                Log("'\r\n");
+            }
         }
         addIpRecord(i, ip, name, protocol);
         
         i = getNameOnly(name);
         if (i >= 0)
         {
-            if (Nr4Trace) LogTimeF("NR - clear name '%s' with no ip\r\n", name);
+            if (Nr4Trace) LogTimeF("NR record %d - clear name '%s' with no ip\r\n", i, name);
             records[i].state = STATE_EMPTY;
         }
         return;
     }
     
-    //Get name only entry and, if found, add it
-    i = getNameOnly(name);
+    //Get existing name and, if found, add the ip then clear any ip only entries
+    i = getExistingName(name);
     if (i >= 0)
     {
         if (Nr4Trace)
         {
-            LogTimeF("NR - add ip for name ");
-            Ip4AddressLog(ip);
-            Log(" == '");
-            Log(name);
-            Log("'\r\n");
+            if (ip == records[i].ip)
+            {
+                LogTimeF("NR record %d - confirm existing ip for name '", i);
+                Log(name);
+                Log("' is ");
+                Ip4AddressLog(ip);
+                Log("\r\n");
+            }
+            else
+            {
+                LogTimeF("NR record %d - update ip for name '", i);
+                Log(name);
+                Log("' from ");
+                Ip4AddressLog(records[i].ip);
+                Log(" to ");
+                Ip4AddressLog(ip);
+                Log("\r\n");
+            }
         }
         addIpRecord(i, ip, name, protocol);
+        
+        i = getIpOnly(ip);
+        if (i >= 0)
+        {
+            if (Nr4Trace) LogTimeF("NR record %d - clear ip ", i);
+            Ip4AddressLog(ip);
+            Log(" with no name\r\n");
+            records[i].state = STATE_EMPTY;
+        }
         return;
     }
     
@@ -219,7 +296,7 @@
     i = getOldest();
     if (Nr4Trace)
     {
-        LogTimeF("NR - add ip for name ");
+        LogTimeF("NR record %d - add entry for ", i);
         Ip4AddressLog(ip);
         Log(" == '");
         Log(name);
@@ -337,7 +414,7 @@
 {
     if (pr->state == STATE_SENT && MsTimerRelative(pr->elapsed, REPLY_TIMEOUT_MS) && pr->protocol)
     {
-        pr->protocol = DnsGetNextProtocol4(pr->protocol);
+        pr->protocol = DnsGetNextProtocol(pr->protocol);
         if (pr->protocol)
         {
             pr->state = STATE_WANT;
--- a/resolve/nr6.c	Tue Dec 01 12:56:34 2020 +0000
+++ b/resolve/nr6.c	Wed Dec 09 18:11:05 2020 +0000
@@ -16,9 +16,10 @@
 bool Nr6Trace = false;
 
 #define NAME_MAX_LENGTH     20
-#define  CACHE_TIMEOUT_MS 3600 * 1000
-#define FREEZE_TIMEOUT_MS 1800 * 1000
-#define  REPLY_TIMEOUT_MS    1 * 1000
+#define CACHE_TIMEOUT_MS 3600 * 1000
+#define STALE_TIMEOUT_MS 1800 * 1000
+#define EMPTY_TIMEOUT_MS    5 * 1000
+#define REPLY_TIMEOUT_MS    1 * 1000
 
 #define RECORDS_COUNT 20
 
@@ -70,6 +71,16 @@
     }
     return -1;
 }
+static int getIpOnly(char* ip)
+{
+    for (int i = 0; i < RECORDS_COUNT; i++)
+    {
+        if (records[i].state == STATE_EMPTY) continue;
+        if (records[i].name[0] != 0) continue;
+        if (Ip6AddressIsSame(records[i].ip, ip)) return i;
+    }
+    return -1;
+}
 static int getOldest()
 {
     int        iOldest = 0;
@@ -96,7 +107,14 @@
     i = getExistingIp(ip);
     if (i > -1)
     {
-        if (!MsTimerRelative(records[i].elapsed, FREEZE_TIMEOUT_MS)) return;
+        if (records[i].name[0] == 0)
+        {
+            if (!MsTimerRelative(records[i].elapsed, EMPTY_TIMEOUT_MS)) return;
+        }
+        else
+        {
+            if (!MsTimerRelative(records[i].elapsed, STALE_TIMEOUT_MS)) return;
+        }
         if (Nr6Trace)
         {
             LogTimeF("NR - renew name of ");
@@ -105,7 +123,7 @@
         }
         records[i].todo     = TODO_NAME_FROM_IP;
         records[i].state    = STATE_WANT;
-        records[i].protocol = DnsGetNextProtocol6(DNS_PROTOCOL_NONE);
+        records[i].protocol = DnsGetNextProtocol(DNS_PROTOCOL_NONE);
         records[i].elapsed  = MsTimerCount;
         return;
     }
@@ -121,7 +139,7 @@
     Ip6AddressCopy(records[i].ip, ip);
     records[i].todo     = TODO_NAME_FROM_IP;
     records[i].state    = STATE_WANT;
-    records[i].protocol = DnsGetNextProtocol6(DNS_PROTOCOL_NONE);
+    records[i].protocol = DnsGetNextProtocol(DNS_PROTOCOL_NONE);
     records[i].elapsed  = MsTimerCount;
     records[i].name[0]  = 0;
 }
@@ -135,14 +153,21 @@
     i = getExistingName(name);
     if (i > -1)
     {
-        if (!MsTimerRelative(records[i].elapsed, FREEZE_TIMEOUT_MS)) return;
+        if (Ip6AddressIsEmpty(records[i].ip))
+        {
+            if (!MsTimerRelative(records[i].elapsed, EMPTY_TIMEOUT_MS)) return;
+        }
+        else
+        {
+            if (!MsTimerRelative(records[i].elapsed, STALE_TIMEOUT_MS)) return;
+        }
         if (Nr6Trace)
         {
             LogTimeF("NR - renew IPv6 of %s\r\n", name);
         }
         records[i].todo     = TODO_IP_FROM_NAME;
         records[i].state    = STATE_WANT;
-        records[i].protocol = DnsGetNextProtocol6(DNS_PROTOCOL_NONE);
+        records[i].protocol = DnsGetNextProtocol(DNS_PROTOCOL_NONE);
         records[i].elapsed  = MsTimerCount;
         return;
     }
@@ -156,7 +181,7 @@
     records[i].ip[0]    = 0;
     records[i].todo     = TODO_IP_FROM_NAME;
     records[i].state    = STATE_WANT;
-    records[i].protocol = DnsGetNextProtocol6(DNS_PROTOCOL_NONE);
+    records[i].protocol = DnsGetNextProtocol(DNS_PROTOCOL_NONE);
     records[i].elapsed  = MsTimerCount;
     strncpy(records[i].name, name, NAME_MAX_LENGTH);
     records[i].name[NAME_MAX_LENGTH - 1] = 0;
@@ -175,25 +200,54 @@
 {
     int i;
     
+    //Print what is being handled
+    if (Nr6Trace)
+    {
+        LogTimeF("NR - received response ");
+        Ip6AddressLog(ip);
+        Log(" == '");
+        Log(name);
+        Log("'\r\n");
+    }
+
+    //Ignore records which do not have both ip and name    
+    if (Ip6AddressIsEmpty(ip) || name == 0 || name[0] == 0)
+    {
+        if (Nr6Trace) LogTimeF("NR - ignoring unresolved response\r\n");
+        return;
+    }
+    
     //Get existing ip and, if found, add it then clear any name only entries
     i = getExistingIp(ip);
     if (i >= 0)
     {
         if (Nr6Trace)
         {
-            if (DnsLabelIsSame(name, records[i].name)) LogTimeF("NR - confirm existing ");
-            else                                       LogTimeF("NR - replace name for existing ip ");
-            Ip6AddressLog(ip);
-            Log(" == '");
-            Log(name);
-            Log("'\r\n");
+            if (DnsLabelIsSame(name, records[i].name))
+            {
+                LogTimeF("NR record %d - confirm existing name for ", i);
+                Ip6AddressLog(ip);
+                Log(" is '");
+                Log(name);
+                Log("'\r\n");
+            }
+            else
+            {
+                LogTimeF("NR record %d - update name for ip ", i);
+                Ip6AddressLog(ip);
+                Log(" from '");
+                Log(records[i].name);
+                Log("' to '");
+                Log(name);
+                Log("'\r\n");
+            }
         }
         addIpRecord(i, ip, name, protocol);
         
         i = getNameOnly(name);
         if (i >= 0)
         {
-            if (Nr6Trace) LogTimeF("NR - clear name '%s' with no ip\r\n", name);
+            if (Nr6Trace) LogTimeF("NR record %d - clear name '%s' with no ip\r\n", i, name);
             records[i].state = STATE_EMPTY;
         }
         return;
@@ -205,11 +259,13 @@
     {
         if (Nr6Trace)
         {
-            LogTimeF("NR - add ip for name ");
+            LogTimeF("NR record %d - update ip for name", i);
+            Log(name);
+            Log(" from ");
             Ip6AddressLog(ip);
-            Log(" == '");
-            Log(name);
-            Log("'\r\n");
+            Log(" to ");
+            Ip6AddressLog(records[i].ip);
+            Log("\r\n");
         }
         addIpRecord(i, ip, name, protocol);
         return;
@@ -219,9 +275,9 @@
     i = getOldest();
     if (Nr6Trace)
     {
-        LogTimeF("NR - add ip for name %s ");
+        LogTimeF("NR record %d - add entry for ", i);
         Ip6AddressLog(ip);
-        Log("== '");
+        Log(" == '");
         Log(name);
         Log("'\r\n");
     }
@@ -337,7 +393,7 @@
 {
     if (pr->state == STATE_SENT && MsTimerRelative(pr->elapsed, REPLY_TIMEOUT_MS) && pr->protocol)
     {
-        pr->protocol = DnsGetNextProtocol6(pr->protocol);
+        pr->protocol = DnsGetNextProtocol(pr->protocol);
         if (pr->protocol)
         {
             pr->state = STATE_WANT;
--- a/udp/dns/dns.c	Tue Dec 01 12:56:34 2020 +0000
+++ b/udp/dns/dns.c	Wed Dec 09 18:11:05 2020 +0000
@@ -60,18 +60,7 @@
         default:              LogF("%d", recordtype); break;
     }
 }
-int DnsGetNextProtocol4(int protocol)
-{
-    switch(protocol)
-    {
-        case DNS_PROTOCOL_NONE:  return DNS_PROTOCOL_MDNS;
-        case DNS_PROTOCOL_MDNS:  return DNS_PROTOCOL_LLMNR;
-        case DNS_PROTOCOL_LLMNR: return DNS_PROTOCOL_UDNS;
-        case DNS_PROTOCOL_UDNS:  return DNS_PROTOCOL_NONE;
-        default: LogTimeF("DNS invalid protocol %d\r\n", protocol); return DNS_PROTOCOL_NONE;
-    }
-}
-int DnsGetNextProtocol6(int protocol)
+int DnsGetNextProtocol(int protocol)
 {
     switch(protocol)
     {
--- a/udp/dns/dns.h	Tue Dec 01 12:56:34 2020 +0000
+++ b/udp/dns/dns.h	Wed Dec 09 18:11:05 2020 +0000
@@ -8,8 +8,7 @@
 extern void DnsProtocolLog     (uint8_t protocol);
 extern void DnsRecordTypeLog   (uint8_t recordtype);
 
-extern int  DnsGetNextProtocol4(int protocol);
-extern int  DnsGetNextProtocol6(int protocol);
+extern int  DnsGetNextProtocol(int protocol);
 
 extern void DnsMain(void);
 extern int  DnsHandlePacketReceived(void (*traceback)(void), int dnsProtocol, int sizeRx, void* pPacketRx, int* pSizeTx, void* pPacketTx);
--- a/udp/dns/dnsreply.c	Tue Dec 01 12:56:34 2020 +0000
+++ b/udp/dns/dnsreply.c	Wed Dec 09 18:11:05 2020 +0000
@@ -123,11 +123,18 @@
 }
 int DnsReplyHandle(void (*traceback)(void), int dnsProtocol)
 {
-    
     bool ok = true;
     if (!DnsHdrAncount) ok = false;
 
     p = DnsHdrData;
+    
+    if (DnsReplyTrace || NetTraceHostGetMatched())
+    {
+        if (NetTraceNewLine) Log("\r\n");
+        LogTimeF("DnsReply received\r\n");
+        if (NetTraceStack) traceback();
+        DnsHdrLog(dnsProtocol);
+    }
 
     if (ok)
     {
@@ -157,14 +164,5 @@
     NetTraceHostCheckIp6(DnsReplyIp6);
     NetTraceHostCheckIp6(DnsReplyRecordNameAsIp6);
     
-    if (DnsReplyTrace || NetTraceHostGetMatched())
-    {
-        if (NetTraceNewLine) Log("\r\n");
-        LogTimeF("DnsReply received\r\n");
-        if (NetTraceStack) traceback();
-        DnsHdrLog(dnsProtocol);
-    }
-
-    
     return DO_NOTHING;
 }
--- a/udp/tftp/tftp.c	Tue Dec 01 12:56:34 2020 +0000
+++ b/udp/tftp/tftp.c	Wed Dec 09 18:11:05 2020 +0000
@@ -13,7 +13,7 @@
 
 bool TftpTrace = false;
 
-#define WRITE_TIMEOUT_MS 6000 //Make this longer than the resolve time which is up to 3 seconds
+#define WRITE_TIMEOUT_MS 7000 //Make this longer than the resolve time which is up to 3 seconds
 
 #define TFTP_RRQ   1
 #define TFTP_WRQ   2