aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Cantrell <dcantrell@redhat.com>2008-09-26 16:29:21 -1000
committerBill Nottingham <notting@redhat.com>2009-03-19 15:50:24 -0400
commitc4c3bd478fd00208219542657a595098130a56fa (patch)
tree5be990fa700666ab52ac32d492542d8c307383f4
parente95df210a7f00e337b22044f8f71ca22683cff19 (diff)
downloadinitscripts-c4c3bd478fd00208219542657a595098130a56fa.tar
initscripts-c4c3bd478fd00208219542657a595098130a56fa.tar.gz
initscripts-c4c3bd478fd00208219542657a595098130a56fa.tar.bz2
initscripts-c4c3bd478fd00208219542657a595098130a56fa.tar.xz
initscripts-c4c3bd478fd00208219542657a595098130a56fa.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 e3a6a7f5..c7b11d77 100644
--- a/src/ipcalc.c
+++ b/src/ipcalc.c
@@ -20,7 +20,6 @@
* David Cantrell <dcantrell@redhat.com>
*/
-
#include <ctype.h>
#include <popt.h>
#include <stdio.h>
@@ -202,19 +201,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,
@@ -236,21 +236,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",
@@ -269,26 +254,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--;
@@ -360,14 +325,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;
}