2 * ntpdate - set the time of day by polling one or more NTP servers
10 #include <netinfo/ni.h>
13 #include "ntp_machine.h"
17 #include "timevalops.h"
19 #include "ntp_string.h"
20 #include "ntp_syslog.h"
21 #include "ntp_select.h"
22 #include "ntp_stdlib.h"
23 #include <ssl_applink.c>
26 #include "isc/result.h"
27 #include "isc/sockaddr.h"
39 #ifdef HAVE_SYS_SIGNAL_H
40 # include <sys/signal.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
45 #ifdef HAVE_SYS_RESOURCE_H
46 # include <sys/resource.h>
49 #include <arpa/inet.h>
56 /* select wants a zero structure ... */
57 struct timeval timeout = {0,0};
58 #elif defined(SYS_WINNT)
60 * Windows does not abort a select select call if SIGALRM goes off
61 * so a 200 ms timeout is needed (TIMER_HZ is 5).
63 struct sock_timeval timeout = {0,1000000/TIMER_HZ};
65 struct timeval timeout = {60,0};
69 #include <netinfo/ni.h>
75 #define TARGET_RESOLUTION 1 /* Try for 1-millisecond accuracy
76 on Windows NT timers. */
77 #pragma comment(lib, "winmm")
78 isc_boolean_t ntp_port_inuse(int af, u_short port);
80 #endif /* SYS_WINNT */
83 * Scheduling priority we run at
86 # define NTPDATE_PRIO (-12)
88 # define NTPDATE_PRIO (100)
91 #ifdef HAVE_TIMER_CREATE
92 /* POSIX TIMERS - vxWorks doesn't have itimer - casey */
93 static timer_t ntpdate_timerid;
97 * Compatibility stuff for Version 2
99 #define NTP_MAXSKW 0x28f /* 0.01 sec in fp format */
100 #define NTP_MINDIST 0x51f /* 0.02 sec in fp format */
101 #define PEER_MAXDISP (64*FP_SECOND) /* maximum dispersion (fp 64) */
102 #define NTP_INFIN 15 /* max stratum, infinity a la Bellman-Ford */
103 #define NTP_MAXWGT (8*FP_SECOND) /* maximum select weight 8 seconds */
104 #define NTP_MAXLIST 5 /* maximum select list size */
105 #define PEER_SHIFT 8 /* 8 suitable for crystal time base */
110 s_char sys_precision; /* local clock precision (log2 s) */
113 * File descriptor masks etc. for call to select
117 int nbsock; /* the number of sockets used */
119 int fd_family[MAX_AF]; /* to remember the socket family */
121 struct pollfd fdmask[MAX_AF];
129 * Initializing flag. All async routines watch this and only do their
130 * thing when it is clear.
132 int initializing = 1;
135 * Alarm flag. Set when an alarm occurs
137 volatile int alarm_flag = 0;
142 int simple_query = 0;
145 * Unprivileged port flag.
152 char const *progname;
155 * Systemwide parameters and flags
157 int sys_samples = 0; /* number of samples/server, will be modified later */
158 u_long sys_timeout = DEFTIMEOUT; /* timeout time, in TIMER_HZ units */
159 struct server *sys_servers; /* the server list */
160 int sys_numservers = 0; /* number of servers to poll */
161 int sys_authenticate = 0; /* true when authenticating */
162 u_int32 sys_authkey = 0; /* set to authentication key in use */
163 u_long sys_authdelay = 0; /* authentication delay */
164 int sys_version = NTP_VERSION; /* version to poll with */
167 * The current internal time
169 u_long current_time = 0;
172 * Counter for keeping track of completed servers
174 int complete_servers = 0;
177 * File of encryption keys
182 #define KEYFILE "/etc/ntp.keys"
184 #define KEYFILE "%windir%\\ntp.keys"
185 # endif /* SYS_WINNT */
189 const char *key_file = KEYFILE;
191 char key_file_storage[MAX_PATH+1], *key_file ;
192 #endif /* SYS_WINNT */
195 * Miscellaneous flags
201 int ntpdatemain (int, char **);
203 static void transmit (struct server *);
204 static void receive (struct recvbuf *);
205 static void server_data (struct server *, s_fp, l_fp *, u_fp);
206 static void clock_filter (struct server *);
207 static struct server *clock_select (void);
208 static int clock_adjust (void);
209 static void addserver (char *);
210 static struct server *findserver (sockaddr_u *);
212 static void init_alarm (void);
214 static RETSIGTYPE alarming (int);
215 #endif /* SYS_WINNT */
216 static void init_io (void);
217 static void sendpkt (sockaddr_u *, struct pkt *, int);
218 void input_handler (void);
220 static int l_adj_systime (l_fp *);
221 static int l_step_systime (l_fp *);
223 static void print_server (struct server *, FILE *);
227 WORD wVersionRequested;
229 #endif /* SYS_WINNT */
231 #ifdef NO_MAIN_ALLOWED
232 CALL(ntpdate,"ntpdate",ntpdatemain);
243 * Initializing flag. All async routines watch this and only do their
244 * thing when it is clear.
249 * Alarm flag. Set when an alarm occurs
259 * Unprivileged port flag.
264 * Systemwide parameters and flags
266 sys_numservers = 0; /* number of servers to poll */
267 sys_authenticate = 0; /* true when authenticating */
268 sys_authkey = 0; /* set to authentication key in use */
269 sys_authdelay = 0; /* authentication delay */
270 sys_version = NTP_VERSION; /* version to poll with */
273 * The current internal time
278 * Counter for keeping track of completed servers
280 complete_servers = 0;
288 static ni_namelist *getnetinfoservers (void);
292 * Main program. Initialize us and loop waiting for I/O and/or
295 #ifndef NO_MAIN_ALLOWED
302 return ntpdatemain (argc, argv);
304 #endif /* NO_MAIN_ALLOWED */
314 struct recvbuf *rbuf;
321 ni_namelist *netinfoservers;
324 key_file = key_file_storage;
326 if (!ExpandEnvironmentStrings(KEYFILE, key_file, MAX_PATH))
327 msyslog(LOG_ERR, "ExpandEnvironmentStrings(KEYFILE) failed: %m");
330 #endif /* SYS_WINNT */
332 #ifdef NO_MAIN_ALLOWED
336 init_lib(); /* sets up ipv4_works, ipv6_works */
338 /* Check to see if we have IPv6. Otherwise default to IPv4 */
340 ai_fam_templ = AF_INET;
343 errflg = 0; /* servers can come from netinfo */
345 errflg = (argc < 2); /* need at least server on cmdline */
351 * Decode argument list
353 while ((c = ntp_getopt(argc, argv, "46a:bBde:k:o:p:qst:uv")) != EOF)
357 ai_fam_templ = AF_INET;
360 ai_fam_templ = AF_INET6;
363 c = atoi(ntp_optarg);
364 sys_authenticate = 1;
379 if (!atolfp(ntp_optarg, &tmp)
381 (void) fprintf(stderr,
382 "%s: encryption delay %s is unlikely\n",
383 progname, ntp_optarg);
386 sys_authdelay = tmp.l_uf;
390 key_file = ntp_optarg;
393 sys_version = atoi(ntp_optarg);
396 c = atoi(ntp_optarg);
397 if (c <= 0 || c > NTP_SHIFT) {
398 (void) fprintf(stderr,
399 "%s: number of samples (%d) is invalid\n",
413 if (!atolfp(ntp_optarg, &tmp)) {
414 (void) fprintf(stderr,
415 "%s: timeout %s is undecodeable\n",
416 progname, ntp_optarg);
419 sys_timeout = ((LFPTOFP(&tmp) * TIMER_HZ)
421 sys_timeout = max(sys_timeout, MINTIMEOUT);
438 (void) fprintf(stderr,
439 "usage: %s [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] server ...\n",
445 * If number of Samples (-p) not specified by user:
446 * - if a simple_query (-q) just ONE will do
447 * - otherwise the normal is DEFSAMPLES
449 if (sys_samples == 0)
450 sys_samples = (simple_query ? 1 : DEFSAMPLES);
452 if (debug || simple_query) {
454 static char buf[BUFSIZ];
455 setvbuf(stdout, buf, _IOLBF, BUFSIZ);
462 * Logging. Open the syslog if we have to
465 #if !defined (SYS_WINNT) && !defined (SYS_VXWORKS) && !defined SYS_CYGWIN32
467 openlog("ntpdate", LOG_PID);
471 # define LOG_NTP LOG_DAEMON
473 openlog("ntpdate", LOG_PID | LOG_NDELAY, LOG_NTP);
475 setlogmask(LOG_UPTO(LOG_DEBUG));
477 setlogmask(LOG_UPTO(LOG_INFO));
478 # endif /* LOG_DAEMON */
479 #endif /* SYS_WINNT */
482 if (debug || verbose)
483 msyslog(LOG_NOTICE, "%s", Version);
486 * Add servers we are going to be polling
489 netinfoservers = getnetinfoservers();
492 for ( ; ntp_optind < argc; ntp_optind++)
493 addserver(argv[ntp_optind]);
496 if (netinfoservers) {
497 if ( netinfoservers->ni_namelist_len &&
498 *netinfoservers->ni_namelist_val ) {
499 u_int servercount = 0;
500 while (servercount < netinfoservers->ni_namelist_len) {
501 if (debug) msyslog(LOG_DEBUG,
502 "Adding time server %s from NetInfo configuration.",
503 netinfoservers->ni_namelist_val[servercount]);
504 addserver(netinfoservers->ni_namelist_val[servercount++]);
507 ni_namelist_free(netinfoservers);
508 free(netinfoservers);
512 if (sys_numservers == 0) {
513 msyslog(LOG_ERR, "no servers can be used, exiting");
518 * Initialize the time of day routines and the I/O subsystem
520 if (sys_authenticate) {
522 if (!authreadkeys(key_file)) {
523 msyslog(LOG_ERR, "no key file <%s>, exiting", key_file);
526 authtrust(sys_authkey, 1);
527 if (!authistrusted(sys_authkey)) {
528 msyslog(LOG_ERR, "authentication key %lu unknown",
529 (unsigned long) sys_authkey);
540 taskPrioritySet( taskIdSelf(), NTPDATE_PRIO);
542 #if defined(HAVE_ATT_NICE)
545 #if defined(HAVE_BSD_NICE)
546 (void) setpriority(PRIO_PROCESS, 0, NTPDATE_PRIO);
553 while (complete_servers < sys_numservers) {
555 struct pollfd* rdfdes;
562 if (alarm_flag) { /* alarmed? */
566 tot_recvbufs = full_recvbuffs(); /* get received buffers */
568 if (!was_alarmed && tot_recvbufs == 0) {
570 * Nothing to do. Wait for something.
573 nfound = poll(rdfdes, (unsigned int)nbsock, timeout.tv_sec * 1000);
576 nfound = select(maxfd, &rdfdes, NULL, NULL,
581 else if (nfound == SOCKET_ERROR)
586 if (WSAGetLastError() != WSAEINTR)
595 } else if (errno != 0) {
599 "poll(): nfound = %d, error: %m",
601 "select(): nfound = %d, error: %m",
606 if (alarm_flag) { /* alarmed? */
610 tot_recvbufs = full_recvbuffs(); /* get received buffers */
614 * Out here, signals are unblocked. Call receive
615 * procedure for each incoming packet.
617 rbuf = get_full_recv_buffer();
622 rbuf = get_full_recv_buffer();
626 * Call timer to process any timeouts
639 * When we get here we've completed the polling of all servers.
640 * Adjust the clock, then exit.
647 timer_delete(ntpdate_timerid);
650 return clock_adjust();
655 * transmit - transmit a packet to the given server, or mark it completed.
656 * This is called by the timeout routine and by the receive
661 register struct server *server
666 if (server->filter_nextpt < server->xmtcnt) {
669 * Last message to this server timed out. Shift
670 * zeros into the filter.
673 server_data(server, 0, &ts, 0);
676 if ((int)server->filter_nextpt >= sys_samples) {
678 * Got all the data we need. Mark this guy
679 * completed and return.
681 server->event_time = 0;
687 printf("transmit(%s)\n", stoa(&server->srcadr));
690 * If we're here, send another message to the server. Fill in
691 * the packet and let 'er rip.
693 xpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
694 sys_version, MODE_CLIENT);
695 xpkt.stratum = STRATUM_TO_PKT(STRATUM_UNSPEC);
696 xpkt.ppoll = NTP_MINPOLL;
697 xpkt.precision = NTPDATE_PRECISION;
698 xpkt.rootdelay = htonl(NTPDATE_DISTANCE);
699 xpkt.rootdisp = htonl(NTPDATE_DISP);
700 xpkt.refid = htonl(NTPDATE_REFID);
701 L_CLR(&xpkt.reftime);
706 * Determine whether to authenticate or not. If so,
707 * fill in the extended part of the packet and do it.
708 * If not, just timestamp it and send it away.
710 if (sys_authenticate) {
713 xpkt.exten[0] = htonl(sys_authkey);
714 get_systime(&server->xmt);
715 L_ADDUF(&server->xmt, sys_authdelay);
716 HTONL_FP(&server->xmt, &xpkt.xmt);
717 len = authencrypt(sys_authkey, (u_int32 *)&xpkt, LEN_PKT_NOMAC);
718 sendpkt(&server->srcadr, &xpkt, (int)(LEN_PKT_NOMAC + len));
721 printf("transmit auth to %s\n",
722 stoa(&server->srcadr));
724 get_systime(&(server->xmt));
725 HTONL_FP(&server->xmt, &xpkt.xmt);
726 sendpkt(&server->srcadr, &xpkt, LEN_PKT_NOMAC);
729 printf("transmit to %s\n", stoa(&server->srcadr));
733 * Update the server timeout and transmit count
735 server->event_time = current_time + sys_timeout;
741 * receive - receive and process an incoming frame
745 struct recvbuf *rbufp
748 register struct pkt *rpkt;
749 register struct server *server;
759 printf("receive(%s)\n", stoa(&rbufp->recv_srcadr));
761 * Check to see if the packet basically looks like something
764 if (rbufp->recv_length == LEN_PKT_NOMAC)
766 else if (rbufp->recv_length >= (int)LEN_PKT_NOMAC)
770 printf("receive: packet length %d\n",
772 return; /* funny length packet */
775 rpkt = &(rbufp->recv_pkt);
776 if (PKT_VERSION(rpkt->li_vn_mode) < NTP_OLDVERSION ||
777 PKT_VERSION(rpkt->li_vn_mode) > NTP_VERSION) {
781 if ((PKT_MODE(rpkt->li_vn_mode) != MODE_SERVER
782 && PKT_MODE(rpkt->li_vn_mode) != MODE_PASSIVE)
783 || rpkt->stratum >= STRATUM_UNSPEC) {
785 printf("receive: mode %d stratum %d\n",
786 PKT_MODE(rpkt->li_vn_mode), rpkt->stratum);
791 * So far, so good. See if this is from a server we know.
793 server = findserver(&(rbufp->recv_srcadr));
794 if (server == NULL) {
796 printf("receive: server not found\n");
801 * Decode the org timestamp and make sure we're getting a response
802 * to our last request.
804 NTOHL_FP(&rpkt->org, &org);
805 if (!L_ISEQU(&org, &server->xmt)) {
807 printf("receive: pkt.org and peer.xmt differ\n");
812 * Check out the authenticity if we're doing that.
814 if (!sys_authenticate)
820 printf("receive: rpkt keyid=%ld sys_authkey=%ld decrypt=%ld\n",
821 (long int)ntohl(rpkt->exten[0]), (long int)sys_authkey,
822 (long int)authdecrypt(sys_authkey, (u_int32 *)rpkt,
823 LEN_PKT_NOMAC, (size_t)(rbufp->recv_length - LEN_PKT_NOMAC)));
825 if (has_mac && ntohl(rpkt->exten[0]) == sys_authkey &&
826 authdecrypt(sys_authkey, (u_int32 *)rpkt, LEN_PKT_NOMAC,
827 (size_t)(rbufp->recv_length - LEN_PKT_NOMAC)))
830 printf("receive: authentication %s\n",
831 is_authentic ? "passed" : "failed");
838 * Check for a KoD (rate limiting) response, cease and decist.
840 if (LEAP_NOTINSYNC == PKT_LEAP(rpkt->li_vn_mode) &&
841 STRATUM_PKT_UNSPEC == rpkt->stratum &&
842 !memcmp("RATE", &rpkt->refid, 4)) {
843 msyslog(LOG_ERR, "%s rate limit response from server.",
844 stoa(&rbufp->recv_srcadr));
845 server->event_time = 0;
851 * Looks good. Record info from the packet.
853 server->leap = PKT_LEAP(rpkt->li_vn_mode);
854 server->stratum = PKT_TO_STRATUM(rpkt->stratum);
855 server->precision = rpkt->precision;
856 server->rootdelay = ntohl(rpkt->rootdelay);
857 server->rootdisp = ntohl(rpkt->rootdisp);
858 server->refid = rpkt->refid;
859 NTOHL_FP(&rpkt->reftime, &server->reftime);
860 NTOHL_FP(&rpkt->rec, &rec);
861 NTOHL_FP(&rpkt->xmt, &server->org);
864 * Make sure the server is at least somewhat sane. If not, try
867 if (L_ISZERO(&rec) || !L_ISHIS(&server->org, &rec)) {
868 server->event_time = current_time + sys_timeout;
873 * Calculate the round trip delay (di) and the clock offset (ci).
874 * We use the equations (reordered from those in the spec):
876 * d = (t2 - t3) - (t1 - t0)
877 * c = ((t2 - t3) + (t1 - t0)) / 2
879 t10 = server->org; /* pkt.xmt == t1 */
880 L_SUB(&t10, &rbufp->recv_time); /* recv_time == t0*/
882 t23 = rec; /* pkt.rec == t2 */
883 L_SUB(&t23, &org); /* pkt->org == t3 */
885 /* now have (t2 - t3) and (t0 - t1). Calculate (ci) and (di) */
887 * Calculate (ci) = ((t1 - t0) / 2) + ((t2 - t3) / 2)
888 * For large offsets this may prevent an overflow on '+'
897 * Calculate di in t23 in full precision, then truncate
904 printf("offset: %s, delay %s\n", lfptoa(&ci, 6), fptoa(di, 5));
906 di += (FP_SECOND >> (-(int)NTPDATE_PRECISION))
907 + (FP_SECOND >> (-(int)server->precision)) + NTP_MAXSKW;
909 if (di <= 0) { /* value still too raunchy to use? */
913 di = max(di, NTP_MINDIST);
917 * Shift this data in, then schedule another transmit.
919 server_data(server, (s_fp) di, &ci, 0);
921 if ((int)server->filter_nextpt >= sys_samples) {
923 * Got all the data we need. Mark this guy
924 * completed and return.
926 server->event_time = 0;
931 server->event_time = current_time + sys_timeout;
936 * server_data - add a sample to the server's filter registers
940 register struct server *server,
948 i = server->filter_nextpt;
950 server->filter_delay[i] = d;
951 server->filter_offset[i] = *c;
952 server->filter_soffset[i] = LFPTOFP(c);
953 server->filter_error[i] = e;
954 server->filter_nextpt = (u_short)(i + 1);
960 * clock_filter - determine a server's delay, dispersion and offset
964 register struct server *server
970 INSIST((0 < sys_samples) && (sys_samples <= NTP_SHIFT));
973 * Sort indices into increasing delay order
975 for (i = 0; i < sys_samples; i++)
978 for (i = 0; i < (sys_samples-1); i++) {
979 for (j = i+1; j < sys_samples; j++) {
980 if (server->filter_delay[ord[j]] == 0)
982 if (server->filter_delay[ord[i]] == 0
983 || (server->filter_delay[ord[i]]
984 > server->filter_delay[ord[j]])) {
995 * Now compute the dispersion, and assign values to delay and
996 * offset. If there are no samples in the register, delay and
997 * offset go to zero and dispersion is set to the maximum.
999 if (server->filter_delay[ord[0]] == 0) {
1001 L_CLR(&server->offset);
1002 server->soffset = 0;
1003 server->dispersion = PEER_MAXDISP;
1007 server->delay = server->filter_delay[ord[0]];
1008 server->offset = server->filter_offset[ord[0]];
1009 server->soffset = LFPTOFP(&server->offset);
1010 server->dispersion = 0;
1011 for (i = 1; i < sys_samples; i++) {
1012 if (server->filter_delay[ord[i]] == 0)
1015 d = server->filter_soffset[ord[i]]
1016 - server->filter_soffset[ord[0]];
1019 if (d > PEER_MAXDISP)
1023 * XXX This *knows* PEER_FILTER is 1/2
1025 server->dispersion += (u_fp)(d) >> i;
1035 * clock_select - select the pick-of-the-litter clock from the samples
1038 static struct server *
1041 struct server *server;
1049 s_fp local_threshold;
1050 struct server *server_list[NTP_MAXCLOCK];
1051 u_fp server_badness[NTP_MAXCLOCK];
1052 struct server *sys_server;
1055 * This first chunk of code is supposed to go through all
1056 * servers we know about to find the NTP_MAXLIST servers which
1057 * are most likely to succeed. We run through the list
1058 * doing the sanity checks and trying to insert anyone who
1059 * looks okay. We are at all times aware that we should
1060 * only keep samples from the top two strata and we only need
1061 * NTP_MAXLIST of them.
1063 nlist = 0; /* none yet */
1064 for (server = sys_servers; server != NULL; server = server->next_server) {
1065 if (server->stratum == 0) {
1067 printf("%s: Server dropped: no data\n", ntoa(&server->srcadr));
1068 continue; /* no data */
1070 if (server->stratum > NTP_INFIN) {
1072 printf("%s: Server dropped: strata too high\n", ntoa(&server->srcadr));
1073 continue; /* stratum no good */
1075 if (server->delay > NTP_MAXWGT) {
1077 printf("%s: Server dropped: server too far away\n",
1078 ntoa(&server->srcadr));
1079 continue; /* too far away */
1081 if (server->leap == LEAP_NOTINSYNC) {
1083 printf("%s: Server dropped: leap not in sync\n", ntoa(&server->srcadr));
1084 continue; /* he's in trouble */
1086 if (!L_ISHIS(&server->org, &server->reftime)) {
1088 printf("%s: Server dropped: server is very broken\n",
1089 ntoa(&server->srcadr));
1090 continue; /* very broken host */
1092 if ((server->org.l_ui - server->reftime.l_ui)
1095 printf("%s: Server dropped: server has gone too long without sync\n",
1096 ntoa(&server->srcadr));
1097 continue; /* too long without sync */
1099 if (server->trust != 0) {
1101 printf("%s: Server dropped: Server is untrusted\n",
1102 ntoa(&server->srcadr));
1107 * This one seems sane. Find where he belongs
1110 d = server->dispersion + server->dispersion;
1111 for (i = 0; i < nlist; i++)
1112 if (server->stratum <= server_list[i]->stratum)
1114 for ( ; i < nlist; i++) {
1115 if (server->stratum < server_list[i]->stratum)
1117 if (d < (s_fp) server_badness[i])
1122 * If i points past the end of the list, this
1123 * guy is a loser, else stick him in.
1125 if (i >= NTP_MAXLIST)
1127 for (j = nlist; j > i; j--)
1128 if (j < NTP_MAXLIST) {
1129 server_list[j] = server_list[j-1];
1131 = server_badness[j-1];
1134 server_list[i] = server;
1135 server_badness[i] = d;
1136 if (nlist < NTP_MAXLIST)
1141 * Got the five-or-less best. Cut the list where the number of
1142 * strata exceeds two.
1145 for (i = 1; i < nlist; i++)
1146 if (server_list[i]->stratum > server_list[i-1]->stratum) {
1155 * Whew! What we should have by now is 0 to 5 candidates for
1156 * the job of syncing us. If we have none, we're out of luck.
1157 * If we have one, he's a winner. If we have more, do falseticker
1163 else if (1 == nlist) {
1164 sys_server = server_list[0];
1167 * Re-sort by stratum, bdelay estimate quality and
1170 for (i = 0; i < nlist-1; i++)
1171 for (j = i+1; j < nlist; j++) {
1172 if (server_list[i]->stratum <
1173 server_list[j]->stratum)
1174 /* already sorted by stratum */
1176 if (server_list[i]->delay <
1177 server_list[j]->delay)
1179 server = server_list[i];
1180 server_list[i] = server_list[j];
1181 server_list[j] = server;
1185 * Calculate the fixed part of the dispersion limit
1187 local_threshold = (FP_SECOND >> (-(int)NTPDATE_PRECISION))
1191 * Now drop samples until we're down to one.
1194 for (k = 0; k < nlist; k++) {
1195 server_badness[k] = 0;
1196 for (j = 0; j < nlist; j++) {
1197 if (j == k) /* with self? */
1199 d = server_list[j]->soffset -
1200 server_list[k]->soffset;
1201 if (d < 0) /* abs value */
1204 * XXX This code *knows* that
1207 for (i = 0; i < j; i++)
1208 d = (d>>1) + (d>>2);
1209 server_badness[k] += d;
1214 * We now have an array of nlist badness
1215 * coefficients. Find the badest. Find
1216 * the minimum precision while we're at
1220 n = server_list[0]->precision;;
1221 for (j = 1; j < nlist; j++) {
1222 if (server_badness[j] >= server_badness[i])
1224 if (n > server_list[j]->precision)
1225 n = server_list[j]->precision;
1229 * i is the index of the server with the worst
1230 * dispersion. If his dispersion is less than
1231 * the threshold, stop now, else delete him and
1232 * continue around again.
1234 if ( (s_fp) server_badness[i] < (local_threshold
1235 + (FP_SECOND >> (-n))))
1237 for (j = i + 1; j < nlist; j++)
1238 server_list[j-1] = server_list[j];
1243 * What remains is a list of less than 5 servers. Take
1246 sys_server = server_list[0];
1250 * That's it. Return our server.
1257 * clock_adjust - process what we've received, and adjust the time
1258 * if we got anything decent.
1263 register struct server *sp, *server;
1266 for (sp = sys_servers; sp != NULL; sp = sp->next_server)
1268 server = clock_select();
1270 if (debug || simple_query) {
1273 for (sp = sys_servers; sp != NULL; sp = sp->next_server)
1274 print_server(sp, stdout);
1279 "no server suitable for synchronization found");
1285 } else if (never_step) {
1288 /* [Bug 3023] get absolute difference, avoiding signed
1289 * integer overflow like hell.
1292 if (server->soffset < 0)
1293 absoffset = 1u + (u_fp)(-(server->soffset + 1));
1295 absoffset = (u_fp)server->soffset;
1296 dostep = (absoffset >= NTPDATE_THRESHOLD);
1300 if (simple_query || l_step_systime(&server->offset)){
1301 msyslog(LOG_NOTICE, "step time server %s offset %s sec",
1302 stoa(&server->srcadr),
1303 lfptoa(&server->offset, 6));
1306 if (simple_query || l_adj_systime(&server->offset)) {
1307 msyslog(LOG_NOTICE, "adjust time server %s offset %s sec",
1308 stoa(&server->srcadr),
1309 lfptoa(&server->offset, 6));
1317 * is_unreachable - check to see if we have a route to given destination
1321 is_reachable (sockaddr_u *dst)
1325 sockfd = socket(AF(dst), SOCK_DGRAM, 0);
1330 if (connect(sockfd, &dst->sa, SOCKLEN(dst))) {
1331 closesocket(sockfd);
1334 closesocket(sockfd);
1340 /* XXX ELIMINATE: merge BIG slew into adj_systime in lib/systime.c */
1342 * addserver - determine a server's address and allocate a new structure
1350 register struct server *server;
1351 /* Address infos structure to store result of getaddrinfo */
1352 struct addrinfo *addrResult, *ptr;
1353 /* Address infos structure to store hints for getaddrinfo */
1354 struct addrinfo hints;
1355 /* Error variable for getaddrinfo */
1361 strlcpy(service, "ntp", sizeof(service));
1363 /* Get host address. Looking for UDP datagram connection. */
1365 hints.ai_family = ai_fam_templ;
1366 hints.ai_socktype = SOCK_DGRAM;
1370 printf("Looking for host %s and service %s\n", serv, service);
1373 error = getaddrinfo(serv, service, &hints, &addrResult);
1374 if (error == EAI_SERVICE) {
1375 strlcpy(service, "123", sizeof(service));
1376 error = getaddrinfo(serv, service, &hints, &addrResult);
1379 /* Conduct more refined error analysis */
1380 if (error == EAI_FAIL || error == EAI_AGAIN){
1381 /* Name server is unusable. Exit after failing on the
1382 first server, in order to shorten the timeout caused
1383 by waiting for resolution of several servers */
1384 fprintf(stderr, "Exiting, name server cannot be used: %s (%d)",
1385 gai_strerror(error), error);
1386 msyslog(LOG_ERR, "name server cannot be used: %s (%d)",
1387 gai_strerror(error), error);
1390 fprintf(stderr, "Error resolving %s: %s (%d)\n", serv,
1391 gai_strerror(error), error);
1392 msyslog(LOG_ERR, "Can't find host %s: %s (%d)", serv,
1393 gai_strerror(error), error);
1399 INSIST(addrResult->ai_addrlen <= sizeof(addr));
1400 memcpy(&addr, addrResult->ai_addr, addrResult->ai_addrlen);
1401 fprintf(stderr, "host found : %s\n", stohost(&addr));
1405 /* We must get all returned server in case the first one fails */
1406 for (ptr = addrResult; ptr != NULL; ptr = ptr->ai_next) {
1408 INSIST(ptr->ai_addrlen <= sizeof(addr));
1409 memcpy(&addr, ptr->ai_addr, ptr->ai_addrlen);
1410 if (is_reachable(&addr)) {
1411 server = emalloc_zero(sizeof(*server));
1412 memcpy(&server->srcadr, ptr->ai_addr, ptr->ai_addrlen);
1413 server->event_time = ++sys_numservers;
1414 if (sys_servers == NULL)
1415 sys_servers = server;
1419 for (sp = sys_servers; sp->next_server != NULL;
1420 sp = sp->next_server)
1422 sp->next_server = server;
1427 freeaddrinfo(addrResult);
1432 * findserver - find a server in the list given its address
1433 * ***(For now it isn't totally AF-Independant, to check later..)
1435 static struct server *
1440 struct server *server;
1441 struct server *mc_server;
1444 if (SRCPORT(addr) != NTP_PORT)
1447 for (server = sys_servers; server != NULL;
1448 server = server->next_server) {
1449 if (SOCK_EQ(addr, &server->srcadr))
1452 if (AF(addr) == AF(&server->srcadr)) {
1453 if (IS_MCAST(&server->srcadr))
1458 if (mc_server != NULL) {
1462 if (mc_server->event_time != 0) {
1463 mc_server->event_time = 0;
1467 server = emalloc_zero(sizeof(*server));
1469 server->srcadr = *addr;
1471 server->event_time = ++sys_numservers;
1473 for (sp = sys_servers; sp->next_server != NULL;
1474 sp = sp->next_server)
1476 sp->next_server = server;
1484 * timer - process a timer interrupt
1489 struct server *server;
1492 * Bump the current idea of the time
1497 * Search through the server list looking for guys
1498 * who's event timers have expired. Give these to
1499 * the transmit routine.
1501 for (server = sys_servers; server != NULL;
1502 server = server->next_server) {
1503 if (server->event_time != 0
1504 && server->event_time <= current_time)
1511 * The code duplication in the following subroutine sucks, but
1512 * we need to appease ansi2knr.
1517 * alarming - record the occurance of an alarm interrupt
1526 #else /* SYS_WINNT follows */
1528 alarming(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
1530 UNUSED_ARG(uTimerID); UNUSED_ARG(uMsg); UNUSED_ARG(dwUser);
1531 UNUSED_ARG(dw1); UNUSED_ARG(dw2);
1537 callTimeEndPeriod(void)
1539 timeEndPeriod( wTimerRes );
1542 #endif /* SYS_WINNT */
1546 * init_alarm - set up the timer interrupt
1552 # ifdef HAVE_TIMER_CREATE
1553 struct itimerspec its;
1555 struct itimerval itv;
1557 #else /* SYS_WINNT follows */
1561 TOKEN_PRIVILEGES tkp;
1563 #endif /* SYS_WINNT */
1568 # ifdef HAVE_TIMER_CREATE
1570 /* this code was put in as setitimer() is non existant this us the
1571 * POSIX "equivalents" setup - casey
1573 /* ntpdate_timerid is global - so we can kill timer later */
1574 if (timer_create (CLOCK_REALTIME, NULL, &ntpdate_timerid) ==
1582 fprintf (stderr, "init_alarm(): timer_create (...) FAILED\n");
1587 * Set up the alarm interrupt. The first comes 1/(2*TIMER_HZ)
1588 * seconds from now and they continue on every 1/TIMER_HZ seconds.
1590 signal_no_reset(SIGALRM, alarming);
1591 its.it_interval.tv_sec = 0;
1592 its.it_value.tv_sec = 0;
1593 its.it_interval.tv_nsec = 1000000000/TIMER_HZ;
1594 its.it_value.tv_nsec = 1000000000/(TIMER_HZ<<1);
1595 timer_settime(ntpdate_timerid, 0 /* !TIMER_ABSTIME */, &its, NULL);
1596 # else /* !HAVE_TIMER_CREATE follows */
1598 * Set up the alarm interrupt. The first comes 1/(2*TIMER_HZ)
1599 * seconds from now and they continue on every 1/TIMER_HZ seconds.
1601 signal_no_reset(SIGALRM, alarming);
1602 itv.it_interval.tv_sec = 0;
1603 itv.it_value.tv_sec = 0;
1604 itv.it_interval.tv_usec = 1000000/TIMER_HZ;
1605 itv.it_value.tv_usec = 1000000/(TIMER_HZ<<1);
1607 setitimer(ITIMER_REAL, &itv, NULL);
1608 # endif /* !HAVE_TIMER_CREATE */
1609 #else /* SYS_WINNT follows */
1612 if (!simple_query && !debug) {
1614 * Get privileges needed for fiddling with the clock
1617 /* get the current process token handle */
1618 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
1619 msyslog(LOG_ERR, "OpenProcessToken failed: %m");
1622 /* get the LUID for system-time privilege. */
1623 LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkp.Privileges[0].Luid);
1624 tkp.PrivilegeCount = 1; /* one privilege to set */
1625 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1626 /* get set-time privilege for this process. */
1627 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);
1628 /* cannot test return value of AdjustTokenPrivileges. */
1629 if (GetLastError() != ERROR_SUCCESS)
1630 msyslog(LOG_ERR, "AdjustTokenPrivileges failed: %m");
1634 * Set up timer interrupts for every 2**EVENT_TIMEOUT seconds
1635 * Under Win/NT, expiry of timer interval leads to invocation
1636 * of a callback function (on a different thread) rather than
1637 * generating an alarm signal
1640 /* determine max and min resolution supported */
1641 if(timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) {
1642 msyslog(LOG_ERR, "timeGetDevCaps failed: %m");
1645 wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
1646 /* establish the minimum timer resolution that we'll use */
1647 timeBeginPeriod(wTimerRes);
1648 atexit(callTimeEndPeriod);
1650 /* start the timer event */
1651 wTimerID = timeSetEvent(
1652 (UINT) (1000/TIMER_HZ), /* Delay */
1653 wTimerRes, /* Resolution */
1654 (LPTIMECALLBACK) alarming, /* Callback function */
1655 (DWORD) dwUser, /* User data */
1656 TIME_PERIODIC); /* Event type (periodic) */
1657 if (wTimerID == 0) {
1658 msyslog(LOG_ERR, "timeSetEvent failed: %m");
1661 #endif /* SYS_WINNT */
1668 * We do asynchronous input using the SIGIO facility. A number of
1669 * recvbuf buffers are preallocated for input. In the signal
1670 * handler we poll to see if the socket is ready and read the
1671 * packets from it into the recvbuf's along with a time stamp and
1672 * an indication of the source host and the interface it was received
1673 * through. This allows us to get as accurate receive time stamps
1674 * as possible independent of other processing going on.
1676 * We allocate a number of recvbufs equal to the number of servers
1677 * plus 2. This should be plenty.
1682 * init_io - initialize I/O data and open socket
1687 struct addrinfo *res, *ressave;
1688 struct addrinfo hints;
1693 int check_ntp_port_in_use = !debug && !simple_query && !unpriv_port;
1696 * Init buffer free list and stat counters
1698 init_recvbuff(sys_numservers + 2);
1704 strlcpy(service, "ntp", sizeof(service));
1707 * Init hints addrinfo structure
1710 hints.ai_family = ai_fam_templ;
1711 hints.ai_flags = AI_PASSIVE;
1712 hints.ai_socktype = SOCK_DGRAM;
1714 rc = getaddrinfo(NULL, service, &hints, &res);
1715 if (rc == EAI_SERVICE) {
1716 strlcpy(service, "123", sizeof(service));
1717 rc = getaddrinfo(NULL, service, &hints, &res);
1720 msyslog(LOG_ERR, "getaddrinfo() failed: %m");
1726 if (check_ntp_port_in_use && ntp_port_inuse(AF_INET, NTP_PORT)){
1727 msyslog(LOG_ERR, "the NTP socket is in use, exiting: %m");
1732 /* Remember the address of the addrinfo structure chain */
1736 * For each structure returned, open and bind socket
1738 for(nbsock = 0; (nbsock < MAX_AF) && res ; res = res->ai_next) {
1739 /* create a datagram (UDP) socket */
1740 fd[nbsock] = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1741 if (fd[nbsock] == SOCKET_ERROR) {
1743 if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT ||
1744 errno == EPFNOSUPPORT)
1746 int err = WSAGetLastError();
1747 if (err == WSAEPROTONOSUPPORT || err == WSAEAFNOSUPPORT ||
1748 err == WSAEPFNOSUPPORT)
1751 msyslog(LOG_ERR, "socket() failed: %m");
1755 /* set socket to reuse address */
1756 if (setsockopt(fd[nbsock], SOL_SOCKET, SO_REUSEADDR, (void*) &optval, sizeof(optval)) < 0) {
1757 msyslog(LOG_ERR, "setsockopt() SO_REUSEADDR failed: %m");
1762 /* Restricts AF_INET6 socket to IPv6 communications (see RFC 2553bis-03) */
1763 if (res->ai_family == AF_INET6)
1764 if (setsockopt(fd[nbsock], IPPROTO_IPV6, IPV6_V6ONLY, (void*) &optval, sizeof(optval)) < 0) {
1765 msyslog(LOG_ERR, "setsockopt() IPV6_V6ONLY failed: %m");
1769 /* Remember the socket family in fd_family structure */
1770 fd_family[nbsock] = res->ai_family;
1773 * bind the socket to the NTP port
1775 if (check_ntp_port_in_use) {
1777 INSIST(res->ai_addrlen <= sizeof(addr));
1778 memcpy(&addr, res->ai_addr, res->ai_addrlen);
1779 rc = bind(fd[nbsock], &addr.sa, SOCKLEN(&addr));
1781 if (EADDRINUSE == socket_errno())
1782 msyslog(LOG_ERR, "the NTP socket is in use, exiting");
1784 msyslog(LOG_ERR, "bind() fails: %m");
1790 fdmask[nbsock].fd = fd[nbsock];
1791 fdmask[nbsock].events = POLLIN;
1793 FD_SET(fd[nbsock], &fdmask);
1794 if (maxfd < fd[nbsock]+1) {
1795 maxfd = fd[nbsock]+1;
1807 if (ioctl(fd[nbsock],FIONBIO, &on) == ERROR) {
1808 msyslog(LOG_ERR, "ioctl(FIONBIO) fails: %m");
1812 # else /* not SYS_VXWORKS */
1813 # if defined(O_NONBLOCK)
1814 if (fcntl(fd[nbsock], F_SETFL, O_NONBLOCK) < 0) {
1815 msyslog(LOG_ERR, "fcntl(FNDELAY|FASYNC) fails: %m");
1819 # else /* not O_NONBLOCK */
1820 # if defined(FNDELAY)
1821 if (fcntl(fd[nbsock], F_SETFL, FNDELAY) < 0) {
1822 msyslog(LOG_ERR, "fcntl(FNDELAY|FASYNC) fails: %m");
1826 # else /* FNDELAY */
1827 # include "Bletch: Need non blocking I/O"
1828 # endif /* FNDELAY */
1829 # endif /* not O_NONBLOCK */
1830 # endif /* SYS_VXWORKS */
1831 #else /* SYS_WINNT */
1832 if (ioctlsocket(fd[nbsock], FIONBIO, (u_long *) &on) == SOCKET_ERROR) {
1833 msyslog(LOG_ERR, "ioctlsocket(FIONBIO) fails: %m");
1836 #endif /* SYS_WINNT */
1839 freeaddrinfo(ressave);
1843 * sendpkt - send a packet to the specified destination
1854 SOCKET sock = INVALID_SOCKET;
1858 #endif /* SYS_WINNT */
1860 /* Find a local family compatible socket to send ntp packet to ntp server */
1861 for(i = 0; (i < MAX_AF); i++) {
1862 if(AF(dest) == fd_family[i]) {
1868 if (INVALID_SOCKET == sock) {
1869 msyslog(LOG_ERR, "cannot find family compatible socket to send ntp packet");
1874 cc = sendto(sock, (char *)pkt, len, 0, (struct sockaddr *)dest,
1877 if (SOCKET_ERROR == cc) {
1879 if (errno != EWOULDBLOCK && errno != ENOBUFS)
1881 err = WSAGetLastError();
1882 if (err != WSAEWOULDBLOCK && err != WSAENOBUFS)
1883 #endif /* SYS_WINNT */
1884 msyslog(LOG_ERR, "sendto(%s): %m", stohost(dest));
1890 * input_handler - receive packets asynchronously
1896 register struct recvbuf *rb;
1897 struct sock_timeval tvzero;
1898 GETSOCKNAME_SOCKLEN_TYPE fromlen;
1902 struct pollfd fds[MAX_AF];
1909 * Do a poll to see if we have data
1912 tvzero.tv_sec = tvzero.tv_usec = 0;
1914 memcpy(fds, fdmask, sizeof(fdmask));
1915 n = poll(fds, (unsigned int)nbsock, tvzero.tv_sec * 1000);
1918 * Determine which socket received data
1921 for(i=0; i < nbsock; i++) {
1922 if(fds[i].revents & POLLIN) {
1930 n = select(maxfd, &fds, NULL, NULL, &tvzero);
1933 * Determine which socket received data
1936 for(i=0; i < nbsock; i++) {
1937 if(FD_ISSET(fd[i], &fds)) {
1946 * If nothing to do, just return. If an error occurred,
1947 * complain and return. If we've got some, freeze a
1958 "select() error: %m"
1966 * Get a buffer and read the frame. If we
1967 * haven't got a buffer, or this is received
1968 * on the wild card socket, just dump the packet.
1970 if (initializing || free_recvbuffs() == 0) {
1975 (void) read(fdc, buf, sizeof buf);
1977 /* NT's _read does not operate on nonblocking sockets
1978 * either recvfrom or ReadFile() has to be used here.
1979 * ReadFile is used in [ntpd]ntp_intres() and ntpdc,
1980 * just to be different use recvfrom() here
1982 recvfrom(fdc, buf, sizeof(buf), 0, (struct sockaddr *)0, NULL);
1983 #endif /* SYS_WINNT */
1987 rb = get_free_recv_buffer(TRUE);
1989 fromlen = sizeof(rb->recv_srcadr);
1990 rb->recv_length = recvfrom(fdc, (char *)&rb->recv_pkt,
1991 sizeof(rb->recv_pkt), 0,
1992 (struct sockaddr *)&rb->recv_srcadr, &fromlen);
1993 if (rb->recv_length == -1) {
1999 * Got one. Mark how and when it got here,
2000 * put it on the full list.
2003 add_full_recv_buffer(rb);
2009 * adj_systime - do a big long slew of the system time
2016 struct timeval adjtv;
2024 * Take the absolute value of the offset
2027 if (L_ISNEG(&offset)) {
2034 * Calculate the overshoot. XXX N.B. This code *knows*
2035 * ADJ_OVERSHOOT is 1/2.
2038 L_RSHIFTU(&overshoot);
2039 if (overshoot.l_ui != 0 || (overshoot.l_uf > ADJ_MAXOVERSHOOT)) {
2041 overshoot.l_uf = ADJ_MAXOVERSHOOT;
2043 L_ADD(&offset, &overshoot);
2045 TSTOTV(&offset, &adjtv);
2048 adjtv.tv_sec = -adjtv.tv_sec;
2049 adjtv.tv_usec = -adjtv.tv_usec;
2052 if (!debug && (adjtv.tv_usec != 0)) {
2053 /* A time correction needs to be applied. */
2054 #if !defined SYS_WINNT && !defined SYS_CYGWIN32
2055 /* Slew the time on systems that support this. */
2056 struct timeval oadjtv;
2057 if (adjtime(&adjtv, &oadjtv) < 0) {
2058 msyslog(LOG_ERR, "Can't adjust the time of day: %m");
2061 #else /* SYS_WINNT or SYS_CYGWIN32 is defined */
2063 * The NT SetSystemTimeAdjustment() call achieves slewing by
2064 * changing the clock frequency. This means that we cannot specify
2065 * it to slew the clock by a definite amount and then stop like
2066 * the Unix adjtime() routine. We can technically adjust the clock
2067 * frequency, have ntpdate sleep for a while, and then wake
2068 * up and reset the clock frequency, but this might cause some
2069 * grief if the user attempts to run ntpd immediately after
2070 * ntpdate and the socket is in use.
2072 printf("\nSlewing the system time is not supported on Windows. Use the -b option to step the time.\n");
2073 #endif /* defined SYS_WINNT || defined SYS_CYGWIN32 */
2080 * This fuction is not the same as lib/systime step_systime!!!
2099 * Take the absolute value of the offset
2103 if (L_ISNEG(&ftmp)) {
2109 if (ftmp.l_ui >= 3) { /* Step it and slew - we might win */
2111 n = step_systime(dtemp);
2114 if (isneg) /* WTF! */
2120 * Just add adjustment into the current offset. The update
2121 * routine will take care of bringing the system clock into
2127 #ifdef FORCE_NTPDATE_STEP
2129 return step_systime(dtemp);
2134 #else /* SLEWALWAYS */
2138 return step_systime(dtemp);
2139 #endif /* SLEWALWAYS */
2143 /* XXX ELIMINATE print_server similar in ntptrace.c, ntpdate.c */
2145 * print_server - print detail information for a server
2149 register struct server *pp,
2157 if (pp->stratum == 0) /* Nothing received => nothing to print */
2161 (void) fprintf(fp, "server %s, stratum %d, offset %s, delay %s\n",
2162 stoa(&pp->srcadr), pp->stratum,
2163 lfptoa(&pp->offset, 6), fptoa((s_fp)pp->delay, 5));
2167 (void) fprintf(fp, "server %s, port %d\n",
2168 stoa(&pp->srcadr), ntohs(((struct sockaddr_in*)&(pp->srcadr))->sin_port));
2170 (void) fprintf(fp, "stratum %d, precision %d, leap %c%c, trust %03o\n",
2171 pp->stratum, pp->precision,
2172 pp->leap & 0x2 ? '1' : '0',
2173 pp->leap & 0x1 ? '1' : '0',
2176 if (REFID_ISTEXT(pp->stratum)) {
2177 str = (char *) &pp->refid;
2178 for (i=0; i<4 && str[i]; i++) {
2179 junk[i] = (isprint(str[i]) ? str[i] : '.');
2181 junk[i] = 0; // force terminating 0
2184 str = numtoa(pp->refid);
2187 "refid [%s], root delay %s, root dispersion %s\n",
2188 str, fptoa((s_fp)pp->rootdelay, 6),
2189 ufptoa(pp->rootdisp, 6));
2191 if (pp->xmtcnt != pp->filter_nextpt)
2192 (void) fprintf(fp, "transmitted %d, in filter %d\n",
2193 pp->xmtcnt, pp->filter_nextpt);
2195 (void) fprintf(fp, "reference time: %s\n",
2196 prettydate(&pp->reftime));
2197 (void) fprintf(fp, "originate timestamp: %s\n",
2198 prettydate(&pp->org));
2199 (void) fprintf(fp, "transmit timestamp: %s\n",
2200 prettydate(&pp->xmt));
2202 if (sys_samples > 1) {
2203 (void) fprintf(fp, "filter delay: ");
2204 for (i = 0; i < NTP_SHIFT; i++) {
2205 if (i == (NTP_SHIFT>>1))
2206 (void) fprintf(fp, "\n ");
2207 (void) fprintf(fp, " %-10.10s",
2208 (i<sys_samples ? fptoa(pp->filter_delay[i], 5) : "----"));
2210 (void) fprintf(fp, "\n");
2212 (void) fprintf(fp, "filter offset:");
2213 for (i = 0; i < PEER_SHIFT; i++) {
2214 if (i == (PEER_SHIFT>>1))
2215 (void) fprintf(fp, "\n ");
2216 (void) fprintf(fp, " %-10.10s",
2217 (i<sys_samples ? lfptoa(&pp->filter_offset[i], 6): "----"));
2219 (void) fprintf(fp, "\n");
2222 (void) fprintf(fp, "delay %s, dispersion %s, ",
2223 fptoa((s_fp)pp->delay, 5), ufptoa(pp->dispersion, 5));
2225 (void) fprintf(fp, "offset %s\n\n",
2226 lfptoa(&pp->offset, 6));
2231 static ni_namelist *
2232 getnetinfoservers(void)
2237 ni_namelist *namelist = emalloc(sizeof(ni_namelist));
2239 /* Find a time server in NetInfo */
2240 if ((status = ni_open(NULL, ".", &domain)) != NI_OK) return NULL;
2242 while (status = ni_pathsearch(domain, &confdir, NETINFO_CONFIG_DIR) == NI_NODIR) {
2244 if (ni_open(domain, "..", &next_domain) != NI_OK) break;
2246 domain = next_domain;
2248 if (status != NI_OK) return NULL;
2251 if (ni_lookupprop(domain, &confdir, "server", namelist) != NI_OK) {
2252 ni_namelist_free(namelist);
2262 isc_boolean_t ntp_port_inuse(int af, u_short port)
2265 * Check if NTP socket is already in use on this system
2266 * This is only for Windows Systems, as they tend not to fail on the real bind() below
2270 struct sockaddr_in checkservice;
2271 checksocket = socket(af, SOCK_DGRAM, 0);
2272 if (checksocket == INVALID_SOCKET) {
2276 checkservice.sin_family = (short) AF_INET;
2277 checkservice.sin_addr.s_addr = INADDR_LOOPBACK;
2278 checkservice.sin_port = htons(port);
2280 if (bind(checksocket, (struct sockaddr *)&checkservice,
2281 sizeof(checkservice)) == SOCKET_ERROR) {
2282 if ( WSAGetLastError() == WSAEADDRINUSE ){
2283 closesocket(checksocket);
2287 closesocket(checksocket);