h0arry / ipaddr-py

Automatically exported from code.google.com/p/ipaddr-py
0 stars 0 forks source link

_BaseNet.numhosts is incorrect #91

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
numhosts function, line 684:

    @property
    def numhosts(self):
        """Number of hosts in the current subnet."""
        return int(self.broadcast) - int(self.network) + 1

Should be:

    @property
    def numhosts(self):
        """Number of hosts in the current subnet."""
        return int(self.broadcast) - int(self.network) - 1

Example:

>>> ipnet = ipaddr.IPNetwork('192.168.0.0/24')
>>> len([a for a in ipnet.iterhosts()])
254
>>> int(ipnet.broadcast)-int(ipnet.network)+1
256
>>> int(ipnet.broadcast)-int(ipnet.network)-1
254

Original issue reported on code.google.com by gr...@jingojango.net on 27 Oct 2011 at 7:35

GoogleCodeExporter commented 9 years ago
I see this plays havoc with single hosts (/32)...

>>> ipnet = ipaddr.IPNetwork('192.168.1.2')
>>> len([a for a in ipnet.iterhosts()])
0
>>> int(ipnet.broadcast)-int(ipnet.network)+1
1
>>> int(ipnet.broadcast)-int(ipnet.network)-1
-1

Original comment by gr...@jingojango.net on 27 Oct 2011 at 7:45

GoogleCodeExporter commented 9 years ago
There's a difference, albeit somewhat of a subtle one, between numhosts and 
iterhosts. numhosts returns the number of possible addresses on a given network 
while iterhosts returns usable addresses (eg, - network and broadcast).

I could probably fix the method names to make the distinction a little clearer.

Original comment by pmo...@google.com on 24 Nov 2011 at 9:32