Make "-s" option silent again, fixes PR bin/23707.

This has been fixed in current ntp-dev and also includes
some whitespace fixes.  NTP bug report available at:

http://bugzilla.ntp.org/show_bug.cgi?id=263
This commit is contained in:
keihan 2003-12-20 16:12:27 +00:00
parent 8c82867db3
commit 9f474dc36a

View File

@ -1,4 +1,4 @@
/* $NetBSD: ntpdate.c,v 1.3 2003/12/04 16:23:38 drochner Exp $ */
/* $NetBSD: ntpdate.c,v 1.4 2003/12/20 16:12:27 keihan Exp $ */
/*
* ntpdate - set the time of day by polling one or more NTP servers
@ -73,8 +73,8 @@ struct timeval timeout = {60,0};
#ifdef SYS_WINNT
#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#define EPFNOSUPPORT WSAEPFNOSUPPORT
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#define EPFNOSUPPORT WSAEPFNOSUPPORT
#define TARGET_RESOLUTION 1 /* Try for 1-millisecond accuracy
on Windows NT timers. */
#pragma comment(lib, "winmm")
@ -326,7 +326,7 @@ ntpdatemain (
l_fp tmp;
int errflg;
int c;
int nfound;
int nfound;
#ifdef HAVE_NETINFO
ni_namelist *netinfoservers;
@ -575,11 +575,11 @@ ntpdatemain (
while (complete_servers < sys_numservers) {
#ifdef HAVE_POLL_H
struct pollfd* rdfdes;
rdfdes = fdmask;
struct pollfd* rdfdes;
rdfdes = fdmask;
#else
fd_set rdfdes;
rdfdes = fdmask;
rdfdes = fdmask;
#endif
if (alarm_flag) { /* alarmed? */
@ -593,10 +593,10 @@ ntpdatemain (
* Nothing to do. Wait for something.
*/
#ifdef HAVE_POLL_H
nfound = poll(rdfdes, (unsigned int)nbsock, timeout.tv_sec * 1000);
nfound = poll(rdfdes, (unsigned int)nbsock, timeout.tv_sec * 1000);
#else
nfound = select(maxfd, &rdfdes, (fd_set *)0,
nfound = select(maxfd, &rdfdes, (fd_set *)0,
(fd_set *)0, &timeout);
#endif
if (nfound > 0)
@ -678,8 +678,8 @@ ntpdatemain (
/*
* transmit - transmit a packet to the given server, or mark it completed.
* This is called by the timeout routine and by the receive
* procedure.
* This is called by the timeout routine and by the receive
* procedure.
*/
static void
transmit(
@ -712,7 +712,7 @@ transmit(
}
/*
* If we're here, send another message to the server. Fill in
* If we're here, send another message to the server. Fill in
* the packet and let 'er rip.
*/
xpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
@ -781,7 +781,7 @@ receive(
int is_authentic;
if (debug)
printf("receive(%s)\n", stoa(&rbufp->recv_srcadr));
printf("receive(%s)\n", stoa(&rbufp->recv_srcadr));
/*
* Check to see if the packet basically looks like something
* intended for us.
@ -1062,29 +1062,29 @@ clock_select(void)
if (server->delay == 0) {
if (debug)
printf("%s: Server dropped: no data\n", ntoa(&server->srcadr));
continue; /* no data */
continue; /* no data */
}
if (server->stratum > NTP_INFIN) {
if (debug)
printf("%s: Server dropped: strata too high\n", ntoa(&server->srcadr));
continue; /* stratum no good */
continue; /* stratum no good */
}
if (server->delay > NTP_MAXWGT) {
if (debug)
printf("%s: Server dropped: server too far away\n",
ntoa(&server->srcadr));
continue; /* too far away */
ntoa(&server->srcadr));
continue; /* too far away */
}
if (server->leap == LEAP_NOTINSYNC) {
if (debug)
printf("%s: Server dropped: Leap not in sync\n", ntoa(&server->srcadr));
continue; /* he's in trouble */
continue; /* he's in trouble */
}
if (!L_ISHIS(&server->org, &server->reftime)) {
if (debug)
printf("%s: Server dropped: server is very broken\n",
ntoa(&server->srcadr));
continue; /* very broken host */
continue; /* very broken host */
}
if ((server->org.l_ui - server->reftime.l_ui)
>= NTP_MAXAGE) {
@ -1241,7 +1241,7 @@ clock_select(void)
}
/*
* That's it. Return our server.
* That's it. Return our server.
*/
return sys_server;
}
@ -1318,7 +1318,7 @@ clock_adjust(void)
/* XXX ELIMINATE: merge BIG slew into adj_systime in lib/systime.c */
/*
* addserver - determine a server's address and allocate a new structure
* for it.
* for it.
*/
static void
addserver(
@ -1326,39 +1326,44 @@ addserver(
)
{
register struct server *server;
/* Address infos structure to store result of getaddrinfo */
struct addrinfo *addrResult;
/* Address infos structure to store hints for getaddrinfo */
struct addrinfo hints;
/* Error variable for getaddrinfo */
int error;
/* Service name */
char service[5];
/* Address infos structure to store result of getaddrinfo */
struct addrinfo *addrResult;
/* Address infos structure to store hints for getaddrinfo */
struct addrinfo hints;
/* Error variable for getaddrinfo */
int error;
/* Service name */
char service[5];
strcpy(service, "ntp");
/* Get host address. Looking for UDP datagram connection. */
memset(&hints, 0, sizeof(hints));
hints.ai_family = ai_fam_templ;
hints.ai_socktype = SOCK_DGRAM;
/* Get host address. Looking for UDP datagram connection. */
memset(&hints, 0, sizeof(hints));
hints.ai_family = ai_fam_templ;
hints.ai_socktype = SOCK_DGRAM;
printf("Looking for host %s and service %s\n", serv, service);
#ifdef DEBUG
if (debug)
printf("Looking for host %s and service %s\n", serv, service);
#endif
error = getaddrinfo(serv, service, &hints, &addrResult);
if (error != 0) {
fprintf(stderr, "Error : %s\n", gai_strerror(error));
error = getaddrinfo(serv, service, &hints, &addrResult);
if (error != 0) {
fprintf(stderr, "Error : %s\n", gai_strerror(error));
msyslog(LOG_ERR, "can't find host %s\n", serv);
return;
}
else {
fprintf(stderr, "host found : %s\n", stohost((struct sockaddr_storage*)addrResult->ai_addr));
}
#ifdef DEBUG
else if (debug) {
fprintf(stderr, "host found : %s\n", stohost((struct sockaddr_storage*)addrResult->ai_addr));
}
#endif
server = (struct server *)emalloc(sizeof(struct server));
memset((char *)server, 0, sizeof(struct server));
/* For now we only get the first returned server of the addrinfo list */
memset(&(server->srcadr), 0, sizeof(struct sockaddr_storage));
memcpy(&(server->srcadr), addrResult->ai_addr, addrResult->ai_addrlen);
/* For now we only get the first returned server of the addrinfo list */
memset(&(server->srcadr), 0, sizeof(struct sockaddr_storage));
memcpy(&(server->srcadr), addrResult->ai_addr, addrResult->ai_addrlen);
server->event_time = ++sys_numservers;
if (sys_servers == NULL)
sys_servers = server;
@ -1391,21 +1396,21 @@ findserver(
for (server = sys_servers; server != NULL;
server = server->next_server) {
if (memcmp(addr, &server->srcadr, SOCKLEN(addr))==0)
if (memcmp(addr, &server->srcadr, SOCKLEN(addr))==0)
return server;
/* Multicast compatibility to verify here... I'm not sure it's working */
if(addr->ss_family == AF_INET) {
if (IN_MULTICAST(ntohl(((struct sockaddr_in*)addr)->sin_addr.s_addr)))
mc_server = server;
}
else {
/* Multicast compatibility to verify here... I'm not sure it's working */
if(addr->ss_family == AF_INET) {
if (IN_MULTICAST(ntohl(((struct sockaddr_in*)addr)->sin_addr.s_addr)))
mc_server = server;
}
else {
#ifdef AF_INET6
if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)(&server->srcadr))->sin6_addr))
if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)(&server->srcadr))->sin6_addr))
mc_server = server;
#else
return 0;
return 0;
#endif
}
}
}
if (mc_server != NULL) {
@ -1420,7 +1425,7 @@ findserver(
server = (struct server *)emalloc(sizeof(struct server));
memset((char *)server, 0, sizeof(struct server));
memcpy(&server->srcadr, &addr, sizeof(struct sockaddr_storage));
memcpy(&server->srcadr, &addr, sizeof(struct sockaddr_storage));
server->event_time = ++sys_numservers;
@ -1561,7 +1566,7 @@ init_alarm(void)
}
/* get the LUID for system-time privilege. */
LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; /* one privilege to set */
tkp.PrivilegeCount = 1; /* one privilege to set */
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
/* get set-time privilege for this process. */
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);
@ -1583,7 +1588,7 @@ init_alarm(void)
}
/* get the LUID for system-time privilege. */
LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; /* one privilege to set */
tkp.PrivilegeCount = 1; /* one privilege to set */
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
/* get set-time privilege for this process. */
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);
@ -1609,11 +1614,11 @@ init_alarm(void)
/* start the timer event */
wTimerID = timeSetEvent(
(UINT) (1000/TIMER_HZ), /* Delay */
wTimerRes, /* Resolution */
(LPTIMECALLBACK) alarming, /* Callback function */
(DWORD) dwUser, /* User data */
TIME_PERIODIC); /* Event type (periodic) */
(UINT) (1000/TIMER_HZ), /* Delay */
wTimerRes, /* Resolution */
(LPTIMECALLBACK) alarming, /* Callback function */
(DWORD) dwUser, /* User data */
TIME_PERIODIC); /* Event type (periodic) */
if (wTimerID == 0) {
msyslog(LOG_ERR, "timeSetEvent failed: %m");
exit(1);
@ -1644,10 +1649,10 @@ init_alarm(void)
static void
init_io(void)
{
struct addrinfo *res, *ressave;
struct addrinfo hints;
struct addrinfo *res, *ressave;
struct addrinfo hints;
char service[5];
int optval = 1;
int optval = 1;
/*
* Init buffer free list and stat counters
@ -1660,53 +1665,53 @@ init_io(void)
strcpy(service, "ntp");
/*
* Init hints addrinfo structure
*/
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_DGRAM;
/*
* Init hints addrinfo structure
*/
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_DGRAM;
if(getaddrinfo(NULL, service, &hints, &res) != 0) {
msyslog(LOG_ERR, "getaddrinfo() failed: %m");
exit(1);
/*NOTREACHED*/
}
if(getaddrinfo(NULL, service, &hints, &res) != 0) {
msyslog(LOG_ERR, "getaddrinfo() failed: %m");
exit(1);
/*NOTREACHED*/
}
/* Remember the address of the addrinfo structure chain */
ressave = res;
/* Remember the address of the addrinfo structure chain */
ressave = res;
/*
* For each structure returned, open and bind socket
*/
for(nbsock = 0; (nbsock < MAX_AF) && res ; res = res->ai_next) {
/*
* For each structure returned, open and bind socket
*/
for(nbsock = 0; (nbsock < MAX_AF) && res ; res = res->ai_next) {
/* create a datagram (UDP) socket */
if ((fd[nbsock] = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
if ((fd[nbsock] = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT ||
errno == EPFNOSUPPORT)
continue;
netsyslog(LOG_ERR, "socket() failed: %m");
exit(1);
/*NOTREACHED*/
}
/* set socket to reuse address */
if (setsockopt(fd[nbsock], SOL_SOCKET, SO_REUSEADDR, (void*) &optval, sizeof(optval)) < 0) {
netsyslog(LOG_ERR, "setsockopt() SO_REUSEADDR failed: %m");
exit(1);
}
/* set socket to reuse address */
if (setsockopt(fd[nbsock], SOL_SOCKET, SO_REUSEADDR, (void*) &optval, sizeof(optval)) < 0) {
netsyslog(LOG_ERR, "setsockopt() SO_REUSEADDR failed: %m");
exit(1);
/*NOTREACHED*/
}
}
#ifdef IPV6_V6ONLY
/* Restricts AF_INET6 socket to IPv6 communications (see RFC 2553bis-03) */
if (res->ai_family == AF_INET6)
if (setsockopt(fd[nbsock], IPPROTO_IPV6, IPV6_V6ONLY, (void*) &optval, sizeof(optval)) < 0) {
netsyslog(LOG_ERR, "setsockopt() IPV6_V6ONLY failed: %m");
exit(1);
/*NOTREACHED*/
}
/* Restricts AF_INET6 socket to IPv6 communications (see RFC 2553bis-03) */
if (res->ai_family == AF_INET6)
if (setsockopt(fd[nbsock], IPPROTO_IPV6, IPV6_V6ONLY, (void*) &optval, sizeof(optval)) < 0) {
netsyslog(LOG_ERR, "setsockopt() IPV6_V6ONLY failed: %m");
exit(1);
/*NOTREACHED*/
}
#endif
/* Remember the socket family in fd_family structure */
fd_family[nbsock] = res->ai_family;
/* Remember the socket family in fd_family structure */
fd_family[nbsock] = res->ai_family;
/*
* bind the socket to the NTP port
@ -1731,9 +1736,9 @@ init_io(void)
fdmask[nbsock].events = POLLIN;
#else
FD_SET(fd[nbsock], &fdmask);
if ((SOCKET) maxfd < fd[nbsock]+1) {
maxfd = fd[nbsock]+1;
}
if ((SOCKET) maxfd < fd[nbsock]+1) {
maxfd = fd[nbsock]+1;
}
#endif
/*
@ -1744,7 +1749,7 @@ init_io(void)
{
int on = TRUE;
if (ioctl(fd[nbsock],FIONBIO, &on) == ERROR) {
if (ioctl(fd[nbsock],FIONBIO, &on) == ERROR) {
netsyslog(LOG_ERR, "ioctl(FIONBIO) fails: %m");
exit(1);
}
@ -1774,7 +1779,7 @@ init_io(void)
exit(1);
}
#endif /* SYS_WINNT */
nbsock++;
nbsock++;
}
freeaddrinfo(ressave);
}
@ -1789,27 +1794,27 @@ sendpkt(
int len
)
{
int i;
int i;
int cc;
SOCKET sock = 0;
SOCKET sock = 0;
#ifdef SYS_WINNT
DWORD err;
#endif /* SYS_WINNT */
/* Find a local family compatible socket to send ntp packet to ntp server */
for(i = 0; (i < MAX_AF); i++) {
if(dest->ss_family == fd_family[i]) {
sock = fd[i];
break;
}
}
/* Find a local family compatible socket to send ntp packet to ntp server */
for(i = 0; (i < MAX_AF); i++) {
if(dest->ss_family == fd_family[i]) {
sock = fd[i];
break;
}
}
if ( sock == 0 ) {
netsyslog(LOG_ERR, "cannot find family compatible socket to send ntp packet");
exit(1);
/*NOTREACHED*/
}
if ( sock == 0 ) {
netsyslog(LOG_ERR, "cannot find family compatible socket to send ntp packet");
exit(1);
/*NOTREACHED*/
}
cc = sendto(sock, (char *)pkt, len, 0, (struct sockaddr *)dest,
SOCKLEN(dest));
@ -1822,7 +1827,7 @@ sendpkt(
err = WSAGetLastError();
if (err != WSAEWOULDBLOCK && err != WSAENOBUFS)
#endif /* SYS_WINNT */
netsyslog(LOG_ERR, "sendto(%s): %m", stohost(dest));
netsyslog(LOG_ERR, "sendto(%s): %m", stohost(dest));
}
}
@ -1838,13 +1843,13 @@ input_handler(void)
struct timeval tvzero;
int fromlen;
l_fp ts;
int i;
int i;
#ifdef HAVE_POLL_H
struct pollfd fds[MAX_AF];
#else
fd_set fds;
#endif
int fdc = 0;
int fdc = 0;
/*
* Do a poll to see if we have data
@ -1853,33 +1858,33 @@ input_handler(void)
tvzero.tv_sec = tvzero.tv_usec = 0;
#ifdef HAVE_POLL_H
memcpy(fds, fdmask, sizeof(fdmask));
n = poll(fds, (unsigned int)nbsock, tvzero.tv_sec * 1000);
n = poll(fds, (unsigned int)nbsock, tvzero.tv_sec * 1000);
/*
* Determine which socket received data
*/
/*
* Determine which socket received data
*/
for(i=0; i < nbsock; i++) {
if(fds[i].revents & POLLIN) {
fdc = fd[i];
break;
}
}
for(i=0; i < nbsock; i++) {
if(fds[i].revents & POLLIN) {
fdc = fd[i];
break;
}
}
#else
fds = fdmask;
n = select(maxfd, &fds, (fd_set *)0, (fd_set *)0, &tvzero);
n = select(maxfd, &fds, (fd_set *)0, (fd_set *)0, &tvzero);
/*
* Determine which socket received data
*/
/*
* Determine which socket received data
*/
for(i=0; i < maxfd; i++) {
if(FD_ISSET(fd[i], &fds)) {
fdc = fd[i];
break;
}
}
for(i=0; i < maxfd; i++) {
if(FD_ISSET(fd[i], &fds)) {
fdc = fd[i];
break;
}
}
#endif
@ -2054,7 +2059,7 @@ l_step_systime(
l_adj_systime(ts);
return 1;
#endif
#else /* SLEWALWAYS */
#else /* SLEWALWAYS */
if (debug)
return 1;
LFPTOD(ts, dtemp);