From: John Hay Date: Sat, 3 Aug 2002 17:50:25 +0000 (+0200) Subject: Add address family selection to ntpdc and ntpq. The -4|-6 overrides can be used X-Git-Tag: NTP_4_1_73~84 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bb1b39d62ae166ab12787b2992d7d35ae09c3d9e;p=thirdparty%2Fntp.git Add address family selection to ntpdc and ntpq. The -4|-6 overrides can be used on the commandline, the host command and all the places in ntpdc that use dns names or addresses in their arguments. bk: 3d4c17e1UK1USd9aF6ZeGYiCTG4kZg --- diff --git a/ntpdc/ntpdc.c b/ntpdc/ntpdc.c index 87e5126a84..683fd62571 100644 --- a/ntpdc/ntpdc.c +++ b/ntpdc/ntpdc.c @@ -107,8 +107,8 @@ static struct xcmd builtins[] = { { "delay", my_delay, { OPT|INT, NO, NO, NO }, { "msec", "", "", "" }, "set the delay added to encryption time stamps" }, - { "host", host, { OPT|NTP_STR, NO, NO, NO }, - { "hostname", "", "", "" }, + { "host", host, { OPT|NTP_STR, OPT|NTP_STR, NO, NO }, + { "-4|-6", "hostname", "", "" }, "specify the host whose NTP server we talk to" }, { "passwd", passwd, { OPT|NTP_STR, NO, NO, NO }, { "", "", "", "" }, @@ -150,7 +150,9 @@ static struct xcmd builtins[] = { #define MAXCMDS 100 /* maximum commands on cmd line */ #define MAXHOSTS 200 /* maximum hosts on cmd line */ #define MAXLINE 512 /* maximum line length */ -#define MAXTOKENS (1+MAXARGS+2) /* maximum number of usable tokens */ +#define MAXTOKENS (1+1+MAXARGS+2) /* maximum number of usable tokens */ + /* command + -4|-6 + MAXARGS + */ + /* redirection */ /* * Some variables used and manipulated locally @@ -161,6 +163,7 @@ static l_fp delay_time; /* delay time */ static char currenthost[LENHOSTNAME]; /* current host name */ static int showhostnames = 1; /* show host names by default */ +static int ai_fam_templ; /* address family */ static int sockfd; /* fd socket is opened on */ static int havehost = 0; /* set to 1 when host open */ int s_port = 0; @@ -296,8 +299,14 @@ ntpdcmain( #endif progname = argv[0]; - while ((c = ntp_getopt(argc, argv, "c:dilnps")) != EOF) + while ((c = ntp_getopt(argc, argv, "46c:dilnps")) != EOF) switch (c) { + case '4': + ai_fam_templ = AF_INET; + break; + case '6': + ai_fam_templ = AF_INET6; + break; case 'c': ADDCMD(ntp_optarg); break; @@ -325,7 +334,7 @@ ntpdcmain( } if (errflg) { (void) fprintf(stderr, - "usage: %s [-dilnps] [-c cmd] host ...\n", + "usage: %s [-46dilnps] [-c cmd] host ...\n", progname); exit(2); } @@ -404,6 +413,7 @@ openhost( memset((char *)&hints, 0, sizeof(struct addrinfo)); hints.ai_flags = AI_ADDRCONFIG|AI_CANONNAME; + hints.ai_family = ai_fam_templ; hints.ai_protocol = IPPROTO_UDP; hints.ai_socktype = SOCK_DGRAM; @@ -1067,9 +1077,11 @@ docmd( char *tokens[1+MAXARGS+2]; struct parse pcmd; int ntok; - static int i; + int i, ti; + int rval; struct xcmd *xcmd; + ai_fam_templ = 0; /* * Tokenize the command line. If nothing on it, return. */ @@ -1097,22 +1109,29 @@ docmd( */ pcmd.keyword = tokens[0]; pcmd.nargs = 0; - for (i = 0; i < MAXARGS && xcmd->arg[i] != NO; i++) { - if ((i+1) >= ntok) { + ti = 1; + for (i = 0; i < MAXARGS && xcmd->arg[i] != NO;) { + if ((i+ti) >= ntok) { if (!(xcmd->arg[i] & OPT)) { printusage(xcmd, stderr); return; } break; } - if ((xcmd->arg[i] & OPT) && (*tokens[i+1] == '>')) - break; - if (!getarg(tokens[i+1], (int)xcmd->arg[i], &pcmd.argval[i])) - return; + if ((xcmd->arg[i] & OPT) && (*tokens[i+ti] == '>')) + break; + rval = getarg(tokens[i+ti], (int)xcmd->arg[i], &pcmd.argval[i]); + if (rval == -1) { + ti++; + continue; + } + if (rval == 0) + return; pcmd.nargs++; + i++; } - i++; + i += ti; if (i < ntok && *tokens[i] == '>') { char *fname; @@ -1247,8 +1266,12 @@ findcmd( } - /* +/* * getarg - interpret an argument token + * + * return: 0 - failure + * 1 - success + * -1 - skip to next token */ static int getarg( @@ -1266,6 +1289,13 @@ getarg( argp->string = str; break; case ADD: + if (!strcmp("-6", str)) { + ai_fam_templ = AF_INET6; + return -1; + } else if (!strcmp("-4", str)) { + ai_fam_templ = AF_INET; + return -1; + } if (!getnetnum(str, &(argp->netnum), (char *)0, 0)) { return 0; } @@ -1300,16 +1330,15 @@ getarg( argp->ival = -argp->ival; } break; - case IP_VERSION: - if (*str) { - if (*str == '6') /* we want the v6 version*/ - argp->ival = 6 ; - else if (*str == '4') - argp->ival = 4 ; - else{ - (void) fprintf(stderr, "***Version must be either 4 or 6\n"); - return 0; - } + case IP_VERSION: + if (!strcmp("-6", str)) + argp->ival = 6 ; + else if (!strcmp("-4", str)) + argp->ival = 4 ; + else { + (void) fprintf(stderr, + "***Version must be either 4 or 6\n"); + return 0; } break; } @@ -1488,10 +1517,15 @@ printusage( FILE *fp ) { - register int i; + int i, opt46; + opt46 = 0; (void) fprintf(fp, "usage: %s", xcp->keyword); for (i = 0; i < MAXARGS && xcp->arg[i] != NO; i++) { + if (opt46 == 0 && (xcp->arg[i] & ~OPT) == ADD) { + (void) fprintf(fp, " [ -4|-6 ]"); + opt46 = 1; + } if (xcp->arg[i] & OPT) (void) fprintf(fp, " [ %s ]", xcp->desc[i]); else @@ -1566,12 +1600,33 @@ host( FILE *fp ) { + int i; + if (pcmd->nargs == 0) { if (havehost) (void) fprintf(fp, "current host is %s\n", currenthost); else (void) fprintf(fp, "no current host\n"); - } else if (openhost(pcmd->argval[0].string)) { + return; + } + + i = 0; + if (pcmd->nargs == 2) { + if (!strcmp("-4", pcmd->argval[i].string)) + ai_fam_templ = AF_INET; + else if (!strcmp("-6", pcmd->argval[i].string)) + ai_fam_templ = AF_INET6; + else { + if (havehost) + (void) fprintf(fp, + "current host remains %s\n", currenthost); + else + (void) fprintf(fp, "still no current host\n"); + return; + } + i = 1; + } + if (openhost(pcmd->argval[i].string)) { (void) fprintf(fp, "current host set to %s\n", currenthost); } else { if (havehost) diff --git a/ntpdc/ntpdc_ops.c b/ntpdc/ntpdc_ops.c index 5870c5792c..7ceb16c0f3 100644 --- a/ntpdc/ntpdc_ops.c +++ b/ntpdc/ntpdc_ops.c @@ -84,13 +84,13 @@ static void kerninfo P((struct parse *, FILE *)); */ struct xcmd opcmds[] = { { "listpeers", peerlist, { OPT|IP_VERSION, NO, NO, NO }, - { "6|4", "", "", "" }, + { "-4|-6", "", "", "" }, "display list of peers the server knows about [IP Version]" }, { "peers", peers, { OPT|IP_VERSION, NO, NO, NO }, - { "6|4", "", "", "" }, + { "-4|-6", "", "", "" }, "display peer summary information [IP Version]" }, { "dmpeers", dmpeers, { OPT|IP_VERSION, NO, NO, NO }, - { "6|4", "", "", "" }, + { "-4|-6", "", "", "" }, "display peer summary info the way Dave Mills likes it (IP Version)" }, { "showpeer", showpeer, { ADD, OPT|ADD, OPT|ADD, OPT|ADD}, { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" }, @@ -138,7 +138,7 @@ struct xcmd opcmds[] = { { "auth|bclient|monitor|pll|kernel|stats", "...", "...", "..." }, "clear a system flag (auth, bclient, monitor, pll, kernel, stats)" }, { "reslist", reslist, {OPT|IP_VERSION, NO, NO, NO }, - { "6|4", "", "", "" }, + { "-4|-6", "", "", "" }, "display the server's restrict list" }, { "restrict", new_restrict, { ADD, ADD, NTP_STR, OPT|NTP_STR }, { "address", "mask", diff --git a/ntpq/ntpq.c b/ntpq/ntpq.c index 228f364607..d8b7bc2201 100644 --- a/ntpq/ntpq.c +++ b/ntpq/ntpq.c @@ -292,8 +292,8 @@ struct xcmd builtins[] = { { "delay", auth_delay, { OPT|INT, NO, NO, NO }, { "msec", "", "", "" }, "set the delay added to encryption time stamps" }, - { "host", host, { OPT|STR, NO, NO, NO }, - { "hostname", "", "", "" }, + { "host", host, { OPT|STR, OPT|STR, NO, NO }, + { "-4|-6", "hostname", "", "" }, "specify the host whose NTP server we talk to" }, { "poll", ntp_poll, { OPT|UINT, OPT|STR, NO, NO }, { "n", "verbose", "", "" }, @@ -365,6 +365,7 @@ char currenthost[LENHOSTNAME]; /* current host name */ struct sockaddr_in hostaddr = { 0 }; /* host address */ int showhostnames = 1; /* show host names by default */ +int ai_fam_templ; /* address family */ int sockfd; /* fd socket is opened on */ int havehost = 0; /* set to 1 when host open */ int s_port = 0; @@ -498,8 +499,14 @@ ntpqmain( delay_time.l_uf = DEFDELAY; progname = argv[0]; - while ((c = ntp_getopt(argc, argv, "c:dinp")) != EOF) + while ((c = ntp_getopt(argc, argv, "46c:dinp")) != EOF) switch (c) { + case '4': + ai_fam_templ = AF_INET; + break; + case '6': + ai_fam_templ = AF_INET6; + break; case 'c': ADDCMD(ntp_optarg); break; @@ -521,7 +528,7 @@ ntpqmain( } if (errflg) { (void) fprintf(stderr, - "usage: %s [-dinp] [-c cmd] host ...\n", + "usage: %s [-46dinp] [-c cmd] host ...\n", progname); exit(2); } @@ -587,6 +594,7 @@ openhost( memset((char *)&hints, 0, sizeof(struct addrinfo)); hints.ai_flags = AI_ADDRCONFIG|AI_CANONNAME; + hints.ai_family = ai_fam_templ; hints.ai_protocol = IPPROTO_UDP; hints.ai_socktype = SOCK_DGRAM; @@ -1609,15 +1617,14 @@ getarg( } break; case IP_VERSION: - if (*str) { - if (*str == '6') /* We want the v6 version */ - argp->ival = 6; - else if (*str == '4') /* Gimme the IPV4 */ - argp->ival = 4; - else { - (void) fprintf(stderr, "***Version must be either 4 or 6\n"); - return 0; - } + if (!strcmp("-6", str)) + argp->ival = 6 ; + else if (!strcmp("-4", str)) + argp->ival = 4 ; + else { + (void) fprintf(stderr, + "***Version must be either 4 or 6\n"); + return 0; } break; } @@ -2135,12 +2142,34 @@ host( FILE *fp ) { + int i; + if (pcmd->nargs == 0) { if (havehost) (void) fprintf(fp, "current host is %s\n", currenthost); else (void) fprintf(fp, "no current host\n"); - } else if (openhost(pcmd->argval[0].string)) { + return; + } + + i = 0; + ai_fam_templ = 0; + if (pcmd->nargs == 2) { + if (!strcmp("-4", pcmd->argval[i].string)) + ai_fam_templ = AF_INET; + else if (!strcmp("-6", pcmd->argval[i].string)) + ai_fam_templ = AF_INET6; + else { + if (havehost) + (void) fprintf(fp, + "current host remains %s\n", currenthost); + else + (void) fprintf(fp, "still no current host\n"); + return; + } + i = 1; + } + if (openhost(pcmd->argval[i].string)) { (void) fprintf(fp, "current host set to %s\n", currenthost); numassoc = 0; } else { diff --git a/ntpq/ntpq_ops.c b/ntpq/ntpq_ops.c index c76160ea18..dcc61931a4 100644 --- a/ntpq/ntpq_ops.c +++ b/ntpq/ntpq_ops.c @@ -140,19 +140,19 @@ struct xcmd opcmds[] = { { "assocID", "", "", "" }, "print status information returned for a peer" }, { "peers", peers, { OPT|IP_VERSION, NO, NO, NO }, - { "6|4", "", "", "" }, + { "-4|-6", "", "", "" }, "obtain and print a list of the server's peers [IP version]" }, { "lpeers", lpeers, { OPT|IP_VERSION, NO, NO, NO }, - { "6|4", "", "", "" }, + { "-4|-6", "", "", "" }, "obtain and print a list of all peers and clients [IP version]" }, { "opeers", opeers, { OPT|IP_VERSION, NO, NO, NO }, - { "6|4", "", "", "" }, + { "-4|-6", "", "", "" }, "print peer list the old way, with dstadr shown rather than refid [IP version]" }, - { "lopeers", lopeers, { OPT|IP_VERSION, NO, NO, NO }, - { "6|4", "", "", "" }, + { "lopeers", lopeers, { OPT|IP_VERSION, NO, NO, NO }, + { "-4|-6", "", "", "" }, "obtain and print a list of all peers and clients showing dstadr [IP version]" }, { 0, 0, { NO, NO, NO, NO }, - { "6|4", "", "", "" }, "" } + { "-4|-6", "", "", "" }, "" } };