#include <stdlib.h>
#include <string.h>
+#include <systemd/sd-device.h>
#include <systemd/sd-netlink.h>
#include "daemon.h"
NW_LINK_DESTROYED,
} state;
+ // Device
+ struct sd_device* device;
+
// Stats
struct rtnl_link_stats64 stats64;
uint8_t operstate;
};
+static int nw_link_setup_device(nw_link* link) {
+ int r;
+
+ // Fetch sd-device
+ r = sd_device_new_from_ifindex(&link->device, link->ifindex);
+ if (r < 0) {
+ ERROR("Could not fetch sd-device for link %d: %s\n", link->ifindex, strerror(-r));
+ return r;
+ }
+
+ return 0;
+}
+
+static void nw_link_free(nw_link* link) {
+ DEBUG("Freeing link (ifindex = %d)\n", link->ifindex);
+
+ if (link->device)
+ sd_device_unref(link->device);
+ if (link->daemon)
+ nw_daemon_unref(link->daemon);
+}
+
int nw_link_create(nw_link** link, nw_daemon* daemon, int ifindex) {
+ int r;
+
// Allocate a new object
nw_link* l = calloc(1, sizeof(*l));
if (!l)
- return 1;
+ return -errno;
// Store a reference to the daemon
l->daemon = nw_daemon_ref(daemon);
DEBUG("New link allocated (ifindex = %d)\n", l->ifindex);
- *link = l;
+ r = nw_link_setup_device(l);
+ if (r < 0)
+ goto ERROR;
+ *link = l;
return 0;
-}
-
-static void nw_link_free(nw_link* link) {
- DEBUG("Freeing link (ifindex = %d)\n", link->ifindex);
- if (link->daemon)
- nw_daemon_unref(link->daemon);
+ERROR:
+ nw_link_free(l);
+ return r;
}
nw_link* nw_link_ref(nw_link* link) {
return link->ifname;
}
+static int nw_link_get_sd_device(nw_link* link, struct sd_device** device) {
+ int r;
+
+ // Fetch sd-device
+ r = sd_device_new_from_ifindex(device, link->ifindex);
+ if (r < 0) {
+ ERROR("Could not fetch sd-device for link %d: %s\n", link->ifindex, strerror(-r));
+ return r;
+ }
+
+ return 0;
+}
+
// Stats
const struct rtnl_link_stats64* nw_link_get_stats64(nw_link* link) {
// JSON
+static int nw_link_device_to_json(nw_link* link, struct json_object* o) {
+ const char* driver = NULL;
+ const char* vendor = NULL;
+ const char* model = NULL;
+ int r;
+
+ // Fetch driver
+ r = sd_device_get_driver(link->device, &driver);
+ if (r < 0 && r != -ENOENT)
+ return r;
+
+ // Add driver
+ if (driver) {
+ r = json_object_add_string(o, "Driver", driver);
+ if (r < 0)
+ return r;
+ }
+
+ // Fetch vendor
+ r = sd_device_get_property_value(link->device, "ID_VENDOR_FROM_DATABASE", &vendor);
+ if (r < 0) {
+ r = sd_device_get_property_value(link->device, "ID_VENDOR", &vendor);
+ if (r < 0 && r != -ENOENT)
+ return r;
+ }
+
+ // Add vendor
+ if (vendor) {
+ r = json_object_add_string(o, "Vendor", vendor);
+ if (r < 0)
+ return r;
+ }
+
+ // Fetch model
+ r = sd_device_get_property_value(link->device, "ID_MODEL_FROM_DATABASE", &model);
+ if (r < 0) {
+ r = sd_device_get_property_value(link->device, "ID_MODEL", &model);
+ if (r < 0 && r != -ENOENT)
+ return r;
+ }
+
+ // Add model
+ if (model) {
+ r = json_object_add_string(o, "Model", model);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
int nw_link_to_json(nw_link* link, struct json_object* o) {
int r;
if (r < 0)
return r;
+ // Add sd-device stuff
+ r = nw_link_device_to_json(link, o);
+ if (r < 0)
+ return r;
+
return 0;
}