forked from br101/pingcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
234 lines (203 loc) · 5.29 KB
/
main.c
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* pingcheck - Check connectivity of interfaces in OpenWRT
*
* Copyright (C) 2015 Bruno Randolf <br1@einfach.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "main.h"
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* main list of interfaces */
static struct ping_intf intf[MAX_NUM_INTERFACES];
/* timeout for panic scripts */
static struct uloop_timeout timeout_panic;
void state_change(enum online_state state_new, struct ping_intf* pi)
{
if (pi->state == state_new) { /* no change */
return;
}
pi->state = state_new;
LOG_INF("Interface '%s' changed to %s", pi->name,
get_status_str(pi->state));
enum online_state global_state = get_global_status();
if (global_state == OFFLINE) {
if (pi->conf_panic_timeout > 0) {
uloop_timeout_set(&timeout_panic,
pi->conf_panic_timeout * 60 * 1000);
}
} else {
uloop_timeout_cancel(&timeout_panic);
}
scripts_run(pi, state_new);
}
const char* get_status_str(enum online_state state)
{
switch (state) {
case UNKNOWN:
return "UNKNOWN";
case DOWN:
return "DOWN";
case UP_WITHOUT_DEFAULT_ROUTE:
return "UP_WITHOUT_DEFAULT_ROUTE";
case UP:
return "UP";
case OFFLINE:
return "OFFLINE";
case ONLINE:
return "ONLINE";
default:
return "INVALID";
}
}
enum online_state get_global_status(void)
{
for (int i = 0; i < MAX_NUM_INTERFACES && intf[i].name[0]; i++) {
if (intf[i].state == ONLINE) {
return ONLINE;
}
}
return OFFLINE;
}
int get_online_interface_names(const char** dest, int destLen)
{
int j = 0;
for (int i = 0; i < MAX_NUM_INTERFACES && intf[i].name[0]; i++) {
if (intf[i].state == ONLINE && j < destLen) {
dest[j++] = intf[i].name;
}
}
return j;
}
int get_all_interface_names(const char** dest, int destLen)
{
int i = 0;
for (; i < MAX_NUM_INTERFACES && intf[i].name[0] && i < destLen; i++) {
dest[i] = intf[i].name;
}
return i;
}
/* called from ubus interface event */
void notify_interface(const char* interface, const char* action)
{
struct ping_intf* pi = get_interface(interface);
if (pi == NULL) {
return;
}
if (strcmp(action, "ifup") == 0) {
LOG_INF("Interface '%s' event UP", interface);
ping_init(pi);
ping_send(pi);
} else if (strcmp(action, "ifdown") == 0) {
LOG_INF("Interface '%s' event DOWN", interface);
ping_stop(pi);
state_change(DOWN, pi);
}
}
struct ping_intf* get_interface_by_fd(int fd)
{
struct ping_intf* pi = NULL;
/* find interface in our list */
for (int i = 0; i < MAX_NUM_INTERFACES && intf[i].name[0]; i++) {
if (intf[i].ufd.fd == fd) {
pi = &intf[i];
break;
}
}
return pi;
}
/* also called from ubus server_status */
struct ping_intf* get_interface(const char* interface)
{
struct ping_intf* pi = NULL;
/* find interface in our list */
for (int i = 0; i < MAX_NUM_INTERFACES && intf[i].name[0]; i++) {
if (strncmp(intf[i].name, interface, MAX_IFNAME_LEN) == 0) {
pi = &intf[i];
break;
}
}
return pi;
}
/* reset counters for all (pass NULL) or one interface */
void reset_counters(const char* interface)
{
for (int i = 0; i < MAX_NUM_INTERFACES && intf[i].name[0]; i++) {
if (interface == NULL
|| strncmp(intf[i].name, interface, MAX_IFNAME_LEN) == 0) {
intf[i].cnt_sent = 0;
intf[i].cnt_succ = 0;
intf[i].last_rtt = 0;
intf[i].max_rtt = 0;
}
}
}
/* uloop timeout callback when offline for too long */
static void uto_panic_cb(__attribute__((unused)) struct uloop_timeout* t)
{
scripts_run_panic();
}
int main(__attribute__((unused)) int argc, __attribute__((unused)) char** argv)
{
int ret;
log_open("pingcheck");
ret = uloop_init();
if (ret < 0) {
LOG_CRIT("Could not initialize uloop");
return EXIT_FAILURE;
}
ret = uci_config_pingcheck(intf, MAX_NUM_INTERFACES);
if (!ret) {
LOG_CRIT("Could not read UCI config");
goto exit;
}
scripts_init();
ret = ubus_init();
if (!ret) {
goto exit;
}
/* listen for ubus network interface events */
ret = ubus_listen_network_events();
if (!ret) {
LOG_CRIT("Could not listen to interface events");
goto exit;
}
ubus_register_server();
/* start ping on all available interfaces */
for (int i = 0; i < MAX_NUM_INTERFACES && intf[i].name[0]; i++) {
ping_init(&intf[i]);
}
/* initialize panic handler */
timeout_panic.cb = uto_panic_cb;
if (intf[0].conf_panic_timeout > 0) {
uloop_timeout_set(&timeout_panic,
intf[0].conf_panic_timeout * 60 * 1000);
}
/* main loop */
uloop_run();
/* print statistics and cleanup */
printf("\n");
for (int i = 0; i < MAX_NUM_INTERFACES && intf[i].name[0]; i++) {
printf("%s:\t%-8s %3.0f%% (%d/%d on %s)\n", intf[i].name,
get_status_str(intf[i].state),
(float)intf[i].cnt_succ * 100 / intf[i].cnt_sent,
intf[i].cnt_succ, intf[i].cnt_sent, intf[i].device);
ping_stop(&intf[i]);
}
exit:
scripts_finish();
uloop_done();
ubus_finish();
log_close();
return ret ? EXIT_SUCCESS : EXIT_FAILURE;
}