aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Cantrell <dcantrell@redhat.com>2008-09-26 16:03:53 -1000
committerBill Nottingham <notting@redhat.com>2009-03-19 15:50:11 -0400
commitd91005f0d149124749ad6cc98bc42b4111900963 (patch)
treec77bfb5f547dcb6e96fe984b4c583504315e3caf
parentc304852ff24bab0c13bb327336d28cd17cb995b8 (diff)
downloadinitscripts-d91005f0d149124749ad6cc98bc42b4111900963.tar
initscripts-d91005f0d149124749ad6cc98bc42b4111900963.tar.gz
initscripts-d91005f0d149124749ad6cc98bc42b4111900963.tar.bz2
initscripts-d91005f0d149124749ad6cc98bc42b4111900963.tar.xz
initscripts-d91005f0d149124749ad6cc98bc42b4111900963.zip
Add a address check capability for IPv4 and IPv6 addresses.
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.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/ipcalc.c b/src/ipcalc.c
index 23a72207..ede86145 100644
--- a/src/ipcalc.c
+++ b/src/ipcalc.c
@@ -201,6 +201,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;
@@ -209,6 +210,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,
@@ -228,6 +235,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",
@@ -246,6 +268,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--;