2 * ntpq - query an NTP server using mode 6 commands
10 #include <sys/types.h>
14 #include "ntp_unixtime.h"
15 #include "ntp_calendar.h"
17 #include "ntp_select.h"
18 #include "ntp_stdlib.h"
19 /* Don't include ISC's version of IPv6 variables and structures */
22 #include "isc/result.h"
24 #include "ntpq-opts.h"
30 # define closesocket close
31 #endif /* SYS_WINNT */
33 #if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDIT)
34 # include <readline/readline.h>
35 # include <readline/history.h>
36 #endif /* HAVE_LIBREADLINE || HAVE_LIBEDIT */
39 /* vxWorks needs mode flag -casey*/
40 # define open(name, flags) open(name, flags, 0777)
41 # define SERVER_PORT_NUM 123
44 /* we use COMMAND as an autogen keyword */
50 * Because we potentially understand a lot of commands we will run
51 * interactive if connected to a terminal.
53 int interactive = 0; /* set to 1 when we should prompt */
54 const char *prompt = "ntpq> "; /* prompt to ask him about */
60 s_char sys_precision; /* local clock precision (log2 s) */
63 * Keyid used for authenticated requests. Obtained on the fly.
65 u_long info_auth_keyid = 0;
70 #define KEY_TYPE_MD5 4
72 static int info_auth_keytype = KEY_TYPE_MD5; /* MD5 */
73 u_long current_time; /* needed by authkeys; not used */
76 * Flag which indicates we should always send authenticated requests
81 * Flag which indicates raw mode output.
86 * Packet version number we use
88 u_char pktversion = NTP_OLDVERSION + 1;
91 * Don't jump if no set jmp.
93 volatile int jump = 0;
99 #define TS 1 /* time stamp */
100 #define FL 2 /* l_fp type value */
101 #define FU 3 /* u_fp type value */
102 #define FS 4 /* s_fp type value */
103 #define UI 5 /* unsigned integer value */
104 #define SI 6 /* signed integer value */
105 #define HA 7 /* host address */
106 #define NA 8 /* network address */
107 #define ST 9 /* string value */
108 #define RF 10 /* refid (sometimes string, sometimes not) */
109 #define LP 11 /* leap (print in binary) */
110 #define OC 12 /* integer, print in octal */
111 #define MD 13 /* mode */
112 #define AR 14 /* array of times */
113 #define FX 15 /* test flags */
114 #define EOV 255 /* end of table */
118 * System variable values. The array can be indexed by
119 * the variable index to find the textual name.
121 struct ctl_var sys_var[] = {
122 { 0, PADDING, "" }, /* 0 */
123 { CS_LEAP, LP, "leap" }, /* 1 */
124 { CS_STRATUM, UI, "stratum" }, /* 2 */
125 { CS_PRECISION, SI, "precision" }, /* 3 */
126 { CS_ROOTDELAY, FS, "rootdelay" }, /* 4 */
127 { CS_ROOTDISPERSION, FU, "rootdispersion" }, /* 5 */
128 { CS_REFID, RF, "refid" }, /* 6 */
129 { CS_REFTIME, TS, "reftime" }, /* 7 */
130 { CS_POLL, UI, "poll" }, /* 8 */
131 { CS_PEERID, UI, "peer" }, /* 9 */
132 { CS_STATE, UI, "state" }, /* 10 */
133 { CS_OFFSET, FL, "offset" }, /* 11 */
134 { CS_DRIFT, FS, "frequency" }, /* 12 */
135 { CS_JITTER, FU, "jitter" }, /* 13 */
136 { CS_CLOCK, TS, "clock" }, /* 14 */
137 { CS_PROCESSOR, ST, "processor" }, /* 15 */
138 { CS_SYSTEM, ST, "system" }, /* 16 */
139 { CS_VERSION, ST, "version" }, /* 17 */
140 { CS_STABIL, FS, "stability" }, /* 18 */
141 { CS_VARLIST, ST, "sys_var_list" }, /* 19 */
149 struct ctl_var peer_var[] = {
150 { 0, PADDING, "" }, /* 0 */
151 { CP_CONFIG, UI, "config" }, /* 1 */
152 { CP_AUTHENABLE, UI, "authenable" }, /* 2 */
153 { CP_AUTHENTIC, UI, "authentic" }, /* 3 */
154 { CP_SRCADR, HA, "srcadr" }, /* 4 */
155 { CP_SRCPORT, UI, "srcport" }, /* 5 */
156 { CP_DSTADR, NA, "dstadr" }, /* 6 */
157 { CP_DSTPORT, UI, "dstport" }, /* 7 */
158 { CP_LEAP, LP, "leap" }, /* 8 */
159 { CP_HMODE, MD, "hmode" }, /* 9 */
160 { CP_STRATUM, UI, "stratum" }, /* 10 */
161 { CP_PPOLL, UI, "ppoll" }, /* 11 */
162 { CP_HPOLL, UI, "hpoll" }, /* 12 */
163 { CP_PRECISION, SI, "precision" }, /* 13 */
164 { CP_ROOTDELAY, FS, "rootdelay" }, /* 14 */
165 { CP_ROOTDISPERSION, FU, "rootdispersion" }, /* 15 */
166 { CP_REFID, RF, "refid" }, /* 16 */
167 { CP_REFTIME, TS, "reftime" }, /* 17 */
168 { CP_ORG, TS, "org" }, /* 18 */
169 { CP_REC, TS, "rec" }, /* 19 */
170 { CP_XMT, TS, "xmt" }, /* 20 */
171 { CP_REACH, OC, "reach" }, /* 21 */
172 { CP_UNREACH, UI, "unreach" }, /* 22 */
173 { CP_TIMER, UI, "timer" }, /* 23 */
174 { CP_DELAY, FS, "delay" }, /* 24 */
175 { CP_OFFSET, FL, "offset" }, /* 25 */
176 { CP_JITTER, FU, "jitter" }, /* 26 */
177 { CP_DISPERSION, FU, "dispersion" }, /* 27 */
178 { CP_KEYID, UI, "keyid" }, /* 28 */
179 { CP_FILTDELAY, AR, "filtdelay" }, /* 29 */
180 { CP_FILTOFFSET, AR, "filtoffset" }, /* 30 */
181 { CP_PMODE, ST, "pmode" }, /* 31 */
182 { CP_RECEIVED, UI, "received" }, /* 32 */
183 { CP_SENT, UI, "sent" }, /* 33 */
184 { CP_FILTERROR, AR, "filtdisp" }, /* 34 */
185 { CP_FLASH, FX, "flash" }, /* 35 */
186 { CP_TTL, UI, "ttl" }, /* 36 */
188 * These are duplicate entries so that we can
189 * process deviant version of the ntp protocol.
191 { CP_SRCADR, HA, "peeraddr" }, /* 4 */
192 { CP_SRCPORT, UI, "peerport" }, /* 5 */
193 { CP_PPOLL, UI, "peerpoll" }, /* 11 */
194 { CP_HPOLL, UI, "hostpoll" }, /* 12 */
195 { CP_FILTERROR, AR, "filterror" }, /* 34 */
201 * Clock variable list
203 struct ctl_var clock_var[] = {
204 { 0, PADDING, "" }, /* 0 */
205 { CC_TYPE, UI, "type" }, /* 1 */
206 { CC_TIMECODE, ST, "timecode" }, /* 2 */
207 { CC_POLL, UI, "poll" }, /* 3 */
208 { CC_NOREPLY, UI, "noreply" }, /* 4 */
209 { CC_BADFORMAT, UI, "badformat" }, /* 5 */
210 { CC_BADDATA, UI, "baddata" }, /* 6 */
211 { CC_FUDGETIME1, FL, "fudgetime1" }, /* 7 */
212 { CC_FUDGETIME2, FL, "fudgetime2" }, /* 8 */
213 { CC_FUDGEVAL1, UI, "stratum" }, /* 9 */
214 { CC_FUDGEVAL2, RF, "refid" }, /* 10 */
215 { CC_FLAGS, UI, "flags" }, /* 11 */
216 { CC_DEVICE, ST, "device" }, /* 12 */
224 static const char *tstflagnames[] = {
225 "pkt_dup", /* TEST1 */
226 "pkt_bogus", /* TEST2 */
227 "pkt_proto", /* TEST3 */
228 "pkt_denied", /* TEST4 */
229 "pkt_auth", /* TEST5 */
230 "pkt_synch", /* TEST6 */
231 "pkt_dist", /* TEST7 */
232 "pkt_autokey", /* TEST8 */
233 "pkt_crypto", /* TEST9 */
234 "peer_stratum", /* TEST10 */
235 "peer_dist", /* TEST11 */
236 "peer_loop", /* TEST12 */
237 "peer_unfit" /* TEST13 */
241 int ntpqmain P((int, char **));
243 * Built in command handler declarations
245 static int openhost P((const char *));
246 static int sendpkt P((char *, int));
247 static int getresponse P((int, int, u_short *, int *, char **, int));
248 static int sendrequest P((int, int, int, int, char *));
249 static char * tstflags P((u_long));
250 static void getcmds P((void));
251 static RETSIGTYPE abortcmd P((int));
252 static void docmd P((const char *));
253 static void tokenize P((const char *, char **, int *));
254 static int findcmd P((char *, struct xcmd *, struct xcmd *, struct xcmd **));
255 static int getarg P((char *, int, arg_v *));
256 static int rtdatetolfp P((char *, l_fp *));
257 static int decodearr P((char *, int *, l_fp *));
258 static void help P((struct parse *, FILE *));
259 #ifdef QSORT_USES_VOID_P
260 static int helpsort P((const void *, const void *));
262 static int helpsort P((char **, char **));
264 static void printusage P((struct xcmd *, FILE *));
265 static void timeout P((struct parse *, FILE *));
266 static void auth_delay P((struct parse *, FILE *));
267 static void host P((struct parse *, FILE *));
268 static void ntp_poll P((struct parse *, FILE *));
269 static void keyid P((struct parse *, FILE *));
270 static void keytype P((struct parse *, FILE *));
271 static void passwd P((struct parse *, FILE *));
272 static void hostnames P((struct parse *, FILE *));
273 static void setdebug P((struct parse *, FILE *));
274 static void quit P((struct parse *, FILE *));
275 static void version P((struct parse *, FILE *));
276 static void raw P((struct parse *, FILE *));
277 static void cooked P((struct parse *, FILE *));
278 static void authenticate P((struct parse *, FILE *));
279 static void ntpversion P((struct parse *, FILE *));
280 static void warning P((const char *, const char *, const char *));
281 static void error P((const char *, const char *, const char *));
282 static u_long getkeyid P((const char *));
283 static void atoascii P((int, char *, char *));
284 static void makeascii P((int, char *, FILE *));
285 static void rawprint P((int, int, char *, int, FILE *));
286 static void startoutput P((void));
287 static void output P((FILE *, char *, char *));
288 static void endoutput P((FILE *));
289 static void outputarr P((FILE *, char *, int, l_fp *));
290 static void cookedprint P((int, int, char *, int, FILE *));
291 #ifdef QSORT_USES_VOID_P
292 static int assoccmp P((const void *, const void *));
294 static int assoccmp P((struct association *, struct association *));
295 #endif /* sgi || bsdi */
299 * Built-in commands we understand
301 struct xcmd builtins[] = {
302 { "?", help, { OPT|NTP_STR, NO, NO, NO },
303 { "command", "", "", "" },
304 "tell the use and syntax of commands" },
305 { "help", help, { OPT|NTP_STR, NO, NO, NO },
306 { "command", "", "", "" },
307 "tell the use and syntax of commands" },
308 { "timeout", timeout, { OPT|NTP_UINT, NO, NO, NO },
309 { "msec", "", "", "" },
310 "set the primary receive time out" },
311 { "delay", auth_delay, { OPT|NTP_INT, NO, NO, NO },
312 { "msec", "", "", "" },
313 "set the delay added to encryption time stamps" },
314 { "host", host, { OPT|NTP_STR, OPT|NTP_STR, NO, NO },
315 { "-4|-6", "hostname", "", "" },
316 "specify the host whose NTP server we talk to" },
317 { "poll", ntp_poll, { OPT|NTP_UINT, OPT|NTP_STR, NO, NO },
318 { "n", "verbose", "", "" },
319 "poll an NTP server in client mode `n' times" },
320 { "passwd", passwd, { NO, NO, NO, NO },
322 "specify a password to use for authenticated requests"},
323 { "hostnames", hostnames, { OPT|NTP_STR, NO, NO, NO },
324 { "yes|no", "", "", "" },
325 "specify whether hostnames or net numbers are printed"},
326 { "debug", setdebug, { OPT|NTP_STR, NO, NO, NO },
327 { "no|more|less", "", "", "" },
328 "set/change debugging level" },
329 { "quit", quit, { NO, NO, NO, NO },
332 { "exit", quit, { NO, NO, NO, NO },
335 { "keyid", keyid, { OPT|NTP_UINT, NO, NO, NO },
336 { "key#", "", "", "" },
337 "set keyid to use for authenticated requests" },
338 { "version", version, { NO, NO, NO, NO },
340 "print version number" },
341 { "raw", raw, { NO, NO, NO, NO },
343 "do raw mode variable output" },
344 { "cooked", cooked, { NO, NO, NO, NO },
346 "do cooked mode variable output" },
347 { "authenticate", authenticate, { OPT|NTP_STR, NO, NO, NO },
348 { "yes|no", "", "", "" },
349 "always authenticate requests to this server" },
350 { "ntpversion", ntpversion, { OPT|NTP_UINT, NO, NO, NO },
351 { "version number", "", "", "" },
352 "set the NTP version number to use for requests" },
353 { "keytype", keytype, { OPT|NTP_STR, NO, NO, NO },
354 { "key type (md5|des)", "", "", "" },
355 "set key type to use for authenticated requests (des|md5)" },
356 { 0, 0, { NO, NO, NO, NO },
357 { "", "", "", "" }, "" }
362 * Default values we use.
364 #define DEFTIMEOUT (5) /* 5 second time out */
365 #define DEFSTIMEOUT (2) /* 2 second time out after first */
366 #define DEFDELAY 0x51EB852 /* 20 milliseconds, l_fp fraction */
367 #define DEFHOST "localhost" /* default host name */
368 #define LENHOSTNAME 256 /* host name is 256 characters long */
369 #define MAXCMDS 100 /* maximum commands on cmd line */
370 #define MAXHOSTS 200 /* maximum hosts on cmd line */
371 #define MAXLINE 512 /* maximum line length */
372 #define MAXTOKENS (1+MAXARGS+2) /* maximum number of usable tokens */
373 #define MAXVARLEN 256 /* maximum length of a variable name */
374 #define MAXVALLEN 400 /* maximum length of a variable value */
375 #define MAXOUTLINE 72 /* maximum length of an output line */
376 #define SCREENWIDTH 76 /* nominal screen width in columns */
379 * Some variables used and manipulated locally
381 struct timeval tvout = { DEFTIMEOUT, 0 }; /* time out for reads */
382 struct timeval tvsout = { DEFSTIMEOUT, 0 }; /* secondary time out */
383 l_fp delay_time; /* delay time */
384 char currenthost[LENHOSTNAME]; /* current host name */
385 struct sockaddr_in hostaddr = { 0 }; /* host address */
386 int showhostnames = 1; /* show host names by default */
388 int ai_fam_templ; /* address family */
389 int ai_fam_default; /* default address family */
390 SOCKET sockfd; /* fd socket is opened on */
391 int havehost = 0; /* set to 1 when host open */
393 struct servent *server_entry = NULL; /* server entry for ntp */
396 DWORD NumberOfBytesWritten;
398 HANDLE TimerThreadHandle = NULL; /* 1998/06/03 - Used in ntplib/machines.c */
399 void timer(void) { ; }; /* 1998/06/03 - Used in ntplib/machines.c */
401 #endif /* SYS_WINNT */
404 * Sequence number used for requests. It is incremented before
410 * Holds data returned from queries. Declare buffer long to be sure of
413 #define MAXFRAGS 24 /* maximum number of fragments */
414 #define DATASIZE (MAXFRAGS*480) /* maximum amount of data */
415 long pktdata[DATASIZE/sizeof(long)];
418 * Holds association data for use with the &n operator.
420 struct association assoc_cache[MAXASSOC];
421 int numassoc = 0; /* number of cached associations */
424 * For commands typed on the command line (with the -c option)
427 const char *ccmds[MAXCMDS];
428 #define ADDCMD(cp) if (numcmds < MAXCMDS) ccmds[numcmds++] = (cp)
431 * When multiple hosts are specified.
434 const char *chosts[MAXHOSTS];
435 #define ADDHOST(cp) if (numhosts < MAXHOSTS) chosts[numhosts++] = (cp)
438 * Error codes for internal use
440 #define ERR_UNSPEC 256
441 #define ERR_INCOMPLETE 257
442 #define ERR_TIMEOUT 258
443 #define ERR_TOOMUCH 259
446 * Macro definitions we use
448 #define ISSPACE(c) ((c) == ' ' || (c) == '\t')
449 #define ISEOL(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
450 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
453 * Jump buffer for longjumping back to the command level
455 jmp_buf interrupt_buf;
458 * Points at file being currently printed into
460 FILE *current_output;
463 * Command table imported from ntpdc_ops.c
465 extern struct xcmd opcmds[];
470 #ifdef NO_MAIN_ALLOWED
471 CALL(ntpq,"ntpq",ntpqmain);
473 void clear_globals(void)
475 extern int ntp_optind;
476 showhostnames = 0; /* don'tshow host names by default */
478 server_entry = NULL; /* server entry for ntp */
479 havehost = 0; /* set to 1 when host open */
480 numassoc = 0; /* number of cached associations */
487 * main - parse arguments and handle options
489 #ifndef NO_MAIN_ALLOWED
496 return ntpqmain(argc, argv);
506 extern int ntp_optind;
510 taskPrioritySet(taskIdSelf(), 100 );
514 delay_time.l_uf = DEFDELAY;
517 if (!Win32InitSockets())
519 fprintf(stderr, "No useable winsock.dll:");
522 #endif /* SYS_WINNT */
524 /* Check to see if we have IPv6. Otherwise force the -4 flag */
525 if (isc_net_probeipv6() != ISC_R_SUCCESS) {
526 ai_fam_default = AF_INET;
532 int optct = optionProcess(&ntpqOptions, argc, argv);
537 switch (WHICH_IDX_IPV4) {
539 ai_fam_templ = AF_INET;
542 ai_fam_templ = AF_INET6;
545 ai_fam_templ = ai_fam_default;
549 if (HAVE_OPT(COMMAND)) {
550 int cmdct = STACKCT_OPT( COMMAND );
551 const char** cmds = STACKLST_OPT( COMMAND );
553 while (cmdct-- > 0) {
558 debug = DESC(DEBUG_LEVEL).optOccCt;
560 if (HAVE_OPT(INTERACTIVE)) {
564 if (HAVE_OPT(NUMERIC)) {
568 if (HAVE_OPT(PEERS)) {
573 while ((c = ntp_getopt(argc, argv, "46c:dinp")) != EOF)
576 ai_fam_templ = AF_INET;
579 ai_fam_templ = AF_INET6;
601 (void) fprintf(stderr,
602 "usage: %s [-46dinp] [-c cmd] host ...\n",
607 if (ntp_optind == argc) {
610 for (; ntp_optind < argc; ntp_optind++)
611 ADDHOST(argv[ntp_optind]);
614 if (numcmds == 0 && interactive == 0
615 && isatty(fileno(stdin)) && isatty(fileno(stderr))) {
619 #ifndef SYS_WINNT /* Under NT cannot handle SIGINT, WIN32 spawns a handler */
621 (void) signal_no_reset(SIGINT, abortcmd);
622 #endif /* SYS_WINNT */
625 (void) openhost(chosts[0]);
631 for (ihost = 0; ihost < numhosts; ihost++) {
632 if (openhost(chosts[ihost]))
633 for (icmd = 0; icmd < numcmds; icmd++)
639 #endif /* SYS_WINNT */
645 * openhost - open a socket to a host
652 char temphost[LENHOSTNAME];
654 struct addrinfo hints, *ai = NULL;
655 register const char *cp;
656 char name[LENHOSTNAME];
660 * We need to get by the [] if they were entered
667 for(i = 0; *cp != ']'; cp++, i++)
674 * First try to resolve it as an ip address and if that fails,
675 * do a fullblown (dns) lookup. That way we only use the dns
676 * when it is needed and work around some implementations that
677 * will return an "IPv4-mapped IPv6 address" address if you
678 * give it an IPv4 address to lookup.
680 strcpy(service, "ntp");
681 memset((char *)&hints, 0, sizeof(struct addrinfo));
682 hints.ai_family = ai_fam_templ;
683 hints.ai_protocol = IPPROTO_UDP;
684 hints.ai_socktype = SOCK_DGRAM;
685 hints.ai_flags = AI_NUMERICHOST;
687 a_info = getaddrinfo(hname, service, &hints, &ai);
688 if (a_info == EAI_NONAME
690 || a_info == EAI_NODATA
693 hints.ai_flags = AI_CANONNAME;
695 hints.ai_flags |= AI_ADDRCONFIG;
697 a_info = getaddrinfo(hname, service, &hints, &ai);
699 /* Some older implementations don't like AI_ADDRCONFIG. */
700 if (a_info == EAI_BADFLAGS) {
701 hints.ai_flags = AI_CANONNAME;
702 a_info = getaddrinfo(hname, service, &hints, &ai);
705 (void) fprintf(stderr, "%s\n", gai_strerror(a_info));
709 if (ai->ai_canonname == NULL) {
710 strncpy(temphost, stoa((struct sockaddr_storage *)ai->ai_addr),
712 temphost[LENHOSTNAME-1] = '\0';
715 strncpy(temphost, ai->ai_canonname, LENHOSTNAME);
716 temphost[LENHOSTNAME-1] = '\0';
720 printf("Opening host %s\n", temphost);
724 printf("Closing old host %s\n", currenthost);
725 (void) closesocket(sockfd);
728 (void) strcpy(currenthost, temphost);
730 /* port maps to the same location in both families */
731 s_port = ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port;
733 ((struct sockaddr_in6 *)&hostaddr)->sin6_port = htons(SERVER_PORT_NUM);
734 if (ai->ai_family == AF_INET)
735 *(struct sockaddr_in *)&hostaddr=
736 *((struct sockaddr_in *)ai->ai_addr);
738 *(struct sockaddr_in6 *)&hostaddr=
739 *((struct sockaddr_in6 *)ai->ai_addr);
740 #endif /* SYS_VXWORKS */
744 int optionValue = SO_SYNCHRONOUS_NONALERT;
747 err = setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&optionValue, sizeof(optionValue));
748 if (err != NO_ERROR) {
749 (void) fprintf(stderr, "cannot open nonoverlapped sockets\n");
753 #endif /* SYS_WINNT */
755 sockfd = socket(ai->ai_family, SOCK_DGRAM, 0);
756 if (sockfd == INVALID_SOCKET) {
757 error("socket", "", "");
761 #ifdef NEED_RCVBUF_SLOP
763 { int rbufsize = DATASIZE + 2048; /* 2K for slop */
764 if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF,
765 &rbufsize, sizeof(int)) == -1)
766 error("setsockopt", "", "");
772 if (connect(sockfd, (struct sockaddr *)&hostaddr,
773 sizeof(hostaddr)) == -1)
775 if (connect(sockfd, (struct sockaddr *)ai->ai_addr,
776 ai->ai_addrlen) == -1)
777 #endif /* SYS_VXWORKS */
778 error("connect", "", "");
786 /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
788 * sendpkt - send a packet to the remote host
797 printf("Sending %d octets\n", xdatalen);
800 if (send(sockfd, xdata, (size_t)xdatalen, 0) == -1) {
801 warning("write to %s failed", currenthost, "");
807 printf("Packet data:\n");
808 while (xdatalen-- > 0) {
813 printf(" %02x", *xdata++ & 0xff);
823 * getresponse - get a (series of) response packet(s) and return the data
835 struct ntp_control rpkt;
837 u_short offsets[MAXFRAGS+1];
838 u_short counts[MAXFRAGS+1];
847 * This is pretty tricky. We may get between 1 and MAXFRAG packets
848 * back in response to the request. We peel the data out of
849 * each packet and collect it in one long block. When the last
850 * packet in the sequence is received we'll know how much data we
851 * should have had. Note we use one long time out, should reconsider.
856 *rdata = (char *)pktdata;
869 FD_SET(sockfd, &fds);
870 n = select(sockfd+1, &fds, (fd_set *)0, (fd_set *)0, &tvo);
874 printf("select() returns %d\n", n);
878 warning("select fails", "", "");
883 * Timed out. Return what we have
887 (void) fprintf(stderr,
888 "%s: timed out, nothing received\n",
893 (void) fprintf(stderr,
894 "%s: timed out with incomplete data\n",
897 printf("Received fragments:\n");
898 for (n = 0; n < numfrags; n++)
899 printf("%4d %d\n", offsets[n],
902 printf("last fragment received\n");
904 printf("last fragment not received\n");
906 return ERR_INCOMPLETE;
910 n = recv(sockfd, (char *)&rpkt, sizeof(rpkt), 0);
912 warning("read", "", "");
917 int len = n, first = 8;
918 char *data = (char *)&rpkt;
920 printf("Packet data:\n");
926 printf(" %02x", *data++ & 0xff);
932 * Check for format errors. Bug proofing.
934 if (n < CTL_HEADER_LEN) {
936 printf("Short (%d byte) packet received\n", n);
939 if (PKT_VERSION(rpkt.li_vn_mode) > NTP_VERSION
940 || PKT_VERSION(rpkt.li_vn_mode) < NTP_OLDVERSION) {
942 printf("Packet received with version %d\n",
943 PKT_VERSION(rpkt.li_vn_mode));
946 if (PKT_MODE(rpkt.li_vn_mode) != MODE_CONTROL) {
948 printf("Packet received with mode %d\n",
949 PKT_MODE(rpkt.li_vn_mode));
952 if (!CTL_ISRESPONSE(rpkt.r_m_e_op)) {
954 printf("Received request packet, wanted response\n");
959 * Check opcode and sequence number for a match.
960 * Could be old data getting to us.
962 if (ntohs(rpkt.sequence) != sequence) {
965 "Received sequnce number %d, wanted %d\n",
966 ntohs(rpkt.sequence), sequence);
969 if (CTL_OP(rpkt.r_m_e_op) != opcode) {
972 "Received opcode %d, wanted %d (sequence number okay)\n",
973 CTL_OP(rpkt.r_m_e_op), opcode);
978 * Check the error code. If non-zero, return it.
980 if (CTL_ISERROR(rpkt.r_m_e_op)) {
983 errcode = (ntohs(rpkt.status) >> 8) & 0xff;
984 if (debug && CTL_ISMORE(rpkt.r_m_e_op)) {
985 printf("Error code %d received on not-final packet\n",
988 if (errcode == CERR_UNSPEC)
994 * Check the association ID to make sure it matches what
997 if (ntohs(rpkt.associd) != associd) {
999 printf("Association ID %d doesn't match expected %d\n",
1000 ntohs(rpkt.associd), associd);
1002 * Hack for silly fuzzballs which, at the time of writing,
1003 * return an assID of sys.peer when queried for system variables.
1011 * Collect offset and count. Make sure they make sense.
1013 offset = ntohs(rpkt.offset);
1014 count = ntohs(rpkt.count);
1023 * Usually we ignore authentication, but for debugging purposes
1026 shouldbesize = CTL_HEADER_LEN + count;
1028 /* round to 8 octet boundary */
1029 shouldbesize = (shouldbesize + 7) & ~7;
1032 printf("Packet not padded, size = %d\n", n);
1033 } if ((maclen = n - shouldbesize) >= MIN_MAC_LEN) {
1035 "Packet shows signs of authentication (total %d, data %d, mac %d)\n",
1036 n, shouldbesize, maclen);
1037 lpkt = (u_long *)&rpkt;
1038 printf("%08lx %08lx %08lx %08lx %08lx %08lx\n",
1039 (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_long) - 3]),
1040 (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_long) - 2]),
1041 (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_long) - 1]),
1042 (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_long)]),
1043 (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_long) + 1]),
1044 (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_long) + 2]));
1045 key = ntohl(lpkt[(n - maclen) / sizeof(u_long)]);
1046 printf("Authenticated with keyid %lu\n", (u_long)key);
1047 if (key != 0 && key != info_auth_keyid) {
1048 printf("We don't know that key\n");
1050 if (authdecrypt(key, (u_int32 *)&rpkt,
1051 n - maclen, maclen)) {
1052 printf("Auth okay!\n");
1054 printf("Auth failed!\n");
1061 printf("Got packet, size = %d\n", n);
1062 if (count > (u_short)(n-CTL_HEADER_LEN)) {
1065 "Received count of %d octets, data in packet is %d\n",
1066 count, n-CTL_HEADER_LEN);
1069 if (count == 0 && CTL_ISMORE(rpkt.r_m_e_op)) {
1071 printf("Received count of 0 in non-final fragment\n");
1074 if (offset + count > sizeof(pktdata)) {
1076 printf("Offset %d, count %d, too big for buffer\n",
1080 if (seenlastfrag && !CTL_ISMORE(rpkt.r_m_e_op)) {
1082 printf("Received second last fragment packet\n");
1087 * So far, so good. Record this fragment, making sure it doesn't
1091 printf("Packet okay\n");;
1093 if (numfrags == MAXFRAGS) {
1095 printf("Number of fragments exceeds maximum\n");
1099 for (n = 0; n < numfrags; n++) {
1100 if (offset == offsets[n])
1101 goto again; /* duplicate */
1102 if (offset < offsets[n])
1106 if ((u_short)(n > 0 && offsets[n-1] + counts[n-1]) > offset)
1108 if (n < numfrags && (u_short)(offset + count) > offsets[n])
1114 for (i = numfrags; i > n; i--) {
1115 offsets[i] = offsets[i-1];
1116 counts[i] = counts[i-1];
1119 offsets[n] = offset;
1124 * Got that stuffed in right. Figure out if this was the last.
1125 * Record status info out of the last packet.
1127 if (!CTL_ISMORE(rpkt.r_m_e_op)) {
1130 *rstatus = ntohs(rpkt.status);
1134 * Copy the data into the data buffer.
1136 memmove((char *)pktdata + offset, (char *)rpkt.data, count);
1139 * If we've seen the last fragment, look for holes in the sequence.
1140 * If there aren't any, we're done.
1142 if (seenlastfrag && offsets[0] == 0) {
1143 for (n = 1; n < numfrags; n++) {
1144 if (offsets[n-1] + counts[n-1] != offsets[n])
1147 if (n == numfrags) {
1148 *rsize = offsets[numfrags-1] + counts[numfrags-1];
1156 * Print debugging message about overlapping fragments
1159 printf("Overlapping fragments returned in response\n");
1165 * sendrequest - format and send a request packet
1176 struct ntp_control qpkt;
1180 * Check to make sure the data will fit in one packet
1182 if (qsize > CTL_MAX_DATA_LEN) {
1183 (void) fprintf(stderr,
1184 "***Internal error! qsize (%d) too large\n",
1190 * Fill in the packet
1192 qpkt.li_vn_mode = PKT_LI_VN_MODE(0, pktversion, MODE_CONTROL);
1193 qpkt.r_m_e_op = (u_char)(opcode & CTL_OP_MASK);
1194 qpkt.sequence = htons(sequence);
1196 qpkt.associd = htons((u_short)associd);
1198 qpkt.count = htons((u_short)qsize);
1201 * If we have data, copy it in and pad it out to a 64
1205 memmove((char *)qpkt.data, qdata, (unsigned)qsize);
1206 pktsize = qsize + CTL_HEADER_LEN;
1207 while (pktsize & (sizeof(u_long) - 1)) {
1208 qpkt.data[qsize++] = 0;
1212 pktsize = CTL_HEADER_LEN;
1216 * If it isn't authenticated we can just send it. Otherwise
1217 * we're going to have to think about it a little.
1219 if (!auth && !always_auth) {
1220 return sendpkt((char *)&qpkt, pktsize);
1222 const char *pass = "\0";
1227 * Pad out packet to a multiple of 8 octets to be sure
1228 * receiver can handle it.
1230 while (pktsize & 7) {
1231 qpkt.data[qsize++] = 0;
1236 * Get the keyid and the password if we don't have one.
1238 if (info_auth_keyid == 0) {
1239 int u_keyid = getkeyid("Keyid: ");
1240 if (u_keyid == 0 || u_keyid > NTP_MAXKEY) {
1241 (void) fprintf(stderr,
1242 "Invalid key identifier\n");
1245 info_auth_keyid = u_keyid;
1247 if (!authistrusted(info_auth_keyid)) {
1248 pass = getpass("MD5 Password: ");
1249 if (*pass == '\0') {
1250 (void) fprintf(stderr,
1251 "Invalid password\n");
1255 authusekey(info_auth_keyid, info_auth_keytype, (const u_char *)pass);
1256 authtrust(info_auth_keyid, 1);
1259 * Stick the keyid in the packet where
1260 * cp currently points. Cp should be aligned
1261 * properly. Then do the encryptions.
1263 my_keyid = htonl(info_auth_keyid);
1264 memcpy(&qpkt.data[qsize], &my_keyid, sizeof my_keyid);
1265 maclen = authencrypt(info_auth_keyid, (u_int32 *)&qpkt,
1268 (void) fprintf(stderr, "Key not found\n");
1271 return sendpkt((char *)&qpkt, pktsize + maclen);
1278 * doquery - send a request and process the response
1296 * Check to make sure host is open
1299 (void) fprintf(stderr, "***No host open, use `host' command\n");
1310 res = sendrequest(opcode, associd, auth, qsize, qdata);
1315 * Get the response. If we got a standard error, print a message
1317 res = getresponse(opcode, associd, rstatus, rsize, rdata, done);
1320 if (!done && (res == ERR_TIMEOUT || res == ERR_INCOMPLETE)) {
1321 if (res == ERR_INCOMPLETE) {
1323 * better bump the sequence so we don't
1324 * get confused about differing fragments.
1332 (void) fprintf(stderr, "server=%s ", currenthost);
1335 (void) fprintf(stderr,
1336 "***Server reports a bad format request packet\n");
1338 case CERR_PERMISSION:
1339 (void) fprintf(stderr,
1340 "***Server disallowed request (authentication?)\n");
1343 (void) fprintf(stderr,
1344 "***Server reports a bad opcode in request\n");
1347 (void) fprintf(stderr,
1348 "***Association ID %d unknown to server\n",associd);
1350 case CERR_UNKNOWNVAR:
1351 (void) fprintf(stderr,
1352 "***A request variable unknown to the server\n");
1355 (void) fprintf(stderr,
1356 "***Server indicates a request variable was bad\n");
1359 (void) fprintf(stderr,
1360 "***Server returned an unspecified error\n");
1363 (void) fprintf(stderr, "***Request timed out\n");
1365 case ERR_INCOMPLETE:
1366 (void) fprintf(stderr,
1367 "***Response from server was incomplete\n");
1370 (void) fprintf(stderr,
1371 "***Buffer size exceeded for returned data\n");
1374 (void) fprintf(stderr,
1375 "***Server returns unknown error code %d\n", res);
1384 * getcmds - read commands from the standard input and execute them
1389 #if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDIT)
1393 if ((line = readline(interactive?prompt:"")) == NULL) return;
1394 if (*line) add_history(line);
1398 #else /* not (HAVE_LIBREADLINE || HAVE_LIBEDIT) */
1403 #ifdef VMS /* work around a problem with mixing stdout & stderr */
1406 (void) fputs(prompt, stderr);
1407 (void) fflush(stderr);
1410 if (fgets(line, sizeof line, stdin) == NULL)
1415 #endif /* not (HAVE_LIBREADLINE || HAVE_LIBEDIT) */
1418 #ifndef SYS_WINNT /* Under NT cannot handle SIGINT, WIN32 spawns a handler */
1420 * abortcmd - catch interrupts and abort the current command
1427 if (current_output == stdout)
1428 (void) fflush(stdout);
1430 (void) fflush(stderr);
1431 if (jump) longjmp(interrupt_buf, 1);
1433 #endif /* SYS_WINNT */
1436 * docmd - decode the command line and execute a command
1443 char *tokens[1+MAXARGS+2];
1450 * Tokenize the command line. If nothing on it, return.
1452 tokenize(cmdline, tokens, &ntok);
1457 * Find the appropriate command description.
1459 i = findcmd(tokens[0], builtins, opcmds, &xcmd);
1461 (void) fprintf(stderr, "***Command `%s' unknown\n",
1464 } else if (i >= 2) {
1465 (void) fprintf(stderr, "***Command `%s' ambiguous\n",
1471 * Save the keyword, then walk through the arguments, interpreting
1474 pcmd.keyword = tokens[0];
1476 for (i = 0; i < MAXARGS && xcmd->arg[i] != NO; i++) {
1477 if ((i+1) >= ntok) {
1478 if (!(xcmd->arg[i] & OPT)) {
1479 printusage(xcmd, stderr);
1484 if ((xcmd->arg[i] & OPT) && (*tokens[i+1] == '>'))
1486 if (!getarg(tokens[i+1], (int)xcmd->arg[i], &pcmd.argval[i]))
1492 if (i < ntok && *tokens[i] == '>') {
1495 if (*(tokens[i]+1) != '\0')
1496 fname = tokens[i]+1;
1497 else if ((i+1) < ntok)
1498 fname = tokens[i+1];
1500 (void) fprintf(stderr, "***No file for redirect\n");
1504 current_output = fopen(fname, "w");
1505 if (current_output == NULL) {
1506 (void) fprintf(stderr, "***Error opening %s: ", fname);
1510 i = 1; /* flag we need a close */
1512 current_output = stdout;
1513 i = 0; /* flag no close */
1516 if (interactive && setjmp(interrupt_buf)) {
1521 (xcmd->handler)(&pcmd, current_output);
1522 jump = 0; /* HMS: 961106: was after fclose() */
1523 if (i) (void) fclose(current_output);
1529 * tokenize - turn a command line into tokens
1538 register const char *cp;
1540 static char tspace[MAXLINE];
1544 for (*ntok = 0; *ntok < MAXTOKENS; (*ntok)++) {
1546 while (ISSPACE(*cp))
1552 } while (!ISSPACE(*cp) && !ISEOL(*cp));
1561 * findcmd - find a command in a command description table
1566 struct xcmd *clist1,
1567 struct xcmd *clist2,
1571 register struct xcmd *cl;
1574 struct xcmd *nearmatch = NULL;
1581 else if (clist2 != 0)
1587 for (cl = clist; cl->keyword != 0; cl++) {
1588 /* do a first character check, for efficiency */
1589 if (*str != *(cl->keyword))
1591 if (strncmp(str, cl->keyword, (unsigned)clen) == 0) {
1593 * Could be extact match, could be approximate.
1594 * Is exact if the length of the keyword is the
1597 if (*((cl->keyword) + clen) == '\0') {
1607 * See if there is more to do. If so, go again. Sorry about the
1608 * goto, too much looking at BSD sources...
1610 if (clist == clist1 && clist2 != 0) {
1616 * If we got extactly 1 near match, use it, else return number
1628 * getarg - interpret an argument token
1639 static const char *digits = "0123456789";
1641 switch (code & ~OPT) {
1646 if (!getnetnum(str, &(argp->netnum), (char *)0, 0)) {
1658 (void) fprintf(stderr,
1659 "***Association value `%s' invalid/undecodable\n", str);
1662 if (isneg > numassoc) {
1663 if (numassoc == 0) {
1664 (void) fprintf(stderr,
1665 "***Association for `%s' unknown (max &%d)\n",
1672 argp->uval = assoc_cache[isneg-1].assid;
1683 cp = strchr(digits, *np);
1685 (void) fprintf(stderr,
1686 "***Illegal integer value %s\n", str);
1690 argp->uval += (cp - digits);
1691 } while (*(++np) != '\0');
1694 if ((code & ~OPT) == NTP_UINT) {
1695 (void) fprintf(stderr,
1696 "***Value %s should be unsigned\n", str);
1699 argp->ival = -argp->ival;
1703 if (!strcmp("-6", str))
1705 else if (!strcmp("-4", str))
1708 (void) fprintf(stderr,
1709 "***Version must be either 4 or 6\n");
1720 * getnetnum - given a host name, return its net number
1721 * and (optional) full name
1726 struct sockaddr_storage *num,
1732 struct addrinfo hints, *ai = NULL;
1734 sockaddr_len = (af == AF_INET)
1735 ? sizeof(struct sockaddr_in)
1736 : sizeof(struct sockaddr_in6);
1737 memset((char *)&hints, 0, sizeof(struct addrinfo));
1738 hints.ai_flags = AI_CANONNAME;
1739 #ifdef AI_ADDRCONFIG
1740 hints.ai_flags |= AI_ADDRCONFIG;
1743 /* decodenetnum works with addresses only */
1744 if (decodenetnum(hname, num)) {
1745 if (fullhost != 0) {
1746 getnameinfo((struct sockaddr *)num, sockaddr_len,
1747 fullhost, sizeof(fullhost), NULL, 0,
1751 } else if (getaddrinfo(hname, "ntp", &hints, &ai) == 0) {
1752 memmove((char *)num, ai->ai_addr, ai->ai_addrlen);
1753 if (ai->ai_canonname != 0)
1754 (void) strcpy(fullhost, ai->ai_canonname);
1757 (void) fprintf(stderr, "***Can't find host %s\n", hname);
1764 * nntohost - convert network number to host name. This routine enforces
1765 * the showhostnames setting.
1769 struct sockaddr_storage *netnum
1773 return stoa(netnum);
1774 if ((netnum->ss_family == AF_INET) && ISREFCLOCKADR(netnum))
1775 return refnumtoa(netnum);
1776 return socktohost(netnum);
1781 * rtdatetolfp - decode an RT-11 date into an l_fp
1791 struct calendar cal;
1793 static const char *months[12] = {
1794 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1795 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1801 * An RT-11 date looks like:
1803 * d[d]-Mth-y[y] hh:mm:ss
1805 * (No docs, but assume 4-digit years are also legal...)
1807 * d[d]-Mth-y[y[y[y]]] hh:mm:ss
1810 if (!isdigit((int)*cp)) {
1813 * Catch special case
1821 cal.monthday = (u_char) (*cp++ - '0'); /* ascii dependent */
1822 if (isdigit((int)*cp)) {
1823 cal.monthday = (u_char)((cal.monthday << 3) + (cal.monthday << 1));
1824 cal.monthday = (u_char)(cal.monthday + *cp++ - '0');
1830 for (i = 0; i < 3; i++)
1834 for (i = 0; i < 12; i++)
1835 if (STREQ(buf, months[i]))
1839 cal.month = (u_char)(i + 1);
1844 if (!isdigit((int)*cp))
1846 cal.year = (u_short)(*cp++ - '0');
1847 if (isdigit((int)*cp)) {
1848 cal.year = (u_short)((cal.year << 3) + (cal.year << 1));
1849 cal.year = (u_short)(*cp++ - '0');
1851 if (isdigit((int)*cp)) {
1852 cal.year = (u_short)((cal.year << 3) + (cal.year << 1));
1853 cal.year = (u_short)(cal.year + *cp++ - '0');
1855 if (isdigit((int)*cp)) {
1856 cal.year = (u_short)((cal.year << 3) + (cal.year << 1));
1857 cal.year = (u_short)(cal.year + *cp++ - '0');
1861 * Catch special case. If cal.year == 0 this is a zero timestamp.
1863 if (cal.year == 0) {
1868 if (*cp++ != ' ' || !isdigit((int)*cp))
1870 cal.hour = (u_char)(*cp++ - '0');
1871 if (isdigit((int)*cp)) {
1872 cal.hour = (u_char)((cal.hour << 3) + (cal.hour << 1));
1873 cal.hour = (u_char)(cal.hour + *cp++ - '0');
1876 if (*cp++ != ':' || !isdigit((int)*cp))
1878 cal.minute = (u_char)(*cp++ - '0');
1879 if (isdigit((int)*cp)) {
1880 cal.minute = (u_char)((cal.minute << 3) + (cal.minute << 1));
1881 cal.minute = (u_char)(cal.minute + *cp++ - '0');
1884 if (*cp++ != ':' || !isdigit((int)*cp))
1886 cal.second = (u_char)(*cp++ - '0');
1887 if (isdigit((int)*cp)) {
1888 cal.second = (u_char)((cal.second << 3) + (cal.second << 1));
1889 cal.second = (u_char)(cal.second + *cp++ - '0');
1893 * For RT-11, 1972 seems to be the pivot year
1900 lfp->l_ui = caltontp(&cal);
1907 * decodets - decode a timestamp into an l_fp format number, with
1908 * consideration of fuzzball formats.
1917 * If it starts with a 0x, decode as hex.
1919 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
1920 return hextolfp(str+2, lfp);
1923 * If it starts with a '"', try it as an RT-11 date.
1926 register char *cp = str+1;
1931 while (*cp != '"' && *cp != '\0' && bp < &buf[29])
1934 return rtdatetolfp(buf, lfp);
1938 * Might still be hex. Check out the first character. Talk
1941 if ((*str >= 'A' && *str <= 'F') || (*str >= 'a' && *str <= 'f'))
1942 return hextolfp(str, lfp);
1945 * Try it as a decimal. If this fails, try as an unquoted
1946 * RT-11 date. This code should go away eventually.
1948 if (atolfp(str, lfp))
1950 return rtdatetolfp(str, lfp);
1955 * decodetime - decode a time value. It should be in milliseconds
1963 return mstolfp(str, lfp);
1968 * decodeint - decode an integer
1977 if (*(str+1) == 'x' || *(str+1) == 'X')
1978 return hextoint(str+2, val);
1979 return octtoint(str, val);
1981 return atoint(str, val);
1986 * decodeuint - decode an unsigned integer
1995 if (*(str + 1) == 'x' || *(str + 1) == 'X')
1996 return (hextoint(str + 2, val));
1997 return (octtoint(str, val));
1999 return (atouint(str, val));
2004 * decodearr - decode an array of time values
2013 register char *cp, *bp;
2022 while (isspace((int)*cp))
2028 while (!isspace((int)*cp) && *cp != '\0')
2032 if (!decodetime(buf, lfp))
2042 * Finally, the built in command handlers
2046 * help - tell about commands, or details of a particular command
2056 const char *list[100];
2061 if (pcmd->nargs == 0) {
2063 for (xcp = builtins; xcp->keyword != 0; xcp++) {
2064 if (*(xcp->keyword) != '?')
2065 list[words++] = xcp->keyword;
2067 for (xcp = opcmds; xcp->keyword != 0; xcp++)
2068 list[words++] = xcp->keyword;
2071 #ifdef QSORT_USES_VOID_P
2076 (list), (size_t)(words), sizeof(char *), helpsort);
2078 for (word = 0; word < words; word++) {
2079 int length = strlen(list[word]);
2085 cols = SCREENWIDTH / ++col;
2086 rows = (words + cols - 1) / cols;
2088 (void) fprintf(fp, "ntpq commands:\n");
2090 for (row = 0; row < rows; row++) {
2091 for (word = row; word < words; word += rows) {
2092 (void) fprintf(fp, "%-*.*s", col, col-1, list[word]);
2094 (void) fprintf(fp, "\n");
2097 cmd = pcmd->argval[0].string;
2098 words = findcmd(cmd, builtins, opcmds, &xcp);
2100 (void) fprintf(stderr,
2101 "Command `%s' is unknown\n", cmd);
2103 } else if (words >= 2) {
2104 (void) fprintf(stderr,
2105 "Command `%s' is ambiguous\n", cmd);
2108 (void) fprintf(fp, "function: %s\n", xcp->comment);
2109 printusage(xcp, fp);
2115 * helpsort - do hostname qsort comparisons
2117 #ifdef QSORT_USES_VOID_P
2124 char const * const * name1 = (char const * const *)t1;
2125 char const * const * name2 = (char const * const *)t2;
2127 return strcmp(*name1, *name2);
2137 return strcmp(*name1, *name2);
2142 * printusage - print usage information for a command
2152 (void) fprintf(fp, "usage: %s", xcp->keyword);
2153 for (i = 0; i < MAXARGS && xcp->arg[i] != NO; i++) {
2154 if (xcp->arg[i] & OPT)
2155 (void) fprintf(fp, " [ %s ]", xcp->desc[i]);
2157 (void) fprintf(fp, " %s", xcp->desc[i]);
2159 (void) fprintf(fp, "\n");
2164 * timeout - set time out time
2174 if (pcmd->nargs == 0) {
2175 val = tvout.tv_sec * 1000 + tvout.tv_usec / 1000;
2176 (void) fprintf(fp, "primary timeout %d ms\n", val);
2178 tvout.tv_sec = pcmd->argval[0].uval / 1000;
2179 tvout.tv_usec = (pcmd->argval[0].uval - (tvout.tv_sec * 1000))
2186 * auth_delay - set delay for auth requests
2197 if (pcmd->nargs == 0) {
2198 val = delay_time.l_ui * 1000 + delay_time.l_uf / 4294967;
2199 (void) fprintf(fp, "delay %lu ms\n", val);
2201 if (pcmd->argval[0].ival < 0) {
2203 val = (u_long)(-pcmd->argval[0].ival);
2206 val = (u_long)pcmd->argval[0].ival;
2209 delay_time.l_ui = val / 1000;
2211 delay_time.l_uf = val * 4294967; /* 2**32/1000 */
2220 * host - set the host we are dealing with.
2230 if (pcmd->nargs == 0) {
2232 (void) fprintf(fp, "current host is %s\n", currenthost);
2234 (void) fprintf(fp, "no current host\n");
2239 ai_fam_templ = ai_fam_default;
2240 if (pcmd->nargs == 2) {
2241 if (!strcmp("-4", pcmd->argval[i].string))
2242 ai_fam_templ = AF_INET;
2243 else if (!strcmp("-6", pcmd->argval[i].string))
2244 ai_fam_templ = AF_INET6;
2248 "current host remains %s\n", currenthost);
2250 (void) fprintf(fp, "still no current host\n");
2255 if (openhost(pcmd->argval[i].string)) {
2256 (void) fprintf(fp, "current host set to %s\n", currenthost);
2261 "current host remains %s\n", currenthost);
2263 (void) fprintf(fp, "still no current host\n");
2269 * poll - do one (or more) polls of the host via NTP
2278 (void) fprintf(fp, "poll not implemented yet\n");
2283 * keyid - get a keyid to use for authenticating requests
2291 if (pcmd->nargs == 0) {
2292 if (info_auth_keyid == 0)
2293 (void) fprintf(fp, "no keyid defined\n");
2295 (void) fprintf(fp, "keyid is %lu\n", (u_long)info_auth_keyid);
2297 /* allow zero so that keyid can be cleared. */
2298 if(pcmd->argval[0].uval > NTP_MAXKEY)
2299 (void) fprintf(fp, "Invalid key identifier\n");
2300 info_auth_keyid = pcmd->argval[0].uval;
2305 * keytype - get type of key to use for authenticating requests
2313 if (pcmd->nargs == 0)
2314 fprintf(fp, "keytype is %s\n",
2315 (info_auth_keytype == KEY_TYPE_MD5) ? "MD5" : "???");
2317 switch (*(pcmd->argval[0].string)) {
2320 info_auth_keytype = KEY_TYPE_MD5;
2324 fprintf(fp, "keytype must be 'md5'\n");
2331 * passwd - get an authentication key
2342 if (info_auth_keyid == 0) {
2343 int u_keyid = getkeyid("Keyid: ");
2344 if (u_keyid == 0 || u_keyid > NTP_MAXKEY) {
2345 (void)fprintf(fp, "Invalid key identifier\n");
2348 info_auth_keyid = u_keyid;
2350 pass = getpass("MD5 Password: ");
2352 (void) fprintf(fp, "Password unchanged\n");
2354 authusekey(info_auth_keyid, info_auth_keytype, (u_char *)pass);
2355 authtrust(info_auth_keyid, 1);
2361 * hostnames - set the showhostnames flag
2369 if (pcmd->nargs == 0) {
2371 (void) fprintf(fp, "hostnames being shown\n");
2373 (void) fprintf(fp, "hostnames not being shown\n");
2375 if (STREQ(pcmd->argval[0].string, "yes"))
2377 else if (STREQ(pcmd->argval[0].string, "no"))
2380 (void)fprintf(stderr, "What?\n");
2387 * setdebug - set/change debugging level
2395 if (pcmd->nargs == 0) {
2396 (void) fprintf(fp, "debug level is %d\n", debug);
2398 } else if (STREQ(pcmd->argval[0].string, "no")) {
2400 } else if (STREQ(pcmd->argval[0].string, "more")) {
2402 } else if (STREQ(pcmd->argval[0].string, "less")) {
2405 (void) fprintf(fp, "What?\n");
2408 (void) fprintf(fp, "debug level set to %d\n", debug);
2413 * quit - stop this nonsense
2423 closesocket(sockfd); /* cleanliness next to godliness */
2429 * version - print the current version number
2439 (void) fprintf(fp, "%s\n", Version);
2445 * raw - set raw mode output
2455 (void) fprintf(fp, "Output set to raw\n");
2460 * cooked - set cooked mode output
2470 (void) fprintf(fp, "Output set to cooked\n");
2476 * authenticate - always authenticate requests to this host
2484 if (pcmd->nargs == 0) {
2487 "authenticated requests being sent\n");
2490 "unauthenticated requests being sent\n");
2492 if (STREQ(pcmd->argval[0].string, "yes")) {
2494 } else if (STREQ(pcmd->argval[0].string, "no")) {
2497 (void)fprintf(stderr, "What?\n");
2503 * ntpversion - choose the NTP version to use
2511 if (pcmd->nargs == 0) {
2513 "NTP version being claimed is %d\n", pktversion);
2515 if (pcmd->argval[0].uval < NTP_OLDVERSION
2516 || pcmd->argval[0].uval > NTP_VERSION) {
2517 (void) fprintf(stderr, "versions %d to %d, please\n",
2518 NTP_OLDVERSION, NTP_VERSION);
2520 pktversion = (u_char) pcmd->argval[0].uval;
2527 * warning - print a warning message
2536 (void) fprintf(stderr, "%s: ", progname);
2537 (void) fprintf(stderr, fmt, st1, st2);
2538 (void) fprintf(stderr, ": ");
2544 * error - print a message and exit
2553 warning(fmt, st1, st2);
2558 * getkeyid - prompt the user for a keyid to use
2562 const char *keyprompt
2571 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
2573 if ((fi = _fdopen((int)GetStdHandle(STD_INPUT_HANDLE), "r")) == NULL)
2574 #endif /* SYS_WINNT */
2577 setbuf(fi, (char *)NULL);
2578 fprintf(stderr, "%s", keyprompt); fflush(stderr);
2579 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
2586 if (strcmp(pbuf, "0") == 0)
2589 return (u_long) atoi(pbuf);
2594 * atoascii - printable-ize possibly ascii data using the character
2595 * transformations cat -v uses.
2604 register u_char *cp;
2605 register u_char *ocp;
2614 ocp = (u_char *)outdata;
2615 for (cp = (u_char *)data; cp < (u_char *)data + length; cp++) {
2629 *ocp++ = (u_char)(c + '@');
2630 } else if (c == 0177) {
2636 if (ocp >= ((u_char *)outdata + length - 4))
2645 * makeascii - print possibly ascii data using the character
2646 * transformations that cat -v uses.
2655 register u_char *cp;
2658 for (cp = (u_char *)data; cp < (u_char *)data + length; cp++) {
2669 } else if (c == 0177) {
2680 * asciize - same thing as makeascii except add a newline
2689 makeascii(length, data, fp);
2695 * Some circular buffer space
2700 char circ_buf[NUMCB][CBLEN];
2704 * nextvar - find the next variable in the buffer
2716 register char *cpend;
2717 register char *npend; /* character after last */
2719 static char name[MAXVARLEN];
2720 static char value[MAXVALLEN];
2723 cpend = cp + *datalen;
2726 * Space past commas and white space
2728 while (cp < cpend && (*cp == ',' || isspace((int)*cp)))
2734 * Copy name until we hit a ',', an '=', a '\r' or a '\n'. Backspace
2735 * over any white space and terminate it.
2738 npend = &name[MAXVARLEN];
2739 while (cp < cpend && np < npend && *cp != ',' && *cp != '='
2740 && *cp != '\r' && *cp != '\n')
2743 * Check if we ran out of name space, without reaching the end or a
2744 * terminating character
2746 if (np == npend && !(cp == cpend || *cp == ',' || *cp == '=' ||
2747 *cp == '\r' || *cp == '\n'))
2749 while (isspace((int)(*(np-1))))
2755 * Check if we hit the end of the buffer or a ','. If so we are done.
2757 if (cp == cpend || *cp == ',' || *cp == '\r' || *cp == '\n') {
2761 *datalen = cpend - cp;
2762 *vvalue = (char *)0;
2767 * So far, so good. Copy out the value
2769 cp++; /* past '=' */
2770 while (cp < cpend && (isspace((int)*cp) && *cp != '\r' && *cp != '\n'))
2773 npend = &value[MAXVALLEN];
2774 while (cp < cpend && np < npend && ((*cp != ',') || quoted))
2776 quoted ^= ((*np++ = *cp++) == '"');
2780 * Check if we overran the value buffer while still in a quoted string
2781 * or without finding a comma
2783 if (np == npend && (quoted || *cp != ','))
2786 * Trim off any trailing whitespace
2788 while (np > value && isspace((int)(*(np-1))))
2793 * Return this. All done.
2798 *datalen = cpend - cp;
2805 * findvar - see if this variable is known to us.
2806 * If "code" is 1, return ctl_var->code.
2807 * Otherwise return the ordinal position of the found variable.
2812 struct ctl_var *varlist,
2817 register struct ctl_var *vl;
2821 while (vl->fmt != EOV) {
2822 if (vl->fmt != PADDING && STREQ(np, vl->text))
2835 * printvars - print variables returned in response packet
2847 rawprint(sttype, length, data, status, fp);
2849 cookedprint(sttype, length, data, status, fp);
2854 * rawprint - do a printout of the data in raw mode
2866 register char *cpend;
2869 * Essentially print the data as is. We reformat unprintables, though.
2872 cpend = data + length;
2874 (void) fprintf(fp, "status=0x%04x,\n", status);
2876 while (cp < cpend) {
2879 * If this is a \r and the next character is a
2880 * \n, supress this, else pretty print it. Otherwise
2881 * just output the character.
2883 if (cp == (cpend-1) || *(cp+1) != '\n')
2884 makeascii(1, cp, fp);
2885 } else if (isspace((int)*cp) || isprint((int)*cp)) {
2888 makeascii(1, cp, fp);
2896 * Global data used by the cooked output routines
2898 int out_chars; /* number of characters output */
2899 int out_linecount; /* number of characters output on this line */
2903 * startoutput - get ready to do cooked output
2914 * output - output a variable=value combination
2926 lenname = strlen(name);
2927 lenvalue = strlen(value);
2929 if (out_chars != 0) {
2933 if ((out_linecount + lenname + lenvalue + 3) > MAXOUTLINE) {
2947 out_chars += lenname + 1 + lenvalue;
2948 out_linecount += lenname + 1 + lenvalue;
2953 * endoutput - terminate a block of cooked output
2966 * outputarr - output an array of values
2984 * Hack to align delay and offset values
2986 for (i = (int)strlen(name); i < 11; i++)
2989 for (i = narr; i > 0; i--) {
2992 cp = lfptoms(lfp, 2);
3007 output(fp, name, buf);
3015 register char *cb, *s;
3017 register const char *sep;
3021 s = cb = &circ_buf[nextcb][0];
3022 if (++nextcb >= NUMCB)
3025 sprintf(cb, "%02lx", val);
3032 for (i = 0; i < 13; i++) {
3034 sprintf(cb, "%s%s", sep, tstflagnames[i]);
3046 * cookedprint - output variables in cooked mode
3062 struct ctl_var *varlist;
3065 struct sockaddr_storage hval;
3078 varlist = clock_var;
3081 (void) fprintf(stderr, "Unknown datatype(0x%x) in cookedprint\n", datatype);
3085 (void) fprintf(fp, "status=%04x %s,\n", status,
3086 statustoa(datatype, status));
3089 while (nextvar(&length, &data, &name, &value)) {
3090 varid = findvar(name, varlist, 0);
3095 fmt = varlist[varid].fmt;
3098 if (!decodets(value, &lfp))
3101 output(fp, name, prettydate(&lfp));
3106 if (!decodetime(value, &lfp))
3127 if (!decodeuint(value, &uval))
3130 output(fp, name, uinttoa(uval));
3134 if (!decodeint(value, &ival))
3137 output(fp, name, inttoa(ival));
3142 if (!decodenetnum(value, &hval))
3144 else if (fmt == HA){
3145 output(fp, name, nntohost(&hval));
3147 output(fp, name, stoa(&hval));
3156 if (decodenetnum(value, &hval)) {
3157 if ((hval.ss_family == AF_INET) &&
3158 ISREFCLOCKADR(&hval))
3162 output(fp, name, stoa(&hval));
3163 } else if ((int)strlen(value) <= 4)
3164 output(fp, name, value);
3170 if (!decodeuint(value, &uval) || uval > 3)
3180 output(fp, name, b);
3185 if (!decodeuint(value, &uval))
3190 (void) snprintf(b, sizeof b, "%03lo", uval);
3191 output(fp, name, b);
3196 if (!decodeuint(value, &uval))
3199 output(fp, name, uinttoa(uval));
3203 if (!decodearr(value, &narr, lfparr))
3206 outputarr(fp, name, narr, lfparr);
3210 if (!decodeuint(value, &uval))
3213 output(fp, name, tstflags(uval));
3217 (void) fprintf(stderr,
3218 "Internal error in cookedprint, %s=%s, fmt %d\n",
3224 if (output_raw != 0) {
3229 atoascii(400, name, bn);
3230 atoascii(400, value, bv);
3231 if (output_raw != '*') {
3233 bv[len] = output_raw;
3244 * sortassoc - sort associations in the cache into ascending order
3251 #ifdef QSORT_USES_VOID_P
3256 assoc_cache, (size_t)numassoc,
3257 sizeof(struct association), assoccmp);
3262 * assoccmp - compare two associations
3264 #ifdef QSORT_USES_VOID_P
3271 const struct association *ass1 = (const struct association *)t1;
3272 const struct association *ass2 = (const struct association *)t2;
3274 if (ass1->assid < ass2->assid)
3276 if (ass1->assid > ass2->assid)
3283 struct association *ass1,
3284 struct association *ass2
3287 if (ass1->assid < ass2->assid)
3289 if (ass1->assid > ass2->assid)
3293 #endif /* not QSORT_USES_VOID_P */