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