summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorViacheslav Hletenko <v.gletenko@vyos.io>2025-02-27 22:05:41 +0200
committerGitHub <noreply@github.com>2025-02-27 22:05:41 +0200
commit8c14daa8fa262b79107b98e80f960ebcc97b3031 (patch)
treeb3ec56c47a86daa6c62b44bc7e084b69ba1533b5 /python
parentf327d0286e43f35e777f94ce3de1a631f26d1ac2 (diff)
parentcc45fafda413312213b0402e381774aaeb001d6e (diff)
downloadvyos-1x-8c14daa8fa262b79107b98e80f960ebcc97b3031.tar.gz
vyos-1x-8c14daa8fa262b79107b98e80f960ebcc97b3031.zip
Merge pull request #4237 from indrajitr/hostd-update
T6948: Keep DHCP server leases in sync with hostd records
Diffstat (limited to 'python')
-rw-r--r--python/vyos/kea.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/python/vyos/kea.py b/python/vyos/kea.py
index 65e2d99b4..c7947af3e 100644
--- a/python/vyos/kea.py
+++ b/python/vyos/kea.py
@@ -384,6 +384,41 @@ def kea_get_leases(inet):
return leases['arguments']['leases']
+def kea_add_lease(
+ inet,
+ ip_address,
+ host_name=None,
+ mac_address=None,
+ iaid=None,
+ duid=None,
+ subnet_id=None,
+):
+ args = {'ip-address': ip_address}
+
+ if host_name:
+ args['hostname'] = host_name
+
+ if subnet_id:
+ args['subnet-id'] = subnet_id
+
+ # IPv4 requires MAC address, IPv6 requires either MAC address or DUID
+ if mac_address:
+ args['hw-address'] = mac_address
+ if duid:
+ args['duid'] = duid
+
+ # IPv6 requires IAID
+ if inet == '6' and iaid:
+ args['iaid'] = iaid
+
+ result = _ctrl_socket_command(inet, f'lease{inet}-add', args)
+
+ if result and 'result' in result:
+ return result['result'] == 0
+
+ return False
+
+
def kea_delete_lease(inet, ip_address):
args = {'ip-address': ip_address}
@@ -430,6 +465,32 @@ def kea_get_pool_from_subnet_id(config, inet, subnet_id):
return None
+def kea_get_domain_from_subnet_id(config, inet, subnet_id):
+ shared_networks = dict_search_args(
+ config, 'arguments', f'Dhcp{inet}', 'shared-networks'
+ )
+
+ if not shared_networks:
+ return None
+
+ for network in shared_networks:
+ if f'subnet{inet}' not in network:
+ continue
+
+ for subnet in network[f'subnet{inet}']:
+ if 'id' in subnet and int(subnet['id']) == int(subnet_id):
+ for option in subnet['option-data']:
+ if option['name'] == 'domain-name':
+ return option['data']
+
+ # domain-name is not found in subnet, fallback to shared-network pool option
+ for option in network['option-data']:
+ if option['name'] == 'domain-name':
+ return option['data']
+
+ return None
+
+
def kea_get_static_mappings(config, inet, pools=[]) -> list:
"""
Get DHCP static mapping from active Kea DHCPv4 or DHCPv6 configuration
@@ -491,6 +552,11 @@ def kea_get_server_leases(config, inet, pools=[], state=[], origin=None) -> list
if config
else '-'
)
+ data_lease['domain'] = (
+ kea_get_domain_from_subnet_id(config, inet, lease['subnet-id'])
+ if config
+ else ''
+ )
data_lease['end'] = (
lease['expire_time'].timestamp() if lease['expire_time'] else None
)