X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=ipaddrset.py;h=38d45711f3735a93958725aa50fb9e338c9f7b13;hb=9eba4abfe44617aa78f625d900fe6bc2c58bb4cb;hp=8ccc0ea6212217ec0992afaff05b4779f6be4201;hpb=6437945aefa308c06ab14da291c5d5489c25b393;p=secnet.git diff --git a/ipaddrset.py b/ipaddrset.py index 8ccc0ea..38d4571 100644 --- a/ipaddrset.py +++ b/ipaddrset.py @@ -1,24 +1,32 @@ -"""IP address set manipulation, built on top of ipaddr.py""" +"""IP address set manipulation, built on top of ipaddress.py""" +# This file is Free Software. It was originally written for secnet. +# # Copyright 2014 Ian Jackson # -# This file is Free Software. It was originally written for secnet. +# You may redistribute secnet as a whole and/or modify it under the +# terms of the GNU General Public License as published by the Free +# Software Foundation; either version 3, or (at your option) any +# later version. # -# You may redistribute it and/or modify it under the terms of the GNU -# General Public License as published by the Free Software -# Foundation; either version 2, or (at your option) any later -# version. +# You may redistribute this file and/or modify it under the terms of +# the GNU General Public License as published by the Free Software +# Foundation; either version 2, or (at your option) any later version. +# Note however that this version of ipaddrset.py uses the Python +# ipaddr library from Google, which is licenced only under the Apache +# Licence, version 2.0, which is only compatible with the GNU GPL v3 +# (or perhaps later versions), and not with the GNU GPL v2. # -# This program is distributed in the hope that it will be useful, +# This software is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# along with this software; if not, see +# https://www.gnu.org/licenses/gpl.html. -import ipaddr +import ipaddress _vsns = [6,4] @@ -27,7 +35,7 @@ class IPAddressSet: # constructors def __init__(self,l=[]): - "New set contains each ipaddr.IPNetwork in the sequence l" + "New set contains each IP*Network in the sequence l" self._v = {} for v in _vsns: self._v[v] = [ ] @@ -36,7 +44,8 @@ class IPAddressSet: # housekeeping and representation def _compact(self): for v in _vsns: - self._v[v] = ipaddr.collapse_address_list(self._v[v]) + self._v[v] = list( + ipaddress.collapse_addresses(self._v[v])) def __repr__(self): return "IPAddressSet(%s)" % self.networks() def str(self,comma=",",none="-"): @@ -50,21 +59,22 @@ class IPAddressSet: # mutators def append(self,l): - "Appends each ipaddr.IPNetwork in the sequence l to self" + "Appends each IP*Network in the sequence l to self" self._append(l) self._compact() def _append(self,l): - "Appends each ipaddr.IPNetwork in the sequence l to self" + "Appends each IP*Network in the sequence l to self" for a in l: self._v[a.version].append(a) # enquirers including standard comparisons - def __nonzero__(self): + def __bool__(self): for v in _vsns: if self._v[v]: return True return False + __nonzero__=__bool__ # for python2 def __eq__(self,other): for v in _vsns: @@ -134,7 +144,7 @@ class IPAddressSet: except KeyError: v = None if v: - return self._contains_net(ipaddr.IPNetwork(thing)) + return self._contains_net(ipaddress.ip_network(thing)) else: return self.__ge__(thing) @@ -142,7 +152,9 @@ def complete_set(): "Returns a set containing all addresses" s=IPAddressSet() for v in _vsns: - a=ipaddr.IPAddress(0,v) - n=ipaddr.IPNetwork("%s/0" % a) + if v==6: a=ipaddress.IPv6Address(0) + elif v==4: a=ipaddress.IPv4Address(0) + else: raise "internal error" + n=ipaddress.ip_network("%s/0" % a) s.append([n]) return s