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
udp/ntp/ntpclientquery.c@139:1f3641abc07f, 2019-04-08 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon Apr 08 10:53:19 2019 +0000
- Revision:
- 139:1f3641abc07f
- Parent:
- 138:5ff0c7069300
Checked NTP replies originated from a request
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 113:904b40231907 | 1 | #include <stdint.h> |
andrewboyson | 113:904b40231907 | 2 | #include <string.h> |
andrewboyson | 113:904b40231907 | 3 | #include <stdbool.h> |
andrewboyson | 113:904b40231907 | 4 | |
andrewboyson | 124:6e558721ec1c | 5 | #include "log.h" |
andrewboyson | 124:6e558721ec1c | 6 | #include "clk.h" |
andrewboyson | 124:6e558721ec1c | 7 | #include "mstimer.h" |
andrewboyson | 124:6e558721ec1c | 8 | #include "clktime.h" |
andrewboyson | 124:6e558721ec1c | 9 | #include "ntptimestamp.h" |
andrewboyson | 124:6e558721ec1c | 10 | #include "clkutc.h" |
andrewboyson | 124:6e558721ec1c | 11 | #include "clkgov.h" |
andrewboyson | 124:6e558721ec1c | 12 | #include "clktm.h" |
andrewboyson | 124:6e558721ec1c | 13 | #include "net.h" |
andrewboyson | 124:6e558721ec1c | 14 | #include "ntp.h" |
andrewboyson | 138:5ff0c7069300 | 15 | #include "ntphdr.h" |
andrewboyson | 113:904b40231907 | 16 | #include "ntpclient.h" |
andrewboyson | 124:6e558721ec1c | 17 | #include "dns.h" |
andrewboyson | 124:6e558721ec1c | 18 | #include "ip4.h" |
andrewboyson | 116:60521b29e4c9 | 19 | #include "resolve.h" |
andrewboyson | 113:904b40231907 | 20 | #include "action.h" |
andrewboyson | 128:79052cb4a41c | 21 | #include "dnslabel.h" |
andrewboyson | 113:904b40231907 | 22 | |
andrewboyson | 113:904b40231907 | 23 | #define ONE_BILLION 1000000000ULL |
andrewboyson | 113:904b40231907 | 24 | #define ONE_MILLION 1000000LL |
andrewboyson | 113:904b40231907 | 25 | |
andrewboyson | 113:904b40231907 | 26 | char NtpClientQueryServerName[DNS_MAX_LABEL_LENGTH+1]; |
andrewboyson | 113:904b40231907 | 27 | int32_t NtpClientQueryInitialInterval; |
andrewboyson | 113:904b40231907 | 28 | int32_t NtpClientQueryNormalInterval; |
andrewboyson | 113:904b40231907 | 29 | int32_t NtpClientQueryRetryInterval; |
andrewboyson | 113:904b40231907 | 30 | |
andrewboyson | 113:904b40231907 | 31 | bool NtpClientQuerySendRequestsViaIp4 = false; |
andrewboyson | 113:904b40231907 | 32 | uint32_t NtpClientQueryServerIp4; |
andrewboyson | 113:904b40231907 | 33 | char NtpClientQueryServerIp6[16]; |
andrewboyson | 113:904b40231907 | 34 | |
andrewboyson | 113:904b40231907 | 35 | static uint64_t startNtpMs = 0; |
andrewboyson | 113:904b40231907 | 36 | static int intervalTypeNtp = NTP_QUERY_INTERVAL_INITIAL; |
andrewboyson | 113:904b40231907 | 37 | static bool intervalCompleteNtp() |
andrewboyson | 113:904b40231907 | 38 | { |
andrewboyson | 113:904b40231907 | 39 | uint32_t interval; |
andrewboyson | 113:904b40231907 | 40 | switch(intervalTypeNtp) |
andrewboyson | 113:904b40231907 | 41 | { |
andrewboyson | 113:904b40231907 | 42 | case NTP_QUERY_INTERVAL_INITIAL: interval = NtpClientQueryInitialInterval; break; |
andrewboyson | 113:904b40231907 | 43 | case NTP_QUERY_INTERVAL_NORMAL: interval = NtpClientQueryNormalInterval; break; |
andrewboyson | 113:904b40231907 | 44 | case NTP_QUERY_INTERVAL_RETRY: interval = NtpClientQueryRetryInterval; break; |
andrewboyson | 113:904b40231907 | 45 | } |
andrewboyson | 133:a37eb35a03f1 | 46 | return MsTimerRelative(startNtpMs, interval * 1000); |
andrewboyson | 113:904b40231907 | 47 | } |
andrewboyson | 113:904b40231907 | 48 | void NtpClientQueryStartInterval(int type) |
andrewboyson | 113:904b40231907 | 49 | { |
andrewboyson | 113:904b40231907 | 50 | startNtpMs = MsTimerCount; |
andrewboyson | 113:904b40231907 | 51 | intervalTypeNtp = type; |
andrewboyson | 113:904b40231907 | 52 | } |
andrewboyson | 113:904b40231907 | 53 | |
andrewboyson | 138:5ff0c7069300 | 54 | void writeRequest(char* pPacket, int* pSize) |
andrewboyson | 138:5ff0c7069300 | 55 | { |
andrewboyson | 138:5ff0c7069300 | 56 | NtpHdrSetMode(pPacket, NTP_CLIENT); |
andrewboyson | 138:5ff0c7069300 | 57 | NtpHdrSetVersion (pPacket, 3); |
andrewboyson | 138:5ff0c7069300 | 58 | NtpHdrSetLI (pPacket, 0); |
andrewboyson | 138:5ff0c7069300 | 59 | NtpHdrSetStratum (pPacket, 0); |
andrewboyson | 138:5ff0c7069300 | 60 | NtpHdrSetPoll (pPacket, 0); |
andrewboyson | 138:5ff0c7069300 | 61 | NtpHdrSetPrecision (pPacket, 0); |
andrewboyson | 138:5ff0c7069300 | 62 | NtpHdrSetRootDelay (pPacket, 0); |
andrewboyson | 138:5ff0c7069300 | 63 | NtpHdrSetDispersion (pPacket, 0); |
andrewboyson | 138:5ff0c7069300 | 64 | char* p = NtpHdrPtrRefIdentifier(pPacket); |
andrewboyson | 138:5ff0c7069300 | 65 | *p++ = 0; |
andrewboyson | 138:5ff0c7069300 | 66 | *p++ = 0; |
andrewboyson | 138:5ff0c7069300 | 67 | *p++ = 0; |
andrewboyson | 138:5ff0c7069300 | 68 | *p = 0; |
andrewboyson | 138:5ff0c7069300 | 69 | NtpHdrSetRefTimeStamp(pPacket, 0); |
andrewboyson | 138:5ff0c7069300 | 70 | NtpHdrSetOriTimeStamp(pPacket, 0); |
andrewboyson | 138:5ff0c7069300 | 71 | NtpHdrSetRecTimeStamp(pPacket, 0); |
andrewboyson | 139:1f3641abc07f | 72 | NtpClientQueryTime = NtpTimeStampFromClkTime(ClkTimeGet()); //Record the time to filter replies; use the costly time |
andrewboyson | 139:1f3641abc07f | 73 | NtpHdrSetTraTimeStamp(pPacket, NtpClientQueryTime); |
andrewboyson | 113:904b40231907 | 74 | |
andrewboyson | 138:5ff0c7069300 | 75 | *pSize = NTP_HEADER_LENGTH; |
andrewboyson | 113:904b40231907 | 76 | } |
andrewboyson | 113:904b40231907 | 77 | |
andrewboyson | 138:5ff0c7069300 | 78 | int NtpClientQueryPoll(int type, char* pPacket, int* pSize) |
andrewboyson | 113:904b40231907 | 79 | { |
andrewboyson | 113:904b40231907 | 80 | if (NtpClientQueryServerName[0]) //An empty name means ntp client is not enabled |
andrewboyson | 113:904b40231907 | 81 | { |
andrewboyson | 113:904b40231907 | 82 | if (intervalCompleteNtp()) //Wait for the time out |
andrewboyson | 113:904b40231907 | 83 | { |
andrewboyson | 113:904b40231907 | 84 | bool isMulticast = NtpClientQueryServerName[0] == '*'; |
andrewboyson | 116:60521b29e4c9 | 85 | if (isMulticast || Resolve(NtpClientQueryServerName, type, &NtpClientQueryServerIp4, NtpClientQueryServerIp6)) |
andrewboyson | 113:904b40231907 | 86 | { |
andrewboyson | 113:904b40231907 | 87 | ClkGovIsReceivingTime = false; |
andrewboyson | 113:904b40231907 | 88 | NtpClientQueryStartInterval(NTP_QUERY_INTERVAL_RETRY); |
andrewboyson | 113:904b40231907 | 89 | writeRequest(pPacket, pSize); |
andrewboyson | 113:904b40231907 | 90 | |
andrewboyson | 113:904b40231907 | 91 | if (isMulticast) return MULTICAST_NTP; |
andrewboyson | 113:904b40231907 | 92 | else return UNICAST_NTP; |
andrewboyson | 113:904b40231907 | 93 | } |
andrewboyson | 113:904b40231907 | 94 | } |
andrewboyson | 113:904b40231907 | 95 | } |
andrewboyson | 113:904b40231907 | 96 | return DO_NOTHING; |
andrewboyson | 113:904b40231907 | 97 | } |