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