Signed-off-by: YAMAMOTO Takashi <[email protected]>
---
ryu/lib/ip.py | 29 +++--------------------------
ryu/lib/mac.py | 21 ++++++++++-----------
2 files changed, 13 insertions(+), 37 deletions(-)
diff --git a/ryu/lib/ip.py b/ryu/lib/ip.py
index b302d8d..7b2e29d 100644
--- a/ryu/lib/ip.py
+++ b/ryu/lib/ip.py
@@ -1,4 +1,4 @@
-import struct
+from ryu.lib import addrconv
def ipv4_arg_to_bin(w, x, y, z):
@@ -26,39 +26,16 @@ def ipv4_to_str(ip):
z = ip & 0xff
return "%i.%i.%i.%i" % (w, x, y, z)
-IPV6_PACK_STR = '!8H'
-
-
-def ipv6_to_arg_list(ipv6):
- '''
- convert ipv6 string to a list of 8 different parts
- '''
- args = []
- if '::' in ipv6:
- h, t = ipv6.split('::')
- h_list = [int(x, 16) for x in h.split(':')]
- t_list = [int(x, 16) for x in t.split(':')]
- args += h_list
- zero = [0]
- args += ((8 - len(h_list) - len(t_list)) * zero)
- args += t_list
- else:
- args = [int(x, 16) for x in ipv6.split(':')]
-
- return args
-
def ipv6_to_bin(ipv6):
'''
convert ipv6 string to binary representation
'''
- args = ipv6_to_arg_list(ipv6)
- return struct.pack(IPV6_PACK_STR, *args)
+ return addrconv.ipv6.text_to_bin(ipv6)
def ipv6_to_str(bin_addr):
'''
convert binary representation to human readable string
'''
- args = struct.unpack_from(IPV6_PACK_STR, bin_addr)
- return ':'.join('%x' % x for x in args)
+ return addrconv.ipv6.bin_to_text(bin_addr)
diff --git a/ryu/lib/mac.py b/ryu/lib/mac.py
index 2c12877..decc0db 100644
--- a/ryu/lib/mac.py
+++ b/ryu/lib/mac.py
@@ -14,13 +14,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-import itertools
+from ryu.lib import addrconv
-# string representation
-HADDR_PATTERN = r'([0-9a-f]{2}:){5}[0-9a-f]{2}'
+import itertools
-# Internal representation of mac address is string[6]
-_HADDR_LEN = 6
DONTCARE = '\x00' * 6
BROADCAST = '\xff' * 6
@@ -37,17 +34,19 @@ def haddr_to_str(addr):
form"""
if addr is None:
return 'None'
- assert len(addr) == _HADDR_LEN
- return ':'.join('%02x' % ord(char) for char in addr)
+ try:
+ return addrconv.mac.bin_to_text(addr)
+ except:
+ raise AssertionError
def haddr_to_bin(string):
"""Parse mac address string in human readable format into
internal representation"""
- hexes = string.split(':')
- if len(hexes) != _HADDR_LEN:
- raise ValueError('Invalid format for mac address: %s' % string)
- return ''.join(chr(int(h, 16)) for h in hexes)
+ try:
+ return addrconv.mac.text_to_bin(string)
+ except:
+ raise ValueError
def haddr_bitand(addr, mask):
--
1.8.1.5
------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:
Build for Windows Store.
http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel