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 28 17:05:46 2017 +0000
Revision:
57:e0fb648acf48
Parent:
54:84ef2b29cf7e
Added TFTP

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