dhcpv4: simplify dhcpv4_fr_send() a bit
authorDavid Härdeman <[email protected]>
Thu, 25 Sep 2025 13:16:53 +0000 (15:16 +0200)
committerÁlvaro Fernández Rojas <[email protected]>
Tue, 7 Oct 2025 09:03:51 +0000 (11:03 +0200)
Some minor cleanups to dhcpv4_fr_send()

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/266
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/dhcpv4.c

index fe0d869172fdbaa3b601f17b8fa02331965209a8..f1e910617f6715562586aea90c44f156de019dc1 100644 (file)
@@ -181,17 +181,21 @@ static void dhcpv4_fr_send(struct dhcp_assignment *a)
                .key = { 0 },
        };
        struct interface *iface = a->iface;
+       uint8_t *cursor = &fr_msg.options[DHCPV4_MAGIC_COOKIE_LEN];
+       uint8_t msg = DHCPV4_MSG_FORCERENEW;
+       struct sockaddr_in dest = {
+               .sin_family = AF_INET,
+               .sin_port = htons(DHCPV4_CLIENT_PORT),
+               .sin_addr = { a->addr },
+       };
 
        odhcpd_urandom(&fr_msg.xid, sizeof(fr_msg.xid));
        memcpy(fr_msg.chaddr, a->hwaddr, fr_msg.hlen);
 
-       uint8_t *cursor = &fr_msg.options[DHCPV4_MAGIC_COOKIE_LEN];
-       uint8_t msg = DHCPV4_MSG_FORCERENEW;
-
        dhcpv4_put(&fr_msg, &cursor, DHCPV4_OPT_MESSAGE, sizeof(msg), &msg);
        if (a->accept_fr_nonce) {
+               auth_o = (struct dhcpv4_auth_forcerenew *)cursor;
                dhcpv4_put(&fr_msg, &cursor, DHCPV4_OPT_AUTHENTICATION, sizeof(auth), &auth);
-               auth_o = (struct dhcpv4_auth_forcerenew *)(cursor - sizeof(auth));
                dhcpv4_put(&fr_msg, &cursor, DHCPV4_OPT_END, 0, NULL);
 
                md5_ctx_t md5;
@@ -222,14 +226,8 @@ static void dhcpv4_fr_send(struct dhcp_assignment *a)
                dhcpv4_put(&fr_msg, &cursor, DHCPV4_OPT_END, 0, NULL);
        }
 
-       struct sockaddr_in dest;
-       memset(&dest, 0, sizeof(dest));
-       dest.sin_family = AF_INET;
-       dest.sin_port = htons(DHCPV4_CLIENT_PORT);
-       dest.sin_addr.s_addr = a->addr;
-
        if (sendto(iface->dhcpv4_event.uloop.fd, &fr_msg, PACKET_SIZE(&fr_msg, cursor),
-                       MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest)) < 0)
+                  MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest)) < 0)
                syslog(LOG_ERR, "Failed to send %s to %s - %s: %m", dhcpv4_msg_to_string(msg),
                        odhcpd_print_mac(a->hwaddr, sizeof(a->hwaddr)), inet_ntoa(dest.sin_addr));
        else