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
Diff: ip4/ip4.cpp
- Revision:
- 49:1a6336f2b3f9
- Parent:
- 48:952dddb74b8b
- Child:
- 50:492f2d2954e4
--- a/ip4/ip4.cpp Tue Oct 31 21:25:09 2017 +0000 +++ b/ip4/ip4.cpp Thu Nov 02 08:10:55 2017 +0000 @@ -9,70 +9,12 @@ #include "dhcp.h" #include "eth.h" #include "ip.h" -#include "ip4.h" +#include "ip4addr.h" #include "ntp.h" #include "mac.h" -#include "http-reply.h" bool Ip4Trace = true; -#define IP4_BROADCAST_ADDRESS 0xFFFFFFFF -#define IP4_MULTICAST_ALL_HOSTS 0x010000E0 -#define IP4_MULTICAST_ALL_ROUTERS 0x020000E0 -#define IP4_MULTICAST_DNS_ADDRESS 0xFB0000E0 -#define IP4_MULTICAST_LLMNR_ADDRESS 0xFC0000E0 - -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 Ip4Parse(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 Ip4DstIpFromDest(int dest, uint32_t* pDstIp) -{ - switch (dest) - { - case UNICAST: 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("Ip4DestIpFromDest unknown destination %d\r\n", dest); - break; - } -} - #define HEADER_LENGTH 20 __packed struct header