Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Sat Dec 26 17:07:02 2020 +0000
Parent:
175:2d7aa004d881
Child:
177:2cd7fde8bfe5
Commit message:
Fixed 'unused root' problem with dns.

Changed in this revision

ip4/ip4addr.c Show annotated file Show diff for this revision Revisions of this file
ip4/ip4addr.h Show annotated file Show diff for this revision Revisions of this file
ip6/ip6addr.c Show annotated file Show diff for this revision Revisions of this file
ip6/ip6addr.h Show annotated file Show diff for this revision Revisions of this file
resolve/nr.c Show annotated file Show diff for this revision Revisions of this file
resolve/nraddr.c Show annotated file Show diff for this revision Revisions of this file
resolve/nraddr.h Show annotated file Show diff for this revision Revisions of this file
udp/dns/dnsquery.c Show annotated file Show diff for this revision Revisions of this file
--- a/ip4/ip4addr.c	Thu Dec 24 19:17:06 2020 +0000
+++ b/ip4/ip4addr.c	Sat Dec 26 17:07:02 2020 +0000
@@ -9,7 +9,7 @@
 #include "ntpclient.h"
 #include "tftp.h"
 
-int Ip4AddressToString(uint32_t ip, int size, char* text)
+int Ip4AddressToString(const uint32_t ip, const int size, char* text)
 {
     int a0 = (ip & 0xFF000000) >> 24;
     int a1 = (ip & 0x00FF0000) >> 16;
@@ -17,7 +17,7 @@
     int a3 = (ip & 0x000000FF);
     return snprintf(text, size, "%d.%d.%d.%d", a3, a2, a1, a0); 
 }
-int Ip4AddressLog(uint32_t ip)
+int Ip4AddressLog(const uint32_t ip)
 {
     int a0 = (ip & 0xFF000000) >> 24;
     int a1 = (ip & 0x00FF0000) >> 16;
@@ -25,7 +25,7 @@
     int a3 = (ip & 0x000000FF);
     return LogF("%d.%d.%d.%d", a3, a2, a1, a0); 
 }
-int Ip4AddressHttp(uint32_t ip)
+int Ip4AddressHttp(const uint32_t ip)
 {
     int a0 = (ip & 0xFF000000) >> 24;
     int a1 = (ip & 0x00FF0000) >> 16;
@@ -34,14 +34,14 @@
     return HttpAddF("%d.%d.%d.%d", a3, a2, a1, a0); 
 }
 
-uint32_t Ip4AddressParse(char* text)
+uint32_t Ip4AddressParse(const char* text)
 {
     int ints[4];
     sscanf(text, "%d.%d.%d.%d", &ints[3], &ints[2], &ints[1], &ints[0]);
     return (ints[0] << 24) + (ints[1] << 16) + (ints[2] << 8) + ints[3];
 }
 
-void Ip4AddressFromDest(int dest, uint32_t* pDstIp)
+void Ip4AddressFromDest(const int dest, uint32_t* pDstIp)
 {
     switch (dest)
     {
--- a/ip4/ip4addr.h	Thu Dec 24 19:17:06 2020 +0000
+++ b/ip4/ip4addr.h	Sat Dec 26 17:07:02 2020 +0000
@@ -1,9 +1,9 @@
-extern int      Ip4AddressToString(uint32_t ip, int size, char* text);
-extern int      Ip4AddressLog     (uint32_t ip);
-extern int      Ip4AddressHttp    (uint32_t ip);
-extern uint32_t Ip4AddressParse   (char* text);
+extern int      Ip4AddressToString(const uint32_t ip, int size, char* text);
+extern int      Ip4AddressLog     (const uint32_t ip);
+extern int      Ip4AddressHttp    (const uint32_t ip);
+extern uint32_t Ip4AddressParse   (const char* text);
 
-extern void     Ip4AddressFromDest(int dest, uint32_t* pDstIp);
+extern void     Ip4AddressFromDest(const int dest, uint32_t* pDstIp);
 
 #define IP4_BROADCAST_ADDRESS       0xFFFFFFFF
 #define IP4_MULTICAST_ALL_HOSTS     0x010000E0
--- a/ip6/ip6addr.c	Thu Dec 24 19:17:06 2020 +0000
+++ b/ip6/ip6addr.c	Sat Dec 26 17:07:02 2020 +0000
@@ -13,31 +13,10 @@
 void Ip6AddrClear(char* ip)
 {
     ip[ 0] = 0; //Just set the first byte to zero
-    ip[12] = 0; //and the 12 (the first byte of a IP4 address)
 }
 bool Ip6AddrIsEmpty(const char* ip)
 {
-    return !ip[0] && !ip[12]; //Check for the first and 12th byte being non zero
-}
-void Ip6AddrFromIp4(char* ip6, uint32_t ip4)
-{
-    memset(ip6, 0, 16);
-    *ip6++ = 0; *ip6++ = 0; *ip6++ = 0; *ip6++ = 0;
-    *ip6++ = 0; *ip6++ = 0; *ip6++ = 0; *ip6++ = 0;
-    *ip6++ = 0; *ip6++ = 0; *ip6++ = 0; *ip6++ = 0;
-    *ip6++ =  (ip4 >>  0) & 0xFF;
-    *ip6++ =  (ip4 >>  8) & 0xFF;
-    *ip6++ =  (ip4 >> 16) & 0xFF;
-    *ip6++ =  (ip4 >> 24) & 0xFF;
-}
-uint32_t Ip6AddrToIp4(char* ip6)
-{
-    uint32_t ip4;
-    ip4  = ip6[12] << 0;
-    ip4 |= ip6[13] << 8;
-    ip4 |= ip6[14] << 16;
-    ip4 |= ip6[15] << 24;
-    return ip4;
+    return !ip[0]; //Check for the first byte being non zero
 }
 static void addHexNibble(bool* pAdded, int number, int index, char** pp)
 {
@@ -152,12 +131,6 @@
     memcpy(ipTo, ipFrom, 16);
 }
 
-bool Ip6AddrIsIp4(const char *p)
-{
-    if (p[ 0] != 0) return false;
-    if (p[12] != 0) return false;
-    return true;
-}
 bool Ip6AddrIsLinkLocal(const char* p)
 {
     if (p[0] != 0xFE) return false;
--- a/ip6/ip6addr.h	Thu Dec 24 19:17:06 2020 +0000
+++ b/ip6/ip6addr.h	Sat Dec 26 17:07:02 2020 +0000
@@ -1,18 +1,15 @@
 #include <stdint.h>
+#include <stdbool.h>
 
 extern int  Ip6AddrToString(const char* ip, int size, char* text);
 extern int  Ip6AddrLog     (const char* ip);
 extern int  Ip6AddrHttp    (const char* ip);
 
-extern bool     Ip6AddrIsSame (const char* ipA, const char* ipB);
-extern bool     Ip6AddrIsEmpty(const char* ip);
-extern void     Ip6AddrCopy   (char* ipTo, const char* ipFrom);
-extern void     Ip6AddrClear  (char* ip);
-extern void     Ip6AddrFromIp4(char* ip6, uint32_t ip4);
-extern uint32_t Ip6AddrToIp4  (char* ip6);
+extern bool Ip6AddrIsSame (const char* ipA, const char* ipB);
+extern bool Ip6AddrIsEmpty(const char* ip);
+extern void Ip6AddrCopy   (char* ipTo, const char* ipFrom);
+extern void Ip6AddrClear  (char* ip);
 
-
-extern bool Ip6AddrIsIp4        (const char *p);
 extern bool Ip6AddrIsLinkLocal  (const char* p);
 extern bool Ip6AddrIsUniqueLocal(const char* p);
 extern bool Ip6AddrIsGlobal     (const char* p);
--- a/resolve/nr.c	Thu Dec 24 19:17:06 2020 +0000
+++ b/resolve/nr.c	Sat Dec 26 17:07:02 2020 +0000
@@ -7,12 +7,12 @@
 #include      "net.h"
 #include      "eth.h"
 #include      "mac.h"
-#include  "ip6addr.h"
 #include     "dhcp.h"
 #include      "dns.h"
 #include "dnsquery.h"
 #include "dnslabel.h"
 #include     "http.h"
+#include   "nraddr.h"
 
 bool Nr4Trace = false; //Do not use
 bool NrTrace = false;
@@ -54,7 +54,7 @@
     for (int i = 0; i < RECORDS_COUNT; i++)
     {
         if (records[i].state == STATE_EMPTY) continue;
-        if (Ip6AddrIsSame(records[i].address, address)) return i;
+        if (NrAddrIsSame(records[i].address, address)) return i;
     }
     return -1;
 }
@@ -72,7 +72,7 @@
     for (int i = 0; i < RECORDS_COUNT; i++)
     {
         if (records[i].state == STATE_EMPTY) continue;
-        if (!Ip6AddrIsEmpty(records[i].address)) continue;
+        if (!NrAddrIsEmpty(records[i].address)) continue;
         if (DnsLabelIsSame(records[i].name, name)) return i;
     }
     return -1;
@@ -83,7 +83,7 @@
     {
         if (records[i].state == STATE_EMPTY) continue;
         if (records[i].name[0] != 0) continue;
-        if (Ip6AddrIsSame(records[i].address, address)) return i;
+        if (NrAddrIsSame(records[i].address, address)) return i;
     }
     return -1;
 }
@@ -106,7 +106,7 @@
 static void makeRequestForNameFromAddress(char* address)
 {
     //Don't treat non ips
-    if (Ip6AddrIsEmpty(address)) return;
+    if (NrAddrIsEmpty(address)) return;
     int i;
     
     //If a valid record already exists then request an update
@@ -125,7 +125,7 @@
         if (NrTrace)
         {
             LogTimeF("NR - renew name of ");
-            Ip6AddrLog(address);
+            NrAddrLog(address);
             Log("\r\n");
         }
         //Leave the address as is
@@ -138,11 +138,11 @@
         if (NrTrace)
         {
             LogTimeF("NR - request name of ");
-            Ip6AddrLog(address);
+            NrAddrLog(address);
             Log("\r\n");
         }
         i = getOldest();
-        Ip6AddrCopy(records[i].address, address);  //Set the address
+        NrAddrCopy(records[i].address, address);  //Set the address
         records[i].name[0]  = 0;                      //Clear the name
         records[i].ageMs    = MsTimerCount;           //Start age
     }
@@ -163,7 +163,7 @@
     if (i > -1)
     {
         if (records[i].state != STATE_VALID) return;
-        if (Ip6AddrIsEmpty(records[i].address))
+        if (NrAddrIsEmpty(records[i].address))
         {
             if (!MsTimerRelative(records[i].ageMs, EMPTY_TIMEOUT_MS)) return;
         }
@@ -191,7 +191,7 @@
         i = getOldest();
         strncpy(records[i].name, name, NAME_MAX_LENGTH); //Set the name
         records[i].name[NAME_MAX_LENGTH - 1] = 0;
-        Ip6AddrClear(records[i].address);             //Clear the address
+        NrAddrClear(records[i].address);             //Clear the address
         records[i].ageMs  = MsTimerCount;                //Start age
     }
     records[i].todo        = todo;
@@ -204,7 +204,7 @@
 {
     records[i].todo        = TODO_NONE;
     records[i].ageMs       = MsTimerCount;
-    Ip6AddrCopy(records[i].address, address);
+    NrAddrCopy(records[i].address, address);
     records[i].dnsProtocol = dnsProtocol;
     records[i].ipProtocol  = ipProtocol;
     records[i].state       = STATE_VALID;
@@ -232,14 +232,14 @@
     if (NrTrace)
     {
         LogTimeF("NR - received ");
-        Ip6AddrLog(address);
+        NrAddrLog(address);
         Log(" == '");
         Log(name);
         Log("'\r\n");
     }
 
     //Ignore records which do not have both address and name    
-    if (Ip6AddrIsEmpty(address) || name == 0 || name[0] == 0)
+    if (NrAddrIsEmpty(address) || name == 0 || name[0] == 0)
     {
         if (NrTrace) LogTimeF("NR -- ignoring invalid entry\r\n");
         return;
@@ -278,7 +278,7 @@
         if (NrTrace)
         {
             LogTimeF("NR -- record %d - add address ", i);
-            Ip6AddrLog(address);
+            NrAddrLog(address);
             Log("\r\n");
         }
         updateRecord(i, address, name, dnsProtocol, ipProtocol);
@@ -298,7 +298,7 @@
     for (int i = 0; i < RECORDS_COUNT; i++)
     {
         if (records[i].state == STATE_EMPTY) continue;
-        if (Ip6AddrIsSame(records[i].address, address))
+        if (NrAddrIsSame(records[i].address, address))
         {
             strcpy(name, records[i].name);
             return;
@@ -309,17 +309,17 @@
 static void nameToAddress(char* name, char* address)
 {
     uint32_t newest = 0xFFFFFFFF;
-    Ip6AddrClear(address);
+    NrAddrClear(address);
     for (int i = 0; i < RECORDS_COUNT; i++)
     {
         if (records[i].state == STATE_EMPTY) continue;
-        if(Ip6AddrIsEmpty(records[i].address)) continue;
+        if(NrAddrIsEmpty(records[i].address)) continue;
         if (!DnsLabelIsSame(records[i].name, name)) continue;
         uint32_t age = MsTimerCount - records[i].ageMs;
         if (age <= newest)
         {
             newest = age;
-            Ip6AddrCopy(address, records[i].address);
+            NrAddrCopy(address, records[i].address);
         }
     }
 }
@@ -330,7 +330,7 @@
 void NrMakeRequestForNameFromAddress4(uint32_t address4)
 {
     char address6[16];
-    Ip6AddrFromIp4(address6, address4);
+    NrAddrFromIp4(address6, address4);
     makeRequestForNameFromAddress(address6);
 }
 void NrMakeRequestForAddress6FromName(char* name)
@@ -348,7 +348,7 @@
 void NrAddAddress4(uint32_t address4, char* name, int dnsProtocol)
 {
     char address6[16];
-    Ip6AddrFromIp4(address6, address4);
+    NrAddrFromIp4(address6, address4);
     addEntry(address6, name, dnsProtocol, EthProtocol);
 }
 void NrNameToAddress6(char* name, char* address)
@@ -359,7 +359,7 @@
 {
     char address6[16];
     nameToAddress(name, address6);
-    *pAddress4 = Ip6AddrToIp4(address6);
+    *pAddress4 = NrAddrToIp4(address6);
 }
 void NrAddress6ToName(char* address, char* name)
 {
@@ -368,7 +368,7 @@
 void NrAddress4ToName(uint32_t address4, char* name)
 {
     char address6[16];
-    Ip6AddrFromIp4(address6, address4);
+    NrAddrFromIp4(address6, address4);
     addressToName(address6, name);
 }
 static char letterFromStateAndProtocol(uint8_t dnsState, uint8_t dnsProtocol, uint16_t ipProtocol)
@@ -396,12 +396,12 @@
     for (int i = 0; i < RECORDS_COUNT; i++)
     {
         if (records[i].state == STATE_EMPTY) continue;
-        if (!Ip6AddrIsEmpty(records[i].address) || records[i].name[0])
+        if (!NrAddrIsEmpty(records[i].address) || records[i].name[0])
         {
             HttpAddF("%4u ", (MsTimerCount - records[i].ageMs) / 1000 / 60);
             
             int ipLen;
-            ipLen = Ip6AddrHttp(records[i].address);
+            ipLen = NrAddrHttp(records[i].address);
             HttpAddFillChar(' ', 40 - ipLen);
 
             HttpAddChar(letterFromStateAndProtocol(records[i].state, records[i].dnsProtocol, records[i].ipProtocol));
@@ -420,7 +420,7 @@
     for (int i = 0; i < RECORDS_COUNT; i++)
     {
         if (records[i].state == STATE_EMPTY) continue;
-        if (!Ip6AddrIsEmpty(records[i].address) || records[i].name[0])
+        if (!NrAddrIsEmpty(records[i].address) || records[i].name[0])
         {
             HttpAddByteAsHex(i);
             HttpAddChar('\t');
@@ -441,6 +441,11 @@
 }
 static void queryNameFromIp(struct record* pr)
 {
+    if (NrAddrIsEmpty(pr->address))
+    {
+        LogTime("NR -- queryNameFromIp has no address\r\n");
+        return;
+    }
     if (NrTrace)
     {
         LogTime("NR -- send ");
@@ -448,12 +453,12 @@
         Log(" ");
         DnsProtocolLog(pr->dnsProtocol);
         Log(" request for name of ");
-        Ip6AddrLog(pr->address);
+        NrAddrLog(pr->address);
         Log("\r\n");
     }
-    if (Ip6AddrIsIp4(pr->address)) 
+    if (NrAddrIsIp4(pr->address)) 
     {
-        uint32_t address4 = Ip6AddrToIp4(pr->address);
+        uint32_t address4 = NrAddrToIp4(pr->address);
         DnsQueryNameFromIp4(address4, pr->dnsProtocol, pr->ipProtocol);
     }
     else
@@ -553,7 +558,7 @@
                     if (NrTrace)
                     {
                         LogTimeF("NR - request for name of ");
-                        Ip6AddrLog(pr->address);
+                        NrAddrLog(pr->address);
                         Log(" has timed out\r\n");
                     }
                     pr->name[0] = 0;
@@ -567,7 +572,7 @@
                         Log("' has timed out\r\n");
                         Log("\r\n");
                     }
-                    Ip6AddrClear(pr->address);
+                    NrAddrClear(pr->address);
                 }
                 pr->todo   = TODO_NONE;
                 pr->state  = STATE_VALID;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resolve/nraddr.c	Sat Dec 26 17:07:02 2020 +0000
@@ -0,0 +1,76 @@
+
+#include <string.h>
+#include "ip6addr.h"
+#include "ip4addr.h"
+
+void NrAddrClear(char* ip)
+{
+    ip[ 0] = 0;
+    ip[12] = 0;
+}
+bool NrAddrIsEmpty(const char* ip)
+{
+    return ip[0] == 0 && ip[12] == 0; //Check for the first and 12th byte being non zero
+}
+bool NrAddrIsIp4(const char* ip)
+{
+    return ip[0] == 0 && ip[12] != 0; //Check the first byte is zero and the 12th byte is non zero
+}
+bool NrAddrIsIp6(const char* ip)
+{
+    return ip[0] != 0; //Check the first byte is non zero
+}
+void NrAddrFromIp4(char* ip6, const uint32_t ip4)
+{
+    memset(ip6, 0, 16);
+    *ip6++ = 0; *ip6++ = 0; *ip6++ = 0; *ip6++ = 0;
+    *ip6++ = 0; *ip6++ = 0; *ip6++ = 0; *ip6++ = 0;
+    *ip6++ = 0; *ip6++ = 0; *ip6++ = 0; *ip6++ = 0;
+    *ip6++ =  (ip4 >>  0) & 0xFF; //byte 12 192
+    *ip6++ =  (ip4 >>  8) & 0xFF; //byte 13 168
+    *ip6++ =  (ip4 >> 16) & 0xFF; //byte 14   0
+    *ip6++ =  (ip4 >> 24) & 0xFF; //byte 15   x
+}
+uint32_t NrAddrToIp4(const char* ip6)
+{
+    uint32_t ip4;
+    ip4  = ip6[12] << 0;
+    ip4 |= ip6[13] << 8;
+    ip4 |= ip6[14] << 16;
+    ip4 |= ip6[15] << 24;
+    return ip4;
+}
+
+bool NrAddrIsSame(const char* ipA, const char* ipB)
+{
+    return Ip6AddrIsSame(ipA, ipB);
+}
+
+int NrAddrLog(const char* ip)
+{
+    if (NrAddrIsIp4(ip))
+    {
+        uint32_t ip4 = NrAddrToIp4(ip);
+        return Ip4AddressLog(ip4);
+    }
+    else
+    {
+        return Ip6AddrLog(ip);
+    }
+}
+int  NrAddrHttp(const char* ip)
+{
+    if (NrAddrIsIp4(ip))
+    {
+        uint32_t ip4 = NrAddrToIp4(ip);
+        return Ip4AddressHttp(ip4);
+    }
+    else
+    {
+        return Ip6AddrHttp(ip);
+    }
+}
+void NrAddrCopy(char* ipTo, const char* ipFrom)
+{
+    Ip6AddrCopy(ipTo, ipFrom);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resolve/nraddr.h	Sat Dec 26 17:07:02 2020 +0000
@@ -0,0 +1,14 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+extern void     NrAddrClear  (char* ip);
+extern bool     NrAddrIsEmpty(const char* ip);
+extern bool     NrAddrIsIp4  (const char* ip);
+extern bool     NrAddrIsIp6  (const char* ip);
+extern void     NrAddrFromIp4(char* ip, const uint32_t ip4);
+extern uint32_t NrAddrToIp4  (const char* ip);
+
+extern bool     NrAddrIsSame (const char* ipA, const char* ipB);
+extern int      NrAddrLog    (const char* ip);
+extern int      NrAddrHttp   (const char* ip);
+extern void     NrAddrCopy   (char* ipTo, const char* ipFrom);
\ No newline at end of file
--- a/udp/dns/dnsquery.c	Thu Dec 24 19:17:06 2020 +0000
+++ b/udp/dns/dnsquery.c	Sat Dec 26 17:07:02 2020 +0000
@@ -65,6 +65,11 @@
 }
 void DnsQueryIp4FromName(char * name, int dnsProtocol, int ipProtocol)
 {
+    if (!name[0])
+    {
+        LogTime("DnsQueryIp4FromName called with no name\r\n");
+        return;
+    }
     DnsLabelMakeFullNameFromName(dnsProtocol, name, sizeof(DnsQueryName), DnsQueryName);
     DnsQueryIp4     = 0;
     DnsQueryIp6[0]  = 0;
@@ -76,6 +81,11 @@
 }
 void DnsQueryIp6FromName(char * name, int dnsProtocol, int ipProtocol)
 {
+    if (!name[0])
+    {
+        LogTime("DnsQueryIp6FromName called with no name\r\n");
+        return;
+    }
     DnsLabelMakeFullNameFromName(dnsProtocol, name, sizeof(DnsQueryName), DnsQueryName);
     DnsQueryIp4     = 0;
     DnsQueryIp6[0]  = 0;
@@ -87,6 +97,11 @@
 }
 void DnsQueryNameFromIp4(uint32_t ip, int dnsProtocol, int ipProtocol)
 {
+    if (!ip)
+    {
+        LogTime("DnsQueryNameFromIp4 called with no ip\r\n");
+        return;
+    }
     DnsQueryName[0] = 0;
     DnsQueryIp4     = ip;
     DnsQueryIp6[0]  = 0;
@@ -98,6 +113,11 @@
 }
 void DnsQueryNameFromIp6(char* ip, int dnsProtocol, int ipProtocol)
 {
+    if (!ip[0])
+    {
+        LogTime("DnsQueryNameFromIp6 called with no ip\r\n");
+        return;
+    }
     DnsQueryName[0] = 0;
     DnsQueryIp4     = 0;
     Ip6AddrCopy(DnsQueryIp6, ip);
@@ -157,9 +177,10 @@
     DnsHdrWrite();
     char* p = DnsHdrData;
     
-    if      (DnsQueryIp4   ) DnsNameEncodeIp4(DnsQueryIp4,  &p);
-    else if (DnsQueryIp6[0]) DnsNameEncodeIp6(DnsQueryIp6,  &p);
-    else                     DnsNameEncodePtr(DnsQueryName, &p);
+    if      (DnsQueryIp4    ) DnsNameEncodeIp4(DnsQueryIp4,  &p);
+    else if (DnsQueryIp6[0] ) DnsNameEncodeIp6(DnsQueryIp6,  &p);
+    else if (DnsQueryName[0]) DnsNameEncodePtr(DnsQueryName, &p);
+    else return DO_NOTHING;
     
     *p++ = 0;
     *p++ = _RecordType;