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

Committer:
andrewboyson
Date:
Tue Nov 14 17:43:08 2017 +0000
Revision:
56:35117a8b5c65
Parent:
54:84ef2b29cf7e
Child:
57:e0fb648acf48
Corrected closing FIN requirement

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 49:1a6336f2b3f9 1 #include "mbed.h"
andrewboyson 49:1a6336f2b3f9 2 #include "log.h"
andrewboyson 49:1a6336f2b3f9 3 #include "http.h"
andrewboyson 49:1a6336f2b3f9 4 #include "action.h"
andrewboyson 49:1a6336f2b3f9 5 #include "ip4addr.h"
andrewboyson 49:1a6336f2b3f9 6 #include "dhcp.h"
andrewboyson 49:1a6336f2b3f9 7 #include "ntp.h"
andrewboyson 49:1a6336f2b3f9 8
andrewboyson 49:1a6336f2b3f9 9 int Ip4AddressToString(uint32_t ip, int size, char* text)
andrewboyson 49:1a6336f2b3f9 10 {
andrewboyson 49:1a6336f2b3f9 11 int a0 = (ip & 0xFF000000) >> 24;
andrewboyson 49:1a6336f2b3f9 12 int a1 = (ip & 0x00FF0000) >> 16;
andrewboyson 49:1a6336f2b3f9 13 int a2 = (ip & 0x0000FF00) >> 8;
andrewboyson 49:1a6336f2b3f9 14 int a3 = (ip & 0x000000FF);
andrewboyson 49:1a6336f2b3f9 15 return snprintf(text, size, "%d.%d.%d.%d", a3, a2, a1, a0);
andrewboyson 49:1a6336f2b3f9 16 }
andrewboyson 49:1a6336f2b3f9 17 int Ip4AddressLog(uint32_t ip)
andrewboyson 49:1a6336f2b3f9 18 {
andrewboyson 49:1a6336f2b3f9 19 int a0 = (ip & 0xFF000000) >> 24;
andrewboyson 49:1a6336f2b3f9 20 int a1 = (ip & 0x00FF0000) >> 16;
andrewboyson 49:1a6336f2b3f9 21 int a2 = (ip & 0x0000FF00) >> 8;
andrewboyson 49:1a6336f2b3f9 22 int a3 = (ip & 0x000000FF);
andrewboyson 49:1a6336f2b3f9 23 return LogF("%d.%d.%d.%d", a3, a2, a1, a0);
andrewboyson 49:1a6336f2b3f9 24 }
andrewboyson 49:1a6336f2b3f9 25 int Ip4AddressHttp(uint32_t ip)
andrewboyson 49:1a6336f2b3f9 26 {
andrewboyson 49:1a6336f2b3f9 27 int a0 = (ip & 0xFF000000) >> 24;
andrewboyson 49:1a6336f2b3f9 28 int a1 = (ip & 0x00FF0000) >> 16;
andrewboyson 49:1a6336f2b3f9 29 int a2 = (ip & 0x0000FF00) >> 8;
andrewboyson 49:1a6336f2b3f9 30 int a3 = (ip & 0x000000FF);
andrewboyson 54:84ef2b29cf7e 31 return HttpAddF("%d.%d.%d.%d", a3, a2, a1, a0);
andrewboyson 49:1a6336f2b3f9 32 }
andrewboyson 49:1a6336f2b3f9 33
andrewboyson 49:1a6336f2b3f9 34 uint32_t Ip4AddressParse(char* text)
andrewboyson 49:1a6336f2b3f9 35 {
andrewboyson 49:1a6336f2b3f9 36 int ints[4];
andrewboyson 49:1a6336f2b3f9 37 sscanf(text, "%d.%d.%d.%d", &ints[3], &ints[2], &ints[1], &ints[0]);
andrewboyson 49:1a6336f2b3f9 38 return (ints[0] << 24) + (ints[1] << 16) + (ints[2] << 8) + ints[3];
andrewboyson 49:1a6336f2b3f9 39 }
andrewboyson 49:1a6336f2b3f9 40
andrewboyson 49:1a6336f2b3f9 41 void Ip4AddressFromDest(int dest, uint32_t* pDstIp)
andrewboyson 49:1a6336f2b3f9 42 {
andrewboyson 49:1a6336f2b3f9 43 switch (dest)
andrewboyson 49:1a6336f2b3f9 44 {
andrewboyson 49:1a6336f2b3f9 45 case UNICAST: /*No change*/ break;
andrewboyson 49:1a6336f2b3f9 46 case UNICAST_DNS: *pDstIp = DhcpDnsServer; break;
andrewboyson 49:1a6336f2b3f9 47 case UNICAST_DHCP: *pDstIp = DhcpServer; break;
andrewboyson 49:1a6336f2b3f9 48 case UNICAST_NTP: *pDstIp = NtpServerIp4; break;
andrewboyson 49:1a6336f2b3f9 49 case MULTICAST_NODE: *pDstIp = IP4_MULTICAST_ALL_HOSTS; break;
andrewboyson 49:1a6336f2b3f9 50 case MULTICAST_ROUTER: *pDstIp = IP4_MULTICAST_ALL_ROUTERS; break;
andrewboyson 49:1a6336f2b3f9 51 case MULTICAST_MDNS: *pDstIp = IP4_MULTICAST_DNS_ADDRESS; break;
andrewboyson 49:1a6336f2b3f9 52 case MULTICAST_LLMNR: *pDstIp = IP4_MULTICAST_LLMNR_ADDRESS; break;
andrewboyson 49:1a6336f2b3f9 53 case BROADCAST: *pDstIp = IP4_BROADCAST_ADDRESS; break;
andrewboyson 49:1a6336f2b3f9 54 default:
andrewboyson 52:fbc5a46b5e16 55 LogTimeF("Ip4AddressFromDest unknown destination %d\r\n", dest);
andrewboyson 49:1a6336f2b3f9 56 break;
andrewboyson 49:1a6336f2b3f9 57 }
andrewboyson 49:1a6336f2b3f9 58 }