From d566048705bb24b12d46fa4ed7cf9352286d8086 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 6 Jun 2023 10:24:45 +0000 Subject: [PATCH] networkctl: Implement "dump" command for ports which shows the JSON This is just for debugging purposes. Signed-off-by: Michael Tremer --- src/networkctl/port.c | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/networkctl/port.c b/src/networkctl/port.c index 3a886ff8..67bc0037 100644 --- a/src/networkctl/port.c +++ b/src/networkctl/port.c @@ -18,8 +18,11 @@ # # #############################################################################*/ +#include + #include +#include "../networkd/string.h" #include "command.h" #include "port.h" @@ -77,6 +80,53 @@ ERROR: return r; } +// Dump + +static int networkctl_port_dump(sd_bus* bus, int argc, char* argv[]) { + sd_bus_message* reply = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + char path[PATH_MAX]; + const char* text = NULL; + int r; + + if (argc < 1) { + fprintf(stderr, "Port required\n"); + return -EINVAL; + } + + // Make port path + r = nw_string_format(path, "/org/ipfire/network1/port/%s", argv[0]); + if (r < 0) + goto ERROR; + + // Call Describe + r = sd_bus_call_method(bus, "org.ipfire.network1", path, + "org.ipfire.network1.Port", "Describe", &error, &reply, ""); + if (r < 0) { + fprintf(stderr, "Describe() call failed: %m\n"); + goto ERROR; + } + + // Read the text + r = sd_bus_message_read(reply, "s", &text); + if (r < 0) { + fprintf(stderr, "Could not parse bus message: %s\n", strerror(-r)); + goto ERROR; + } + + // Print the text + if (text) + printf("%s\n", text); + +ERROR: + if (reply) + sd_bus_message_unref(reply); + + return r; +} + +// List + static int __networkctl_port_list(sd_bus* bus, const char* path, const char* name, void* data) { printf("%s\n", name); @@ -89,6 +139,7 @@ static int networkctl_port_list(sd_bus* bus, int argc, char* argv[]) { int networkctl_port(sd_bus* bus, int argc, char* argv[]) { static const struct command commands[] = { + { "dump", 0, networkctl_port_dump }, { "list", 0, networkctl_port_list }, { NULL }, }; -- 2.47.2