seancfoley / IPAddress

Java library for handling IP addresses and subnets, both IPv4 and IPv6
https://seancfoley.github.io/IPAddress/
Apache License 2.0
469 stars 63 forks source link

Is there a high performance way to check IP addresses collection exist in a containment relationship ? #83

Closed angelyouyou closed 2 years ago

angelyouyou commented 2 years ago

Hi. I have a collection of addresses which may be single or IP addresses in CIDR format(IPv4 or/and IPv6). I want to efficiently confirm whether these IP addresses exist in a containment relationship. May I ask if there is such a high performance interface or method to accomplish this kind of check? Thank you very much!

For example: A collection need to be checked for conflict: case 1: {1.1.1.1/24, 1.1.1.3/32, 2.2.2.2/24} return true(conflict) case 2: {1.1.1.1/24, 3.1.1.3/32, 2.2.2.2/24} return false(no conflict)

seancfoley commented 2 years ago

"May I ask if there is such a high performance interface or method to accomplish this kind of check?"

Yes, that is why I added the address tries to the library, it is an efficient method for checking containment of many addresses and subnets at once.

However, I am confused by your example, it seems you are saying 2.2.2.2 is in 1.1.1.1/24 and it is also not in 1.1.1.1/24 at the same time? Also, I think you may mean the CIDR block 1.1.1.0/24, the enclosing CIDR block for 1.1.1.1/24. If you could be more clear with your example that would be useful.

angelyouyou commented 2 years ago

@seancfoley Sorry for not replying to your message in time. Returning true in my example means there is an address conflict (containment relationship) in the collection, otherwise it doesn't. Case 1: {1.1.1.1/24, 1.1.1.3/32, 2.2.2.2/24} Since 1.1.1.1/24 contains the address 1.1.1.3/32, there is an address conflict (containment relationship), so it returns true.

Case 2: {1.1.1.1/24, 3.1.1.3/32, 2.2.2.2/24} These 3 addresses do not have any conflict (inclusive relationship), so return false.

Is there a high performance way to check the confliction in a address collection?