Git Product home page Git Product logo

Comments (3)

spragucm avatar spragucm commented on May 20, 2024 1

Thanks for the quick and in-depth response. I really appreciate it.

from ipaddress.

omid avatar omid commented on May 20, 2024

Not every @omid is the @omid ;-D

from ipaddress.

seancfoley avatar seancfoley commented on May 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.