summaryrefslogtreecommitdiffstats
path: root/plugins-root/check_icmp.d/check_icmp_helpers.c
blob: 58e1358128d031d9ca5add5ae839b5d133ea5946 (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
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
#include "./config.h"
#include "states.h"
#include <math.h>
#include <netinet/in.h>
#include "./check_icmp_helpers.h"
#include "../../plugins/netutils.h"

// timeout as a global variable to make it available to the timeout handler
unsigned int timeout = DEFAULT_TIMEOUT;

check_icmp_config check_icmp_config_init() {
	check_icmp_config tmp = {
		.order_mode = false,
		.mos_mode = false,
		.rta_mode = false,
		.pl_mode = false,
		.jitter_mode = false,
		.score_mode = false,

		.min_hosts_alive = -1,
		.crit = {.pl = DEFAULT_CRIT_PL,
				 .rta = DEFAULT_CRIT_RTA,
				 .jitter = 50.0,
				 .mos = 3.0,
				 .score = 70.0},
		.warn = {.pl = DEFAULT_WARN_PL,
				 .rta = DEFAULT_WARN_RTA,
				 .jitter = 40.0,
				 .mos = 3.5,
				 .score = 80.0},

		.ttl = DEFAULT_TTL,
		.icmp_data_size = DEFAULT_PING_DATA_SIZE,
		.icmp_pkt_size = DEFAULT_PING_DATA_SIZE + ICMP_MINLEN,
		.pkt_interval = DEFAULT_PKT_INTERVAL,
		.target_interval = 0,
		.number_of_packets = DEFAULT_NUMBER_OF_PACKETS,
		.source_ip = NULL,

		.sender_id = {},

		.mode = MODE_RTA,

		.number_of_targets = 0,
		.targets = NULL,

		.number_of_hosts = 0,
		.hosts = NULL,
	};
	return tmp;
}

ping_target ping_target_init() {
	ping_target tmp = {
		.rtmin = INFINITY,

		.jitter_min = INFINITY,

		.rta_status = STATE_OK,
		.jitter_status = STATE_OK,
		.mos_status = STATE_OK,
		.score_status = STATE_OK,
		.pl_status = STATE_OK,
		.order_status = STATE_OK,
	};

	return tmp;
}

check_icmp_state check_icmp_state_init() {
	check_icmp_state tmp = {.icmp_sent = 0, .icmp_lost = 0, .icmp_recv = 0, .targets_down = 0};

	return tmp;
}

ping_target_create_wrapper ping_target_create(char *name, struct sockaddr_storage *address) {
	struct sockaddr_in *sin;
	struct sockaddr_in6 *sin6;
	if (address_family == AF_INET) {
		sin = (struct sockaddr_in *)address;
	} else {
		sin6 = (struct sockaddr_in6 *)address;
	}

	ping_target_create_wrapper result = {
		.errorcode = OK,
	};

	/* disregard obviously stupid addresses
	 * (I didn't find an ipv6 equivalent to INADDR_NONE) */
	if (((address_family == AF_INET &&
		  (sin->sin_addr.s_addr == INADDR_NONE || sin->sin_addr.s_addr == INADDR_ANY))) ||
		(address_family == AF_INET6 && (sin6->sin6_addr.s6_addr == in6addr_any.s6_addr))) {
		result.errorcode = ERROR;
		return result;
	}

	// TODO: Maybe add the following back in as a sanity check for the config
	// /* no point in adding two identical IP's, so don't. ;) */
	// struct sockaddr_in *host_sin;
	// struct sockaddr_in6 *host_sin6;
	// struct rta_host *host = host_list;

	// while (host) {
	// 	host_sin = (struct sockaddr_in *)&host->saddr_in;
	// 	host_sin6 = (struct sockaddr_in6 *)&host->saddr_in;

	// 	if ((address_family == AF_INET && host_sin->sin_addr.s_addr == sin->sin_addr.s_addr) ||
	// 		(address_family == AF_INET6 &&
	// 		 host_sin6->sin6_addr.s6_addr == sin6->sin6_addr.s6_addr)) {
	// 		if (debug) {
	// 			printf("Identical IP already exists. Not adding %s\n", name);
	// 		}
	// 		return -1;
	// 	}
	// 	host = host->next;
	// }

	/* add the fresh ip */
	ping_target host = ping_target_init();

	/* set the values. use calling name for output */
	host.name = strdup(name);

	/* fill out the sockaddr_storage struct */
	if (address_family == AF_INET) {
		struct sockaddr_in *host_sin = (struct sockaddr_in *)&host.saddr_in;
		host_sin->sin_family = AF_INET;
		host_sin->sin_addr.s_addr = sin->sin_addr.s_addr;
	} else {
		struct sockaddr_in6 *host_sin6 = (struct sockaddr_in6 *)&host.saddr_in;
		host_sin6->sin6_family = AF_INET6;
		memcpy(host_sin6->sin6_addr.s6_addr, sin6->sin6_addr.s6_addr,
			   sizeof host_sin6->sin6_addr.s6_addr);
	}

	result.host = host;

	return result;
}

check_icmp_target_container check_icmp_target_container_init() {
	check_icmp_target_container tmp = {
		.name = NULL,
		.number_of_targets = 0,
		.target_list = NULL,
	};
	return tmp;
}

unsigned int ping_target_list_append(ping_target *list, ping_target *elem) {
	if (elem == NULL || list == NULL) {
		return 0;
	}

	while (list->next != NULL) {
		list = list->next;
	}

	list->next = elem;

	unsigned int result = 1;

	while (elem->next != NULL) {
		result++;
		elem = elem->next;
	}

	return result;
}

void check_icmp_timeout_handler(int signal, siginfo_t *info, void *ucontext) {
	// Ignore unused arguments
	(void)info;
	(void)ucontext;
	mp_subcheck timeout_sc = mp_subcheck_init();
	timeout_sc = mp_set_subcheck_state(timeout_sc, socket_timeout_state);

	if (signal == SIGALRM) {
		xasprintf(&timeout_sc.output, _("timeout after %d seconds\n"), timeout);
	} else {
		xasprintf(&timeout_sc.output, _("timeout after %d seconds\n"), timeout);
	}

	mp_check overall = mp_check_init();
	mp_add_subcheck_to_check(&overall, timeout_sc);

	mp_exit(overall);
}