addrconv: replace bin<->text converters for ipv6

Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
YAMAMOTO Takashi 2013-07-23 12:54:09 +09:00 committed by FUJITA Tomonori
parent 343e0f6997
commit faf13ff99f

View File

@ -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)