Use a loop to add request options.
authorflorian <florian@openbsd.org>
Wed, 5 Jun 2024 16:14:12 +0000 (16:14 +0000)
committerflorian <florian@openbsd.org>
Wed, 5 Jun 2024 16:14:12 +0000 (16:14 +0000)
This makes it easier to extend in the future.

sbin/dhcp6leased/dhcp6leased.h
sbin/dhcp6leased/frontend.c

index 7a4b3ec..35f29cc 100644 (file)
@@ -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 <florian@openbsd.org>
  * 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"
index c5cb446..b5e349c 100644 (file)
@@ -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 <florian@openbsd.org>
@@ -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);