network / address comparisons

6 views
Skip to first unread message

Peter Moody

unread,
Feb 6, 2010, 5:44:42 PM2/6/10
to ipaddr-py-dev
working through the issues so I can (finally) get another release out and i'm looking at issue28, comparisons between addresses and networks. I'm looking for the cleanest way to do this.

it seems like, when doing <, <=, > and >=, the ip portion of the network address should be ignored lest we end up with super unpredictable results. In that case we're just comparing the the IPAddress with the IPNetwork + netmask. Following this, I would expect

>>> IPAddress('1.1.1.0') < IPNetwork('1.1.1.1/24') (basically, 1.1.1.1 < IPNetwork(1.1.1.<0-255))
True

but then how would the following evaluate?
>>> IPAddress('1.1.1.1') < IPNetwork('1.1.1.0/24')

comparing only the network address of the network object, address > network, but intuitively the network seems larger.  That would lead me to think that a

if address in network:
  # network is larger

but then, should we just be comparing the address's ._ip with the network's .broadcast?

or should networks and addresses just not be sortable? thoughts?

Cheers,
/peter

--
Peter Moody      Google    1.650.253.7306    
Network Security Engineer  pgp:0xC3410038

Dj Gilcrease

unread,
Feb 6, 2010, 7:07:14 PM2/6/10
to Peter Moody, ipaddr-py-dev
On Sat, Feb 6, 2010 at 10:44 AM, Peter Moody <pmo...@google.com> wrote:
> or should networks and addresses just not be sortable? thoughts?


I would say addresses and networks should not be sortable as they
represent two different objects. It would be like trying to sort
watches and gears together.

If sorting of them together in the same list is a must have I would
expect a address to always compare less then a network so the list had
all the networks sorted first then the addresses. For the example from
issue #28 I would expect the following results.

192.0.2.0/28
192.0.2.0/29
192.0.2.0/30
192.0.2.0/31
192.0.2.2/31
192.0.2.4/30
192.0.2.4/31
192.0.2.6/31
192.0.2.8/29
192.0.2.8/30
192.0.2.8/31
192.0.2.10/31
192.0.2.12/30
192.0.2.12/31
192.0.2.14/31
192.0.2.0
192.0.2.1
192.0.2.2
192.0.2.3
192.0.2.4
192.0.2.5
192.0.2.6
192.0.2.7
192.0.2.8
192.0.2.9
192.0.2.10
192.0.2.11
192.0.2.12
192.0.2.13
192.0.2.14
192.0.2.15

Dj Gilcrease

unread,
Feb 6, 2010, 7:08:44 PM2/6/10
to ipaddr-py-dev
On Sat, Feb 6, 2010 at 10:44 AM, Peter Moody <pmo...@google.com> wrote:
> or should networks and addresses just not be sortable? thoughts?

Peter Moody

unread,
Feb 8, 2010, 5:33:38 PM2/8/10
to Dj Gilcrease, ipaddr-py-dev
Unless anyone has any strenuous objections, I'll add a mixed sort function the user can pass to sorted() if they want to sort networks and addresses and I'll make the default be that they're not sortable.

Michael Shields

unread,
Feb 8, 2010, 5:51:28 PM2/8/10
to Peter Moody, Dj Gilcrease, ipaddr-py-dev


On Mon, Feb 8, 2010 at 9:33 AM, Peter Moody <pmo...@google.com> wrote:
Unless anyone has any strenuous objections, I'll add a mixed sort function the user can pass to sorted() if they want to sort networks and addresses and I'll make the default be that they're not sortable.

I think that's a good idea.  Please make it a key function, not a comparator.

Peter Moody

unread,
Feb 8, 2010, 5:54:27 PM2/8/10
to Michael Shields, Dj Gilcrease, ipaddr-py-dev
ok.
Reply all
Reply to author
Forward
0 new messages