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:
192:0dfa138a8e7d
Parent:
189:e1c7990486c4
Child:
193:47a953ab571b
--- a/resolve/nr.c	Thu Jan 21 15:09:55 2021 +0000
+++ b/resolve/nr.c	Fri Jan 22 13:24:08 2021 +0000
@@ -128,49 +128,6 @@
     }
     return -1;
 }
-static int getNameOnly(char* name, uint8_t addrType)
-{
-    for (int i = 0; i < RECORDS_COUNT; i++)
-    {
-        if (records[i].state == STATE_EMPTY) continue;
-        if (records[i].addrType != addrType) continue;
-        if (!addrIsEmpty(records[i].addrType, records[i].address)) continue;
-        if (DnsLabelIsSame(records[i].name, name)) return i;
-    }
-    return -1;
-}
-static int getIpOnly(char* address, uint8_t addrType)
-{
-    for (int i = 0; i < RECORDS_COUNT; i++)
-    {
-        if (records[i].state == STATE_EMPTY) continue;
-        if (records[i].addrType != addrType) continue;
-        if (records[i].name[0] != 0) continue;
-        if (addrIsSame(addrType, records[i].address, address)) return i;
-    }
-    return -1;
-}
-static int getCorrespondingRequest(uint8_t addrType, char* address, char* name)
-{
-    for (int i = 0; i < RECORDS_COUNT; i++)
-    {
-        if (records[i].state == STATE_EMPTY) continue;
-        if (records[i].addrType != addrType) continue;
-        if (records[i].name[0] == 0                              && addrIsSame(addrType, records[i].address, address)) return i;
-        if (addrIsEmpty(records[i].addrType, records[i].address) && DnsLabelIsSame(records[i].name, name))             return i;
-    }
-    return -1;
-}
-static int getExistingEntry(uint8_t addrType, char* address, char* name)
-{
-    for (int i = 0; i < RECORDS_COUNT; i++)
-    {
-        if (records[i].state == STATE_EMPTY) continue;
-        if (records[i].addrType != addrType) continue;
-        if ( addrIsSame(addrType, records[i].address, address) && DnsLabelIsSame(records[i].name, name)) return i;
-    }
-    return -1;
-}
 static int getOldest()
 {
     int        iOldest = 0;
@@ -208,7 +165,7 @@
         }
         if (NrTrace)
         {
-            LogTimeF("NR -- record %d - renew name of ", i);
+            LogTimeF("NR - record %2d - renew name of ", i);
             addrLog(addrType, address);
             Log("\r\n");
         }
@@ -222,7 +179,7 @@
         i = getOldest();
         if (NrTrace)
         {
-            LogTimeF("NR -- record %d - request name of ", i);
+            LogTimeF("NR - record %2d - request name of ", i);
             addrLog(addrType, address);
             Log("\r\n");
         }
@@ -258,8 +215,8 @@
         }
         if (NrTrace)
         {
-            if (addrType == ADDR_TYPE_A) LogTimeF("NR -- record %d - renew A of %s\r\n", i, name);
-            else                         LogTimeF("NR -- record %d - renew AAAA of %s\r\n", i, name);
+            if (addrType == ADDR_TYPE_A) LogTimeF("NR - record %2d - renew A of %s\r\n", i, name);
+            else                         LogTimeF("NR - record %2d - renew AAAA of %s\r\n", i, name);
         }
         //Leave name as is
         //Leave the address as is
@@ -271,8 +228,8 @@
         i = getOldest();
         if (NrTrace)
         {
-            if (addrType == ADDR_TYPE_A) LogTimeF("NR -- record %d - request A of %s\r\n", i, name);
-            else                         LogTimeF("NR -- record %d - request AAAA of %s\r\n", i, name);
+            if (addrType == ADDR_TYPE_A) LogTimeF("NR - record %2d - request A of %s\r\n", i, name);
+            else                         LogTimeF("NR - record %2d - request AAAA of %s\r\n", i, name);
         }
         strncpy(records[i].name, name, NR_NAME_MAX_LENGTH); //Set the name
         records[i].name[NR_NAME_MAX_LENGTH - 1] = 0;
@@ -286,26 +243,35 @@
     records[i].ipProtocol  = ETH_NONE;
     records[i].dnsProtocol = DNS_PROTOCOL_NONE;
 }
-static void updateRecord(int i, char* address, uint8_t addrType, char* name, int dnsProtocol, int ipProtocol)
+static void addEntry(uint8_t addrType, void* address, char* name, int dnsProtocol, int ipProtocol)
 {
-    records[i].todo        = TODO_NONE;
-    records[i].ageMs       = MsTimerCount;
-    records[i].addrType    = addrType;
-    addrCopy(addrType, records[i].address, address);
-    records[i].dnsProtocol = dnsProtocol;
-    records[i].ipProtocol  = ipProtocol;
-    records[i].state       = STATE_VALID;
-    strncpy(records[i].name, name, NR_NAME_MAX_LENGTH);
-    records[i].name[NR_NAME_MAX_LENGTH - 1] = 0;
-}
-void addEntry(uint8_t addrType, void* address, char* name, int dnsProtocol, int ipProtocol)
-{    
-    int i;
+
+    //Ignore records which do not have both address and name    
+    if (addrIsEmpty(addrType, address) || name == 0 || name[0] == 0)
+    {
+        if (NrTrace) LogTimeF("NR - ignoring invalid entry\r\n");
+        return;
+    }
+    //Ignore records with the name 'UNKNOWN'
+    if (strcmp(name, "UNKNOWN") == 0) return;
     
-    //Print what is being handled
+    //Delete any existing records linked to the new entry
+    for (int i = 0; i < RECORDS_COUNT; i++)
+    {
+        if (records[i].state == STATE_EMPTY) continue;
+        if (records[i].addrType != addrType) continue;
+        bool sameAddress = addrIsSame(addrType, records[i].address, address);
+        bool sameName = DnsLabelIsSame(records[i].name, name);
+        bool noAddress = addrIsEmpty(addrType, records[i].address);
+        bool requesting = records[i].state != STATE_VALID;
+        if (sameAddress || (sameName && (noAddress || requesting))) records[i].state = STATE_EMPTY;
+    }
+
+    //Add the new entry
+    int i = getOldest();
     if (NrTrace)
     {
-        LogTimeF("NR - received ");
+        LogTimeF("NR - record %2d - received ", i);
         EthProtocolLog(ipProtocol);
         Log(" ");
         DnsProtocolLog(dnsProtocol);
@@ -315,42 +281,15 @@
         Log(name);
         Log("'\r\n");
     }
-
-    //Ignore records which do not have both address and name    
-    if (addrIsEmpty(addrType, address) || name == 0 || name[0] == 0)
-    {
-        if (NrTrace) LogTimeF("NR -- ignoring invalid entry\r\n");
-        return;
-    }
-    //Ignore records with the name 'UNKNOWN'
-    if (strcmp(name, "UNKNOWN") == 0) return;
-    
-    i = getExistingEntry(addrType, address, name);
-    if (i >= 0)
-    {    
-        updateRecord(i, address, addrType, name, dnsProtocol, ipProtocol);
-        if (NrTrace) LogTimeF("NR -- record %d - refresh existing entry\r\n", i);
-        return;
-    }
-    
-    i = getCorrespondingRequest(addrType, address, name);
-    if (i >= 0)
-    {    
-        updateRecord(i, address, addrType, name, dnsProtocol, ipProtocol);
-        if (NrTrace) LogTimeF("NR -- record %d - replace request with new entry\r\n", i);
-        i = getCorrespondingRequest(addrType, address, name);
-        if (i >= 0)
-        {
-            if (NrTrace) LogTimeF("NR -- record %d - clear duplicate request\r\n", i, name);
-            records[i].state = STATE_EMPTY;
-        }
-        return;
-    }
-    
-    //No other entry exists so just add it to the next available space
-    i = getOldest();
-    if (NrTrace) LogTimeF("NR -- record %d - add entry\r\n", i);
-    updateRecord(i, address, addrType, name, dnsProtocol, ipProtocol);
+    records[i].todo        = TODO_NONE;
+    records[i].ageMs       = MsTimerCount;
+    records[i].addrType    = addrType;
+    addrCopy(addrType, records[i].address, address);
+    records[i].dnsProtocol = dnsProtocol;
+    records[i].ipProtocol  = ipProtocol;
+    records[i].state       = STATE_VALID;
+    strncpy(records[i].name, name, NR_NAME_MAX_LENGTH);
+    records[i].name[NR_NAME_MAX_LENGTH - 1] = 0;
 }
 static void addressToName(uint8_t addrType, void* address, char* name)
 {
@@ -454,12 +393,12 @@
 {
     if (addrIsEmpty(pr->addrType, pr->address))
     {
-        LogTimeF("NR -- record %d - queryNameFromIp has no address\r\n", pr - records);
+        LogTimeF("NR - record %2d - queryNameFromIp has no address\r\n", pr - records);
         return;
     }
     if (NrTrace)
     {
-        LogTimeF("NR -- record %d - send ", pr - records);
+        LogTimeF("NR - record %2d - send ", pr - records);
         EthProtocolLog(pr->ipProtocol);
         Log(" ");
         DnsProtocolLog(pr->dnsProtocol);
@@ -481,7 +420,7 @@
 {
     if (NrTrace)
     {
-        LogTimeF("NR -- record %d - send ", pr - records);
+        LogTimeF("NR - record %2d - send ", pr - records);
         EthProtocolLog(pr->ipProtocol);
         Log(" ");
         DnsProtocolLog(pr->dnsProtocol);
@@ -614,7 +553,7 @@
                 {
                     if (NrTrace)
                     {
-                        LogTimeF("NR - record %d - request for name of ", pr - records);
+                        LogTimeF("NR - record %2d - request for name of ", pr - records);
                         addrLog(pr->addrType, pr->address);
                         Log(" has timed out\r\n");
                     }
@@ -624,7 +563,7 @@
                 {
                     if (NrTrace)
                     {
-                        LogTimeF("NR - record %d request for address of '", pr - records);
+                        LogTimeF("NR - record %2d - request for address of '", pr - records);
                         Log(pr->name);
                         Log("' has timed out\r\n");
                         Log("\r\n");