-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for IPv6 as shown #40
Comments
This is really the same as the previous issue, issue #39. The IPAddress library uses the same types for both subnets and individual addresses. Other libraries do not do this, they typically have one type for an address, another type for a subnet. In practice, using the same type can be really useful (I was motivated to do this myself for my own use-cases at first). However, there is one aspect to that which confuses some people at first. There is a basic convention regarding how to specify CIDR block subnets vs addresses, which also matches the same convention typically used by network admins. If you specify an address string with a zero host, it is considered to be a subnet, otherwise it is considered to be a single address. So, in your example, the difference is this pair: The other library says the second contains the first, mine says it does not. In my library, the second one is not a subnet, it is the same as "fe80:0:0:0:0:0:c0a8:1", it just happens to have a prefix length associated, and since the two addresses are not the same address, false is the result. It is not a subnet because of the 1 at the end. The host, the last 8 bits, is not 0. And in fact, a network admin would say the same, "fe80:0:0:0:0:0:c0a8:1/120" is not a subnet. Why would someone stick the 1 at the end if it does not need to be there? However, in other libraries which have different types for addresses and subnets, when feeding that string into the subnet type it will of course try to convert it to a subnet, that is the only thing it can be in that context. Anyway, all that being said... If you want to be sure to get the subnet, use the toPrefixBlock() method. toPrefixBlock() converts an address with a prefix length to the associated prefix block subnet. Otherwise, to get the subnet, your address must have a zero host (ie the bits after the prefix must be zero). contains("fe80:0:0:0:0:0:c0a8:11", "fe80:0:0:0:0:0:c0a8:1/120"); // true, with this method, second is considered a subnet, not an address
contains("fe80:0:0:0:0:0:c0a8:11", "fe80:0:0:0:0:0:c0a8:0/120"); // true
containsFromString("fe80:0:0:0:0:0:c0a8:11", "fe80:0:0:0:0:0:c0a8:1/120"); // false, second is not a subnet, host is not zero
containsFromString("fe80:0:0:0:0:0:c0a8:11", "fe80:0:0:0:0:0:c0a8:0/120"); // true, second is a subnet because host is zero
static void containsFromString(String address, String network) {
IPAddressString one = new IPAddressString(network);
IPAddressString two = new IPAddressString(address);
System.out.println(one + " string contains " + two + " " + one.contains(two));
}
static void contains(String address, String network) {
IPAddressString one = new IPAddressString(network);
IPAddressString two = new IPAddressString(address);
System.out.println(one + " contains " + two + " " + one.getAddress().toPrefixBlock().contains(two.getAddress()));
} See here or the previous issue for more details. |
Thanks for the quick and in-depth response. I really appreciate it. |
Hello! Great library and thanks for your effort :)
I found it through the following SO answer:
https://stackoverflow.com/questions/577363/how-to-check-if-an-ip-address-is-from-a-particular-network-netmask-in-java/41482123#41482123
I was writing some unit tests against your answer and @omid's answer and found that your subnet test failed in the IPv6 example while his IpAddressMatcher passed.
Am I missing something?
Omid's Matcher:
My Unit Tests (in Kotlin):
The text was updated successfully, but these errors were encountered: