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:
Sat Jan 16 18:34:32 2021 +0000
Revision:
186:24198369b198
Parent:
183:ee809769bf89
Child:
187:122fc1996c86
DNS ignored annoying replies from the Sky router saying that an address name was 'UNKNOWN' rather than that it didn't know.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdint.h>
andrewboyson 61:aad055f1b0d1 2 #include <stdio.h>
andrewboyson 61:aad055f1b0d1 3
andrewboyson 49:1a6336f2b3f9 4 #include "log.h"
andrewboyson 49:1a6336f2b3f9 5 #include "http.h"
andrewboyson 49:1a6336f2b3f9 6 #include "action.h"
andrewboyson 49:1a6336f2b3f9 7 #include "ip4addr.h"
andrewboyson 49:1a6336f2b3f9 8 #include "dhcp.h"
andrewboyson 112:f8694d0b8858 9 #include "ntpclient.h"
andrewboyson 57:e0fb648acf48 10 #include "tftp.h"
andrewboyson 49:1a6336f2b3f9 11
andrewboyson 176:7eb916c22084 12 int Ip4AddressToString(const uint32_t ip, const int size, char* text)
andrewboyson 49:1a6336f2b3f9 13 {
andrewboyson 49:1a6336f2b3f9 14 int a0 = (ip & 0xFF000000) >> 24;
andrewboyson 49:1a6336f2b3f9 15 int a1 = (ip & 0x00FF0000) >> 16;
andrewboyson 49:1a6336f2b3f9 16 int a2 = (ip & 0x0000FF00) >> 8;
andrewboyson 49:1a6336f2b3f9 17 int a3 = (ip & 0x000000FF);
andrewboyson 49:1a6336f2b3f9 18 return snprintf(text, size, "%d.%d.%d.%d", a3, a2, a1, a0);
andrewboyson 49:1a6336f2b3f9 19 }
andrewboyson 176:7eb916c22084 20 int Ip4AddressLog(const uint32_t ip)
andrewboyson 49:1a6336f2b3f9 21 {
andrewboyson 49:1a6336f2b3f9 22 int a0 = (ip & 0xFF000000) >> 24;
andrewboyson 49:1a6336f2b3f9 23 int a1 = (ip & 0x00FF0000) >> 16;
andrewboyson 49:1a6336f2b3f9 24 int a2 = (ip & 0x0000FF00) >> 8;
andrewboyson 49:1a6336f2b3f9 25 int a3 = (ip & 0x000000FF);
andrewboyson 49:1a6336f2b3f9 26 return LogF("%d.%d.%d.%d", a3, a2, a1, a0);
andrewboyson 49:1a6336f2b3f9 27 }
andrewboyson 176:7eb916c22084 28 int Ip4AddressHttp(const uint32_t ip)
andrewboyson 49:1a6336f2b3f9 29 {
andrewboyson 49:1a6336f2b3f9 30 int a0 = (ip & 0xFF000000) >> 24;
andrewboyson 49:1a6336f2b3f9 31 int a1 = (ip & 0x00FF0000) >> 16;
andrewboyson 49:1a6336f2b3f9 32 int a2 = (ip & 0x0000FF00) >> 8;
andrewboyson 49:1a6336f2b3f9 33 int a3 = (ip & 0x000000FF);
andrewboyson 54:84ef2b29cf7e 34 return HttpAddF("%d.%d.%d.%d", a3, a2, a1, a0);
andrewboyson 49:1a6336f2b3f9 35 }
andrewboyson 49:1a6336f2b3f9 36
andrewboyson 176:7eb916c22084 37 uint32_t Ip4AddressParse(const char* text)
andrewboyson 49:1a6336f2b3f9 38 {
andrewboyson 49:1a6336f2b3f9 39 int ints[4];
andrewboyson 49:1a6336f2b3f9 40 sscanf(text, "%d.%d.%d.%d", &ints[3], &ints[2], &ints[1], &ints[0]);
andrewboyson 49:1a6336f2b3f9 41 return (ints[0] << 24) + (ints[1] << 16) + (ints[2] << 8) + ints[3];
andrewboyson 49:1a6336f2b3f9 42 }
andrewboyson 49:1a6336f2b3f9 43
andrewboyson 176:7eb916c22084 44 void Ip4AddressFromDest(const int dest, uint32_t* pDstIp)
andrewboyson 49:1a6336f2b3f9 45 {
andrewboyson 49:1a6336f2b3f9 46 switch (dest)
andrewboyson 49:1a6336f2b3f9 47 {
andrewboyson 49:1a6336f2b3f9 48 case UNICAST: /*No change*/ break;
andrewboyson 116:60521b29e4c9 49 case UNICAST_DNS: *pDstIp = DhcpDnsServerIp; break;
andrewboyson 116:60521b29e4c9 50 case UNICAST_DHCP: *pDstIp = DhcpServerIp; break;
andrewboyson 113:904b40231907 51 case UNICAST_NTP: *pDstIp = NtpClientQueryServerIp4; break;
andrewboyson 57:e0fb648acf48 52 case UNICAST_TFTP: *pDstIp = TftpServerIp4; break;
andrewboyson 49:1a6336f2b3f9 53 case MULTICAST_NODE: *pDstIp = IP4_MULTICAST_ALL_HOSTS; break;
andrewboyson 49:1a6336f2b3f9 54 case MULTICAST_ROUTER: *pDstIp = IP4_MULTICAST_ALL_ROUTERS; break;
andrewboyson 49:1a6336f2b3f9 55 case MULTICAST_MDNS: *pDstIp = IP4_MULTICAST_DNS_ADDRESS; break;
andrewboyson 49:1a6336f2b3f9 56 case MULTICAST_LLMNR: *pDstIp = IP4_MULTICAST_LLMNR_ADDRESS; break;
andrewboyson 107:cc58b4c2090b 57 case MULTICAST_NTP: *pDstIp = IP4_MULTICAST_NTP_ADDRESS; break;
andrewboyson 49:1a6336f2b3f9 58 case BROADCAST: *pDstIp = IP4_BROADCAST_ADDRESS; break;
andrewboyson 49:1a6336f2b3f9 59 default:
andrewboyson 52:fbc5a46b5e16 60 LogTimeF("Ip4AddressFromDest unknown destination %d\r\n", dest);
andrewboyson 49:1a6336f2b3f9 61 break;
andrewboyson 49:1a6336f2b3f9 62 }
andrewboyson 49:1a6336f2b3f9 63 }
andrewboyson 183:ee809769bf89 64 bool Ip4AddrIsRoutable(uint32_t ip)
andrewboyson 183:ee809769bf89 65 {
andrewboyson 183:ee809769bf89 66 if ((ip & DhcpSubnetMask) == (DhcpLocalIp & DhcpSubnetMask)) return false; // Ip is same as local ip in the unmasked area
andrewboyson 183:ee809769bf89 67 if ( ip == (DhcpLocalIp | 0xFF000000) ) return false; // Ip == 192.168.0.255; '|' is lower precendence than '=='
andrewboyson 183:ee809769bf89 68 if ( ip == IP4_BROADCAST_ADDRESS ) return false; // dstIp == 255.255.255.255
andrewboyson 183:ee809769bf89 69 if ((ip & 0xE0) == 0xE0 ) return false; // 224.x.x.x == 1110 0000 == E0.xx.xx.xx == xx.xx.xx.E0 in little endian
andrewboyson 186:24198369b198 70
andrewboyson 183:ee809769bf89 71 return true;
andrewboyson 183:ee809769bf89 72 }