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 <[email protected]>
Reported-by: Li-Der Chou <[email protected]>
Signed-off-by: Wei-Li Tang <[email protected]>
---
 ryu/lib/ofctl_v1_2.py |   18 ++++++++++++++----
 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 6b1d447..80d28b0 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 403c82e..4bc739d 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
-- 
1.7.9.5


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to