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 Dec 14 20:55:40 2017 +0000
Revision:
59:e0e556c8bd46
Parent:
50:492f2d2954e4
Added buffer length to help avoid over runs

Who changed what in which revision?

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