-
Notifications
You must be signed in to change notification settings - Fork 2
/
sntp.c
491 lines (426 loc) · 10.5 KB
/
sntp.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*-
* Copyright (c) 2007 TANDBERG Telecom AS
* Copyright (c) 2008-2009 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Author: Dag-Erling Smørgrav <[email protected]>
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "sntp.h"
#include "zutil.h"
/*
* Convert a struct timeval to an NTP timestamp
*/
void
tv2nt(struct timeval *tv, struct ntptime *nt)
{
uint64_t frac;
nt->sec = tv->tv_sec + UNIX_EPOCH;
frac = tv->tv_usec;
frac <<= 32;
frac /= 1000 * 1000;
nt->frac = frac;
}
/*
* Convert an NTP timestamp to a struct timeval
*/
void
nt2tv(struct ntptime *nt, struct timeval *tv)
{
uint64_t frac;
tv->tv_sec = nt->sec - UNIX_EPOCH;
frac = nt->frac;
frac *= 1000 * 1000;
frac >>= 32;
tv->tv_usec = frac;
}
/*
* Convert a struct timespec to an NTP timestamp
*/
void
ts2nt(struct timespec *ts, struct ntptime *nt)
{
uint64_t frac;
nt->sec = ts->tv_sec + UNIX_EPOCH;
frac = ts->tv_nsec;
frac <<= 32;
frac /= 1000 * 1000 * 1000;
nt->frac = frac;
}
/*
* Convert an NTP timestamp to a struct timespec
*/
void
nt2ts(struct ntptime *nt, struct timespec *ts)
{
uint64_t frac;
ts->tv_sec = nt->sec - UNIX_EPOCH;
frac = nt->frac;
frac *= 1000 * 1000 * 1000;
frac >>= 32;
ts->tv_nsec = frac;
}
/*
* Convert struct ntptime in-place from network to host order
*/
void
n2h_ntp(struct ntptime *nt)
{
nt->sec = ntohl(nt->sec);
nt->frac = ntohl(nt->frac);
}
/*
* Convert struct ntptime in-place from host to network order
*/
void
h2n_ntp(struct ntptime *nt)
{
nt->sec = htonl(nt->sec);
nt->frac = htonl(nt->frac);
}
/*
* SNTP client state
*/
struct sntp {
/* parameters from sntp_create() */
char *srcaddr;
char *srcport;
char *dstaddr;
char *dstport;
/* DNS data */
int family;
int socktype;
int protocol;
struct sockaddr *laddr;
socklen_t laddrlen;
struct sockaddr *raddr;
socklen_t raddrlen;
/* socket and poll structure */
int sd;
struct pollfd pfd;
/* protocol state */
struct ntptime last_send;
struct ntptime last_recv;
};
/*
* Initialize an SNTP client context
*
* Multiple contexts can coexist as long as they do not use the same
* source port.
*
* TODO: add support for binding to a specific source address.
*/
struct sntp *
sntp_create(const char *dstaddr, const char *dstport,
const char *srcaddr, const char *srcport)
{
struct sntp *sntp;
zassert(dstaddr != NULL);
sntp = zalloc(sizeof *sntp);
sntp->sd = -1;
sntp->srcaddr = srcaddr ? zstrdup(srcaddr) : NULL;
sntp->srcport = zstrdup(srcport ? srcport : "ntp");
sntp->dstaddr = zstrdup(dstaddr);
sntp->dstport = zstrdup(dstport ? dstport : "ntp");
/* good to go */
return (sntp);
}
/*
* Look up local and remote addresses and set up the socket
*/
int
sntp_open(struct sntp *sntp)
{
struct addrinfo hints;
struct addrinfo *aiv, *ai;
int ret;
/*
* TODO: even if sntp->sd != -1, we may want to close and reopen
* if a certain amount of time has elapsed since we last did so.
*/
if (sntp->sd != -1)
return (SNTP_OK);
/* resolve the server address */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((ret = getaddrinfo(sntp->dstaddr, sntp->dstport, &hints, &aiv)) != 0)
return (SNTP_DNSERR);
zassert(aiv != NULL);
/*
* Iterate over the results until we find one we can use. This is
* sometimes necessary on systems with partial IPv6 support, where
* the resolver may return IPv6 addresses which the network stack
* can't handle.
*/
for (ai = aiv; ai; ai = ai->ai_next) {
sntp->sd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (sntp->sd >= 0)
break;
}
if (sntp->sd == -1) {
freeaddrinfo(aiv);
return (SNTP_SYSERR);
}
sntp->raddr = zmemdup(ai->ai_addr, ai->ai_addrlen);
sntp->raddrlen = ai->ai_addrlen;
sntp->family = ai->ai_family;
sntp->socktype = ai->ai_socktype;
sntp->protocol = ai->ai_protocol;
freeaddrinfo(aiv);
/* get a matching local address */
memset(&hints, 0, sizeof hints);
hints.ai_family = sntp->family;
hints.ai_socktype = sntp->socktype;
hints.ai_protocol = sntp->protocol;
hints.ai_flags = AI_PASSIVE;
if ((ret = getaddrinfo(sntp->srcaddr, sntp->srcport, &hints, &aiv)) != 0) {
sntp_close(sntp);
return (SNTP_DNSERR);
}
zassert(aiv != NULL);
/* TODO: assert that results match expectations */
sntp->laddr = zmemdup(aiv->ai_addr, aiv->ai_addrlen);
sntp->laddrlen = aiv->ai_addrlen;
freeaddrinfo(aiv);
/* prepare our socket */
if (bind(sntp->sd, sntp->laddr, sntp->laddrlen) != 0) {
sntp_close(sntp);
return (SNTP_SYSERR);
}
if (connect(sntp->sd, sntp->raddr, sntp->raddrlen) != 0) {
sntp_close(sntp);
return (SNTP_SYSERR);
}
/* prepare our pollfd */
sntp->pfd.fd = sntp->sd;
sntp->pfd.events = POLLIN;
sntp->pfd.revents = 0;
return (SNTP_OK);
}
/*
* Destroy an SNTP client context
*
* This is called several times during error handling in other parts of
* the code, so we should save and restore errno
*/
void
sntp_close(struct sntp *sntp)
{
int serrno;
serrno = errno;
sntp->family = 0;
sntp->socktype = 0;
sntp->protocol = 0;
if (sntp->laddr)
zfree(sntp->laddr, sntp->laddrlen);
sntp->laddrlen = 0;
if (sntp->raddr)
zfree(sntp->raddr, sntp->raddrlen);
sntp->raddrlen = 0;
if (sntp->sd != -1)
zclose(sntp->sd);
memset(&sntp->pfd, 0, sizeof sntp->pfd);
nt_zero(sntp->last_send);
nt_zero(sntp->last_recv);
errno = serrno;
}
/*
* Destroy an SNTP client context
*/
void
sntp_destroy(struct sntp *sntp)
{
(void)sntp_close(sntp);
if (sntp->srcaddr)
zfree(sntp->srcaddr, 0);
if (sntp->srcport)
zfree(sntp->srcport, 0);
if (sntp->dstaddr)
zfree(sntp->dstaddr, 0);
if (sntp->dstport)
zfree(sntp->dstport, 0);
zfree(sntp, sizeof *sntp);
}
/*
* Structure of an NTP message without authenticator
*/
struct ntp_msg {
uint8_t flags;
uint8_t stratum;
uint8_t poll;
uint8_t precision;
uint32_t root_delay;
uint32_t root_dispersion;
uint8_t reference_id[4];
struct ntptime reference;
struct ntptime originate;
struct ntptime receive;
struct ntptime transmit;
};
/*
* Send an SNTP request
*/
sntp_err_t
sntp_send(struct sntp *sntp)
{
struct timespec ts;
struct ntp_msg msg;
ssize_t ret;
sntp_err_t se;
if ((se = sntp_open(sntp)) != SNTP_OK)
return (se);
memset(&msg, 0, sizeof msg);
msg.flags = 0x23; /* version 4, client */
if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
return (SNTP_SYSERR);
ts2nt(&ts, &msg.transmit);
h2n_ntp(&msg.transmit);
ret = send(sntp->sd, &msg, sizeof msg, 0);
if (ret < 0)
return (SNTP_SYSERR);
ts2nt(&ts, &sntp->last_send);
return (SNTP_OK);
}
/*
* Have we sent a request to which we're still expecting a response?
*/
sntp_err_t
sntp_pending(struct sntp *sntp)
{
/* not currently open */
if (sntp->sd == -1)
return (SNTP_NOREQ);
/* last request predates last response */
if (nt_lt(sntp->last_send, sntp->last_recv))
return (SNTP_NOREQ);
/* TODO: enforce a minimum timeout */
return (SNTP_OK);
}
/*
* Poll for the arrival of an SNTP reply
*/
sntp_err_t
sntp_poll(struct sntp *sntp, int timeout)
{
sntp_err_t se;
if ((se = sntp_pending(sntp)) != SNTP_OK)
return (se);
switch (poll(&sntp->pfd, 1, timeout)) {
case -1:
sntp_close(sntp);
return (SNTP_SYSERR);
case 0:
return (SNTP_NORESP);
case 1:
if (sntp->pfd.revents & (POLLERR|POLLHUP)) {
sntp_close(sntp);
return (SNTP_SYSERR);
}
return (SNTP_OK);
}
zunreach();
}
/*
* Receive and process an SNTP reply
*/
sntp_err_t
sntp_recv(struct sntp *sntp, struct ntptime *nt)
{
struct timespec ts;
struct ntp_msg msg;
sntp_err_t se;
if ((se = sntp_pending(sntp)) != SNTP_OK)
return (se);
/* TODO: use recvmsg() instead */
switch (recv(sntp->sd, &msg, sizeof msg, MSG_DONTWAIT)) {
case -1:
if (errno == EAGAIN)
return (SNTP_NORESP);
sntp_close(sntp);
return (SNTP_SYSERR);
case 0:
/* can this actually occur? */
return (SNTP_NORESP);
case sizeof msg:
/* good! */
break;
default:
/* we got something, but bob knows what */
return (SNTP_BADRESP);
}
/* record time of arrival */
if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
return (SNTP_SYSERR);
/* convert to host order */
n2h_ntp(&msg.originate);
n2h_ntp(&msg.receive);
n2h_ntp(&msg.transmit);
/* look for kiss packet */
if (msg.flags == 0xe4 && msg.stratum == 0) {
warnx("kiss: %.4s", msg.reference_id);
/* TODO: closer look at the kiss code */
return (SNTP_BACKOFF);
}
/* check validity: synchronized NTPv4 server */
switch (msg.flags) {
case 0x23: /* version 4 client */
/* we're probably accidentally querying ourselves */
return (SNTP_BADRESP);
case 0x24: /* no warning, version 4, server */
case 0x64: /* subtract leap second, version 4, server */
case 0xa4: /* add leap second, version 4, server */
/* these are the normal, useful cases */
break;
case 0xe4: /* unsynchronized, version 4, server */
/* server not usable (yet?) */
return (SNTP_LAME);
default:
return (SNTP_BADRESP);
}
/* check if this is the response we were expecting */
if (!nt_eq(msg.originate, sntp->last_send))
/* probably delayed response to old request */
return (SNTP_NORESP);
ts2nt(&ts, &sntp->last_recv);
*nt = msg.transmit;
return (SNTP_OK);
}