Skip to content

Commit aa1d03f

Browse files
committed
Merge branch 'pull/19'
* Merging pull request apenwarr#19.
2 parents 0f0f132 + 582f63f commit aa1d03f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

server.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
def _ipmatch(ipstr):
1010
if ipstr == 'default':
1111
ipstr = '0.0.0.0/0'
12-
m = re.match(r'^(\d+(\.\d+(\.\d+(\.\d+)?)?)?)(?:/(\d+))?$', ipstr)
12+
# Regex to check that ipstr is formatted like an IPv4 address
13+
m = re.match('^(\d+)' + '(\.\d+)?' * 3 + '(?:/(\d+))?$', ipstr)
1314
if m:
14-
g = m.groups()
15-
ips = g[0]
16-
width = int(g[4] or 32)
17-
if g[1] == None:
18-
ips += '.0.0.0'
19-
width = min(width, 8)
20-
elif g[2] == None:
21-
ips += '.0.0'
22-
width = min(width, 16)
23-
elif g[3] == None:
24-
ips += '.0'
25-
width = min(width, 24)
15+
# If the network prefix width is specified, use it. Otherwise, assume 32
16+
if '/' in ipstr:
17+
ips, width = ipstr.split('/')
18+
width = int(width)
19+
else:
20+
ips, width = ipstr, 32
21+
# If ips doesn't have four octets, then assume that the network
22+
# prefix width is constrained. Pad ips with 0s
23+
octets = ips.count('.') + 1
24+
width = min(width, 8 * octets)
25+
ips += '.0' * (4 - octets)
2626
return (struct.unpack('!I', socket.inet_aton(ips))[0], width)
2727

2828

0 commit comments

Comments
 (0)