]> git.ipfire.org Git - network.git/commitdiff
networkd: ports: Export Ethernet address over dbus
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2023 13:54:33 +0000 (13:54 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2023 13:54:33 +0000 (13:54 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/port-bus.c
src/networkd/port.c
src/networkd/port.h

index 8c327b9317c85dc77333c46535922283f61f643e..1a486554e56ea6913f64e4a1413df475cabc6dbc 100644 (file)
@@ -19,7 +19,9 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <stdlib.h>
 
+#include "address.h"
 #include "bus.h"
 #include "daemon.h"
 #include "logging.h"
@@ -76,8 +78,40 @@ static int nw_port_object_find(sd_bus* bus, const char* path, const char* interf
        return 1;
 }
 
+static int nw_port_bus_get_address(sd_bus* bus, const char* path, const char* interface,
+               const char* property, sd_bus_message* reply, void* data, sd_bus_error* error) {
+       struct nw_port* port = (struct nw_port*)data;
+       int r;
+
+       // Fetch the address
+       const nw_address_t* address = nw_port_get_address(port);
+
+       // Format the address as a string
+       char* s = nw_address_to_string(address);
+       if (!s) {
+               // XXX How to handle any errors?
+               return 0;
+       }
+
+       // Append the address to the return value
+       r = sd_bus_message_append(reply, "s", s);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       if (s)
+               free(s);
+
+       return r;
+}
+
 static const sd_bus_vtable port_vtable[] = {
        SD_BUS_VTABLE_START(0),
+
+       // Address
+       SD_BUS_PROPERTY("Address", "s", nw_port_bus_get_address,
+               0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+
        SD_BUS_VTABLE_END
 };
 
index d1e3f7ff9294204a844804c35b02b7ab8d89ec3b..e59e760626583c9fa280ee8723f1fa327592f064 100644 (file)
@@ -221,3 +221,7 @@ char* nw_port_bus_path(struct nw_port* port) {
 
        return p;
 }
+
+const nw_address_t* nw_port_get_address(struct nw_port* port) {
+       return &port->address;
+}
index e3655cff8e266f8cfbd153dd6e9a4518499d6d85..caf1433b47a7251884171c8caa7e8bbc5be2c81c 100644 (file)
@@ -30,6 +30,8 @@ typedef enum nw_port_type {
 
 struct nw_port;
 
+#include "address.h"
+
 int nw_port_create(struct nw_port** port, const char* name);
 
 struct nw_port* nw_port_ref(struct nw_port* port);
@@ -39,4 +41,6 @@ const char* nw_port_name(struct nw_port* port);
 
 char* nw_port_bus_path(struct nw_port* port);
 
+const nw_address_t* nw_port_get_address(struct nw_port* port);
+
 #endif /* NETWORKD_PORT_H */