pbiering/ipv6calc

Several questions regarding IPv6 categorization

Closed this issue · 10 comments

Hi,

I have a list with some IPv6 addresses I would like to put them into perspective.
I would like to specify to which of those assignment a specific IP belongs.
http://www.iana.org/assignments/ipv6-unicast-address-assignments/ipv6-unicast-address-assignments.xhtml
What I need to do for that is making a comparison of a given network prefix like 2001:0600::/23 and see if my specific IP address belongs to that range.

Is there any way your toolkit can do this for me? If not, do you have any suggestions for me?

You have several options masking an IPv6 address to its prefix using "ipv6calc", best is using "maskprefix" and then string-compare the result.

Your reference: 2001:600::/23 (you can also normalize it in front of in case leading 0 are given by accident, e.g. using "--printfulluncompressed" in addition).

Test 1 (inside):

$ ipv6calc -I ipv6addr -O ipv6addr  2001:600::1/23 --maskprefix 
2001:600::/23  <- equal string

Test 2 (outside):

$ ipv6calc -I ipv6addr -O ipv6addr  2001:1600::1/23 --maskprefix 
2001:1600::/23  <- not equal string

If one more vote for a feature which check whether a given IPv(4|6) address is inside a given prefix/length and return then "inside" or "outside" (or different exit codes), I can implement this easily

BTW: -O ... and -I .. are optional, but then "-q" should be used to avoid autodetection information.

One can also use the filter option capabilities:

$ echo -e "2001:db8::1\n2001:db9::1" | ipv6calc -E ipv6.addr=2001:db8::/32
2001:db8::1
$ echo -e "2001:db8::1\n2001:db9::1" | ipv6calc -E ipv6.addr=2001:db9::/32
2001:db9::1

BTW: --test_prefix PREFIX is on the way being implemented and will work in pipe mode (appending included|excluded) or single shot (here with different exit codes).

You can now try new option --test_prefix PREFIX from branch option-test_prefix

Thank you so much. This helps me a lot.
I have one other question. What if I had a starting address like: 2804:490:: and an ending address like: 2804:490:ffff:ffff:ffff:ffff:ffff:ffff.
Can I use your tool, to specify the corresponding prefix, which would be 2804:490::/32 here?
Thank you!

No, autodetection of a prefix from a given range (begin + end) is not supported. But as a workaround, you can retrieve a prefix from given "end" with the tool by some scripting, starting

$ ipv6calc -q 2804:490:: -O hex 
28040490000000000000000000000000

$ ipv6calc -q 2804:490:ffff:ffff:ffff:ffff:ffff:ffff -O hex 
28040490ffffffffffffffffffffffff

then count the continous "f" from right to left (by checking that corresponding start has still "0") and multiply the amount of "f" by 4, then calculate 128 - result and you should have the prefix length.

Perhaps I would also implement besides --test_prefix $prefix something like --test_begin $startIP --test_end $endIP

Thanks a lot. Works like a charm 👍

Edit:
Actually, it doesn't.
With your formula this network:
first: 2003:71:c800::
last: 2003:71:cfff:ffff:ffff:ffff:ffff:ffff
results in /40, which is actually a /37... :-(

Edit2:
I figured it out. I converted both start and end address into binary and compared every single bit. I count until the first time the bits differ. This is how I got the right prefix size.

You can now also use fresh implemented options --test_ge IP-BEGIN --test_le IP-END if using latest push to branch option_test_prefix (besides that also --test_gt and --test_lt is supported, if required)

$ ./ipv6calc --test_ge 2804:490:: --test_le 2804:490:ffff:ffff:ffff:ffff:ffff:ffff 2804:490::1; echo $?
no input type specified, try autodetection...found type: ipv6addr
No output type specified, try autodetection...found type: ipv6addr
2804:490::1   greater/equal than 2804:490::  less/equal than 2804:490:ffff:ffff:ffff:ffff:ffff:ffff
0
$ ./ipv6calc --test_ge 2804:490:: --test_le 2804:490:ffff:ffff:ffff:ffff:ffff:ffff 2001:db8::1; echo $?
no input type specified, try autodetection...found type: ipv6addr
No output type specified, try autodetection...found type: ipv6addr
2001:db8::1   NOT greater/equal than 2804:490::  less/equal than 2804:490:ffff:ffff:ffff:ffff:ffff:ffff
1

For those which like to use the filter option, range support is now added also:

$ echo "2001:db9::2" | ./ipv6calc -A filter -E ipv6.addr=ge=2001:db8::,ipv6.addr=le=2001:db8:ffff:ffff:ffff:ffff:ffff:ffff
(empty)

$ echo "2001:db8::2" | ./ipv6calc -A filter -E ipv6.addr=ge=2001:db8::,ipv6.addr=le=2001:db8:ffff:ffff:ffff:ffff:ffff:ffff
2001:db8::2

Branch with new filter action merged now into master