xref: /freebsd/contrib/ntp/ntpq/ntpq.h (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*
2  * ntpq.h - definitions of interest to ntpq
3  */
4 #ifdef HAVE_UNISTD_H
5 # include <unistd.h>
6 #endif
7 #include "ntp_fp.h"
8 #include "ntp.h"
9 #include "ntp_stdlib.h"
10 #include "ntp_string.h"
11 #include "ntp_malloc.h"
12 #include "ntp_assert.h"
13 #include "ntp_control.h"
14 #include "lib_strbuf.h"
15 
16 #include "ntpq-opts.h"
17 
18 /*
19  * Maximum number of arguments
20  */
21 #define	MAXARGS	4
22 
23 /*
24  * Limit on packets in a single response.  Increasing this value to
25  * 96 will marginally speed "mrulist" operation on lossless networks
26  * but it has been observed to cause loss on WiFi networks and with
27  * an IPv6 go6.net tunnel over UDP.  That loss causes the request
28  * row limit to be cut in half, and it grows back very slowly to
29  * ensure forward progress is made and loss isn't triggered too quickly
30  * afterward.  While the lossless case gains only marginally with
31  * MAXFRAGS == 96, the lossy case is a lot slower due to the repeated
32  * timeouts.  Empirally, MAXFRAGS == 32 avoids most of the routine loss
33  * on both the WiFi and UDP v6 tunnel tests and seems a good compromise.
34  * This suggests some device in the path has a limit of 32 ~512 byte UDP
35  * packets in queue.
36  * Lowering MAXFRAGS may help with particularly lossy networks, but some
37  * ntpq commands may rely on the longtime value of 24 implicitly,
38  * assuming a single multipacket response will be large enough for any
39  * needs.  In contrast, the "mrulist" command is implemented as a series
40  * of requests and multipacket responses to each.
41  */
42 #define	MAXFRAGS	32
43 
44 /*
45  * Error codes for internal use
46  */
47 #define	ERR_UNSPEC		256
48 #define	ERR_INCOMPLETE		257
49 #define	ERR_TIMEOUT		258
50 #define	ERR_TOOMUCH		259
51 
52 /*
53  * Flags for forming descriptors.
54  */
55 #define	OPT		0x80	/* this argument is optional, or'd with type */
56 
57 #define	NO		0x0
58 #define	NTP_STR		0x1	/* string argument */
59 #define	NTP_UINT	0x2	/* unsigned integer */
60 #define	NTP_INT		0x3	/* signed integer */
61 #define	NTP_ADD		0x4	/* IP network address */
62 #define IP_VERSION	0x5	/* IP version */
63 #define	NTP_ADP		0x6	/* IP address and port */
64 #define NTP_LFP		0x7	/* NTP timestamp */
65 #define NTP_MODE	0x8	/* peer mode */
66 #define NTP_2BIT	0x9	/* leap bits */
67 #define NTP_REFID	0xA	/* RefID */
68 
69 /*
70  * Arguments are returned in a union
71  */
72 typedef union {
73 	const char *string;
74 	long ival;
75 	u_long uval;
76 	sockaddr_u netnum;
77 } arg_v;
78 
79 /*
80  * Structure for passing parsed command line
81  */
82 struct parse {
83 	const char *keyword;
84 	arg_v argval[MAXARGS];
85 	size_t nargs;
86 };
87 
88 /*
89  * ntpdc includes a command parser which could charitably be called
90  * crude.  The following structure is used to define the command
91  * syntax.
92  */
93 struct xcmd {
94   const char *keyword;		/* command key word */
95 	void (*handler)	(struct parse *, FILE *);	/* command handler */
96 	u_char arg[MAXARGS];	/* descriptors for arguments */
97   const char *desc[MAXARGS];	/* descriptions for arguments */
98   const char *comment;
99 };
100 
101 /*
102  * Structure to hold association data
103  */
104 struct association {
105 	associd_t assid;
106 	u_short status;
107 };
108 
109 /*
110  * mrulist terminal status interval
111  */
112 #define	MRU_REPORT_SECS	5
113 
114 /*
115  * var_format is used to override cooked formatting for selected vars.
116  */
117 typedef struct var_format_tag {
118 	const char *	varname;
119 	u_short		fmt;
120 } var_format;
121 
122 typedef struct chost_tag chost;
123 struct chost_tag {
124 	const char *name;
125 	int 	    fam;
126 };
127 
128 extern chost	chosts[];
129 
130 extern int	interactive;	/* are we prompting? */
131 extern int	old_rv;		/* use old rv behavior? --old-rv */
132 extern te_Refid	drefid;		/* How should we display a refid? */
133 extern u_int	assoc_cache_slots;/* count of allocated array entries */
134 extern u_int	numassoc;	/* number of cached associations */
135 extern u_int	numhosts;
136 
137 extern	void	grow_assoc_cache(void);
138 extern	void	asciize		(int, char *, FILE *);
139 extern	int	getnetnum	(const char *, sockaddr_u *, char *, int);
140 extern	void	sortassoc	(void);
141 extern	void	show_error_msg	(int, associd_t);
142 extern	int	dogetassoc	(FILE *);
143 extern	int	doquery		(int, associd_t, int, size_t, const char *,
144 				 u_short *, size_t *, const char **);
145 extern	int	doqueryex	(int, associd_t, int, size_t, const char *,
146 				 u_short *, size_t *, const char **, int);
147 extern	const char * nntohost	(sockaddr_u *);
148 extern	const char * nntohost_col (sockaddr_u *, size_t, int);
149 extern	const char * nntohostp	(sockaddr_u *);
150 extern	int	decodets	(char *, l_fp *);
151 extern	int	decodeuint	(char *, u_long *);
152 extern	int	nextvar		(size_t *, const char **, char **, char **);
153 extern	int	decodetime	(char *, l_fp *);
154 extern	void	printvars	(size_t, const char *, int, int, int, FILE *);
155 extern	int	decodeint	(char *, long *);
156 extern	void	makeascii	(size_t, const char *, FILE *);
157 extern	const char * trunc_left	(const char *, size_t);
158 extern	const char * trunc_right(const char *, size_t);
159 
160 typedef	int/*BOOL*/ (*Ctrl_C_Handler)(void);
161 extern	int/*BOOL*/ 	push_ctrl_c_handler(Ctrl_C_Handler);
162 extern	int/*BOOL*/ 	pop_ctrl_c_handler(Ctrl_C_Handler);
163