18013136b120474c6cb2b23c716fa38b20db743d
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:255.255.255.255/128 ")];
48 bool (*a1
)(struct cidr
*a
);
49 bool (*a2
)(struct cidr
*a
, struct cidr
*b
);
52 bool (*a1
)(struct cidr
*a
);
53 bool (*a2
)(struct cidr
*a
, struct cidr
*b
);
58 static bool quiet
= false;
59 static bool printed
= false;
61 static struct cidr
*stack
= NULL
;
63 #define qprintf(...) \
65 if (!quiet) printf(__VA_ARGS__); \
69 static void cidr_push(struct cidr
*a
)
78 static bool cidr_pop(struct cidr
*a
)
80 struct cidr
*old
= stack
;
93 static struct cidr
* cidr_clone(struct cidr
*a
)
95 struct cidr
*b
= malloc(sizeof(*b
));
99 fprintf(stderr
, "out of memory\n");
103 memcpy(b
, a
, sizeof(*b
));
110 static struct cidr
* cidr_parse4(const char *s
)
114 struct cidr
*addr
= malloc(sizeof(struct cidr
));
116 if (!addr
|| (strlen(s
) >= sizeof(addr
->buf
.v4
)))
119 snprintf(addr
->buf
.v4
, sizeof(addr
->buf
.v4
), "%s", s
);
121 addr
->family
= AF_INET
;
123 if ((p
= strchr(addr
->buf
.v4
, '/')) != NULL
)
127 if (strchr(p
, '.') != NULL
)
129 if (inet_pton(AF_INET
, p
, &mask
) != 1)
132 for (addr
->prefix
= 0; mask
.s_addr
; mask
.s_addr
>>= 1)
133 addr
->prefix
+= (mask
.s_addr
& 1);
137 addr
->prefix
= strtoul(p
, &r
, 10);
139 if ((p
== r
) || (*r
!= 0) || (addr
->prefix
> 32))
148 if (p
== addr
->buf
.v4
+1)
149 memset(&addr
->addr
.v4
, 0, sizeof(addr
->addr
.v4
));
150 else if (inet_pton(AF_INET
, addr
->buf
.v4
, &addr
->addr
.v4
) != 1)
162 static bool cidr_add4(struct cidr
*a
, struct cidr
*b
)
164 uint32_t x
= ntohl(a
->addr
.v4
.s_addr
);
165 uint32_t y
= ntohl(b
->addr
.v4
.s_addr
);
167 struct cidr
*n
= cidr_clone(a
);
169 if ((n
->family
!= AF_INET
) || (b
->family
!= AF_INET
))
172 if ((uint32_t)(x
+ y
) < x
)
174 fprintf(stderr
, "overflow during 'add'\n");
178 n
->addr
.v4
.s_addr
= htonl(x
+ y
);
182 static bool cidr_sub4(struct cidr
*a
, struct cidr
*b
)
184 uint32_t x
= ntohl(a
->addr
.v4
.s_addr
);
185 uint32_t y
= ntohl(b
->addr
.v4
.s_addr
);
187 struct cidr
*n
= cidr_clone(a
);
189 if ((n
->family
!= AF_INET
) || (b
->family
!= AF_INET
))
192 if ((uint32_t)(x
- y
) > x
)
194 fprintf(stderr
, "underflow during 'sub'\n");
198 n
->addr
.v4
.s_addr
= htonl(x
- y
);
202 static bool cidr_network4(struct cidr
*a
)
204 struct cidr
*n
= cidr_clone(a
);
206 n
->addr
.v4
.s_addr
&= htonl(~((1 << (32 - n
->prefix
)) - 1));
212 static bool cidr_broadcast4(struct cidr
*a
)
214 struct cidr
*n
= cidr_clone(a
);
216 n
->addr
.v4
.s_addr
|= htonl(((1 << (32 - n
->prefix
)) - 1));
222 static bool cidr_contains4(struct cidr
*a
, struct cidr
*b
)
224 uint32_t net1
= a
->addr
.v4
.s_addr
& htonl(~((1 << (32 - a
->prefix
)) - 1));
225 uint32_t net2
= b
->addr
.v4
.s_addr
& htonl(~((1 << (32 - a
->prefix
)) - 1));
227 if ((b
->prefix
>= a
->prefix
) && (net1
== net2
))
239 static bool cidr_netmask4(struct cidr
*a
)
241 struct cidr
*n
= cidr_clone(a
);
243 n
->addr
.v4
.s_addr
= htonl(~((1 << (32 - n
->prefix
)) - 1));
249 static bool cidr_private4(struct cidr
*a
)
251 uint32_t x
= ntohl(a
->addr
.v4
.s_addr
);
253 if (((x
>= 0x0A000000) && (x
<= 0x0AFFFFFF)) ||
254 ((x
>= 0xAC100000) && (x
<= 0xAC1FFFFF)) ||
255 ((x
>= 0xC0A80000) && (x
<= 0xC0A8FFFF)))
267 static bool cidr_linklocal4(struct cidr
*a
)
269 uint32_t x
= ntohl(a
->addr
.v4
.s_addr
);
271 if ((x
>= 0xA9FE0000) && (x
<= 0xA9FEFFFF))
283 static bool cidr_prev4(struct cidr
*a
, struct cidr
*b
)
285 struct cidr
*n
= cidr_clone(a
);
287 n
->prefix
= b
->prefix
;
288 n
->addr
.v4
.s_addr
-= htonl(1 << (32 - b
->prefix
));
293 static bool cidr_next4(struct cidr
*a
, struct cidr
*b
)
295 struct cidr
*n
= cidr_clone(a
);
297 n
->prefix
= b
->prefix
;
298 n
->addr
.v4
.s_addr
+= htonl(1 << (32 - b
->prefix
));
303 static bool cidr_6to4(struct cidr
*a
)
305 struct cidr
*n
= cidr_clone(a
);
306 uint32_t x
= a
->addr
.v4
.s_addr
;
308 memset(&n
->addr
.v6
.s6_addr
, 0, sizeof(n
->addr
.v6
.s6_addr
));
310 n
->family
= AF_INET6
;
313 n
->addr
.v6
.s6_addr
[0] = 0x20;
314 n
->addr
.v6
.s6_addr
[1] = 0x02;
315 n
->addr
.v6
.s6_addr
[2] = (x
>> 24);
316 n
->addr
.v6
.s6_addr
[3] = (x
>> 16) & 0xFF;
317 n
->addr
.v6
.s6_addr
[4] = (x
>> 8) & 0xFF;
318 n
->addr
.v6
.s6_addr
[5] = x
& 0xFF;
323 static bool cidr_print4(struct cidr
*a
)
327 if (!a
|| (a
->family
!= AF_INET
))
330 if (!(p
= (char *)inet_ntop(AF_INET
, &a
->addr
.v4
, a
->buf
.v4
, sizeof(a
->buf
.v4
))))
339 qprintf("/%u", a
->prefix
);
347 static struct cidr
* cidr_parse6(const char *s
)
350 struct cidr
*addr
= malloc(sizeof(struct cidr
));
352 if (!addr
|| (strlen(s
) >= sizeof(addr
->buf
.v6
)))
355 snprintf(addr
->buf
.v4
, sizeof(addr
->buf
.v6
), "%s", s
);
357 addr
->family
= AF_INET6
;
359 if ((p
= strchr(addr
->buf
.v4
, '/')) != NULL
)
363 addr
->prefix
= strtoul(p
, &r
, 10);
365 if ((p
== r
) || (*r
!= 0) || (addr
->prefix
> 128))
373 if (p
== addr
->buf
.v4
+1)
374 memset(&addr
->addr
.v6
, 0, sizeof(addr
->addr
.v6
));
375 else if (inet_pton(AF_INET6
, addr
->buf
.v4
, &addr
->addr
.v6
) != 1)
387 static bool cidr_add6(struct cidr
*a
, struct cidr
*b
)
389 uint8_t idx
= 15, carry
= 0, overflow
= 0;
391 struct cidr
*n
= cidr_clone(a
);
392 struct in6_addr
*x
= &n
->addr
.v6
;
393 struct in6_addr
*y
= &b
->addr
.v6
;
395 if ((a
->family
!= AF_INET6
) || (b
->family
!= AF_INET6
))
399 overflow
= !!((x
->s6_addr
[idx
] + y
->s6_addr
[idx
] + carry
) >= 256);
400 x
->s6_addr
[idx
] += y
->s6_addr
[idx
] + carry
;
407 fprintf(stderr
, "overflow during 'add'\n");
414 static bool cidr_sub6(struct cidr
*a
, struct cidr
*b
)
416 uint8_t idx
= 15, carry
= 0, underflow
= 0;
418 struct cidr
*n
= cidr_clone(a
);
419 struct in6_addr
*x
= &n
->addr
.v6
;
420 struct in6_addr
*y
= &b
->addr
.v6
;
422 if ((n
->family
!= AF_INET6
) || (b
->family
!= AF_INET6
))
426 underflow
= !!((x
->s6_addr
[idx
] - y
->s6_addr
[idx
] - carry
) < 0);
427 x
->s6_addr
[idx
] -= y
->s6_addr
[idx
] + carry
;
434 fprintf(stderr
, "underflow during 'sub'\n");
441 static bool cidr_prev6(struct cidr
*a
, struct cidr
*b
)
443 uint8_t idx
, carry
= 1, underflow
= 0;
444 struct cidr
*n
= cidr_clone(a
);
445 struct in6_addr
*x
= &n
->addr
.v6
;
449 fprintf(stderr
, "underflow during 'prev'\n");
453 idx
= (b
->prefix
- 1) / 8;
456 underflow
= !!((x
->s6_addr
[idx
] - carry
) < 0);
457 x
->s6_addr
[idx
] -= carry
;
464 fprintf(stderr
, "underflow during 'prev'\n");
468 n
->prefix
= b
->prefix
;
473 static bool cidr_next6(struct cidr
*a
, struct cidr
*b
)
475 uint8_t idx
, carry
= 1, overflow
= 0;
476 struct cidr
*n
= cidr_clone(a
);
477 struct in6_addr
*x
= &n
->addr
.v6
;
481 fprintf(stderr
, "overflow during 'next'\n");
485 idx
= (b
->prefix
- 1) / 8;
488 overflow
= !!((x
->s6_addr
[idx
] + carry
) >= 256);
489 x
->s6_addr
[idx
] += carry
;
496 fprintf(stderr
, "overflow during 'next'\n");
500 n
->prefix
= b
->prefix
;
505 static bool cidr_network6(struct cidr
*a
)
508 struct cidr
*n
= cidr_clone(a
);
510 for (i
= 0; i
< (128 - n
->prefix
) / 8; i
++)
511 n
->addr
.v6
.s6_addr
[15-i
] = 0;
513 if ((128 - n
->prefix
) % 8)
514 n
->addr
.v6
.s6_addr
[15-i
] &= ~((1 << ((128 - n
->prefix
) % 8)) - 1);
519 static bool cidr_contains6(struct cidr
*a
, struct cidr
*b
)
521 struct cidr
*n
= cidr_clone(a
);
522 struct in6_addr
*x
= &n
->addr
.v6
;
523 struct in6_addr
*y
= &b
->addr
.v6
;
524 uint8_t i
= (128 - n
->prefix
) / 8;
525 uint8_t m
= ~((1 << ((128 - n
->prefix
) % 8)) - 1);
526 uint8_t net1
= x
->s6_addr
[15-i
] & m
;
527 uint8_t net2
= y
->s6_addr
[15-i
] & m
;
529 if ((b
->prefix
>= n
->prefix
) && (net1
== net2
) &&
530 ((i
== 15) || !memcmp(&x
->s6_addr
, &y
->s6_addr
, 15-i
)))
542 static bool cidr_linklocal6(struct cidr
*a
)
544 if ((a
->addr
.v6
.s6_addr
[0] == 0xFE) &&
545 (a
->addr
.v6
.s6_addr
[1] >= 0x80) &&
546 (a
->addr
.v6
.s6_addr
[1] <= 0xBF))
558 static bool cidr_ula6(struct cidr
*a
)
560 if ((a
->addr
.v6
.s6_addr
[0] >= 0xFC) &&
561 (a
->addr
.v6
.s6_addr
[0] <= 0xFD))
573 static bool cidr_print6(struct cidr
*a
)
577 if (!a
|| (a
->family
!= AF_INET6
))
580 if (!(p
= (char *)inet_ntop(AF_INET6
, &a
->addr
.v6
, a
->buf
.v6
, sizeof(a
->buf
.v6
))))
589 qprintf("/%u", a
->prefix
);
597 static struct cidr
* cidr_parse(const char *op
, const char *s
, int af_hint
)
603 uint32_t sum
= strtoul(s
, &r
, 0);
605 if ((r
> s
) && (*r
== 0))
607 a
= malloc(sizeof(struct cidr
));
612 if (af_hint
== AF_INET
)
616 a
->addr
.v4
.s_addr
= htonl(sum
);
620 a
->family
= AF_INET6
;
623 for (i
= 0; i
<= 15; i
++)
625 a
->addr
.v6
.s6_addr
[15-i
] = sum
% 256;
641 if (a
->family
!= af_hint
)
643 fprintf(stderr
, "attempt to '%s' %s with %s address\n",
645 (af_hint
== AF_INET
) ? "ipv4" : "ipv6",
646 (af_hint
!= AF_INET
) ? "ipv4" : "ipv6");
653 static bool cidr_howmany(struct cidr
*a
, struct cidr
*b
)
655 if (b
->prefix
< a
->prefix
)
658 qprintf("%u\n", 1 << (b
->prefix
- a
->prefix
));
663 static bool cidr_prefix(struct cidr
*a
, struct cidr
*b
)
665 a
->prefix
= b
->prefix
;
669 static bool cidr_quiet(struct cidr
*a
)
678 .desc
= "Add argument to base address",
680 .f6
.a2
= cidr_add6
},
683 .desc
= "Substract argument from base address",
685 .f6
.a2
= cidr_sub6
},
688 .desc
= "Advance base address to next prefix of given size",
690 .f6
.a2
= cidr_next6
},
693 .desc
= "Lower base address to previous prefix of give size",
695 .f6
.a2
= cidr_prev6
},
698 .desc
= "Turn base address into network address",
699 .f4
.a1
= cidr_network4
,
700 .f6
.a1
= cidr_network6
},
702 { .name
= "broadcast",
703 .desc
= "Turn base address into broadcast address",
704 .f4
.a1
= cidr_broadcast4
},
707 .desc
= "Set the prefix of base address to argument",
708 .f4
.a2
= cidr_prefix
,
709 .f6
.a2
= cidr_prefix
},
712 .desc
= "Calculate netmask of base address",
713 .f4
.a1
= cidr_netmask4
},
716 .desc
= "Calculate 6to4 prefix of given ipv4-address",
717 .f4
.a1
= cidr_6to4
},
720 .desc
= "Print amount of righ-hand prefixes that fit into base address",
721 .f4
.a2
= cidr_howmany
,
722 .f6
.a2
= cidr_howmany
},
724 { .name
= "contains",
725 .desc
= "Print '1' if argument fits into base address or '0' if not",
726 .f4
.a2
= cidr_contains4
,
727 .f6
.a2
= cidr_contains6
},
730 .desc
= "Print '1' if base address is in RFC1918 private space or '0' "
732 .f4
.a1
= cidr_private4
},
734 { .name
= "linklocal",
735 .desc
= "Print '1' if base address is in 169.254.0.0/16 or FE80::/10 "
736 "link local space or '0' if not",
737 .f4
.a1
= cidr_linklocal4
,
738 .f6
.a1
= cidr_linklocal6
},
741 .desc
= "Print '1' if base address is in FC00::/7 unique local address "
742 "(ULA) space or '0' if not",
743 .f6
.a1
= cidr_ula6
},
746 .desc
= "Suppress output, useful for test operation where the result can "
747 "be inferred from the exit code",
749 .f6
.a1
= cidr_quiet
},
752 .desc
= "Pop intermediate result from stack",
757 .desc
= "Print intermediate result and pop it from stack, invoked "
758 "implicitely at the end of calculation if no intermediate prints "
760 .f4
.a1
= cidr_print4
,
761 .f6
.a1
= cidr_print6
},
764 static void usage(const char *prog
)
771 " %s {base address} operation [argument] "
772 "[operation [argument] ...]\n\n"
776 for (i
= 0; i
< sizeof(ops
) / sizeof(ops
[0]); i
++)
778 if (ops
[i
].f4
.a2
|| ops
[i
].f6
.a2
)
780 fprintf(stderr
, " %s %s\n",
782 (ops
[i
].f4
.a2
&& ops
[i
].f6
.a2
) ? "{ipv4/ipv6/amount}" :
783 (ops
[i
].f6
.a2
? "{ipv6/amount}" : "{ipv4/amount}"));
787 fprintf(stderr
, " %s\n", ops
[i
].name
);
790 fprintf(stderr
, " %s.\n", ops
[i
].desc
);
792 if ((ops
[i
].f4
.a1
&& ops
[i
].f6
.a1
) || (ops
[i
].f4
.a2
&& ops
[i
].f6
.a2
))
793 fprintf(stderr
, " Applicable to ipv4- and ipv6-addresses.\n\n");
794 else if (ops
[i
].f6
.a2
|| ops
[i
].f6
.a1
)
795 fprintf(stderr
, " Only applicable to ipv6-addresses.\n\n");
797 fprintf(stderr
, " Only applicable to ipv4-addresses.\n\n");
802 " Calculate a DHCP range:\n\n"
803 " $ %s 192.168.1.1/255.255.255.0 network add 100 print add 150 print\n"
806 " Count number of prefixes:\n\n"
807 " $ %s 2001:0DB8:FDEF::/48 howmany ::/64\n"
814 static bool runop(char ***arg
, int *status
)
818 char *arg2
= *(*arg
+1);
819 struct cidr
*a
= stack
;
820 struct cidr
*b
= NULL
;
825 for (i
= 0; i
< sizeof(ops
) / sizeof(ops
[0]); i
++)
827 if (!strcmp(ops
[i
].name
, arg1
))
829 if (ops
[i
].f4
.a2
|| ops
[i
].f6
.a2
)
833 fprintf(stderr
, "'%s' requires an argument\n",
840 b
= cidr_parse(ops
[i
].name
, arg2
, a
->family
);
844 fprintf(stderr
, "invalid address argument for '%s'\n",
853 if (((a
->family
== AF_INET
) && !ops
[i
].f4
.a2
) ||
854 ((a
->family
== AF_INET6
) && !ops
[i
].f6
.a2
))
856 fprintf(stderr
, "'%s' not supported for %s addresses\n",
858 (a
->family
== AF_INET
) ? "ipv4" : "ipv6");
864 *status
= !((a
->family
== AF_INET
) ? ops
[i
].f4
.a2(a
, b
)
865 : ops
[i
].f6
.a2(a
, b
));
873 if (((a
->family
== AF_INET
) && !ops
[i
].f4
.a1
) ||
874 ((a
->family
== AF_INET6
) && !ops
[i
].f6
.a1
))
876 fprintf(stderr
, "'%s' not supported for %s addresses\n",
878 (a
->family
== AF_INET
) ? "ipv4" : "ipv6");
884 *status
= !((a
->family
== AF_INET
) ? ops
[i
].f4
.a1(a
)
895 int main(int argc
, char **argv
)
904 a
= strchr(argv
[1], ':') ? cidr_parse6(argv
[1]) : cidr_parse4(argv
[1]);
911 while (runop(&arg
, &status
));
915 fprintf(stderr
, "unknown operation '%s'\n", *arg
);
919 if (!printed
&& (status
< 2))
921 if (stack
->family
== AF_INET
)