chiark / gitweb /
networkd: add network_get_by_name
authorTom Gundersen <teg@jklm.no>
Sun, 8 Feb 2015 12:29:35 +0000 (13:29 +0100)
committerTom Gundersen <teg@jklm.no>
Sun, 8 Feb 2015 19:15:37 +0000 (20:15 +0100)
src/network/networkd-manager.c
src/network/networkd-network.c
src/network/networkd.h

index 8dfe437..e2c8a23 100644 (file)
@@ -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);
index 4589162..504419c 100644 (file)
@@ -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) {
index cd54e9b..d144c11 100644 (file)
@@ -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);