Commit 7b11a0d7 authored by Tom Niget's avatar Tom Niget

py2to3: fix various places mishandling strings and bytes

parent 5655ddc5
......@@ -252,15 +252,18 @@ class Babel:
unidentified = set(n)
self.neighbours = neighbours = {}
a = len(self.network)
logging.info("Routes: %r", routes)
for route in routes:
assert route.flags & 1, route # installed
if route.prefix.startswith(b'\0\0\0\0\0\0\0\0\0\0\xff\xff'):
logging.warning("Ignoring IPv4 route: %r", route)
continue
assert route.neigh_address == route.nexthop, route
address = route.neigh_address, route.ifindex
neigh_routes = n[address]
ip = utils.binFromRawIp(route.prefix)
if ip[:a] == self.network:
logging.debug("Route is on the network: %r", route)
prefix = ip[a:route.plen]
if prefix and not route.refmetric:
neighbours[prefix] = neigh_routes
......@@ -275,7 +278,9 @@ class Babel:
socket.inet_ntop(socket.AF_INET6, route.prefix),
route.plen)
else:
logging.debug("Route is not on the network: %r", route)
prefix = None
logging.debug("Adding route %r to %r", route, neigh_routes)
neigh_routes[1][prefix] = route
self.locked.clear()
if unidentified:
......
......@@ -205,13 +205,16 @@ class RegistryServer:
def recv(self, code):
try:
prefix, msg = self.sock.recv(1<<16).split(b'\x00', 1)
prefix, msg = self.sock.recv(1 << 16).split(b'\x00', 1)
int(prefix, 2)
except ValueError:
pass
else:
if msg and msg[0:1] == code:
return prefix, msg[1:]
if len(msg) >= 1 and msg[0] == code:
return prefix.decode(), msg[1:].decode()
logging.error("Invalid message or unexpected code: %r", msg)
return None, None
def select(self, r, w, t):
......@@ -608,7 +611,7 @@ class RegistryServer:
return zlib.compress(json.dumps(config).encode("utf-8"))
def _queryAddress(self, peer):
logging.info("Querying address for %s/%s", int(peer, 2), len(peer))
logging.info("Querying address for %s/%s %r", int(peer, 2), len(peer), peer)
self.sendto(peer, 1)
s = self.sock,
timeout = 3
......@@ -616,9 +619,9 @@ class RegistryServer:
# Loop because there may be answers from previous requests.
while select.select(s, (), (), timeout)[0]:
prefix, msg = self.recv(1)
logging.info("* received: %s - %s", prefix, msg)
logging.info("* received: %r - %r", prefix, msg)
if prefix == peer:
return msg
return msg.decode()
timeout = max(0, end - time.time())
logging.info("Timeout while querying address for %s/%s",
int(peer, 2), len(peer))
......@@ -661,7 +664,7 @@ class RegistryServer:
cert = self.getCert(cn)
msg = "%s %s" % (peer, msg)
logging.info("Sending bootstrap peer: %s", msg)
return x509.encrypt(cert, msg)
return x509.encrypt(cert, msg.encode())
@rpc_private
def revoke(self, cn_or_serial):
......
......@@ -244,6 +244,7 @@ def binFromSubnet(subnet):
return bin(int(p))[2:].rjust(int(l), '0')
def newHmacSecret():
"""returns bytes"""
from random import getrandbits as g
pack = struct.Struct(">QQI").pack
assert len(pack(0,0,0)) == HMAC_LEN
......
......@@ -31,7 +31,7 @@ def openssl(*args, fds=[]):
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, pass_fds=fds)
def encrypt(cert, data):
def encrypt(cert: bytes, data: bytes) -> bytes:
r, w = os.pipe()
try:
threading.Thread(target=os.write, args=(w, cert)).start()
......@@ -163,7 +163,6 @@ class Cert:
return r
def verify(self, sign: bytes, data: bytes):
assert isinstance(data, bytes)
pub_key = self.ca_crypto.public_key()
pub_key.verify(
sign,
......@@ -173,7 +172,6 @@ class Cert:
)
def sign(self, data: bytes) -> bytes:
assert isinstance(data, bytes)
return self.key_crypto.sign(
data,
padding.PKCS1v15(),
......@@ -288,7 +286,7 @@ class Peer:
seqno_struct = struct.Struct("!L")
def decode(self, msg: bytes, _unpack=seqno_struct.unpack) -> str:
def decode(self, msg: bytes, _unpack=seqno_struct.unpack) -> tuple[int, bytes, int | None] | bytes:
seqno, = _unpack(msg[:4])
if seqno <= 2:
msg = msg[4:]
......@@ -302,11 +300,7 @@ class Peer:
if self._hmac(msg[:i]) == msg[i:] and self._i < seqno:
self._last = None
self._i = seqno
try:
return msg[4:i].decode()
except UnicodeDecodeError:
logging.error("Invalid message from %s: %r", self.prefix, msg)
raise
return msg[4:i]
def encode(self, msg: str | bytes, _pack=seqno_struct.pack) -> bytes:
self._j += 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment