summaryrefslogtreecommitdiff
path: root/net/ntp.c
diff options
context:
space:
mode:
authorMiguel <m.i@gmx.at>2018-09-27 14:30:33 +0200
committerMiguel <m.i@gmx.at>2018-09-27 14:30:33 +0200
commit8e005d0f13767db538e3d939188d73370db9042d (patch)
treea9b320d6b3da7137348019de70846bdf8aa03246 /net/ntp.c
parent05d86ed530c05ba3f8648ffd7e67f4a593ae58d8 (diff)
added linux tap support for fool's network stack
Diffstat (limited to 'net/ntp.c')
-rw-r--r--net/ntp.c28
1 files changed, 26 insertions, 2 deletions
diff --git a/net/ntp.c b/net/ntp.c
index 0e9b19d..9c0d3c7 100644
--- a/net/ntp.c
+++ b/net/ntp.c
@@ -3,6 +3,29 @@
#include "net_sys.h"
#include "ntp.h"
+//http://ptspts.blogspot.com/2009/11/how-to-convert-unix-timestamp-to-civil.html
+// Convert a Unix timestamp to a civil date ([year, month, day, hour, minute,
+// second]) in GMT.
+void timestamp_to_gmt_civil(uint32_t ts)
+{
+ uint32_t s = ts%86400;
+ ts /= 86400;
+ uint32_t h = s/3600;
+ uint32_t m = s/60%60;
+ s = s%60;
+ uint32_t x = (ts*4+102032)/146097+15;
+ uint32_t b = ts+2442113+x-(x/4);
+ uint32_t c = (b*20-2442)/7305;
+ uint32_t d = b-365*c-c/4;
+ uint32_t e = d*1000/30601;
+ uint32_t f = d-e*30-e*601/1000;
+ if(e < 14){c-=4716;e-=1;}
+ else {c-=4715;e-=13;}
+
+ // final result is in: c / e / f / h / m / s
+ klog ("%d.%d.%d %d:%d:%d UTC",f,e,c,h,m,s);
+}
+
uint32_t ntp_generic(struct netdev *dev, uint32_t ip, uint16_t src, uint16_t dst, uint8_t* pos,uint8_t *end)
{
pos-=48; // we need 48 bytes
@@ -39,8 +62,9 @@ uint32_t ntp_generic(struct netdev *dev, uint32_t ip, uint16_t src, uint16_t dst
uint32_t ntp_incoming(uint8_t* start, uint8_t *pos)
{
struct udp_v4_ntp *ntp=pos;
- klog("0x%08X",ntohl(ntp->txTm_s)); // timestamp recevived
- klog("google's NTP says that %d seconds elapsed since 1 jan 1970",ntohl(ntp->txTm_s)); // timestamp recevived
+ uint32_t ntp_time=ntohl(ntp->txTm_s);
+ uint32_t unix_time=ntp_time-2208988800; // subtract offset to get unixtime
+ timestamp_to_gmt_civil(unix_time);
return 0; // all ok
}