mirror of
https://github.com/kernelkit/infix.git
synced 2026-08-06 15:43:02 +02:00
Add support for DHCP server/relay
This patch introduces a new yang yang model for DHCP server and relay, using dnsmasq to provide the functionality on a per-interface basis. Available DHCP options will be queried by confd on startup and can then be configured in a flexible manner. The DHCP option 82 feature has been realized by patching dnsmasq (see patches/dnsmasq/2.90/0000-relay-agent-info.patch). Signed-off-by: Stefan Schlosser <sgs@grmmbl.org>
This commit is contained in:
committed by
Joachim Wiberg
parent
455c384164
commit
60f459687c
@@ -57,6 +57,12 @@ class PadSoftware:
|
||||
state = 10
|
||||
version = 23
|
||||
|
||||
class PadDhcpServer:
|
||||
iface = 7
|
||||
ip = 17
|
||||
mac = 19
|
||||
host = 21
|
||||
exp = 7
|
||||
|
||||
class PadUsbPort:
|
||||
title = 30
|
||||
@@ -350,6 +356,34 @@ class STPPortID:
|
||||
)
|
||||
return f"{prio:1x}.{pid:03x}"
|
||||
|
||||
class DhcpServer:
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.iface = get_json_data('', self.data, 'if-name')
|
||||
self.leases = []
|
||||
now = datetime.now(timezone.utc)
|
||||
for lease in get_json_data([], self.data, 'server', 'lease', 'host'):
|
||||
dt = datetime.strptime(lease["expires"], "%Y-%m-%dT%H:%M:%S%z")
|
||||
exp = (dt - now).total_seconds()
|
||||
self.leases.append({
|
||||
"ip": lease["ip-address"],
|
||||
"mac": lease["hardware-address"],
|
||||
"host": lease["hostname"],
|
||||
"exp": int(exp)
|
||||
})
|
||||
|
||||
def print(self):
|
||||
for lease in self.leases:
|
||||
ip = lease["ip"]
|
||||
mac = lease["mac"]
|
||||
exp = "%ds" % lease["exp"]
|
||||
host = lease["host"][:20]
|
||||
row = f"{self.iface:<{PadDhcpServer.iface}}"
|
||||
row += f"{ip:<{PadDhcpServer.ip}}"
|
||||
row += f"{mac:<{PadDhcpServer.mac}}"
|
||||
row += f"{host:<{PadDhcpServer.host}}"
|
||||
row += f"{exp:<{PadDhcpServer.exp}}"
|
||||
print(row)
|
||||
|
||||
class Iface:
|
||||
def __init__(self, data):
|
||||
@@ -1038,6 +1072,23 @@ def show_ntp(json):
|
||||
row += f"{source['poll']:>{PadNtpSource.poll}}"
|
||||
print(row)
|
||||
|
||||
def show_dhcp_server(json):
|
||||
if not json.get("infix-dhcp-server:dhcp-server"):
|
||||
print("DHCP server not enabled.")
|
||||
return
|
||||
|
||||
hdr = (f"{'IFACE':<{PadDhcpServer.iface}}"
|
||||
f"{'IP':<{PadDhcpServer.ip}}"
|
||||
f"{'MAC':<{PadDhcpServer.mac}}"
|
||||
f"{'HOSTNAME':<{PadDhcpServer.host}}"
|
||||
f"{'EXPIRES':<{PadDhcpServer.exp}}")
|
||||
print(Decore.invert(hdr))
|
||||
|
||||
servers = get_json_data({}, json, "infix-dhcp-server:dhcp-server", "server-if")
|
||||
for s in servers:
|
||||
server = DhcpServer(s)
|
||||
server.print()
|
||||
|
||||
def main():
|
||||
global UNIT_TEST
|
||||
|
||||
@@ -1074,6 +1125,8 @@ def main():
|
||||
|
||||
parser_show_boot_order = subparsers.add_parser('show-boot-order', help='Show NTP sources')
|
||||
|
||||
parser_show_routing_table = subparsers.add_parser('show-dhcp-server', help='Show DHCP server')
|
||||
|
||||
args = parser.parse_args()
|
||||
UNIT_TEST = args.test
|
||||
|
||||
@@ -1091,6 +1144,8 @@ def main():
|
||||
show_hardware(json_data)
|
||||
elif args.command == "show-ntp":
|
||||
show_ntp(json_data)
|
||||
elif args.command == "show-dhcp-server":
|
||||
show_dhcp_server(json_data)
|
||||
else:
|
||||
print(f"Error, unknown command '{args.command}'")
|
||||
sys.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user