xref: /netbsd-src/external/bsd/ntp/dist/ntpq/ntpq.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: ntpq.h,v 1.9 2016/05/01 23:32:01 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 
70 /*
71  * Arguments are returned in a union
72  */
73 typedef union {
74 	const char *string;
75 	long ival;
76 	u_long uval;
77 	sockaddr_u netnum;
78 } arg_v;
79 
80 /*
81  * Structure for passing parsed command line
82  */
83 struct parse {
84 	const char *keyword;
85 	arg_v argval[MAXARGS];
86 	size_t nargs;
87 };
88 
89 /*
90  * ntpdc includes a command parser which could charitably be called
91  * crude.  The following structure is used to define the command
92  * syntax.
93  */
94 struct xcmd {
95   const char *keyword;		/* command key word */
96 	void (*handler)	(struct parse *, FILE *);	/* command handler */
97 	u_char arg[MAXARGS];	/* descriptors for arguments */
98   const char *desc[MAXARGS];	/* descriptions for arguments */
99   const char *comment;
100 };
101 
102 /*
103  * Structure to hold association data
104  */
105 struct association {
106 	associd_t assid;
107 	u_short status;
108 };
109 
110 /*
111  * mrulist terminal status interval
112  */
113 #define	MRU_REPORT_SECS	5
114 
115 /*
116  * var_format is used to override cooked formatting for selected vars.
117  */
118 typedef struct var_format_tag {
119 	const char *	varname;
120 	u_short		fmt;
121 } var_format;
122 
123 typedef struct chost_tag chost;
124 struct chost_tag {
125 	const char *name;
126 	int 	    fam;
127 };
128 
129 extern chost	chosts[];
130 
131 extern int	interactive;	/* are we prompting? */
132 extern int	old_rv;		/* use old rv behavior? --old-rv */
133 extern te_Refid	drefid;		/* How should we display a refid? */
134 extern u_int	assoc_cache_slots;/* count of allocated array entries */
135 extern u_int	numassoc;	/* number of cached associations */
136 extern u_int	numhosts;
137 
138 extern	void	grow_assoc_cache(void);
139 extern	void	asciize		(int, char *, FILE *);
140 extern	int	getnetnum	(const char *, sockaddr_u *, char *, int);
141 extern	void	sortassoc	(void);
142 extern	void	show_error_msg	(int, associd_t);
143 extern	int	dogetassoc	(FILE *);
144 extern	int	doquery		(int, associd_t, int, size_t, const char *,
145 				 u_short *, size_t *, const char **);
146 extern	int	doqueryex	(int, associd_t, int, size_t, const char *,
147 				 u_short *, size_t *, const char **, int);
148 extern	const char * nntohost	(sockaddr_u *);
149 extern	const char * nntohost_col (sockaddr_u *, size_t, int);
150 extern	const char * nntohostp	(sockaddr_u *);
151 extern	int	decodets	(char *, l_fp *);
152 extern	int	decodeuint	(char *, u_long *);
153 extern	int	nextvar		(size_t *, const char **, char **, char **);
154 extern	int	decodetime	(char *, l_fp *);
155 extern	void	printvars	(size_t, const char *, int, int, int, FILE *);
156 extern	int	decodeint	(char *, long *);
157 extern	void	makeascii	(size_t, const char *, FILE *);
158 extern	const char * trunc_left	(const char *, size_t);
159 extern	const char * trunc_right(const char *, size_t);
160 
161 typedef	int/*BOOL*/ (*Ctrl_C_Handler)(void);
162 extern	int/*BOOL*/ 	push_ctrl_c_handler(Ctrl_C_Handler);
163 extern	int/*BOOL*/ 	pop_ctrl_c_handler(Ctrl_C_Handler);
164