2 * owipcalc - OpenWrt IP Calculator
4 * Copyright (C) 2012 Jo-Philipp Wich <jo@mein.io>
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));
230 if ((b
->prefix
>= a
->prefix
) && (net1
== net2
))
242 static bool cidr_netmask4(struct cidr
*a
)
244 struct cidr
*n
= cidr_clone(a
);
246 n
->addr
.v4
.s_addr
= htonl(~((1 << (32 - n
->prefix
)) - 1));
252 static bool cidr_private4(struct cidr
*a
)
254 uint32_t x
= ntohl(a
->addr
.v4
.s_addr
);
259 if (((x
>= 0x0A000000) && (x
<= 0x0AFFFFFF)) ||
260 ((x
>= 0xAC100000) && (x
<= 0xAC1FFFFF)) ||
261 ((x
>= 0xC0A80000) && (x
<= 0xC0A8FFFF)))
273 static bool cidr_linklocal4(struct cidr
*a
)
275 uint32_t x
= ntohl(a
->addr
.v4
.s_addr
);
280 if ((x
>= 0xA9FE0000) && (x
<= 0xA9FEFFFF))
292 static bool cidr_prev4(struct cidr
*a
, struct cidr
*b
)
294 struct cidr
*n
= cidr_clone(a
);
296 n
->prefix
= b
->prefix
;
297 n
->addr
.v4
.s_addr
-= htonl(1 << (32 - b
->prefix
));
302 static bool cidr_next4(struct cidr
*a
, struct cidr
*b
)
304 struct cidr
*n
= cidr_clone(a
);
306 n
->prefix
= b
->prefix
;
307 n
->addr
.v4
.s_addr
+= htonl(1 << (32 - b
->prefix
));
312 static bool cidr_6to4(struct cidr
*a
)
314 struct cidr
*n
= cidr_clone(a
);
315 uint32_t x
= a
->addr
.v4
.s_addr
;
317 memset(&n
->addr
.v6
.s6_addr
, 0, sizeof(n
->addr
.v6
.s6_addr
));
319 n
->family
= AF_INET6
;
322 n
->addr
.v6
.s6_addr
[0] = 0x20;
323 n
->addr
.v6
.s6_addr
[1] = 0x02;
324 n
->addr
.v6
.s6_addr
[2] = (x
>> 24);
325 n
->addr
.v6
.s6_addr
[3] = (x
>> 16) & 0xFF;
326 n
->addr
.v6
.s6_addr
[4] = (x
>> 8) & 0xFF;
327 n
->addr
.v6
.s6_addr
[5] = x
& 0xFF;
332 static bool cidr_print4(struct cidr
*a
)
336 if (!a
|| (a
->family
!= AF_INET
))
339 if (!(p
= (char *)inet_ntop(AF_INET
, &a
->addr
.v4
, a
->buf
.v4
, sizeof(a
->buf
.v4
))))
348 qprintf("/%u", a
->prefix
);
356 static struct cidr
* cidr_parse6(const char *s
)
359 struct cidr
*addr
= malloc(sizeof(struct cidr
));
361 if (!addr
|| (strlen(s
) >= sizeof(addr
->buf
.v6
)))
364 snprintf(addr
->buf
.v6
, sizeof(addr
->buf
.v6
), "%s", s
);
366 addr
->family
= AF_INET6
;
368 if ((p
= strchr(addr
->buf
.v6
, '/')) != NULL
)
372 addr
->prefix
= strtoul(p
, &r
, 10);
374 if ((p
== r
) || (*r
!= 0) || (addr
->prefix
> 128))
382 if (p
== addr
->buf
.v6
+1)
383 memset(&addr
->addr
.v6
, 0, sizeof(addr
->addr
.v6
));
384 else if (inet_pton(AF_INET6
, addr
->buf
.v6
, &addr
->addr
.v6
) != 1)
396 static bool cidr_add6(struct cidr
*a
, struct cidr
*b
)
398 uint8_t idx
= 15, carry
= 0, overflow
= 0;
400 struct cidr
*n
= cidr_clone(a
);
401 struct in6_addr
*x
= &n
->addr
.v6
;
402 struct in6_addr
*y
= &b
->addr
.v6
;
404 if ((a
->family
!= AF_INET6
) || (b
->family
!= AF_INET6
))
408 overflow
= !!((x
->s6_addr
[idx
] + y
->s6_addr
[idx
] + carry
) >= 256);
409 x
->s6_addr
[idx
] += y
->s6_addr
[idx
] + carry
;
416 fprintf(stderr
, "overflow during 'add'\n");
423 static bool cidr_sub6(struct cidr
*a
, struct cidr
*b
)
425 uint8_t idx
= 15, carry
= 0, underflow
= 0;
427 struct cidr
*n
= cidr_clone(a
);
428 struct in6_addr
*x
= &n
->addr
.v6
;
429 struct in6_addr
*y
= &b
->addr
.v6
;
431 if ((n
->family
!= AF_INET6
) || (b
->family
!= AF_INET6
))
435 underflow
= !!((x
->s6_addr
[idx
] - y
->s6_addr
[idx
] - carry
) < 0);
436 x
->s6_addr
[idx
] -= y
->s6_addr
[idx
] + carry
;
443 fprintf(stderr
, "underflow during 'sub'\n");
450 static bool cidr_prev6(struct cidr
*a
, struct cidr
*b
)
452 uint8_t idx
, carry
= 1, underflow
= 0;
453 struct cidr
*n
= cidr_clone(a
);
454 struct in6_addr
*x
= &n
->addr
.v6
;
458 fprintf(stderr
, "underflow during 'prev'\n");
462 idx
= (b
->prefix
- 1) / 8;
465 underflow
= !!((x
->s6_addr
[idx
] - carry
) < 0);
466 x
->s6_addr
[idx
] -= carry
;
473 fprintf(stderr
, "underflow during 'prev'\n");
477 n
->prefix
= b
->prefix
;
482 static bool cidr_next6(struct cidr
*a
, struct cidr
*b
)
484 uint8_t idx
, carry
= 1, overflow
= 0;
485 struct cidr
*n
= cidr_clone(a
);
486 struct in6_addr
*x
= &n
->addr
.v6
;
490 fprintf(stderr
, "overflow during 'next'\n");
494 idx
= (b
->prefix
- 1) / 8;
497 overflow
= !!((x
->s6_addr
[idx
] + carry
) >= 256);
498 x
->s6_addr
[idx
] += carry
;
505 fprintf(stderr
, "overflow during 'next'\n");
509 n
->prefix
= b
->prefix
;
514 static bool cidr_network6(struct cidr
*a
)
517 struct cidr
*n
= cidr_clone(a
);
519 for (i
= 0; i
< (128 - n
->prefix
) / 8; i
++)
520 n
->addr
.v6
.s6_addr
[15-i
] = 0;
522 if ((128 - n
->prefix
) % 8)
523 n
->addr
.v6
.s6_addr
[15-i
] &= ~((1 << ((128 - n
->prefix
) % 8)) - 1);
528 static bool cidr_contains6(struct cidr
*a
, struct cidr
*b
)
530 struct in6_addr
*x
= &a
->addr
.v6
;
531 struct in6_addr
*y
= &b
->addr
.v6
;
532 uint8_t i
= (128 - a
->prefix
) / 8;
533 uint8_t m
= ~((1 << ((128 - a
->prefix
) % 8)) - 1);
534 uint8_t net1
= x
->s6_addr
[15-i
] & m
;
535 uint8_t net2
= y
->s6_addr
[15-i
] & m
;
540 if ((b
->prefix
>= a
->prefix
) && (net1
== net2
) &&
541 ((i
== 15) || !memcmp(&x
->s6_addr
, &y
->s6_addr
, 15-i
)))
553 static bool cidr_linklocal6(struct cidr
*a
)
558 if ((a
->addr
.v6
.s6_addr
[0] == 0xFE) &&
559 (a
->addr
.v6
.s6_addr
[1] >= 0x80) &&
560 (a
->addr
.v6
.s6_addr
[1] <= 0xBF))
572 static bool cidr_ula6(struct cidr
*a
)
577 if ((a
->addr
.v6
.s6_addr
[0] >= 0xFC) &&
578 (a
->addr
.v6
.s6_addr
[0] <= 0xFD))
590 static bool cidr_print6(struct cidr
*a
)
594 if (!a
|| (a
->family
!= AF_INET6
))
597 if (!(p
= (char *)inet_ntop(AF_INET6
, &a
->addr
.v6
, a
->buf
.v6
, sizeof(a
->buf
.v6
))))
606 qprintf("/%u", a
->prefix
);
614 static struct cidr
* cidr_parse(const char *op
, const char *s
, int af_hint
)
620 uint32_t sum
= strtoul(s
, &r
, 0);
622 if ((r
> s
) && (*r
== 0))
624 a
= malloc(sizeof(struct cidr
));
629 if (af_hint
== AF_INET
)
633 a
->addr
.v4
.s_addr
= htonl(sum
);
637 a
->family
= AF_INET6
;
640 for (i
= 0; i
<= 15; i
++)
642 a
->addr
.v6
.s6_addr
[15-i
] = sum
% 256;
658 if (a
->family
!= af_hint
)
660 fprintf(stderr
, "attempt to '%s' %s with %s address\n",
662 (af_hint
== AF_INET
) ? "ipv4" : "ipv6",
663 (af_hint
!= AF_INET
) ? "ipv4" : "ipv6");
670 static bool cidr_howmany(struct cidr
*a
, struct cidr
*b
)
675 if (b
->prefix
< a
->prefix
)
678 qprintf("%u", 1 << (b
->prefix
- a
->prefix
));
683 static bool cidr_prefix(struct cidr
*a
, struct cidr
*b
)
685 a
->prefix
= b
->prefix
;
689 static bool cidr_quiet(struct cidr
*a
)
698 .desc
= "Add argument to base address",
700 .f6
.a2
= cidr_add6
},
703 .desc
= "Substract argument from base address",
705 .f6
.a2
= cidr_sub6
},
708 .desc
= "Advance base address to next prefix of given size",
710 .f6
.a2
= cidr_next6
},
713 .desc
= "Lower base address to previous prefix of give size",
715 .f6
.a2
= cidr_prev6
},
718 .desc
= "Turn base address into network address",
719 .f4
.a1
= cidr_network4
,
720 .f6
.a1
= cidr_network6
},
722 { .name
= "broadcast",
723 .desc
= "Turn base address into broadcast address",
724 .f4
.a1
= cidr_broadcast4
},
727 .desc
= "Set the prefix of base address to argument",
728 .f4
.a2
= cidr_prefix
,
729 .f6
.a2
= cidr_prefix
},
732 .desc
= "Calculate netmask of base address",
733 .f4
.a1
= cidr_netmask4
},
736 .desc
= "Calculate 6to4 prefix of given ipv4-address",
737 .f4
.a1
= cidr_6to4
},
740 .desc
= "Print amount of righ-hand prefixes that fit into base address",
741 .f4
.a2
= cidr_howmany
,
742 .f6
.a2
= cidr_howmany
},
744 { .name
= "contains",
745 .desc
= "Print '1' if argument fits into base address or '0' if not",
746 .f4
.a2
= cidr_contains4
,
747 .f6
.a2
= cidr_contains6
},
750 .desc
= "Print '1' if base address is in RFC1918 private space or '0' "
752 .f4
.a1
= cidr_private4
},
754 { .name
= "linklocal",
755 .desc
= "Print '1' if base address is in 169.254.0.0/16 or FE80::/10 "
756 "link local space or '0' if not",
757 .f4
.a1
= cidr_linklocal4
,
758 .f6
.a1
= cidr_linklocal6
},
761 .desc
= "Print '1' if base address is in FC00::/7 unique local address "
762 "(ULA) space or '0' if not",
763 .f6
.a1
= cidr_ula6
},
766 .desc
= "Suppress output, useful for test operation where the result can "
767 "be inferred from the exit code",
769 .f6
.a1
= cidr_quiet
},
772 .desc
= "Pop intermediate result from stack",
777 .desc
= "Print intermediate result and pop it from stack, invoked "
778 "implicitely at the end of calculation if no intermediate prints "
780 .f4
.a1
= cidr_print4
,
781 .f6
.a1
= cidr_print6
},
784 static void usage(const char *prog
)
791 " %s {base address} operation [argument] "
792 "[operation [argument] ...]\n\n"
796 for (i
= 0; i
< sizeof(ops
) / sizeof(ops
[0]); i
++)
798 if (ops
[i
].f4
.a2
|| ops
[i
].f6
.a2
)
800 fprintf(stderr
, " %s %s\n",
802 (ops
[i
].f4
.a2
&& ops
[i
].f6
.a2
) ? "{ipv4/ipv6/amount}" :
803 (ops
[i
].f6
.a2
? "{ipv6/amount}" : "{ipv4/amount}"));
807 fprintf(stderr
, " %s\n", ops
[i
].name
);
810 fprintf(stderr
, " %s.\n", ops
[i
].desc
);
812 if ((ops
[i
].f4
.a1
&& ops
[i
].f6
.a1
) || (ops
[i
].f4
.a2
&& ops
[i
].f6
.a2
))
813 fprintf(stderr
, " Applicable to ipv4- and ipv6-addresses.\n\n");
814 else if (ops
[i
].f6
.a2
|| ops
[i
].f6
.a1
)
815 fprintf(stderr
, " Only applicable to ipv6-addresses.\n\n");
817 fprintf(stderr
, " Only applicable to ipv4-addresses.\n\n");
822 " Calculate a DHCP range:\n\n"
823 " $ %s 192.168.1.1/255.255.255.0 network add 100 print add 150 print\n"
826 " Count number of prefixes:\n\n"
827 " $ %s 2001:0DB8:FDEF::/48 howmany ::/64\n"
834 static bool runop(char ***arg
, int *status
)
838 char *arg2
= *(*arg
+1);
839 struct cidr
*a
= stack
;
840 struct cidr
*b
= NULL
;
845 for (i
= 0; i
< sizeof(ops
) / sizeof(ops
[0]); i
++)
847 if (!strcmp(ops
[i
].name
, arg1
))
849 if (ops
[i
].f4
.a2
|| ops
[i
].f6
.a2
)
853 fprintf(stderr
, "'%s' requires an argument\n",
860 b
= cidr_parse(ops
[i
].name
, arg2
, a
->family
);
864 fprintf(stderr
, "invalid address argument for '%s'\n",
873 if (((a
->family
== AF_INET
) && !ops
[i
].f4
.a2
) ||
874 ((a
->family
== AF_INET6
) && !ops
[i
].f6
.a2
))
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
.a2(a
, b
)
885 : ops
[i
].f6
.a2(a
, b
));
893 if (((a
->family
== AF_INET
) && !ops
[i
].f4
.a1
) ||
894 ((a
->family
== AF_INET6
) && !ops
[i
].f6
.a1
))
896 fprintf(stderr
, "'%s' not supported for %s addresses\n",
898 (a
->family
== AF_INET
) ? "ipv4" : "ipv6");
904 *status
= !((a
->family
== AF_INET
) ? ops
[i
].f4
.a1(a
)
915 int main(int argc
, char **argv
)
924 a
= strchr(argv
[1], ':') ? cidr_parse6(argv
[1]) : cidr_parse4(argv
[1]);
931 while (runop(&arg
, &status
));
935 fprintf(stderr
, "unknown operation '%s'\n", *arg
);
939 if (!printed
&& (status
< 2))
941 if (stack
->family
== AF_INET
)