src/networkd/daemon-bus.h \
src/networkd/devmon.c \
src/networkd/devmon.h \
+ src/networkd/json.h \
src/networkd/link.c \
src/networkd/link.h \
src/networkd/links.c \
return 0;
}
+static int nw_port_bonding_to_json(nw_port* port, struct json_object* o) {
+ int r;
+
+ // Add mode
+ r = json_object_add_string(o, "BondingMode",
+ nw_port_bonding_mode_to_string(port->bonding.mode));
+ if (r < 0)
+ goto ERROR;
+
+ERROR:
+ return r;
+}
+
const nw_port_info_t nw_port_info_bonding = {
.kind = "bond",
// Link
.create_link = nw_port_bonding_create_link,
+
+ // JSON
+ .to_json = nw_port_bonding_to_json,
},
};
#include <errno.h>
#include <stdlib.h>
+#include <systemd/sd-bus.h>
#include "address.h"
#include "bus.h"
#include "daemon.h"
+#include "json.h"
#include "logging.h"
#include "port.h"
#include "port-bus.h"
return r;
}
+static int nw_port_bus_describe(sd_bus_message* message, void* data,
+ sd_bus_error* error) {
+ sd_bus_message* reply = NULL;
+ struct json_object* json = NULL;
+ char* text = NULL;
+ int r;
+
+ nw_port* port = (nw_port*)data;
+
+ // Export all data to JSON
+ r = nw_port_to_json(port, &json);
+ if (r < 0)
+ goto ERROR;
+
+ // Convert JSON to string
+ r = json_to_string(json, &text, NULL);
+ if (r < 0)
+ goto ERROR;
+
+ // Create a reply message
+ r = sd_bus_message_new_method_return(message, &reply);
+ if (r < 0)
+ goto ERROR;
+
+ r = sd_bus_message_append(reply, "s", text);
+ if (r < 0)
+ goto ERROR;
+
+ // Send the reply
+ r = sd_bus_send(NULL, reply, NULL);
+
+ERROR:
+ if (reply)
+ sd_bus_message_unref(reply);
+ if (text)
+ free(text);
+
+ return r;
+}
+
static const sd_bus_vtable port_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("Address", "s", nw_port_bus_get_address,
0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+ // Operations
+ SD_BUS_METHOD_WITH_ARGS("Describe", SD_BUS_NO_ARGS, SD_BUS_RESULT("s", json),
+ nw_port_bus_describe, SD_BUS_VTABLE_UNPRIVILEGED),
+
SD_BUS_VTABLE_END
};
#include "config.h"
#include "daemon.h"
+#include "json.h"
#include "logging.h"
#include "port.h"
#include "port-vlan.h"
return 0;
}
+static int nw_port_vlan_to_json(nw_port* port, struct json_object* o) {
+ nw_port* parent = NULL;
+ int r;
+
+ // Add the VLAN ID
+ r = json_object_add_int64(o, "VLANId", port->vlan.id);
+ if (r < 0)
+ goto ERROR;
+
+ // Fetch the parent
+ parent = nw_port_get_parent_port(port);
+ if (parent) {
+ r = json_object_add_string(o, "VLANParentPort", nw_port_name(parent));
+ if (r < 0)
+ goto ERROR;
+ }
+
+ERROR:
+ if (parent)
+ nw_port_unref(parent);
+
+ return r;
+}
+
const nw_port_info_t nw_port_info_vlan = {
.kind = "vlan",
// Link
.create_link = nw_port_vlan_create_link,
+
+ // JSON
+ .to_json = nw_port_vlan_to_json,
},
};
#include "address.h"
#include "config.h"
+#include "json.h"
#include "link.h"
#include "logging.h"
#include "port.h"
errno = ENOTSUP;
return -errno;
}
+
+/*
+ JSON
+*/
+int nw_port_to_json(nw_port* port, struct json_object** object) {
+ char* address = NULL;
+ int r;
+
+ // Create a new JSON object
+ struct json_object* o = json_object_new_object();
+ if (!o) {
+ r = -errno;
+ goto ERROR;
+ }
+
+ // Add name
+ r = json_object_add_string(o, "Name", port->name);
+ if (r < 0)
+ goto ERROR;
+
+ // Add Type
+ r = json_object_add_string(o, "Type", NW_PORT_INFO(port)->kind);
+ if (r < 0)
+ goto ERROR;
+
+ // Add address
+ address = nw_address_to_string(&port->address);
+ if (address) {
+ r = json_object_add_string(o, "Address", address);
+ if (r < 0)
+ goto ERROR;
+ }
+
+ // Call custom stuff
+ if (NW_PORT_OPS(port)->to_json) {
+ r = NW_PORT_OPS(port)->to_json(port, o);
+ if (r < 0)
+ goto ERROR;
+ }
+
+ // Success
+ r = 0;
+
+ // Return a reference to the created object
+ *object = json_object_ref(o);
+
+ERROR:
+ if (address)
+ free(address);
+ if (o)
+ json_object_unref(o);
+
+ return r;
+}
#ifndef NETWORKD_PORT_H
#define NETWORKD_PORT_H
+#include <json.h>
+
#include <systemd/sd-netlink.h>
#ifndef IF_NAMESIZE
#include "address.h"
#include "config.h"
#include "daemon.h"
+#include "json.h"
#include "port-bonding.h"
#include "port-vlan.h"
// Link
int (*create_link)(nw_port* port, sd_netlink_message* message);
int (*destroy_link)(nw_port* port);
+
+ // JSON
+ int (*to_json)(nw_port* port, struct json_object* object);
} ops;
};
int __nw_port_update_stats(nw_daemon* daemon, nw_port* port, void* data);
int nw_port_update_stats(nw_port* port);
+// JSON
+int nw_port_to_json(nw_port* port, struct json_object** object);
+
#endif /* NETWORKD_PORT_H */