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 22 15:41:12 2019 +0000
Revision:
113:904b40231907
Parent:
112:f8694d0b8858
Child:
121:bc048b65a630
Incorporated ntp server module

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 37:793b39683406 7 #include "udp.h"
andrewboyson 112:f8694d0b8858 8 #include "ntpclient.h"
andrewboyson 113:904b40231907 9 #include "ntpserver.h"
andrewboyson 112:f8694d0b8858 10 #include "clkntp.h"
andrewboyson 112:f8694d0b8858 11 #include "clktime.h"
andrewboyson 113:904b40231907 12 #include "ntp.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 61:aad055f1b0d1 36 for (int i = 0; i < 4; i++) if (pHeader->RefIdentifier[i]) LogPush(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 113:904b40231907 50 if (sizeRx != sizeof(struct NtpHeader))
andrewboyson 22:914b970356f0 51 {
andrewboyson 59:e0e556c8bd46 52 LogTimeF("\r\nNTP packet wrong size %d\r\n", sizeRx);
andrewboyson 22:914b970356f0 53 return DO_NOTHING;
andrewboyson 22:914b970356f0 54 }
andrewboyson 113:904b40231907 55 struct NtpHeader* pHeaderRx = (struct NtpHeader*)pPacketRx;
andrewboyson 113:904b40231907 56 struct NtpHeader* pHeaderTx = (struct NtpHeader*)pPacketTx;
andrewboyson 17:e475ab861365 57
andrewboyson 59:e0e556c8bd46 58 int dest = DO_NOTHING;
andrewboyson 59:e0e556c8bd46 59 switch (pHeaderRx->Mode)
andrewboyson 0:faa09bd4e6bf 60 {
andrewboyson 113:904b40231907 61 case NTP_CLIENT: dest = NtpServerRequest(traceback, pHeaderRx, pHeaderTx); break;
andrewboyson 113:904b40231907 62 case NTP_SERVER: NtpClientReply (traceback, pHeaderRx); return DO_NOTHING;
andrewboyson 0:faa09bd4e6bf 63 default:
andrewboyson 59:e0e556c8bd46 64 LogTimeF("\r\nNTP packet unknown mode %d\r\n", pHeaderRx->Mode);
andrewboyson 22:914b970356f0 65 return DO_NOTHING;
andrewboyson 0:faa09bd4e6bf 66 }
andrewboyson 59:e0e556c8bd46 67
andrewboyson 59:e0e556c8bd46 68 if (dest == DO_NOTHING) return DO_NOTHING;
andrewboyson 113:904b40231907 69 *pSizeTx = sizeof(struct NtpHeader);
andrewboyson 59:e0e556c8bd46 70
andrewboyson 59:e0e556c8bd46 71 return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack);
andrewboyson 22:914b970356f0 72 }
andrewboyson 112:f8694d0b8858 73 int NtpPollForPacketToSend(int type, void* pPacket, int* pSize)
andrewboyson 112:f8694d0b8858 74 {
andrewboyson 113:904b40231907 75 int dest = NtpClientQueryPoll(type, pPacket, pSize);
andrewboyson 112:f8694d0b8858 76 if (!dest) return DO_NOTHING;
andrewboyson 36:900e24b27bfb 77
andrewboyson 42:222a4f45f916 78 if (NtpTrace)
andrewboyson 37:793b39683406 79 {
andrewboyson 43:bc028d5a6424 80 if (NetTraceNewLine) Log("\r\n");
andrewboyson 37:793b39683406 81 LogTimeF("Sending NTP request\r\n");
andrewboyson 113:904b40231907 82 NtpLogHeader((struct NtpHeader*)pPacket);
andrewboyson 37:793b39683406 83 }
andrewboyson 43:bc028d5a6424 84 return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack);
andrewboyson 112:f8694d0b8858 85
andrewboyson 112:f8694d0b8858 86
andrewboyson 22:914b970356f0 87 }
andrewboyson 22:914b970356f0 88