summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorCan Zhang <can@canx.me>2013-04-16 07:52:33 -0700
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2013-04-16 07:52:33 -0700
commitfd7723eaa1e02b91ff7db07ddd5521bb9bd56d00 (patch)
tree3b9f411dbede876eb9de19e57b5bd4d10db29fb4
parentdb46baf4e8be1f43e199d556ec4e99636a044e91 (diff)
lib: ip address presentation convert helper methods
Signed-off-by: Can Zhang <can@canx.me> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/lib/ip.py64
-rw-r--r--ryu/tests/unit/lib/test_ip.py60
2 files changed, 124 insertions, 0 deletions
diff --git a/ryu/lib/ip.py b/ryu/lib/ip.py
new file mode 100644
index 00000000..b302d8d8
--- /dev/null
+++ b/ryu/lib/ip.py
@@ -0,0 +1,64 @@
+import struct
+
+
+def ipv4_arg_to_bin(w, x, y, z):
+ """Generate unsigned int from components of IP address
+ returns: w << 24 | x << 16 | y << 8 | z"""
+ return (w << 24) | (x << 16) | (y << 8) | z
+
+
+def ipv4_to_bin(ip):
+ '''
+ Parse an IP address and return an unsigned int.
+ The IP address is in dotted decimal notation.
+ '''
+ args = [int(arg) for arg in ip.split('.')]
+ return ipv4_arg_to_bin(*args)
+
+
+def ipv4_to_str(ip):
+ """Generate IP address string from an unsigned int.
+ ip: unsigned int of form w << 24 | x << 16 | y << 8 | z
+ returns: ip address string w.x.y.z"""
+ w = (ip >> 24) & 0xff
+ x = (ip >> 16) & 0xff
+ y = (ip >> 8) & 0xff
+ 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)
+
+
+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)
diff --git a/ryu/tests/unit/lib/test_ip.py b/ryu/tests/unit/lib/test_ip.py
new file mode 100644
index 00000000..7663745f
--- /dev/null
+++ b/ryu/tests/unit/lib/test_ip.py
@@ -0,0 +1,60 @@
+import unittest
+import logging
+import struct
+import netaddr
+from struct import *
+from nose.tools import *
+from nose.plugins.skip import Skip, SkipTest
+
+from ryu.lib import ip
+
+LOG = logging.getLogger('test_ip')
+
+
+class Test_ip(unittest.TestCase):
+ '''
+ test case for ip address module
+ '''
+
+ def setUp(self):
+ pass
+
+ def tearDown(self):
+ pass
+
+ def test_ipv4_to_bin(self):
+ ipv4_str = '10.28.197.1'
+ val = 0x0a1cc501
+
+ res = ip.ipv4_to_bin(ipv4_str)
+ eq_(val, res)
+
+ def test_ipv4_to_str(self):
+ ipv4_bin = 0x0a1cc501
+ val = '10.28.197.1'
+
+ res = ip.ipv4_to_str(ipv4_bin)
+ eq_(val, res)
+
+ def test_ipv6_to_bin(self):
+ ipv6_str = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
+ val = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20, 0x66ff,
+ 0xfe4c, 0x9c3c)
+ res = ip.ipv6_to_bin(ipv6_str)
+ eq_(val, res)
+
+ def test_ipv6_to_bin_with_shortcut(self):
+ ipv6_str = '3f:10::1:2'
+ val = struct.pack('!8H', 0x3f, 0x10, 0, 0, 0, 0, 0x1, 0x2)
+
+ res = ip.ipv6_to_bin(ipv6_str)
+ eq_(val, res)
+
+ def test_ipv6_to_str(self):
+ ipv6_bin = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20,
+ 0x66ff, 0xfe4c, 0x9c3c)
+ val = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
+
+ res = ip.ipv6_to_str(ipv6_bin)
+ print val, res
+ eq_(val, res)