Skip to content

Commit

Permalink
validate_netmask: ensure 32 bit expansion
Browse files Browse the repository at this point in the history
Ensure that the bit string representation of a netmask is the full 32
bits before validating the left most bits are 1s.

See #18
  • Loading branch information
bd808 committed Aug 21, 2016
1 parent 33954e7 commit 285fe5c
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion iptools/ipv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ def validate_netmask(s):
True
>>> validate_netmask('128.0.0.1')
False
>>> validate_netmask('1.255.255.0')
False
>>> validate_netmask('0.255.255.0')
False
:param s: String to validate as a dotted-quad notation netmask.
Expand All @@ -293,7 +297,8 @@ def validate_netmask(s):
:raises: TypeError
"""
if validate_ip(s):
mask = bin(ip2network(s))[2:]
# Convert to binary string, strip '0b' prefix, 0 pad to 32 bits
mask = bin(ip2network(s))[2:].zfill(32)
# all left most bits must be 1, all right most must be 0
seen0 = False
for c in mask:
Expand Down

0 comments on commit 285fe5c

Please sign in to comment.