Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Revision:
176:7eb916c22084
--- /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);
+}