ofctl_v1_2/3: Convert IP fields to string with dotted decimal mask

This enables match_ip*_to_str() functions to output IP address with
dotted decimal subnet mask if the mask cannot be represented in CIDR
format.

Reported-by: Yi-Ching Lee <potatoching11@gmail.com>
Reported-by: Li-Der Chou <cld@csie.ncu.edu.tw>
Signed-off-by: Wei-Li Tang <alextwl@xinguard.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
Wei-Li Tang 2014-03-11 13:49:24 +08:00 committed by FUJITA Tomonori
parent 24b850f323
commit 06f343bb99
2 changed files with 28 additions and 8 deletions

View File

@ -380,8 +380,11 @@ def match_ip_to_str(value, mask):
ip = socket.inet_ntoa(struct.pack('!I', value))
if mask is not None and mask != 0:
binary_str = bin(mask)[2:].zfill(8)
netmask = '/%d' % len(binary_str.rstrip('0'))
binary_str = bin(mask)[2:].zfill(32).rstrip('0')
if binary_str.find('0') >= 0:
netmask = '/%s' % socket.inet_ntoa(struct.pack('!I', mask))
else:
netmask = '/%d' % len(binary_str)
else:
netmask = ''
@ -395,14 +398,21 @@ def match_ipv6_to_str(value, mask):
ip = netaddr.IPNetwork(':'.join(ip_list))
netmask = 128
netmask_str = None
if mask is not None:
mask_list = []
for word in mask:
mask_list.append('%04x' % word)
mask_v = netaddr.IPNetwork(':'.join(mask_list))
netmask = len(mask_v.ip.bits().replace(':', '').rstrip('0'))
binary_str = mask_v.ip.bits().replace(':', '').zfill(128).rstrip('0')
if binary_str.find('0') >= 0:
netmask_str = str(mask_v.ip)
else:
netmask = len(binary_str)
if netmask == 128:
if netmask_str is not None:
ip_str = str(ip.ip) + '/' + netmask_str
elif netmask == 128:
ip_str = str(ip.ip)
else:
ip.prefixlen = netmask

View File

@ -460,8 +460,11 @@ def match_ip_to_str(value, mask):
ip = socket.inet_ntoa(struct.pack('!I', value))
if mask is not None and mask != 0:
binary_str = bin(mask)[2:].zfill(8)
netmask = '/%d' % len(binary_str.rstrip('0'))
binary_str = bin(mask)[2:].zfill(32).rstrip('0')
if binary_str.find('0') >= 0:
netmask = '/%s' % socket.inet_ntoa(struct.pack('!I', mask))
else:
netmask = '/%d' % len(binary_str)
else:
netmask = ''
@ -475,14 +478,21 @@ def match_ipv6_to_str(value, mask):
ip = netaddr.IPNetwork(':'.join(ip_list))
netmask = 128
netmask_str = None
if mask is not None:
mask_list = []
for word in mask:
mask_list.append('%04x' % word)
mask_v = netaddr.IPNetwork(':'.join(mask_list))
netmask = len(mask_v.ip.bits().replace(':', '').rstrip('0'))
binary_str = mask_v.ip.bits().replace(':', '').zfill(128).rstrip('0')
if binary_str.find('0') >= 0:
netmask_str = str(mask_v.ip)
else:
netmask = len(binary_str)
if netmask == 128:
if netmask_str is not None:
ip_str = str(ip.ip) + '/' + netmask_str
elif netmask == 128:
ip_str = str(ip.ip)
else:
ip.prefixlen = netmask