From: florian Date: Wed, 5 Jun 2024 16:14:12 +0000 (+0000) Subject: Use a loop to add request options. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=1788a9629d2a9b2adf949840d6c5bd40293d0f23;p=openbsd Use a loop to add request options. This makes it easier to extend in the future. --- diff --git a/sbin/dhcp6leased/dhcp6leased.h b/sbin/dhcp6leased/dhcp6leased.h index 7a4b3ece1aa..35f29cc2d8a 100644 --- a/sbin/dhcp6leased/dhcp6leased.h +++ b/sbin/dhcp6leased/dhcp6leased.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcp6leased.h,v 1.5 2024/06/03 18:10:04 florian Exp $ */ +/* $OpenBSD: dhcp6leased.h,v 1.6 2024/06/05 16:14:12 florian Exp $ */ /* * Copyright (c) 2017, 2021 Florian Obser @@ -18,6 +18,10 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#ifndef nitems +#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) +#endif + #define _PATH_LOCKFILE "/dev/dhcp6leased.lock" #define _PATH_CONF_FILE "/etc/dhcp6leased.conf" #define _PATH_CTRL_SOCKET "/dev/dhcp6leased.sock" diff --git a/sbin/dhcp6leased/frontend.c b/sbin/dhcp6leased/frontend.c index c5cb4467f83..b5e349ccae0 100644 --- a/sbin/dhcp6leased/frontend.c +++ b/sbin/dhcp6leased/frontend.c @@ -1,4 +1,4 @@ -/* $OpenBSD: frontend.c,v 1.10 2024/06/05 10:25:07 florian Exp $ */ +/* $OpenBSD: frontend.c,v 1.11 2024/06/05 16:14:12 florian Exp $ */ /* * Copyright (c) 2017, 2021, 2024 Florian Obser @@ -795,8 +795,11 @@ build_packet(uint8_t message_type, struct iface *iface, char *if_name) struct dhcp_iapd iapd; struct dhcp_iaprefix iaprefix; struct dhcp_vendor_class vendor_class; + size_t i; ssize_t len; uint16_t request_option_code, elapsed_time; + const uint16_t options[] = {DHO_SOL_MAX_RT, + DHO_INF_MAX_RT}; uint8_t *p; switch (message_type) { @@ -890,15 +893,14 @@ build_packet(uint8_t message_type, struct iface *iface, char *if_name) } opt_hdr.code = htons(DHO_ORO); - opt_hdr.len = htons(2 * 2); + opt_hdr.len = htons(sizeof(request_option_code) * nitems(options)); memcpy(p, &opt_hdr, sizeof(struct dhcp_option_hdr)); p += sizeof(struct dhcp_option_hdr); - request_option_code = htons(DHO_SOL_MAX_RT); - memcpy(p, &request_option_code, sizeof(uint16_t)); - p += sizeof(uint16_t); - request_option_code = htons(DHO_INF_MAX_RT); - memcpy(p, &request_option_code, sizeof(uint16_t)); - p += sizeof(uint16_t); + for (i = 0; i < nitems(options); i++) { + request_option_code = htons(options[i]); + memcpy(p, &request_option_code, sizeof(uint16_t)); + p += sizeof(uint16_t); + } opt_hdr.code = htons(DHO_ELAPSED_TIME); opt_hdr.len = htons(2);