diff options
author | Brian Christopher Raaen <braaen@zcorum.com> | 2015-09-03 12:24:21 -0400 |
---|---|---|
committer | Brian Christopher Raaen <braaen@zcorum.com> | 2015-09-03 12:24:21 -0400 |
commit | e6efb8a4dc4a9cde1b8f94343eabc368afeeab2c (patch) | |
tree | 0af20e7f7b28a0e5946b1ceea6451b5cb9b16c35 | |
parent | 8b51cbe99c6f40aa51ed66f422eaba098711ba7c (diff) | |
parent | 2d5e4d8d56592036ce9732ebc3325c9c492c8bc7 (diff) | |
download | netaddr-e6efb8a4dc4a9cde1b8f94343eabc368afeeab2c.tar.gz |
Merge branch 'rel-0.7.x' of https://github.com/drkjam/netaddr into rel-0.7.x
-rw-r--r-- | CHANGELOG | 19 | ||||
-rw-r--r-- | docs/source/changes.rst | 2 | ||||
-rw-r--r-- | docs/source/conf.py | 2 | ||||
-rw-r--r-- | docs/source/index.rst | 2 | ||||
-rw-r--r-- | netaddr/__init__.py | 2 | ||||
-rw-r--r-- | netaddr/compat.py | 5 | ||||
-rw-r--r-- | netaddr/ip/nmap.py | 55 | ||||
-rw-r--r-- | netaddr/tests/core/test_compat.py | 7 | ||||
-rw-r--r-- | netaddr/tests/ip/test_nmap.py | 32 |
9 files changed, 100 insertions, 26 deletions
@@ -1,4 +1,23 @@ --------------- +Release: 0.7.17 +--------------- +Date: 31 Aug 2015 + +^^^^^^^^^^^^^^^^^^^^ +Changes since 0.7.16 +^^^^^^^^^^^^^^^^^^^^ + +* Fixed a regression with valid_mac due to shadow import in the + netaddr module. + +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Specific bug fixes addressed in this release +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +FIXED Issue 114: https://github.com/drkjam/netaddr/issues/114 + - netaddr.valid_mac('00-B0-D0-86-BB-F7')==False for 0.7.16 but True for 0.7.15 + +--------------- Release: 0.7.16 --------------- Date: 30 Aug 2015 diff --git a/docs/source/changes.rst b/docs/source/changes.rst index cb4d8f6..8c1d5bd 100644 --- a/docs/source/changes.rst +++ b/docs/source/changes.rst @@ -1,5 +1,5 @@ ============================ -What's new in netaddr 0.7.16 +What's new in netaddr 0.7.17 ============================ .. include:: ../../CHANGELOG diff --git a/docs/source/conf.py b/docs/source/conf.py index 83cddf8..d4fc49b 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -50,7 +50,7 @@ copyright = u'2008-2015, David P. D. Moss. All rights reserved' # The short X.Y version. version = '0.7.16' # The full version, including alpha/beta/rc tags. -release = '0.7.16' +release = '0.7.17' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/source/index.rst b/docs/source/index.rst index 855806a..575d238 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -1,5 +1,5 @@ ============================ -netaddr 0.7.16 documentation +netaddr 0.7.17 documentation ============================ .. toctree:: diff --git a/netaddr/__init__.py b/netaddr/__init__.py index a707d97..0578e0d 100644 --- a/netaddr/__init__.py +++ b/netaddr/__init__.py @@ -6,7 +6,7 @@ """A Python library for manipulating IP and EUI network addresses.""" #: Version info (major, minor, maintenance, status) -VERSION = (0, 7, 16) +VERSION = (0, 7, 17) STATUS = '' __version__ = '%d.%d.%d' % VERSION[0:3] + STATUS diff --git a/netaddr/compat.py b/netaddr/compat.py index 2a099a0..7e39ed7 100644 --- a/netaddr/compat.py +++ b/netaddr/compat.py @@ -56,6 +56,9 @@ if _sys.version_info[0] == 3: else: return f.__doc__ + def _iter_next(x): + return next(x) + elif _sys.version_info[0:2] > [2, 3]: # Python 2.4 or higher. _sys_maxint = _sys.maxint @@ -99,6 +102,8 @@ elif _sys.version_info[0:2] > [2, 3]: else: return f.func_doc + def _iter_next(x): + return x.next() else: # Unsupported versions. raise RuntimeError( diff --git a/netaddr/ip/nmap.py b/netaddr/ip/nmap.py index f04c8fb..25fa279 100644 --- a/netaddr/ip/nmap.py +++ b/netaddr/ip/nmap.py @@ -12,8 +12,8 @@ Based on nmap's Target Specification :- """ from netaddr.core import AddrFormatError -from netaddr.ip import IPAddress -from netaddr.compat import _iter_range, _is_str +from netaddr.ip import IPAddress, IPNetwork +from netaddr.compat import _iter_range, _is_str, _iter_next def _nmap_octet_target_values(spec): @@ -65,34 +65,53 @@ def _generate_nmap_octet_ranges(nmap_target_spec): _nmap_octet_target_values(tokens[3])) -def valid_nmap_range(nmap_target_spec): +def _parse_nmap_target_spec(target_spec): + if '/' in target_spec: + _, prefix = target_spec.split('/', 1) + if not (0 < int(prefix) < 33): + raise AddrFormatError('CIDR prefix expected, not %s' % prefix) + net = IPNetwork(target_spec) + if net.version != 4: + raise AddrFormatError('CIDR only support for IPv4!') + for ip in net: + yield ip + elif ':' in target_spec: + # nmap only currently supports IPv6 addresses without prefixes. + yield IPAddress(target_spec) + else: + octet_ranges = _generate_nmap_octet_ranges(target_spec) + for w in octet_ranges[0]: + for x in octet_ranges[1]: + for y in octet_ranges[2]: + for z in octet_ranges[3]: + yield IPAddress("%d.%d.%d.%d" % (w, x, y, z), 4) + + +def valid_nmap_range(target_spec): """ - :param nmap_target_spec: an nmap-style IP range target specification. + :param target_spec: an nmap-style IP range target specification. :return: ``True`` if IP range target spec is valid, ``False`` otherwise. """ try: - _generate_nmap_octet_ranges(nmap_target_spec) + _iter_next(_parse_nmap_target_spec(target_spec)) return True except (TypeError, ValueError, AddrFormatError): pass return False -def iter_nmap_range(nmap_target_spec): +def iter_nmap_range(*nmap_target_spec): """ - The nmap security tool supports a custom type of IPv4 range using multiple - hyphenated octets. This generator provides iterators yielding IP addresses - according to this rule set. + An generator that yields IPAddress objects from defined by nmap target + specifications. - :param nmap_target_spec: an nmap-style IP range target specification. + See https://nmap.org/book/man-target-specification.html for details. - :return: an iterator producing IPAddress objects for each IP in the range. - """ - octet_ranges = _generate_nmap_octet_ranges(nmap_target_spec) - for w in octet_ranges[0]: - for x in octet_ranges[1]: - for y in octet_ranges[2]: - for z in octet_ranges[3]: - yield IPAddress("%d.%d.%d.%d" % (w, x, y, z), 4) + :param *nmap_target_spec: one or more nmap IP range target specification. + :return: an iterator producing IPAddress objects for each IP in the target spec(s). + """ + for target_spec in nmap_target_spec: + for addr in _parse_nmap_target_spec(target_spec): + yield addr diff --git a/netaddr/tests/core/test_compat.py b/netaddr/tests/core/test_compat.py index 3a97e4c..3b79bbb 100644 --- a/netaddr/tests/core/test_compat.py +++ b/netaddr/tests/core/test_compat.py @@ -2,7 +2,7 @@ import sys import pytest -from netaddr.compat import _sys_maxint, _is_str, _is_int, _callable +from netaddr.compat import _sys_maxint, _is_str, _is_int, _callable, _iter_next from netaddr.compat import _dict_keys, _dict_items from netaddr.compat import _bytes_join, _zip, _range from netaddr.compat import _iter_range, _func_name, _func_doc @@ -75,3 +75,8 @@ def test_compat_callable(): assert _callable(f2) assert _func_name(f1) == 'f1' assert _func_doc(f1) == 'docstring' + + +def test_iter_next(): + it = iter([42]) + assert _iter_next(it) == 42 diff --git a/netaddr/tests/ip/test_nmap.py b/netaddr/tests/ip/test_nmap.py index 6d9813b..7b5caf8 100644 --- a/netaddr/tests/ip/test_nmap.py +++ b/netaddr/tests/ip/test_nmap.py @@ -2,19 +2,25 @@ import pytest from netaddr import valid_nmap_range, iter_nmap_range, IPAddress, AddrFormatError -def test_valid_nmap_range(): +def test_valid_nmap_range_with_valid_target_specs(): assert valid_nmap_range('192.0.2.1') assert valid_nmap_range('192.0.2.0-31') assert valid_nmap_range('192.0.2-3.1-254') assert valid_nmap_range('0-255.0-255.0-255.0-255') assert valid_nmap_range('192.168.3-5,7.1') assert valid_nmap_range('192.168.3-5,7,10-12,13,14.1') + assert valid_nmap_range('fe80::1') + assert valid_nmap_range('::') + assert valid_nmap_range('192.0.2.0/24') + +def test_valid_nmap_range_with_invalid_target_specs(): + assert not valid_nmap_range('192.0.2.0/255.255.255.0') assert not valid_nmap_range(1) assert not valid_nmap_range('1') assert not valid_nmap_range([]) assert not valid_nmap_range({}) - assert not valid_nmap_range('::') + assert not valid_nmap_range('fe80::/64') assert not valid_nmap_range('255.255.255.256') assert not valid_nmap_range('0-255.0-255.0-255.0-256') assert not valid_nmap_range('0-255.0-255.0-255.-1-0') @@ -23,6 +29,7 @@ def test_valid_nmap_range(): assert not valid_nmap_range('a.b.c.d-e') assert not valid_nmap_range('255.255.255.a-b') + def test_iter_nmap_range(): assert list(iter_nmap_range('192.0.2.1')) == [IPAddress('192.0.2.1')] @@ -52,9 +59,28 @@ def test_iter_nmap_range(): ] +def test_iter_nmap_range_with_multiple_targets_including_cidr(): + assert list(iter_nmap_range('192.168.0.0/29', '192.168.3-5,7.1', 'fe80::1')) == [ + IPAddress('192.168.0.0'), + IPAddress('192.168.0.1'), + IPAddress('192.168.0.2'), + IPAddress('192.168.0.3'), + IPAddress('192.168.0.4'), + IPAddress('192.168.0.5'), + IPAddress('192.168.0.6'), + IPAddress('192.168.0.7'), + IPAddress('192.168.3.1'), + IPAddress('192.168.4.1'), + IPAddress('192.168.5.1'), + IPAddress('192.168.7.1'), + IPAddress('fe80::1'), + ] + + def test_iter_nmap_range_invalid(): with pytest.raises(AddrFormatError): - list(iter_nmap_range('::')) + list(iter_nmap_range('fe80::/64')) + def test_iter_nmap_range_remove_duplicates(): assert list(iter_nmap_range('10.0.0.42,42-42')) == [IPAddress('10.0.0.42')] |