From 37bc48add47d635a51c025b63208da2d0a58d271 Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Wed, 14 Jul 2021 09:37:05 -0500 Subject: [PATCH] handshake: Optimize replacement of IEs During processing of Connect events by netdev, some of these elements might be updated even when already set. Instead of issuing l_free/l_memdup each time, check and see whether the elements are bitwise identical first. --- src/handshake.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/handshake.c b/src/handshake.c index 23d1cb0e..8317d96a 100644 --- a/src/handshake.c +++ b/src/handshake.c @@ -241,6 +241,26 @@ bool handshake_state_set_supplicant_ie(struct handshake_state *s, return handshake_state_setup_own_ciphers(s, &info); } +static void replace_ie(uint8_t **old, const uint8_t *new) +{ + if (*old == NULL) { + *old = new ? l_memdup(new, new[1] + 2) : NULL; + return; + } + + if (!new) { + l_free(*old); + *old = NULL; + return; + } + + if ((*old)[1] == new[1] && !memcmp(*old, new, new[1] + 2)) + return; + + l_free(*old); + *old = l_memdup(new, new[1] + 2); +} + void handshake_state_set_ssid(struct handshake_state *s, const uint8_t *ssid, size_t ssid_len) { @@ -250,18 +270,12 @@ void handshake_state_set_ssid(struct handshake_state *s, const uint8_t *ssid, void handshake_state_set_mde(struct handshake_state *s, const uint8_t *mde) { - if (s->mde) - l_free(s->mde); - - s->mde = mde ? l_memdup(mde, mde[1] + 2) : NULL; + replace_ie(&s->mde, mde); } void handshake_state_set_fte(struct handshake_state *s, const uint8_t *fte) { - if (s->fte) - l_free(s->fte); - - s->fte = fte ? l_memdup(fte, fte[1] + 2) : NULL; + replace_ie(&s->fte, fte); } void handshake_state_set_kh_ids(struct handshake_state *s,