From 43f001cb5b58dca18090f32eb1eae63bb001a329 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 10 May 2024 14:50:30 +0100 Subject: [PATCH] unbound-dhcp-leases-bridge: Decode any incoming messages Signed-off-by: Michael Tremer --- config/unbound/unbound-dhcp-leases-bridge | 59 +++++++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/config/unbound/unbound-dhcp-leases-bridge b/config/unbound/unbound-dhcp-leases-bridge index 6aa9bf9da..aff82ec99 100644 --- a/config/unbound/unbound-dhcp-leases-bridge +++ b/config/unbound/unbound-dhcp-leases-bridge @@ -105,12 +105,35 @@ class UnboundDHCPLeasesBridge(object): except OSError as e: break - # Receive what the client is sending - data, ancillary_data, flags, address = conn.recvmsg(4096) + try: + # Receive what the client is sending + data, ancillary_data, flags, address = conn.recvmsg(4096) + + # Log that we have received some data + log.debug("Received message of %s byte(s)" % len(data)) + + # Decode the data + message = self._decode_message(data) + + # Log the received message + log.debug("Received message:") + for key in message: + log.debug(" %-20s = %s" % (key, message[key])) + + # TODO - log.error("Received message:\n%s" % data.decode()) + conn.send(b"OK\n") - # TODO + # Send ERROR to the client if something went wrong + except Exception as e: + log.error("Could not handle message: %s" % e) + + conn.send(b"ERROR\n") + continue + + # Close the connection + finally: + conn.close() log.info("Unbound DHCP Leases Bridge terminated") @@ -137,6 +160,34 @@ class UnboundDHCPLeasesBridge(object): return s + def _decode_message(self, data): + message = {} + + for line in data.splitlines(): + # Skip empty lines + if not line: + continue + + # Try to decode the line + try: + line = line.decode() + except UnicodeError as e: + log.error("Could not decode %r: %s" % (line, e)) + + raise e + + # Split the line + key, _, value = line.partition("=") + + # Skip the line if it does not have a value + if not _: + raise ValueError("No value given") + + # Store the attributes + message[key] = value + + return message + def update_dhcp_leases(self): leases = [] -- 2.39.5