Comments (3)
Thanks for the quick and in-depth response. I really appreciate it.
from ipaddress.
Not every @omid is the @omid ;-D
from ipaddress.
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:
"fe80:0:0:0:0:0:c0a8:11", "fe80:0:0:0:0:0:c0a8:1/120"
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.
from ipaddress.
Related Issues (20)
- Unexpected address returned when pass integer to IPAddressString HOT 1
- Returns the subset of arguments in the Trie HOT 2
- Question: longestPrefixMatch in a loop based on condition HOT 4
- is there an out-of-the-box way to create an associative address trie that supports both IPv4 and IPv6? HOT 11
- HashCode issue on IPv6Address HOT 2
- Potential bug in left-padded hex encoded addresses HOT 3
- Have you considered the performance of using [Adaptive-Radix-Tree] to further improve the performance of the IP address search? HOT 3
- IPAddressParseData stack overflow HOT 2
- Extrapolating partial IP string into a Set of IPAddressSeqRange HOT 1
- How to create virtual IP ? HOT 1
- How to get mask length from IPv4 mask string? HOT 1
- Address intersection identification HOT 7
- Ip address contain method fail check HOT 3
- Small suggestion, how about handling "localhost" string too? HOT 1
- Can I ask a question about the numeric IP address and the format introduction of various IP addresses? HOT 3
- How to quickly find all group objects that may be associated with a specific IP address HOT 1
- Upper bound doesn't work properly HOT 3
- subnet contains error
- Is there a way to get address string (without length) from IPAddress? HOT 2
- puzzling behaviour with leading zeros HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ipaddress.