-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.c
More file actions
207 lines (163 loc) · 4.61 KB
/
interface.c
File metadata and controls
207 lines (163 loc) · 4.61 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (C) 2024 TDT AG <development@tdt.de>
*
* This is free software, licensed under the GNU General Public License v2.
* See https://www.gnu.org/licenses/gpl-2.0.txt for more information.
*
*/
#include <libubus.h>
#include "interface.h"
#include "logging.h"
#include "ping.h"
#include "target.h"
static LIST_HEAD(interfaces);
static struct uci_context *uci_ctx;
static struct blob_buf b;
static struct interface * interface_alloc(const char *name, struct blob_attr *config)
{
struct interface *iface;
iface = (struct interface *) calloc(1, sizeof(struct interface));
iface->name = strdup(name);
INIT_LIST_HEAD(&iface->targets);
return iface;
}
static struct interface * interface_get(const char *name)
{
struct interface *iface;
list_for_each_entry(iface, &interfaces, list)
{
if (strcmp(iface->name, name) == 0)
return iface;
}
return NULL;
}
static void interface_add(struct uci_section *s)
{
struct blob_attr *cur, *tb[ARRAY_SIZE(interface_attrs)], *c;
struct interface *iface;
bool enabled = false;
int i;
blob_buf_init(&b, 0);
uci_to_blob(&b, s, &interface_attr_list);
blobmsg_parse(interface_attrs, ARRAY_SIZE(interface_attrs), tb,
blob_data(b.head), blob_len(b.head));
if ((c = tb[IFACE_ATTR_ENABLED]))
enabled = blobmsg_get_bool(c);
if (enabled == false)
return;
iface = interface_alloc(s->e.name, b.head);
if (!iface) {
LOGGING_INFO("%s: unable to allocate interface '%s'",
__func__, s->e.name);
return;
}
list_add(&iface->list, &interfaces);
if ((c = tb[IFACE_ATTR_INTERVAL]))
iface->interval = blobmsg_get_u32(c);
else
iface->interval = 30;
blobmsg_for_each_attr(cur, tb[IFACE_ATTR_TRACKING_IPS], i)
{
if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
continue;
/* @todo get physical interface form ubus call network.interface.<iface> */
target_add(iface, blobmsg_get_string(cur), "eth0");
}
}
static void interface_del(struct interface *iface)
{
targets_del(iface);
LOGGING_INFO("%s: del interface '%s'", __func__, iface->name);
if (iface->name)
free(iface->name);
list_del(&iface->list);
free(iface);
}
void interface_ping_start(struct interface *iface)
{
struct target *target, *tmp;
int ret;
list_for_each_entry_safe(target, tmp, &iface->targets, list) {
LOGGING_INFO("%s: init ping for target '%s'", __func__, target->remote);
ret = ping_init(target);
if (ret < 0) {
LOGGING_INFO("%s: target already initilized '%s'", __func__, target->remote);
continue;
}
LOGGING_INFO("%s: start ping for target '%s'", __func__, target->remote);
ping_send(target);
}
}
void interface_ping_stop(struct interface *iface)
{
struct target *target, *tmp;
list_for_each_entry_safe(target, tmp, &iface->targets, list) {
LOGGING_INFO("%s: stop ping for target '%s'", __func__, target->remote);
ping_stop(target);
}
}
void interface_notify(const char* interface, const char* action)
{
struct interface *iface = interface_get(interface);
if (iface == NULL) {
LOGGING_INFO("interface '%s' not tracked by mwan4", interface);
return;
}
if (strcmp(action, "ifup") == 0) {
LOGGING_INFO("interface '%s' event UP", interface);
interface_ping_start(iface);
}
if (strcmp(action, "ifdown") == 0) {
LOGGING_INFO("interface '%s' event DOWN", interface);
interface_ping_stop(iface);
}
}
void interface_get_status(const char *interface, struct blob_buf *b)
{
struct target *target, *tmp;
struct interface *iface;
iface = interface_get(interface);
if (iface == NULL) {
LOGGING_INFO("interface '%s' not tracked by mwan4", interface);
return;
}
list_for_each_entry_safe(target, tmp, &iface->targets, list) {
LOGGING_INFO("%s: get ubus ping info for target '%s'", __func__,
target->remote);
void *k = blobmsg_open_table(b, target->remote);
blobmsg_add_u32(b, "rtt_last", target->rtt_last);
blobmsg_add_u32(b, "rtt_max", target->rtt_max);
blobmsg_add_u32(b, "cnt_sent", target->cnt_sent);
blobmsg_close_table(b, k);
}
}
void interfaces_done(void)
{
struct interface *iface, *tmp;
list_for_each_entry_safe(iface, tmp, &interfaces, list) {
interface_del(iface);
}
}
int interfaces_init(void)
{
struct uci_package* p;
struct uci_element* e;
uci_ctx = uci_alloc_context();
if (uci_ctx == NULL) {
LOGGING_CRIT("%s: unable to initialize uci context", __func__);
return -1;
}
if (uci_load(uci_ctx, "mwan4", &p)) {
LOGGING_CRIT("%s: unable to load mwan4 uci config", __func__);
uci_free_context(uci_ctx);
return -1;
}
uci_foreach_element(&p->sections, e) {
struct uci_section *s = uci_to_section(e);
if (!strcmp(s->type, "interface"))
interface_add(s);
}
uci_unload(uci_ctx, p);
uci_free_context(uci_ctx);
return 0;
}