diff options
author | Wei-Li Tang <alextwl@xinguard.com> | 2014-03-11 13:49:24 +0800 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-03-16 21:05:13 +0900 |
commit | 06f343bb99269bc8138e155d0b60bf4efd08307f (patch) | |
tree | b7d708d5b20037fcb9de658d4ad94f9d0476ac4e | |
parent | 24b850f323564ba23150d9e76f52940ba0a45a1d (diff) |
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>
-rw-r--r-- | ryu/lib/ofctl_v1_2.py | 18 | ||||
-rw-r--r-- | ryu/lib/ofctl_v1_3.py | 18 |
2 files changed, 28 insertions, 8 deletions
diff --git a/ryu/lib/ofctl_v1_2.py b/ryu/lib/ofctl_v1_2.py index 335709db..e6023ed2 100644 --- a/ryu/lib/ofctl_v1_2.py +++ b/ryu/lib/ofctl_v1_2.py @@ -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 diff --git a/ryu/lib/ofctl_v1_3.py b/ryu/lib/ofctl_v1_3.py index 1f0fd30a..748f3898 100644 --- a/ryu/lib/ofctl_v1_3.py +++ b/ryu/lib/ofctl_v1_3.py @@ -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 |