[Python-Dev] PEP 3144 review.
Andrew McNamara
andrewm at object-craft.com.au
Fri Sep 18 06:26:19 CEST 2009
>On Fri, 18 Sep 2009 11:04:46 am Andrew McNamara wrote:
>> >To a non-specialist, "the network address" is ambiguous. There are
>> > many addresses in a network, and none of them are the entire
>> > network. It's like saying, given a list [2, 4, 8, 12], what's "the
>> > list item"?
>>
>> A "network address" is an IP address and mask, but I understand your
>> confusion - we're mixing terminology from disperate domains. In my
>> postings, I have tried to refer to Network (a containter) and Address
>> (an item).
>
>So to clarify, how many different things which need to be handled are
>there?
>
>Items:
>1 IP address -- a 32 bit (IPv4) or 128 bit (IPv6) number
Yes.
>2 Netmask -- a bit mask of the form 111..100..0
I don't think there's much to be gained by exposing a Netmask object,
although other objects might have a .netmask property returning an
IPAddress instance. Where we expose a netmask, it should be as an
Address instance (or maybe a subclass with additional restrictions).
>3 Network address -- the lowest address in a network, and equal
> to (defined by?) the bitwise-AND of any address in the network
> with the network's netmask
This idea of a "network address" being simply an IP address is in error -
a network address was always an address and a mask, however in the
days prior to CIDR, the mask was implicitly specified by the class of
the network.
>4 Host address -- the part of the IP address that is not masked
> by the netmask
Well, yes, but I don't think we need an entity representing that.
>5 Broadcast address -- the highest address in a IPv4 network
Yes, but again, we don't need an entity - as with the netmask, when
exposed, it should just be an Address instance (or subclass thereof).
>Containers:
>6 Network -- a range of IP address
Yes, although not an arbitrary or discontinuous range of address.
Really, I think we just need two entities (per protocol):
Address (& maybe AddressWithMask)
* If no mask is specified, this is just the IP address.
* If a mask is specified, then it gains a .network property returning a
Network instance. It probably should also have a .netmask property
containing an Address instance.
Network
* Has an IP address with netmask
* for consistency's sake, masked address bits are not allowed
* behaves like a read-only container wrt Addresses
So, you want to represent an interface on your host:
>>> if_addr = IPv4Address('10.0.0.1/24')
from this, you could get:
>>> if_addr.address
IPv4Address('10.0.0.1')
>>> if_addr.netmask
IPv4Address('255.255.255.0')
>>> if_addr.broadcast
IPv4Address('10.0.0.255')
>>> if_addr.network
IPV4Network('10.0.0.0/24')
you might also have an address for the default gateway:
>>> router_addr = IPv4Address('10.0.0.254/24')
>>> router_addr in if_addr.network
True
or:
>>> router_addr = IPv4Address('10.0.0.254')
>>> router_addr in if_addr.network
True
Or maybe you've subneted your LAN:
>>> IPV4Network('10.0.0.0/24') in IPv4Network('10.0.0.0/8')
True
>>> IPV4Network('10.0.1.0/24') in IPv4Network('10.0.0.0/8')
True
but:
>>> IPV4Network('10.0.0.0/8') in IPv4Network('10.0.0.0/24')
False
This suggests the natural behaviour if the Address mask doesn't fit in the
network:
>>> IPv4Address('10.0.0.254/8') in IPv4Network('10.0.0.0/24')
False
--
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/
More information about the Python-Dev
mailing list