tehmaze/ipcalc

Strange subnet calculations

Closed this issue · 2 comments

I think you are doing strange calculations. (You aren't doing things with binary (and,or,xor) operations?)
The three following examples are plain wrong.

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import ipcalc
ipcalc.version
'0.5'
from ipcalc import Network
n = Network("129.0.0.0/2")
"128.0.0.0" in n
False
n.in_network("128.0.0.1")
False
"192.0.0.0" in n
True

You aren't doing network calculations based upon the network address, but upon the representative the network is generated from.
This is a hack, which fixes the issues above. A cleaner solution would be to "normalize" the network address upon generation. This would be cleaner, for comparison, also. (e.g. Network("0.0.0.1/24") == Network("0.0.0.2/24")) This does not seem to be supported atm.

--- ipcalc.py   2011-12-20 12:34:35.000000000 +0100
+++ ipcalc-new.py   2012-01-22 17:32:42.000000000 +0100
@@ -499,7 +499,7 @@
         Check if the given IP address is within this network.
         '''
         other = Network(other)
-        return long(other) >= long(self) and long(other) < long(self) + self.size() - other.size() + 1
+        return long(other) >= long(self.network()) and long(other) < long(self.network()) + self.size() - other.size() + 1

     def __contains__(self, ip):
         '''

Some form of normalisation is definately required, will be looking at this shortly,