diff options
author | YAMAMOTO Takashi <yamamoto@valinux.co.jp> | 2013-07-23 12:54:10 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2013-07-24 04:12:07 +0900 |
commit | d31b5dd3676fef728411584d823aa08238f99f65 (patch) | |
tree | 4125475c36f2a1a536e0dfac8e904a6271442566 | |
parent | faf13ff99fd47424c4fb93f1c2a6b3b80c81e0d1 (diff) |
addrconv: use bytes instead of int to represent ipv4 addresses
change ipv4 representation from int to bytes in many places.
replace homegrown bin<->text routines with addrconv for ipv4.
Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/lib/ip.py | 15 | ||||
-rw-r--r-- | ryu/lib/packet/arp.py | 2 | ||||
-rw-r--r-- | ryu/lib/packet/ipv4.py | 4 | ||||
-rw-r--r-- | ryu/lib/packet/packet_utils.py | 2 | ||||
-rw-r--r-- | ryu/lib/packet/vrrp.py | 12 | ||||
-rw-r--r-- | ryu/tests/unit/lib/test_ip.py | 4 | ||||
-rw-r--r-- | ryu/tests/unit/packet/test_arp.py | 4 | ||||
-rw-r--r-- | ryu/tests/unit/packet/test_ipv4.py | 4 | ||||
-rw-r--r-- | ryu/tests/unit/packet/test_packet.py | 10 | ||||
-rw-r--r-- | ryu/tests/unit/packet/test_tcp.py | 10 | ||||
-rw-r--r-- | ryu/tests/unit/packet/test_udp.py | 6 | ||||
-rw-r--r-- | ryu/tests/unit/packet/test_vlan.py | 4 | ||||
-rw-r--r-- | ryu/tests/unit/packet/test_vrrp.py | 31 |
13 files changed, 48 insertions, 60 deletions
diff --git a/ryu/lib/ip.py b/ryu/lib/ip.py index 7b2e29dd..10a9abdb 100644 --- a/ryu/lib/ip.py +++ b/ryu/lib/ip.py @@ -1,30 +1,19 @@ from ryu.lib import addrconv -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) + return addrconv.ipv4.text_to_bin(ip) 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) + return addrconv.ipv4.bin_to_text(ip) def ipv6_to_bin(ipv6): diff --git a/ryu/lib/packet/arp.py b/ryu/lib/packet/arp.py index b6266df0..616ab86b 100644 --- a/ryu/lib/packet/arp.py +++ b/ryu/lib/packet/arp.py @@ -51,7 +51,7 @@ class arp(packet_base.PacketBase): ============== ==================== """ - _PACK_STR = '!HHBBH6sI6sI' + _PACK_STR = '!HHBBH6s4s6s4s' _MIN_LEN = struct.calcsize(_PACK_STR) def __init__(self, hwtype=ARP_HW_TYPE_ETHERNET, proto=ether.ETH_TYPE_IP, diff --git a/ryu/lib/packet/ipv4.py b/ryu/lib/packet/ipv4.py index cc82a5d3..16b46980 100644 --- a/ryu/lib/packet/ipv4.py +++ b/ryu/lib/packet/ipv4.py @@ -26,7 +26,7 @@ from ryu.lib import ip IPV4_ADDRESS_PACK_STR = '!I' IPV4_ADDRESS_LEN = struct.calcsize(IPV4_ADDRESS_PACK_STR) -IPV4_PSEUDO_HEADER_PACK_STR = '!II2xHH' +IPV4_PSEUDO_HEADER_PACK_STR = '!4s4s2xHH' class ipv4(packet_base.PacketBase): @@ -62,7 +62,7 @@ class ipv4(packet_base.PacketBase): ============== ==================== """ - _PACK_STR = '!BBHHHBBHII' + _PACK_STR = '!BBHHHBBH4s4s' _MIN_LEN = struct.calcsize(_PACK_STR) def __init__(self, version=4, header_length=5, tos=0, diff --git a/ryu/lib/packet/packet_utils.py b/ryu/lib/packet/packet_utils.py index c36be490..0052335a 100644 --- a/ryu/lib/packet/packet_utils.py +++ b/ryu/lib/packet/packet_utils.py @@ -35,7 +35,7 @@ def checksum(data): # avoid circular import -_IPV4_PSEUDO_HEADER_PACK_STR = '!IIxBH' +_IPV4_PSEUDO_HEADER_PACK_STR = '!4s4sxBH' _IPV6_PSEUDO_HEADER_PACK_STR = '!16s16sI3xB' diff --git a/ryu/lib/packet/vrrp.py b/ryu/lib/packet/vrrp.py index 6330dc55..e79d7584 100644 --- a/ryu/lib/packet/vrrp.py +++ b/ryu/lib/packet/vrrp.py @@ -88,7 +88,7 @@ VRRP_IPV4_SRC_MAC_ADDRESS = netaddr.EUI(VRRP_IPV4_SRC_MAC_ADDRESS_STR).packed VRRP_IPV4_DST_MAC_ADDRESS_STR = '01:00:5E:00:00:12' VRRP_IPV4_DST_MAC_ADDRESS = netaddr.EUI(VRRP_IPV4_DST_MAC_ADDRESS_STR).packed VRRP_IPV4_DST_ADDRESS_STR = '224.0.0.18' -VRRP_IPV4_DST_ADDRESS = netaddr.IPAddress(VRRP_IPV4_DST_ADDRESS_STR).value +VRRP_IPV4_DST_ADDRESS = netaddr.IPAddress(VRRP_IPV4_DST_ADDRESS_STR).packed VRRP_IPV4_TTL = 255 @@ -166,10 +166,9 @@ VRRP_V2_MAX_ADVER_INT_MAX = 0xff def is_ipv6(ip_address): - if type(ip_address) == int: - return False - assert type(ip_address) == str + if len(ip_address) == 4: + return False assert len(ip_address) == 16 return True @@ -203,7 +202,7 @@ class vrrp(packet_base.PacketBase): """ _VERSION_PACK_STR = '!B' - _IPV4_ADDRESS_PACK_STR_RAW = 'I' + _IPV4_ADDRESS_PACK_STR_RAW = '4s' _IPV4_ADDRESS_PACK_STR = '!' + _IPV4_ADDRESS_PACK_STR_RAW _IPV4_ADDRESS_LEN = struct.calcsize(_IPV4_ADDRESS_PACK_STR) _IPV6_ADDRESS_LEN = 16 @@ -580,7 +579,8 @@ class vrrpv3(vrrp): @staticmethod def serialize_static(vrrp_, prev): if isinstance(prev, ipv4.ipv4): - assert type(vrrp_.ip_addresses[0]) == int + assert type(vrrp_.ip_addresses[0]) == str + assert len(vrrp_.ip_addresses[0]) == 4 ip_address_pack_raw = vrrpv3._IPV4_ADDRESS_PACK_STR_RAW elif isinstance(prev, ipv6.ipv6): assert type(vrrp_.ip_addresses[0]) == str diff --git a/ryu/tests/unit/lib/test_ip.py b/ryu/tests/unit/lib/test_ip.py index 7663745f..0de3ad6f 100644 --- a/ryu/tests/unit/lib/test_ip.py +++ b/ryu/tests/unit/lib/test_ip.py @@ -26,11 +26,11 @@ class Test_ip(unittest.TestCase): ipv4_str = '10.28.197.1' val = 0x0a1cc501 - res = ip.ipv4_to_bin(ipv4_str) + (res,) = struct.unpack('!I', ip.ipv4_to_bin(ipv4_str)) eq_(val, res) def test_ipv4_to_str(self): - ipv4_bin = 0x0a1cc501 + ipv4_bin = struct.pack('!I', 0x0a1cc501) val = '10.28.197.1' res = ip.ipv4_to_str(ipv4_bin) diff --git a/ryu/tests/unit/packet/test_arp.py b/ryu/tests/unit/packet/test_arp.py index 62950700..b1f8d900 100644 --- a/ryu/tests/unit/packet/test_arp.py +++ b/ryu/tests/unit/packet/test_arp.py @@ -43,9 +43,9 @@ class Test_arp(unittest.TestCase): plen = 4 opcode = 1 src_mac = mac.haddr_to_bin('00:07:0d:af:f4:54') - src_ip = int(netaddr.IPAddress('24.166.172.1')) + src_ip = netaddr.IPAddress('24.166.172.1').packed dst_mac = mac.haddr_to_bin('00:00:00:00:00:00') - dst_ip = int(netaddr.IPAddress('24.166.173.159')) + dst_ip = netaddr.IPAddress('24.166.173.159').packed fmt = arp._PACK_STR buf = pack(fmt, hwtype, proto, hlen, plen, opcode, src_mac, src_ip, diff --git a/ryu/tests/unit/packet/test_ipv4.py b/ryu/tests/unit/packet/test_ipv4.py index b8d56d69..fe74c985 100644 --- a/ryu/tests/unit/packet/test_ipv4.py +++ b/ryu/tests/unit/packet/test_ipv4.py @@ -50,8 +50,8 @@ class Test_ipv4(unittest.TestCase): ttl = 64 proto = inet.IPPROTO_TCP csum = 0xadc6 - src = int(netaddr.IPAddress('131.151.32.21')) - dst = int(netaddr.IPAddress('131.151.32.129')) + src = netaddr.IPAddress('131.151.32.21').packed + dst = netaddr.IPAddress('131.151.32.129').packed length = header_length * 4 option = '\x86\x28\x00\x00\x00\x01\x01\x22' \ + '\x00\x01\xae\x00\x00\x00\x00\x00' \ diff --git a/ryu/tests/unit/packet/test_packet.py b/ryu/tests/unit/packet/test_packet.py index 6ddb05dc..8594002d 100644 --- a/ryu/tests/unit/packet/test_packet.py +++ b/ryu/tests/unit/packet/test_packet.py @@ -36,10 +36,8 @@ class TestPacket(unittest.TestCase): dst_mac = mac.haddr_to_bin('AA:AA:AA:AA:AA:AA') src_mac = mac.haddr_to_bin('BB:BB:BB:BB:BB:BB') - dst_ip = int(netaddr.IPAddress('192.168.128.10')) - dst_ip_bin = struct.pack('!I', dst_ip) - src_ip = int(netaddr.IPAddress('192.168.122.20')) - src_ip_bin = struct.pack('!I', src_ip) + dst_ip_bin = dst_ip = netaddr.IPAddress('192.168.128.10').packed + src_ip_bin = src_ip = netaddr.IPAddress('192.168.122.20').packed payload = '\x06\x06\x47\x50\x00\x00\x00\x00' \ + '\xcd\xc5\x00\x00\x00\x00\x00\x00' \ + '\x10\x11\x12\x13\x14\x15\x16\x17' \ @@ -262,7 +260,7 @@ class TestPacket(unittest.TestCase): eq_(0x77b2, p_udp.csum) t = bytearray(u_buf) struct.pack_into('!H', t, 6, p_udp.csum) - ph = struct.pack('!IIBBH', self.src_ip, self.dst_ip, 0, + ph = struct.pack('!4s4sBBH', self.src_ip, self.dst_ip, 0, 17, len(u_buf) + len(self.payload)) t = ph + t + self.payload eq_(packet_utils.checksum(t), 0) @@ -361,7 +359,7 @@ class TestPacket(unittest.TestCase): eq_(len(t_buf), len(p_tcp)) t = bytearray(t_buf) struct.pack_into('!H', t, 16, p_tcp.csum) - ph = struct.pack('!IIBBH', self.src_ip, self.dst_ip, 0, + ph = struct.pack('!4s4sBBH', self.src_ip, self.dst_ip, 0, 6, len(t_buf) + len(self.payload)) t = ph + t + self.payload eq_(packet_utils.checksum(t), 0) diff --git a/ryu/tests/unit/packet/test_tcp.py b/ryu/tests/unit/packet/test_tcp.py index c79ab3c7..fe6f88b1 100644 --- a/ryu/tests/unit/packet/test_tcp.py +++ b/ryu/tests/unit/packet/test_tcp.py @@ -92,8 +92,8 @@ class Test_tcp(unittest.TestCase): offset = 5 csum = 0 - src_ip = int(netaddr.IPAddress('192.168.10.1')) - dst_ip = int(netaddr.IPAddress('192.168.100.1')) + src_ip = netaddr.IPAddress('192.168.10.1').packed + dst_ip = netaddr.IPAddress('192.168.100.1').packed prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_TCP, 0, src_ip, dst_ip) @@ -112,7 +112,7 @@ class Test_tcp(unittest.TestCase): eq_(res[8], self.urgent) # checksum - ph = struct.pack('!IIBBH', src_ip, dst_ip, 0, 6, offset * 4) + ph = struct.pack('!4s4sBBH', src_ip, dst_ip, 0, 6, offset * 4) d = ph + buf + bytearray() s = packet_utils.checksum(d) eq_(0, s) @@ -122,8 +122,8 @@ class Test_tcp(unittest.TestCase): csum = 0 option = '\x01\x02' - src_ip = int(netaddr.IPAddress('192.168.10.1')) - dst_ip = int(netaddr.IPAddress('192.168.100.1')) + src_ip = netaddr.IPAddress('192.168.10.1').packed + dst_ip = netaddr.IPAddress('192.168.100.1').packed prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_TCP, 0, src_ip, dst_ip) diff --git a/ryu/tests/unit/packet/test_udp.py b/ryu/tests/unit/packet/test_udp.py index 2a7044b1..9d52eaf0 100644 --- a/ryu/tests/unit/packet/test_udp.py +++ b/ryu/tests/unit/packet/test_udp.py @@ -71,8 +71,8 @@ class Test_udp(unittest.TestCase): total_length = 0 csum = 0 - src_ip = int(netaddr.IPAddress('192.168.10.1')) - dst_ip = int(netaddr.IPAddress('192.168.100.1')) + src_ip = netaddr.IPAddress('192.168.10.1').packed + dst_ip = netaddr.IPAddress('192.168.100.1').packed prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_UDP, 0, src_ip, dst_ip) @@ -85,7 +85,7 @@ class Test_udp(unittest.TestCase): eq_(res[2], struct.calcsize(udp._PACK_STR)) # checksum - ph = struct.pack('!IIBBH', src_ip, dst_ip, 0, 17, res[2]) + ph = struct.pack('!4s4sBBH', src_ip, dst_ip, 0, 17, res[2]) d = ph + buf + bytearray() s = packet_utils.checksum(d) eq_(0, s) diff --git a/ryu/tests/unit/packet/test_vlan.py b/ryu/tests/unit/packet/test_vlan.py index f306c770..5c31e507 100644 --- a/ryu/tests/unit/packet/test_vlan.py +++ b/ryu/tests/unit/packet/test_vlan.py @@ -100,8 +100,8 @@ class Test_vlan(unittest.TestCase): ttl = 64 proto = inet.IPPROTO_ICMP csum = 0xa7f2 - src = int(netaddr.IPAddress('131.151.32.21')) - dst = int(netaddr.IPAddress('131.151.32.129')) + src = netaddr.IPAddress('131.151.32.21').packed + dst = netaddr.IPAddress('131.151.32.129').packed option = 'TEST' ip = ipv4(version, header_length, tos, total_length, identification, flags, offset, ttl, proto, csum, src, dst, option) diff --git a/ryu/tests/unit/packet/test_vrrp.py b/ryu/tests/unit/packet/test_vrrp.py index 08e90a2f..56bb092c 100644 --- a/ryu/tests/unit/packet/test_vrrp.py +++ b/ryu/tests/unit/packet/test_vrrp.py @@ -46,11 +46,11 @@ class Test_vrrpv2(unittest.TestCase): auth_type = vrrp.VRRP_AUTH_NO_AUTH max_adver_int = 100 checksum = 0 - ip_address = netaddr.IPAddress('192.168.0.1').value + ip_address = netaddr.IPAddress('192.168.0.1').packed auth_data = (0, 0) vrrpv2 = vrrp.vrrpv2.create(type_, vrid, priority, max_adver_int, [ip_address]) - buf = struct.pack(vrrp.vrrpv2._PACK_STR + 'III', + buf = struct.pack(vrrp.vrrpv2._PACK_STR + '4sII', vrrp.vrrp_to_version_type(vrrp.VRRP_VERSION_V2, type_), vrid, priority, count_ip, auth_type, max_adver_int, checksum, ip_address, @@ -84,12 +84,12 @@ class Test_vrrpv2(unittest.TestCase): eq_(self.max_adver_int, vrrpv2.max_adver_int) eq_(self.checksum, vrrpv2.checksum) eq_(1, len(vrrpv2.ip_addresses)) - eq_(int, type(vrrpv2.ip_addresses[0])) + eq_(str, type(vrrpv2.ip_addresses[0])) eq_(self.ip_address, vrrpv2.ip_addresses[0]) eq_(self.auth_data, vrrpv2.auth_data) def test_serialize(self): - src_ip = netaddr.IPAddress('192.168.0.1').value + src_ip = netaddr.IPAddress('192.168.0.1').packed dst_ip = vrrp.VRRP_IPV4_DST_ADDRESS prev = ipv4.ipv4(4, 5, 0, 0, 0, 0, 0, vrrp.VRRP_IPV4_TTL, inet.IPPROTO_VRRP, 0, src_ip, dst_ip) @@ -98,14 +98,14 @@ class Test_vrrpv2(unittest.TestCase): vrid = 5 priority = 10 max_adver_int = 30 - ip_address = netaddr.IPAddress('192.168.0.2').value + ip_address = netaddr.IPAddress('192.168.0.2').packed ip_addresses = [ip_address] vrrp_ = vrrp.vrrpv2.create( type_, vrid, priority, max_adver_int, ip_addresses) buf = vrrp_.serialize(bytearray(), prev) - pack_str = vrrp.vrrpv2._PACK_STR + 'III' + pack_str = vrrp.vrrpv2._PACK_STR + '4sII' pack_len = struct.calcsize(pack_str) res = struct.unpack(pack_str, str(buf)) eq_(res[0], vrrp.vrrp_to_version_type(vrrp.VRRP_VERSION_V2, type_)) @@ -130,7 +130,7 @@ class Test_vrrpv2(unittest.TestCase): vrrp.vrrp.parser(m_short_buf) def test_create_packet(self): - primary_ip = netaddr.IPAddress('192.168.0.2').value + primary_ip = netaddr.IPAddress('192.168.0.2').packed p0 = self.vrrpv2.create_packet(primary_ip) p0.serialize() p1 = packet.Packet(str(p0.data)) @@ -193,10 +193,10 @@ class Test_vrrpv3_ipv4(unittest.TestCase): count_ip = 1 max_adver_int = 111 checksum = 0 - ip_address = netaddr.IPAddress('192.168.0.1').value + ip_address = netaddr.IPAddress('192.168.0.1').packed vrrpv3 = vrrp.vrrpv3.create(type_, vrid, priority, max_adver_int, [ip_address]) - buf = struct.pack(vrrp.vrrpv3._PACK_STR + 'I', + buf = struct.pack(vrrp.vrrpv3._PACK_STR + '4s', vrrp.vrrp_to_version_type(vrrp.VRRP_VERSION_V3, type_), vrid, priority, count_ip, max_adver_int, checksum, ip_address) @@ -226,11 +226,11 @@ class Test_vrrpv3_ipv4(unittest.TestCase): eq_(self.max_adver_int, vrrpv3.max_adver_int) eq_(self.checksum, vrrpv3.checksum) eq_(1, len(vrrpv3.ip_addresses)) - eq_(int, type(vrrpv3.ip_addresses[0])) + eq_(str, type(vrrpv3.ip_addresses[0])) eq_(self.ip_address, vrrpv3.ip_addresses[0]) def test_serialize(self): - src_ip = netaddr.IPAddress('192.168.0.1').value + src_ip = netaddr.IPAddress('192.168.0.1').packed dst_ip = vrrp.VRRP_IPV4_DST_ADDRESS prev = ipv4.ipv4(4, 5, 0, 0, 0, 0, 0, vrrp.VRRP_IPV4_TTL, inet.IPPROTO_VRRP, 0, src_ip, dst_ip) @@ -239,7 +239,7 @@ class Test_vrrpv3_ipv4(unittest.TestCase): vrid = 5 priority = 10 max_adver_int = 30 - ip_address = netaddr.IPAddress('192.168.0.2').value + ip_address = netaddr.IPAddress('192.168.0.2').packed ip_addresses = [ip_address] vrrp_ = vrrp.vrrpv3.create( @@ -247,7 +247,7 @@ class Test_vrrpv3_ipv4(unittest.TestCase): buf = vrrp_.serialize(bytearray(), prev) print(len(buf), type(buf), buf) - pack_str = vrrp.vrrpv3._PACK_STR + 'I' + pack_str = vrrp.vrrpv3._PACK_STR + '4s' pack_len = struct.calcsize(pack_str) res = struct.unpack(pack_str, str(buf)) eq_(res[0], vrrp.vrrp_to_version_type(vrrp.VRRP_VERSION_V3, type_)) @@ -261,7 +261,8 @@ class Test_vrrpv3_ipv4(unittest.TestCase): print(res) # checksum - ph = struct.pack('!IIxBH', src_ip, dst_ip, inet.IPPROTO_VRRP, pack_len) + ph = struct.pack('!4s4sxBH', src_ip, dst_ip, inet.IPPROTO_VRRP, + pack_len) s = packet_utils.checksum(ph + buf) eq_(0, s) @@ -271,7 +272,7 @@ class Test_vrrpv3_ipv4(unittest.TestCase): vrrp.vrrp.parser(m_short_buf) def test_create_packet(self): - primary_ip = netaddr.IPAddress('192.168.0.2').value + primary_ip = netaddr.IPAddress('192.168.0.2').packed p0 = self.vrrpv3.create_packet(primary_ip) p0.serialize() p1 = packet.Packet(str(p0.data)) |