dhcpv4: more refactoring of dhcpv4_handle_msg()
authorDavid Härdeman <[email protected]>
Sun, 5 Oct 2025 19:21:50 +0000 (21:21 +0200)
committerÁlvaro Fernández Rojas <[email protected]>
Tue, 21 Oct 2025 17:04:39 +0000 (19:04 +0200)
Move some gnarly conditionals into a switch in preparation of later patches.

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

index 8d633b6f587dbc5b8650381cdf4ae063cd1d4068..6b24ae1883193c990d5593cc26ad209c0000fb06 100644 (file)
@@ -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);