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"
36 # ifdef HAVE_SYS_SIGNAL_H
37 # include <sys/signal.h>
41 # ifdef HAVE_SYS_IOCTL_H
42 # include <sys/ioctl.h>
44 #endif /* SYS_WINNT */
45 #ifdef HAVE_SYS_RESOURCE_H
46 # include <sys/resource.h>
47 #endif /* HAVE_SYS_RESOURCE_H */
49 #include <arpa/inet.h>
60 /* select wants a zero structure ... */
61 struct timeval timeout = {0,0};
63 struct timeval timeout = {60,0};
67 #include <netinfo/ni.h>
73 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
74 #define EAFNOSUPPORT WSAEAFNOSUPPORT
75 #define EPFNOSUPPORT WSAEPFNOSUPPORT
76 #define TARGET_RESOLUTION 1 /* Try for 1-millisecond accuracy
77 on Windows NT timers. */
78 #pragma comment(lib, "winmm")
79 #endif /* SYS_WINNT */
82 * Scheduling priority we run at
85 # define NTPDATE_PRIO (-12)
87 # define NTPDATE_PRIO (100)
90 #if defined(HAVE_TIMER_SETTIME) || defined (HAVE_TIMER_CREATE)
91 /* POSIX TIMERS - vxWorks doesn't have itimer - casey */
92 static timer_t ntpdate_timerid;
96 * Compatibility stuff for Version 2
98 #define NTP_MAXSKW 0x28f /* 0.01 sec in fp format */
99 #define NTP_MINDIST 0x51f /* 0.02 sec in fp format */
100 #define PEER_MAXDISP (64*FP_SECOND) /* maximum dispersion (fp 64) */
101 #define NTP_INFIN 15 /* max stratum, infinity a la Bellman-Ford */
102 #define NTP_MAXWGT (8*FP_SECOND) /* maximum select weight 8 seconds */
103 #define NTP_MAXLIST 5 /* maximum select list size */
104 #define PEER_SHIFT 8 /* 8 suitable for crystal time base */
109 volatile int debug = 0;
112 * File descriptor masks etc. for call to select
117 SOCKET fd[MAX_AF]; /* support up to 2 sockets */
118 int fd_family[MAX_AF]; /* to remember the socket family */
120 struct pollfd fdmask[MAX_AF];
128 * Initializing flag. All async routines watch this and only do their
129 * thing when it is clear.
131 int initializing = 1;
134 * Alarm flag. Set when an alarm occurs
136 volatile int alarm_flag = 0;
141 int simple_query = 0;
144 * Unpriviledged port flag.
149 * Time to spend measuring drift rate
159 * Systemwide parameters and flags
161 int sys_samples = DEFSAMPLES; /* number of samples/server */
162 u_long sys_timeout = DEFTIMEOUT; /* timeout time, in TIMER_HZ units */
163 struct server *sys_servers; /* the server list */
164 int sys_numservers = 0; /* number of servers to poll */
165 int sys_authenticate = 0; /* true when authenticating */
166 u_int32 sys_authkey = 0; /* set to authentication key in use */
167 u_long sys_authdelay = 0; /* authentication delay */
168 int sys_version = NTP_VERSION; /* version to poll with */
171 * The current internal time
173 u_long current_time = 0;
176 * Counter for keeping track of completed servers
178 int complete_servers = 0;
181 * File of encryption keys
186 #define KEYFILE "/etc/ntp.keys"
188 #define KEYFILE "%windir%\\ntp.keys"
189 # endif /* SYS_WINNT */
193 const char *key_file = KEYFILE;
195 char key_file_storage[MAX_PATH+1], *key_file ;
196 #endif /* SYS_WINNT */
199 * Miscellaneous flags
205 int ntpdatemain P((int, char **));
207 static void transmit P((struct server *));
208 static void receive P((struct recvbuf *));
209 static void server_data P((struct server *, s_fp, l_fp *, u_fp));
210 static void clock_filter P((struct server *));
211 static struct server *clock_select P((void));
212 static int clock_adjust P((void));
213 static void addserver P((char *));
214 static struct server *findserver P((struct sockaddr_storage *));
215 void timer P((void));
216 static void init_alarm P((void));
218 static RETSIGTYPE alarming P((int));
219 #endif /* SYS_WINNT */
220 static void init_io P((void));
221 static void sendpkt P((struct sockaddr_storage *, struct pkt *, int));
222 void input_handler P((void));
224 static int l_adj_systime P((l_fp *));
225 static int l_step_systime P((l_fp *));
227 static void printserver P((struct server *, FILE *));
231 WORD wVersionRequested;
233 HANDLE TimerThreadHandle = NULL;
234 #endif /* SYS_WINNT */
236 #ifdef NO_MAIN_ALLOWED
237 CALL(ntpdate,"ntpdate",ntpdatemain);
248 * Initializing flag. All async routines watch this and only do their
249 * thing when it is clear.
254 * Alarm flag. Set when an alarm occurs
264 * Unpriviledged port flag.
269 * Time to spend measuring drift rate
273 * Systemwide parameters and flags
275 sys_numservers = 0; /* number of servers to poll */
276 sys_authenticate = 0; /* true when authenticating */
277 sys_authkey = 0; /* set to authentication key in use */
278 sys_authdelay = 0; /* authentication delay */
279 sys_version = NTP_VERSION; /* version to poll with */
282 * The current internal time
287 * Counter for keeping track of completed servers
289 complete_servers = 0;
297 static ni_namelist *getnetinfoservers P((void));
301 * Main program. Initialize us and loop waiting for I/O and/or
304 #ifndef NO_MAIN_ALLOWED
311 return ntpdatemain (argc, argv);
313 #endif /* NO_MAIN_ALLOWED */
322 struct recvbuf *rbuflist;
323 struct recvbuf *rbuf;
330 ni_namelist *netinfoservers;
333 HANDLE process_handle;
335 wVersionRequested = MAKEWORD(1,1);
336 if (WSAStartup(wVersionRequested, &wsaData)) {
337 netsyslog(LOG_ERR, "No useable winsock.dll: %m");
341 key_file = key_file_storage;
343 if (!ExpandEnvironmentStrings(KEYFILE, key_file, MAX_PATH))
345 msyslog(LOG_ERR, "ExpandEnvironmentStrings(KEYFILE) failed: %m\n");
347 #endif /* SYS_WINNT */
349 #ifdef NO_MAIN_ALLOWED
358 * Decode argument list
360 while ((c = ntp_getopt(argc, argv, "46a:bBde:k:o:p:qr:st:uv")) != EOF)
364 ai_fam_templ = AF_INET;
367 ai_fam_templ = AF_INET6;
370 c = atoi(ntp_optarg);
371 sys_authenticate = 1;
386 if (!atolfp(ntp_optarg, &tmp)
388 (void) fprintf(stderr,
389 "%s: encryption delay %s is unlikely\n",
390 progname, ntp_optarg);
393 sys_authdelay = tmp.l_uf;
397 key_file = ntp_optarg;
400 sys_version = atoi(ntp_optarg);
403 c = atoi(ntp_optarg);
404 if (c <= 0 || c > NTP_SHIFT) {
405 (void) fprintf(stderr,
406 "%s: number of samples (%d) is invalid\n",
417 c = atoi(ntp_optarg);
418 if (c <= 0 || c > (60 * 60)) {
419 (void) fprintf(stderr,
420 "%s: rate (%d) is invalid: 0 - %d\n",
421 progname, c, (60 * 60));
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#] [-r rate] [-t timeo] server ...\n",
463 if (debug || simple_query) {
465 static char buf[BUFSIZ];
466 setvbuf(stdout, buf, _IOLBF, BUFSIZ);
473 * Logging. Open the syslog if we have to
476 #if !defined (SYS_WINNT) && !defined (SYS_VXWORKS) && !defined SYS_CYGWIN32
478 openlog("ntpdate", LOG_PID);
482 # define LOG_NTP LOG_DAEMON
484 openlog("ntpdate", LOG_PID | LOG_NDELAY, LOG_NTP);
486 setlogmask(LOG_UPTO(LOG_DEBUG));
488 setlogmask(LOG_UPTO(LOG_INFO));
489 # endif /* LOG_DAEMON */
490 #endif /* SYS_WINNT */
493 if (debug || verbose)
494 msyslog(LOG_NOTICE, "%s", Version);
497 * Add servers we are going to be polling
500 netinfoservers = getnetinfoservers();
503 for ( ; ntp_optind < argc; ntp_optind++)
504 addserver(argv[ntp_optind]);
507 if (netinfoservers) {
508 if ( netinfoservers->ni_namelist_len &&
509 *netinfoservers->ni_namelist_val ) {
510 u_int servercount = 0;
511 while (servercount < netinfoservers->ni_namelist_len) {
512 if (debug) msyslog(LOG_DEBUG,
513 "Adding time server %s from NetInfo configuration.",
514 netinfoservers->ni_namelist_val[servercount]);
515 addserver(netinfoservers->ni_namelist_val[servercount++]);
518 ni_namelist_free(netinfoservers);
519 free(netinfoservers);
523 if (sys_numservers == 0) {
524 msyslog(LOG_ERR, "no servers can be used, exiting");
529 * Initialize the time of day routines and the I/O subsystem
531 if (sys_authenticate) {
533 if (!authreadkeys(key_file)) {
534 msyslog(LOG_ERR, "no key file <%s>, exiting", key_file);
537 authtrust(sys_authkey, 1);
538 if (!authistrusted(sys_authkey)) {
541 (void) sprintf(buf, "%lu", (unsigned long)sys_authkey);
542 msyslog(LOG_ERR, "authentication key %s unknown", buf);
553 taskPrioritySet( taskIdSelf(), NTPDATE_PRIO);
555 #if defined(HAVE_ATT_NICE)
558 #if defined(HAVE_BSD_NICE)
559 (void) setpriority(PRIO_PROCESS, 0, NTPDATE_PRIO);
562 process_handle = GetCurrentProcess();
563 if (!SetPriorityClass(process_handle, (DWORD) REALTIME_PRIORITY_CLASS)) {
564 msyslog(LOG_ERR, "SetPriorityClass failed: %m");
566 #endif /* SYS_WINNT */
572 rbuflist = (struct recvbuf *)0;
574 while (complete_servers < sys_numservers) {
576 struct pollfd* rdfdes;
583 if (alarm_flag) { /* alarmed? */
587 rbuflist = getrecvbufs(); /* get received buffers */
589 if (!was_alarmed && rbuflist == (struct recvbuf *)0) {
591 * Nothing to do. Wait for something.
594 nfound = poll(rdfdes, (unsigned int)nbsock, timeout.tv_sec * 1000);
597 nfound = select(maxfd, &rdfdes, (fd_set *)0,
598 (fd_set *)0, &timeout);
606 nfound == SOCKET_ERROR
607 #endif /* SYS_WINNT */
623 "poll(): nfound = %d, error: %m",
625 "select(): nfound = %d, error: %m",
630 if (alarm_flag) { /* alarmed? */
634 rbuflist = getrecvbufs(); /* get received buffers */
638 * Out here, signals are unblocked. Call receive
639 * procedure for each incoming packet.
641 while (rbuflist != (struct recvbuf *)0) {
643 rbuflist = rbuf->next;
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 || 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 */
1316 /* XXX ELIMINATE: merge BIG slew into adj_systime in lib/systime.c */
1318 * addserver - determine a server's address and allocate a new structure
1326 register struct server *server;
1327 /* Address infos structure to store result of getaddrinfo */
1328 struct addrinfo *addrResult;
1329 /* Address infos structure to store hints for getaddrinfo */
1330 struct addrinfo hints;
1331 /* Error variable for getaddrinfo */
1335 strcpy(service, "ntp");
1337 /* Get host address. Looking for UDP datagram connection. */
1338 memset(&hints, 0, sizeof(hints));
1339 hints.ai_family = ai_fam_templ;
1340 hints.ai_socktype = SOCK_DGRAM;
1344 printf("Looking for host %s and service %s\n", serv, service);
1347 error = getaddrinfo(serv, service, &hints, &addrResult);
1349 fprintf(stderr, "Error : %s\n", gai_strerror(error));
1350 msyslog(LOG_ERR, "can't find host %s\n", serv);
1355 fprintf(stderr, "host found : %s\n", stohost((struct sockaddr_storage*)addrResult->ai_addr));
1359 server = (struct server *)emalloc(sizeof(struct server));
1360 memset((char *)server, 0, sizeof(struct server));
1362 /* For now we only get the first returned server of the addrinfo list */
1363 memset(&(server->srcadr), 0, sizeof(struct sockaddr_storage));
1364 memcpy(&(server->srcadr), addrResult->ai_addr, addrResult->ai_addrlen);
1365 server->event_time = ++sys_numservers;
1366 if (sys_servers == NULL)
1367 sys_servers = server;
1371 for (sp = sys_servers; sp->next_server != NULL;
1372 sp = sp->next_server) ;
1373 sp->next_server = server;
1379 * findserver - find a server in the list given its address
1380 * ***(For now it isn't totally AF-Independant, to check later..)
1382 static struct server *
1384 struct sockaddr_storage *addr
1387 struct server *server;
1388 struct server *mc_server;
1391 if (htons(((struct sockaddr_in*)addr)->sin_port) != NTP_PORT)
1394 for (server = sys_servers; server != NULL;
1395 server = server->next_server) {
1397 if (memcmp(addr, &server->srcadr, SOCKLEN(addr))==0)
1399 /* Multicast compatibility to verify here... I'm not sure it's working */
1400 if(addr->ss_family == AF_INET) {
1401 if (IN_MULTICAST(ntohl(((struct sockaddr_in*)addr)->sin_addr.s_addr)))
1406 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)(&server->srcadr))->sin6_addr))
1414 if (mc_server != NULL) {
1418 if (mc_server->event_time != 0) {
1419 mc_server->event_time = 0;
1423 server = (struct server *)emalloc(sizeof(struct server));
1424 memset((char *)server, 0, sizeof(struct server));
1426 memcpy(&server->srcadr, &addr, sizeof(struct sockaddr_storage));
1428 server->event_time = ++sys_numservers;
1430 for (sp = sys_servers; sp->next_server != NULL;
1431 sp = sp->next_server) ;
1432 sp->next_server = server;
1440 * timer - process a timer interrupt
1445 struct server *server;
1448 * Bump the current idea of the time
1453 * Search through the server list looking for guys
1454 * who's event timers have expired. Give these to
1455 * the transmit routine.
1457 for (server = sys_servers; server != NULL;
1458 server = server->next_server) {
1459 if (server->event_time != 0
1460 && server->event_time <= current_time)
1467 * The code duplication in the following subroutine sucks, but
1468 * we need to appease ansi2knr.
1473 * alarming - record the occurance of an alarm interrupt
1484 alarming(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
1488 #endif /* SYS_WINNT */
1492 * init_alarm - set up the timer interrupt
1498 # ifndef HAVE_TIMER_SETTIME
1499 struct itimerval itimer;
1501 struct itimerspec ntpdate_itimer;
1505 UINT wTimerRes, wTimerID;
1506 # endif /* SYS_WINNT */
1507 #if defined SYS_CYGWIN32 || defined SYS_WINNT
1509 TOKEN_PRIVILEGES tkp;
1511 #endif /* SYS_WINNT */
1516 # if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME)
1518 /* this code was put in as setitimer() is non existant this us the
1519 * POSIX "equivalents" setup - casey
1521 /* ntpdate_timerid is global - so we can kill timer later */
1522 if (timer_create (CLOCK_REALTIME, NULL, &ntpdate_timerid) ==
1530 fprintf (stderr, "init_alarm(): timer_create (...) FAILED\n");
1535 * Set up the alarm interrupt. The first comes 1/(2*TIMER_HZ)
1536 * seconds from now and they continue on every 1/TIMER_HZ seconds.
1538 (void) signal_no_reset(SIGALRM, alarming);
1539 ntpdate_itimer.it_interval.tv_sec = ntpdate_itimer.it_value.tv_sec = 0;
1540 ntpdate_itimer.it_interval.tv_nsec = 1000000000/TIMER_HZ;
1541 ntpdate_itimer.it_value.tv_nsec = 1000000000/(TIMER_HZ<<1);
1542 timer_settime(ntpdate_timerid, 0 /* !TIMER_ABSTIME */, &ntpdate_itimer, NULL);
1545 * Set up the alarm interrupt. The first comes 1/(2*TIMER_HZ)
1546 * seconds from now and they continue on every 1/TIMER_HZ seconds.
1548 (void) signal_no_reset(SIGALRM, alarming);
1549 itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0;
1550 itimer.it_interval.tv_usec = 1000000/TIMER_HZ;
1551 itimer.it_value.tv_usec = 1000000/(TIMER_HZ<<1);
1553 setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
1555 #if defined SYS_CYGWIN32
1557 * Get previleges needed for fiddling with the clock
1560 /* get the current process token handle */
1561 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
1562 msyslog(LOG_ERR, "OpenProcessToken failed: %m");
1565 /* get the LUID for system-time privilege. */
1566 LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkp.Privileges[0].Luid);
1567 tkp.PrivilegeCount = 1; /* one privilege to set */
1568 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1569 /* get set-time privilege for this process. */
1570 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);
1571 /* cannot test return value of AdjustTokenPrivileges. */
1572 if (GetLastError() != ERROR_SUCCESS)
1573 msyslog(LOG_ERR, "AdjustTokenPrivileges failed: %m");
1575 #else /* SYS_WINNT */
1579 * Get previleges needed for fiddling with the clock
1582 /* get the current process token handle */
1583 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
1584 msyslog(LOG_ERR, "OpenProcessToken failed: %m");
1587 /* get the LUID for system-time privilege. */
1588 LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkp.Privileges[0].Luid);
1589 tkp.PrivilegeCount = 1; /* one privilege to set */
1590 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1591 /* get set-time privilege for this process. */
1592 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);
1593 /* cannot test return value of AdjustTokenPrivileges. */
1594 if (GetLastError() != ERROR_SUCCESS)
1595 msyslog(LOG_ERR, "AdjustTokenPrivileges failed: %m");
1598 * Set up timer interrupts for every 2**EVENT_TIMEOUT seconds
1599 * Under Win/NT, expiry of timer interval leads to invocation
1600 * of a callback function (on a different thread) rather than
1601 * generating an alarm signal
1604 /* determine max and min resolution supported */
1605 if(timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) {
1606 msyslog(LOG_ERR, "timeGetDevCaps failed: %m");
1609 wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
1610 /* establish the minimum timer resolution that we'll use */
1611 timeBeginPeriod(wTimerRes);
1613 /* start the timer event */
1614 wTimerID = timeSetEvent(
1615 (UINT) (1000/TIMER_HZ), /* Delay */
1616 wTimerRes, /* Resolution */
1617 (LPTIMECALLBACK) alarming, /* Callback function */
1618 (DWORD) dwUser, /* User data */
1619 TIME_PERIODIC); /* Event type (periodic) */
1620 if (wTimerID == 0) {
1621 msyslog(LOG_ERR, "timeSetEvent failed: %m");
1624 #endif /* SYS_WINNT */
1631 * We do asynchronous input using the SIGIO facility. A number of
1632 * recvbuf buffers are preallocated for input. In the signal
1633 * handler we poll to see if the socket is ready and read the
1634 * packets from it into the recvbuf's along with a time stamp and
1635 * an indication of the source host and the interface it was received
1636 * through. This allows us to get as accurate receive time stamps
1637 * as possible independent of other processing going on.
1639 * We allocate a number of recvbufs equal to the number of servers
1640 * plus 2. This should be plenty.
1645 * init_io - initialize I/O data and open socket
1650 struct addrinfo *res, *ressave;
1651 struct addrinfo hints;
1656 * Init buffer free list and stat counters
1658 init_recvbuff(sys_numservers + 2);
1664 strcpy(service, "ntp");
1667 * Init hints addrinfo structure
1669 memset(&hints, 0, sizeof(hints));
1670 hints.ai_flags = AI_PASSIVE;
1671 hints.ai_socktype = SOCK_DGRAM;
1673 if(getaddrinfo(NULL, service, &hints, &res) != 0) {
1674 msyslog(LOG_ERR, "getaddrinfo() failed: %m");
1679 /* Remember the address of the addrinfo structure chain */
1683 * For each structure returned, open and bind socket
1685 for(nbsock = 0; (nbsock < MAX_AF) && res ; res = res->ai_next) {
1686 /* create a datagram (UDP) socket */
1687 if ((fd[nbsock] = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
1688 if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT ||
1689 errno == EPFNOSUPPORT)
1691 netsyslog(LOG_ERR, "socket() failed: %m");
1695 /* set socket to reuse address */
1696 if (setsockopt(fd[nbsock], SOL_SOCKET, SO_REUSEADDR, (void*) &optval, sizeof(optval)) < 0) {
1697 netsyslog(LOG_ERR, "setsockopt() SO_REUSEADDR failed: %m");
1702 /* Restricts AF_INET6 socket to IPv6 communications (see RFC 2553bis-03) */
1703 if (res->ai_family == AF_INET6)
1704 if (setsockopt(fd[nbsock], IPPROTO_IPV6, IPV6_V6ONLY, (void*) &optval, sizeof(optval)) < 0) {
1705 netsyslog(LOG_ERR, "setsockopt() IPV6_V6ONLY failed: %m");
1711 /* Remember the socket family in fd_family structure */
1712 fd_family[nbsock] = res->ai_family;
1715 * bind the socket to the NTP port
1717 if (!debug && !simple_query && !unpriv_port) {
1718 if (bind(fd[nbsock], res->ai_addr, SOCKLEN(res->ai_addr)) < 0) {
1720 if (errno == EADDRINUSE)
1722 if (WSAGetLastError() == WSAEADDRINUSE)
1723 #endif /* SYS_WINNT */
1725 "the NTP socket is in use, exiting");
1727 netsyslog(LOG_ERR, "bind() fails: %m");
1733 fdmask[nbsock].fd = fd[nbsock];
1734 fdmask[nbsock].events = POLLIN;
1736 FD_SET(fd[nbsock], &fdmask);
1737 if ((SOCKET) maxfd < fd[nbsock]+1) {
1738 maxfd = fd[nbsock]+1;
1750 if (ioctl(fd[nbsock],FIONBIO, &on) == ERROR) {
1751 netsyslog(LOG_ERR, "ioctl(FIONBIO) fails: %m");
1755 # else /* not SYS_VXWORKS */
1756 # if defined(O_NONBLOCK)
1757 if (fcntl(fd[nbsock], F_SETFL, O_NONBLOCK) < 0) {
1758 netsyslog(LOG_ERR, "fcntl(FNDELAY|FASYNC) fails: %m");
1762 # else /* not O_NONBLOCK */
1763 # if defined(FNDELAY)
1764 if (fcntl(fd[nbsock], F_SETFL, FNDELAY) < 0) {
1765 netsyslog(LOG_ERR, "fcntl(FNDELAY|FASYNC) fails: %m");
1769 # else /* FNDELAY */
1770 # include "Bletch: Need non blocking I/O"
1771 # endif /* FNDELAY */
1772 # endif /* not O_NONBLOCK */
1773 # endif /* SYS_VXWORKS */
1774 #else /* SYS_WINNT */
1775 if (ioctlsocket(fd[nbsock], FIONBIO, (u_long *) &on) == SOCKET_ERROR) {
1776 netsyslog(LOG_ERR, "ioctlsocket(FIONBIO) fails: %m");
1779 #endif /* SYS_WINNT */
1782 freeaddrinfo(ressave);
1786 * sendpkt - send a packet to the specified destination
1790 struct sockaddr_storage *dest,
1801 #endif /* SYS_WINNT */
1803 /* Find a local family compatible socket to send ntp packet to ntp server */
1804 for(i = 0; (i < MAX_AF); i++) {
1805 if(dest->ss_family == fd_family[i]) {
1812 netsyslog(LOG_ERR, "cannot find family compatible socket to send ntp packet");
1817 cc = sendto(sock, (char *)pkt, len, 0, (struct sockaddr *)dest,
1822 if (errno != EWOULDBLOCK && errno != ENOBUFS)
1824 if (cc == SOCKET_ERROR) {
1825 err = WSAGetLastError();
1826 if (err != WSAEWOULDBLOCK && err != WSAENOBUFS)
1827 #endif /* SYS_WINNT */
1828 netsyslog(LOG_ERR, "sendto(%s): %m", stohost(dest));
1834 * input_handler - receive packets asynchronously
1840 register struct recvbuf *rb;
1841 struct timeval tvzero;
1846 struct pollfd fds[MAX_AF];
1853 * Do a poll to see if we have data
1856 tvzero.tv_sec = tvzero.tv_usec = 0;
1858 memcpy(fds, fdmask, sizeof(fdmask));
1859 n = poll(fds, (unsigned int)nbsock, tvzero.tv_sec * 1000);
1862 * Determine which socket received data
1865 for(i=0; i < nbsock; i++) {
1866 if(fds[i].revents & POLLIN) {
1874 n = select(maxfd, &fds, (fd_set *)0, (fd_set *)0, &tvzero);
1877 * Determine which socket received data
1880 for(i=0; i < maxfd; i++) {
1881 if(FD_ISSET(fd[i], &fds)) {
1890 * If nothing to do, just return. If an error occurred,
1891 * complain and return. If we've got some, freeze a
1902 "select() error: %m"
1910 * Get a buffer and read the frame. If we
1911 * haven't got a buffer, or this is received
1912 * on the wild card socket, just dump the packet.
1914 if (initializing || free_recvbuffs() == 0) {
1919 (void) read(fdc, buf, sizeof buf);
1921 /* NT's _read does not operate on nonblocking sockets
1922 * either recvfrom or ReadFile() has to be used here.
1923 * ReadFile is used in [ntpd]ntp_intres() and ntpdc,
1924 * just to be different use recvfrom() here
1926 recvfrom(fdc, buf, sizeof(buf), 0, (struct sockaddr *)0, NULL);
1927 #endif /* SYS_WINNT */
1931 rb = get_free_recv_buffer();
1933 fromlen = sizeof(struct sockaddr_storage);
1934 rb->recv_length = recvfrom(fdc, (char *)&rb->recv_pkt,
1935 sizeof(rb->recv_pkt), 0,
1936 (struct sockaddr *)&rb->recv_srcadr, &fromlen);
1937 if (rb->recv_length == -1) {
1943 * Got one. Mark how and when it got here,
1944 * put it on the full list.
1947 add_full_recv_buffer(rb);
1952 #if !defined SYS_WINNT && !defined SYS_CYGWIN32
1954 * adj_systime - do a big long slew of the system time
1961 struct timeval adjtv, oadjtv;
1969 * Take the absolute value of the offset
1972 if (L_ISNEG(&offset)) {
1979 * Calculate the overshoot. XXX N.B. This code *knows*
1980 * ADJ_OVERSHOOT is 1/2.
1983 L_RSHIFTU(&overshoot);
1984 if (overshoot.l_ui != 0 || (overshoot.l_uf > ADJ_MAXOVERSHOOT)) {
1986 overshoot.l_uf = ADJ_MAXOVERSHOOT;
1988 L_ADD(&offset, &overshoot);
1990 TSTOTV(&offset, &adjtv);
1993 adjtv.tv_sec = -adjtv.tv_sec;
1994 adjtv.tv_usec = -adjtv.tv_usec;
1997 if (adjtv.tv_usec != 0 && !debug) {
1998 if (adjtime(&adjtv, &oadjtv) < 0) {
1999 msyslog(LOG_ERR, "Can't adjust the time of day: %m");
2005 #endif /* SYS_WINNT */
2009 * This fuction is not the same as lib/systime step_systime!!!
2024 if (debug) return 1;
2026 * Take the absolute value of the offset
2029 if (L_ISNEG(&ftmp)) {
2035 if (ftmp.l_ui >= 3) { /* Step it and slew - we might win */
2037 n = step_systime(dtemp);
2046 * Just add adjustment into the current offset. The update
2047 * routine will take care of bringing the system clock into
2053 #ifdef FORCE_NTPDATE_STEP
2055 return step_systime(dtemp);
2060 #else /* SLEWALWAYS */
2064 return step_systime(dtemp);
2065 #endif /* SLEWALWAYS */
2069 /* XXX ELIMINATE printserver similar in ntptrace.c, ntpdate.c */
2071 * printserver - print detail information for a server
2075 register struct server *pp,
2084 (void) fprintf(fp, "server %s, stratum %d, offset %s, delay %s\n",
2085 stoa(&pp->srcadr), pp->stratum,
2086 lfptoa(&pp->offset, 6), fptoa((s_fp)pp->delay, 5));
2090 (void) fprintf(fp, "server %s, port %d\n",
2091 stoa(&pp->srcadr), ntohs(((struct sockaddr_in*)&(pp->srcadr))->sin_port));
2093 (void) fprintf(fp, "stratum %d, precision %d, leap %c%c, trust %03o\n",
2094 pp->stratum, pp->precision,
2095 pp->leap & 0x2 ? '1' : '0',
2096 pp->leap & 0x1 ? '1' : '0',
2099 if (pp->stratum == 1) {
2101 memmove(junk, (char *)&pp->refid, 4);
2104 str = stoa(&pp->srcadr);
2107 "refid [%s], delay %s, dispersion %s\n",
2108 str, fptoa((s_fp)pp->delay, 5),
2109 ufptoa(pp->dispersion, 5));
2111 (void) fprintf(fp, "transmitted %d, in filter %d\n",
2112 pp->xmtcnt, pp->filter_nextpt);
2114 (void) fprintf(fp, "reference time: %s\n",
2115 prettydate(&pp->reftime));
2116 (void) fprintf(fp, "originate timestamp: %s\n",
2117 prettydate(&pp->org));
2118 (void) fprintf(fp, "transmit timestamp: %s\n",
2119 prettydate(&pp->xmt));
2121 (void) fprintf(fp, "filter delay: ");
2122 for (i = 0; i < NTP_SHIFT; i++) {
2123 (void) fprintf(fp, " %-8.8s", fptoa(pp->filter_delay[i], 5));
2124 if (i == (NTP_SHIFT>>1)-1)
2125 (void) fprintf(fp, "\n ");
2127 (void) fprintf(fp, "\n");
2129 (void) fprintf(fp, "filter offset:");
2130 for (i = 0; i < PEER_SHIFT; i++) {
2131 (void) fprintf(fp, " %-8.8s", lfptoa(&pp->filter_offset[i], 6));
2132 if (i == (PEER_SHIFT>>1)-1)
2133 (void) fprintf(fp, "\n ");
2135 (void) fprintf(fp, "\n");
2137 (void) fprintf(fp, "delay %s, dispersion %s\n",
2138 fptoa((s_fp)pp->delay, 5), ufptoa(pp->dispersion, 5));
2140 (void) fprintf(fp, "offset %s\n\n",
2141 lfptoa(&pp->offset, 6));
2144 #if !defined(HAVE_VSPRINTF)
2155 f._flag = _IOWRT+_IOSTRG;
2158 len = _doprnt(fmt, ap, &f);
2165 /* override function in library since SA_RESTART makes ALL syscalls restart */
2174 struct sigaction vec;
2176 vec.sa_handler = func;
2177 sigemptyset(&vec.sa_mask);
2182 n = sigaction(sig, &vec, NULL);
2183 if (n == -1 && errno == EINTR)
2189 perror("sigaction");
2197 static ni_namelist *
2198 getnetinfoservers(void)
2203 ni_namelist *namelist = (ni_namelist*)malloc(sizeof(ni_namelist));
2205 /* Find a time server in NetInfo */
2206 if ((status = ni_open(NULL, ".", &domain)) != NI_OK) return NULL;
2208 while (status = ni_pathsearch(domain, &confdir, NETINFO_CONFIG_DIR) == NI_NODIR) {
2210 if (ni_open(domain, "..", &next_domain) != NI_OK) break;
2212 domain = next_domain;
2214 if (status != NI_OK) return NULL;
2217 if (ni_lookupprop(domain, &confdir, "server", namelist) != NI_OK) {
2218 ni_namelist_free(namelist);