From 7ebda3892c446154e10dc13ea488e4e2ebd40d21 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Wed, 30 Oct 2024 10:15:42 -0400 Subject: [PATCH 1/3] lib: Replace usage of in6addr_cmp with memcmp memcmp will return and act exactly the same as in6addr_cmp but it does it significantly faster than how in6addr_cmp does it. Let this be a lesson for implementing something that is a duplicate of what is provided by the c library. Signed-off-by: Donald Sharp --- lib/sockunion.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sockunion.c b/lib/sockunion.c index c37ab1d6dd53..1b08a06e9362 100644 --- a/lib/sockunion.c +++ b/lib/sockunion.c @@ -621,7 +621,8 @@ int sockunion_cmp(const union sockunion *su1, const union sockunion *su2) return -1; } if (su1->sa.sa_family == AF_INET6) - return in6addr_cmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr); + return IN6_ADDR_CMP(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr); + return 0; } From 1cb6b71b4059b5fba67f78aebdbd7b4c6d8547df Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Wed, 30 Oct 2024 10:41:08 -0400 Subject: [PATCH 2/3] lib, tests: Remove in6addr_cmp function from the system This function should just be memcmp. Signed-off-by: Donald Sharp --- lib/sockunion.c | 17 ----------------- lib/sockunion.h | 1 - tests/lib/test_frrlua.c | 2 +- 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/lib/sockunion.c b/lib/sockunion.c index 1b08a06e9362..4a7c190953c7 100644 --- a/lib/sockunion.c +++ b/lib/sockunion.c @@ -588,23 +588,6 @@ static void __attribute__((unused)) sockunion_print(const union sockunion *su) } } -int in6addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2) -{ - unsigned int i; - const uint8_t *p1, *p2; - - p1 = (const uint8_t *)addr1; - p2 = (const uint8_t *)addr2; - - for (i = 0; i < sizeof(struct in6_addr); i++) { - if (p1[i] > p2[i]) - return 1; - else if (p1[i] < p2[i]) - return -1; - } - return 0; -} - int sockunion_cmp(const union sockunion *su1, const union sockunion *su2) { if (su1->sa.sa_family > su2->sa.sa_family) diff --git a/lib/sockunion.h b/lib/sockunion.h index 146651225cdf..5152e70a234b 100644 --- a/lib/sockunion.h +++ b/lib/sockunion.h @@ -93,7 +93,6 @@ enum connect_result { connect_error, connect_success, connect_in_progress }; /* Prototypes. */ extern int str2sockunion(const char *, union sockunion *); extern const char *sockunion2str(const union sockunion *, char *, size_t); -int in6addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2); extern int sockunion_cmp(const union sockunion *, const union sockunion *); extern int sockunion_same(const union sockunion *, const union sockunion *); extern unsigned int sockunion_hash(const union sockunion *); diff --git a/tests/lib/test_frrlua.c b/tests/lib/test_frrlua.c index 4aa8c8a8c396..9a1c3eaab66a 100644 --- a/tests/lib/test_frrlua.c +++ b/tests/lib/test_frrlua.c @@ -90,7 +90,7 @@ static void test_encode_decode(void) lua_pushin6addr(L, &in6addr_a); lua_decode_in6addr(L, -1, &in6addr_a); - assert(in6addr_cmp(&in6addr_a, &in6addr_b) == 0); + assert(memcmp(&in6addr_a, &in6addr_b, sizeof(struct in6_addr)) == 0); assert(lua_gettop(L) == 0); union sockunion su_a, su_b; From ff9781ebf529eb6c0ab62e4789fbeaa7283404b7 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Thu, 31 Oct 2024 10:06:26 -0400 Subject: [PATCH 3/3] lib: In sockunion.c convert v6 memcmp's to IPV6_ADDR_CMP Signed-off-by: Donald Sharp --- lib/sockunion.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/sockunion.c b/lib/sockunion.c index 4a7c190953c7..7acb5004db0f 100644 --- a/lib/sockunion.c +++ b/lib/sockunion.c @@ -403,8 +403,7 @@ int sockunion_same(const union sockunion *su1, const union sockunion *su2) sizeof(struct in_addr)); break; case AF_INET6: - ret = memcmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr, - sizeof(struct in6_addr)); + ret = IPV6_ADDR_CMP(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr); if ((ret == 0) && IN6_IS_ADDR_LINKLOCAL(&su1->sin6.sin6_addr)) { /* compare interface indices */ if (su1->sin6.sin6_scope_id && su2->sin6.sin6_scope_id) @@ -604,7 +603,7 @@ int sockunion_cmp(const union sockunion *su1, const union sockunion *su2) return -1; } if (su1->sa.sa_family == AF_INET6) - return IN6_ADDR_CMP(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr); + return IPV6_ADDR_CMP(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr); return 0; } @@ -711,8 +710,7 @@ int sockunion_is_null(const union sockunion *su) case AF_INET: return (su->sin.sin_addr.s_addr == 0); case AF_INET6: - return !memcmp(su->sin6.sin6_addr.s6_addr, null_s6_addr, - sizeof(null_s6_addr)); + return !IPV6_ADDR_CMP(su->sin6.sin6_addr.s6_addr, null_s6_addr); default: return 0; }