-
Notifications
You must be signed in to change notification settings - Fork 176
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
IPy forces the user to provide normalized values #68
Comments
@SantjagoCorkez have you experimented with make_net argument to init? |
@d33tah I've checked the API. No, I didn't try this. Would help. But then I'd propose to change I'd also propose to make it automatic:
and then make it automatic in case the argument value matches AUTO. That is obvious since there's no reasonable situation when one makes a string like |
TL;DR @SantjagoCorkez initial report could be considered an unintended bug [see example later] but the fix should not assume the callers intent is to have a network object returned if the IP value provided doesn't fall on the 2-bit boundary that the netmask provided implies as this would create unintended consequences for users of certain use cases. It also should be said that the behavior is in keeping with the Perl Net::IP module that inspired IPy, thus a lot of users of this module likely appreciate that backwards compatibility when migrating legacy Perl programs to Python for the next generation. First, two observations...
The reason is that no networking device ever considers its interface IP to be x.x.x.x/32, with the exception of loopbacks. The assumption is always local IP and network mask. This seems to imply that the ValueError exception is in fact a bug. Take this IP example from @SantjagoCorkez .
This matches the behavior of the Perl's Net::IP.
With make_net=0 as the default, one would assume that only a /32 can be used without the use of make_net=1, but that is not the case. The output above suggests that you only get an exception when the IP value doesn't fall exactly on a 2-bit boundary represented by the mask, i.e. the a valid network address & mask is actually the expected input. This of course is inconsistent with the make_net=0 default. If make_net=1 were the IPy default, the implied intent would be consistent and prevent an exception but it would change historical operation and likely silently break deployed user code. So if this is a "bug", it is arguably one inherited from Perl's Net::IP due to inspiration, i.e. backwards compatibility. Therefore I only partially agree with @SantjagoCorkez since one could argue that supplying a specific IP with any network mask should be permitted because the network can always be computed when needed, though it would depart from Perl Net::IP behavior and should be carefully considered with respect to IPy module use to replace legacy Perl implementations. As a helper library, IPy could be made more flexible to accept input like you'd find on an actual network device and perhaps auto-calculate and store the network IP within the object for use by other class functions. One example of this is the original example by @SantjagoCorkez of validating if an IP is on the same network as another, without calculating two network addresses to supply. While make_net=1 can be used to check same network as @d33tah suggested, it only works in simple scenarios.
If testing direct network equality, that would be fine. But what if the network that IP2 is part of falls entirely within the network with IP1? Some would argue that the latter is also a valid "isMemberOf" sort of test. Such a comparison using make_net=1 will report false and that would not be altered by adoption of the first fix suggested by @SantjagoCorkez alone.
If the first suggestion on this issue were adopted, perhaps this nested example can be more easily construed as a "isChildOf" sort of test, where the IP and network implied by object IP2 is wholly consumed within the network implied by IP1. Naturally this could be checked with an additional comparison using IP1's netmask and IP2's IPv4 value, to compare parent networks. Unfortunately, there's no easy that I have found to obtain the CIDR netmask from an already existing IP1, short of parsing it as a string once created, but strNetmask() can be used instead.
One might also be capable of doing this sort of test using an IPSet, i.e. by combining the two IPs and checking if the set still contains a single entry or not; however, I haven't found a means to find the # of IP objects within an IPSet. But even this could produce matches you may not intend, i.e. it's better left for creating summary ranges and is excellent at doing that.
Given the potential impact to existing IPy users, in my opinion this "issue" should be considered very carefully before the code is altered. If continued compatibility with the Perl module that inspired IPy is a key goal, then perhaps adding use cases for the things @SantjagoCorkez wants to accomplish would be better. I'm guessing there are old Perl module examples that could serve as guidance, e.g. see https://metacpan.org/pod/Net::IP#overlaps and https://metacpan.org/pod/Net::IP#ip_is_overlap |
@bwmetz Thanks for your clarification. I appreciate this. |
Since IPy is a helper library it'd be very good if it does routines itself. Though, you can't provide a non-normalized prefix:
I understand that it's a standard de-facto for prefixes to have their
address
part be the first address of their range. According to RFC1812:So, if a library's user wants a prefix (by providing a mask along with an IP address to constructor) it would be great if IPy do the Network Address construction by itself, not enforcing a user do the same calculations as IPy does under the hood.
For example, a user has an IP as a string
192.168.99.230
The user wants to check, if another IP
192.168.99.126
belong to the same /25 network. In order to do this a user needs to construct a network address (all 7 last bits dropped to 0) for both (hence, take an integer equivalent) and then compare. By the time a user has ints and a mask, he does not need IPy at all any more.Instead of raising, IPy could provide a user with functionality like this:
The text was updated successfully, but these errors were encountered: