]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/networkd/daemon-bus.c
ports: Implement listing ports over DBus
[people/ms/network.git] / src / networkd / daemon-bus.c
index 93a041105ce848e484a5e23754d9cac7f7a62cf6..4de8c0fb35a6c2455f9262bf50bccdaf76257561 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <stdlib.h>
+
 #include <systemd/sd-bus.h>
 
 #include "bus.h"
 #include "daemon.h"
+#include "logging.h"
 #include "port-bus.h"
 #include "zone-bus.h"
+#include "zones.h"
 
 static int nw_daemon_bus_reload(sd_bus_message* m, void* data, sd_bus_error* error) {
        nw_daemon* daemon = (nw_daemon*)data;
@@ -35,8 +39,110 @@ static int nw_daemon_bus_reload(sd_bus_message* m, void* data, sd_bus_error* err
        return sd_bus_reply_method_return(m, NULL);
 }
 
+static int __nw_daemon_bus_list_ports(nw_daemon* daemon, nw_port* port, void* data) {
+       sd_bus_message* reply = (sd_bus_message*)data;
+       int r;
+
+       // Fetch port name
+       const char* name = nw_port_name(port);
+
+       // Fetch bus path
+       char* path = nw_port_bus_path(port);
+
+       r = sd_bus_message_append(reply, "(so)", name, path);
+
+       free(path);
+
+       return r;
+}
+
+static int nw_daemon_bus_list_ports(sd_bus_message* m, void* data, sd_bus_error* error) {
+       nw_daemon* daemon = (nw_daemon*)data;
+       sd_bus_message* reply = NULL;
+       int r;
+
+       // Form a reply message
+       r = sd_bus_message_new_method_return(m, &reply);
+       if (r < 0)
+               goto ERROR;
+
+       r = sd_bus_message_open_container(reply, 'a', "(so)");
+       if (r < 0)
+               goto ERROR;
+
+       r = nw_daemon_ports_walk(daemon, __nw_daemon_bus_list_ports, reply);
+       if (r < 0)
+               goto ERROR;
+
+       r = sd_bus_message_close_container(reply);
+       if (r < 0)
+               goto ERROR;
+
+       // Send the reply
+       r = sd_bus_send(NULL, reply, NULL);
+
+ERROR:
+       if (reply)
+               sd_bus_message_unref(reply);
+
+       return r;
+}
+
+static int __nw_daemon_bus_list_zones(nw_daemon* daemon, nw_zone* zone, void* data) {
+       sd_bus_message* reply = (sd_bus_message*)data;
+       int r;
+
+       // Fetch zone name
+       const char* name = nw_zone_name(zone);
+
+       // Fetch bus path
+       char* path = nw_zone_bus_path(zone);
+
+       r = sd_bus_message_append(reply, "(so)", name, path);
+
+       free(path);
+
+       return r;
+}
+
+static int nw_daemon_bus_list_zones(sd_bus_message* m, void* data, sd_bus_error* error) {
+       nw_daemon* daemon = (nw_daemon*)data;
+       sd_bus_message* reply = NULL;
+       int r;
+
+       // Form a reply message
+       r = sd_bus_message_new_method_return(m, &reply);
+       if (r < 0)
+               goto ERROR;
+
+       r = sd_bus_message_open_container(reply, 'a', "(so)");
+       if (r < 0)
+               goto ERROR;
+
+       r = nw_daemon_zones_walk(daemon, __nw_daemon_bus_list_zones, reply);
+       if (r < 0)
+               goto ERROR;
+
+       r = sd_bus_message_close_container(reply);
+       if (r < 0)
+               goto ERROR;
+
+       // Send the reply
+       r = sd_bus_send(NULL, reply, NULL);
+
+ERROR:
+       if (reply)
+               sd_bus_message_unref(reply);
+
+       return r;
+}
+
 static const sd_bus_vtable daemon_vtable[] = {
        SD_BUS_VTABLE_START(0),
+       SD_BUS_METHOD_WITH_ARGS("ListPorts", SD_BUS_NO_ARGS, SD_BUS_RESULT("a(so)", links),
+               nw_daemon_bus_list_ports, SD_BUS_VTABLE_UNPRIVILEGED),
+       SD_BUS_METHOD_WITH_ARGS("ListZones", SD_BUS_NO_ARGS, SD_BUS_RESULT("a(so)", links),
+               nw_daemon_bus_list_zones, SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD("Reload", SD_BUS_NO_ARGS, SD_BUS_NO_RESULT,
                nw_daemon_bus_reload, SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_VTABLE_END,