]>
git.ipfire.org Git - network.git/blob - src/inetcalc.c
1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2015 IPFire Network Development Team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
22 #include <arpa/inet.h>
25 #include <netinet/in.h>
29 #include <sys/socket.h>
31 typedef struct ip_address
{
37 static struct in6_addr
prefix_to_bitmask(int prefix
) {
38 assert(prefix
<= 128);
40 struct in6_addr bitmask
;
42 for (int i
= 0; i
< 16; i
++)
43 bitmask
.s6_addr
[i
] = 0;
45 for (int i
= prefix
, j
= 0; i
> 0; i
-= 8, j
++) {
47 bitmask
.s6_addr
[j
] = 0xff;
49 bitmask
.s6_addr
[j
] = 0xff << (8 - i
);
55 static int bitmask_to_prefix(uint32_t bits
) {
58 // Count all ones until we find the first zero
59 while (bits
& (1 << 31)) {
64 // The remaining bits must all be zero
71 static int ip_address_parse_subnet_mask(ip_address_t
* ip
, const char* prefix
) {
74 int r
= inet_pton(AF_INET
, prefix
, &mask
.s_addr
);
78 uint32_t bits
= ntohl(mask
.s_addr
);
79 ip
->prefix
= bitmask_to_prefix(bits
);
81 return (ip
->prefix
< 0 || ip
->prefix
> 32);
84 static int ip_address_parse_prefix_cidr(ip_address_t
* ip
, const int family
, const char* prefix
) {
89 if (p
>= '0' && p
<= '9') {
91 ip
->prefix
+= p
- '0';
99 return (ip
->prefix
< 0 || ip
->prefix
> 128);
102 return (ip
->prefix
< 0 || ip
->prefix
> 32);
109 static int ip_address_parse_prefix(ip_address_t
* ip
, const int family
, const char* prefix
) {
110 int r
= ip_address_parse_prefix_cidr(ip
, family
, prefix
);
112 if (r
&& family
== AF_INET
) {
113 r
= ip_address_parse_subnet_mask(ip
, prefix
);
119 static int ip_address_parse_simple(ip_address_t
* ip
, const int family
, const char* address
) {
120 assert(family
== AF_INET
|| family
== AF_INET6
);
122 size_t address_length
= strlen(address
);
123 char buffer
[address_length
+ 1];
124 strncpy(buffer
, address
, sizeof(buffer
));
126 // Search for a prefix or subnet mask
127 char* prefix
= strchr(buffer
, '/');
129 buffer
[prefix
- buffer
] = '\0';
133 memset(&ip
->addr
, 0, sizeof(ip
->addr
));
134 int r
= inet_pton(family
, buffer
, &ip
->addr
);
137 // If parsing the IP address failed, we will return false
141 // If the IP address could be successfully parsed, we will
142 // save the address family and return true
153 r
= ip_address_parse_prefix(ip
, family
, prefix
);
160 static int ip_address_parse(ip_address_t
* ip
, const int family
, const char* address
) {
161 static int families
[] = { AF_INET
, AF_INET6
, 0 };
166 if (family
== AF_UNSPEC
|| family
== *f
) {
167 r
= ip_address_parse_simple(ip
, *f
, address
);
179 static int ip_address_eq(const ip_address_t
* a1
, const ip_address_t
* a2
) {
180 if (a1
->family
!= a2
->family
)
183 if (a1
->addr
.s6_addr
!= a2
->addr
.s6_addr
)
186 if (a1
->prefix
!= a2
->prefix
)
192 static int ip_address_gt(const ip_address_t
* a1
, const ip_address_t
* a2
) {
193 if (a1
->family
!= a2
->family
|| a1
->prefix
!= a2
->prefix
)
196 if (a1
->addr
.s6_addr
> a2
->addr
.s6_addr
)
202 static int ip_address_format_string(char* buffer
, size_t size
, const ip_address_t
* ip
) {
203 assert(ip
->family
== AF_INET
|| ip
->family
== AF_INET6
);
205 const char* p
= inet_ntop(ip
->family
, &ip
->addr
.s6_addr
, buffer
, size
);
212 static void ip_address_print(const ip_address_t
* ip
) {
213 char buffer
[INET6_ADDRSTRLEN
+4];
215 int r
= ip_address_format_string(buffer
, sizeof(buffer
), ip
);
219 if (ip
->prefix
>= 0) {
220 size_t len
= strlen(buffer
);
221 snprintf(buffer
+ len
, sizeof(buffer
) - len
, "/%d", ip
->prefix
);
224 printf("%s\n", buffer
);
227 static void ip_address_make_network(ip_address_t
* net
, const ip_address_t
* ip
) {
228 assert(ip
->prefix
>= 0);
230 struct in6_addr mask
= prefix_to_bitmask(ip
->prefix
);
232 net
->family
= ip
->family
;
233 net
->prefix
= ip
->prefix
;
235 for (int i
= 0; i
< 16; i
++)
236 net
->addr
.s6_addr
[i
] = ip
->addr
.s6_addr
[i
] & mask
.s6_addr
[i
];
239 static void ip_address_make_broadcast(ip_address_t
* broadcast
, const ip_address_t
* ip
) {
240 assert(ip
->family
== AF_INET
&& ip
->prefix
>= 0);
242 struct in6_addr mask
= prefix_to_bitmask(ip
->prefix
);
244 broadcast
->family
= ip
->family
;
245 broadcast
->prefix
= ip
->prefix
;
247 for (int i
= 0; i
< 16; i
++)
248 broadcast
->addr
.s6_addr
[i
] = ip
->addr
.s6_addr
[i
] | ~mask
.s6_addr
[i
];
251 static int action_check(const int family
, const char* address
) {
254 int r
= ip_address_parse(&ip
, family
, address
);
259 return (ip
.prefix
>= 0);
262 static int action_equal(const int family
, const char* addr1
, const char* addr2
) {
267 r
= ip_address_parse(&a1
, family
, addr1
);
271 r
= ip_address_parse(&a2
, family
, addr2
);
275 return ip_address_eq(&a1
, &a2
);
278 static int action_greater(const int family
, const char* addr1
, const char* addr2
) {
283 r
= ip_address_parse(&a1
, family
, addr1
);
287 r
= ip_address_parse(&a2
, family
, addr2
);
291 return ip_address_gt(&a1
, &a2
);
294 static int action_format(const int family
, const char* address
) {
297 int r
= ip_address_parse(&ip
, family
, address
);
301 ip_address_print(&ip
);
305 static int action_broadcast(const int family
, const char* address
) {
307 int r
= ip_address_parse(&ip
, family
, address
);
309 fprintf(stderr
, "Invalid IP address: %s\n", address
);
313 if (ip
.family
!= AF_INET
) {
314 fprintf(stderr
, "This is only possible for IPv4\n");
318 ip_address_t broadcast
;
319 ip_address_make_broadcast(&broadcast
, &ip
);
321 ip_address_print(&broadcast
);
325 static int action_network(const int family
, const char* address
) {
328 int r
= ip_address_parse(&ip
, family
, address
);
330 fprintf(stderr
, "Invalid IP address: %s\n", address
);
334 ip_address_t network
;
335 ip_address_make_network(&network
, &ip
);
337 ip_address_print(&network
);
341 static int action_prefix(const int family
, const char* addr1
, const char* addr2
) {
344 ip_address_t network
;
345 r
= ip_address_parse(&network
, family
, addr1
);
349 ip_address_t broadcast
;
350 r
= ip_address_parse(&broadcast
, family
, addr2
);
354 r
= ip_address_gt(&broadcast
, &network
);
358 struct in6_addr netmask
;
359 for (int i
= 0; i
< 16; i
++)
360 netmask
.s6_addr
[i
] = network
.addr
.s6_addr
[i
] ^ broadcast
.addr
.s6_addr
[i
];
362 uint32_t mask
= netmask
.s6_addr
[0] << 24 | netmask
.s6_addr
[1] << 16 |
363 netmask
.s6_addr
[2] << 8 | netmask
.s6_addr
[3];
365 int prefix
= bitmask_to_prefix(~mask
);
369 printf("%d\n", prefix
);
384 static void set_action(int* action
, int what
) {
385 if (*action
!= AC_UNSPEC
) {
386 printf("Another action has already been selected\n");
393 static struct option long_options
[] = {
394 {"broadcast", no_argument
, 0, 'b'},
395 {"check", no_argument
, 0, 'c'},
396 {"equal", no_argument
, 0, 'e'},
397 {"format", no_argument
, 0, 'f'},
398 {"greater", no_argument
, 0, 'g'},
399 {"ipv4-only", no_argument
, 0, '4'},
400 {"ipv6-only", no_argument
, 0, '6'},
401 {"network", no_argument
, 0, 'n'},
402 {"prefix", no_argument
, 0, 'p'},
403 {"verbose", no_argument
, 0, 'v'},
407 int main(int argc
, char** argv
) {
408 int option_index
= 0;
409 int required_arguments
= 0;
412 int action
= AC_UNSPEC
;
413 int family
= AF_UNSPEC
;
416 int c
= getopt_long(argc
, argv
, "46bcefgnpv", long_options
, &option_index
);
422 if (long_options
[option_index
].flag
!= 0)
425 printf("option: %s", long_options
[option_index
].name
);
427 printf(" with arg %s", optarg
);
440 set_action(&action
, AC_BROADCAST
);
441 required_arguments
= 1;
445 set_action(&action
, AC_CHECK
);
446 required_arguments
= 1;
450 set_action(&action
, AC_EQUAL
);
451 required_arguments
= 2;
455 set_action(&action
, AC_FORMAT
);
456 required_arguments
= 1;
460 set_action(&action
, AC_GREATER
);
461 required_arguments
= 2;
465 set_action(&action
, AC_NETWORK
);
466 required_arguments
= 1;
470 set_action(&action
, AC_PREFIX
);
471 required_arguments
= 2;
491 if (argc
!= required_arguments
) {
492 fprintf(stderr
, "Invalid number of arguments. Got %d, required %d.\n",
493 argc
, required_arguments
);
497 if (verbose
&& family
!= AF_UNSPEC
)
498 printf("Address family = %d\n", family
);
504 printf("No action specified\n");
509 r
= action_broadcast(family
, argv
[0]);
513 r
= action_check(family
, argv
[0]);
517 printf("%s is a valid IP address\n", argv
[0]);
519 printf("%s is not a valid IP address\n", argv
[0]);
524 r
= action_equal(family
, argv
[0], argv
[1]);
528 printf("%s equals %s\n", argv
[0], argv
[1]);
530 printf("Invalid IP address provided\n");
532 printf("%s does not equal %s\n", argv
[0], argv
[1]);
537 r
= action_format(family
, argv
[0]);
540 printf("Invalid IP address given\n");
545 r
= action_greater(family
, argv
[0], argv
[1]);
549 printf("%s is greater than %s\n", argv
[0], argv
[1]);
551 printf("Invalid IP address provided\n");
553 printf("%s is not greater than %s\n", argv
[0], argv
[1]);
558 r
= action_network(family
, argv
[0]);
562 r
= action_prefix(family
, argv
[0], argv
[1]);