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/ntp.c@200:5acbc41bf469, 2021-05-20 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu May 20 14:32:52 2021 +0000
- Revision:
- 200:5acbc41bf469
- Parent:
- 142:a8c0890a58d1
Increased number of arp entries from 20 to 30 to accommodate the number of WIZ devices plus a few incoming port 80 calls from the internet.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdint.h> |
andrewboyson | 61:aad055f1b0d1 | 2 | #include <stdbool.h> |
andrewboyson | 61:aad055f1b0d1 | 3 | |
andrewboyson | 121:bc048b65a630 | 4 | #include "log.h" |
andrewboyson | 121:bc048b65a630 | 5 | #include "net.h" |
andrewboyson | 37:793b39683406 | 6 | #include "action.h" |
andrewboyson | 121:bc048b65a630 | 7 | #include "udp.h" |
andrewboyson | 112:f8694d0b8858 | 8 | #include "ntpclient.h" |
andrewboyson | 113:904b40231907 | 9 | #include "ntpserver.h" |
andrewboyson | 112:f8694d0b8858 | 10 | #include "clktime.h" |
andrewboyson | 113:904b40231907 | 11 | #include "ntp.h" |
andrewboyson | 138:5ff0c7069300 | 12 | #include "ntphdr.h" |
andrewboyson | 142:a8c0890a58d1 | 13 | #include "restart.h" |
andrewboyson | 22:914b970356f0 | 14 | |
andrewboyson | 37:793b39683406 | 15 | bool NtpTrace = false; |
andrewboyson | 0:faa09bd4e6bf | 16 | |
andrewboyson | 113:904b40231907 | 17 | void NtpInit() |
andrewboyson | 61:aad055f1b0d1 | 18 | { |
andrewboyson | 113:904b40231907 | 19 | NtpClientInit(); |
andrewboyson | 113:904b40231907 | 20 | } |
andrewboyson | 113:904b40231907 | 21 | |
andrewboyson | 138:5ff0c7069300 | 22 | void NtpLogHeader(char* pPacket) |
andrewboyson | 37:793b39683406 | 23 | { |
andrewboyson | 61:aad055f1b0d1 | 24 | if (NetTraceVerbose) Log ("NTP header\r\n "); |
andrewboyson | 61:aad055f1b0d1 | 25 | else Log ("NTP header: "); |
andrewboyson | 61:aad055f1b0d1 | 26 | |
andrewboyson | 138:5ff0c7069300 | 27 | LogF("Mode %d", NtpHdrGetMode(pPacket)); |
andrewboyson | 138:5ff0c7069300 | 28 | LogF(", Version %d", NtpHdrGetVersion(pPacket)); |
andrewboyson | 138:5ff0c7069300 | 29 | LogF(", LI %d", NtpHdrGetLI(pPacket)); |
andrewboyson | 138:5ff0c7069300 | 30 | LogF(", Stratum %d", NtpHdrGetStratum(pPacket)); |
andrewboyson | 138:5ff0c7069300 | 31 | LogF(", Poll %d", NtpHdrGetPoll(pPacket)); |
andrewboyson | 138:5ff0c7069300 | 32 | LogF(", Precision %d", NtpHdrGetPrecision(pPacket)); |
andrewboyson | 138:5ff0c7069300 | 33 | LogF(", Root delay %d", NtpHdrGetRootDelay(pPacket)); |
andrewboyson | 138:5ff0c7069300 | 34 | LogF(", Dispersion %d", NtpHdrGetDispersion(pPacket)); |
andrewboyson | 61:aad055f1b0d1 | 35 | Log (", Ident "); |
andrewboyson | 138:5ff0c7069300 | 36 | for (int i = 0; i < 4; i++) if (NtpHdrPtrRefIdentifier(pPacket)[i]) LogChar(NtpHdrPtrRefIdentifier(pPacket)[i]); |
andrewboyson | 61:aad055f1b0d1 | 37 | Log ("\r\n"); |
andrewboyson | 61:aad055f1b0d1 | 38 | |
andrewboyson | 43:bc028d5a6424 | 39 | if (NetTraceVerbose) |
andrewboyson | 43:bc028d5a6424 | 40 | { |
andrewboyson | 138:5ff0c7069300 | 41 | LogF(" REF %llu\r\n", NtpHdrGetRefTimeStamp(pPacket)); |
andrewboyson | 138:5ff0c7069300 | 42 | LogF(" ORI %llu\r\n", NtpHdrGetOriTimeStamp(pPacket)); |
andrewboyson | 138:5ff0c7069300 | 43 | LogF(" REC %llu\r\n", NtpHdrGetRecTimeStamp(pPacket)); |
andrewboyson | 138:5ff0c7069300 | 44 | LogF(" TRA %llu\r\n", NtpHdrGetTraTimeStamp(pPacket)); |
andrewboyson | 43:bc028d5a6424 | 45 | } |
andrewboyson | 37:793b39683406 | 46 | } |
andrewboyson | 112:f8694d0b8858 | 47 | |
andrewboyson | 138:5ff0c7069300 | 48 | int NtpHandlePacketReceived(void (*traceback)(void), int sizeRx, char* pPacketRx, int* pSizeTx, char* pPacketTx) |
andrewboyson | 22:914b970356f0 | 49 | { |
andrewboyson | 142:a8c0890a58d1 | 50 | int lastRestartPoint = RestartPoint; |
andrewboyson | 142:a8c0890a58d1 | 51 | RestartPoint = FAULT_POINT_NtpHandlePacketReceived; |
andrewboyson | 121:bc048b65a630 | 52 | |
andrewboyson | 138:5ff0c7069300 | 53 | if (sizeRx != NTP_HEADER_LENGTH) |
andrewboyson | 22:914b970356f0 | 54 | { |
andrewboyson | 59:e0e556c8bd46 | 55 | LogTimeF("\r\nNTP packet wrong size %d\r\n", sizeRx); |
andrewboyson | 22:914b970356f0 | 56 | return DO_NOTHING; |
andrewboyson | 22:914b970356f0 | 57 | } |
andrewboyson | 17:e475ab861365 | 58 | |
andrewboyson | 59:e0e556c8bd46 | 59 | int dest = DO_NOTHING; |
andrewboyson | 138:5ff0c7069300 | 60 | switch (NtpHdrGetMode(pPacketRx)) |
andrewboyson | 0:faa09bd4e6bf | 61 | { |
andrewboyson | 138:5ff0c7069300 | 62 | case NTP_CLIENT: dest = NtpServerRequest(traceback, pPacketRx, pPacketTx); break; |
andrewboyson | 138:5ff0c7069300 | 63 | case NTP_SERVER: NtpClientReply (traceback, pPacketRx); break; |
andrewboyson | 138:5ff0c7069300 | 64 | default: LogTimeF("\r\nNTP packet unknown mode %d\r\n", NtpHdrGetMode(pPacketRx)); break; |
andrewboyson | 0:faa09bd4e6bf | 65 | } |
andrewboyson | 59:e0e556c8bd46 | 66 | |
andrewboyson | 142:a8c0890a58d1 | 67 | RestartPoint = lastRestartPoint; |
andrewboyson | 121:bc048b65a630 | 68 | if (dest == DO_NOTHING) |
andrewboyson | 121:bc048b65a630 | 69 | { |
andrewboyson | 121:bc048b65a630 | 70 | return DO_NOTHING; |
andrewboyson | 121:bc048b65a630 | 71 | } |
andrewboyson | 121:bc048b65a630 | 72 | else |
andrewboyson | 121:bc048b65a630 | 73 | { |
andrewboyson | 138:5ff0c7069300 | 74 | *pSizeTx = NTP_HEADER_LENGTH; |
andrewboyson | 121:bc048b65a630 | 75 | return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack); |
andrewboyson | 121:bc048b65a630 | 76 | } |
andrewboyson | 22:914b970356f0 | 77 | } |
andrewboyson | 138:5ff0c7069300 | 78 | int NtpPollForPacketToSend(int type, char* pPacket, int* pSize) |
andrewboyson | 112:f8694d0b8858 | 79 | { |
andrewboyson | 113:904b40231907 | 80 | int dest = NtpClientQueryPoll(type, pPacket, pSize); |
andrewboyson | 112:f8694d0b8858 | 81 | if (!dest) return DO_NOTHING; |
andrewboyson | 36:900e24b27bfb | 82 | |
andrewboyson | 42:222a4f45f916 | 83 | if (NtpTrace) |
andrewboyson | 37:793b39683406 | 84 | { |
andrewboyson | 43:bc028d5a6424 | 85 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 37:793b39683406 | 86 | LogTimeF("Sending NTP request\r\n"); |
andrewboyson | 138:5ff0c7069300 | 87 | NtpLogHeader(pPacket); |
andrewboyson | 37:793b39683406 | 88 | } |
andrewboyson | 43:bc028d5a6424 | 89 | return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack); |
andrewboyson | 112:f8694d0b8858 | 90 | |
andrewboyson | 112:f8694d0b8858 | 91 | |
andrewboyson | 22:914b970356f0 | 92 | } |
andrewboyson | 22:914b970356f0 | 93 |