summaryrefslogtreecommitdiff
path: root/src/clock/s6-taiclockd.c
blob: b206400d0647565c199e6a35a23c6bcb91f4759f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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") ;
    }
  }
}