aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Cantrell <dcantrell@redhat.com>2008-09-26 16:29:21 -1000
committerHarald Hoyer <harald@redhat.com>2010-09-13 17:27:21 +0200
commite921ac88353159dee451f7c0094fca95a9b2bb56 (patch)
treee50ceba3914b54e00363550630cbc048e758d3ee
parent9253ed4bdddd635706a0a741259cbaa8a21d8b89 (diff)
downloadinitscripts-e921ac88353159dee451f7c0094fca95a9b2bb56.tar
initscripts-e921ac88353159dee451f7c0094fca95a9b2bb56.tar.gz
initscripts-e921ac88353159dee451f7c0094fca95a9b2bb56.tar.bz2
initscripts-e921ac88353159dee451f7c0094fca95a9b2bb56.tar.xz
initscripts-e921ac88353159dee451f7c0094fca95a9b2bb56.zip
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.
-rw-r--r--src/ipcalc.c83
1 files changed, 41 insertions, 42 deletions
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 <dcantrell@redhat.com>
*/
-
#include <ctype.h>
#include <popt.h>
#include <stdio.h>
@@ -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;
}