|
| 1 | +# NTP client (Posix UDP) |
| 2 | + |
| 3 | +Minimal [NTP](https://www.ntp.org/) client over UDP using only the Posix VAPI |
| 4 | +(`--pkg posix`): `Posix.getaddrinfo` for host and port lookup (IPv4 and IPv6), |
| 5 | +UDP `socket` / `connect` / `read` / `write`, byte order on the transmit timestamp, |
| 6 | +and `Posix.ctime` / `time_t` for printing. |
| 7 | + |
| 8 | +The service name `"123"` is passed to `getaddrinfo` so the port does not need |
| 9 | +to be filled in by hand. `Posix.getnameinfo` prints the numeric address that was |
| 10 | +actually used. Always call `Posix.freeaddrinfo` on the result list. |
| 11 | + |
| 12 | +## Program |
| 13 | + |
| 14 | +Save as `ntp-client.vala`: |
| 15 | + |
| 16 | +```vala |
| 17 | +struct NtpPacket { |
| 18 | + uint8 li_vn_mode; |
| 19 | + uint8 stratum; |
| 20 | + uint8 poll; |
| 21 | + uint8 precision; |
| 22 | + uint32 root_delay; |
| 23 | + uint32 root_dispersion; |
| 24 | + uint32 ref_id; |
| 25 | + uint32 ref_tm_s; |
| 26 | + uint32 ref_tm_f; |
| 27 | + uint32 orig_tm_s; |
| 28 | + uint32 orig_tm_f; |
| 29 | + uint32 rx_tm_s; |
| 30 | + uint32 rx_tm_f; |
| 31 | + uint32 tx_tm_s; |
| 32 | + uint32 tx_tm_f; |
| 33 | +} |
| 34 | +
|
| 35 | +int main () { |
| 36 | + const string HOSTNAME = "pool.ntp.org"; |
| 37 | + Posix.AddrInfo hints = Posix.AddrInfo (); |
| 38 | + hints.ai_family = Posix.AF_UNSPEC; |
| 39 | + hints.ai_socktype = Posix.SOCK_DGRAM; |
| 40 | + hints.ai_protocol = Posix.IPProto.UDP; |
| 41 | +
|
| 42 | + Posix.AddrInfo* res; |
| 43 | + int gai = Posix.getaddrinfo (HOSTNAME, "123", hints, out res); |
| 44 | + if (gai != 0) { |
| 45 | + stderr.printf ("getaddrinfo: %s\n", Posix.gai_strerror (gai)); |
| 46 | + return 1; |
| 47 | + } |
| 48 | +
|
| 49 | + int sockfd = -1; |
| 50 | + unowned Posix.AddrInfo* chosen = null; |
| 51 | + for (unowned Posix.AddrInfo* ai = res; ai != null; ai = ai.ai_next) { |
| 52 | + int fd = Posix.socket (ai.ai_family, ai.ai_socktype, ai.ai_protocol); |
| 53 | + if (fd < 0) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + if (Posix.connect (fd, ai.ai_addr, (Posix.socklen_t) ai.ai_addrlen) == 0) { |
| 57 | + sockfd = fd; |
| 58 | + chosen = ai; |
| 59 | + break; |
| 60 | + } |
| 61 | + Posix.close (fd); |
| 62 | + } |
| 63 | +
|
| 64 | + if (sockfd < 0 || chosen == null) { |
| 65 | + stderr.printf ("Could not connect to any resolved address\n"); |
| 66 | + Posix.freeaddrinfo (res); |
| 67 | + return 1; |
| 68 | + } |
| 69 | +
|
| 70 | + var hostbuf = new char[256]; |
| 71 | + var servbuf = new char[64]; |
| 72 | + if (Posix.getnameinfo (*chosen.ai_addr, (Posix.socklen_t) chosen.ai_addrlen, |
| 73 | + hostbuf, servbuf, Posix.NI_NUMERICHOST | Posix.NI_NUMERICSERV) == 0) { |
| 74 | + stderr.printf ("Using %s port %s\n", (string) hostbuf, (string) servbuf); |
| 75 | + } |
| 76 | +
|
| 77 | + var packet = NtpPacket (); |
| 78 | + assert (sizeof (NtpPacket) == 48); |
| 79 | + packet.li_vn_mode = 0x1b; |
| 80 | +
|
| 81 | + if (Posix.write (sockfd, &packet, sizeof (NtpPacket)) < 0) { |
| 82 | + stderr.printf ("Can't send UDP packet: errno %d\n", Posix.errno); |
| 83 | + Posix.close (sockfd); |
| 84 | + Posix.freeaddrinfo (res); |
| 85 | + return 1; |
| 86 | + } |
| 87 | +
|
| 88 | + if (Posix.read (sockfd, &packet, sizeof (NtpPacket)) < 0) { |
| 89 | + stderr.printf ("Can't read from socket: errno %d\n", Posix.errno); |
| 90 | + Posix.close (sockfd); |
| 91 | + Posix.freeaddrinfo (res); |
| 92 | + return 1; |
| 93 | + } |
| 94 | + Posix.close (sockfd); |
| 95 | + Posix.freeaddrinfo (res); |
| 96 | +
|
| 97 | + packet.tx_tm_s = Posix.ntohl (packet.tx_tm_s); |
| 98 | + packet.tx_tm_f = Posix.ntohl (packet.tx_tm_f); |
| 99 | + const uint64 NTP_TIMESTAMP_DELTA = 2208988800ULL; |
| 100 | + time_t tx_tm = (time_t) ((int64) packet.tx_tm_s - (int64) NTP_TIMESTAMP_DELTA); |
| 101 | + unowned string? utc_str = Posix.ctime (ref tx_tm); |
| 102 | + stdout.printf ("Current UTC time is %s", utc_str ?? "unknown\n"); |
| 103 | + return 0; |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Compile and run |
| 108 | + |
| 109 | +```shell |
| 110 | +valac --pkg posix ntp-client.vala |
| 111 | +./ntp-client |
| 112 | +``` |
| 113 | + |
| 114 | +`Posix.ctime` returns a libc-formatted string (usually including a trailing newline). |
| 115 | +Outbound UDP to port 123 must be allowed. The program prints diagnostics on |
| 116 | +`stderr` and the time string on `stdout`. |
0 commit comments