router: optimize duplicated PIO comparison
authorÁlvaro Fernández Rojas <[email protected]>
Thu, 4 Dec 2025 14:38:20 +0000 (15:38 +0100)
committerÁlvaro Fernández Rojas <[email protected]>
Thu, 4 Dec 2025 14:54:59 +0000 (15:54 +0100)
Compare prefix and length with a single memcmp instead of performing
separate comparisons.

Suggested-by: Paul Donald <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/336
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/odhcpd.h
src/router.c

index 1ce2452ce6f46b491a3d49f2c6e04baaa10348fc..0b308b2d1cabcbd2ced44ee44177fbd5d35d7ba6 100644 (file)
@@ -346,10 +346,13 @@ struct dnr_options {
 
 // RA PIO - RFC9096
 struct ra_pio {
-       struct in6_addr prefix;
-       uint8_t length;
+       struct {
+               struct in6_addr prefix;
+               uint8_t length;
+       };
        time_t lifetime;
 };
+#define ra_pio_cmp_len offsetof(struct ra_pio, lifetime)
 
 
 struct interface {
index ef47d0ffac0b78763dbd2cb3bd4ca8ae0dd7361a..0e7a1109e5b7853683f1852bba12f58033b2f66b 100644 (file)
@@ -538,8 +538,7 @@ static void router_clear_duplicated_ra_pio(struct interface *iface)
                while (j < iface->pio_cnt) {
                        struct ra_pio *pio_b = &iface->pios[j];
 
-                       if (pio_a->length == pio_b->length &&
-                           !memcmp(&pio_a->prefix, &pio_b->prefix, sizeof(struct in6_addr))) {
+                       if (!memcmp(pio_a, pio_b, ra_pio_cmp_len)) {
                                warn("rfc9096: %s: clear duplicated %s/%u",
                                     iface->ifname,
                                     inet_ntop(AF_INET6, &pio_a->prefix, ipv6_str, sizeof(ipv6_str)),