From e921ac88353159dee451f7c0094fca95a9b2bb56 Mon Sep 17 00:00:00 2001 From: David Cantrell Date: Fri, 26 Sep 2008 16:29:21 -1000 Subject: Correct address family option handling in ipcalc. Make sure the default is IPv4. If the check option is specified, make sure we just validate the address and return 1 or 0. --- src/ipcalc.c | 83 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 41 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/ipcalc.c b/src/ipcalc.c index 0727cea5..c7ede522 100644 --- a/src/ipcalc.c +++ b/src/ipcalc.c @@ -14,7 +14,6 @@ * David Cantrell */ - #include #include #include @@ -196,19 +195,20 @@ int main(int argc, const char **argv) { int showBroadcast = 0, showPrefix = 0, showNetwork = 0; int showHostname = 0, showNetmask = 0; int beSilent = 0; - int doCheck = 0, familyIPv4 = 1, familyIPv6 = 0; + int doCheck = 0, familyIPv4 = 0, familyIPv6 = 0; int rc; poptContext optCon; char *ipStr, *prefixStr, *netmaskStr, *hostName, *chptr; char namebuf[INET6_ADDRSTRLEN+1]; struct in_addr ip, netmask, network, broadcast; + struct in6_addr ip6; int prefix = 0; char errBuf[250]; struct poptOption optionsTable[] = { { "check", 'c', 0, &doCheck, 0, "Validate IP address for specified address family", }, { "ipv4", '4', 0, &familyIPv4, 0, - "IPv4 address family", }, + "IPv4 address family (default)", }, { "ipv6", '6', 0, &familyIPv6, 0, "IPv6 address family", }, { "broadcast", 'b', 0, &showBroadcast, 0, @@ -230,21 +230,6 @@ int main(int argc, const char **argv) { optCon = poptGetContext("ipcalc", argc, argv, optionsTable, 0); poptReadDefaultConfig(optCon, 1); - if (familyIPv4 && familyIPv6) { - if (!beSilent) { - fprintf(stderr, "ipcalc: cannot specify both address families\n"); - } - return 1; - } - - if (familyIPv6 && - (showBroadcast || showNetmask || showNetwork || showPrefix)) { - if (!beSilent) { - fprintf(stderr, "ipcalc: unable to show setting for IPv6\n"); - } - return 1; - } - if ((rc = poptGetNextOpt(optCon)) < -1) { if (!beSilent) { fprintf(stderr, "ipcalc: bad argument %s: %s\n", @@ -263,26 +248,6 @@ int main(int argc, const char **argv) { return 1; } - if (doCheck) { - if (familyIPv4) { - struct in_addr tmpaddr; - memset(&tmpaddr, 0, sizeof(tmpaddr)); - - if (inet_pton(AF_INET, ipStr, &tmpaddr) >= 1) - return 0; - else - return 1; - } else if (familyIPv6) { - struct in6_addr tmpaddr; - memset(&tmpaddr, 0, sizeof(tmpaddr)); - - if (inet_pton(AF_INET6, ipStr, &tmpaddr) >= 1) - return 0; - else - return 1; - } - } - if (strchr(ipStr,'/') != NULL) { prefixStr = strchr(ipStr, '/') + 1; prefixStr--; @@ -354,14 +319,48 @@ int main(int argc, const char **argv) { } } - if (inet_pton(AF_INET, ipStr, &ip) <= 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad ip address: %s\n", ipStr); + if (!familyIPv4 && !familyIPv6) + familyIPv4 = 1; + + if (familyIPv4 && familyIPv6) { + if (!beSilent) { + fprintf(stderr, "ipcalc: cannot specify both address families\n"); + } return 1; } + if (familyIPv4) { + if (inet_pton(AF_INET, ipStr, &ip) <= 0) { + if (!beSilent) + fprintf(stderr, "ipcalc: bad IPv4 address: %s\n", ipStr); + return 1; + } else { + if (doCheck) + return 0; + } + } + + if (familyIPv6) { + if (inet_pton(AF_INET6, ipStr, &ip6) <= 0) { + if (!beSilent) + fprintf(stderr, "ipcalc: bad IPv6 address: %s\n", ipStr); + return 1; + } else { + if (doCheck) + return 0; + } + } + + if (familyIPv6 && + (showBroadcast || showNetmask || showNetwork || showPrefix)) { + if (!beSilent) { + fprintf(stderr, "ipcalc: unable to show setting for IPv6\n"); + } + return 1; + } - if (!(showNetmask|showPrefix|showBroadcast|showNetwork|showHostname)) { + if (familyIPv4 && + !(showNetmask|showPrefix|showBroadcast|showNetwork|showHostname)) { poptPrintHelp(optCon, stderr, 0); return 1; } -- cgit v1.2.1