]> git.ipfire.org Git - network.git/blob - src/inetcalc.c
inetcalc: Allow build on 64 bit architectures
[network.git] / src / inetcalc.c
1 /*#############################################################################
2 # #
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2015 IPFire Network Development Team #
5 # #
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. #
10 # #
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. #
15 # #
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/>. #
18 # #
19 #############################################################################*/
20
21 #include <assert.h>
22 #include <arpa/inet.h>
23 #include <errno.h>
24 #include <getopt.h>
25 #include <netinet/in.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/socket.h>
30
31 typedef struct ip_address {
32 int family;
33 struct in6_addr addr;
34 int prefix;
35 } ip_address_t;
36
37 static struct in6_addr prefix_to_bitmask(int prefix) {
38 assert(prefix <= 128);
39
40 struct in6_addr bitmask;
41
42 for (int i = 0; i < 16; i++)
43 bitmask.s6_addr[i] = 0;
44
45 for (int i = prefix, j = 0; i > 0; i -= 8, j++) {
46 if (i >= 8)
47 bitmask.s6_addr[j] = 0xff;
48 else
49 bitmask.s6_addr[j] = 0xff << (8 - i);
50 }
51
52 return bitmask;
53 }
54
55 static int bitmask_to_prefix(uint32_t bits) {
56 int prefix = 0;
57
58 // Count all ones until we find the first zero
59 while (bits & (1 << 31)) {
60 bits <<= 1;
61 prefix++;
62 }
63
64 // The remaining bits must all be zero
65 if (bits)
66 return -1;
67
68 return prefix;
69 }
70
71 static int ip_address_parse_subnet_mask(ip_address_t* ip, const char* prefix) {
72 struct in_addr mask;
73
74 int r = inet_pton(AF_INET, prefix, &mask.s_addr);
75 if (r != 1)
76 return 1;
77
78 uint32_t bits = ntohl(mask.s_addr);
79 ip->prefix = bitmask_to_prefix(bits);
80
81 return (ip->prefix < 0 || ip->prefix > 32);
82 }
83
84 static int ip_address_parse_prefix_cidr(ip_address_t* ip, const int family, const char* prefix) {
85 ip->prefix = 0;
86 while (*prefix) {
87 char p = *prefix++;
88
89 if (p >= '0' && p <= '9') {
90 ip->prefix *= 10;
91 ip->prefix += p - '0';
92 } else {
93 return 1;
94 }
95 }
96
97 switch (family) {
98 case AF_INET6:
99 return (ip->prefix < 0 || ip->prefix > 128);
100
101 case AF_INET:
102 return (ip->prefix < 0 || ip->prefix > 32);
103
104 default:
105 return 1;
106 }
107 }
108
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);
111
112 if (r && family == AF_INET) {
113 r = ip_address_parse_subnet_mask(ip, prefix);
114 }
115
116 return r;
117 }
118
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);
121
122 size_t address_length = strlen(address);
123 char buffer[address_length + 1];
124 strncpy(buffer, address, sizeof(buffer));
125
126 // Search for a prefix or subnet mask
127 char* prefix = strchr(buffer, '/');
128 if (prefix) {
129 buffer[prefix - buffer] = '\0';
130 prefix++;
131 }
132
133 memset(&ip->addr, 0, sizeof(ip->addr));
134 int r = inet_pton(family, buffer, &ip->addr);
135
136 switch (r) {
137 // If parsing the IP address failed, we will return false
138 case 0:
139 return 1;
140
141 // If the IP address could be successfully parsed, we will
142 // save the address family and return true
143 case 1:
144 ip->family = family;
145 r = 0;
146 break;
147
148 default:
149 return r;
150 }
151
152 if (prefix)
153 r = ip_address_parse_prefix(ip, family, prefix);
154 else
155 ip->prefix = -1;
156
157 return r;
158 }
159
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 };
162
163 int r = 1;
164 int* f = families;
165 while (*f) {
166 if (family == AF_UNSPEC || family == *f) {
167 r = ip_address_parse_simple(ip, *f, address);
168
169 if (r == 0)
170 break;
171 }
172
173 f++;
174 }
175
176 return r;
177 }
178
179 static int ip_address_eq(const ip_address_t* a1, const ip_address_t* a2) {
180 if (a1->family != a2->family)
181 return 1;
182
183 if (a1->addr.s6_addr != a2->addr.s6_addr)
184 return 1;
185
186 if (a1->prefix != a2->prefix)
187 return 1;
188
189 return 0;
190 }
191
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)
194 return -1;
195
196 if (a1->addr.s6_addr > a2->addr.s6_addr)
197 return 0;
198
199 return 1;
200 }
201
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);
204
205 const char* p = inet_ntop(ip->family, &ip->addr.s6_addr, buffer, size);
206 if (!p)
207 return errno;
208
209 return 0;
210 }
211
212 static void ip_address_print(const ip_address_t* ip) {
213 char buffer[INET6_ADDRSTRLEN+4];
214
215 int r = ip_address_format_string(buffer, sizeof(buffer), ip);
216 if (r)
217 return;
218
219 if (ip->prefix >= 0) {
220 size_t len = strlen(buffer);
221 snprintf(buffer + len, sizeof(buffer) - len, "/%d", ip->prefix);
222 }
223
224 printf("%s\n", buffer);
225 }
226
227 static void ip_address_make_network(ip_address_t* net, const ip_address_t* ip) {
228 assert(ip->prefix >= 0);
229
230 struct in6_addr mask = prefix_to_bitmask(ip->prefix);
231
232 net->family = ip->family;
233 net->prefix = ip->prefix;
234
235 for (int i = 0; i < 16; i++)
236 net->addr.s6_addr[i] = ip->addr.s6_addr[i] & mask.s6_addr[i];
237 }
238
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);
241
242 struct in6_addr mask = prefix_to_bitmask(ip->prefix);
243
244 broadcast->family = ip->family;
245 broadcast->prefix = ip->prefix;
246
247 for (int i = 0; i < 16; i++)
248 broadcast->addr.s6_addr[i] = ip->addr.s6_addr[i] | ~mask.s6_addr[i];
249 }
250
251 static int action_check(const int family, const char* address) {
252 ip_address_t ip;
253
254 int r = ip_address_parse(&ip, family, address);
255 if (r)
256 return r;
257
258 // No prefix allowed
259 return (ip.prefix >= 0);
260 }
261
262 static int action_equal(const int family, const char* addr1, const char* addr2) {
263 ip_address_t a1;
264 ip_address_t a2;
265 int r;
266
267 r = ip_address_parse(&a1, family, addr1);
268 if (r)
269 return 2;
270
271 r = ip_address_parse(&a2, family, addr2);
272 if (r)
273 return 2;
274
275 return ip_address_eq(&a1, &a2);
276 }
277
278 static int action_greater(const int family, const char* addr1, const char* addr2) {
279 ip_address_t a1;
280 ip_address_t a2;
281 int r;
282
283 r = ip_address_parse(&a1, family, addr1);
284 if (r)
285 return 2;
286
287 r = ip_address_parse(&a2, family, addr2);
288 if (r)
289 return 2;
290
291 return ip_address_gt(&a1, &a2);
292 }
293
294 static int action_format(const int family, const char* address) {
295 ip_address_t ip;
296
297 int r = ip_address_parse(&ip, family, address);
298 if (r)
299 return r;
300
301 ip_address_print(&ip);
302 return 0;
303 }
304
305 static int action_broadcast(const int family, const char* address) {
306 ip_address_t ip;
307 int r = ip_address_parse(&ip, family, address);
308 if (r) {
309 fprintf(stderr, "Invalid IP address: %s\n", address);
310 return r;
311 }
312
313 if (ip.family != AF_INET) {
314 fprintf(stderr, "This is only possible for IPv4\n");
315 return 1;
316 }
317
318 ip_address_t broadcast;
319 ip_address_make_broadcast(&broadcast, &ip);
320
321 ip_address_print(&broadcast);
322 return 0;
323 }
324
325 static int action_network(const int family, const char* address) {
326 ip_address_t ip;
327
328 int r = ip_address_parse(&ip, family, address);
329 if (r) {
330 fprintf(stderr, "Invalid IP address: %s\n", address);
331 return r;
332 }
333
334 ip_address_t network;
335 ip_address_make_network(&network, &ip);
336
337 ip_address_print(&network);
338 return 0;
339 }
340
341 static int action_prefix(const int family, const char* addr1, const char* addr2) {
342 int r;
343
344 ip_address_t network;
345 r = ip_address_parse(&network, family, addr1);
346 if (r)
347 return r;
348
349 ip_address_t broadcast;
350 r = ip_address_parse(&broadcast, family, addr2);
351 if (r)
352 return r;
353
354 r = ip_address_gt(&broadcast, &network);
355 if (r)
356 return r;
357
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];
361
362 uint32_t mask = netmask.s6_addr[0] << 24 | netmask.s6_addr[1] << 16 |
363 netmask.s6_addr[2] << 8 | netmask.s6_addr[3];
364
365 int prefix = bitmask_to_prefix(~mask);
366 if (prefix < 0)
367 return 1;
368
369 printf("%d\n", prefix);
370 return 0;
371 }
372
373 enum actions {
374 AC_UNSPEC = 0,
375 AC_BROADCAST,
376 AC_CHECK,
377 AC_EQUAL,
378 AC_FORMAT,
379 AC_GREATER,
380 AC_NETWORK,
381 AC_PREFIX,
382 };
383
384 static void set_action(int* action, int what) {
385 if (*action != AC_UNSPEC) {
386 printf("Another action has already been selected\n");
387 exit(1);
388 }
389
390 *action = what;
391 }
392
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'},
404 {0, 0, 0, 0}
405 };
406
407 int main(int argc, char** argv) {
408 int option_index = 0;
409 int required_arguments = 0;
410
411 int verbose = 0;
412 int action = AC_UNSPEC;
413 int family = AF_UNSPEC;
414
415 while (1) {
416 int c = getopt_long(argc, argv, "46bcefgnpv", long_options, &option_index);
417 if (c == -1)
418 break;
419
420 switch (c) {
421 case 0:
422 if (long_options[option_index].flag != 0)
423 break;
424
425 printf("option: %s", long_options[option_index].name);
426 if (optarg)
427 printf(" with arg %s", optarg);
428 printf("\n");
429 break;
430
431 case '4':
432 family = AF_INET;
433 break;
434
435 case '6':
436 family = AF_INET6;
437 break;
438
439 case 'b':
440 set_action(&action, AC_BROADCAST);
441 required_arguments = 1;
442 break;
443
444 case 'c':
445 set_action(&action, AC_CHECK);
446 required_arguments = 1;
447 break;
448
449 case 'e':
450 set_action(&action, AC_EQUAL);
451 required_arguments = 2;
452 break;
453
454 case 'f':
455 set_action(&action, AC_FORMAT);
456 required_arguments = 1;
457 break;
458
459 case 'g':
460 set_action(&action, AC_GREATER);
461 required_arguments = 2;
462 break;
463
464 case 'n':
465 set_action(&action, AC_NETWORK);
466 required_arguments = 1;
467 break;
468
469 case 'p':
470 set_action(&action, AC_PREFIX);
471 required_arguments = 2;
472 break;
473
474 case 'v':
475 verbose = 1;
476 break;
477
478 case '?':
479 break;
480
481 default:
482 abort();
483 }
484 }
485
486 while (optind--) {
487 argc--;
488 argv++;
489 }
490
491 if (argc != required_arguments) {
492 fprintf(stderr, "Invalid number of arguments. Got %d, required %d.\n",
493 argc, required_arguments);
494 return 1;
495 }
496
497 if (verbose && family != AF_UNSPEC)
498 printf("Address family = %d\n", family);
499
500 int r = 0;
501
502 switch (action) {
503 case AC_UNSPEC:
504 printf("No action specified\n");
505 r = 1;
506 break;
507
508 case AC_BROADCAST:
509 r = action_broadcast(family, argv[0]);
510 break;
511
512 case AC_CHECK:
513 r = action_check(family, argv[0]);
514
515 if (verbose) {
516 if (r == 0)
517 printf("%s is a valid IP address\n", argv[0]);
518 else
519 printf("%s is not a valid IP address\n", argv[0]);
520 }
521 break;
522
523 case AC_EQUAL:
524 r = action_equal(family, argv[0], argv[1]);
525
526 if (verbose) {
527 if (r == 0)
528 printf("%s equals %s\n", argv[0], argv[1]);
529 else if (r == 2)
530 printf("Invalid IP address provided\n");
531 else
532 printf("%s does not equal %s\n", argv[0], argv[1]);
533 }
534 break;
535
536 case AC_FORMAT:
537 r = action_format(family, argv[0]);
538
539 if (verbose && r)
540 printf("Invalid IP address given\n");
541
542 break;
543
544 case AC_GREATER:
545 r = action_greater(family, argv[0], argv[1]);
546
547 if (verbose) {
548 if (r == 0)
549 printf("%s is greater than %s\n", argv[0], argv[1]);
550 else if (r == 2)
551 printf("Invalid IP address provided\n");
552 else
553 printf("%s is not greater than %s\n", argv[0], argv[1]);
554 }
555 break;
556
557 case AC_NETWORK:
558 r = action_network(family, argv[0]);
559 break;
560
561 case AC_PREFIX:
562 r = action_prefix(family, argv[0], argv[1]);
563 break;
564 }
565
566 return r;
567 }