From: David Härdeman Date: Thu, 25 Sep 2025 13:16:53 +0000 (+0200) Subject: dhcpv4: simplify dhcpv4_fr_send() a bit X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=99c35f35971caea5b0e5a5daab7fddb189b06086;p=project%2Fodhcpd.git dhcpv4: simplify dhcpv4_fr_send() a bit Some minor cleanups to dhcpv4_fr_send() Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/266 Signed-off-by: Álvaro Fernández Rojas --- diff --git a/src/dhcpv4.c b/src/dhcpv4.c index fe0d869..f1e9106 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -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