6d7a8aaf43870203210890b43ab76cd2199d33aa
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_quiet(struct cidr
*a
)
530 .desc
= "Add argument to base address",
532 .f6
.a2
= cidr_add6
},
535 .desc
= "Substract argument from base address",
537 .f6
.a2
= cidr_sub6
},
540 .desc
= "Turn base address into network address",
541 .f4
.a1
= cidr_network4
,
542 .f6
.a1
= cidr_network6
},
544 { .name
= "broadcast",
545 .desc
= "Turn base address into broadcast address",
546 .f4
.a1
= cidr_broadcast4
},
549 .desc
= "Print netmask of base address, does not change base address",
550 .f4
.a1
= cidr_netmask4
},
553 .desc
= "Print amount of righ-hand prefixes that fit into base address, "
554 "does not change base address",
555 .f4
.a2
= cidr_howmany
,
556 .f6
.a2
= cidr_howmany
},
558 { .name
= "contains",
559 .desc
= "Print '1' if argument fits into base address or '0' "
560 "if not, does not change base address",
561 .f4
.a2
= cidr_contains4
,
562 .f6
.a2
= cidr_contains6
},
565 .desc
= "Print '1' if base address is in RFC1918 private space or '0' "
566 "if not, does not change base address",
567 .f4
.a1
= cidr_private4
},
569 { .name
= "linklocal",
570 .desc
= "Print '1' if base address is in 169.254.0.0/16 or FE80::/10 "
571 "link local space or '0' if not, does not change base address",
572 .f4
.a1
= cidr_linklocal4
,
573 .f6
.a1
= cidr_linklocal6
},
576 .desc
= "Print '1' if base address is in FC00::/7 unique local address "
577 "(ULA) space or '0' if not, does not change base address",
578 .f6
.a1
= cidr_ula6
},
581 .desc
= "Suppress output, useful for test operation where the result can "
582 "be inferred from the exit code, does not change base address",
584 .f6
.a1
= cidr_quiet
},
587 .desc
= "Print intermediate result, invoked implicitely at the end of "
588 "calculation if no intermediate prints happened",
589 .f4
.a1
= cidr_print4
,
590 .f6
.a1
= cidr_print6
},
593 static void usage(const char *prog
)
600 " %s {base address} operation [argument] "
601 "[operation [argument] ...]\n\n"
605 for (i
= 0; i
< sizeof(ops
) / sizeof(ops
[0]); i
++)
607 if (ops
[i
].f4
.a2
|| ops
[i
].f6
.a2
)
609 fprintf(stderr
, " %s %s\n",
611 (ops
[i
].f4
.a2
&& ops
[i
].f6
.a2
) ? "{ipv4/ipv6/amount}" :
612 (ops
[i
].f6
.a2
? "{ipv6/amount}" : "{ipv4/amount}"));
616 fprintf(stderr
, " %s\n", ops
[i
].name
);
619 fprintf(stderr
, " %s.\n", ops
[i
].desc
);
621 if ((ops
[i
].f4
.a1
&& ops
[i
].f6
.a1
) || (ops
[i
].f4
.a2
&& ops
[i
].f6
.a2
))
622 fprintf(stderr
, " Applicable to ipv4- and ipv6-addresses.\n\n");
623 else if (ops
[i
].f6
.a2
|| ops
[i
].f6
.a1
)
624 fprintf(stderr
, " Only applicable to ipv6-addresses.\n\n");
626 fprintf(stderr
, " Only applicable to ipv4-addresses.\n\n");
631 " Calculate a DHCP range:\n\n"
632 " $ %s 192.168.1.1/255.255.255.0 network add 100 print add 150 print\n"
635 " Count number of prefixes:\n\n"
636 " $ %s 2001:0DB8:FDEF::/48 howmany ::/64\n"
643 static bool runop(struct cidr
*a
, char ***arg
, int *status
)
647 char *arg2
= *(*arg
+1);
648 struct cidr
*b
= NULL
;
653 for (i
= 0; i
< sizeof(ops
) / sizeof(ops
[0]); i
++)
655 if (!strcmp(ops
[i
].name
, arg1
))
657 if (ops
[i
].f4
.a2
|| ops
[i
].f6
.a2
)
661 fprintf(stderr
, "'%s' requires an argument\n",
668 b
= cidr_parse(ops
[i
].name
, arg2
, a
->family
);
672 fprintf(stderr
, "invalid address argument for '%s'\n",
681 if (((a
->family
== AF_INET
) && !ops
[i
].f4
.a2
) ||
682 ((a
->family
== AF_INET6
) && !ops
[i
].f6
.a2
))
684 fprintf(stderr
, "'%s' not supported for %s addresses\n",
686 (a
->family
== AF_INET
) ? "ipv4" : "ipv6");
692 *status
= !((a
->family
== AF_INET
) ? ops
[i
].f4
.a2(a
, b
)
693 : ops
[i
].f6
.a2(a
, b
));
701 if (((a
->family
== AF_INET
) && !ops
[i
].f4
.a1
) ||
702 ((a
->family
== AF_INET6
) && !ops
[i
].f6
.a1
))
704 fprintf(stderr
, "'%s' not supported for %s addresses\n",
706 (a
->family
== AF_INET
) ? "ipv4" : "ipv6");
712 *status
= !((a
->family
== AF_INET
) ? ops
[i
].f4
.a1(a
)
723 int main(int argc
, char **argv
)
732 a
= strchr(argv
[1], ':') ? cidr_parse6(argv
[1]) : cidr_parse4(argv
[1]);
737 while (runop(a
, &arg
, &status
));
741 fprintf(stderr
, "unknown operation '%s'\n", *arg
);
745 if (!printed
&& (status
< 2))
747 if (a
->family
== AF_INET
)