From 95d26df34c70f87bf3c2d20f2fbd0b22b7c88ce4 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Thu, 23 Jul 2020 11:08:35 -0600 Subject: [PATCH] lifecycle: refactor address handling a little --- libifupdown/lifecycle.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/libifupdown/lifecycle.c b/libifupdown/lifecycle.c index 0312788..65a6981 100644 --- a/libifupdown/lifecycle.c +++ b/libifupdown/lifecycle.c @@ -43,6 +43,17 @@ handle_commands_for_phase(const struct lif_execute_opts *opts, char *const envp[ return true; } +static bool +handle_address(const struct lif_execute_opts *opts, struct lif_address *addr, const char *cmd, const char *lifname) +{ + char addrbuf[4096]; + + if (!lif_address_unparse(addr, addrbuf, sizeof addrbuf, true)) + return false; + + return lif_execute_fmt(opts, NULL, "/sbin/ip addr %s %s dev %s", cmd, addrbuf, lifname); +} + static bool handle_pre_up(const struct lif_execute_opts *opts, struct lif_interface *iface, const char *lifname) { @@ -68,17 +79,12 @@ handle_up(const struct lif_execute_opts *opts, struct lif_interface *iface, cons { struct lif_dict_entry *entry = iter->data; - if (strcmp(entry->key, "address")) - continue; + if (!strcmp(entry->key, "address")) + { + struct lif_address *addr = entry->data; - struct lif_address *addr = entry->data; - char addrbuf[4096]; - - if (!lif_address_unparse(addr, addrbuf, sizeof addrbuf, true)) - return false; - - if (!lif_execute_fmt(opts, NULL, "/sbin/ip addr add %s dev %s", addrbuf, lifname)) - return false; + handle_address(opts, addr, "add", lifname); + } } if (iface->is_dhcp) @@ -103,17 +109,12 @@ handle_down(const struct lif_execute_opts *opts, struct lif_interface *iface, co { struct lif_dict_entry *entry = iter->data; - if (strcmp(entry->key, "address")) - continue; + if (!strcmp(entry->key, "address")) + { + struct lif_address *addr = entry->data; - struct lif_address *addr = entry->data; - char addrbuf[4096]; - - if (!lif_address_unparse(addr, addrbuf, sizeof addrbuf, true)) - return false; - - if (!lif_execute_fmt(opts, NULL, "/sbin/ip addr del %s dev %s", addrbuf, lifname)) - return false; + handle_address(opts, addr, "del", lifname); + } } if (iface->is_dhcp)