Skip to content

Commit

Permalink
Merge pull request #22 from jathanism/u/jathanism-11-v6-wildcard
Browse files Browse the repository at this point in the history
Fix #11 - Add v6 wildcard support.
  • Loading branch information
jathanism authored Feb 18, 2023
2 parents 36d3d7f + 1b24b49 commit da46f55
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
28 changes: 21 additions & 7 deletions cidrize.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,24 @@


# Globals
EVERYTHING = [
"internet at large",

# Patterns for matching v4 wildcards
EVERYTHING_V4 = [
"*",
"all",
"any",
"internet",
"0.0.0.0",
"0.0.0.0/0",
"0.0.0.0-255.255.255.255",
"all",
"any",
"internet",
"internet at large",
]
EVERYTHING = EVERYTHING_V4 # Backwards compatibility (just in case)

# Patterns for matching v6 wildcards
EVERYTHING_V6 = [
"::",
"[::]",
]

# IPRange objects larger than MAX_RANGE_LEN will always be strict.
Expand Down Expand Up @@ -338,11 +347,16 @@ def cidrize(
# Otherwise try everything else
result = None
try:
# Parse "everything" & immediately return; strict/loose doesn't apply
if ipstr in EVERYTHING: # pylint: disable = no-else-return
# Parse "everything" v4 & immediately return; strict/loose doesn't apply
if ipstr in EVERYTHING_V4: # pylint: disable = no-else-return
log.debug("Trying everything style...")
return [IPNetwork("0.0.0.0/0")]

# Parse "everything" v6 & immediately return; strict/loose doesn't apply
elif ipstr in EVERYTHING_V6:
log.debug("Trying everything style...")
return [IPNetwork("::/0")]

# Parse old-fashioned CIDR notation & immediately return; strict/loose doesn't apply
# Now with IPv6!
elif RE_CIDR.match(ipstr) or is_ipv6(ipstr):
Expand Down
11 changes: 9 additions & 2 deletions tests/test_cidrize.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ class TestCidrize(unittest.TestCase):
def setUp(self):
self.test = cidrize.cidrize

def test_everything_style(self):
def test_everything_style_v4(self):
expected = set([IPNetwork("0.0.0.0/0")])
_input = set()
for item in cidrize.EVERYTHING:
for item in cidrize.EVERYTHING_V4:
_input.add(self.test(item)[0])
assert expected == _input

def test_everything_style_v6(self):
expected = set([IPNetwork("::/0")])
_input = set()
for item in cidrize.EVERYTHING_V6:
_input.add(self.test(item)[0])
assert expected == _input

Expand Down

0 comments on commit da46f55

Please sign in to comment.