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:
Wed Dec 05 18:30:37 2018 +0000
Revision:
94:e2973a2c488e
Parent:
61:aad055f1b0d1
Child:
107:cc58b4c2090b
Fixed bug - incorrect MSS being sent from a polled sync: expected 1440 but had -60. Traced to buffer datalength in EthPollForPacketToSend being set to zero instead of being calculated from the buffer length - headersize.

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 49:1a6336f2b3f9 9 #include "ntp.h"
andrewboyson 57:e0fb648acf48 10 #include "tftp.h"
andrewboyson 49:1a6336f2b3f9 11
andrewboyson 49:1a6336f2b3f9 12 int Ip4AddressToString(uint32_t ip, 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 49:1a6336f2b3f9 20 int Ip4AddressLog(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 49:1a6336f2b3f9 28 int Ip4AddressHttp(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 49:1a6336f2b3f9 37 uint32_t Ip4AddressParse(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 49:1a6336f2b3f9 44 void Ip4AddressFromDest(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 49:1a6336f2b3f9 49 case UNICAST_DNS: *pDstIp = DhcpDnsServer; break;
andrewboyson 49:1a6336f2b3f9 50 case UNICAST_DHCP: *pDstIp = DhcpServer; break;
andrewboyson 49:1a6336f2b3f9 51 case UNICAST_NTP: *pDstIp = NtpServerIp4; 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 49:1a6336f2b3f9 57 case BROADCAST: *pDstIp = IP4_BROADCAST_ADDRESS; break;
andrewboyson 49:1a6336f2b3f9 58 default:
andrewboyson 52:fbc5a46b5e16 59 LogTimeF("Ip4AddressFromDest unknown destination %d\r\n", dest);
andrewboyson 49:1a6336f2b3f9 60 break;
andrewboyson 49:1a6336f2b3f9 61 }
andrewboyson 49:1a6336f2b3f9 62 }