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:
Thu May 20 14:32:52 2021 +0000
Revision:
200:5acbc41bf469
Parent:
195:bd5b123143ca
Increased number of arp entries from 20 to 30 to accommodate the number of WIZ devices plus a few incoming port 80 calls from the internet.

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 195:bd5b123143ca 11 #include "user.h"
andrewboyson 49:1a6336f2b3f9 12
andrewboyson 187:122fc1996c86 13 int Ip4AddrToString(const uint32_t ip, const int size, char* text)
andrewboyson 49:1a6336f2b3f9 14 {
andrewboyson 49:1a6336f2b3f9 15 int a0 = (ip & 0xFF000000) >> 24;
andrewboyson 49:1a6336f2b3f9 16 int a1 = (ip & 0x00FF0000) >> 16;
andrewboyson 49:1a6336f2b3f9 17 int a2 = (ip & 0x0000FF00) >> 8;
andrewboyson 49:1a6336f2b3f9 18 int a3 = (ip & 0x000000FF);
andrewboyson 49:1a6336f2b3f9 19 return snprintf(text, size, "%d.%d.%d.%d", a3, a2, a1, a0);
andrewboyson 49:1a6336f2b3f9 20 }
andrewboyson 187:122fc1996c86 21 int Ip4AddrLog(const uint32_t ip)
andrewboyson 49:1a6336f2b3f9 22 {
andrewboyson 49:1a6336f2b3f9 23 int a0 = (ip & 0xFF000000) >> 24;
andrewboyson 49:1a6336f2b3f9 24 int a1 = (ip & 0x00FF0000) >> 16;
andrewboyson 49:1a6336f2b3f9 25 int a2 = (ip & 0x0000FF00) >> 8;
andrewboyson 49:1a6336f2b3f9 26 int a3 = (ip & 0x000000FF);
andrewboyson 49:1a6336f2b3f9 27 return LogF("%d.%d.%d.%d", a3, a2, a1, a0);
andrewboyson 49:1a6336f2b3f9 28 }
andrewboyson 187:122fc1996c86 29 int Ip4AddrHttp(const uint32_t ip)
andrewboyson 49:1a6336f2b3f9 30 {
andrewboyson 49:1a6336f2b3f9 31 int a0 = (ip & 0xFF000000) >> 24;
andrewboyson 49:1a6336f2b3f9 32 int a1 = (ip & 0x00FF0000) >> 16;
andrewboyson 49:1a6336f2b3f9 33 int a2 = (ip & 0x0000FF00) >> 8;
andrewboyson 49:1a6336f2b3f9 34 int a3 = (ip & 0x000000FF);
andrewboyson 54:84ef2b29cf7e 35 return HttpAddF("%d.%d.%d.%d", a3, a2, a1, a0);
andrewboyson 49:1a6336f2b3f9 36 }
andrewboyson 49:1a6336f2b3f9 37
andrewboyson 193:47a953ab571b 38 uint32_t Ip4AddrParse(const char* pText) //Returns 0 on error
andrewboyson 49:1a6336f2b3f9 39 {
andrewboyson 193:47a953ab571b 40 const char* p = pText;
andrewboyson 49:1a6336f2b3f9 41 int ints[4];
andrewboyson 193:47a953ab571b 42 int field = 0;
andrewboyson 193:47a953ab571b 43 int word = 0;
andrewboyson 193:47a953ab571b 44 while(true)
andrewboyson 193:47a953ab571b 45 {
andrewboyson 193:47a953ab571b 46 switch (*p)
andrewboyson 193:47a953ab571b 47 {
andrewboyson 193:47a953ab571b 48 case '.':
andrewboyson 193:47a953ab571b 49 ints[field] = word;
andrewboyson 193:47a953ab571b 50 field++;
andrewboyson 193:47a953ab571b 51 if (field > 3) return 0;
andrewboyson 193:47a953ab571b 52 word = 0;
andrewboyson 193:47a953ab571b 53 break;
andrewboyson 193:47a953ab571b 54 case '0': word *= 10; word += 0; break;
andrewboyson 193:47a953ab571b 55 case '1': word *= 10; word += 1; break;
andrewboyson 193:47a953ab571b 56 case '2': word *= 10; word += 2; break;
andrewboyson 193:47a953ab571b 57 case '3': word *= 10; word += 3; break;
andrewboyson 193:47a953ab571b 58 case '4': word *= 10; word += 4; break;
andrewboyson 193:47a953ab571b 59 case '5': word *= 10; word += 5; break;
andrewboyson 193:47a953ab571b 60 case '6': word *= 10; word += 6; break;
andrewboyson 193:47a953ab571b 61 case '7': word *= 10; word += 7; break;
andrewboyson 193:47a953ab571b 62 case '8': word *= 10; word += 8; break;
andrewboyson 193:47a953ab571b 63 case '9': word *= 10; word += 9; break;
andrewboyson 193:47a953ab571b 64 case 0:
andrewboyson 193:47a953ab571b 65 ints[field] = word;
andrewboyson 193:47a953ab571b 66 uint32_t addr4 = (ints[0] << 0) + (ints[1] << 8) + (ints[2] << 16) + (ints[3] << 24);
andrewboyson 193:47a953ab571b 67 return addr4;
andrewboyson 193:47a953ab571b 68 default: return 0;
andrewboyson 193:47a953ab571b 69 }
andrewboyson 193:47a953ab571b 70 p++;
andrewboyson 193:47a953ab571b 71 }
andrewboyson 49:1a6336f2b3f9 72 }
andrewboyson 187:122fc1996c86 73 void Ip4AddrFromDest(const int dest, uint32_t* pDstIp)
andrewboyson 49:1a6336f2b3f9 74 {
andrewboyson 49:1a6336f2b3f9 75 switch (dest)
andrewboyson 49:1a6336f2b3f9 76 {
andrewboyson 49:1a6336f2b3f9 77 case UNICAST: /*No change*/ break;
andrewboyson 116:60521b29e4c9 78 case UNICAST_DNS: *pDstIp = DhcpDnsServerIp; break;
andrewboyson 116:60521b29e4c9 79 case UNICAST_DHCP: *pDstIp = DhcpServerIp; break;
andrewboyson 113:904b40231907 80 case UNICAST_NTP: *pDstIp = NtpClientQueryServerIp4; break;
andrewboyson 57:e0fb648acf48 81 case UNICAST_TFTP: *pDstIp = TftpServerIp4; break;
andrewboyson 195:bd5b123143ca 82 case UNICAST_USER: *pDstIp = UserIp4; break;
andrewboyson 49:1a6336f2b3f9 83 case MULTICAST_NODE: *pDstIp = IP4_MULTICAST_ALL_HOSTS; break;
andrewboyson 49:1a6336f2b3f9 84 case MULTICAST_ROUTER: *pDstIp = IP4_MULTICAST_ALL_ROUTERS; break;
andrewboyson 49:1a6336f2b3f9 85 case MULTICAST_MDNS: *pDstIp = IP4_MULTICAST_DNS_ADDRESS; break;
andrewboyson 49:1a6336f2b3f9 86 case MULTICAST_LLMNR: *pDstIp = IP4_MULTICAST_LLMNR_ADDRESS; break;
andrewboyson 107:cc58b4c2090b 87 case MULTICAST_NTP: *pDstIp = IP4_MULTICAST_NTP_ADDRESS; break;
andrewboyson 49:1a6336f2b3f9 88 case BROADCAST: *pDstIp = IP4_BROADCAST_ADDRESS; break;
andrewboyson 49:1a6336f2b3f9 89 default:
andrewboyson 195:bd5b123143ca 90 LogTimeF("Ip4AddrFromDest unknown destination %d\r\n", dest);
andrewboyson 49:1a6336f2b3f9 91 break;
andrewboyson 49:1a6336f2b3f9 92 }
andrewboyson 49:1a6336f2b3f9 93 }
andrewboyson 187:122fc1996c86 94 bool Ip4AddrIsExternal(uint32_t ip)
andrewboyson 187:122fc1996c86 95 //Logic is if it isn't local and it isn't one of the three types of broadcast then it must be external.
andrewboyson 183:ee809769bf89 96 {
andrewboyson 183:ee809769bf89 97 if ((ip & DhcpSubnetMask) == (DhcpLocalIp & DhcpSubnetMask)) return false; // Ip is same as local ip in the unmasked area
andrewboyson 183:ee809769bf89 98 if ( ip == (DhcpLocalIp | 0xFF000000) ) return false; // Ip == 192.168.0.255; '|' is lower precendence than '=='
andrewboyson 183:ee809769bf89 99 if ( ip == IP4_BROADCAST_ADDRESS ) return false; // dstIp == 255.255.255.255
andrewboyson 183:ee809769bf89 100 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 101
andrewboyson 183:ee809769bf89 102 return true;
andrewboyson 183:ee809769bf89 103 }