#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")
l_fp tmp;
int errflg;
int c;
- int nfound;
+ int nfound;
#ifdef HAVE_NETINFO
ni_namelist *netinfoservers;
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? */
* 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)
/*
* 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(
}
/*
- * 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,
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.
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) {
}
/*
- * That's it. Return our server.
+ * That's it. Return our server.
*/
return sys_server;
}
/* 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(
)
{
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;
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) {
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;
}
/* 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);
}
/* 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);
/* 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);
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
strcpy(service, "ntp");
- /*
- * 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*/
- }
-
- /* 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) {
+ /*
+ * 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*/
+ }
+
+ /* 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) {
/* 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
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
/*
{
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);
}
exit(1);
}
#endif /* SYS_WINNT */
- nbsock++;
+ nbsock++;
}
freeaddrinfo(ressave);
}
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));
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));
}
}
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
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
l_adj_systime(ts);
return 1;
#endif
-#else /* SLEWALWAYS */
+#else /* SLEWALWAYS */
if (debug)
return 1;
LFPTOD(ts, dtemp);