aboutsummaryrefslogtreecommitdiffstats
path: root/src/ipcalc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ipcalc.c')
-rw-r--r--src/ipcalc.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/ipcalc.c b/src/ipcalc.c
index ad279587..d981be83 100644
--- a/src/ipcalc.c
+++ b/src/ipcalc.c
@@ -239,13 +239,6 @@ int main(int argc, const char **argv) {
} else
prefixStr = NULL;
- if (!inet_aton(ipStr, &ip)) {
- if (!beSilent)
- fprintf(stderr, "ipcalc: bad ip address: %s\n",
- ipStr);
- return 1;
- }
-
if (prefixStr != NULL) {
prefix = atoi(prefixStr);
if (prefix == 0) {
@@ -264,6 +257,12 @@ int main(int argc, const char **argv) {
poptPrintHelp(optCon, stderr, 0);
}
return 1;
+ } else if (netmaskStr && prefix != 0) {
+ if (!beSilent) {
+ fprintf(stderr, "ipcalc: both netmask and prefix specified\n");
+ poptPrintHelp(optCon, stderr, 0);
+ }
+ return 1;
} else if (netmaskStr) {
if (!inet_aton(netmaskStr, &netmask)) {
if (!beSilent)
@@ -283,6 +282,26 @@ int main(int argc, const char **argv) {
return 1;
}
+ /* Handle CIDR entries such as 172/8 */
+ if (prefix) {
+ char *tmp = ipStr;
+ int i;
+
+ for(i=3; i> 0; i--) {
+ tmp = strchr(tmp,'.');
+ if (!tmp)
+ break;
+ else
+ tmp++;
+ }
+ tmp = NULL;
+ for (; i>0; i--) {
+ tmp = malloc(strlen(ipStr + 3));
+ sprintf(tmp,"%s.0",ipStr);
+ ipStr = tmp;
+ }
+ }
+
if (!inet_aton(ipStr, (struct in_addr *) &ip)) {
if (!beSilent)
fprintf(stderr, "ipcalc: bad ip address: %s\n", ipStr);