Skip to content

Commit 17512c4

Browse files
committed
Constants module for common and special use blocks.
Add collection of predefined variables for common and special use IPv4 address blocks in common use on the Internet. Closes #3.
1 parent 398a1f3 commit 17512c4

3 files changed

Lines changed: 127 additions & 0 deletions

File tree

docs/index.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,26 @@ iptools's documentation
1010
:members:
1111
:exclude-members: IpRange, IpRangeList
1212

13+
=======
14+
IpRange
15+
=======
1316
.. autoclass:: IpRange
1417
:members:
1518
:special-members:
1619

20+
===========
21+
IpRangeList
22+
===========
1723
.. autoclass:: IpRangeList
1824
:members:
1925
:special-members:
2026

27+
=========
28+
Constants
29+
=========
30+
.. automodule:: iptools.constants
31+
:members:
32+
2133
Indices and tables
2234
==================
2335

src/iptools/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
- :class:`IpRangeList`: List of IpRange objects supporting ``in`` and
5555
iteration.
5656
57+
:Constants:
58+
- :mod:`iptools.constants`: Common and special use IPv4 address blocks.
5759
5860
The :class:`IpRangeList` object can be used in a django settings file to
5961
allow CIDR notation and/or (start, end) ranges to be used in the
@@ -88,6 +90,7 @@
8890
)
8991

9092
import re
93+
from . import constants
9194

9295
# sniff for python2.x / python3k compatibility "fixes'
9396
try:
@@ -151,6 +154,8 @@ def validate_ip (s):
151154
True
152155
>>> validate_ip('127.0.0.256')
153156
False
157+
>>> validate_ip(constants.LOCALHOST)
158+
True
154159
>>> validate_ip(None) #doctest: +IGNORE_EXCEPTION_DETAIL
155160
Traceback (most recent call last):
156161
...
@@ -189,6 +194,8 @@ def validate_cidr (s):
189194
False
190195
>>> validate_cidr('127.0.0.0')
191196
False
197+
>>> validate_cidr(constants.LOOPBACK)
198+
True
192199
>>> validate_cidr(None) #doctest: +IGNORE_EXCEPTION_DETAIL
193200
Traceback (most recent call last):
194201
...
@@ -223,6 +230,8 @@ def validate_netmask (s):
223230
True
224231
>>> validate_netmask('255.255.255.255')
225232
True
233+
>>> validate_netmask(constants.BROADCAST)
234+
True
226235
>>> validate_netmask('128.0.0.1')
227236
False
228237

src/iptools/constants.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2008-2013, Bryan Davis
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without
8+
# modification, are permitted provided that the following conditions are met:
9+
# - Redistributions of source code must retain the above copyright notice,
10+
# this list of conditions and the following disclaimer.
11+
# - Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25+
# POSSIBILITY OF SUCH DAMAGE.
26+
"""Common and special use IPv4 address blocks."""
27+
28+
#: Broadcast messages to the current network (only valid as source address)
29+
#: (`RFC 5735 <https://tools.ietf.org/html/rfc5735>`_)
30+
CURRENT_NETWORK = "0.0.0.0/8"
31+
32+
#: Private network
33+
#: (`RFC 1918 <https://tools.ietf.org/html/rfc1918>`_)
34+
PRIVATE_NETWORK_10 = "10.0.0.0/8"
35+
36+
#: Carrier-grade NAT private network
37+
#: (`RFC 6598 <https://tools.ietf.org/html/rfc6598>`_)
38+
SHARED_ADDRESS_SPACE = "100.64.0.0/10"
39+
40+
#: Loopback addresses on the local host
41+
#: (`RFC 5735 <https://tools.ietf.org/html/rfc5735>`_)
42+
LOOPBACK = "127.0.0.0/8"
43+
44+
#: Common `localhost` address
45+
#: (`RFC 5735 <https://tools.ietf.org/html/rfc5735>`_)
46+
LOCALHOST = "127.0.0.1"
47+
48+
#: Autoconfiguration when no IP address available
49+
#: (`RFC 3972 <https://tools.ietf.org/html/rfc3972>`_)
50+
LINK_LOCAL = "169.254.0.0/16"
51+
52+
#: Private network
53+
#: (`RFC 1918 <https://tools.ietf.org/html/rfc1918>`_)
54+
PRIVATE_NETWORK_172_16 = "172.16.0.0/12"
55+
56+
#: IETF protocol assignments reserved block
57+
#: (`RFC 5735 <https://tools.ietf.org/html/rfc5735>`_)
58+
IETF_PROTOCOL_RESERVED = "192.0.0.0/24"
59+
60+
#: Dual-Stack Lite link address
61+
#: (`RFC 6333 <https://tools.ietf.org/html/rfc6333>`_)
62+
DUAL_STACK_LITE = "192.0.0.0/29"
63+
64+
#: Documentation and example network
65+
#: (`RFC 5737 <https://tools.ietf.org/html/rfc5737>`_)
66+
TEST_NET_1 = "192.0.2.0/24"
67+
68+
#: 6to4 anycast relay
69+
#: (`RFC 3068 <https://tools.ietf.org/html/rfc3068>`_)
70+
IPV6_TO_IPV4_RELAY = "192.88.99.0/24"
71+
72+
#: Private network
73+
#: (`RFC 1918 <https://tools.ietf.org/html/rfc1918>`_)
74+
PRIVATE_NETWORK_192_168 = "192.168.0.0/16"
75+
76+
#: Inter-network communications testing
77+
#: (`RFC 2544 <https://tools.ietf.org/html/rfc2544>`_)
78+
BENCHMARK_TESTS = "198.18.0.0/15"
79+
80+
#: Documentation and example network
81+
#: (`RFC 5737 <https://tools.ietf.org/html/rfc5737>`_)
82+
TEST_NET_2 = "198.51.100.0/24"
83+
84+
#: Documentation and example network
85+
#: (`RFC 5737 <https://tools.ietf.org/html/rfc5737>`_)
86+
TEST_NET_3 = "203.0.113.0/24"
87+
88+
#: Multicast reserved block
89+
#: (`RFC 5771 <https://tools.ietf.org/html/rfc5771>`_)
90+
MULTICAST = "224.0.0.0/4"
91+
92+
#: Link local multicast
93+
#: (`RFC 5771 <https://tools.ietf.org/html/rfc5771>`_)
94+
MULTICAST_LOCAL = "224.0.0.0/24"
95+
96+
#: Forwardable multicast
97+
#: (`RFC 5771 <https://tools.ietf.org/html/rfc5771>`_)
98+
MULTICAST_INTERNETWORK = "224.0.1.0/24"
99+
100+
#: Former Class E address space. Reserved for future use
101+
#: (`RFC 1700 <https://tools.ietf.org/html/rfc1700>`_)
102+
RESERVED = "240.0.0.0/4"
103+
104+
#: Broadcast messages to the current network (only valid as destination address)
105+
#: (`RFC 919 <https://tools.ietf.org/html/rfc919>`_)
106+
BROADCAST = "255.255.255.255"

0 commit comments

Comments
 (0)