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 "ntp_unixtime.h"
19 #include "ntp_string.h"
20 #include "ntp_syslog.h"
21 #include "ntp_select.h"
22 #include "ntp_stdlib.h"
24 /* Don't include ISC's version of IPv6 variables and structures */
27 #include "isc/result.h"
28 #include "isc/sockaddr.h"
41 # ifdef HAVE_SYS_SIGNAL_H
42 # include <sys/signal.h>
46 # ifdef HAVE_SYS_IOCTL_H
47 # include <sys/ioctl.h>
49 #endif /* SYS_WINNT */
50 #ifdef HAVE_SYS_RESOURCE_H
51 # include <sys/resource.h>
52 #endif /* HAVE_SYS_RESOURCE_H */
54 #include <arpa/inet.h>
61 /* select wants a zero structure ... */
62 struct timeval timeout = {0,0};
63 #elif defined(SYS_WINNT)
65 * Windows does not abort a select select call if SIGALRM goes off
66 * so a 200 ms timeout is needed
68 struct timeval timeout = {0,1000000/TIMER_HZ};
70 struct timeval timeout = {60,0};
74 #include <netinfo/ni.h>
80 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
81 #define EAFNOSUPPORT WSAEAFNOSUPPORT
82 #define EPFNOSUPPORT WSAEPFNOSUPPORT
83 #define TARGET_RESOLUTION 1 /* Try for 1-millisecond accuracy
84 on Windows NT timers. */
85 #pragma comment(lib, "winmm")
86 isc_boolean_t ntp_port_inuse(int af, u_short port);
88 #endif /* SYS_WINNT */
91 * Scheduling priority we run at
94 # define NTPDATE_PRIO (-12)
96 # define NTPDATE_PRIO (100)
99 #if defined(HAVE_TIMER_SETTIME) || defined (HAVE_TIMER_CREATE)
100 /* POSIX TIMERS - vxWorks doesn't have itimer - casey */
101 static timer_t ntpdate_timerid;
105 * Compatibility stuff for Version 2
107 #define NTP_MAXSKW 0x28f /* 0.01 sec in fp format */
108 #define NTP_MINDIST 0x51f /* 0.02 sec in fp format */
109 #define PEER_MAXDISP (64*FP_SECOND) /* maximum dispersion (fp 64) */
110 #define NTP_INFIN 15 /* max stratum, infinity a la Bellman-Ford */
111 #define NTP_MAXWGT (8*FP_SECOND) /* maximum select weight 8 seconds */
112 #define NTP_MAXLIST 5 /* maximum select list size */
113 #define PEER_SHIFT 8 /* 8 suitable for crystal time base */
118 s_char sys_precision; /* local clock precision (log2 s) */
123 volatile int debug = 0;
126 * File descriptor masks etc. for call to select
130 int nbsock; /* the number of sockets used */
132 int fd_family[MAX_AF]; /* to remember the socket family */
134 struct pollfd fdmask[MAX_AF];
142 * Initializing flag. All async routines watch this and only do their
143 * thing when it is clear.
145 int initializing = 1;
148 * Alarm flag. Set when an alarm occurs
150 volatile int alarm_flag = 0;
155 int simple_query = 0;
158 * Unprivileged port flag.
168 * Systemwide parameters and flags
170 int sys_samples = DEFSAMPLES; /* number of samples/server */
171 u_long sys_timeout = DEFTIMEOUT; /* timeout time, in TIMER_HZ units */
172 struct server *sys_servers; /* the server list */
173 int sys_numservers = 0; /* number of servers to poll */
174 int sys_authenticate = 0; /* true when authenticating */
175 u_int32 sys_authkey = 0; /* set to authentication key in use */
176 u_long sys_authdelay = 0; /* authentication delay */
177 int sys_version = NTP_VERSION; /* version to poll with */
180 * The current internal time
182 u_long current_time = 0;
185 * Counter for keeping track of completed servers
187 int complete_servers = 0;
190 * File of encryption keys
195 #define KEYFILE "/etc/ntp.keys"
197 #define KEYFILE "%windir%\\ntp.keys"
198 # endif /* SYS_WINNT */
202 const char *key_file = KEYFILE;
204 char key_file_storage[MAX_PATH+1], *key_file ;
205 #endif /* SYS_WINNT */
208 * Miscellaneous flags
214 int ntpdatemain P((int, char **));
216 static void transmit P((struct server *));
217 static void receive P((struct recvbuf *));
218 static void server_data P((struct server *, s_fp, l_fp *, u_fp));
219 static void clock_filter P((struct server *));
220 static struct server *clock_select P((void));
221 static int clock_adjust P((void));
222 static void addserver P((char *));
223 static struct server *findserver P((struct sockaddr_storage *));
224 void timer P((void));
225 static void init_alarm P((void));
227 static RETSIGTYPE alarming P((int));
228 #endif /* SYS_WINNT */
229 static void init_io P((void));
230 static void sendpkt P((struct sockaddr_storage *, struct pkt *, int));
231 void input_handler P((void));
233 static int l_adj_systime P((l_fp *));
234 static int l_step_systime P((l_fp *));
236 static void printserver P((struct server *, FILE *));
240 WORD wVersionRequested;
242 HANDLE TimerThreadHandle = NULL;
243 #endif /* SYS_WINNT */
245 #ifdef NO_MAIN_ALLOWED
246 CALL(ntpdate,"ntpdate",ntpdatemain);
257 * Initializing flag. All async routines watch this and only do their
258 * thing when it is clear.
263 * Alarm flag. Set when an alarm occurs
273 * Unprivileged port flag.
278 * Systemwide parameters and flags
280 sys_numservers = 0; /* number of servers to poll */
281 sys_authenticate = 0; /* true when authenticating */
282 sys_authkey = 0; /* set to authentication key in use */
283 sys_authdelay = 0; /* authentication delay */
284 sys_version = NTP_VERSION; /* version to poll with */
287 * The current internal time
292 * Counter for keeping track of completed servers
294 complete_servers = 0;
302 static ni_namelist *getnetinfoservers P((void));
306 * Main program. Initialize us and loop waiting for I/O and/or
309 #ifndef NO_MAIN_ALLOWED
316 return ntpdatemain (argc, argv);
318 #endif /* NO_MAIN_ALLOWED */
328 struct recvbuf *rbuf;
335 ni_namelist *netinfoservers;
338 HANDLE process_handle;
340 wVersionRequested = MAKEWORD(1,1);
341 if (WSAStartup(wVersionRequested, &wsaData)) {
342 netsyslog(LOG_ERR, "No useable winsock.dll: %m");
346 key_file = key_file_storage;
348 if (!ExpandEnvironmentStrings(KEYFILE, key_file, MAX_PATH))
350 msyslog(LOG_ERR, "ExpandEnvironmentStrings(KEYFILE) failed: %m\n");
352 #endif /* SYS_WINNT */
354 #ifdef NO_MAIN_ALLOWED
359 /* Check to see if we have IPv6. Otherwise force the -4 flag */
360 if (isc_net_probeipv6() != ISC_R_SUCCESS) {
361 ai_fam_templ = AF_INET;
369 * Decode argument list
371 while ((c = ntp_getopt(argc, argv, "46a:bBde:k:o:p:qst:uv")) != EOF)
375 ai_fam_templ = AF_INET;
378 ai_fam_templ = AF_INET6;
381 c = atoi(ntp_optarg);
382 sys_authenticate = 1;
397 if (!atolfp(ntp_optarg, &tmp)
399 (void) fprintf(stderr,
400 "%s: encryption delay %s is unlikely\n",
401 progname, ntp_optarg);
404 sys_authdelay = tmp.l_uf;
408 key_file = ntp_optarg;
411 sys_version = atoi(ntp_optarg);
414 c = atoi(ntp_optarg);
415 if (c <= 0 || c > NTP_SHIFT) {
416 (void) fprintf(stderr,
417 "%s: number of samples (%d) is invalid\n",
431 if (!atolfp(ntp_optarg, &tmp)) {
432 (void) fprintf(stderr,
433 "%s: timeout %s is undecodeable\n",
434 progname, ntp_optarg);
437 sys_timeout = ((LFPTOFP(&tmp) * TIMER_HZ)
439 if (sys_timeout == 0)
457 (void) fprintf(stderr,
458 "usage: %s [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] server ...\n",
463 if (debug || simple_query) {
465 static char buf[BUFSIZ];
467 /* Win32 does not implement line buffering */
468 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
470 setvbuf(stdout, buf, _IOLBF, BUFSIZ);
471 #endif /* SYS_WINNT */
478 * Logging. Open the syslog if we have to
481 #if !defined (SYS_WINNT) && !defined (SYS_VXWORKS) && !defined SYS_CYGWIN32
483 openlog("ntpdate", LOG_PID);
487 # define LOG_NTP LOG_DAEMON
489 openlog("ntpdate", LOG_PID | LOG_NDELAY, LOG_NTP);
491 setlogmask(LOG_UPTO(LOG_DEBUG));
493 setlogmask(LOG_UPTO(LOG_INFO));
494 # endif /* LOG_DAEMON */
495 #endif /* SYS_WINNT */
498 if (debug || verbose)
499 msyslog(LOG_NOTICE, "%s", Version);
502 * Add servers we are going to be polling
505 netinfoservers = getnetinfoservers();
508 for ( ; ntp_optind < argc; ntp_optind++)
509 addserver(argv[ntp_optind]);
512 if (netinfoservers) {
513 if ( netinfoservers->ni_namelist_len &&
514 *netinfoservers->ni_namelist_val ) {
515 u_int servercount = 0;
516 while (servercount < netinfoservers->ni_namelist_len) {
517 if (debug) msyslog(LOG_DEBUG,
518 "Adding time server %s from NetInfo configuration.",
519 netinfoservers->ni_namelist_val[servercount]);
520 addserver(netinfoservers->ni_namelist_val[servercount++]);
523 ni_namelist_free(netinfoservers);
524 free(netinfoservers);
528 if (sys_numservers == 0) {
529 msyslog(LOG_ERR, "no servers can be used, exiting");
534 * Initialize the time of day routines and the I/O subsystem
536 if (sys_authenticate) {
538 if (!authreadkeys(key_file)) {
539 msyslog(LOG_ERR, "no key file <%s>, exiting", key_file);
542 authtrust(sys_authkey, 1);
543 if (!authistrusted(sys_authkey)) {
544 msyslog(LOG_ERR, "authentication key %lu unknown",
545 (unsigned long) sys_authkey);
556 taskPrioritySet( taskIdSelf(), NTPDATE_PRIO);
558 #if defined(HAVE_ATT_NICE)
561 #if defined(HAVE_BSD_NICE)
562 (void) setpriority(PRIO_PROCESS, 0, NTPDATE_PRIO);
565 process_handle = GetCurrentProcess();
566 if (!SetPriorityClass(process_handle, (DWORD) REALTIME_PRIORITY_CLASS)) {
567 msyslog(LOG_ERR, "SetPriorityClass failed: %m");
569 #endif /* SYS_WINNT */
576 while (complete_servers < sys_numservers) {
578 struct pollfd* rdfdes;
585 if (alarm_flag) { /* alarmed? */
589 tot_recvbufs = full_recvbuffs(); /* get received buffers */
591 if (!was_alarmed && tot_recvbufs == 0) {
593 * Nothing to do. Wait for something.
596 nfound = poll(rdfdes, (unsigned int)nbsock, timeout.tv_sec * 1000);
599 nfound = select(maxfd, &rdfdes, (fd_set *)0,
600 (fd_set *)0, &timeout);
604 else if (nfound == SOCKET_ERROR)
609 if (WSAGetLastError() != WSAEINTR)
618 } else if (errno != 0) {
622 "poll(): nfound = %d, error: %m",
624 "select(): nfound = %d, error: %m",
629 if (alarm_flag) { /* alarmed? */
633 tot_recvbufs = full_recvbuffs(); /* get received buffers */
637 * Out here, signals are unblocked. Call receive
638 * procedure for each incoming packet.
640 rbuf = get_full_recv_buffer();
645 rbuf = get_full_recv_buffer();
649 * Call timer to process any timeouts
662 * When we get here we've completed the polling of all servers.
663 * Adjust the clock, then exit.
670 timer_delete(ntpdate_timerid);
673 return clock_adjust();
678 * transmit - transmit a packet to the given server, or mark it completed.
679 * This is called by the timeout routine and by the receive
684 register struct server *server
690 printf("transmit(%s)\n", stoa(&(server->srcadr)));
692 if (server->filter_nextpt < server->xmtcnt) {
695 * Last message to this server timed out. Shift
696 * zeros into the filter.
699 server_data(server, 0, &ts, 0);
702 if ((int)server->filter_nextpt >= sys_samples) {
704 * Got all the data we need. Mark this guy
705 * completed and return.
707 server->event_time = 0;
713 * If we're here, send another message to the server. Fill in
714 * the packet and let 'er rip.
716 xpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
717 sys_version, MODE_CLIENT);
718 xpkt.stratum = STRATUM_TO_PKT(STRATUM_UNSPEC);
719 xpkt.ppoll = NTP_MINPOLL;
720 xpkt.precision = NTPDATE_PRECISION;
721 xpkt.rootdelay = htonl(NTPDATE_DISTANCE);
722 xpkt.rootdispersion = htonl(NTPDATE_DISP);
723 xpkt.refid = htonl(NTPDATE_REFID);
724 L_CLR(&xpkt.reftime);
729 * Determine whether to authenticate or not. If so,
730 * fill in the extended part of the packet and do it.
731 * If not, just timestamp it and send it away.
733 if (sys_authenticate) {
736 xpkt.exten[0] = htonl(sys_authkey);
737 get_systime(&server->xmt);
738 L_ADDUF(&server->xmt, sys_authdelay);
739 HTONL_FP(&server->xmt, &xpkt.xmt);
740 len = authencrypt(sys_authkey, (u_int32 *)&xpkt, LEN_PKT_NOMAC);
741 sendpkt(&(server->srcadr), &xpkt, (int)(LEN_PKT_NOMAC + len));
744 printf("transmit auth to %s\n",
745 stoa(&(server->srcadr)));
747 get_systime(&(server->xmt));
748 HTONL_FP(&server->xmt, &xpkt.xmt);
749 sendpkt(&(server->srcadr), &xpkt, LEN_PKT_NOMAC);
752 printf("transmit to %s\n", stoa(&(server->srcadr)));
756 * Update the server timeout and transmit count
758 server->event_time = current_time + sys_timeout;
764 * receive - receive and process an incoming frame
768 struct recvbuf *rbufp
771 register struct pkt *rpkt;
772 register struct server *server;
782 printf("receive(%s)\n", stoa(&rbufp->recv_srcadr));
784 * Check to see if the packet basically looks like something
787 if (rbufp->recv_length == LEN_PKT_NOMAC)
789 else if (rbufp->recv_length >= LEN_PKT_NOMAC)
793 printf("receive: packet length %d\n",
795 return; /* funny length packet */
798 rpkt = &(rbufp->recv_pkt);
799 if (PKT_VERSION(rpkt->li_vn_mode) < NTP_OLDVERSION ||
800 PKT_VERSION(rpkt->li_vn_mode) > NTP_VERSION) {
804 if ((PKT_MODE(rpkt->li_vn_mode) != MODE_SERVER
805 && PKT_MODE(rpkt->li_vn_mode) != MODE_PASSIVE)
806 || rpkt->stratum >= STRATUM_UNSPEC) {
808 printf("receive: mode %d stratum %d\n",
809 PKT_MODE(rpkt->li_vn_mode), rpkt->stratum);
814 * So far, so good. See if this is from a server we know.
816 server = findserver(&(rbufp->recv_srcadr));
817 if (server == NULL) {
819 printf("receive: server not found\n");
824 * Decode the org timestamp and make sure we're getting a response
825 * to our last request.
827 NTOHL_FP(&rpkt->org, &org);
828 if (!L_ISEQU(&org, &server->xmt)) {
830 printf("receive: pkt.org and peer.xmt differ\n");
835 * Check out the authenticity if we're doing that.
837 if (!sys_authenticate)
843 printf("receive: rpkt keyid=%ld sys_authkey=%ld decrypt=%ld\n",
844 (long int)ntohl(rpkt->exten[0]), (long int)sys_authkey,
845 (long int)authdecrypt(sys_authkey, (u_int32 *)rpkt,
846 LEN_PKT_NOMAC, (int)(rbufp->recv_length - LEN_PKT_NOMAC)));
848 if (has_mac && ntohl(rpkt->exten[0]) == sys_authkey &&
849 authdecrypt(sys_authkey, (u_int32 *)rpkt, LEN_PKT_NOMAC,
850 (int)(rbufp->recv_length - LEN_PKT_NOMAC)))
853 printf("receive: authentication %s\n",
854 is_authentic ? "passed" : "failed");
861 * Looks good. Record info from the packet.
863 server->leap = PKT_LEAP(rpkt->li_vn_mode);
864 server->stratum = PKT_TO_STRATUM(rpkt->stratum);
865 server->precision = rpkt->precision;
866 server->rootdelay = ntohl(rpkt->rootdelay);
867 server->rootdispersion = ntohl(rpkt->rootdispersion);
868 server->refid = rpkt->refid;
869 NTOHL_FP(&rpkt->reftime, &server->reftime);
870 NTOHL_FP(&rpkt->rec, &rec);
871 NTOHL_FP(&rpkt->xmt, &server->org);
874 * Make sure the server is at least somewhat sane. If not, try
877 if (L_ISZERO(&rec) || !L_ISHIS(&server->org, &rec)) {
883 * Calculate the round trip delay (di) and the clock offset (ci).
884 * We use the equations (reordered from those in the spec):
886 * d = (t2 - t3) - (t1 - t0)
887 * c = ((t2 - t3) + (t1 - t0)) / 2
889 t10 = server->org; /* pkt.xmt == t1 */
890 L_SUB(&t10, &rbufp->recv_time); /* recv_time == t0*/
892 t23 = rec; /* pkt.rec == t2 */
893 L_SUB(&t23, &org); /* pkt->org == t3 */
895 /* now have (t2 - t3) and (t0 - t1). Calculate (ci) and (di) */
897 * Calculate (ci) = ((t1 - t0) / 2) + ((t2 - t3) / 2)
898 * For large offsets this may prevent an overflow on '+'
907 * Calculate di in t23 in full precision, then truncate
914 printf("offset: %s, delay %s\n", lfptoa(&ci, 6), fptoa(di, 5));
916 di += (FP_SECOND >> (-(int)NTPDATE_PRECISION))
917 + (FP_SECOND >> (-(int)server->precision)) + NTP_MAXSKW;
919 if (di <= 0) { /* value still too raunchy to use? */
923 di = max(di, NTP_MINDIST);
927 * Shift this data in, then transmit again.
929 server_data(server, (s_fp) di, &ci, 0);
935 * server_data - add a sample to the server's filter registers
939 register struct server *server,
947 i = server->filter_nextpt;
949 server->filter_delay[i] = d;
950 server->filter_offset[i] = *c;
951 server->filter_soffset[i] = LFPTOFP(c);
952 server->filter_error[i] = e;
953 server->filter_nextpt = (u_short)(i + 1);
959 * clock_filter - determine a server's delay, dispersion and offset
963 register struct server *server
970 * Sort indices into increasing delay order
972 for (i = 0; i < sys_samples; i++)
975 for (i = 0; i < (sys_samples-1); i++) {
976 for (j = i+1; j < sys_samples; j++) {
977 if (server->filter_delay[ord[j]] == 0)
979 if (server->filter_delay[ord[i]] == 0
980 || (server->filter_delay[ord[i]]
981 > server->filter_delay[ord[j]])) {
992 * Now compute the dispersion, and assign values to delay and
993 * offset. If there are no samples in the register, delay and
994 * offset go to zero and dispersion is set to the maximum.
996 if (server->filter_delay[ord[0]] == 0) {
998 L_CLR(&server->offset);
1000 server->dispersion = PEER_MAXDISP;
1004 server->delay = server->filter_delay[ord[0]];
1005 server->offset = server->filter_offset[ord[0]];
1006 server->soffset = LFPTOFP(&server->offset);
1007 server->dispersion = 0;
1008 for (i = 1; i < sys_samples; i++) {
1009 if (server->filter_delay[ord[i]] == 0)
1012 d = server->filter_soffset[ord[i]]
1013 - server->filter_soffset[ord[0]];
1016 if (d > PEER_MAXDISP)
1020 * XXX This *knows* PEER_FILTER is 1/2
1022 server->dispersion += (u_fp)(d) >> i;
1032 * clock_select - select the pick-of-the-litter clock from the samples
1035 static struct server *
1038 register struct server *server;
1044 s_fp local_threshold;
1045 struct server *server_list[NTP_MAXCLOCK];
1046 u_fp server_badness[NTP_MAXCLOCK];
1047 struct server *sys_server;
1050 * This first chunk of code is supposed to go through all
1051 * servers we know about to find the NTP_MAXLIST servers which
1052 * are most likely to succeed. We run through the list
1053 * doing the sanity checks and trying to insert anyone who
1054 * looks okay. We are at all times aware that we should
1055 * only keep samples from the top two strata and we only need
1056 * NTP_MAXLIST of them.
1058 nlist = 0; /* none yet */
1059 for (server = sys_servers; server != NULL; server = server->next_server) {
1060 if (server->delay == 0) {
1062 printf("%s: Server dropped: no data\n", ntoa(&server->srcadr));
1063 continue; /* no data */
1065 if (server->stratum > NTP_INFIN) {
1067 printf("%s: Server dropped: strata too high\n", ntoa(&server->srcadr));
1068 continue; /* stratum no good */
1070 if (server->delay > NTP_MAXWGT) {
1072 printf("%s: Server dropped: server too far away\n",
1073 ntoa(&server->srcadr));
1074 continue; /* too far away */
1076 if (server->leap == LEAP_NOTINSYNC) {
1078 printf("%s: Server dropped: Leap not in sync\n", ntoa(&server->srcadr));
1079 continue; /* he's in trouble */
1081 if (!L_ISHIS(&server->org, &server->reftime)) {
1083 printf("%s: Server dropped: server is very broken\n",
1084 ntoa(&server->srcadr));
1085 continue; /* very broken host */
1087 if ((server->org.l_ui - server->reftime.l_ui)
1090 printf("%s: Server dropped: Server has gone too long without sync\n",
1091 ntoa(&server->srcadr));
1092 continue; /* too long without sync */
1094 if (server->trust != 0) {
1096 printf("%s: Server dropped: Server is untrusted\n",
1097 ntoa(&server->srcadr));
1102 * This one seems sane. Find where he belongs
1105 d = server->dispersion + server->dispersion;
1106 for (i = 0; i < nlist; i++)
1107 if (server->stratum <= server_list[i]->stratum)
1109 for ( ; i < nlist; i++) {
1110 if (server->stratum < server_list[i]->stratum)
1112 if (d < (s_fp) server_badness[i])
1117 * If i points past the end of the list, this
1118 * guy is a loser, else stick him in.
1120 if (i >= NTP_MAXLIST)
1122 for (j = nlist; j > i; j--)
1123 if (j < NTP_MAXLIST) {
1124 server_list[j] = server_list[j-1];
1126 = server_badness[j-1];
1129 server_list[i] = server;
1130 server_badness[i] = d;
1131 if (nlist < NTP_MAXLIST)
1136 * Got the five-or-less best. Cut the list where the number of
1137 * strata exceeds two.
1140 for (i = 1; i < nlist; i++)
1141 if (server_list[i]->stratum > server_list[i-1]->stratum)
1148 * Whew! What we should have by now is 0 to 5 candidates for
1149 * the job of syncing us. If we have none, we're out of luck.
1150 * If we have one, he's a winner. If we have more, do falseticker
1156 else if (nlist == 1) {
1157 sys_server = server_list[0];
1160 * Re-sort by stratum, bdelay estimate quality and
1163 for (i = 0; i < nlist-1; i++)
1164 for (j = i+1; j < nlist; j++) {
1165 if (server_list[i]->stratum
1166 < server_list[j]->stratum)
1167 break; /* already sorted by stratum */
1168 if (server_list[i]->delay
1169 < server_list[j]->delay)
1171 server = server_list[i];
1172 server_list[i] = server_list[j];
1173 server_list[j] = server;
1177 * Calculate the fixed part of the dispersion limit
1179 local_threshold = (FP_SECOND >> (-(int)NTPDATE_PRECISION))
1183 * Now drop samples until we're down to one.
1186 for (n = 0; n < nlist; n++) {
1187 server_badness[n] = 0;
1188 for (j = 0; j < nlist; j++) {
1189 if (j == n) /* with self? */
1191 d = server_list[j]->soffset
1192 - server_list[n]->soffset;
1193 if (d < 0) /* absolute value */
1196 * XXX This code *knows* that
1199 for (i = 0; i < j; i++)
1200 d = (d>>1) + (d>>2);
1201 server_badness[n] += d;
1206 * We now have an array of nlist badness
1207 * coefficients. Find the badest. Find
1208 * the minimum precision while we're at
1212 n = server_list[0]->precision;;
1213 for (j = 1; j < nlist; j++) {
1214 if (server_badness[j] >= server_badness[i])
1216 if (n > server_list[j]->precision)
1217 n = server_list[j]->precision;
1221 * i is the index of the server with the worst
1222 * dispersion. If his dispersion is less than
1223 * the threshold, stop now, else delete him and
1224 * continue around again.
1226 if ( (s_fp) server_badness[i] < (local_threshold
1227 + (FP_SECOND >> (-n))))
1229 for (j = i + 1; j < nlist; j++)
1230 server_list[j-1] = server_list[j];
1235 * What remains is a list of less than 5 servers. Take
1238 sys_server = server_list[0];
1242 * That's it. Return our server.
1249 * clock_adjust - process what we've received, and adjust the time
1250 * if we got anything decent.
1255 register struct server *sp, *server;
1259 for (sp = sys_servers; sp != NULL; sp = sp->next_server)
1261 server = clock_select();
1263 if (debug || simple_query) {
1264 for (sp = sys_servers; sp != NULL; sp = sp->next_server)
1265 printserver(sp, stdout);
1270 "no server suitable for synchronization found");
1276 } else if (never_step) {
1279 absoffset = server->soffset;
1281 absoffset = -absoffset;
1282 dostep = (absoffset >= NTPDATE_THRESHOLD || absoffset < 0);
1286 if (simple_query || debug || l_step_systime(&server->offset)){
1287 msyslog(LOG_NOTICE, "step time server %s offset %s sec",
1288 stoa(&server->srcadr),
1289 lfptoa(&server->offset, 6));
1292 #if !defined SYS_WINNT && !defined SYS_CYGWIN32
1293 if (simple_query || l_adj_systime(&server->offset)) {
1294 msyslog(LOG_NOTICE, "adjust time server %s offset %s sec",
1295 stoa(&server->srcadr),
1296 lfptoa(&server->offset, 6));
1299 /* The NT SetSystemTimeAdjustment() call achieves slewing by
1300 * changing the clock frequency. This means that we cannot specify
1301 * it to slew the clock by a definite amount and then stop like
1302 * the Unix adjtime() routine. We can technically adjust the clock
1303 * frequency, have ntpdate sleep for a while, and then wake
1304 * up and reset the clock frequency, but this might cause some
1305 * grief if the user attempts to run ntpd immediately after
1306 * ntpdate and the socket is in use.
1308 printf("\nThe -b option is required by ntpdate on Windows NT platforms\n");
1310 #endif /* SYS_WINNT */
1317 * is_unreachable - check to see if we have a route to given destination
1321 is_reachable (struct sockaddr_storage *dst)
1325 sockfd = socket(dst->ss_family, SOCK_DGRAM, 0);
1330 if(connect(sockfd, (struct sockaddr *)dst, 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 */
1359 strcpy(service, "ntp");
1361 /* Get host address. Looking for UDP datagram connection. */
1362 memset(&hints, 0, sizeof(hints));
1363 hints.ai_family = ai_fam_templ;
1364 hints.ai_socktype = SOCK_DGRAM;
1368 printf("Looking for host %s and service %s\n", serv, service);
1371 error = getaddrinfo(serv, service, &hints, &addrResult);
1373 /* Conduct more refined error analysis */
1374 if (error == EAI_FAIL || error == EAI_AGAIN){
1375 /* Name server is unusable. Exit after failing on the
1376 first server, in order to shorten the timeout caused
1377 by waiting for resolution of several servers */
1378 fprintf(stderr, "Name server cannot be used, exiting");
1379 msyslog(LOG_ERR, "name server cannot be used, reason: %s\n", gai_strerror(error));
1382 fprintf(stderr, "Error : %s\n", gai_strerror(error));
1383 msyslog(LOG_ERR, "can't find host %s\n", serv);
1388 fprintf(stderr, "host found : %s\n", stohost((struct sockaddr_storage*)addrResult->ai_addr));
1392 /* We must get all returned server in case the first one fails */
1393 for (ptr = addrResult; ptr != NULL; ptr = ptr->ai_next) {
1394 if (is_reachable ((struct sockaddr_storage *)ptr->ai_addr)) {
1395 server = (struct server *)emalloc(sizeof(struct server));
1396 memset((char *)server, 0, sizeof(struct server));
1398 memset(&(server->srcadr), 0, sizeof(struct sockaddr_storage));
1399 memcpy(&(server->srcadr), ptr->ai_addr, ptr->ai_addrlen);
1400 server->event_time = ++sys_numservers;
1401 if (sys_servers == NULL)
1402 sys_servers = server;
1406 for (sp = sys_servers; sp->next_server != NULL;
1407 sp = sp->next_server) ;
1408 sp->next_server = server;
1413 freeaddrinfo(addrResult);
1418 * findserver - find a server in the list given its address
1419 * ***(For now it isn't totally AF-Independant, to check later..)
1421 static struct server *
1423 struct sockaddr_storage *addr
1426 struct server *server;
1427 struct server *mc_server;
1428 isc_sockaddr_t laddr;
1429 isc_sockaddr_t saddr;
1431 if(addr->ss_family == AF_INET) {
1432 isc_sockaddr_fromin( &laddr, &((struct sockaddr_in*)addr)->sin_addr, 0);
1435 isc_sockaddr_fromin6(&laddr, &((struct sockaddr_in6*)addr)->sin6_addr, 0);
1440 if (htons(((struct sockaddr_in*)addr)->sin_port) != NTP_PORT)
1443 for (server = sys_servers; server != NULL;
1444 server = server->next_server) {
1446 if(server->srcadr.ss_family == AF_INET) {
1447 isc_sockaddr_fromin(&saddr, &((struct sockaddr_in*)&server->srcadr)->sin_addr, 0);
1450 isc_sockaddr_fromin6(&saddr, &((struct sockaddr_in6*)&server->srcadr)->sin6_addr, 0);
1452 if (isc_sockaddr_eqaddr(&laddr, &saddr) == ISC_TRUE)
1455 if(addr->ss_family == server->srcadr.ss_family) {
1456 if (isc_sockaddr_ismulticast(&saddr) == ISC_TRUE)
1461 if (mc_server != NULL) {
1465 if (mc_server->event_time != 0) {
1466 mc_server->event_time = 0;
1470 server = (struct server *)emalloc(sizeof(struct server));
1471 memset((char *)server, 0, sizeof(struct server));
1473 memcpy(&server->srcadr, addr, sizeof(struct sockaddr_storage));
1475 server->event_time = ++sys_numservers;
1477 for (sp = sys_servers; sp->next_server != NULL;
1478 sp = sp->next_server) ;
1479 sp->next_server = server;
1487 * timer - process a timer interrupt
1492 struct server *server;
1495 * Bump the current idea of the time
1500 * Search through the server list looking for guys
1501 * who's event timers have expired. Give these to
1502 * the transmit routine.
1504 for (server = sys_servers; server != NULL;
1505 server = server->next_server) {
1506 if (server->event_time != 0
1507 && server->event_time <= current_time)
1514 * The code duplication in the following subroutine sucks, but
1515 * we need to appease ansi2knr.
1520 * alarming - record the occurance of an alarm interrupt
1531 alarming(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
1535 #endif /* SYS_WINNT */
1540 callTimeEndPeriod(void)
1542 timeEndPeriod( wTimerRes );
1545 #endif /* SYS_WINNT */
1549 * init_alarm - set up the timer interrupt
1555 # ifndef HAVE_TIMER_SETTIME
1556 struct itimerval itimer;
1558 struct itimerspec ntpdate_itimer;
1563 # endif /* SYS_WINNT */
1564 #if defined SYS_CYGWIN32 || defined SYS_WINNT
1566 TOKEN_PRIVILEGES tkp;
1568 #endif /* SYS_WINNT */
1573 # if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME)
1575 /* this code was put in as setitimer() is non existant this us the
1576 * POSIX "equivalents" setup - casey
1578 /* ntpdate_timerid is global - so we can kill timer later */
1579 if (timer_create (CLOCK_REALTIME, NULL, &ntpdate_timerid) ==
1587 fprintf (stderr, "init_alarm(): timer_create (...) FAILED\n");
1592 * Set up the alarm interrupt. The first comes 1/(2*TIMER_HZ)
1593 * seconds from now and they continue on every 1/TIMER_HZ seconds.
1595 (void) signal_no_reset(SIGALRM, alarming);
1596 ntpdate_itimer.it_interval.tv_sec = ntpdate_itimer.it_value.tv_sec = 0;
1597 ntpdate_itimer.it_interval.tv_nsec = 1000000000/TIMER_HZ;
1598 ntpdate_itimer.it_value.tv_nsec = 1000000000/(TIMER_HZ<<1);
1599 timer_settime(ntpdate_timerid, 0 /* !TIMER_ABSTIME */, &ntpdate_itimer, NULL);
1602 * Set up the alarm interrupt. The first comes 1/(2*TIMER_HZ)
1603 * seconds from now and they continue on every 1/TIMER_HZ seconds.
1605 (void) signal_no_reset(SIGALRM, alarming);
1606 itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0;
1607 itimer.it_interval.tv_usec = 1000000/TIMER_HZ;
1608 itimer.it_value.tv_usec = 1000000/(TIMER_HZ<<1);
1610 setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
1612 #if defined SYS_CYGWIN32
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");
1632 #else /* SYS_WINNT */
1636 * Get privileges needed for fiddling with the clock
1639 /* get the current process token handle */
1640 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
1641 msyslog(LOG_ERR, "OpenProcessToken failed: %m");
1644 /* get the LUID for system-time privilege. */
1645 LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkp.Privileges[0].Luid);
1646 tkp.PrivilegeCount = 1; /* one privilege to set */
1647 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1648 /* get set-time privilege for this process. */
1649 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);
1650 /* cannot test return value of AdjustTokenPrivileges. */
1651 if (GetLastError() != ERROR_SUCCESS)
1652 msyslog(LOG_ERR, "AdjustTokenPrivileges failed: %m");
1655 * Set up timer interrupts for every 2**EVENT_TIMEOUT seconds
1656 * Under Win/NT, expiry of timer interval leads to invocation
1657 * of a callback function (on a different thread) rather than
1658 * generating an alarm signal
1661 /* determine max and min resolution supported */
1662 if(timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) {
1663 msyslog(LOG_ERR, "timeGetDevCaps failed: %m");
1666 wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
1667 /* establish the minimum timer resolution that we'll use */
1668 timeBeginPeriod(wTimerRes);
1669 atexit(callTimeEndPeriod);
1671 /* start the timer event */
1672 wTimerID = timeSetEvent(
1673 (UINT) (1000/TIMER_HZ), /* Delay */
1674 wTimerRes, /* Resolution */
1675 (LPTIMECALLBACK) alarming, /* Callback function */
1676 (DWORD) dwUser, /* User data */
1677 TIME_PERIODIC); /* Event type (periodic) */
1678 if (wTimerID == 0) {
1679 msyslog(LOG_ERR, "timeSetEvent failed: %m");
1682 #endif /* SYS_WINNT */
1689 * We do asynchronous input using the SIGIO facility. A number of
1690 * recvbuf buffers are preallocated for input. In the signal
1691 * handler we poll to see if the socket is ready and read the
1692 * packets from it into the recvbuf's along with a time stamp and
1693 * an indication of the source host and the interface it was received
1694 * through. This allows us to get as accurate receive time stamps
1695 * as possible independent of other processing going on.
1697 * We allocate a number of recvbufs equal to the number of servers
1698 * plus 2. This should be plenty.
1703 * init_io - initialize I/O data and open socket
1708 struct addrinfo *res, *ressave;
1709 struct addrinfo hints;
1712 int check_ntp_port_in_use = !debug && !simple_query && !unpriv_port;
1715 * Init buffer free list and stat counters
1717 init_recvbuff(sys_numservers + 2);
1723 strcpy(service, "ntp");
1726 * Init hints addrinfo structure
1728 memset(&hints, 0, sizeof(hints));
1729 hints.ai_family = ai_fam_templ;
1730 hints.ai_flags = AI_PASSIVE;
1731 hints.ai_socktype = SOCK_DGRAM;
1733 if(getaddrinfo(NULL, service, &hints, &res) != 0) {
1734 msyslog(LOG_ERR, "getaddrinfo() failed: %m");
1740 if (check_ntp_port_in_use && ntp_port_inuse(AF_INET, NTP_PORT)){
1741 netsyslog(LOG_ERR, "the NTP socket is in use, exiting: %m");
1746 /* Remember the address of the addrinfo structure chain */
1750 * For each structure returned, open and bind socket
1752 for(nbsock = 0; (nbsock < MAX_AF) && res ; res = res->ai_next) {
1753 /* create a datagram (UDP) socket */
1754 fd[nbsock] = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1755 if (fd[nbsock] == SOCKET_ERROR) {
1757 if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT ||
1758 errno == EPFNOSUPPORT)
1760 int err = WSAGetLastError();
1761 if (err == WSAEPROTONOSUPPORT || err == WSAEAFNOSUPPORT ||
1762 err == WSAEPFNOSUPPORT)
1765 netsyslog(LOG_ERR, "socket() failed: %m");
1769 /* set socket to reuse address */
1770 if (setsockopt(fd[nbsock], SOL_SOCKET, SO_REUSEADDR, (void*) &optval, sizeof(optval)) < 0) {
1771 netsyslog(LOG_ERR, "setsockopt() SO_REUSEADDR failed: %m");
1776 /* Restricts AF_INET6 socket to IPv6 communications (see RFC 2553bis-03) */
1777 if (res->ai_family == AF_INET6)
1778 if (setsockopt(fd[nbsock], IPPROTO_IPV6, IPV6_V6ONLY, (void*) &optval, sizeof(optval)) < 0) {
1779 netsyslog(LOG_ERR, "setsockopt() IPV6_V6ONLY failed: %m");
1785 /* Remember the socket family in fd_family structure */
1786 fd_family[nbsock] = res->ai_family;
1789 * bind the socket to the NTP port
1791 if (check_ntp_port_in_use) {
1792 if (bind(fd[nbsock], res->ai_addr, SOCKLEN(res->ai_addr)) < 0) {
1794 if (errno == EADDRINUSE)
1796 if (WSAGetLastError() == WSAEADDRINUSE)
1797 #endif /* SYS_WINNT */
1798 netsyslog(LOG_ERR, "the NTP socket is in use, exiting");
1800 netsyslog(LOG_ERR, "bind() fails: %m");
1806 fdmask[nbsock].fd = fd[nbsock];
1807 fdmask[nbsock].events = POLLIN;
1809 FD_SET(fd[nbsock], &fdmask);
1810 if (maxfd < fd[nbsock]+1) {
1811 maxfd = fd[nbsock]+1;
1823 if (ioctl(fd[nbsock],FIONBIO, &on) == ERROR) {
1824 netsyslog(LOG_ERR, "ioctl(FIONBIO) fails: %m");
1828 # else /* not SYS_VXWORKS */
1829 # if defined(O_NONBLOCK)
1830 if (fcntl(fd[nbsock], F_SETFL, O_NONBLOCK) < 0) {
1831 netsyslog(LOG_ERR, "fcntl(FNDELAY|FASYNC) fails: %m");
1835 # else /* not O_NONBLOCK */
1836 # if defined(FNDELAY)
1837 if (fcntl(fd[nbsock], F_SETFL, FNDELAY) < 0) {
1838 netsyslog(LOG_ERR, "fcntl(FNDELAY|FASYNC) fails: %m");
1842 # else /* FNDELAY */
1843 # include "Bletch: Need non blocking I/O"
1844 # endif /* FNDELAY */
1845 # endif /* not O_NONBLOCK */
1846 # endif /* SYS_VXWORKS */
1847 #else /* SYS_WINNT */
1848 if (ioctlsocket(fd[nbsock], FIONBIO, (u_long *) &on) == SOCKET_ERROR) {
1849 netsyslog(LOG_ERR, "ioctlsocket(FIONBIO) fails: %m");
1852 #endif /* SYS_WINNT */
1855 freeaddrinfo(ressave);
1859 * sendpkt - send a packet to the specified destination
1863 struct sockaddr_storage *dest,
1870 SOCKET sock = INVALID_SOCKET;
1874 #endif /* SYS_WINNT */
1876 /* Find a local family compatible socket to send ntp packet to ntp server */
1877 for(i = 0; (i < MAX_AF); i++) {
1878 if(dest->ss_family == fd_family[i]) {
1884 if ( sock == INVALID_SOCKET ) {
1885 netsyslog(LOG_ERR, "cannot find family compatible socket to send ntp packet");
1890 cc = sendto(sock, (char *)pkt, len, 0, (struct sockaddr *)dest,
1893 if (cc == SOCKET_ERROR) {
1895 if (errno != EWOULDBLOCK && errno != ENOBUFS)
1897 err = WSAGetLastError();
1898 if (err != WSAEWOULDBLOCK && err != WSAENOBUFS)
1899 #endif /* SYS_WINNT */
1900 netsyslog(LOG_ERR, "sendto(%s): %m", stohost(dest));
1906 * input_handler - receive packets asynchronously
1912 register struct recvbuf *rb;
1913 struct timeval tvzero;
1918 struct pollfd fds[MAX_AF];
1925 * Do a poll to see if we have data
1928 tvzero.tv_sec = tvzero.tv_usec = 0;
1930 memcpy(fds, fdmask, sizeof(fdmask));
1931 n = poll(fds, (unsigned int)nbsock, tvzero.tv_sec * 1000);
1934 * Determine which socket received data
1937 for(i=0; i < nbsock; i++) {
1938 if(fds[i].revents & POLLIN) {
1946 n = select(maxfd, &fds, (fd_set *)0, (fd_set *)0, &tvzero);
1949 * Determine which socket received data
1952 for(i=0; i < nbsock; i++) {
1953 if(FD_ISSET(fd[i], &fds)) {
1962 * If nothing to do, just return. If an error occurred,
1963 * complain and return. If we've got some, freeze a
1974 "select() error: %m"
1982 * Get a buffer and read the frame. If we
1983 * haven't got a buffer, or this is received
1984 * on the wild card socket, just dump the packet.
1986 if (initializing || free_recvbuffs() == 0) {
1991 (void) read(fdc, buf, sizeof buf);
1993 /* NT's _read does not operate on nonblocking sockets
1994 * either recvfrom or ReadFile() has to be used here.
1995 * ReadFile is used in [ntpd]ntp_intres() and ntpdc,
1996 * just to be different use recvfrom() here
1998 recvfrom(fdc, buf, sizeof(buf), 0, (struct sockaddr *)0, NULL);
1999 #endif /* SYS_WINNT */
2003 rb = get_free_recv_buffer();
2005 fromlen = sizeof(struct sockaddr_storage);
2006 rb->recv_length = recvfrom(fdc, (char *)&rb->recv_pkt,
2007 sizeof(rb->recv_pkt), 0,
2008 (struct sockaddr *)&rb->recv_srcadr, &fromlen);
2009 if (rb->recv_length == -1) {
2015 * Got one. Mark how and when it got here,
2016 * put it on the full list.
2019 add_full_recv_buffer(rb);
2024 #if !defined SYS_WINNT && !defined SYS_CYGWIN32
2026 * adj_systime - do a big long slew of the system time
2033 struct timeval adjtv, oadjtv;
2041 * Take the absolute value of the offset
2044 if (L_ISNEG(&offset)) {
2051 * Calculate the overshoot. XXX N.B. This code *knows*
2052 * ADJ_OVERSHOOT is 1/2.
2055 L_RSHIFTU(&overshoot);
2056 if (overshoot.l_ui != 0 || (overshoot.l_uf > ADJ_MAXOVERSHOOT)) {
2058 overshoot.l_uf = ADJ_MAXOVERSHOOT;
2060 L_ADD(&offset, &overshoot);
2062 TSTOTV(&offset, &adjtv);
2065 adjtv.tv_sec = -adjtv.tv_sec;
2066 adjtv.tv_usec = -adjtv.tv_usec;
2069 if (adjtv.tv_usec != 0 && !debug) {
2070 if (adjtime(&adjtv, &oadjtv) < 0) {
2071 msyslog(LOG_ERR, "Can't adjust the time of day: %m");
2077 #endif /* SYS_WINNT */
2081 * This fuction is not the same as lib/systime step_systime!!!
2096 if (debug) return 1;
2098 * Take the absolute value of the offset
2101 if (L_ISNEG(&ftmp)) {
2107 if (ftmp.l_ui >= 3) { /* Step it and slew - we might win */
2109 n = step_systime(dtemp);
2118 * Just add adjustment into the current offset. The update
2119 * routine will take care of bringing the system clock into
2125 #ifdef FORCE_NTPDATE_STEP
2127 return step_systime(dtemp);
2132 #else /* SLEWALWAYS */
2136 return step_systime(dtemp);
2137 #endif /* SLEWALWAYS */
2141 /* XXX ELIMINATE printserver similar in ntptrace.c, ntpdate.c */
2143 * printserver - print detail information for a server
2147 register struct server *pp,
2156 (void) fprintf(fp, "server %s, stratum %d, offset %s, delay %s\n",
2157 stoa(&pp->srcadr), pp->stratum,
2158 lfptoa(&pp->offset, 6), fptoa((s_fp)pp->delay, 5));
2162 (void) fprintf(fp, "server %s, port %d\n",
2163 stoa(&pp->srcadr), ntohs(((struct sockaddr_in*)&(pp->srcadr))->sin_port));
2165 (void) fprintf(fp, "stratum %d, precision %d, leap %c%c, trust %03o\n",
2166 pp->stratum, pp->precision,
2167 pp->leap & 0x2 ? '1' : '0',
2168 pp->leap & 0x1 ? '1' : '0',
2171 if (pp->stratum == 1) {
2173 memmove(junk, (char *)&pp->refid, 4);
2176 str = stoa(&pp->srcadr);
2179 "refid [%s], delay %s, dispersion %s\n",
2180 str, fptoa((s_fp)pp->delay, 5),
2181 ufptoa(pp->dispersion, 5));
2183 (void) fprintf(fp, "transmitted %d, in filter %d\n",
2184 pp->xmtcnt, pp->filter_nextpt);
2186 (void) fprintf(fp, "reference time: %s\n",
2187 prettydate(&pp->reftime));
2188 (void) fprintf(fp, "originate timestamp: %s\n",
2189 prettydate(&pp->org));
2190 (void) fprintf(fp, "transmit timestamp: %s\n",
2191 prettydate(&pp->xmt));
2193 (void) fprintf(fp, "filter delay: ");
2194 for (i = 0; i < NTP_SHIFT; i++) {
2195 (void) fprintf(fp, " %-8.8s", fptoa(pp->filter_delay[i], 5));
2196 if (i == (NTP_SHIFT>>1)-1)
2197 (void) fprintf(fp, "\n ");
2199 (void) fprintf(fp, "\n");
2201 (void) fprintf(fp, "filter offset:");
2202 for (i = 0; i < PEER_SHIFT; i++) {
2203 (void) fprintf(fp, " %-8.8s", lfptoa(&pp->filter_offset[i], 6));
2204 if (i == (PEER_SHIFT>>1)-1)
2205 (void) fprintf(fp, "\n ");
2207 (void) fprintf(fp, "\n");
2209 (void) fprintf(fp, "delay %s, dispersion %s\n",
2210 fptoa((s_fp)pp->delay, 5), ufptoa(pp->dispersion, 5));
2212 (void) fprintf(fp, "offset %s\n\n",
2213 lfptoa(&pp->offset, 6));
2216 #if !defined(HAVE_VSPRINTF)
2227 f._flag = _IOWRT+_IOSTRG;
2230 len = _doprnt(fmt, ap, &f);
2237 /* override function in library since SA_RESTART makes ALL syscalls restart */
2246 struct sigaction vec;
2248 vec.sa_handler = func;
2249 sigemptyset(&vec.sa_mask);
2254 n = sigaction(sig, &vec, NULL);
2255 if (n == -1 && errno == EINTR)
2261 perror("sigaction");
2269 static ni_namelist *
2270 getnetinfoservers(void)
2275 ni_namelist *namelist = (ni_namelist*)malloc(sizeof(ni_namelist));
2277 /* Find a time server in NetInfo */
2278 if ((status = ni_open(NULL, ".", &domain)) != NI_OK) return NULL;
2280 while (status = ni_pathsearch(domain, &confdir, NETINFO_CONFIG_DIR) == NI_NODIR) {
2282 if (ni_open(domain, "..", &next_domain) != NI_OK) break;
2284 domain = next_domain;
2286 if (status != NI_OK) return NULL;
2289 if (ni_lookupprop(domain, &confdir, "server", namelist) != NI_OK) {
2290 ni_namelist_free(namelist);
2300 isc_boolean_t ntp_port_inuse(int af, u_short port)
2303 * Check if NTP socket is already in use on this system
2304 * This is only for Windows Systems, as they tend not to fail on the real bind() below
2308 struct sockaddr_in checkservice;
2309 checksocket = socket(af, SOCK_DGRAM, 0);
2310 if (checksocket == INVALID_SOCKET) {
2314 checkservice.sin_family = (short) AF_INET;
2315 checkservice.sin_addr.s_addr = INADDR_LOOPBACK;
2316 checkservice.sin_port = htons(port);
2318 if (bind(checksocket, (struct sockaddr *)&checkservice,
2319 sizeof(checkservice)) == SOCKET_ERROR) {
2320 if ( WSAGetLastError() == WSAEADDRINUSE ){
2321 closesocket(checksocket);
2325 closesocket(checksocket);