summaryrefslogtreecommitdiffhomepage
path: root/src/netlink.c
diff options
context:
space:
mode:
authorHans Dedecker <dedeckeh@gmail.com>2019-03-29 15:10:29 +0100
committerHans Dedecker <dedeckeh@gmail.com>2019-03-29 15:11:25 +0100
commit7798d502f4c2e89a2493c0ea7a800a9359ee0bbc (patch)
treec936bcb532c3b9422c889307105b7883a92c26ab /src/netlink.c
parent0b20876022fed4f68934233b55c95b8fa91f92a9 (diff)
netlink: rework IPv4 address refresh logic
Rework logic to refresh IPv4 addresses so we can get rid of the second for loop Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Diffstat (limited to 'src/netlink.c')
-rw-r--r--src/netlink.c41
1 files changed, 17 insertions, 24 deletions
diff --git a/src/netlink.c b/src/netlink.c
index 4e151f5..d9c2b40 100644
--- a/src/netlink.c
+++ b/src/netlink.c
@@ -140,23 +140,6 @@ static void refresh_iface_addr4(int ifindex)
return;
avl_for_each_element(&interfaces, iface, avl) {
- if (iface->ifindex != ifindex)
- continue;
-
- change = len != (ssize_t)iface->addr4_len;
- for (ssize_t i = 0; !change && i < len; ++i) {
- if (addr[i].addr.in.s_addr != iface->addr4[i].addr.in.s_addr)
- change = true;
- }
- break;
- }
-
- if (!change) {
- free(addr);
- return;
- }
-
- avl_for_element_range(iface, avl_last_element(&interfaces, iface, avl), iface, avl) {
struct netevent_handler_info event_info;
if (iface->ifindex != ifindex)
@@ -167,20 +150,30 @@ static void refresh_iface_addr4(int ifindex)
event_info.addrs_old.addrs = iface->addr4;
event_info.addrs_old.len = iface->addr4_len;
+ if (!change) {
+ change = len != (ssize_t)iface->addr4_len;
+ for (ssize_t i = 0; !change && i < len; ++i) {
+ if (addr[i].addr.in.s_addr != iface->addr4[i].addr.in.s_addr)
+ change = true;
+ }
+ }
+
iface->addr4 = addr;
iface->addr4_len = len;
- call_netevent_handler_list(NETEV_ADDRLIST_CHANGE, &event_info);
+ if (change)
+ call_netevent_handler_list(NETEV_ADDRLIST_CHANGE, &event_info);
free(event_info.addrs_old.addrs);
- if (len) {
- addr = malloc(len * sizeof(*addr));
- if (!addr)
- return;
+ if (!len)
+ continue;
+
+ addr = malloc(len * sizeof(*addr));
+ if (!addr)
+ break;
- memcpy(addr, iface->addr4, len * sizeof(*addr));
- }
+ memcpy(addr, iface->addr4, len * sizeof(*addr));
}
free(addr);