FreeBSD/contrib/ntp/ntpdc/ntpdc.c
/*
 * doquery - send a request and process the response
 */
send a request and process the response
send a request and process the response
int
doquery(
	int implcode,
	int reqcode,
	int auth,
	size_t qitems,
	size_t qsize,
	const char *qdata,
	size_t *ritems,
	size_t *rsize,
	const char **rdata,
 	int quiet_mask,
	int esize
	)
{
	int res;
	char junk[512];
	fd_set fds;
	struct sock_timeval tvzero;
	if (!havehost) {
		(void) fprintf(stderr, "***No host open, use `host' command\n");
		return -1;
	}
again:
	do {
		tvzero.tv_sec = tvzero.tv_usec = 0;
		FD_ZERO(&fds);
		FD_SET(sockfd, &fds);
		res = select(sockfd+1, &fds, NULL, NULL, &tvzero);
		if (res == -1) {
			warning("polling select");
			return -1;
		} else if (res > 0)
		    (void) recv(sockfd, junk, sizeof junk, 0);
	} while (res > 0);
	res = sendrequest(implcode, reqcode, auth, qitems, qsize, qdata);
	if (res != 0)
		return res;
	res = getresponse(implcode, reqcode, ritems, rsize, rdata, esize);
	if (res == INFO_ERR_FMT && req_pkt_size != 48) {
		int oldsize;
		oldsize = req_pkt_size;
		switch(req_pkt_size) {
		case REQ_LEN_NOMAC:
			req_pkt_size = 160;
			break;
		case 160:
			req_pkt_size = 48;
			break;
		}
		if (impl_ver == IMPL_XNTPD) {
			fprintf(stderr,
			    "***Warning changing to older implementation\n");
			return INFO_ERR_IMPL;
		}
		fprintf(stderr,
		    "***Warning changing the request packet size from %d to %d\n",
		    oldsize, req_pkt_size);
		goto again;
	}
 	if ((res > 0) && (((1 << res) & quiet_mask) == 0)) {
		switch(res) {
		case INFO_ERR_IMPL:
			if (implcode == IMPL_XNTPD)
				break;
			(void) fprintf(stderr,
				       "***Server implementation incompatible with our own\n");
			break;
		case INFO_ERR_REQ:
			(void) fprintf(stderr,
				       "***Server doesn't implement this request\n");
			break;
		case INFO_ERR_FMT:
			(void) fprintf(stderr,
				       "***Server reports a format error in the received packet (shouldn't happen)\n");
			break;
		case INFO_ERR_NODATA:
			(void) fprintf(stderr,
				       "***Server reports data not found\n");
			break;
		case INFO_ERR_AUTH:
			(void) fprintf(stderr, "***Permission denied\n");
			break;
		case ERR_TIMEOUT:
			(void) fprintf(stderr, "***Request timed out\n");
			break;
		case ERR_INCOMPLETE:
			(void) fprintf(stderr,
				       "***Response from server was incomplete\n");
			break;
		default:
			(void) fprintf(stderr,
				       "***Server returns unknown error code %d\n", res);
			break;
		}
	}
	return res;
}
int doquery ( int implcode , int reqcode , int auth , size_t qitems , size_t qsize , const char * qdata , size_t * ritems , size_t * rsize , const char * * rdata , int quiet_mask , int esize ) { int res ; char junk [ 512 ] ; fd_set fds ; struct sock_timeval tvzero ; if ( ! havehost ) { ( void ) fprintf ( stderr , " \n " ) ; return -1 ; } again : do { tvzero . tv_sec = tvzero . tv_usec = 0 ; FD_ZERO ( & fds ) ; FD_SET ( sockfd , & fds ) ; res = select ( sockfd + 1 , & fds , NULL , NULL , & tvzero ) ; if ( res == -1 ) { warning ( " " ) ; return -1 ; } else if ( res > 0 ) ( void ) recv ( sockfd , junk , sizeof junk , 0 ) ; } while ( res > 0 ) ; res = sendrequest ( implcode , reqcode , auth , qitems , qsize , qdata ) ; if ( res != 0 ) return res ; res = getresponse ( implcode , reqcode , ritems , rsize , rdata , esize ) ; if ( res == INFO_ERR_FMT && req_pkt_size != 48 ) { int oldsize ; oldsize = req_pkt_size ; switch ( req_pkt_size ) { case REQ_LEN_NOMAC : req_pkt_size = 160 ; break ; case 160 : req_pkt_size = 48 ; break ; } if ( impl_ver == IMPL_XNTPD ) { fprintf ( stderr , " \n " ) ; return INFO_ERR_IMPL ; } fprintf ( stderr , " \n " , oldsize , req_pkt_size ) ; goto again ; } if ( ( res > 0 ) && ( ( ( 1 << res ) & quiet_mask ) == 0 ) ) { switch ( res ) { case INFO_ERR_IMPL : if ( implcode == IMPL_XNTPD ) break ; ( void ) fprintf ( stderr , " \n " ) ; break ; case INFO_ERR_REQ : ( void ) fprintf ( stderr , " \n " ) ; break ; case INFO_ERR_FMT : ( void ) fprintf ( stderr , " \n " ) ; break ; case INFO_ERR_NODATA : ( void ) fprintf ( stderr , " \n " ) ; break ; case INFO_ERR_AUTH : ( void ) fprintf ( stderr , " \n " ) ; break ; case ERR_TIMEOUT : ( void ) fprintf ( stderr , " \n " ) ; break ; case ERR_INCOMPLETE : ( void ) fprintf ( stderr , " \n " ) ; break ; default : ( void ) fprintf ( stderr , " \n " , res ) ; break ; } } return res ; }
doquery - send a request and process the response
doquery - send a request and process the response
/*
	 * Check to make sure host is open
	 */ /*
	 * Poll the socket and clear out any pending data
	 */ /*
	 * send a request
	 */ /*
	 * Get the response.  If we got a standard error, print a message
	 */ /*
	 * Try to be compatible with older implementations of ntpd.
	 */ /* log error message if not told to be quiet */ /* Give us a chance to try the older implementation. */