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 Jan 08 15:06:07 2019 +0000
Revision:
101:a677d8aee6dd
Parent:
83:08c983006a6e
Child:
102:a53ab3f0456b
Added links to get and set the NTP LI flags

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