简体   繁体   中英

Why does the Python3 ipaddress.IPv6Network.hosts function omit the first and last IPv6 address?

I noticed that the first and last IPv6 addresses of a given network are omitted:

$ python3
Python 3.3.2 (default, Sep  6 2013, 09:30:10) 
[GCC 4.8.1 20130725 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> print("\n".join([str(x) for x in ipaddress.ip_network("2001:0db8::/120").hosts()]))
2001:db8::1
2001:db8::2
...
2001:db8::fe
>>> 
>>> hex(int(ipaddress.ip_address('2001:db8::fe')))
'0x20010db80000000000000000000000fe'

I believe that for IPv4 this is correct, as those represent the network and broadcast addresses, but I don't believe those exist in IPv6 - at least section-2.5.4 of RFC4291 doesn't seem to mention it. I checked the errata and I don't think the updating RFCs are relevant. The section on interface identifiers also doesn't appear to reserve the first/last addresses.

Does anyone know why these addresses are omitted? Is this a bug in the standard library?

EDIT: Ok, looks like the first address is a Subnet-Router anycast address , so that explains why the first address is omitted.

The last address could be a reserved anycast address , though so is for example 2001:db8::fe/120 (which you can find in the output above), so this is certainly handled inconsistently. Does anybody know why?

This is a bug in the ipaddress module , as unlike IPv4, both the first and last addresses in an IPv6 subnet are valid and usable, though they may have special uses as you've noted.

A quick survey of Python's bug tracker doesn't turn up this bug , so your next step should be to file a bug report on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM