aboutsummaryrefslogtreecommitdiff
path: root/networking/ntpd.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-12-17 23:29:33 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2009-12-17 23:29:33 +0100
commitafa2d33aa495ab243e9b1347ae0ee9c6eb30c260 (patch)
tree38f57f96dfd0c65da560335f34137545694af724 /networking/ntpd.c
parente99c8d2ef5bb94e64ea55f5900c5d9a513684ec8 (diff)
downloadbusybox-w32-afa2d33aa495ab243e9b1347ae0ee9c6eb30c260.tar.gz
busybox-w32-afa2d33aa495ab243e9b1347ae0ee9c6eb30c260.tar.bz2
busybox-w32-afa2d33aa495ab243e9b1347ae0ee9c6eb30c260.zip
ntpd: field renames and style fixes, no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/ntpd.c')
-rw-r--r--networking/ntpd.c316
1 files changed, 158 insertions, 158 deletions
diff --git a/networking/ntpd.c b/networking/ntpd.c
index 1e6c4da7f..719b00550 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -16,23 +16,23 @@
16 16
17 17
18/* Sync to peers every N secs */ 18/* Sync to peers every N secs */
19#define INTERVAL_QUERY_NORMAL 30 19#define INTERVAL_QUERY_NORMAL 30
20#define INTERVAL_QUERY_PATHETIC 60 20#define INTERVAL_QUERY_PATHETIC 60
21#define INTERVAL_QUERY_AGRESSIVE 5 21#define INTERVAL_QUERY_AGRESSIVE 5
22 22
23/* Bad if *less than* TRUSTLEVEL_BADPEER */ 23/* Bad if *less than* TRUSTLEVEL_BADPEER */
24#define TRUSTLEVEL_BADPEER 6 24#define TRUSTLEVEL_BADPEER 6
25#define TRUSTLEVEL_PATHETIC 2 25#define TRUSTLEVEL_PATHETIC 2
26#define TRUSTLEVEL_AGRESSIVE 8 26#define TRUSTLEVEL_AGRESSIVE 8
27#define TRUSTLEVEL_MAX 10 27#define TRUSTLEVEL_MAX 10
28 28
29#define QSCALE_OFF_MIN 0.05 29#define QSCALE_OFF_MIN 0.05
30#define QSCALE_OFF_MAX 0.50 30#define QSCALE_OFF_MAX 0.50
31 31
32/* Single query might take n secs max */ 32/* Single query might take n secs max */
33#define QUERYTIME_MAX 15 33#define QUERYTIME_MAX 15
34/* Min offset for settime at start. "man ntpd" says it's 128 ms */ 34/* Min offset for settime at start. "man ntpd" says it's 128 ms */
35#define STEPTIME_MIN_OFFSET 0.128 35#define STEPTIME_MIN_OFFSET 0.128
36 36
37typedef struct { 37typedef struct {
38 uint32_t int_partl; 38 uint32_t int_partl;
@@ -67,58 +67,58 @@ typedef struct {
67} ntp_msg_t; 67} ntp_msg_t;
68 68
69enum { 69enum {
70 NTP_VERSION = 4, 70 NTP_VERSION = 4,
71 NTP_MAXSTRATUM = 15, 71 NTP_MAXSTRATUM = 15,
72 /* Leap Second Codes (high order two bits of m_status) */ 72 /* Leap Second Codes (high order two bits of m_status) */
73 LI_NOWARNING = (0 << 6), /* no warning */ 73 LI_NOWARNING = (0 << 6), /* no warning */
74 LI_PLUSSEC = (1 << 6), /* add a second (61 seconds) */ 74 LI_PLUSSEC = (1 << 6), /* add a second (61 seconds) */
75 LI_MINUSSEC = (2 << 6), /* minus a second (59 seconds) */ 75 LI_MINUSSEC = (2 << 6), /* minus a second (59 seconds) */
76 LI_ALARM = (3 << 6), /* alarm condition */ 76 LI_ALARM = (3 << 6), /* alarm condition */
77 77
78 /* Status Masks */ 78 /* Status Masks */
79 MODE_MASK = (7 << 0), 79 MODE_MASK = (7 << 0),
80 VERSION_MASK = (7 << 3), 80 VERSION_MASK = (7 << 3),
81 VERSION_SHIFT = 3, 81 VERSION_SHIFT = 3,
82 LI_MASK = (3 << 6), 82 LI_MASK = (3 << 6),
83 83
84 /* Mode values */ 84 /* Mode values */
85 MODE_RES0 = 0, /* reserved */ 85 MODE_RES0 = 0, /* reserved */
86 MODE_SYM_ACT = 1, /* symmetric active */ 86 MODE_SYM_ACT = 1, /* symmetric active */
87 MODE_SYM_PAS = 2, /* symmetric passive */ 87 MODE_SYM_PAS = 2, /* symmetric passive */
88 MODE_CLIENT = 3, /* client */ 88 MODE_CLIENT = 3, /* client */
89 MODE_SERVER = 4, /* server */ 89 MODE_SERVER = 4, /* server */
90 MODE_BROADCAST = 5, /* broadcast */ 90 MODE_BROADCAST = 5, /* broadcast */
91 MODE_RES1 = 6, /* reserved for NTP control message */ 91 MODE_RES1 = 6, /* reserved for NTP control message */
92 MODE_RES2 = 7, /* reserved for private use */ 92 MODE_RES2 = 7, /* reserved for private use */
93}; 93};
94 94
95#define OFFSET_1900_1970 2208988800UL /* 1970 - 1900 in seconds */ 95#define OFFSET_1900_1970 2208988800UL /* 1970 - 1900 in seconds */
96 96
97typedef struct { 97typedef struct {
98 double o_offset; 98 double d_offset;
99 double o_delay; 99 double d_delay;
100 //UNUSED: double o_error; 100 //UNUSED: double d_error;
101 time_t o_rcvd; 101 time_t d_rcvd;
102 uint32_t o_refid4; 102 uint32_t d_refid4;
103 uint8_t o_leap; 103 uint8_t d_leap;
104 uint8_t o_stratum; 104 uint8_t d_stratum;
105 uint8_t o_good; 105 uint8_t d_good;
106} ntp_offset_t; 106} ntp_datapoint_t;
107 107
108#define OFFSET_ARRAY_SIZE 8 108#define NUM_DATAPOINTS 8
109typedef struct { 109typedef struct {
110 len_and_sockaddr *lsa; 110 len_and_sockaddr *p_lsa;
111 char *dotted; 111 char *p_dotted;
112 /* When to send new query (if fd == -1) 112 /* When to send new query (if p_fd == -1)
113 * or when receive times out (if fd >= 0): */ 113 * or when receive times out (if p_fd >= 0): */
114 time_t next_action_time; 114 time_t next_action_time;
115 int fd; 115 int p_fd;
116 uint8_t shift; 116 uint8_t p_datapoint_idx;
117 uint8_t trustlevel; 117 uint8_t trustlevel;
118 ntp_msg_t msg; 118 ntp_msg_t msg;
119 double xmttime; 119 double p_xmttime;
120 ntp_offset_t update; 120 ntp_datapoint_t update;
121 ntp_offset_t reply[OFFSET_ARRAY_SIZE]; 121 ntp_datapoint_t p_datapoint[NUM_DATAPOINTS];
122} ntp_peer_t; 122} ntp_peer_t;
123 123
124enum { 124enum {
@@ -134,24 +134,24 @@ enum {
134 134
135 135
136struct globals { 136struct globals {
137 double rootdelay; 137 double rootdelay;
138 double reftime; 138 double reftime;
139 llist_t *ntp_peers; 139 llist_t *ntp_peers;
140#if ENABLE_FEATURE_NTPD_SERVER 140#if ENABLE_FEATURE_NTPD_SERVER
141 int listen_fd; 141 int listen_fd;
142#endif 142#endif
143 unsigned verbose; 143 unsigned verbose;
144 unsigned peer_cnt; 144 unsigned peer_cnt;
145 unsigned scale; 145 unsigned scale;
146 uint32_t refid; 146 uint32_t refid;
147 uint32_t refid4; 147 uint32_t refid4;
148 uint8_t synced; 148 uint8_t synced;
149 uint8_t leap; 149 uint8_t leap;
150#define G_precision -6 150#define G_precision -6
151// int8_t precision; 151// int8_t precision;
152 uint8_t stratum; 152 uint8_t stratum;
153 uint8_t time_is_stepped; 153 uint8_t time_was_stepped;
154 uint8_t first_adj_done; 154 uint8_t first_adj_done;
155}; 155};
156#define G (*ptr_to_globals) 156#define G (*ptr_to_globals)
157 157
@@ -171,9 +171,9 @@ add_peers(char *s)
171 ntp_peer_t *p; 171 ntp_peer_t *p;
172 172
173 p = xzalloc(sizeof(*p)); 173 p = xzalloc(sizeof(*p));
174 p->lsa = xhost2sockaddr(s, 123); 174 p->p_lsa = xhost2sockaddr(s, 123);
175 p->dotted = xmalloc_sockaddr2dotted_noport(&p->lsa->u.sa); 175 p->p_dotted = xmalloc_sockaddr2dotted_noport(&p->p_lsa->u.sa);
176 p->fd = -1; 176 p->p_fd = -1;
177 p->msg.m_status = MODE_CLIENT | (NTP_VERSION << 3); 177 p->msg.m_status = MODE_CLIENT | (NTP_VERSION << 3);
178 p->trustlevel = TRUSTLEVEL_PATHETIC; 178 p->trustlevel = TRUSTLEVEL_PATHETIC;
179 p->next_action_time = time(NULL); /* = set_next(p, 0); */ 179 p->next_action_time = time(NULL); /* = set_next(p, 0); */
@@ -292,15 +292,15 @@ send_query_to_peer(ntp_peer_t *p)
292 // Uncomment this and use strace to see it in action: 292 // Uncomment this and use strace to see it in action:
293#define PROBE_LOCAL_ADDR // { len_and_sockaddr lsa; lsa.len = LSA_SIZEOF_SA; getsockname(p->query.fd, &lsa.u.sa, &lsa.len); } 293#define PROBE_LOCAL_ADDR // { len_and_sockaddr lsa; lsa.len = LSA_SIZEOF_SA; getsockname(p->query.fd, &lsa.u.sa, &lsa.len); }
294 294
295 if (p->fd == -1) { 295 if (p->p_fd == -1) {
296 int fd, family; 296 int fd, family;
297 len_and_sockaddr *local_lsa; 297 len_and_sockaddr *local_lsa;
298 298
299 family = p->lsa->u.sa.sa_family; 299 family = p->p_lsa->u.sa.sa_family;
300 p->fd = fd = xsocket_type(&local_lsa, family, SOCK_DGRAM); 300 p->p_fd = fd = xsocket_type(&local_lsa, family, SOCK_DGRAM);
301 /* local_lsa has "null" address and port 0 now. 301 /* local_lsa has "null" address and port 0 now.
302 * bind() ensures we have a *particular port* selected by kernel 302 * bind() ensures we have a *particular port* selected by kernel
303 * and remembered in p->fd, thus later recv(p->fd) 303 * and remembered in p->p_fd, thus later recv(p->p_fd)
304 * receives only packets sent to this port. 304 * receives only packets sent to this port.
305 */ 305 */
306 PROBE_LOCAL_ADDR 306 PROBE_LOCAL_ADDR
@@ -328,19 +328,19 @@ send_query_to_peer(ntp_peer_t *p)
328 */ 328 */
329 p->msg.m_xmttime.int_partl = random(); 329 p->msg.m_xmttime.int_partl = random();
330 p->msg.m_xmttime.fractionl = random(); 330 p->msg.m_xmttime.fractionl = random();
331 p->xmttime = gettime1900d(); 331 p->p_xmttime = gettime1900d();
332 332
333 if (do_sendto(p->fd, /*from:*/ NULL, /*to:*/ &p->lsa->u.sa, /*addrlen:*/ p->lsa->len, 333 if (do_sendto(p->p_fd, /*from:*/ NULL, /*to:*/ &p->p_lsa->u.sa, /*addrlen:*/ p->p_lsa->len,
334 &p->msg, NTP_MSGSIZE_NOAUTH) == -1 334 &p->msg, NTP_MSGSIZE_NOAUTH) == -1
335 ) { 335 ) {
336 close(p->fd); 336 close(p->p_fd);
337 p->fd = -1; 337 p->p_fd = -1;
338 set_next(p, INTERVAL_QUERY_PATHETIC); 338 set_next(p, INTERVAL_QUERY_PATHETIC);
339 return -1; 339 return -1;
340 } 340 }
341 341
342 if (G.verbose) 342 if (G.verbose)
343 bb_error_msg("sent query to %s", p->dotted); 343 bb_error_msg("sent query to %s", p->p_dotted);
344 set_next(p, QUERYTIME_MAX); 344 set_next(p, QUERYTIME_MAX);
345 345
346 return 0; 346 return 0;
@@ -357,9 +357,9 @@ step_time_once(double offset)
357 char buf[80]; 357 char buf[80];
358 time_t tval; 358 time_t tval;
359 359
360 if (G.time_is_stepped) 360 if (G.time_was_stepped)
361 goto bail; 361 goto bail;
362 G.time_is_stepped = 1; 362 G.time_was_stepped = 1;
363 363
364 /* if the offset is small, don't step, slew (later) */ 364 /* if the offset is small, don't step, slew (later) */
365 if (offset < STEPTIME_MIN_OFFSET && offset > -STEPTIME_MIN_OFFSET) 365 if (offset < STEPTIME_MIN_OFFSET && offset > -STEPTIME_MIN_OFFSET)
@@ -397,9 +397,9 @@ compare_offsets(const void *aa, const void *bb)
397{ 397{
398 const ntp_peer_t *const *a = aa; 398 const ntp_peer_t *const *a = aa;
399 const ntp_peer_t *const *b = bb; 399 const ntp_peer_t *const *b = bb;
400 if ((*a)->update.o_offset < (*b)->update.o_offset) 400 if ((*a)->update.d_offset < (*b)->update.d_offset)
401 return -1; 401 return -1;
402 return ((*a)->update.o_offset > (*b)->update.o_offset); 402 return ((*a)->update.d_offset > (*b)->update.d_offset);
403} 403}
404static unsigned 404static unsigned
405updated_scale(double offset) 405updated_scale(double offset)
@@ -428,7 +428,7 @@ slew_time(void)
428 ntp_peer_t *p = (ntp_peer_t *) item->data; 428 ntp_peer_t *p = (ntp_peer_t *) item->data;
429 if (p->trustlevel < TRUSTLEVEL_BADPEER) 429 if (p->trustlevel < TRUSTLEVEL_BADPEER)
430 continue; 430 continue;
431 if (!p->update.o_good) { 431 if (!p->update.d_good) {
432 free(peers); 432 free(peers);
433 return; 433 return;
434 } 434 }
@@ -444,22 +444,22 @@ slew_time(void)
444 444
445 middle = goodpeer_cnt / 2; 445 middle = goodpeer_cnt / 2;
446 if (middle != 0 && (goodpeer_cnt & 1) == 0) { 446 if (middle != 0 && (goodpeer_cnt & 1) == 0) {
447 offset_median = (peers[middle-1]->update.o_offset + peers[middle]->update.o_offset) / 2; 447 offset_median = (peers[middle-1]->update.d_offset + peers[middle]->update.d_offset) / 2;
448 G.rootdelay = (peers[middle-1]->update.o_delay + peers[middle]->update.o_delay) / 2; 448 G.rootdelay = (peers[middle-1]->update.d_delay + peers[middle]->update.d_delay) / 2;
449 G.stratum = 1 + MAX(peers[middle-1]->update.o_stratum, peers[middle]->update.o_stratum); 449 G.stratum = 1 + MAX(peers[middle-1]->update.d_stratum, peers[middle]->update.d_stratum);
450 } else { 450 } else {
451 offset_median = peers[middle]->update.o_offset; 451 offset_median = peers[middle]->update.d_offset;
452 G.rootdelay = peers[middle]->update.o_delay; 452 G.rootdelay = peers[middle]->update.d_delay;
453 G.stratum = 1 + peers[middle]->update.o_stratum; 453 G.stratum = 1 + peers[middle]->update.d_stratum;
454 } 454 }
455 G.leap = peers[middle]->update.o_leap; 455 G.leap = peers[middle]->update.d_leap;
456 G.refid4 = peers[middle]->update.o_refid4; 456 G.refid4 = peers[middle]->update.d_refid4;
457 G.refid = 457 G.refid =
458#if ENABLE_FEATURE_IPV6 458#if ENABLE_FEATURE_IPV6
459 peers[middle]->lsa->u.sa.sa_family != AF_INET ? 459 peers[middle]->p_lsa->u.sa.sa_family != AF_INET ?
460 G.refid4 : 460 G.refid4 :
461#endif 461#endif
462 peers[middle]->lsa->u.sin.sin_addr.s_addr; 462 peers[middle]->p_lsa->u.sin.sin_addr.s_addr;
463 free(peers); 463 free(peers);
464 } 464 }
465//TODO: if (offset_median > BIG) step_time(offset_median)? 465//TODO: if (offset_median > BIG) step_time(offset_median)?
@@ -490,7 +490,7 @@ slew_time(void)
490 clear_good: 490 clear_good:
491 for (item = G.ntp_peers; item != NULL; item = item->link) { 491 for (item = G.ntp_peers; item != NULL; item = item->link) {
492 ntp_peer_t *p = (ntp_peer_t *) item->data; 492 ntp_peer_t *p = (ntp_peer_t *) item->data;
493 p->update.o_good = 0; 493 p->update.d_good = 0;
494 } 494 }
495} 495}
496 496
@@ -498,7 +498,7 @@ static void
498update_peer_data(ntp_peer_t *p) 498update_peer_data(ntp_peer_t *p)
499{ 499{
500 /* Clock filter. 500 /* Clock filter.
501 * Find the offset which arrived with the lowest delay. 501 * Find the datapoint with the lowest delay.
502 * Use that as the peer update. 502 * Use that as the peer update.
503 * Invalidate it and all older ones. 503 * Invalidate it and all older ones.
504 */ 504 */
@@ -506,23 +506,23 @@ update_peer_data(ntp_peer_t *p)
506 int best = -1; 506 int best = -1;
507 int good = 0; 507 int good = 0;
508 508
509 for (i = 0; i < OFFSET_ARRAY_SIZE; i++) { 509 for (i = 0; i < NUM_DATAPOINTS; i++) {
510 if (p->reply[i].o_good) { 510 if (p->p_datapoint[i].d_good) {
511 good++; 511 good++;
512 if (best < 0 || p->reply[i].o_delay < p->reply[best].o_delay) 512 if (best < 0 || p->p_datapoint[i].d_delay < p->p_datapoint[best].d_delay)
513 best = i; 513 best = i;
514 } 514 }
515 } 515 }
516 516
517 if (good < 8) //FIXME: was it meant to be OFFSET_ARRAY_SIZE, not 8? 517 if (good < 8) //FIXME: was it meant to be NUM_DATAPOINTS, not 8?
518 return; 518 return;
519 519
520 memcpy(&p->update, &p->reply[best], sizeof(p->update)); 520 memcpy(&p->update, &p->p_datapoint[best], sizeof(p->update));
521 slew_time(); 521 slew_time();
522 522
523 for (i = 0; i < OFFSET_ARRAY_SIZE; i++) 523 for (i = 0; i < NUM_DATAPOINTS; i++)
524 if (p->reply[i].o_rcvd <= p->reply[best].o_rcvd) 524 if (p->p_datapoint[i].d_rcvd <= p->p_datapoint[best].d_rcvd)
525 p->reply[i].o_good = 0; 525 p->p_datapoint[i].d_good = 0;
526} 526}
527 527
528static unsigned 528static unsigned
@@ -536,19 +536,19 @@ scale_interval(unsigned requested)
536static void 536static void
537recv_and_process_peer_pkt(ntp_peer_t *p) 537recv_and_process_peer_pkt(ntp_peer_t *p)
538{ 538{
539 ssize_t size; 539 ssize_t size;
540 ntp_msg_t msg; 540 ntp_msg_t msg;
541 double T1, T2, T3, T4; 541 double T1, T2, T3, T4;
542 unsigned interval; 542 unsigned interval;
543 ntp_offset_t *offset; 543 ntp_datapoint_t *datapoint;
544 544
545 /* We can recvfrom here and check from.IP, but some multihomed 545 /* We can recvfrom here and check from.IP, but some multihomed
546 * ntp servers reply from their *other IP*. 546 * ntp servers reply from their *other IP*.
547 * TODO: maybe we should check at least what we can: from.port == 123? 547 * TODO: maybe we should check at least what we can: from.port == 123?
548 */ 548 */
549 size = recv(p->fd, &msg, sizeof(msg), MSG_DONTWAIT); 549 size = recv(p->p_fd, &msg, sizeof(msg), MSG_DONTWAIT);
550 if (size == -1) { 550 if (size == -1) {
551 bb_perror_msg("recv(%s) error", p->dotted); 551 bb_perror_msg("recv(%s) error", p->p_dotted);
552 if (errno == EHOSTUNREACH || errno == EHOSTDOWN 552 if (errno == EHOSTUNREACH || errno == EHOSTDOWN
553 || errno == ENETUNREACH || errno == ENETDOWN 553 || errno == ENETUNREACH || errno == ENETDOWN
554 || errno == ECONNREFUSED || errno == EADDRNOTAVAIL 554 || errno == ECONNREFUSED || errno == EADDRNOTAVAIL
@@ -562,7 +562,7 @@ recv_and_process_peer_pkt(ntp_peer_t *p)
562 } 562 }
563 563
564 if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) { 564 if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) {
565 bb_error_msg("malformed packet received from %s", p->dotted); 565 bb_error_msg("malformed packet received from %s", p->p_dotted);
566 goto bail; 566 goto bail;
567 } 567 }
568 568
@@ -580,52 +580,52 @@ recv_and_process_peer_pkt(ntp_peer_t *p)
580// "DENY", "RSTR" - peer does not like us at all 580// "DENY", "RSTR" - peer does not like us at all
581// "RATE" - peer is overloaded, reduce polling freq 581// "RATE" - peer is overloaded, reduce polling freq
582 interval = error_interval(); 582 interval = error_interval();
583 bb_error_msg("reply from %s: not synced, next query in %us", p->dotted, interval); 583 bb_error_msg("reply from %s: not synced, next query in %us", p->p_dotted, interval);
584 goto close_sock; 584 goto close_sock;
585 } 585 }
586 586
587 /* 587 /*
588 * From RFC 2030 (with a correction to the delay math): 588 * From RFC 2030 (with a correction to the delay math):
589 * 589 *
590 * Timestamp Name ID When Generated 590 * Timestamp Name ID When Generated
591 * ------------------------------------------------------------ 591 * ------------------------------------------------------------
592 * Originate Timestamp T1 time request sent by client 592 * Originate Timestamp T1 time request sent by client
593 * Receive Timestamp T2 time request received by server 593 * Receive Timestamp T2 time request received by server
594 * Transmit Timestamp T3 time reply sent by server 594 * Transmit Timestamp T3 time reply sent by server
595 * Destination Timestamp T4 time reply received by client 595 * Destination Timestamp T4 time reply received by client
596 * 596 *
597 * The roundtrip delay d and local clock offset t are defined as 597 * The roundtrip delay and local clock offset are defined as
598 * 598 *
599 * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2. 599 * delay = (T4 - T1) - (T3 - T2); offset = ((T2 - T1) + (T3 - T4)) / 2
600 */ 600 */
601 T4 = gettime1900d(); 601 T4 = gettime1900d();
602 T1 = p->xmttime; 602 T1 = p->p_xmttime;
603 T2 = lfp_to_d(msg.m_rectime); 603 T2 = lfp_to_d(msg.m_rectime);
604 T3 = lfp_to_d(msg.m_xmttime); 604 T3 = lfp_to_d(msg.m_xmttime);
605 605
606 offset = &p->reply[p->shift]; 606 datapoint = &p->p_datapoint[p->p_datapoint_idx];
607 607
608 offset->o_offset = ((T2 - T1) + (T3 - T4)) / 2; 608 datapoint->d_offset = ((T2 - T1) + (T3 - T4)) / 2;
609 offset->o_delay = (T4 - T1) - (T3 - T2); 609 datapoint->d_delay = (T4 - T1) - (T3 - T2);
610 if (offset->o_delay < 0) { 610 if (datapoint->d_delay < 0) {
611 bb_error_msg("reply from %s: negative delay %f", p->dotted, offset->o_delay); 611 bb_error_msg("reply from %s: negative delay %f", p->p_dotted, datapoint->d_delay);
612 interval = error_interval(); 612 interval = error_interval();
613 set_next(p, interval); 613 set_next(p, interval);
614 goto close_sock; 614 goto close_sock;
615 } 615 }
616 //UNUSED: offset->o_error = (T2 - T1) - (T3 - T4); 616 //UNUSED: datapoint->d_error = (T2 - T1) - (T3 - T4);
617 offset->o_rcvd = (time_t)(T4 - OFFSET_1900_1970); /* = time(NULL); */ 617 datapoint->d_rcvd = (time_t)(T4 - OFFSET_1900_1970); /* = time(NULL); */
618 offset->o_good = 1; 618 datapoint->d_good = 1;
619 619
620 offset->o_leap = (msg.m_status & LI_MASK); 620 datapoint->d_leap = (msg.m_status & LI_MASK);
621 //UNUSED: offset->o_precision = msg.m_precision; 621 //UNUSED: datapoint->o_precision = msg.m_precision;
622 //UNUSED: offset->o_rootdelay = sfp_to_d(msg.m_rootdelay); 622 //UNUSED: datapoint->o_rootdelay = sfp_to_d(msg.m_rootdelay);
623 //UNUSED: offset->o_rootdispersion = sfp_to_d(msg.m_dispersion); 623 //UNUSED: datapoint->o_rootdispersion = sfp_to_d(msg.m_dispersion);
624 //UNUSED: offset->o_refid = ntohl(msg.m_refid); 624 //UNUSED: datapoint->d_refid = ntohl(msg.m_refid);
625 offset->o_refid4 = msg.m_xmttime.fractionl; 625 datapoint->d_refid4 = msg.m_xmttime.fractionl;
626 //UNUSED: offset->o_reftime = lfp_to_d(msg.m_reftime); 626 //UNUSED: datapoint->o_reftime = lfp_to_d(msg.m_reftime);
627 //UNUSED: offset->o_poll = msg.m_ppoll; 627 //UNUSED: datapoint->o_poll = msg.m_ppoll;
628 offset->o_stratum = msg.m_stratum; 628 datapoint->d_stratum = msg.m_stratum;
629 629
630 if (p->trustlevel < TRUSTLEVEL_PATHETIC) 630 if (p->trustlevel < TRUSTLEVEL_PATHETIC)
631 interval = scale_interval(INTERVAL_QUERY_PATHETIC); 631 interval = scale_interval(INTERVAL_QUERY_PATHETIC);
@@ -640,28 +640,28 @@ recv_and_process_peer_pkt(ntp_peer_t *p)
640 if (p->trustlevel < TRUSTLEVEL_MAX) { 640 if (p->trustlevel < TRUSTLEVEL_MAX) {
641 p->trustlevel++; 641 p->trustlevel++;
642 if (p->trustlevel == TRUSTLEVEL_BADPEER) 642 if (p->trustlevel == TRUSTLEVEL_BADPEER)
643 bb_error_msg("peer %s now valid", p->dotted); 643 bb_error_msg("peer %s now valid", p->p_dotted);
644 } 644 }
645 645
646 if (G.verbose) 646 if (G.verbose)
647 bb_error_msg("reply from %s: offset %f delay %f, next query in %us", p->dotted, 647 bb_error_msg("reply from %s: offset %f delay %f, next query in %us", p->p_dotted,
648 offset->o_offset, offset->o_delay, interval); 648 datapoint->d_offset, datapoint->d_delay, interval);
649 649
650 update_peer_data(p); 650 update_peer_data(p);
651//TODO: do it after all peers had a chance to return at least one reply? 651//TODO: do it after all peers had a chance to return at least one reply?
652 step_time_once(offset->o_offset); 652 step_time_once(datapoint->d_offset);
653 653
654 p->shift++; 654 p->p_datapoint_idx++;
655 if (p->shift >= OFFSET_ARRAY_SIZE) 655 if (p->p_datapoint_idx >= NUM_DATAPOINTS)
656 p->shift = 0; 656 p->p_datapoint_idx = 0;
657 657
658 close_sock: 658 close_sock:
659 /* We do not expect any more packets from this peer for now. 659 /* We do not expect any more packets from this peer for now.
660 * Closing the socket informs kernel about it. 660 * Closing the socket informs kernel about it.
661 * We open a new socket when we send a new query. 661 * We open a new socket when we send a new query.
662 */ 662 */
663 close(p->fd); 663 close(p->p_fd);
664 p->fd = -1; 664 p->p_fd = -1;
665 bail: 665 bail:
666 return; 666 return;
667} 667}
@@ -840,7 +840,7 @@ static NOINLINE void ntp_init(char **argv)
840 if (!(opts & (OPT_p|OPT_l))) 840 if (!(opts & (OPT_p|OPT_l)))
841 bb_show_usage(); 841 bb_show_usage();
842 if (opts & OPT_x) /* disable stepping, only slew is allowed */ 842 if (opts & OPT_x) /* disable stepping, only slew is allowed */
843 G.time_is_stepped = 1; 843 G.time_was_stepped = 1;
844 while (peers) 844 while (peers)
845 add_peers(llist_pop(&peers)); 845 add_peers(llist_pop(&peers));
846 if (!(opts & OPT_n)) { 846 if (!(opts & OPT_n)) {
@@ -872,7 +872,7 @@ static NOINLINE void ntp_init(char **argv)
872 clock_getres(CLOCK_REALTIME, &tp); 872 clock_getres(CLOCK_REALTIME, &tp);
873 tp.tv_sec = 0; 873 tp.tv_sec = 0;
874 tp.tv_nsec = 10000000; 874 tp.tv_nsec = 10000000;
875 b = 1000000000 / tp.tv_nsec; /* convert to Hz */ 875 b = 1000000000 / tp.tv_nsec; /* convert to Hz */
876# else 876# else
877 b = 100; /* b = 1000000000/10000000 = 100 */ 877 b = 100; /* b = 1000000000/10000000 = 100 */
878# endif 878# endif
@@ -934,22 +934,22 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
934 934
935 /* Overflow-safe "if (p->next_action_time <= cur_time) ..." */ 935 /* Overflow-safe "if (p->next_action_time <= cur_time) ..." */
936 if ((int)(cur_time - p->next_action_time) >= 0) { 936 if ((int)(cur_time - p->next_action_time) >= 0) {
937 if (p->fd == -1) { 937 if (p->p_fd == -1) {
938 /* Time to send new req */ 938 /* Time to send new req */
939 trial_cnt++; 939 trial_cnt++;
940 if (send_query_to_peer(p) == 0) 940 if (send_query_to_peer(p) == 0)
941 sent_cnt++; 941 sent_cnt++;
942 } else { 942 } else {
943 /* Timed out waiting for reply */ 943 /* Timed out waiting for reply */
944 close(p->fd); 944 close(p->p_fd);
945 p->fd = -1; 945 p->p_fd = -1;
946 timeout = error_interval(); 946 timeout = error_interval();
947 bb_error_msg("timed out waiting for %s, " 947 bb_error_msg("timed out waiting for %s, "
948 "next query in %us", p->dotted, timeout); 948 "next query in %us", p->p_dotted, timeout);
949 if (p->trustlevel >= TRUSTLEVEL_BADPEER) { 949 if (p->trustlevel >= TRUSTLEVEL_BADPEER) {
950 p->trustlevel /= 2; 950 p->trustlevel /= 2;
951 if (p->trustlevel < TRUSTLEVEL_BADPEER) 951 if (p->trustlevel < TRUSTLEVEL_BADPEER)
952 bb_error_msg("peer %s now invalid", p->dotted); 952 bb_error_msg("peer %s now invalid", p->p_dotted);
953 } 953 }
954 set_next(p, timeout); 954 set_next(p, timeout);
955 } 955 }
@@ -958,9 +958,9 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
958 if (p->next_action_time < nextaction) 958 if (p->next_action_time < nextaction)
959 nextaction = p->next_action_time; 959 nextaction = p->next_action_time;
960 960
961 if (p->fd >= 0) { 961 if (p->p_fd >= 0) {
962 /* Wait for reply from this peer */ 962 /* Wait for reply from this peer */
963 pfd[i].fd = p->fd; 963 pfd[i].fd = p->p_fd;
964 pfd[i].events = POLLIN; 964 pfd[i].events = POLLIN;
965 idx2peer[i] = p; 965 idx2peer[i] = p;
966 i++; 966 i++;