From: Harlan Stenn Date: Mon, 5 Nov 2012 02:34:01 +0000 (+0000) Subject: [Bug 922] Allow interspersed -4 and -6 flags on the ntpq command line X-Git-Tag: NTP_4_2_7P317~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=31b94d5e4424c0418b8cea189b406742f67936ed;p=thirdparty%2Fntp.git [Bug 922] Allow interspersed -4 and -6 flags on the ntpq command line bk: 50972599x2vmXebreHA1yXOlSBdHDQ --- diff --git a/ChangeLog b/ChangeLog index 6ac8dce57..b872f43e5 100644 --- 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 * [Bug 2296] Update fix for Bug 2294 to handle --without-crypto. (4.2.7p315) 2012/10/26 Released by Harlan Stenn diff --git a/ntpq/libntpq.c b/ntpq/libntpq.c index fff8e81ea..becd956f2 100644 --- a/ntpq/libntpq.c +++ b/ntpq/libntpq.c @@ -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 { diff --git a/ntpq/libntpq.h b/ntpq/libntpq.h index 14bd1de48..82a874093 100644 --- a/ntpq/libntpq.h +++ b/ntpq/libntpq.h @@ -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); diff --git a/ntpq/ntpq-subs.c b/ntpq/ntpq-subs.c index 52e952b86..a299e7817 100644 --- a/ntpq/ntpq-subs.c +++ b/ntpq/ntpq-subs.c @@ -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); } diff --git a/ntpq/ntpq.c b/ntpq/ntpq.c index 4583dccbc..562fb7b1e 100644 --- a/ntpq/ntpq.c +++ b/ntpq/ntpq.c @@ -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: diff --git a/ntpq/ntpq.h b/ntpq/ntpq.h index f5d7dcd4b..ec2bcb98d 100644 --- a/ntpq/ntpq.h +++ b/ntpq/ntpq.h @@ -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 */ diff --git a/ntpsnmpd/ntpsnmpd.c b/ntpsnmpd/ntpsnmpd.c index 278d5a86e..d96ad3af4 100644 --- a/ntpsnmpd/ntpsnmpd.c +++ b/ntpsnmpd/ntpsnmpd.c @@ -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);