From 50e65f8ce4d3c1f036bc3c356f595eaaa53d8aac Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Wed, 14 Jul 2021 11:57:59 +0200 Subject: [PATCH] n-dhcp4: add an accessor for the file name in the lease The name of the boot file can be either in option 67 or in a field of the DHCP header. Add an accessor for the one from the DHCP header. (cherry picked from commit 6a1349c5fb1968f67cd536b4a0135a8712bc66f6) --- src/n-dhcp4/src/libndhcp4.sym | 1 + src/n-dhcp4/src/n-dhcp4-c-lease.c | 36 +++++++++++++++++++++++++++++++ src/n-dhcp4/src/n-dhcp4.h | 1 + 3 files changed, 38 insertions(+) diff --git a/src/n-dhcp4/src/libndhcp4.sym b/src/n-dhcp4/src/libndhcp4.sym index 6c06e2fd28..4d11734e4d 100644 --- a/src/n-dhcp4/src/libndhcp4.sym +++ b/src/n-dhcp4/src/libndhcp4.sym @@ -37,6 +37,7 @@ global: n_dhcp4_client_lease_get_siaddr; n_dhcp4_client_lease_get_lifetime; n_dhcp4_client_lease_get_server_identifier; + n_dhcp4_client_lease_get_file; n_dhcp4_client_lease_query; n_dhcp4_client_lease_select; n_dhcp4_client_lease_accept; diff --git a/src/n-dhcp4/src/n-dhcp4-c-lease.c b/src/n-dhcp4/src/n-dhcp4-c-lease.c index 6564dbe811..bae2f674cd 100644 --- a/src/n-dhcp4/src/n-dhcp4-c-lease.c +++ b/src/n-dhcp4/src/n-dhcp4-c-lease.c @@ -269,6 +269,42 @@ _c_public_ int n_dhcp4_client_lease_get_server_identifier(NDhcp4ClientLease *lea return 0; } +/** + * n_dhcp4_client_lease_get_file() - query the lease for the boot file name + * @lease: the lease to operate on + * @file: return argument for the file name + * + * Query the lease for the boot file name from the DHCP header. The file name + * is returned as a NULL-terminated string. + * + * Return: 0 on success, + * N_DHCP4_E_UNSET if the lease does not contain a file name, or + * N_DCHP4_E_INTERNAL if the file name is invalid. + */ +_c_public_ int n_dhcp4_client_lease_get_file(NDhcp4ClientLease *lease, const char **file) { + NDhcp4Message *message; + + if (lease->message->options[N_DHCP4_OPTION_OVERLOAD].size > 0 + && ((*lease->message->options[N_DHCP4_OPTION_OVERLOAD].value) & N_DHCP4_OVERLOAD_FILE)) { + /* The field is overloaded to contain other options */ + return N_DHCP4_E_UNSET; + } + + message = &lease->message->message; + + if (message->file[0] == '\0') + return N_DHCP4_E_UNSET; + + if (!memchr(message->file, '\0', sizeof(message->file))) { + /* The field is NULL-terminated (RFC 2131 section 2) */ + return N_DHCP4_E_INTERNAL; + } + + *file = (const char *) message->file; + + return 0; +} + /** * n_dhcp4_client_lease_query() - query the lease for an option * @lease: the lease to operate on diff --git a/src/n-dhcp4/src/n-dhcp4.h b/src/n-dhcp4/src/n-dhcp4.h index 0be86e9fab..8493a487dd 100644 --- a/src/n-dhcp4/src/n-dhcp4.h +++ b/src/n-dhcp4/src/n-dhcp4.h @@ -172,6 +172,7 @@ void n_dhcp4_client_lease_get_basetime(NDhcp4ClientLease *lease, uint64_t *ns_ba void n_dhcp4_client_lease_get_lifetime(NDhcp4ClientLease *lease, uint64_t *ns_lifetimep); int n_dhcp4_client_lease_query(NDhcp4ClientLease *lease, uint8_t option, uint8_t **datap, size_t *n_datap); int n_dhcp4_client_lease_get_server_identifier(NDhcp4ClientLease *lease, struct in_addr *addr); +int n_dhcp4_client_lease_get_file(NDhcp4ClientLease *lease, const char **file); int n_dhcp4_client_lease_select(NDhcp4ClientLease *lease); int n_dhcp4_client_lease_accept(NDhcp4ClientLease *lease);