| 1 | #!/usr/bin/python |
| 2 | |
| 3 | # This file is Free Software. It was originally written for secnet. |
| 4 | # |
| 5 | # Copyright 2014 Ian Jackson |
| 6 | # |
| 7 | # You may redistribute secnet as a whole and/or modify it under the |
| 8 | # terms of the GNU General Public License as published by the Free |
| 9 | # Software Foundation; either version 3, or (at your option) any |
| 10 | # later version. |
| 11 | # |
| 12 | # You may redistribute this fileand/or modify it under the terms of |
| 13 | # the GNU General Public License as published by the Free Software |
| 14 | # Foundation; either version 2, or (at your option) any later version. |
| 15 | # Note however that this version of ipaddrset.py uses the Python |
| 16 | # ipaddr library from Google, which is licenced only under the Apache |
| 17 | # Licence, version 2.0, which is only compatible with the GNU GPL v3 |
| 18 | # (or perhaps later versions), and not with the GNU GPL v2. |
| 19 | # |
| 20 | # This software is distributed in the hope that it will be useful, |
| 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | # GNU General Public License for more details. |
| 24 | # |
| 25 | # You should have received a copy of the GNU General Public License |
| 26 | # along with this software; if not, see |
| 27 | # https://www.gnu.org/licenses/gpl.html. |
| 28 | # |
| 29 | # The corresponding test vector file ise ipaddrset-test.expected. I |
| 30 | # don't believe it is a creative work that attracts copyright. -iwj. |
| 31 | |
| 32 | import ipaddr |
| 33 | from ipaddr import IPNetwork, IPAddress |
| 34 | |
| 35 | import ipaddrset |
| 36 | from ipaddrset import IPAddressSet |
| 37 | |
| 38 | v4a=IPAddress('172.18.45.6') |
| 39 | |
| 40 | s=IPAddressSet() |
| 41 | print 's =', s |
| 42 | s.append([IPNetwork('172.18.45.0/24')]) |
| 43 | s.append([IPNetwork('2001:23:24::/40')]) |
| 44 | print s |
| 45 | |
| 46 | t=IPAddressSet(map(IPNetwork,['172.31.80.8/32','172.18.45.192/28'])) |
| 47 | print 't =', t |
| 48 | print t <= s |
| 49 | print t == s |
| 50 | |
| 51 | for n1s in ['172.18.44.0/23','172.18.45.6/32','172.18.45.0/24']: |
| 52 | n1=IPNetwork(n1s) |
| 53 | print n1 |
| 54 | print s.contains(n1) |
| 55 | print t.contains(n1) |
| 56 | |
| 57 | n=s.networks()[0] |
| 58 | |
| 59 | a=ipaddrset.complete_set() |
| 60 | print 'a =', a |
| 61 | print a >= s |
| 62 | print a >= t |
| 63 | |
| 64 | print '^' |
| 65 | print s.intersection(t) |
| 66 | print t.intersection(s) |
| 67 | |
| 68 | print 'u' |
| 69 | print s.union(t) |
| 70 | print t.union(s) |