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
ip4/ip4addr.cpp
- Committer:
- andrewboyson
- Date:
- 2017-11-13
- Revision:
- 54:84ef2b29cf7e
- Parent:
- 52:fbc5a46b5e16
- Child:
- 57:e0fb648acf48
File content as of revision 54:84ef2b29cf7e:
#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 HttpAddF("%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("Ip4AddressFromDest unknown destination %d\r\n", dest); break; } }