forked from br101/pingcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.h
115 lines (99 loc) · 3.15 KB
/
main.h
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
/* pingcheck - Check connectivity of interfaces in OpenWRT
*
* Copyright (C) 2015 Bruno Randolf <[email protected]>
*
* 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 <libubox/runqueue.h>
#include <libubox/uloop.h>
#include <stdbool.h>
#define MAX_IFNAME_LEN 256
#define MAX_HOSTNAME_LEN 256
#define MAX_NUM_INTERFACES 8
#define SCRIPTS_TIMEOUT 10 /* 10 sec */
#define UBUS_TIMEOUT 3000 /* 3 sec */
enum online_state {
UNKNOWN,
DOWN,
UP_WITHOUT_DEFAULT_ROUTE,
UP,
OFFLINE,
ONLINE
};
enum protocol { ICMP, TCP };
struct scripts_proc {
struct runqueue_process proc;
struct ping_intf* intf;
enum online_state state;
};
struct ping_intf {
/* public state */
char name[MAX_IFNAME_LEN];
char device[MAX_IFNAME_LEN];
enum online_state state;
unsigned int cnt_sent;
unsigned int cnt_succ;
unsigned int last_rtt; /* in ms */
unsigned int max_rtt; /* in ms */
/* config items */
int conf_interval;
int conf_timeout;
char conf_hostname[MAX_HOSTNAME_LEN];
int conf_host; /* resolved IP */
enum protocol conf_proto;
int conf_tcp_port;
int conf_panic_timeout; /* minutes */
/* internal state for ping */
struct uloop_fd ufd;
struct uloop_timeout timeout_offline;
struct uloop_timeout timeout_send;
struct timespec time_sent;
/* internal state for scripts */
struct scripts_proc scripts_on;
struct scripts_proc scripts_off;
};
// utils.c
long timespec_diff_ms(struct timespec start, struct timespec end);
// icmp.c
int icmp_init(const char* ifname);
bool icmp_echo_send(int fd, int dst, int cnt);
int icmp_echo_receive(int fd);
// tcp.c
int tcp_connect(const char* ifname, int dst, int port);
bool tcp_check_connect(int fd);
// ping.c
bool ping_init(struct ping_intf* pi);
bool ping_send(struct ping_intf* pi);
void ping_stop(struct ping_intf* pi);
// ubus.c
bool ubus_init(void);
bool ubus_listen_network_events(void);
int ubus_interface_get_status(const char* name, char* device,
size_t device_len);
bool ubus_register_server(void);
void ubus_finish(void);
// uci.c
int uci_config_pingcheck(struct ping_intf* intf, int len);
// scripts.c
void scripts_init(void);
void scripts_run(struct ping_intf* pi, enum online_state state_new);
void scripts_run_panic(void);
void scripts_finish(void);
// main.c
void notify_interface(const char* interface, const char* action);
struct ping_intf* get_interface_by_fd(int fd);
struct ping_intf* get_interface(const char* interface);
const char* get_status_str(enum online_state state);
enum online_state get_global_status();
int get_online_interface_names(const char** dest, int destLen);
int get_all_interface_names(const char** dest, int destLen);
void state_change(enum online_state state_new, struct ping_intf* pi);
void reset_counters(const char* interface);