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:
49:1a6336f2b3f9
Child:
52:fbc5a46b5e16
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ip4/ip4addr.cpp	Thu Nov 02 08:10:55 2017 +0000
@@ -0,0 +1,58 @@
+#include "mbed.h"
+#include "log.h"
+#include "http.h"
+#include "action.h"
+#include "ip4addr.h"
+#include "dhcp.h"
+#include "ntp.h"
+
+int Ip4AddressToString(uint32_t ip, int size, char* text)
+{
+    int a0 = (ip & 0xFF000000) >> 24;
+    int a1 = (ip & 0x00FF0000) >> 16;
+    int a2 = (ip & 0x0000FF00) >>  8;
+    int a3 = (ip & 0x000000FF);
+    return snprintf(text, size, "%d.%d.%d.%d", a3, a2, a1, a0); 
+}
+int Ip4AddressLog(uint32_t ip)
+{
+    int a0 = (ip & 0xFF000000) >> 24;
+    int a1 = (ip & 0x00FF0000) >> 16;
+    int a2 = (ip & 0x0000FF00) >>  8;
+    int a3 = (ip & 0x000000FF);
+    return LogF("%d.%d.%d.%d", a3, a2, a1, a0); 
+}
+int Ip4AddressHttp(uint32_t ip)
+{
+    int a0 = (ip & 0xFF000000) >> 24;
+    int a1 = (ip & 0x00FF0000) >> 16;
+    int a2 = (ip & 0x0000FF00) >>  8;
+    int a3 = (ip & 0x000000FF);
+    return HttpReplyAddF("%d.%d.%d.%d", a3, a2, a1, a0); 
+}
+
+uint32_t Ip4AddressParse(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)
+{
+    switch (dest)
+    {
+        case UNICAST:          /*No change*/                          break;
+        case UNICAST_DNS:      *pDstIp = DhcpDnsServer;               break;
+        case UNICAST_DHCP:     *pDstIp = DhcpServer;                  break;
+        case UNICAST_NTP:      *pDstIp = NtpServerIp4;                break;
+        case MULTICAST_NODE:   *pDstIp = IP4_MULTICAST_ALL_HOSTS;     break;
+        case MULTICAST_ROUTER: *pDstIp = IP4_MULTICAST_ALL_ROUTERS;   break;
+        case MULTICAST_MDNS:   *pDstIp = IP4_MULTICAST_DNS_ADDRESS;   break;
+        case MULTICAST_LLMNR:  *pDstIp = IP4_MULTICAST_LLMNR_ADDRESS; break;
+        case BROADCAST:        *pDstIp = IP4_BROADCAST_ADDRESS;       break;
+        default:
+            LogTimeF("Ip4AddressDestIpFromDest unknown destination %d\r\n", dest);
+            break;
+    }
+}