diff options
| author | David Härdeman | 2025-10-08 15:23:46 +0000 |
|---|---|---|
| committer | Álvaro Fernández Rojas | 2025-10-25 15:09:24 +0000 |
| commit | 48487aedead85b87fe4b209e5ca199159337ba23 (patch) | |
| tree | cfb0e62bee3eb8709f0c0400eaf95f26663f2205 | |
| parent | 3ad54baa4adc0d6d102ccdc82f8b1f7439059f6e (diff) | |
| download | odhcpd-48487aedead85b87fe4b209e5ca199159337ba23.tar.gz | |
dhcpv4: dhcpv4_lease() - convert to switch statement
And move the simple cases first, also bail if there's nothing to do.
Signed-off-by: David Härdeman <david@hardeman.nu>
Link: https://github.com/openwrt/odhcpd/pull/286
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
| -rw-r--r-- | src/dhcpv4.c | 49 |
1 files 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(); |