From 48487aedead85b87fe4b209e5ca199159337ba23 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20H=C3=A4rdeman?= Date: Wed, 8 Oct 2025 17:23:46 +0200 Subject: [PATCH] dhcpv4: dhcpv4_lease() - convert to switch statement MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit And move the simple cases first, also bail if there's nothing to do. Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/286 Signed-off-by: Álvaro Fernández Rojas --- src/dhcpv4.c | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 173457a..88892e2 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -474,7 +474,36 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg msg, const uint8_t *mac, dhcpv4_fr_stop(a); } - if (msg == DHCPV4_MSG_DISCOVER || msg == DHCPV4_MSG_REQUEST) { + switch (msg) { + case DHCPV4_MSG_RELEASE: + if (!a) + return NULL; + + ubus_bcast_dhcp_event("dhcp.release", mac, + (struct in_addr *)&a->addr, + a->hostname, iface->ifname); + free_assignment(a); + a = NULL; + break; + + case DHCPV4_MSG_DECLINE: + if (!a) + return NULL; + + a->flags &= ~OAF_BOUND; + + if (!(a->flags & OAF_STATIC) || a->lease->ipaddr != a->addr) { + memset(a->hwaddr, 0, sizeof(a->hwaddr)); + a->valid_until = now + 3600; /* Block address for 1h */ + } else { + a->valid_until = now - 1; + } + break; + + case DHCPV4_MSG_DISCOVER: + _fallthrough; + + case DHCPV4_MSG_REQUEST: bool assigned = !!a; if (!a) { @@ -564,22 +593,10 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg msg, const uint8_t *mac, free_assignment(a); a = NULL; } + break; - } else if (msg == DHCPV4_MSG_RELEASE && a) { - ubus_bcast_dhcp_event("dhcp.release", mac, - (struct in_addr *)&a->addr, - a->hostname, iface->ifname); - free_assignment(a); - a = NULL; - - } else if (msg == DHCPV4_MSG_DECLINE && a) { - a->flags &= ~OAF_BOUND; - - if (!(a->flags & OAF_STATIC) || a->lease->ipaddr != a->addr) { - memset(a->hwaddr, 0, sizeof(a->hwaddr)); - a->valid_until = now + 3600; /* Block address for 1h */ - } else - a->valid_until = now - 1; + default: + return NULL; } dhcpv6_ia_write_statefile(); -- 2.30.2