diff options
author | David Cantrell <dcantrell@redhat.com> | 2008-09-26 16:03:53 -1000 |
---|---|---|
committer | Harald Hoyer <harald@redhat.com> | 2009-05-04 16:05:05 +0200 |
commit | eca23923643c99cca861abbd924efbb996849ea4 (patch) | |
tree | 7ba23f516c9d288019a0eaee1b3c7b2c4620b196 | |
parent | 7e7689a8c0300fd76fd97613d2c49d06444cfa72 (diff) | |
download | initscripts-eca23923643c99cca861abbd924efbb996849ea4.tar initscripts-eca23923643c99cca861abbd924efbb996849ea4.tar.gz initscripts-eca23923643c99cca861abbd924efbb996849ea4.tar.bz2 initscripts-eca23923643c99cca861abbd924efbb996849ea4.tar.xz initscripts-eca23923643c99cca861abbd924efbb996849ea4.zip |
Add a address check capability for IPv4 and IPv6 addresses. (#464268)
Add the -c, --check argument which enables address validation. The
ipcalc program will exit with 0 if the address is valid or 1 if it
is invalid, for the specified family.
Add the -4, --ipv4 argument to specify the IPv4 address family.
Add the -6, --ipv6 argument to specify the IPv6 address family.
Ensure that the IPv6 family argument is invalid with the IPv4-only
show arguments (broadcast, network, netmask, prefix).
-rw-r--r-- | src/ipcalc.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/ipcalc.c b/src/ipcalc.c index 648e153c..a9efa384 100644 --- a/src/ipcalc.c +++ b/src/ipcalc.c @@ -195,6 +195,7 @@ 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 rc; poptContext optCon; char *ipStr, *prefixStr, *netmaskStr, *hostName, *chptr; @@ -203,6 +204,12 @@ int main(int argc, const char **argv) { 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", }, + { "ipv6", '6', 0, &familyIPv6, 0, + "IPv6 address family", }, { "broadcast", 'b', 0, &showBroadcast, 0, "Display calculated broadcast address", }, { "hostname", 'h', 0, &showHostname, 0, @@ -222,6 +229,21 @@ 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", @@ -240,6 +262,26 @@ 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--; |