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 Nov 17 15:49:03 2018 +0000
Revision:
83:08c983006a6e
Parent:
69:6b3c648e1452
Child:
101:a677d8aee6dd
Added "send requests via IPv4" toggles for DNS, NTP and TFTP.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdint.h>
andrewboyson 61:aad055f1b0d1 2 #include <stdbool.h>
andrewboyson 61:aad055f1b0d1 3
andrewboyson 37:793b39683406 4 #include "log.h"
andrewboyson 37:793b39683406 5 #include "net.h"
andrewboyson 37:793b39683406 6 #include "action.h"
andrewboyson 49:1a6336f2b3f9 7 #include "ip6addr.h"
andrewboyson 37:793b39683406 8 #include "udp.h"
andrewboyson 48:952dddb74b8b 9 #include "ar4.h"
andrewboyson 48:952dddb74b8b 10 #include "ar6.h"
andrewboyson 37:793b39683406 11 #include "arp.h"
andrewboyson 37:793b39683406 12 #include "eth.h"
andrewboyson 50:492f2d2954e4 13 #include "nr4.h"
andrewboyson 50:492f2d2954e4 14 #include "nr6.h"
andrewboyson 37:793b39683406 15 #include "dns.h"
andrewboyson 37:793b39683406 16 #include "mac.h"
andrewboyson 22:914b970356f0 17
andrewboyson 83:08c983006a6e 18
andrewboyson 37:793b39683406 19 bool NtpTrace = false;
andrewboyson 0:faa09bd4e6bf 20
andrewboyson 0:faa09bd4e6bf 21 #define CLIENT 3
andrewboyson 0:faa09bd4e6bf 22 #define SERVER 4
andrewboyson 0:faa09bd4e6bf 23
andrewboyson 22:914b970356f0 24 uint64_t (*NtpGetClockNowFunction) ();
andrewboyson 22:914b970356f0 25
andrewboyson 22:914b970356f0 26 bool NtpServerEnable = false;
andrewboyson 22:914b970356f0 27 uint64_t (*NtpGetClockRefFunction) ();
andrewboyson 22:914b970356f0 28 int (*NtpGetClockStratumFunction)();
andrewboyson 22:914b970356f0 29 char * (*NtpGetClockIdentFunction) ();
andrewboyson 22:914b970356f0 30
andrewboyson 83:08c983006a6e 31 bool NtpSendRequestsViaIp4 = false;
andrewboyson 36:900e24b27bfb 32 uint32_t NtpServerIp4;
andrewboyson 36:900e24b27bfb 33 char NtpServerIp6[16];
andrewboyson 22:914b970356f0 34 bool NtpClientRequest = false;
andrewboyson 68:e8c002e5ee4b 35 bool NtpClientTrace = false;
andrewboyson 35:93c39d260a83 36 char NtpServerName[DNS_MAX_LABEL_LENGTH+1];
andrewboyson 35:93c39d260a83 37 void (*NtpSetClockFunction) (uint64_t ori, uint64_t rec);
andrewboyson 35:93c39d260a83 38
andrewboyson 61:aad055f1b0d1 39 __packed struct header
andrewboyson 61:aad055f1b0d1 40 {
andrewboyson 0:faa09bd4e6bf 41 unsigned Mode : 3;
andrewboyson 0:faa09bd4e6bf 42 unsigned VN : 3;
andrewboyson 0:faa09bd4e6bf 43 unsigned LI : 2;
andrewboyson 0:faa09bd4e6bf 44 uint8_t Stratum;
andrewboyson 0:faa09bd4e6bf 45 int8_t Poll;
andrewboyson 0:faa09bd4e6bf 46 int8_t Precision;
andrewboyson 0:faa09bd4e6bf 47 uint32_t RootDelay;
andrewboyson 0:faa09bd4e6bf 48 uint32_t Dispersion;
andrewboyson 0:faa09bd4e6bf 49 char RefIdentifier[4];
andrewboyson 0:faa09bd4e6bf 50
andrewboyson 0:faa09bd4e6bf 51 uint64_t RefTimeStamp;
andrewboyson 0:faa09bd4e6bf 52 uint64_t OriTimeStamp;
andrewboyson 0:faa09bd4e6bf 53 uint64_t RecTimeStamp;
andrewboyson 0:faa09bd4e6bf 54 uint64_t TraTimeStamp;
andrewboyson 0:faa09bd4e6bf 55 };
andrewboyson 37:793b39683406 56 static void logHeader(struct header* pHeader)
andrewboyson 37:793b39683406 57 {
andrewboyson 61:aad055f1b0d1 58 if (NetTraceVerbose) Log ("NTP header\r\n ");
andrewboyson 61:aad055f1b0d1 59 else Log ("NTP header: ");
andrewboyson 61:aad055f1b0d1 60
andrewboyson 61:aad055f1b0d1 61 LogF("Mode %d", pHeader->Mode);
andrewboyson 61:aad055f1b0d1 62 LogF(", Version %d", pHeader->VN);
andrewboyson 61:aad055f1b0d1 63 LogF(", LI %d", pHeader->LI);
andrewboyson 61:aad055f1b0d1 64 LogF(", Stratum %d", pHeader->Stratum);
andrewboyson 61:aad055f1b0d1 65 LogF(", Poll %d", pHeader->Poll);
andrewboyson 61:aad055f1b0d1 66 LogF(", Precision %d", pHeader->Precision);
andrewboyson 61:aad055f1b0d1 67 LogF(", Root delay %d", NetToHost32(pHeader->RootDelay));
andrewboyson 61:aad055f1b0d1 68 LogF(", Dispersion %d", NetToHost32(pHeader->Dispersion));
andrewboyson 61:aad055f1b0d1 69 Log (", Ident ");
andrewboyson 61:aad055f1b0d1 70 for (int i = 0; i < 4; i++) if (pHeader->RefIdentifier[i]) LogPush(pHeader->RefIdentifier[i]);
andrewboyson 61:aad055f1b0d1 71 Log ("\r\n");
andrewboyson 61:aad055f1b0d1 72
andrewboyson 43:bc028d5a6424 73 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 74 {
andrewboyson 43:bc028d5a6424 75 LogF(" REF %llu\r\n", NetToHost64(pHeader->RefTimeStamp));
andrewboyson 43:bc028d5a6424 76 LogF(" ORI %llu\r\n", NetToHost64(pHeader->OriTimeStamp));
andrewboyson 43:bc028d5a6424 77 LogF(" REC %llu\r\n", NetToHost64(pHeader->RecTimeStamp));
andrewboyson 43:bc028d5a6424 78 LogF(" TRA %llu\r\n", NetToHost64(pHeader->TraTimeStamp));
andrewboyson 43:bc028d5a6424 79 }
andrewboyson 37:793b39683406 80 }
andrewboyson 69:6b3c648e1452 81 static void (*pTraceBack)(void);
andrewboyson 59:e0e556c8bd46 82 static int handleRequest(struct header* pHeaderRx, struct header* pHeaderTx)
andrewboyson 0:faa09bd4e6bf 83 {
andrewboyson 22:914b970356f0 84 if (!NtpServerEnable) return DO_NOTHING;
andrewboyson 17:e475ab861365 85
andrewboyson 17:e475ab861365 86 if (!NtpGetClockRefFunction || !NtpGetClockNowFunction || !NtpGetClockStratumFunction || !NtpGetClockIdentFunction)
andrewboyson 17:e475ab861365 87 {
andrewboyson 17:e475ab861365 88 LogTimeF("NtpHandleRequest - NTP server is enabled but has not been plumbed into a clock\r\n");
andrewboyson 17:e475ab861365 89 return DO_NOTHING;
andrewboyson 17:e475ab861365 90 }
andrewboyson 69:6b3c648e1452 91
andrewboyson 69:6b3c648e1452 92 if (NtpTrace)
andrewboyson 69:6b3c648e1452 93 {
andrewboyson 69:6b3c648e1452 94 if (NetTraceNewLine) Log("\r\n");
andrewboyson 69:6b3c648e1452 95 LogTimeF("NTP received request\r\n");
andrewboyson 69:6b3c648e1452 96 if (NetTraceStack) pTraceBack();
andrewboyson 69:6b3c648e1452 97 logHeader(pHeaderRx);
andrewboyson 69:6b3c648e1452 98 }
andrewboyson 69:6b3c648e1452 99
andrewboyson 20:23f2b29b48ea 100 uint64_t refNtp = NtpGetClockRefFunction();
andrewboyson 20:23f2b29b48ea 101 uint64_t nowNtp = NtpGetClockNowFunction();
andrewboyson 20:23f2b29b48ea 102 int stratum = NtpGetClockStratumFunction();
andrewboyson 20:23f2b29b48ea 103 char* ident = NtpGetClockIdentFunction();
andrewboyson 22:914b970356f0 104
andrewboyson 59:e0e556c8bd46 105 pHeaderTx->Mode = SERVER;
andrewboyson 61:aad055f1b0d1 106 pHeaderTx->VN = 3;
andrewboyson 59:e0e556c8bd46 107 pHeaderTx->LI = 0;
andrewboyson 59:e0e556c8bd46 108 pHeaderTx->Stratum = stratum;
andrewboyson 59:e0e556c8bd46 109 pHeaderTx->Poll = 0;
andrewboyson 59:e0e556c8bd46 110 pHeaderTx->Precision = 0;
andrewboyson 59:e0e556c8bd46 111 pHeaderTx->RootDelay = 0;
andrewboyson 59:e0e556c8bd46 112 pHeaderTx->Dispersion = 0;
andrewboyson 59:e0e556c8bd46 113 pHeaderTx->RefIdentifier[0] = ident[0]; //For stratum 1 (reference clock), this is a four-octet, left-justified, zero-padded ASCII string.
andrewboyson 59:e0e556c8bd46 114 pHeaderTx->RefIdentifier[1] = ident[1];
andrewboyson 59:e0e556c8bd46 115 pHeaderTx->RefIdentifier[2] = ident[2];
andrewboyson 59:e0e556c8bd46 116 pHeaderTx->RefIdentifier[3] = ident[3];
andrewboyson 59:e0e556c8bd46 117 pHeaderTx->RefTimeStamp = NetToHost64(refNtp);
andrewboyson 59:e0e556c8bd46 118 pHeaderTx->OriTimeStamp = pHeaderRx->TraTimeStamp;
andrewboyson 59:e0e556c8bd46 119 pHeaderTx->RecTimeStamp = NetToHost64(nowNtp);
andrewboyson 59:e0e556c8bd46 120 pHeaderTx->TraTimeStamp = NetToHost64(nowNtp);
andrewboyson 69:6b3c648e1452 121
andrewboyson 69:6b3c648e1452 122 if (NtpTrace) logHeader(pHeaderTx);
andrewboyson 22:914b970356f0 123 return UNICAST;
andrewboyson 22:914b970356f0 124 }
andrewboyson 59:e0e556c8bd46 125 static void handleReply(struct header* pHeader)
andrewboyson 22:914b970356f0 126 {
andrewboyson 22:914b970356f0 127 if (!NtpGetClockNowFunction || !NtpSetClockFunction)
andrewboyson 22:914b970356f0 128 {
andrewboyson 22:914b970356f0 129 LogTimeF("Ntp reply has been received but NTP has not been plumbed into a clock\r\n");
andrewboyson 59:e0e556c8bd46 130 return;
andrewboyson 22:914b970356f0 131 }
andrewboyson 37:793b39683406 132 if (NtpTrace)
andrewboyson 22:914b970356f0 133 {
andrewboyson 43:bc028d5a6424 134 if (NetTraceNewLine) Log("\r\n");
andrewboyson 22:914b970356f0 135 LogTimeF("NTP received reply\r\n");
andrewboyson 43:bc028d5a6424 136 if (NetTraceStack) pTraceBack();
andrewboyson 37:793b39683406 137 logHeader(pHeader);
andrewboyson 22:914b970356f0 138 }
andrewboyson 37:793b39683406 139
andrewboyson 22:914b970356f0 140 uint64_t ori = NetToHost64(pHeader->OriTimeStamp);
andrewboyson 22:914b970356f0 141 uint64_t rec = NetToHost64(pHeader->RecTimeStamp);
andrewboyson 22:914b970356f0 142
andrewboyson 22:914b970356f0 143 NtpSetClockFunction(ori, rec);
andrewboyson 22:914b970356f0 144 }
andrewboyson 59:e0e556c8bd46 145 int NtpHandlePacketReceived(void (*traceback)(void), int sizeRx, void * pPacketRx, int* pSizeTx, void* pPacketTx)
andrewboyson 22:914b970356f0 146 {
andrewboyson 37:793b39683406 147 pTraceBack = traceback;
andrewboyson 37:793b39683406 148
andrewboyson 61:aad055f1b0d1 149 if (sizeRx != sizeof(struct header))
andrewboyson 22:914b970356f0 150 {
andrewboyson 59:e0e556c8bd46 151 LogTimeF("\r\nNTP packet wrong size %d\r\n", sizeRx);
andrewboyson 22:914b970356f0 152 return DO_NOTHING;
andrewboyson 22:914b970356f0 153 }
andrewboyson 59:e0e556c8bd46 154 struct header* pHeaderRx = (struct header*)pPacketRx;
andrewboyson 59:e0e556c8bd46 155 struct header* pHeaderTx = (struct header*)pPacketTx;
andrewboyson 17:e475ab861365 156
andrewboyson 59:e0e556c8bd46 157 int dest = DO_NOTHING;
andrewboyson 59:e0e556c8bd46 158 switch (pHeaderRx->Mode)
andrewboyson 0:faa09bd4e6bf 159 {
andrewboyson 59:e0e556c8bd46 160 case CLIENT: dest = handleRequest(pHeaderRx, pHeaderTx); break;
andrewboyson 59:e0e556c8bd46 161 case SERVER: handleReply (pHeaderRx); return DO_NOTHING;
andrewboyson 0:faa09bd4e6bf 162 default:
andrewboyson 59:e0e556c8bd46 163 LogTimeF("\r\nNTP packet unknown mode %d\r\n", pHeaderRx->Mode);
andrewboyson 22:914b970356f0 164 return DO_NOTHING;
andrewboyson 0:faa09bd4e6bf 165 }
andrewboyson 59:e0e556c8bd46 166
andrewboyson 59:e0e556c8bd46 167 if (dest == DO_NOTHING) return DO_NOTHING;
andrewboyson 61:aad055f1b0d1 168 *pSizeTx = sizeof(struct header);
andrewboyson 59:e0e556c8bd46 169
andrewboyson 59:e0e556c8bd46 170 return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack);
andrewboyson 22:914b970356f0 171 }
andrewboyson 22:914b970356f0 172 static int sendRequest(void* pPacket, int* pSize)
andrewboyson 22:914b970356f0 173 {
andrewboyson 22:914b970356f0 174 struct header* pHeader = (struct header*)pPacket;
andrewboyson 22:914b970356f0 175
andrewboyson 59:e0e556c8bd46 176 pHeader->Mode = CLIENT;
andrewboyson 69:6b3c648e1452 177 pHeader->VN = 3;
andrewboyson 59:e0e556c8bd46 178 pHeader->LI = 0;
andrewboyson 59:e0e556c8bd46 179 pHeader->Stratum = 0;
andrewboyson 59:e0e556c8bd46 180 pHeader->Poll = 0;
andrewboyson 59:e0e556c8bd46 181 pHeader->Precision = 0;
andrewboyson 59:e0e556c8bd46 182 pHeader->RootDelay = 0;
andrewboyson 59:e0e556c8bd46 183 pHeader->Dispersion = 0;
andrewboyson 22:914b970356f0 184 pHeader->RefIdentifier[0] = 0;
andrewboyson 22:914b970356f0 185 pHeader->RefIdentifier[1] = 0;
andrewboyson 22:914b970356f0 186 pHeader->RefIdentifier[2] = 0;
andrewboyson 22:914b970356f0 187 pHeader->RefIdentifier[3] = 0;
andrewboyson 59:e0e556c8bd46 188 pHeader->RefTimeStamp = 0;
andrewboyson 59:e0e556c8bd46 189 pHeader->OriTimeStamp = 0;
andrewboyson 59:e0e556c8bd46 190 pHeader->RecTimeStamp = 0;
andrewboyson 59:e0e556c8bd46 191 pHeader->TraTimeStamp = NetToHost64(NtpGetClockNowFunction());
andrewboyson 59:e0e556c8bd46 192
andrewboyson 61:aad055f1b0d1 193 *pSize = sizeof(struct header);
andrewboyson 22:914b970356f0 194
andrewboyson 22:914b970356f0 195 return UNICAST_NTP;
andrewboyson 0:faa09bd4e6bf 196 }
andrewboyson 36:900e24b27bfb 197 static bool resolve4(char* server, uint32_t* pIp)
andrewboyson 36:900e24b27bfb 198 {
andrewboyson 36:900e24b27bfb 199 //Check if have IP, if not, then request it and stop
andrewboyson 50:492f2d2954e4 200 Nr4NameToIp(server, pIp);
andrewboyson 36:900e24b27bfb 201 if (!*pIp)
andrewboyson 36:900e24b27bfb 202 {
andrewboyson 50:492f2d2954e4 203 Nr4MakeRequestForIpFromName(server); //The request is only repeated if made after a freeze time - call as often as you want.
andrewboyson 36:900e24b27bfb 204 return false;
andrewboyson 36:900e24b27bfb 205 }
andrewboyson 35:93c39d260a83 206
andrewboyson 36:900e24b27bfb 207 //Check if have MAC and, if not, request it and stop
andrewboyson 36:900e24b27bfb 208 char mac[6];
andrewboyson 48:952dddb74b8b 209 Ar4IpToMac(*pIp, mac);
andrewboyson 36:900e24b27bfb 210 if (MacIsEmpty(mac))
andrewboyson 36:900e24b27bfb 211 {
andrewboyson 48:952dddb74b8b 212 Ar4MakeRequestForMacFromIp(*pIp); //The request is only repeated if made after a freeze time - call as often as you want.
andrewboyson 36:900e24b27bfb 213 return false;
andrewboyson 36:900e24b27bfb 214 }
andrewboyson 36:900e24b27bfb 215
andrewboyson 36:900e24b27bfb 216 return true;
andrewboyson 36:900e24b27bfb 217 }
andrewboyson 36:900e24b27bfb 218 static bool resolve6(char* server, char* ip)
andrewboyson 36:900e24b27bfb 219 {
andrewboyson 36:900e24b27bfb 220 //Check if have IP, if not, then request it and stop
andrewboyson 50:492f2d2954e4 221 Nr6NameToIp(server, ip);
andrewboyson 49:1a6336f2b3f9 222 if (Ip6AddressIsEmpty(ip))
andrewboyson 36:900e24b27bfb 223 {
andrewboyson 50:492f2d2954e4 224 Nr6MakeRequestForIpFromName(server); //The request is only repeated if made after a freeze time - call as often as you want.
andrewboyson 36:900e24b27bfb 225 return false;
andrewboyson 36:900e24b27bfb 226 }
andrewboyson 36:900e24b27bfb 227
andrewboyson 36:900e24b27bfb 228 //Check if have MAC and, if not, request it and stop
andrewboyson 36:900e24b27bfb 229 char mac[6];
andrewboyson 48:952dddb74b8b 230 Ar6IpToMac(ip, mac);
andrewboyson 36:900e24b27bfb 231 if (MacIsEmpty(mac))
andrewboyson 36:900e24b27bfb 232 {
andrewboyson 48:952dddb74b8b 233 Ar6MakeRequestForMacFromIp(ip); //The request is only repeated if made after a freeze time - call as often as you want.
andrewboyson 36:900e24b27bfb 234 return false;
andrewboyson 36:900e24b27bfb 235 }
andrewboyson 36:900e24b27bfb 236
andrewboyson 36:900e24b27bfb 237 return true;
andrewboyson 36:900e24b27bfb 238 }
andrewboyson 22:914b970356f0 239 int NtpPollForPacketToSend(int type, void* pPacket, int* pSize)
andrewboyson 22:914b970356f0 240 {
andrewboyson 22:914b970356f0 241 if (!NtpClientRequest) return DO_NOTHING; //Wait until a request for time is made
andrewboyson 22:914b970356f0 242
andrewboyson 35:93c39d260a83 243 if (!NtpServerName[0])
andrewboyson 35:93c39d260a83 244 {
andrewboyson 35:93c39d260a83 245 LogTimeF("NtpPollForRequestToSend - A request to send a client message has been made but no server name has been specified\r\n");
andrewboyson 35:93c39d260a83 246 NtpClientRequest = false;
andrewboyson 35:93c39d260a83 247 return DO_NOTHING;
andrewboyson 35:93c39d260a83 248 }
andrewboyson 35:93c39d260a83 249
andrewboyson 35:93c39d260a83 250 if (!NtpGetClockNowFunction || !NtpSetClockFunction)
andrewboyson 22:914b970356f0 251 {
andrewboyson 22:914b970356f0 252 LogTimeF("NtpPollForRequestToSend - A request to send a client message has been made but NTP has not been plumbed into a clock\r\n");
andrewboyson 22:914b970356f0 253 NtpClientRequest = false;
andrewboyson 22:914b970356f0 254 return DO_NOTHING;
andrewboyson 22:914b970356f0 255 }
andrewboyson 42:222a4f45f916 256
andrewboyson 42:222a4f45f916 257 if (type == IPV4)
andrewboyson 42:222a4f45f916 258 {
andrewboyson 42:222a4f45f916 259 if (!resolve4(NtpServerName, &NtpServerIp4)) return DO_NOTHING;
andrewboyson 42:222a4f45f916 260 }
andrewboyson 42:222a4f45f916 261 else if (type == IPV6)
andrewboyson 42:222a4f45f916 262 {
andrewboyson 42:222a4f45f916 263 if (!resolve6(NtpServerName, NtpServerIp6)) return DO_NOTHING;
andrewboyson 42:222a4f45f916 264 }
andrewboyson 42:222a4f45f916 265 else
andrewboyson 42:222a4f45f916 266 {
andrewboyson 42:222a4f45f916 267 return DO_NOTHING;
andrewboyson 42:222a4f45f916 268 }
andrewboyson 35:93c39d260a83 269
andrewboyson 35:93c39d260a83 270 //Have IP and MAC so send request
andrewboyson 42:222a4f45f916 271 NtpClientRequest = false;
andrewboyson 42:222a4f45f916 272 int dest = sendRequest(pPacket, pSize);
andrewboyson 42:222a4f45f916 273 if (NtpTrace)
andrewboyson 37:793b39683406 274 {
andrewboyson 43:bc028d5a6424 275 if (NetTraceNewLine) Log("\r\n");
andrewboyson 37:793b39683406 276 LogTimeF("Sending NTP request\r\n");
andrewboyson 42:222a4f45f916 277 logHeader((struct header*)pPacket);
andrewboyson 37:793b39683406 278 }
andrewboyson 43:bc028d5a6424 279 return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack);
andrewboyson 22:914b970356f0 280 }
andrewboyson 22:914b970356f0 281