d22594bd765ce7e5a7ac67d5bdde3ee96ad5fed3
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));
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
.v4
, sizeof(addr
->buf
.v6
), "%s", s
);
366 addr
->family
= AF_INET6
;
368 if ((p
= strchr(addr
->buf
.v4
, '/')) != NULL
)
372 addr
->prefix
= strtoul(p
, &r
, 10);
374 if ((p
== r
) || (*r
!= 0) || (addr
->prefix
> 128))
382 if (p
== addr
->buf
.v4
+1)
383 memset(&addr
->addr
.v6
, 0, sizeof(addr
->addr
.v6
));
384 else if (inet_pton(AF_INET6
, addr
->buf
.v4
, &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 cidr
*n
= cidr_clone(a
);
531 struct in6_addr
*x
= &n
->addr
.v6
;
532 struct in6_addr
*y
= &b
->addr
.v6
;
533 uint8_t i
= (128 - n
->prefix
) / 8;
534 uint8_t m
= ~((1 << ((128 - n
->prefix
) % 8)) - 1);
535 uint8_t net1
= x
->s6_addr
[15-i
] & m
;
536 uint8_t net2
= y
->s6_addr
[15-i
] & m
;
541 if ((b
->prefix
>= n
->prefix
) && (net1
== net2
) &&
542 ((i
== 15) || !memcmp(&x
->s6_addr
, &y
->s6_addr
, 15-i
)))
554 static bool cidr_linklocal6(struct cidr
*a
)
559 if ((a
->addr
.v6
.s6_addr
[0] == 0xFE) &&
560 (a
->addr
.v6
.s6_addr
[1] >= 0x80) &&
561 (a
->addr
.v6
.s6_addr
[1] <= 0xBF))
573 static bool cidr_ula6(struct cidr
*a
)
578 if ((a
->addr
.v6
.s6_addr
[0] >= 0xFC) &&
579 (a
->addr
.v6
.s6_addr
[0] <= 0xFD))
591 static bool cidr_print6(struct cidr
*a
)
595 if (!a
|| (a
->family
!= AF_INET6
))
598 if (!(p
= (char *)inet_ntop(AF_INET6
, &a
->addr
.v6
, a
->buf
.v6
, sizeof(a
->buf
.v6
))))
607 qprintf("/%u", a
->prefix
);
615 static struct cidr
* cidr_parse(const char *op
, const char *s
, int af_hint
)
621 uint32_t sum
= strtoul(s
, &r
, 0);
623 if ((r
> s
) && (*r
== 0))
625 a
= malloc(sizeof(struct cidr
));
630 if (af_hint
== AF_INET
)
634 a
->addr
.v4
.s_addr
= htonl(sum
);
638 a
->family
= AF_INET6
;
641 for (i
= 0; i
<= 15; i
++)
643 a
->addr
.v6
.s6_addr
[15-i
] = sum
% 256;
659 if (a
->family
!= af_hint
)
661 fprintf(stderr
, "attempt to '%s' %s with %s address\n",
663 (af_hint
== AF_INET
) ? "ipv4" : "ipv6",
664 (af_hint
!= AF_INET
) ? "ipv4" : "ipv6");
671 static bool cidr_howmany(struct cidr
*a
, struct cidr
*b
)
676 if (b
->prefix
< a
->prefix
)
679 qprintf("%u", 1 << (b
->prefix
- a
->prefix
));
684 static bool cidr_prefix(struct cidr
*a
, struct cidr
*b
)
686 a
->prefix
= b
->prefix
;
690 static bool cidr_quiet(struct cidr
*a
)
699 .desc
= "Add argument to base address",
701 .f6
.a2
= cidr_add6
},
704 .desc
= "Substract argument from base address",
706 .f6
.a2
= cidr_sub6
},
709 .desc
= "Advance base address to next prefix of given size",
711 .f6
.a2
= cidr_next6
},
714 .desc
= "Lower base address to previous prefix of give size",
716 .f6
.a2
= cidr_prev6
},
719 .desc
= "Turn base address into network address",
720 .f4
.a1
= cidr_network4
,
721 .f6
.a1
= cidr_network6
},
723 { .name
= "broadcast",
724 .desc
= "Turn base address into broadcast address",
725 .f4
.a1
= cidr_broadcast4
},
728 .desc
= "Set the prefix of base address to argument",
729 .f4
.a2
= cidr_prefix
,
730 .f6
.a2
= cidr_prefix
},
733 .desc
= "Calculate netmask of base address",
734 .f4
.a1
= cidr_netmask4
},
737 .desc
= "Calculate 6to4 prefix of given ipv4-address",
738 .f4
.a1
= cidr_6to4
},
741 .desc
= "Print amount of righ-hand prefixes that fit into base address",
742 .f4
.a2
= cidr_howmany
,
743 .f6
.a2
= cidr_howmany
},
745 { .name
= "contains",
746 .desc
= "Print '1' if argument fits into base address or '0' if not",
747 .f4
.a2
= cidr_contains4
,
748 .f6
.a2
= cidr_contains6
},
751 .desc
= "Print '1' if base address is in RFC1918 private space or '0' "
753 .f4
.a1
= cidr_private4
},
755 { .name
= "linklocal",
756 .desc
= "Print '1' if base address is in 169.254.0.0/16 or FE80::/10 "
757 "link local space or '0' if not",
758 .f4
.a1
= cidr_linklocal4
,
759 .f6
.a1
= cidr_linklocal6
},
762 .desc
= "Print '1' if base address is in FC00::/7 unique local address "
763 "(ULA) space or '0' if not",
764 .f6
.a1
= cidr_ula6
},
767 .desc
= "Suppress output, useful for test operation where the result can "
768 "be inferred from the exit code",
770 .f6
.a1
= cidr_quiet
},
773 .desc
= "Pop intermediate result from stack",
778 .desc
= "Print intermediate result and pop it from stack, invoked "
779 "implicitely at the end of calculation if no intermediate prints "
781 .f4
.a1
= cidr_print4
,
782 .f6
.a1
= cidr_print6
},
785 static void usage(const char *prog
)
792 " %s {base address} operation [argument] "
793 "[operation [argument] ...]\n\n"
797 for (i
= 0; i
< sizeof(ops
) / sizeof(ops
[0]); i
++)
799 if (ops
[i
].f4
.a2
|| ops
[i
].f6
.a2
)
801 fprintf(stderr
, " %s %s\n",
803 (ops
[i
].f4
.a2
&& ops
[i
].f6
.a2
) ? "{ipv4/ipv6/amount}" :
804 (ops
[i
].f6
.a2
? "{ipv6/amount}" : "{ipv4/amount}"));
808 fprintf(stderr
, " %s\n", ops
[i
].name
);
811 fprintf(stderr
, " %s.\n", ops
[i
].desc
);
813 if ((ops
[i
].f4
.a1
&& ops
[i
].f6
.a1
) || (ops
[i
].f4
.a2
&& ops
[i
].f6
.a2
))
814 fprintf(stderr
, " Applicable to ipv4- and ipv6-addresses.\n\n");
815 else if (ops
[i
].f6
.a2
|| ops
[i
].f6
.a1
)
816 fprintf(stderr
, " Only applicable to ipv6-addresses.\n\n");
818 fprintf(stderr
, " Only applicable to ipv4-addresses.\n\n");
823 " Calculate a DHCP range:\n\n"
824 " $ %s 192.168.1.1/255.255.255.0 network add 100 print add 150 print\n"
827 " Count number of prefixes:\n\n"
828 " $ %s 2001:0DB8:FDEF::/48 howmany ::/64\n"
835 static bool runop(char ***arg
, int *status
)
839 char *arg2
= *(*arg
+1);
840 struct cidr
*a
= stack
;
841 struct cidr
*b
= NULL
;
846 for (i
= 0; i
< sizeof(ops
) / sizeof(ops
[0]); i
++)
848 if (!strcmp(ops
[i
].name
, arg1
))
850 if (ops
[i
].f4
.a2
|| ops
[i
].f6
.a2
)
854 fprintf(stderr
, "'%s' requires an argument\n",
861 b
= cidr_parse(ops
[i
].name
, arg2
, a
->family
);
865 fprintf(stderr
, "invalid address argument for '%s'\n",
874 if (((a
->family
== AF_INET
) && !ops
[i
].f4
.a2
) ||
875 ((a
->family
== AF_INET6
) && !ops
[i
].f6
.a2
))
877 fprintf(stderr
, "'%s' not supported for %s addresses\n",
879 (a
->family
== AF_INET
) ? "ipv4" : "ipv6");
885 *status
= !((a
->family
== AF_INET
) ? ops
[i
].f4
.a2(a
, b
)
886 : ops
[i
].f6
.a2(a
, b
));
894 if (((a
->family
== AF_INET
) && !ops
[i
].f4
.a1
) ||
895 ((a
->family
== AF_INET6
) && !ops
[i
].f6
.a1
))
897 fprintf(stderr
, "'%s' not supported for %s addresses\n",
899 (a
->family
== AF_INET
) ? "ipv4" : "ipv6");
905 *status
= !((a
->family
== AF_INET
) ? ops
[i
].f4
.a1(a
)
916 int main(int argc
, char **argv
)
925 a
= strchr(argv
[1], ':') ? cidr_parse6(argv
[1]) : cidr_parse4(argv
[1]);
932 while (runop(&arg
, &status
));
936 fprintf(stderr
, "unknown operation '%s'\n", *arg
);
940 if (!printed
&& (status
< 2))
942 if (stack
->family
== AF_INET
)