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@124:6e558721ec1c, 2019-02-22 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Feb 22 08:41:47 2019 +0000
- Revision:
- 124:6e558721ec1c
- Parent:
- 121:bc048b65a630
- Child:
- 138:5ff0c7069300
Moved NTP conversion routines from clock library
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 | 121:bc048b65a630 | 12 | #include "fault.h" |
andrewboyson | 22:914b970356f0 | 13 | |
andrewboyson | 83:08c983006a6e | 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 | 113:904b40231907 | 22 | void NtpLogHeader(struct NtpHeader* pHeader) |
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 | 61:aad055f1b0d1 | 27 | LogF("Mode %d", pHeader->Mode); |
andrewboyson | 61:aad055f1b0d1 | 28 | LogF(", Version %d", pHeader->VN); |
andrewboyson | 61:aad055f1b0d1 | 29 | LogF(", LI %d", pHeader->LI); |
andrewboyson | 61:aad055f1b0d1 | 30 | LogF(", Stratum %d", pHeader->Stratum); |
andrewboyson | 61:aad055f1b0d1 | 31 | LogF(", Poll %d", pHeader->Poll); |
andrewboyson | 61:aad055f1b0d1 | 32 | LogF(", Precision %d", pHeader->Precision); |
andrewboyson | 61:aad055f1b0d1 | 33 | LogF(", Root delay %d", NetToHost32(pHeader->RootDelay)); |
andrewboyson | 61:aad055f1b0d1 | 34 | LogF(", Dispersion %d", NetToHost32(pHeader->Dispersion)); |
andrewboyson | 61:aad055f1b0d1 | 35 | Log (", Ident "); |
andrewboyson | 121:bc048b65a630 | 36 | for (int i = 0; i < 4; i++) if (pHeader->RefIdentifier[i]) LogChar(pHeader->RefIdentifier[i]); |
andrewboyson | 61:aad055f1b0d1 | 37 | Log ("\r\n"); |
andrewboyson | 61:aad055f1b0d1 | 38 | |
andrewboyson | 43:bc028d5a6424 | 39 | if (NetTraceVerbose) |
andrewboyson | 43:bc028d5a6424 | 40 | { |
andrewboyson | 43:bc028d5a6424 | 41 | LogF(" REF %llu\r\n", NetToHost64(pHeader->RefTimeStamp)); |
andrewboyson | 43:bc028d5a6424 | 42 | LogF(" ORI %llu\r\n", NetToHost64(pHeader->OriTimeStamp)); |
andrewboyson | 43:bc028d5a6424 | 43 | LogF(" REC %llu\r\n", NetToHost64(pHeader->RecTimeStamp)); |
andrewboyson | 43:bc028d5a6424 | 44 | LogF(" TRA %llu\r\n", NetToHost64(pHeader->TraTimeStamp)); |
andrewboyson | 43:bc028d5a6424 | 45 | } |
andrewboyson | 37:793b39683406 | 46 | } |
andrewboyson | 112:f8694d0b8858 | 47 | |
andrewboyson | 59:e0e556c8bd46 | 48 | int NtpHandlePacketReceived(void (*traceback)(void), int sizeRx, void * pPacketRx, int* pSizeTx, void* pPacketTx) |
andrewboyson | 22:914b970356f0 | 49 | { |
andrewboyson | 121:bc048b65a630 | 50 | int lastFaultPoint = FaultPoint; |
andrewboyson | 121:bc048b65a630 | 51 | FaultPoint = FAULT_POINT_NtpHandlePacketReceived; |
andrewboyson | 121:bc048b65a630 | 52 | |
andrewboyson | 113:904b40231907 | 53 | if (sizeRx != sizeof(struct NtpHeader)) |
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 | 113:904b40231907 | 58 | struct NtpHeader* pHeaderRx = (struct NtpHeader*)pPacketRx; |
andrewboyson | 113:904b40231907 | 59 | struct NtpHeader* pHeaderTx = (struct NtpHeader*)pPacketTx; |
andrewboyson | 17:e475ab861365 | 60 | |
andrewboyson | 59:e0e556c8bd46 | 61 | int dest = DO_NOTHING; |
andrewboyson | 59:e0e556c8bd46 | 62 | switch (pHeaderRx->Mode) |
andrewboyson | 0:faa09bd4e6bf | 63 | { |
andrewboyson | 121:bc048b65a630 | 64 | case NTP_CLIENT: dest = NtpServerRequest(traceback, pHeaderRx, pHeaderTx); break; |
andrewboyson | 121:bc048b65a630 | 65 | case NTP_SERVER: NtpClientReply (traceback, pHeaderRx); break; |
andrewboyson | 121:bc048b65a630 | 66 | default: LogTimeF("\r\nNTP packet unknown mode %d\r\n", pHeaderRx->Mode); break; |
andrewboyson | 0:faa09bd4e6bf | 67 | } |
andrewboyson | 59:e0e556c8bd46 | 68 | |
andrewboyson | 121:bc048b65a630 | 69 | FaultPoint = lastFaultPoint; |
andrewboyson | 121:bc048b65a630 | 70 | if (dest == DO_NOTHING) |
andrewboyson | 121:bc048b65a630 | 71 | { |
andrewboyson | 121:bc048b65a630 | 72 | return DO_NOTHING; |
andrewboyson | 121:bc048b65a630 | 73 | } |
andrewboyson | 121:bc048b65a630 | 74 | else |
andrewboyson | 121:bc048b65a630 | 75 | { |
andrewboyson | 121:bc048b65a630 | 76 | *pSizeTx = sizeof(struct NtpHeader); |
andrewboyson | 121:bc048b65a630 | 77 | return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack); |
andrewboyson | 121:bc048b65a630 | 78 | } |
andrewboyson | 22:914b970356f0 | 79 | } |
andrewboyson | 112:f8694d0b8858 | 80 | int NtpPollForPacketToSend(int type, void* pPacket, int* pSize) |
andrewboyson | 112:f8694d0b8858 | 81 | { |
andrewboyson | 113:904b40231907 | 82 | int dest = NtpClientQueryPoll(type, pPacket, pSize); |
andrewboyson | 112:f8694d0b8858 | 83 | if (!dest) return DO_NOTHING; |
andrewboyson | 36:900e24b27bfb | 84 | |
andrewboyson | 42:222a4f45f916 | 85 | if (NtpTrace) |
andrewboyson | 37:793b39683406 | 86 | { |
andrewboyson | 43:bc028d5a6424 | 87 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 37:793b39683406 | 88 | LogTimeF("Sending NTP request\r\n"); |
andrewboyson | 113:904b40231907 | 89 | NtpLogHeader((struct NtpHeader*)pPacket); |
andrewboyson | 37:793b39683406 | 90 | } |
andrewboyson | 43:bc028d5a6424 | 91 | return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack); |
andrewboyson | 112:f8694d0b8858 | 92 | |
andrewboyson | 112:f8694d0b8858 | 93 | |
andrewboyson | 22:914b970356f0 | 94 | } |
andrewboyson | 22:914b970356f0 | 95 |