summaryrefslogtreecommitdiff
path: root/src/clock/s6-taiclockd.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-12-15 23:08:59 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-12-15 23:08:59 +0000
commite0fc82203d677a6f1e808e9a1a46176c109d89be (patch)
treee9609209b755e3f7a8480aea86601ffe9d4ca540 /src/clock/s6-taiclockd.c
downloads6-networking-e0fc82203d677a6f1e808e9a1a46176c109d89be.tar.xz
Initial commit
Diffstat (limited to 'src/clock/s6-taiclockd.c')
-rw-r--r--src/clock/s6-taiclockd.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/clock/s6-taiclockd.c b/src/clock/s6-taiclockd.c
new file mode 100644
index 0000000..b206400
--- /dev/null
+++ b/src/clock/s6-taiclockd.c
@@ -0,0 +1,54 @@
+/* ISC license. */
+
+#include <skalibs/bytestr.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/fmtscan.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/socket.h>
+#include <skalibs/tai.h>
+#include <skalibs/ip46.h>
+
+#define USAGE "s6-taiclockd [ -i ip ] [ -p port ]"
+#define dieusage() strerr_dieusage(100, USAGE)
+
+int main (int argc, char const *const *argv)
+{
+ int s ;
+ ip46_t ip = IP46_ZERO ;
+ uint16 port = 4014 ;
+ subgetopt_t l = SUBGETOPT_ZERO ;
+ PROG = "s6-taiclockd" ;
+ for (;;)
+ {
+ register int opt = subgetopt_r(argc, argv, "i:p:", &l) ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ case 'i' : if (!ip46_scan(l.arg, &ip)) dieusage() ; break ;
+ case 'p' : if (!uint160_scan(l.arg, &port)) dieusage() ; break ;
+ default : dieusage() ;
+ }
+ }
+ argc -= l.ind ; argv += l.ind ;
+ s = socket_udp46(ip46_is6(&ip)) ;
+ if (s < 0) strerr_diefu1sys(111, "socket_udp") ;
+ if (ndelay_off(s) < 0) strerr_diefu1sys(111, "ndelay_off") ;
+ if (socket_bind46_reuse(s, &ip, port) < 0)
+ strerr_diefu1sys(111, "socket_bind_reuse") ;
+
+ for (;;)
+ {
+ char packet[256] ;
+ register int r = socket_recv46(s, packet, 256, &ip, &port) ;
+ if ((r >= 20) && !byte_diff(packet, 4, "ctai"))
+ {
+ tain_t now ;
+ packet[0] = 's' ;
+ if (!tain_sysclock(&now)) strerr_diefu1sys(111, "tain_sysclock") ;
+ tain_pack(packet + 4, &now) ;
+ if (socket_send46(s, packet, r, &ip, port) < 0)
+ strerr_warnwu1sys("socket_send") ;
+ }
+ }
+}