#include "device-common.h" #include "util.h" #include "vector.h" #include #include #include #include static device_type_t device_type_interface; typedef struct _device_interface { device_t device; device_common_t common; } device_interface_t; static void interface_free(device_t *dev) { device_interface_t *iface = container_of(dev, device_interface_t, device); VECTOR_FREE(iface->common.addrs); free(NODE_NAME(dev)); free(iface); } static device_t * interface_process_config(const char *name, struct json_object *config) { device_interface_t *iface = calloc(1, sizeof(*iface)); if (!iface) return NULL; device_t *dev = &iface->device; dev->type = &device_type_interface; NODE_NAME(dev) = strdup(name); if (!NODE_NAME(dev)) { free(iface); return NULL; } if (!device_common_process_config(&iface->common, config)) goto err; return dev; err: interface_free(dev); return NULL; } static void interface_init(device_t *dev) { device_interface_t *iface = container_of(dev, device_interface_t, device); unsigned ifindex = if_nametoindex(NODE_NAME(dev)); if (!ifindex) return; device_common_init(&iface->common, ifindex); } static void interface_update(device_t *dev) { } static void interface_release(device_t *dev) { device_interface_t *iface = container_of(dev, device_interface_t, device); unsigned ifindex = if_nametoindex(NODE_NAME(dev)); if (!ifindex) return; device_common_release(&iface->common, ifindex); } static device_type_t device_type_interface = { .process_config = interface_process_config, .free = interface_free, .init = interface_init, .update = interface_update, .release = interface_release, }; __attribute__((constructor)) static void interface_constructor(void) { register_device_type("interface", &device_type_interface); }