aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/sntp.c
blob: f34c3da2c3c4d8f747fb0fa2c785f0565d38a446 (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
55
56
57
58
59
60
61
62
63
64
65
/* sntp.c - sntp client and server
 *
 * Copyright 2019 Rob Landley <rob@landley.net>
 *
 * See https://www.ietf.org/rfc/rfc4330.txt

USE_SNTP(NEWTOY(sntp, "<1", TOYFLAG_USR|TOYFLAG_BIN))

config SNTP
  bool "sntp"
  default n
  help
    usage: sntp SERVER...

    Simple Network Time Protocol client, set system clock from a server.
*/

#define FOR_sntp
#include "toys.h"

GLOBALS(
  int unused;
)

union socksaddr {
  struct sockaddr_in in;
  struct sockaddr_in6 in6;
};

// timeout in milliseconds
int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout)
{
  socklen_t sl = sizeof(*sa);

  if (timeout >= 0) {
    struct pollfd pfd;

    pfd.fd = fd;
    pfd.events = POLLIN;
    if (!xpoll(&pfd, 1, timeout)) return 0;
  }

  len = recvfrom(fd, buf, len, 0, (void *)sa, &sl);
  if (len<0) perror_exit("recvfrom");

  return len;
}

void sntp_main(void)
{
  struct addrinfo *ai;
  union socksaddr sa;
  int fd, len;

  ai = xgetaddrinfo(*toys.optargs, 0, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0);
  // When root, bind to local server address
  if (!getuid())
    fd = xbind(xgetaddrinfo("", "123", AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0));
  else fd = xsocket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP);

  xsendto(fd, toybuf, 48, ai->ai_addr);
  len = xrecvwait(fd, toybuf, sizeof(toybuf), &sa, 5000);
  printf("%d\n", len);
  if (len>0) write(1, toybuf, len);
}