Home | History | Annotate | Line # | Download | only in ntpq
      1 /*	$NetBSD: ntpq.h,v 1.11 2024/08/18 20:47:19 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 
     17 #include "ntpq-opts.h"
     18 
     19 /*
     20  * Maximum number of arguments
     21  */
     22 #define	MAXARGS	4
     23 
     24 /*
     25  * Limit on packets in a single response.  Increasing this value to
     26  * 96 will marginally speed "mrulist" operation on lossless networks
     27  * but it has been observed to cause loss on WiFi networks and with
     28  * an IPv6 go6.net tunnel over UDP.  That loss causes the request
     29  * row limit to be cut in half, and it grows back very slowly to
     30  * ensure forward progress is made and loss isn't triggered too quickly
     31  * afterward.  While the lossless case gains only marginally with
     32  * MAXFRAGS == 96, the lossy case is a lot slower due to the repeated
     33  * timeouts.  Empirally, MAXFRAGS == 32 avoids most of the routine loss
     34  * on both the WiFi and UDP v6 tunnel tests and seems a good compromise.
     35  * This suggests some device in the path has a limit of 32 ~512 byte UDP
     36  * packets in queue.
     37  * Lowering MAXFRAGS may help with particularly lossy networks, but some
     38  * ntpq commands may rely on the longtime value of 24 implicitly,
     39  * assuming a single multipacket response will be large enough for any
     40  * needs.  In contrast, the "mrulist" command is implemented as a series
     41  * of requests and multipacket responses to each.
     42  */
     43 #define	MAXFRAGS	32
     44 
     45 /*
     46  * Error codes for internal use
     47  */
     48 #define	ERR_UNSPEC		256
     49 #define	ERR_INCOMPLETE		257
     50 #define	ERR_TIMEOUT		258
     51 #define	ERR_TOOMUCH		259
     52 
     53 /*
     54  * Flags for forming descriptors.
     55  */
     56 #define	OPT		0x80	/* this argument is optional, or'd with type */
     57 
     58 #define	NO		0x0
     59 #define	NTP_STR		0x1	/* string argument */
     60 #define	NTP_UINT	0x2	/* unsigned integer */
     61 #define	NTP_INT		0x3	/* signed integer */
     62 #define	NTP_ADD		0x4	/* IP network address */
     63 #define IP_VERSION	0x5	/* IP version */
     64 #define	NTP_ADP		0x6	/* IP address and port */
     65 #define NTP_LFP		0x7	/* NTP timestamp */
     66 #define NTP_MODE	0x8	/* peer mode */
     67 #define NTP_2BIT	0x9	/* leap bits */
     68 #define NTP_REFID	0xA	/* RefID */
     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