From 3d5ca9050e009533dd63f893131a8b4f955f72f4 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Tue, 4 Aug 2020 11:46:10 -0600 Subject: [PATCH] interface: add lif_interface_collection_inherit() (ref #2) --- libifupdown/interface.c | 30 ++++++++++++++++++++++++++++++ libifupdown/interface.h | 1 + 2 files changed, 31 insertions(+) diff --git a/libifupdown/interface.c b/libifupdown/interface.c index 87f6c97..8eab120 100644 --- a/libifupdown/interface.c +++ b/libifupdown/interface.c @@ -214,3 +214,33 @@ lif_interface_collection_delete(struct lif_dict *collection, struct lif_interfac lif_dict_delete_entry(collection, entry); } + +bool +lif_interface_collection_inherit(struct lif_interface *interface, struct lif_dict *collection, const char *ifname) +{ + struct lif_interface *parent = lif_interface_collection_find(collection, ifname); + + if (parent == NULL) + return false; + + /* copy the variables */ + struct lif_node *iter; + LIF_DICT_FOREACH(iter, &parent->vars) + { + struct lif_dict_entry *entry = iter->data; + + if (!strcmp(entry->key, "address")) + { + struct lif_address *addr = calloc(1, sizeof *addr); + struct lif_address *other_addr = entry->data; + + memcpy(addr, other_addr, sizeof *addr); + + lif_dict_add(&interface->vars, entry->key, addr); + } + else + lif_dict_add(&interface->vars, entry->key, strdup(entry->data)); + } + + return true; +} diff --git a/libifupdown/interface.h b/libifupdown/interface.h index 9ef9387..045f848 100644 --- a/libifupdown/interface.h +++ b/libifupdown/interface.h @@ -73,6 +73,7 @@ extern void lif_interface_collection_init(struct lif_dict *collection); extern void lif_interface_collection_fini(struct lif_dict *collection); extern struct lif_interface *lif_interface_collection_find(struct lif_dict *collection, const char *ifname); extern struct lif_interface *lif_interface_collection_upsert(struct lif_dict *collection, struct lif_interface *interface); +extern bool lif_interface_collection_inherit(struct lif_interface *interface, struct lif_dict *collection, const char *ifname); extern void lif_interface_collection_delete(struct lif_dict *collection, struct lif_interface *interface); #endif