File tree Expand file tree Collapse file tree 1 file changed +13
-13
lines changed Expand file tree Collapse file tree 1 file changed +13
-13
lines changed Original file line number Diff line number Diff line change 9
9
def _ipmatch (ipstr ):
10
10
if ipstr == 'default' :
11
11
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 )
13
14
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 )
26
26
return (struct .unpack ('!I' , socket .inet_aton (ips ))[0 ], width )
27
27
28
28
You can’t perform that action at this time.
0 commit comments