From: Michael Tremer Date: Sat, 11 Feb 2023 13:54:33 +0000 (+0000) Subject: networkd: ports: Export Ethernet address over dbus X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4a383286ff94b4bc6efdde95c26070b68b8942e3;p=network.git networkd: ports: Export Ethernet address over dbus Signed-off-by: Michael Tremer --- diff --git a/src/networkd/port-bus.c b/src/networkd/port-bus.c index 8c327b93..1a486554 100644 --- a/src/networkd/port-bus.c +++ b/src/networkd/port-bus.c @@ -19,7 +19,9 @@ #############################################################################*/ #include +#include +#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 }; diff --git a/src/networkd/port.c b/src/networkd/port.c index d1e3f7ff..e59e7606 100644 --- a/src/networkd/port.c +++ b/src/networkd/port.c @@ -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; +} diff --git a/src/networkd/port.h b/src/networkd/port.h index e3655cff..caf1433b 100644 --- a/src/networkd/port.h +++ b/src/networkd/port.h @@ -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 */