]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
unbound-dhcp-leases-bridge: Decode any incoming messages
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 May 2024 13:50:30 +0000 (14:50 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 May 2024 13:50:30 +0000 (14:50 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
config/unbound/unbound-dhcp-leases-bridge

index 6aa9bf9da4625c61769b71af4448eb0b094df5e3..aff82ec99d7fa57677c1e82a94979dce38277354 100644 (file)
@@ -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 = []