From b1be3984ebf87cdc7a489623b299e574923e3475 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20H=C3=A4rdeman?= Date: Sun, 5 Oct 2025 21:21:50 +0200 Subject: [PATCH] dhcpv4: more refactoring of dhcpv4_handle_msg() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Move some gnarly conditionals into a switch in preparation of later patches. Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/278 Signed-off-by: Álvaro Fernández Rojas --- src/dhcpv4.c | 79 ++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 8d633b6..6b24ae1 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -703,49 +703,54 @@ void dhcpv4_handle_msg(void *addr, void *data, size_t len, return; } - if (!a) { - if (req_msg == DHCPV4_MSG_REQUEST) - msg = DHCPV4_MSG_NAK; - else if (req_msg == DHCPV4_MSG_DISCOVER) + info("Received %s from %s on %s", dhcpv4_msg_to_string(req_msg), + odhcpd_print_mac(req->chaddr, req->hlen), iface->name); + + switch (req_msg) { + case DHCPV4_MSG_DISCOVER: + if (!a) return; - } else if (req_msg == DHCPV4_MSG_DISCOVER) msg = DHCPV4_MSG_OFFER; - else if (req_msg == DHCPV4_MSG_REQUEST && - ((req_addr && req_addr != a->addr) || - (req->ciaddr.s_addr && req->ciaddr.s_addr != a->addr))) { - msg = DHCPV4_MSG_NAK; - /* - * DHCP client requested an IP which we can't offer to him. Probably the - * client changed the network or the network has been changed. The reply - * type is set to DHCPV4_MSG_NAK, because the client should not use that IP. - * - * For modern devices we build an answer that includes a valid IP, like - * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to - * perform additional DHCP round trips. - * - */ - - /* - * - * Buggy clients do serverid checking in nack messages; therefore set the - * serverid in nack messages triggered by a previous force renew equal to - * the server id in use at that time by the server - * - */ - if (fr_serverid) - serverid = fr_serverid; + break; - if (req->ciaddr.s_addr && - ((iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr) != - (req->ciaddr.s_addr & iface->dhcpv4_mask.s_addr))) - req->ciaddr.s_addr = INADDR_ANY; - } + case DHCPV4_MSG_REQUEST: + if (!a) { + msg = DHCPV4_MSG_NAK; + break; + } - info("Received %s from %s on %s", dhcpv4_msg_to_string(req_msg), - odhcpd_print_mac(req->chaddr, req->hlen), iface->name); + if ((req_addr && req_addr != a->addr) || + (req->ciaddr.s_addr && req->ciaddr.s_addr != a->addr)) { + msg = DHCPV4_MSG_NAK; + /* + * DHCP client requested an IP which we can't offer to him. Probably the + * client changed the network or the network has been changed. The reply + * type is set to DHCPV4_MSG_NAK, because the client should not use that IP. + * + * For modern devices we build an answer that includes a valid IP, like + * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to + * perform additional DHCP round trips. + * + * Buggy clients do serverid checking in nack messages; therefore set the + * serverid in nack messages triggered by a previous force renew equal to + * the server id in use at that time by the server + * + */ + if (fr_serverid) + serverid = fr_serverid; + + if (req->ciaddr.s_addr && + ((iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr) != + (req->ciaddr.s_addr & iface->dhcpv4_mask.s_addr))) + req->ciaddr.s_addr = INADDR_ANY; + } + break; - if (req_msg == DHCPV4_MSG_DECLINE || req_msg == DHCPV4_MSG_RELEASE) + case DHCPV4_MSG_RELEASE: + _fallthrough; + case DHCPV4_MSG_DECLINE: return; + } dhcpv4_put(&reply, &cursor, DHCPV4_OPT_MESSAGE, 1, &msg); dhcpv4_put(&reply, &cursor, DHCPV4_OPT_SERVERID, 4, &serverid); -- 2.30.2