Skip to content

Commit 472d60a

Browse files
author
Nikos Mavrogiannopoulos
committed
Assume a classless IPv4 address space by default
Introduce the --class-prefix option to preserve compatibility with previous versions. Otherwise, if not specified, the default IPv4 prefix is /32.
1 parent 178fb70 commit 472d60a

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ clean:
2929
rm -f ipcalc
3030

3131
check: ipcalc
32-
./ipcalc -bmnp 12.15.1.5 > out.tmp && cmp out.tmp tests/12.15.1.5
33-
./ipcalc -bmnp 129.15.31.5 > out.tmp && cmp out.tmp tests/129.15.31.5
34-
./ipcalc -bmnp 193.92.31.0 > out.tmp && cmp out.tmp tests/193.92.31.0
32+
./ipcalc -bmnp 12.15.1.5 --class-prefix > out.tmp && cmp out.tmp tests/12.15.1.5
33+
./ipcalc -bmnp 129.15.31.5 --class-prefix > out.tmp && cmp out.tmp tests/129.15.31.5
34+
./ipcalc -bmnp 193.92.31.0 --class-prefix > out.tmp && cmp out.tmp tests/193.92.31.0
3535
./ipcalc -bmnp 192.168.1.5/31 > out.tmp && cmp out.tmp tests/192.168.1.5-31
3636
./ipcalc -bmnp 10.10.10.5/24 > out.tmp && cmp out.tmp tests/192.168.1.5-24
3737
./ipcalc -bmnp 10.100.4.1/30 > out.tmp && cmp out.tmp tests/192.168.1.5-30

ipcalc-tests

+4-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ TestOutput "$bin -p 192.168.1.1 255.255.255.255" "PREFIX=32"
8989
TestOutput "$bin -p 192.168.1.1 0.0.0.0" "PREFIX=0"
9090
TestOutput "$bin -p 172.16.59.222 255.255.252.0" "PREFIX=22"
9191
TestOutput "$bin -m 172.16.59.222/22" "NETMASK=255.255.252.0"
92-
TestOutput "$bin -m 192.168.1.1" "NETMASK=255.255.255.0"
93-
TestOutput "$bin -m 10.1.2.3" "NETMASK=255.0.0.0"
94-
TestOutput "$bin -m 129.22.4.3" "NETMASK=255.255.0.0"
92+
TestOutput "$bin --class-prefix -m 192.168.1.1" "NETMASK=255.255.255.0"
93+
TestOutput "$bin -m 192.168.1.1" "NETMASK=255.255.255.255"
94+
TestOutput "$bin --class-prefix -m 10.1.2.3" "NETMASK=255.0.0.0"
95+
TestOutput "$bin --class-prefix -m 129.22.4.3" "NETMASK=255.255.0.0"
9596
TestOutput "$bin -n 192.168.1.1/32" "NETWORK=192.168.1.1"
9697
TestOutput "$bin -n 192.168.1.1/0" "NETWORK=0.0.0.0"
9798
TestOutput "$bin --reverse-dns 193.92.150.3/32" "REVERSEDNS=3.150.92.193.in-addr.arpa."

ipcalc.c

+19-6
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,10 @@ unsigned default_ipv4_prefix(struct in_addr net)
569569
#define FLAG_SHOW_GEOIP ((1<<15)|FLAG_GET_GEOIP)
570570
#define FLAG_SHOW_ALL_INFO ((1<<16)|FLAG_SHOW_INFO)
571571
#define FLAG_SHOW_REVERSE (1<<17)
572+
#define FLAG_ASSUME_CLASS_PREFIX (1<<18)
572573

573-
#define FLAGS_TO_IGNORE (FLAG_GET_GEOIP|(1<<16))
574+
/* Flags that are not real options */
575+
#define FLAGS_TO_IGNORE (FLAG_GET_GEOIP|FLAG_ASSUME_CLASS_PREFIX|(1<<16))
574576
#define FLAGS_TO_IGNORE_MASK (~FLAGS_TO_IGNORE)
575577

576578
int get_ipv4_info(const char *ipStr, int prefix, ip_info_st * info,
@@ -613,8 +615,11 @@ int get_ipv4_info(const char *ipStr, int prefix, ip_info_st * info,
613615
}
614616
ipStr = tmp;
615617
}
616-
} else { /* assume good old days classful Internet */
617-
prefix = default_ipv4_prefix(ip);
618+
} else { /* assume good old days classful Internet */
619+
if (flags & FLAG_ASSUME_CLASS_PREFIX)
620+
prefix = default_ipv4_prefix(ip);
621+
else
622+
prefix = 32;
618623
}
619624

620625
if (prefix > 32) {
@@ -1033,6 +1038,8 @@ int str_to_prefix(int *ipv6, const char *prefixStr, unsigned fix)
10331038
#define OPT_ADDRSPACE 5
10341039
#define OPT_USAGE 6
10351040
#define OPT_REVERSE 7
1041+
#define OPT_CLASS_PREFIX 8
1042+
10361043
static const struct option long_options[] = {
10371044
{"check", 0, 0, 'c'},
10381045
{"random-private", 1, 0, 'r'},
@@ -1050,6 +1057,7 @@ static const struct option long_options[] = {
10501057
{"netmask", 0, 0, 'm'},
10511058
{"network", 0, 0, 'n'},
10521059
{"prefix", 0, 0, 'p'},
1060+
{"class-prefix", 0, 0, OPT_CLASS_PREFIX},
10531061
{"minaddr", 0, 0, OPT_MINADDR},
10541062
{"maxaddr", 0, 0, OPT_MAXADDR},
10551063
{"addresses", 0, 0, OPT_ADDRESSES},
@@ -1095,6 +1103,8 @@ void usage(unsigned verbose)
10951103
#endif
10961104
fprintf(stderr, "\n");
10971105
fprintf(stderr, "Other options:\n");
1106+
fprintf(stderr, " --class-prefix When specified the default prefix will be determined\n");
1107+
fprintf(stderr, " by the IPv4 address class\n");
10981108
fprintf(stderr, " -s, --silent Don't ever display error messages\n");
10991109
fprintf(stderr, " -v, --version Display program version\n");
11001110
fprintf(stderr, " -?, --help Show this help message\n");
@@ -1105,7 +1115,7 @@ void usage(unsigned verbose)
11051115
fprintf(stderr, " [-h|--hostname] [-o|--lookup-host=STRING] [-g|--geoinfo]\n");
11061116
fprintf(stderr, " [-m|--netmask] [-n|--network] [-p|--prefix] [--minaddr] [--maxaddr]\n");
11071117
fprintf(stderr, " [--addresses] [--addrspace] [-s|--silent] [-v|--version]\n");
1108-
fprintf(stderr, " [--reverse-dns]\n");
1118+
fprintf(stderr, " [--reverse-dns] [--class-prefix]\n");
11091119
fprintf(stderr, " [-?|--help] [--usage]\n");
11101120
}
11111121
}
@@ -1186,6 +1196,9 @@ int main(int argc, char **argv)
11861196
case OPT_ALLINFO:
11871197
flags |= FLAG_SHOW_ALL_INFO;
11881198
break;
1199+
case OPT_CLASS_PREFIX:
1200+
flags |= FLAG_ASSUME_CLASS_PREFIX;
1201+
break;
11891202
case OPT_REVERSE:
11901203
flags |= FLAG_SHOW_REVERSE;
11911204
break;
@@ -1449,9 +1462,9 @@ int main(int argc, char **argv)
14491462
default_printf("Hosts/Net:\t", "%s\n", info.hosts);
14501463
} else {
14511464
if (info.type)
1452-
default_printf("Address space:\t", "%s\n", info.type);
1465+
dist_printf("Address space:\t", "%s\n", info.type);
14531466
if (info.class)
1454-
default_printf("Address class:\t", "%s\n", info.class);
1467+
dist_printf("Address class:\t", "%s\n", info.class);
14551468

14561469
}
14571470

0 commit comments

Comments
 (0)