diff options
| author | Cy Schubert <cy@FreeBSD.org> | 2020-03-04 13:59:29 +0000 | 
|---|---|---|
| committer | Cy Schubert <cy@FreeBSD.org> | 2020-03-04 13:59:29 +0000 | 
| commit | 5171bc9b11192d9ad273db7854787eaa65eb9997 (patch) | |
| tree | b872a53605ef14ae76de1104afb4be189d1a958a /ntpq/ntpq.c | |
| parent | 360c01464aee3bc4c520898a675f35967db09ac2 (diff) | |
Diffstat (limited to 'ntpq/ntpq.c')
| -rw-r--r-- | ntpq/ntpq.c | 67 | 
1 files changed, 52 insertions, 15 deletions
| diff --git a/ntpq/ntpq.c b/ntpq/ntpq.c index 6e7a5c375175a..0382c0f948d7d 100644 --- a/ntpq/ntpq.c +++ b/ntpq/ntpq.c @@ -122,10 +122,12 @@ u_char pktversion = NTP_OLDVERSION + 1;  #define	NA	2	/* network address */  #define	LP	3	/* leap (print in binary) */  #define	RF	4	/* refid (sometimes string, sometimes not) */ -#define	AR	5	/* array of times */ +#define	AU	5	/* array of unsigned times */  #define FX	6	/* test flags */  #define TS	7	/* l_fp timestamp in hex */  #define	OC	8	/* integer, print in octal */ +#define	AS	9	/* array of signed times */ +#define	SN	10	/* signed number: must display +/- sign */  #define	EOV	255	/* end of table */  /* @@ -146,10 +148,12 @@ const var_format cookedvars[] = {  	{ "srcadr",		HA },  	{ "peeradr",		HA },	/* compat with others */  	{ "dstadr",		NA }, -	{ "filtdelay",		AR }, -	{ "filtoffset",		AR }, -	{ "filtdisp",		AR }, -	{ "filterror",		AR },	/* compat with others */ +	{ "filtdelay",		AU }, +	{ "filtoffset",		AS }, +	{ "filtdisp",		AU }, +	{ "filterror",		AU },	/* compat with others */ +	{ "offset",		SN }, +	{ "frequency",		SN }  }; @@ -225,7 +229,7 @@ static	void	rawprint	(int, size_t, const char *, int, int, FILE *);  static	void	startoutput	(void);  static	void	output		(FILE *, const char *, const char *);  static	void	endoutput	(FILE *); -static	void	outputarr	(FILE *, char *, int, l_fp *); +static	void	outputarr	(FILE *, char *, int, l_fp *, int);  static	int	assoccmp	(const void *, const void *);  	u_short	varfmt		(const char *);  	void	ntpq_custom_opt_handler(tOptions *, tOptDesc *); @@ -3493,7 +3497,8 @@ outputarr(  	FILE *fp,  	char *name,  	int narr, -	l_fp *lfp +	l_fp *lfp, +	int issigned  	)  {  	char *bp; @@ -3512,7 +3517,7 @@ outputarr(  	for (i = narr; i > 0; i--) {  		if (i != (size_t)narr)  			*bp++ = ' '; -		cp = lfptoms(lfp, 2); +		cp = (issigned ? lfptoms(lfp, 2) : ulfptoms(lfp, 2));  		len = strlen(cp);  		if (len > 7) {  			cp[7] = '\0'; @@ -3657,11 +3662,31 @@ cookedprint(  			if (!value) {  				output_raw = '?';  			} else if (decodenetnum(value, &hval)) { -				if (ISREFCLOCKADR(&hval)) -					output(fp, name, -					       refnumtoa(&hval)); -				else -					output(fp, name, stoa(&hval)); +				if (datatype == TYPE_CLOCK && IS_IPV4(&hval)) { +					/* +					 * Workaround to override numeric refid formats  +					 * for refclocks received from faulty nptd servers  +					 * and output them as text. +					 */ +					int i; +					unsigned char *str = (unsigned char *)&(hval.sa4).sin_addr; +					char refid_buf[5]; +					for (i=0; i<4 && str[i]; i++) +						refid_buf[i] = (isprint(str[i]) ? str[i] : '?'); +					refid_buf[i] = 0; /* Null terminator */ +					output(fp, name, refid_buf); +				} else if (ISREFCLOCKADR(&hval)) { +					output(fp, name, refnumtoa(&hval)); +				} else { +					if (drefid == REFID_IPV4) { +						output(fp, name, stoa(&hval)); +					} else { +						char refid_buf[12]; +						snprintf (refid_buf, sizeof(refid_buf),  +							  "0x%08x", ntohl(addr2refid(&hval))); +						output(fp, name, refid_buf); +					} +				}  			} else if (strlen(value) <= 4) {  				output(fp, name, value);  			} else { @@ -3693,11 +3718,12 @@ cookedprint(  			}  			break; -		case AR: +		case AU: +		case AS:  			if (!value || !decodearr(value, &narr, lfparr, 8))  				output_raw = '?';  			else -				outputarr(fp, name, narr, lfparr); +				outputarr(fp, name, narr, lfparr, (fmt==AS));  			break;  		case FX: @@ -3707,6 +3733,17 @@ cookedprint(  				output(fp, name, tstflags(uval));  			break; +		case SN: +			if (!value) +				output_raw = '?'; +			else if (isdigit(*value)) {	/* number without sign */ +				bv[0] = '+'; +				atoascii (value, MAXVALLEN, bv+1, sizeof(bv)-1); +				output(fp, name, bv); +			} else +				output_raw = '*';		/* output as-is */ +			break; +  		default:  			fprintf(stderr, "Internal error in cookedprint, %s=%s, fmt %d\n",  				name, value, fmt); | 
