]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 922] Allow interspersed -4 and -6 flags on the ntpq command line
authorHarlan Stenn <stenn@ntp.org>
Mon, 5 Nov 2012 02:34:01 +0000 (02:34 +0000)
committerHarlan Stenn <stenn@ntp.org>
Mon, 5 Nov 2012 02:34:01 +0000 (02:34 +0000)
bk: 50972599x2vmXebreHA1yXOlSBdHDQ

ChangeLog
ntpq/libntpq.c
ntpq/libntpq.h
ntpq/ntpq-subs.c
ntpq/ntpq.c
ntpq/ntpq.h
ntpsnmpd/ntpsnmpd.c

index 6ac8dce577bb140d55cfb34de203626b2eb2da4f..b872f43e5727c380e9b869efe6407e2250f0dd80 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+* [Bug 922] Allow interspersed -4 and -6 flags on the ntpq command line.
 (4.2.7p316) 2012/10/27 Released by Harlan Stenn <stenn@ntp.org>
 * [Bug 2296] Update fix for Bug 2294 to handle --without-crypto.
 (4.2.7p315) 2012/10/26 Released by Harlan Stenn <stenn@ntp.org>
index fff8e81eab7451fba0c535d7af49bf6e2558a1c2..becd956f2889476b0341e6d5a3345c54b5c5927a 100644 (file)
@@ -215,6 +215,7 @@ int ntpq_queryhost(unsigned short VARSET, unsigned short association, char *resu
  ****************************************************************************
  * Parameters:
  *     hostname        char*   Hostname/IP of the host running ntpd
+ *     fam             int     Address Family (AF_INET, AF_INET6, or 0)
  *
  * Returns:
  *     int             1 if the host connection could be set up, i.e. 
@@ -224,9 +225,13 @@ int ntpq_queryhost(unsigned short VARSET, unsigned short association, char *resu
  *                     0 (zero) if a failure occured
  ****************************************************************************/
 
-int ntpq_openhost(char *hostname)
+int
+ntpq_openhost(
+       char *hostname,
+       int fam
+       )
 {
-       if ( openhost(hostname) )
+       if ( openhost(hostname, fam) )
        {
                numhosts = 1;
        } else {
index 14bd1de48c388a242e1853fffab2635c31c6c3f5..82a874093c9b7506b088fa501d97eb6673afe8a8 100644 (file)
@@ -80,7 +80,7 @@ extern struct ntpq_varlist ntpq_varlist[MAXLIST];
  */
 
 /* from libntpq.c */
-extern int ntpq_openhost(char *);
+extern int ntpq_openhost(char *, int);
 extern int ntpq_closehost(void);
 extern int ntpq_queryhost(unsigned short VARSET, associd_t association,
                          char *resultbuf, int maxlen);
index 52e952b868755e1776337846da0f817154fda06c..a299e7817583a76afcd9f6e7d90d637785832f12 100644 (file)
@@ -10,7 +10,6 @@
 #include "ntpq.h"
 #include "ntpq-opts.h"
 
-extern char *  chosts[];
 extern char    currenthost[];
 extern int     currenthostisnum;
 size_t         maxhostlen;
@@ -1858,7 +1857,7 @@ dopeers(
                return;
 
        for (u = 0; u < numhosts; u++) {
-               if (getnetnum(chosts[u], &netnum, fullname, af)) {
+               if (getnetnum(chosts[u].name, &netnum, fullname, af)) {
                        name_or_num = nntohost(&netnum);
                        sl = strlen(name_or_num);
                        maxhostlen = max(maxhostlen, sl);
@@ -1954,7 +1953,7 @@ doopeers(
                return;
 
        for (i = 0; i < numhosts; ++i) {
-               if (getnetnum(chosts[i], &netnum, fullname, af))
+               if (getnetnum(chosts[i].name, &netnum, fullname, af))
                        if (strlen(fullname) > maxhostlen)
                                maxhostlen = strlen(fullname);
        }
index 4583dccbc24a5cf7ff1049b93385f90948f4c989..562fb7b1efd41418421c16cdde4ace9fab8f365a 100644 (file)
@@ -164,7 +164,7 @@ int         ntpqmain        (int,   char **);
 /*
  * Built in command handler declarations
  */
-static int     openhost        (const char *);
+static int     openhost        (const char *, int);
 static void    dump_hex_printable(const void *, size_t);
 static int     sendpkt         (void *, size_t);
 static int     getresponse     (int, int, u_short *, int *, const char **, int);
@@ -359,9 +359,18 @@ const char *ccmds[MAXCMDS];
 /*
  * When multiple hosts are specified.
  */
+
 u_int numhosts;
-const char *chosts[MAXHOSTS];
-#define        ADDHOST(cp)     if (numhosts < MAXHOSTS) chosts[numhosts++] = (cp)
+
+chost chosts[MAXHOSTS];
+#define        ADDHOST(cp)                                             \
+       do {                                                    \
+               if (numhosts < MAXHOSTS) {                      \
+                       chosts[numhosts].name = (cp);           \
+                       chosts[numhosts].fam = ai_fam_templ;    \
+                       numhosts++;                             \
+               }                                               \
+       } while (0)
 
 /*
  * Macro definitions we use
@@ -479,8 +488,26 @@ ntpqmain(
        if (0 == argc) {
                ADDHOST(DEFHOST);
        } else {
-               for (ihost = 0; ihost < (u_int)argc; ihost++)
+               for (ihost = 0; ihost < (u_int)argc; ihost++) {
+                       if ('-' == *argv[ihost]) {
+                               //
+                               // If I really cared I'd also check:
+                               // 0 == argv[ihost][2]
+                               //
+                               // and there are other cases as well...
+                               //
+                               if ('4' == argv[ihost][1]) {
+                                       ai_fam_templ = AF_INET;
+                                       continue;
+                               } else if ('6' == argv[ihost][1]) {
+                                       ai_fam_templ = AF_INET6;
+                                       continue;
+                               } else {
+                                       // XXX Throw a usage error
+                               }
+                       }
                        ADDHOST(argv[ihost]);
+               }
        }
 
        if (numcmds == 0 && interactive == 0
@@ -494,11 +521,12 @@ ntpqmain(
 #endif /* SYS_WINNT */
 
        if (numcmds == 0) {
-               (void) openhost(chosts[0]);
+               (void) openhost(chosts[0].name, chosts[0].fam);
                getcmds();
        } else {
                for (ihost = 0; ihost < numhosts; ihost++) {
-                       if (openhost(chosts[ihost]))
+                       printf("About to call docmd() for <%s> %d\n", chosts[ihost].name, chosts[ihost].fam);
+                       if (openhost(chosts[ihost].name, chosts[ihost].fam))
                                for (icmd = 0; icmd < numcmds; icmd++)
                                        docmd(ccmds[icmd]);
                }
@@ -515,7 +543,8 @@ ntpqmain(
  */
 static int
 openhost(
-       const char *hname
+       const char *hname,
+       int         fam
        )
 {
        const char svc[] = "ntp";
@@ -553,7 +582,7 @@ openhost(
         * give it an IPv4 address to lookup.
         */
        ZERO(hints);
-       hints.ai_family = ai_fam_templ;
+       hints.ai_family = fam;
        hints.ai_protocol = IPPROTO_UDP;
        hints.ai_socktype = SOCK_DGRAM;
        hints.ai_flags = Z_AI_NUMERICHOST;
@@ -597,7 +626,14 @@ openhost(
        }
 
        if (debug > 2)
-               printf("Opening host %s\n", temphost);
+               printf("Opening host %s (%s)\n",
+                       temphost,
+                       (ai->ai_family == AF_INET)
+                       ? "AF_INET"
+                       : (ai->ai_family == AF_INET6)
+                         ? "AF_INET6"
+                         : "AF-???"
+                       );
 
        if (havehost == 1) {
                if (debug > 2)
@@ -655,13 +691,15 @@ openhost(
 # endif
 #endif
 
+       if
 #ifdef SYS_VXWORKS
-       if (connect(sockfd, (struct sockaddr *)&hostaddr,
-                   sizeof(hostaddr)) == -1) {
+          (connect(sockfd, (struct sockaddr *)&hostaddr,
+                   sizeof(hostaddr)) == -1)
 #else
-       if (connect(sockfd, (struct sockaddr *)ai->ai_addr,
-                   ai->ai_addrlen) == -1) {
+          (connect(sockfd, (struct sockaddr *)ai->ai_addr,
+                   ai->ai_addrlen) == -1)
 #endif /* SYS_VXWORKS */
+           {
                error("connect", "", "");
                freeaddrinfo(ai);
                return 0;
@@ -1490,6 +1528,8 @@ docmd(
                jump = 0;       /* HMS: 961106: was after fclose() */
                if (i) (void) fclose(current_output);
        }
+
+       return;
 }
 
 
@@ -2273,7 +2313,7 @@ host(
                        goto no_change;
                i = 1;
        }
-       if (openhost(pcmd->argval[i].string)) {
+       if (openhost(pcmd->argval[i].string, ai_fam_templ)) {
                fprintf(fp, "current host set to %s\n", currenthost);
        } else {
     no_change:
index f5d7dcd4bea7f4977ebd0f0e9994e454d073d76f..ec2bcb98d3bb7916e892b9f87832e55f2ee50b8f 100644 (file)
@@ -116,6 +116,14 @@ typedef struct var_format_tag {
        u_short         fmt;
 } var_format;
 
+typedef struct chost_tag chost;
+struct chost_tag {
+       const char *name;
+       int         fam;
+};
+
+extern chost   chosts[];
+
 extern int     interactive;    /* are we prompting? */
 extern int     old_rv;         /* use old rv behavior? --old-rv */
 extern u_int   assoc_cache_slots;/* count of allocated array entries */
index 278d5a86e17092f33fba6e51cb136f6edca0edcb..d96ad3af453ba88add283ec6cba96d33780332a6 100644 (file)
@@ -85,7 +85,7 @@ main (int argc, char **argv) {
   init_agent("ntpsnmpd");
 
   /* Try to connect to ntpd */
-  if ( ntpq_openhost("localhost") == 0 )
+  if ( ntpq_openhost("localhost", 0) == 0 )
   {
        fprintf(stderr, "Error: Could not connect to ntpd. Aborting.\n");
        exit(1);