From 3c32b27d8bab7547f48f6bfbfcc3847ccca24894 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20H=C3=A4rdeman?= Date: Fri, 17 Oct 2025 13:57:14 +0200 Subject: [PATCH] dhcpv4: simplify error handling in dhcpv4_setup_interface() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Remove the "ret" variable which can anyway always just be -1 on error, and rename the "out" label to "error" to clarify that it's only used on error. Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/318 Signed-off-by: Álvaro Fernández Rojas --- src/config.c | 6 ------ src/dhcpv4.c | 41 +++++++++++++++-------------------------- src/odhcpd.h | 6 +++++- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/config.c b/src/config.c index 865dadf..01bd375 100644 --- a/src/config.c +++ b/src/config.c @@ -372,9 +372,7 @@ static void close_interface(struct interface *iface) router_setup_interface(iface, false); dhcpv6_setup_interface(iface, false); ndp_setup_interface(iface, false); -#ifdef DHCPV4_SUPPORT dhcpv4_setup_interface(iface, false); -#endif /* make sure timer is not on the timeouts list before freeing */ uloop_timeout_cancel(&iface->timer_rs); @@ -1918,17 +1916,13 @@ void reload_services(struct interface *iface) router_setup_interface(iface, iface->ra != MODE_DISABLED); dhcpv6_setup_interface(iface, iface->dhcpv6 != MODE_DISABLED); ndp_setup_interface(iface, iface->ndp != MODE_DISABLED); -#ifdef DHCPV4_SUPPORT dhcpv4_setup_interface(iface, iface->dhcpv4 != MODE_DISABLED); -#endif } else { debug("Disabling services with %s not running", iface->ifname); router_setup_interface(iface, false); dhcpv6_setup_interface(iface, false); ndp_setup_interface(iface, false); -#ifdef DHCPV4_SUPPORT dhcpv4_setup_interface(iface, false); -#endif } } diff --git a/src/dhcpv4.c b/src/dhcpv4.c index c31d521..5d49b76 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -1435,9 +1435,8 @@ static int dhcpv4_setup_addresses(struct interface *iface) return 0; } -int dhcpv4_setup_interface(struct interface *iface, bool enable) +bool dhcpv4_setup_interface(struct interface *iface, bool enable) { - int ret = 0; struct sockaddr_in bind_addr = { .sin_family = AF_INET, .sin_port = htons(DHCPV4_SERVER_PORT), @@ -1457,75 +1456,65 @@ int dhcpv4_setup_interface(struct interface *iface, bool enable) avl_remove_all_elements(&iface->dhcpv4_leases, lease, iface_avl, tmp) dhcpv4_free_lease(lease); - return 0; + return true; } fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); if (fd < 0) { error("socket(AF_INET): %m"); - ret = -1; - goto out; + goto error; } /* Basic IPv4 configuration */ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) { error("setsockopt(SO_REUSEADDR): %m"); - ret = -1; - goto out; + goto error; } if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)) < 0) { error("setsockopt(SO_BROADCAST): %m"); - ret = -1; - goto out; + goto error; } if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val)) < 0) { error("setsockopt(IP_PKTINFO): %m"); - ret = -1; - goto out; + goto error; } val = IPTOS_CLASS_CS6; if (setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof(val)) < 0) { error("setsockopt(IP_TOS): %m"); - ret = -1; - goto out; + goto error; } val = IP_PMTUDISC_DONT; if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)) < 0) { error("setsockopt(IP_MTU_DISCOVER): %m"); - ret = -1; - goto out; + goto error; } if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, iface->ifname, strlen(iface->ifname)) < 0) { error("setsockopt(SO_BINDTODEVICE): %m"); - ret = -1; - goto out; + goto error; } if (bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { error("bind(): %m"); - ret = -1; - goto out; + goto error; } - if (dhcpv4_setup_addresses(iface) < 0) { - ret = -1; - goto out; - } + if (dhcpv4_setup_addresses(iface) < 0) + goto error; iface->dhcpv4_event.uloop.fd = fd; iface->dhcpv4_event.handle_dgram = dhcpv4_handle_dgram; odhcpd_register(&iface->dhcpv4_event); - return 0; + return true; -out: +error: close(fd); - return ret; + return false; } static void dhcpv4_addrlist_change(struct interface *iface) diff --git a/src/odhcpd.h b/src/odhcpd.h index 018426f..28478ab 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -644,11 +644,15 @@ int ndp_init(void); #ifdef DHCPV4_SUPPORT int dhcpv4_init(void); void dhcpv4_free_lease(struct dhcpv4_lease *a); -int dhcpv4_setup_interface(struct interface *iface, bool enable); +bool dhcpv4_setup_interface(struct interface *iface, bool enable); void dhcpv4_handle_msg(void *addr, void *data, size_t len, struct interface *iface, _o_unused void *dest_addr, send_reply_cb_t send_reply, void *opaque); #else +static inline bool dhcpv4_setup_interface(struct interface *iface, bool enable) { + return true; +} + static inline void dhcpv4_free_lease(struct dhcpv4_lease *lease) { error("Trying to free IPv4 assignment 0x%p", lease); } -- 2.30.2