odhcpd: reduce use of WITH_UBUS defines in code
authorDavid Härdeman <[email protected]>
Tue, 7 Oct 2025 11:18:21 +0000 (13:18 +0200)
committerÁlvaro Fernández Rojas <[email protected]>
Thu, 9 Oct 2025 06:48:13 +0000 (08:48 +0200)
By defining some dummy functions in the header file, we can avoid sprinkling
WITH_UBUS defines through the code, which helps readability and also ensures
that compilation errors (e.g. when a variable is renamed) is caught in builds
with and without ubus.

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/270/
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/config.c
src/dhcpv4.c
src/odhcpd.h

index 46b73639dd1fd024013300383f05ef86ac34980b..0dcef1b568129f50d03184d37194fc24563e5dc7 100644 (file)
@@ -2100,9 +2100,7 @@ void odhcpd_reload(void)
 
        vlist_flush(&leases);
 
-#ifdef WITH_UBUS
        ubus_apply_network();
-#endif
 
        bool any_dhcpv6_slave = false, any_ra_slave = false, any_ndp_slave = false;
 
@@ -2210,10 +2208,8 @@ void odhcpd_run(void)
        signal(SIGINT, handle_signal);
        signal(SIGHUP, handle_signal);
 
-#ifdef WITH_UBUS
        while (ubus_init())
                sleep(1);
-#endif
 
        odhcpd_reload();
        uloop_run();
index 9caf35ec16745158954e641afedff9e57bbf0196..8b6a0ce639157f2d796f4468e95d3cabcd6b6382 100644 (file)
@@ -703,13 +703,11 @@ void dhcpv4_handle_msg(void *addr, void *data, size_t len,
        syslog(LOG_INFO, "Received %s from %s on %s", dhcpv4_msg_to_string(reqmsg),
                        odhcpd_print_mac(req->chaddr, req->hlen), iface->name);
 
-#ifdef WITH_UBUS
        if (reqmsg == DHCPV4_MSG_RELEASE) {
                struct in_addr ciaddr = req->ciaddr; // ensure pointer alignment
                ubus_bcast_dhcp_event("dhcp.release", req->chaddr, req->hlen,
-                                       &ciaddr, a ? a->hostname : NULL, iface->ifname);
+                                     &ciaddr, a ? a->hostname : NULL, iface->ifname);
        }
-#endif
 
        if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
                return;
@@ -933,13 +931,11 @@ void dhcpv4_handle_msg(void *addr, void *data, size_t len,
                        "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req->chaddr, req->hlen),
                        inet_ntoa(dest.sin_addr));
 
-#ifdef WITH_UBUS
        if (msg == DHCPV4_MSG_ACK) {
                struct in_addr yiaddr = reply.yiaddr; // ensure pointer alignment
                ubus_bcast_dhcp_event("dhcp.ack", req->chaddr, req->hlen, &yiaddr,
-                                       a ? a->hostname : NULL, iface->ifname);
+                                     a ? a->hostname : NULL, iface->ifname);
        }
-#endif
 }
 
 /* Handler for DHCPv4 messages */
index 4f88727d562c5da67f0af30b9f9b45f3e331f4dd..72442742c4d787aa26da36d89c8d345a56cdacf2 100644 (file)
@@ -519,9 +519,28 @@ int ubus_init(void);
 const char* ubus_get_ifname(const char *name);
 void ubus_apply_network(void);
 bool ubus_has_prefix(const char *name, const char *ifname);
-void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, const size_t mac_len,
-               const struct in_addr *addr, const char *name, const char *interface);
-#endif
+void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac,
+                          const size_t mac_len, const struct in_addr *addr,
+                          const char *name, const char *interface);
+#else
+static inline int ubus_init(void)
+{
+       return 0;
+}
+
+static inline void ubus_apply_network(void)
+{
+       return;
+}
+
+static inline
+void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac,
+                          const size_t mac_len, const struct in_addr *addr,
+                          const char *name, const char *interface)
+{
+       return;
+}
+#endif /* WITH_UBUS */
 
 ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *iface,
                const struct sockaddr_in6 *addr, const void *data, const uint8_t *end);