odhcpd: break up complex matching logic
authorDavid Härdeman <[email protected]>
Sat, 20 Sep 2025 14:40:32 +0000 (16:40 +0200)
committerÁlvaro Fernández Rojas <[email protected]>
Thu, 9 Oct 2025 06:57:23 +0000 (08:57 +0200)
Like Winnie the Pooh, “I am a Bear of Very Little Brain, and long words
Bother me”.

Simplify the assignment checking logic by breaking it up a bit. This is
in preparation for subsequent patches.

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

index 8cc95156f939502834faa5c346f7df1226760a8a..549256f866ac8466d62ae20be7bbd31032aef5f4 100644 (file)
@@ -1499,21 +1499,38 @@ ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *ifac
                        }
                }
 
-               /* Find assignment */
+               /* Find an existing assignment */
                struct dhcp_assignment *c, *a = NULL;
                list_for_each_entry(c, &iface->ia_assignments, head) {
-                       if ((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) &&
-                           c->iaid == ia->iaid && (INFINITE_VALID(c->valid_until) || now < c->valid_until) &&
-                           ((is_pd && (c->flags & OAF_DHCPV6_PD)) || (is_na && (c->flags & OAF_DHCPV6_NA)))) {
-                               a = c;
+                       /* If we're looking for a PD, is this a PD? */
+                       if (is_pd && !(c->flags & OAF_DHCPV6_PD))
+                               continue;
 
-                               /* Reset state */
-                               if (a->flags & OAF_BOUND)
-                                       apply_lease(a, false);
+                       /* If we're looking for a NA, is this a NA? */
+                       if (is_na && !(c->flags & OAF_DHCPV6_NA))
+                               continue;
 
-                               stop_reconf(a);
-                               break;
-                       }
+                       /* Is this assignment still valid? */
+                       if (!INFINITE_VALID(c->valid_until) && now >= c->valid_until)
+                               continue;
+
+                       /* Does the DUID match? */
+                       if (c->clid_len != clid_len || memcmp(c->clid_data, clid_data, clid_len))
+                              continue;
+
+                       /* Does the IAID match? */
+                       if (c->iaid != ia->iaid)
+                              continue;
+
+                       /* We have a match */
+                       a = c;
+
+                       /* Reset state */
+                       if (a->flags & OAF_BOUND)
+                               apply_lease(a, false);
+
+                       stop_reconf(a);
+                       break;
                }
 
                if (l && a && a->lease != l) {