mirror of
https://github.com/vincentmli/bpfire.git
synced 2026-04-10 11:05:54 +02:00
unbound-dhcp-leases-bridge: Decode any incoming messages
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
This commit is contained in:
@@ -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.error("Received message:\n%s" % data.decode())
|
||||
# Log that we have received some data
|
||||
log.debug("Received message of %s byte(s)" % len(data))
|
||||
|
||||
# TODO
|
||||
# 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
|
||||
|
||||
conn.send(b"OK\n")
|
||||
|
||||
# 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 = []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user