diff options
author | Tom Gundersen <teg@jklm.no> | 2015-02-08 13:29:35 +0100 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2015-02-08 20:15:37 +0100 |
commit | dbffab87f1504abc9f189dd253111693c99fbd9a (patch) | |
tree | 6d9d7f01ef17379dd1eb3584fa074c4bfddfac86 | |
parent | 23cb977ac382931a0b5c1385cdbb7fe5c4c92b1a (diff) |
networkd: add network_get_by_name
-rw-r--r-- | src/network/networkd-manager.c | 2 | ||||
-rw-r--r-- | src/network/networkd-network.c | 48 | ||||
-rw-r--r-- | src/network/networkd.h | 3 |
3 files changed, 51 insertions, 2 deletions
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 8dfe4377de..e2c8a23e0f 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -481,6 +481,8 @@ void manager_free(Manager *m) { while ((network = m->networks)) network_free(network); + hashmap_free(m->networks_by_name); + while ((netdev = hashmap_first(m->netdevs))) netdev_unref(netdev); hashmap_free(m->netdevs); diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index 4589162944..504419cdb0 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -34,6 +34,7 @@ static int network_load_one(Manager *manager, const char *filename) { _cleanup_network_free_ Network *network = NULL; _cleanup_fclose_ FILE *file = NULL; + char *d; Route *route; Address *address; int r; @@ -84,6 +85,18 @@ static int network_load_one(Manager *manager, const char *filename) { if (!network->filename) return log_oom(); + network->name = strdup(basename(filename)); + if (!network->name) + return log_oom(); + + d = strrchr(network->name, '.'); + if (!d) + return -EINVAL; + + assert(streq(d, ".network")); + + *d = '\0'; + network->dhcp = ADDRESS_FAMILY_NO; network->dhcp_ntp = true; network->dhcp_dns = true; @@ -115,6 +128,14 @@ static int network_load_one(Manager *manager, const char *filename) { LIST_PREPEND(networks, manager->networks, network); + r = hashmap_ensure_allocated(&manager->networks_by_name, &string_hash_ops); + if (r < 0) + return r; + + r = hashmap_put(manager->networks_by_name, network->name, network); + if (r < 0) + return r; + LIST_FOREACH(routes, route, network->static_routes) { if (!route->family) { log_warning("Route section without Gateway field configured in %s. " @@ -210,8 +231,15 @@ void network_free(Network *network) { hashmap_free(network->routes_by_section); hashmap_free(network->fdb_entries_by_section); - if (network->manager && network->manager->networks) - LIST_REMOVE(networks, network->manager->networks, network); + if (network->manager) { + if (network->manager->networks) + LIST_REMOVE(networks, network->manager->networks, network); + + if (network->manager->networks_by_name) + hashmap_remove(network->manager->networks_by_name, network->name); + } + + free(network->name); condition_free_list(network->match_host); condition_free_list(network->match_virt); @@ -221,6 +249,22 @@ void network_free(Network *network) { free(network); } +int network_get_by_name(Manager *manager, const char *name, Network **ret) { + Network *network; + + assert(manager); + assert(name); + assert(ret); + + network = hashmap_get(manager->networks_by_name, name); + if (!network) + return -ENOENT; + + *ret = network; + + return 0; +} + int network_get(Manager *manager, struct udev_device *device, const char *ifname, const struct ether_addr *address, Network **ret) { diff --git a/src/network/networkd.h b/src/network/networkd.h index cd54e9bb06..d144c11edc 100644 --- a/src/network/networkd.h +++ b/src/network/networkd.h @@ -97,6 +97,7 @@ struct Network { Manager *manager; char *filename; + char *name; struct ether_addr *match_mac; char *match_path; @@ -224,6 +225,7 @@ struct Manager { Hashmap *links; Hashmap *netdevs; + Hashmap *networks_by_name; LIST_HEAD(Network, networks); LIST_HEAD(AddressPool, address_pools); @@ -265,6 +267,7 @@ void network_free(Network *network); DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free); #define _cleanup_network_free_ _cleanup_(network_freep) +int network_get_by_name(Manager *manager, const char *name, Network **ret); int network_get(Manager *manager, struct udev_device *device, const char *ifname, const struct ether_addr *mac, Network **ret); |