2 * owipcalc - OpenWrt IP Calculator
4 * Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
27 #include <arpa/inet.h>
38 char v4[sizeof("255.255.255.255/255.255.255.255 ")];
39 char v6[sizeof("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF/128 ")];
47 bool (*a1)(struct cidr *a);
48 bool (*a2)(struct cidr *a, struct cidr *b);
51 bool (*a1)(struct cidr *a);
52 bool (*a2)(struct cidr *a, struct cidr *b);
57 static bool quiet = false;
58 static bool printed = false;
61 static struct cidr * cidr_parse4(const char *s)
65 struct cidr *addr = malloc(sizeof(struct cidr));
67 if (!addr || (strlen(s) >= sizeof(addr->buf.v4)))
70 snprintf(addr->buf.v4, sizeof(addr->buf.v4), "%s", s);
72 addr->family = AF_INET;
74 if ((p = strchr(addr->buf.v4, '/')) != NULL)
78 if (strchr(p, '.') != NULL)
80 if (inet_pton(AF_INET, p, &mask) != 1)
83 for (addr->prefix = 0; mask.s_addr; mask.s_addr >>= 1)
84 addr->prefix += (mask.s_addr & 1);
88 addr->prefix = strtoul(p, &r, 10);
90 if ((p == r) || (*r != 0) || (addr->prefix > 32))
99 if (p == addr->buf.v4+1)
100 memset(&addr->addr.v4, 0, sizeof(addr->addr.v4));
101 else if (inet_pton(AF_INET, addr->buf.v4, &addr->addr.v4) != 1)
113 static bool cidr_add4(struct cidr *a, struct cidr *b)
115 uint32_t x = ntohl(a->addr.v4.s_addr);
116 uint32_t y = ntohl(b->addr.v4.s_addr);
118 if ((a->family != AF_INET) || (b->family != AF_INET))
121 if ((uint32_t)(x + y) < x)
123 fprintf(stderr, "overflow during 'add'\n");
127 a->addr.v4.s_addr = htonl(x + y);
131 static bool cidr_sub4(struct cidr *a, struct cidr *b)
133 uint32_t x = ntohl(a->addr.v4.s_addr);
134 uint32_t y = ntohl(b->addr.v4.s_addr);
136 if ((a->family != AF_INET) || (b->family != AF_INET))
139 if ((uint32_t)(x - y) > x)
141 fprintf(stderr, "underflow during 'sub'\n");
145 a->addr.v4.s_addr = htonl(x - y);
149 static bool cidr_network4(struct cidr *a)
151 a->addr.v4.s_addr &= htonl(~((1 << (32 - a->prefix)) - 1));
156 static bool cidr_broadcast4(struct cidr *a)
158 a->addr.v4.s_addr |= htonl(((1 << (32 - a->prefix)) - 1));
163 static bool cidr_contains4(struct cidr *a, struct cidr *b)
165 uint32_t net1 = a->addr.v4.s_addr & htonl(~((1 << (32 - a->prefix)) - 1));
166 uint32_t net2 = b->addr.v4.s_addr & htonl(~((1 << (32 - a->prefix)) - 1));
170 if ((b->prefix >= a->prefix) && (net1 == net2))
172 if (!quiet) printf("1\n");
177 if (!quiet) printf("0\n");
182 static bool cidr_netmask4(struct cidr *a)
185 char buf[sizeof("255.255.255.255 ")];
187 mask.s_addr = htonl(~((1 << (32 - a->prefix)) - 1));
190 printf("%s\n", inet_ntop(AF_INET, &mask, buf, sizeof(buf)));
197 static bool cidr_private4(struct cidr *a)
199 uint32_t x = ntohl(a->addr.v4.s_addr);
203 if (((x >= 0x0A000000) && (x <= 0x0AFFFFFF)) ||
204 ((x >= 0xAC100000) && (x <= 0xAC1FFFFF)) ||
205 ((x >= 0xC0A80000) && (x <= 0xC0A8FFFF)))
207 if (!quiet) printf("1\n");
212 if (!quiet) printf("0\n");
217 static bool cidr_linklocal4(struct cidr *a)
219 uint32_t x = ntohl(a->addr.v4.s_addr);
223 if ((x >= 0xA9FE0000) && (x <= 0xA9FEFFFF))
225 if (!quiet) printf("1\n");
230 if (!quiet) printf("0\n");
235 static bool cidr_print4(struct cidr *a)
239 if (a->family != AF_INET)
242 if (!(p = (char *)inet_ntop(AF_INET, &a->addr.v4, a->buf.v4, sizeof(a->buf.v4))))
248 if (!quiet && (a->prefix < 32))
249 printf("/%u", a->prefix);
260 static struct cidr * cidr_parse6(const char *s)
263 struct cidr *addr = malloc(sizeof(struct cidr));
265 if (!addr || (strlen(s) >= sizeof(addr->buf.v6)))
268 snprintf(addr->buf.v6, sizeof(addr->buf.v6), "%s", s);
270 addr->family = AF_INET6;
272 if ((p = strchr(addr->buf.v6, '/')) != NULL)
276 addr->prefix = strtoul(p, &r, 10);
278 if ((p == r) || (*r != 0) || (addr->prefix > 128))
286 if (p == addr->buf.v6+1)
287 memset(&addr->addr.v6, 0, sizeof(addr->addr.v6));
288 else if (inet_pton(AF_INET6, addr->buf.v6, &addr->addr.v6) != 1)
300 static bool cidr_add6(struct cidr *a, struct cidr *b)
302 uint8_t idx = 15, carry = 0, overflow = 0;
304 struct in6_addr *x = &a->addr.v6;
305 struct in6_addr *y = &b->addr.v6;
307 if ((a->family != AF_INET6) || (b->family != AF_INET6))
311 overflow = !!((x->s6_addr[idx] + y->s6_addr[idx] + carry) >= 256);
312 x->s6_addr[idx] += y->s6_addr[idx] + carry;
319 fprintf(stderr, "overflow during 'add'\n");
326 static bool cidr_sub6(struct cidr *a, struct cidr *b)
328 uint8_t idx = 15, carry = 0, underflow = 0;
330 struct in6_addr *x = &a->addr.v6;
331 struct in6_addr *y = &b->addr.v6;
333 if ((a->family != AF_INET6) || (b->family != AF_INET6))
337 underflow = !!((x->s6_addr[idx] - y->s6_addr[idx] - carry) < 0);
338 x->s6_addr[idx] -= y->s6_addr[idx] + carry;
345 fprintf(stderr, "underflow during 'sub'\n");
352 static bool cidr_network6(struct cidr *a)
356 for (i = 0; i < (128 - a->prefix) / 8; i++)
357 a->addr.v6.s6_addr[15-i] = 0;
359 if ((128 - a->prefix) % 8)
360 a->addr.v6.s6_addr[15-i] &= ~((1 << ((128 - a->prefix) % 8)) - 1);
365 static bool cidr_contains6(struct cidr *a, struct cidr *b)
367 struct in6_addr *x = &a->addr.v6;
368 struct in6_addr *y = &b->addr.v6;
370 uint8_t i = (128 - a->prefix) / 8;
371 uint8_t m = ~((1 << ((128 - a->prefix) % 8)) - 1);
372 uint8_t net1 = x->s6_addr[15-i] & m;
373 uint8_t net2 = y->s6_addr[15-i] & m;
377 if ((b->prefix >= a->prefix) && (net1 == net2) &&
378 ((i == 15) || !memcmp(&x->s6_addr, &y->s6_addr, 15-i)))
380 if (!quiet) printf("1\n");
385 if (!quiet) printf("0\n");
390 static bool cidr_linklocal6(struct cidr *a)
394 if ((a->addr.v6.s6_addr[0] == 0xFE) &&
395 (a->addr.v6.s6_addr[1] >= 0x80) &&
396 (a->addr.v6.s6_addr[1] <= 0xBF))
398 if (!quiet) printf("1\n");
403 if (!quiet) printf("0\n");
408 static bool cidr_ula6(struct cidr *a)
412 if ((a->addr.v6.s6_addr[0] >= 0xFC) &&
413 (a->addr.v6.s6_addr[0] <= 0xFD))
415 if (!quiet) printf("1\n");
420 if (!quiet) printf("0\n");
425 static bool cidr_print6(struct cidr *a)
429 if (a->family != AF_INET6)
432 if (!(p = (char *)inet_ntop(AF_INET6, &a->addr.v6, a->buf.v6, sizeof(a->buf.v6))))
438 if (!quiet && (a->prefix < 128))
439 printf("/%u", a->prefix);
450 static struct cidr * cidr_parse(const char *op, const char *s, int af_hint)
456 uint32_t sum = strtoul(s, &r, 0);
458 if ((r > s) && (*r == 0))
460 a = malloc(sizeof(struct cidr));
465 if (af_hint == AF_INET)
469 a->addr.v4.s_addr = htonl(sum);
473 a->family = AF_INET6;
476 for (i = 0; i <= 15; i++)
478 a->addr.v6.s6_addr[15-i] = sum % 256;
494 if (a->family != af_hint)
496 fprintf(stderr, "attempt to '%s' %s with %s address\n",
498 (af_hint == AF_INET) ? "ipv4" : "ipv6",
499 (af_hint != AF_INET) ? "ipv4" : "ipv6");
506 static bool cidr_howmany(struct cidr *a, struct cidr *b)
510 if (b->prefix < a->prefix)
513 printf("%u\n", 1 << (b->prefix - a->prefix));
521 static bool cidr_prefix(struct cidr *a, struct cidr *b)
523 a->prefix = b->prefix;
527 static bool cidr_quiet(struct cidr *a)
536 .desc = "Add argument to base address",
538 .f6.a2 = cidr_add6 },
541 .desc = "Substract argument from base address",
543 .f6.a2 = cidr_sub6 },
546 .desc = "Turn base address into network address",
547 .f4.a1 = cidr_network4,
548 .f6.a1 = cidr_network6 },
550 { .name = "broadcast",
551 .desc = "Turn base address into broadcast address",
552 .f4.a1 = cidr_broadcast4 },
555 .desc = "Set the prefix of base address to argument",
556 .f4.a2 = cidr_prefix,
557 .f6.a2 = cidr_prefix },
560 .desc = "Print netmask of base address, does not change base address",
561 .f4.a1 = cidr_netmask4 },
564 .desc = "Print amount of righ-hand prefixes that fit into base address, "
565 "does not change base address",
566 .f4.a2 = cidr_howmany,
567 .f6.a2 = cidr_howmany },
569 { .name = "contains",
570 .desc = "Print '1' if argument fits into base address or '0' "
571 "if not, does not change base address",
572 .f4.a2 = cidr_contains4,
573 .f6.a2 = cidr_contains6 },
576 .desc = "Print '1' if base address is in RFC1918 private space or '0' "
577 "if not, does not change base address",
578 .f4.a1 = cidr_private4 },
580 { .name = "linklocal",
581 .desc = "Print '1' if base address is in 169.254.0.0/16 or FE80::/10 "
582 "link local space or '0' if not, does not change base address",
583 .f4.a1 = cidr_linklocal4,
584 .f6.a1 = cidr_linklocal6 },
587 .desc = "Print '1' if base address is in FC00::/7 unique local address "
588 "(ULA) space or '0' if not, does not change base address",
589 .f6.a1 = cidr_ula6 },
592 .desc = "Suppress output, useful for test operation where the result can "
593 "be inferred from the exit code, does not change base address",
595 .f6.a1 = cidr_quiet },
598 .desc = "Print intermediate result, invoked implicitely at the end of "
599 "calculation if no intermediate prints happened",
600 .f4.a1 = cidr_print4,
601 .f6.a1 = cidr_print6 },
604 static void usage(const char *prog)
611 " %s {base address} operation [argument] "
612 "[operation [argument] ...]\n\n"
616 for (i = 0; i < sizeof(ops) / sizeof(ops[0]); i++)
618 if (ops[i].f4.a2 || ops[i].f6.a2)
620 fprintf(stderr, " %s %s\n",
622 (ops[i].f4.a2 && ops[i].f6.a2) ? "{ipv4/ipv6/amount}" :
623 (ops[i].f6.a2 ? "{ipv6/amount}" : "{ipv4/amount}"));
627 fprintf(stderr, " %s\n", ops[i].name);
630 fprintf(stderr, " %s.\n", ops[i].desc);
632 if ((ops[i].f4.a1 && ops[i].f6.a1) || (ops[i].f4.a2 && ops[i].f6.a2))
633 fprintf(stderr, " Applicable to ipv4- and ipv6-addresses.\n\n");
634 else if (ops[i].f6.a2 || ops[i].f6.a1)
635 fprintf(stderr, " Only applicable to ipv6-addresses.\n\n");
637 fprintf(stderr, " Only applicable to ipv4-addresses.\n\n");
642 " Calculate a DHCP range:\n\n"
643 " $ %s 192.168.1.1/255.255.255.0 network add 100 print add 150 print\n"
646 " Count number of prefixes:\n\n"
647 " $ %s 2001:0DB8:FDEF::/48 howmany ::/64\n"
654 static bool runop(struct cidr *a, char ***arg, int *status)
658 char *arg2 = *(*arg+1);
659 struct cidr *b = NULL;
664 for (i = 0; i < sizeof(ops) / sizeof(ops[0]); i++)
666 if (!strcmp(ops[i].name, arg1))
668 if (ops[i].f4.a2 || ops[i].f6.a2)
672 fprintf(stderr, "'%s' requires an argument\n",
679 b = cidr_parse(ops[i].name, arg2, a->family);
683 fprintf(stderr, "invalid address argument for '%s'\n",
692 if (((a->family == AF_INET) && !ops[i].f4.a2) ||
693 ((a->family == AF_INET6) && !ops[i].f6.a2))
695 fprintf(stderr, "'%s' not supported for %s addresses\n",
697 (a->family == AF_INET) ? "ipv4" : "ipv6");
703 *status = !((a->family == AF_INET) ? ops[i].f4.a2(a, b)
704 : ops[i].f6.a2(a, b));
712 if (((a->family == AF_INET) && !ops[i].f4.a1) ||
713 ((a->family == AF_INET6) && !ops[i].f6.a1))
715 fprintf(stderr, "'%s' not supported for %s addresses\n",
717 (a->family == AF_INET) ? "ipv4" : "ipv6");
723 *status = !((a->family == AF_INET) ? ops[i].f4.a1(a)
734 int main(int argc, char **argv)
743 a = strchr(argv[1], ':') ? cidr_parse6(argv[1]) : cidr_parse4(argv[1]);
748 while (runop(a, &arg, &status));
752 fprintf(stderr, "unknown operation '%s'\n", *arg);
756 if (!printed && (status < 2))
758 if (a->family == AF_INET)