marvin-zhao / ipaddr-py

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

iterhosts doesn't produce the expected behavior for /31 or /127 networks #84

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
>>> x = ipaddr.IPv4Network('192.0.2.0/31')
>>> list(x)
[IPv4Address('192.0.2.0'), IPv4Address('192.0.2.1')]
>>> list(x.iterhosts())
[]

What is the expected output? What do you see instead?
for /31's it is expected that there is no network/broadcast address only 2 
hosts.
so I would expect the iterator return 2 IPv4Address objects for each /32 in the 
network

I would expect the same behavior of /127's in ipv6

What version of the product are you using? On what operating system?
2.1.8 Ubuntu

Please provide any additional information below.
Something like:

    def iterhosts(self):
        """Generate Iterator over usable hosts in a network.

           This is like __iter__ except it doesn't return the network
           or broadcast addresses.

        """
        if self._max_prefixlen - self._prefixlen > 1:
          cur = int(self.network) + 1
          bcast = int(self.broadcast) - 1
        while cur <= bcast:
            cur += 1
            yield IPAddress(cur - 1, version=self._version)

Thanks,
Marcus

Original issue reported on code.google.com by hi...@google.com on 16 May 2011 at 6:55

GoogleCodeExporter commented 9 years ago
that's the behavior of __iter__.  Are you suggesting that we just detect if we 
have an rfc3021 or rfc6164 address and use that in place of the normal 
iterhosts?

Original comment by pmo...@google.com on 17 May 2011 at 3:13

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
wow, unintelligible reply, sorry.

what I meant to say was, "that's the expected behavior of iterhosts. Are you 
suggesting we detect if we have an rfc3021 or rfc6164 addr and use __iter__ in 
place of iterhosts?"

Original comment by pmo...@google.com on 17 May 2011 at 3:44

GoogleCodeExporter commented 9 years ago
Yes, pretty much.
Just keeps getting host addresses from a network consistent for all block sizes.

Original comment by hi...@google.com on 17 May 2011 at 3:59

GoogleCodeExporter commented 9 years ago
check http://codereview.appspot.com/4523073/ and let me know if it does what 
you want. I'm not convinced that this is the right thing to do; it seems like 
we're presuming something which, while is usually the case (that a /31 means 
two hosts), isn't always the case.

Original comment by pmo...@google.com on 17 May 2011 at 4:08

GoogleCodeExporter commented 9 years ago
>>> a = ipaddr.IPNetwork('2.0.0.0/31')
>>> list(a.iterhosts())
[IPv4Address('2.0.0.0'), IPv4Address('2.0.0.1')]
>>> a = ipaddr.IPNetwork('1::/127')
>>> list(a.iterhosts())
[IPv6Address('1::'), IPv6Address('1::1')]

Looks correct - I understand that feeling. Everything was nice and happy until 
those RFC's as far as there were no exceptions to the rule on how subnets 
worked...

Original comment by hi...@google.com on 17 May 2011 at 4:13

GoogleCodeExporter commented 9 years ago
fixed in r236

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