From 2daad77ec25127315fde68729434a598b139afa0 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 24 Oct 2019 19:03:59 +0100 Subject: [PATCH] make-secnet-sites: Switch to `ipaddress' from `ipaddr' ipaddress is available in python3 and ipaddr is not. Code changes: - Change the imports and references to the module name - IPNetwork & IPAddress functions => ip_address & ip_network - There is no IPNetwork superclass so don't mention it in docstrings - collapse_address_list => collapse_addresses - There is no version parameter to ip_address; we have to switch on v ourselves and call IPv6Address or IPv4Address Administrivia: - Update debian/control and INSTALL. - Remove references to ipaddr's licence. ipaddress is under the same licence as python so does not need special mention. Signed-off-by: Ian Jackson --- INSTALL | 4 ++-- debian/control | 2 +- ipaddrset-test.py | 18 +++++++----------- ipaddrset.py | 21 ++++++++++++--------- make-secnet-sites | 9 ++++----- 5 files changed, 26 insertions(+), 28 deletions(-) diff --git a/INSTALL b/INSTALL index 020907d..fa5bf3a 100644 --- a/INSTALL +++ b/INSTALL @@ -30,8 +30,8 @@ http://vtun.sourceforge.net/tun/ You will probably be using the supplied `make-secnet-sites' program to generate your VPN's list of sites as a secnet configuration from a more-human-writeable form. If so you need to install the standard -`future' and `ipaddr' Python modules (python-future and python-ipaddr -on Debian-derived systems). +`future' and `ipaddress' Python modules (python-future and +python-ipaddress on Debian-derived systems). ** System and network configuration diff --git a/debian/control b/debian/control index e0b0f88..d4cdbd2 100644 --- a/debian/control +++ b/debian/control @@ -5,7 +5,7 @@ Maintainer: Ian Jackson Uploaders: Stephen Early , Richard Kettlewell Build-Depends: debhelper (>= 5), libgmp3-dev, libadns1-dev, bison, flex, - libbsd-dev, python-ipaddr, python-future, tclx, tcl, libtcl-chiark-1 + libbsd-dev, python-ipaddress, python-future, tclx, tcl, libtcl-chiark-1 Standards-Version: 3.0.1 Package: secnet diff --git a/ipaddrset-test.py b/ipaddrset-test.py index fdc0b6b..f17e757 100755 --- a/ipaddrset-test.py +++ b/ipaddrset-test.py @@ -12,10 +12,6 @@ # You may redistribute this fileand/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 software is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -32,27 +28,27 @@ from __future__ import print_function from __future__ import unicode_literals -import ipaddr -from ipaddr import IPNetwork, IPAddress +import ipaddress +from ipaddress import ip_network, ip_address import ipaddrset from ipaddrset import IPAddressSet -v4a=IPAddress('172.18.45.6') +v4a=ip_address('172.18.45.6') s=IPAddressSet() print('s =', s) -s.append([IPNetwork('172.18.45.0/24')]) -s.append([IPNetwork('2001:23:24::/48')]) +s.append([ip_network('172.18.45.0/24')]) +s.append([ip_network('2001:23:24::/48')]) print(s) -t=IPAddressSet(map(IPNetwork,['172.31.80.8/32','172.18.45.192/28'])) +t=IPAddressSet(map(ip_network,['172.31.80.8/32','172.18.45.192/28'])) print('t =', t) print(t <= s) print(t == s) for n1s in ['172.18.44.0/23','172.18.45.6/32','172.18.45.0/24']: - n1=IPNetwork(n1s) + n1=ip_network(n1s) print(n1) print(s.contains(n1)) print(t.contains(n1)) diff --git a/ipaddrset.py b/ipaddrset.py index 84c7dca..38d4571 100644 --- a/ipaddrset.py +++ b/ipaddrset.py @@ -1,4 +1,4 @@ -"""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. # @@ -26,7 +26,7 @@ # along with this software; if not, see # https://www.gnu.org/licenses/gpl.html. -import ipaddr +import ipaddress _vsns = [6,4] @@ -35,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] = [ ] @@ -44,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="-"): @@ -58,12 +59,12 @@ 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) @@ -143,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) @@ -151,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 diff --git a/make-secnet-sites b/make-secnet-sites index 1cc979b..158f5b3 100755 --- a/make-secnet-sites +++ b/make-secnet-sites @@ -46,8 +46,7 @@ no-suppress-args cd ~/secnet/sites-test/ execute ~/secnet/make-secnet-sites.py -u vpnheader groupfiles sites -This program is part of secnet. It relies on the "ipaddr" library from -Cendio Systems AB. +This program is part of secnet. """ @@ -62,7 +61,7 @@ import os import getopt import re -import ipaddr +import ipaddress # entry 0 is "near the executable", or maybe from PYTHONPATH=., # which we don't want to preempt @@ -148,7 +147,7 @@ def listof(subtype): class single_ipaddr (basetype): "An IP address" def __init__(self,w): - self.addr=ipaddr.IPAddress(w[1]) + self.addr=ipaddress.ip_address(w[1]) def __str__(self): return '"%s"'%self.addr @@ -157,7 +156,7 @@ class networks (basetype): def __init__(self,w): self.set=ipaddrset.IPAddressSet() for i in w[1:]: - x=ipaddr.IPNetwork(i,strict=True) + x=ipaddress.ip_network(i,strict=True) self.set.append([x]) def __str__(self): return ",".join(map((lambda n: '"%s"'%n), self.set.networks())) -- 2.30.2