summaryrefslogtreecommitdiffstats
path: root/src/device-interface.c
blob: 1add2b2eccfd8b352621c25d49c01e462d427511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "device-common.h"
#include "util.h"
#include "vector.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include <net/if.h>

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);
}