]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 263] (NetBSD PR bin/23707)
authorHarlan Stenn <stenn@ntp.org>
Sat, 20 Dec 2003 06:03:29 +0000 (01:03 -0500)
committerHarlan Stenn <stenn@ntp.org>
Sat, 20 Dec 2003 06:03:29 +0000 (01:03 -0500)
bk: 3fe3e631bW8Kv5GhiLTVVAa39aQdTg

ntpdate/ntpdate.c

index 398d194660380910185d5cd41a4a505d4eec77c6..35ac1fa66d2429f45209fabb03a295a1a99c6020 100644 (file)
@@ -71,8 +71,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")
@@ -324,7 +324,7 @@ ntpdatemain (
        l_fp tmp;
        int errflg;
        int c;
-        int nfound;
+       int nfound;
 
 #ifdef HAVE_NETINFO
        ni_namelist *netinfoservers;
@@ -573,11 +573,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? */
@@ -591,10 +591,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)
@@ -676,8 +676,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(
@@ -710,7 +710,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,
@@ -779,7 +779,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.
@@ -1060,29 +1060,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) {
@@ -1239,7 +1239,7 @@ clock_select(void)
        }
 
        /*
-        * That's it.    Return our server.
+        * That's it.  Return our server.
         */
        return sys_server;
 }
@@ -1316,7 +1316,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(
@@ -1324,39 +1324,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;
@@ -1389,21 +1394,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) {        
@@ -1418,7 +1423,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;
 
@@ -1559,7 +1564,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);
@@ -1581,7 +1586,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);
@@ -1607,11 +1612,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);
@@ -1642,10 +1647,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
@@ -1658,53 +1663,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;
-
-        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
@@ -1729,9 +1734,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
 
        /*
@@ -1742,7 +1747,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);
        }
@@ -1772,7 +1777,7 @@ init_io(void)
                exit(1);
        }
 #endif /* SYS_WINNT */
-       nbsock++;
+       nbsock++;
     }
     freeaddrinfo(ressave);
 }
@@ -1787,27 +1792,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));
@@ -1820,7 +1825,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));
        }
 }
 
@@ -1836,13 +1841,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
@@ -1851,33 +1856,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
 
@@ -2052,7 +2057,7 @@ l_step_systime(
        l_adj_systime(ts);
        return 1;
 #endif
-#else /* SLEWALWAYS  */
+#else /* SLEWALWAYS */
        if (debug)
                return 1;
        LFPTOD(ts, dtemp);