]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: Return zone when it is being accessed by its path
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 2 Feb 2023 01:07:51 +0000 (01:07 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 2 Feb 2023 01:07:51 +0000 (01:07 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/bus.c
src/networkd/bus.h
src/networkd/daemon.c
src/networkd/daemon.h
src/networkd/zone-bus.c

index 26876fa888162db0eef871fa06e224e5e216280c..cab2ae6a34a796badb8bfc76e1142ca2ec909c77 100644 (file)
@@ -162,6 +162,7 @@ int nw_bus_register_implementation(sd_bus* bus,
        DEBUG("Registering bus object implementation for path=%s iface=%s\n",
                impl->path, impl->interface);
 
+       // Register vtables
        for (const sd_bus_vtable** vtable = impl->vtables; vtable && *vtable; vtable++) {
                r = sd_bus_add_object_vtable(bus, NULL, impl->path, impl->interface, *vtable, data);
                if (r < 0) {
@@ -171,6 +172,17 @@ int nw_bus_register_implementation(sd_bus* bus,
                }
        }
 
+       // Register fallback vtables
+       for (const struct nw_bus_vtable_pair* p = impl->fallback_vtables; p && p->vtable; p++) {
+               r = sd_bus_add_fallback_vtable(bus, NULL, impl->path, impl->interface,
+                               p->vtable, p->object_find, data);
+               if (r < 0) {
+                       ERROR("Could not register bus path %s with interface %s: %m\n",
+                               impl->path, impl->interface);
+                       return 1;
+               }
+       }
+
        // Register the node enumerator
        if (impl->node_enumerator) {
                r = sd_bus_add_node_enumerator(bus, NULL, impl->path, impl->node_enumerator, data);
index 6232ebb1f119bcfcd0a97e27890ec77ab1e7b2e9..42ab2d312ffad2f79b1b996f6b4b9afe60b886fc 100644 (file)
 
 int nw_bus_connect(sd_bus* bus, sd_event* loop, struct nw_daemon* daemon);
 
+struct nw_bus_vtable_pair {
+       const sd_bus_vtable* vtable;
+       sd_bus_object_find_t object_find;
+};
+
 struct nw_bus_implementation {
        const char* path;
        const char* interface;
-
        const sd_bus_vtable** vtables;
+       const struct nw_bus_vtable_pair* fallback_vtables;
        sd_bus_node_enumerator_t node_enumerator;
        const struct nw_bus_implementation** children;
 };
 
+#define BUS_FALLBACK_VTABLES(...) ((const struct nw_bus_vtable_pair[]) { __VA_ARGS__, {} })
 #define BUS_IMPLEMENTATIONS(...) ((const struct nw_bus_implementation* []) { __VA_ARGS__, NULL })
 #define BUS_VTABLES(...) ((const sd_bus_vtable* []){ __VA_ARGS__, NULL })
 
index 588aa4e1e186bad7a839838fecbaf638eb5b0f6e..5b5cbb44447acb7d71064e6957dfc486d8d5a05f 100644 (file)
@@ -236,6 +236,17 @@ int nw_daemon_reload(struct nw_daemon* daemon) {
        return 0;
 }
 
+/*
+       Zones
+*/
+
 struct nw_zones* nw_daemon_zones(struct nw_daemon* daemon) {
        return nw_zones_ref(daemon->zones);
 }
+
+struct nw_zone* nw_daemon_get_zone_by_name(struct nw_daemon* daemon, const char* name) {
+       if (!daemon->zones)
+               return NULL;
+
+       return nw_zones_get_by_name(daemon->zones, name);
+}
index bc69795eef5af1da63d4fdc6858e086c43beeead..891c297ab1bce6c815b5e990dd378e0cdd6a937d 100644 (file)
@@ -34,6 +34,10 @@ int nw_daemon_run(struct nw_daemon* daemon);
 
 int nw_daemon_reload(struct nw_daemon* daemon);
 
+/*
+       Zones
+*/
 struct nw_zones* nw_daemon_zones(struct nw_daemon* daemon);
+struct nw_zone* nw_daemon_get_zone_by_name(struct nw_daemon* daemon, const char* name);
 
 #endif /* NETWORKD_DAEMON_H */
index ebd878988e45981bf52e8886a7673dcbc18ff777..aafbfaf75e37bdea689eb71c00f462e7ffdbabcd 100644 (file)
@@ -48,9 +48,41 @@ ERROR:
        return r;
 }
 
+static int nw_zone_object_find(sd_bus* bus, const char* path, const char* interface,
+               void* data, void** found, sd_bus_error* error) {
+       char* name = NULL;
+       int r;
+
+       // Fetch a reference to the daemon
+       struct nw_daemon* daemon = (struct nw_daemon*)data;
+
+       // Decode the path of the requested object
+       r = sd_bus_path_decode(path, "/org/ipfire/network1/zone", &name);
+       if (r <= 0)
+               return 0;
+
+       // Find the zone
+       struct nw_zone* zone = nw_daemon_get_zone_by_name(daemon, name);
+       if (!zone)
+               return 0;
+
+       // Match!
+       *found = zone;
+
+       nw_zone_unref(zone);
+
+       return 1;
+}
+
+static const sd_bus_vtable zone_vtable[] = {
+       SD_BUS_VTABLE_START(0),
+
+       SD_BUS_VTABLE_END
+};
+
 const struct nw_bus_implementation zone_bus_impl = {
        "/org/ipfire/network1/zone",
        "org.ipfire.network1.Zone",
-       //.fallback_vtables = BUS_FALLBACK_VTABLES({zone_bus_vtable, nw_zone_object_find}),
+       .fallback_vtables = BUS_FALLBACK_VTABLES({zone_vtable, nw_zone_object_find}),
        .node_enumerator = nw_zone_node_enumerator,
 };