From bb5e68ad8b9044de047ce8f6f4a37e06c74ccd64 Mon Sep 17 00:00:00 2001 From: Blake Harnden Date: Mon, 3 Jun 2019 11:49:43 -0700 Subject: [PATCH] 2/3 fixes for ip address and mac byte handling --- daemon/core/api/tlv/coreapi.py | 6 ------ daemon/core/nodes/base.py | 2 -- daemon/core/nodes/ipaddress.py | 23 ++++++++++------------- daemon/core/nodes/network.py | 4 ---- 4 files changed, 10 insertions(+), 25 deletions(-) diff --git a/daemon/core/api/tlv/coreapi.py b/daemon/core/api/tlv/coreapi.py index 218dbdf8..40227bcc 100644 --- a/daemon/core/api/tlv/coreapi.py +++ b/daemon/core/api/tlv/coreapi.py @@ -276,8 +276,6 @@ class CoreTlvDataIpv4Addr(CoreTlvDataObj): :return: Ipv4 address :rtype: core.misc.ipaddress.IpAddress """ - # value = value.decode("ISO-8859-1") - # value = socket.inet_ntoa(value) return IpAddress(af=socket.AF_INET, address=value) @@ -308,8 +306,6 @@ class CoreTlvDataIPv6Addr(CoreTlvDataObj): :return: Ipv4 address :rtype: core.misc.ipaddress.IpAddress """ - # value = value.decode("ISO-8859-1") - # value = socket.inet_ntoa(value) return IpAddress(af=socket.AF_INET6, address=value) @@ -342,8 +338,6 @@ class CoreTlvDataMacAddr(CoreTlvDataObj): :rtype: core.misc.ipaddress.MacAddress """ # only use 48 bits - # value = value.decode("ISO-8859-1") - # value = socket.inet_ntoa(value) return MacAddress(address=value[2:]) diff --git a/daemon/core/nodes/base.py b/daemon/core/nodes/base.py index a0410d92..219757d2 100644 --- a/daemon/core/nodes/base.py +++ b/daemon/core/nodes/base.py @@ -1063,13 +1063,11 @@ class CoreNetworkBase(NodeBase): if ipaddress.is_ipv4_address(ip): family = AF_INET ipl = socket.inet_pton(family, ip) - # ipl = ipl.decode("ISO-8859-1") interface2_ip4 = ipaddress.IpAddress(af=family, address=ipl) interface2_ip4_mask = mask else: family = AF_INET6 ipl = socket.inet_pton(family, ip) - # ipl = ipl.decode("ISO-8859-1") interface2_ip6 = ipaddress.IpAddress(af=family, address=ipl) interface2_ip6_mask = mask diff --git a/daemon/core/nodes/ipaddress.py b/daemon/core/nodes/ipaddress.py index f1a763d1..111bb38c 100644 --- a/daemon/core/nodes/ipaddress.py +++ b/daemon/core/nodes/ipaddress.py @@ -6,6 +6,7 @@ import logging import random import socket import struct +from builtins import bytes from builtins import range from socket import AF_INET from socket import AF_INET6 @@ -31,9 +32,7 @@ class MacAddress(object): :return: string representation :rtype: str """ - logging.info("mac addr: %s", type(self.addr)) - addr = self.addr.decode("ISO-8859-1") - return ":".join("%02x" % ord(x) for x in addr) + return ":".join("%02x" % x for x in bytearray(self.addr)) def to_link_local(self): """ @@ -63,7 +62,7 @@ class MacAddress(object): :return: mac address class :rtype: MacAddress """ - addr = "".join(chr(int(x, 16)) for x in s.split(":")) + addr = b"".join(bytes([int(x, 16)]) for x in s.split(":")) return cls(addr) @classmethod @@ -95,9 +94,7 @@ class IpAddress(object): :return: """ # check if (af, addr) is valid - logging.info("ip address: %s", type(address)) if not socket.inet_ntop(af, address): - # if not socket.inet_ntop(af, address.encode("ISO-8859-1")): raise ValueError("invalid af/addr") self.af = af self.addr = address @@ -128,7 +125,6 @@ class IpAddress(object): :rtype: str """ return socket.inet_ntop(self.af, self.addr) - # return socket.inet_ntop(self.af, self.addr.encode("ISO-8859-1")) def __eq__(self, other): """ @@ -159,14 +155,14 @@ class IpAddress(object): logging.exception("error during addition") return NotImplemented - tmp = [ord(x) for x in self.addr] + tmp = [x for x in bytearray(self.addr)] for i in range(len(tmp) - 1, -1, -1): x = tmp[i] + carry tmp[i] = x & 0xff carry = x >> 8 if carry == 0: break - addr = "".join(chr(x) for x in tmp) + addr = bytes(tmp) return self.__class__(self.af, addr) def __sub__(self, other): @@ -237,12 +233,13 @@ class IpPrefix(object): else: self.prefixlen = self.addrlen self.prefix = socket.inet_pton(self.af, tmp[0]) + self.prefix = bytes(self.prefix) if self.addrlen > self.prefixlen: addrbits = self.addrlen - self.prefixlen netmask = ((1 << self.prefixlen) - 1) << addrbits - prefix = "" + prefix = bytes(b"") for i in range(-1, -(addrbits >> 3) - 2, -1): - prefix = chr(ord(self.prefix[i]) & (netmask & 0xff)) + prefix + prefix = bytes([self.prefix[i] & (netmask & 0xff)]) + prefix netmask >>= 8 self.prefix = self.prefix[:i] + prefix @@ -323,11 +320,11 @@ class IpPrefix(object): self.af == AF_INET and tmp == (1 << (self.addrlen - self.prefixlen)) - 1): raise ValueError("invalid hostid for prefix %s: %s" % (self, hostid)) - addr = "" + addr = bytes(b"") prefix_endpoint = -1 for i in range(-1, -(self.addrlen >> 3) - 1, -1): prefix_endpoint = i - addr = chr(ord(self.prefix[i]) | (tmp & 0xff)) + addr + addr = bytes([self.prefix[i] | (tmp & 0xff)]) + addr tmp >>= 8 if not tmp: break diff --git a/daemon/core/nodes/network.py b/daemon/core/nodes/network.py index 82e20017..b76a035a 100644 --- a/daemon/core/nodes/network.py +++ b/daemon/core/nodes/network.py @@ -855,13 +855,11 @@ class PtpNet(CoreNetwork): if ipaddress.is_ipv4_address(ip): family = AF_INET ipl = socket.inet_pton(family, ip) - # ipl = ipl.decode("ISO-8859-1") interface1_ip4 = ipaddress.IpAddress(af=family, address=ipl) interface1_ip4_mask = mask else: family = AF_INET6 ipl = socket.inet_pton(family, ip) - # ipl = ipl.decode("ISO-8859-1") interface1_ip6 = ipaddress.IpAddress(af=family, address=ipl) interface1_ip6_mask = mask @@ -875,13 +873,11 @@ class PtpNet(CoreNetwork): if ipaddress.is_ipv4_address(ip): family = AF_INET ipl = socket.inet_pton(family, ip) - # ipl = ipl.decode("ISO-8859-1") interface2_ip4 = ipaddress.IpAddress(af=family, address=ipl) interface2_ip4_mask = mask else: family = AF_INET6 ipl = socket.inet_pton(family, ip) - # ipl = ipl.decode("ISO-8859-1") interface2_ip6 = ipaddress.IpAddress(af=family, address=ipl) interface2_ip6_mask = mask