-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.c
206 lines (171 loc) · 5.14 KB
/
ping.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
/*
* Copyright (C) 2024 TDT AG <[email protected]>
*
* 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 <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include "icmp.h"
#include "logging.h"
#include "ping.h"
#include "target.h"
static void ping_uloop_fd_close(struct uloop_fd* ufd)
{
if (ufd != NULL && ufd->fd > 0) {
uloop_fd_delete(ufd);
close(ufd->fd);
ufd->fd = 0;
}
}
/* Don't want to pull-in librt and libpthread just for a monotonic clock */
int timespec_diff_ms(struct timespec *t1, struct timespec *t2)
{
int t1ms, t2ms;
t2ms = ((uint64_t)t2->tv_sec) * 1000 + ((uint64_t)t2->tv_nsec) / 1000000;
t1ms = ((uint64_t)t1->tv_sec) * 1000 + ((uint64_t)t1->tv_nsec) / 1000000;
return t2ms - t1ms;
}
static void ping_fd_handler(struct uloop_fd* fd, unsigned int events)
{
struct target *target = container_of(fd, struct target, ping);
struct timespec time_recv;
int received_fd = icmp_echo_receive(fd->fd);
if (received_fd < 0) {
LOGGING_INFO("%s: received pong failed from '%s' for '%s' (%s)",
__func__, target->remote, target->iface->name,
target->device);
return;
}
LOGGING_INFO("%s: received pong from '%s' for '%s' (%s)", __func__,
target->remote, target->iface->name, target->device);
target->cnt_succ++;
/* calculate round trip time */
clock_gettime(CLOCK_MONOTONIC, &time_recv);
target->rtt_last = timespec_diff_ms(&target->time_sent, &time_recv);
LOGGING_INFO("%s: rtt last from '%s' for '%s' (%s) was '%d'", __func__,
target->remote, target->iface->name, target->device,
target->rtt_last);
if (target->rtt_last > target->rtt_max) {
target->rtt_max = target->rtt_last;
}
/* online just confirmed: move timeout for offline to later
* and give the next reply an extra window of two times the last RTT
*/
uloop_timeout_set(&target->timeout_offline,
target->timeout * 1000 + target->rtt_last * 2);
}
/* uloop timeout callback when it's time to send a ping */
static void ping_send_timeout_cb(struct uloop_timeout* t)
{
struct target *target = container_of(t, struct target, timeout_send);
ping_send(target);
/* re-schedule next sending */
uloop_timeout_set(t, target->interval * 1000);
}
static int ping_resolve(struct target *target)
{
struct sockaddr_in *sa;
struct addrinfo hints;
struct addrinfo *addr;
int ret;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
ret = getaddrinfo(target->remote, NULL, &hints, &addr);
if (ret < 0) {
LOGGING_ERR("%s: failed to resolve '%s'", __func__,
target->remote);
return -1;
}
if (addr == NULL) {
LOGGING_ERR("%s: addr info is NULL for '%s'", __func__,
target->remote);
return -1;
}
/* use only first address */
sa = (struct sockaddr_in*)addr->ai_addr;
LOGGING_INFO("%s: resolved '%s' to '%s'", __func__,
target->remote, inet_ntoa((struct in_addr)sa->sin_addr));
target->ip = sa->sin_addr.s_addr;
freeaddrinfo(addr);
return 0;
}
int ping_init(struct target *target)
{
int ret;
if (target->ping.fd != 0) {
LOGGING_ERR("%s: ping on '%s' already initialized", __func__,
target->remote);
return -1;
}
target->state = TARGET_STATE_UNKNOWN;
target->cnt_sent = 0;
target->cnt_succ = 0;
target->rtt_last = 0;
target->rtt_max = 0;
target->timeout = 60;
LOGGING_INFO("%s: init icmp ping on '%s' (%s)", __func__,
target->remote, target->device);
ret = icmp_init(target->device);
if (ret < 0) {
LOGGING_ERR("%s: unable to initialize icmp on '%s' (%s)",
__func__, target->remote, target->device);
return -2;
}
/* add socket handler to uloop */
target->ping.fd = ret;
target->ping.cb = ping_fd_handler;
ret = uloop_fd_add(&target->ping, ULOOP_READ);
if (ret < 0) {
LOGGING_ERR("%s: could not add uloop fd %d for '%s' (%s)",
__func__, target->ping.fd, target->remote, target->device);
return -3;
}
/* regular sending of ping (start first in 1 sec) */
target->timeout_send.cb = ping_send_timeout_cb;
ret = uloop_timeout_set(&target->timeout_send, 1000);
if (ret < 0) {
LOGGING_ERR("%s: could not add uloop send timeout for '%s' (%s)",
__func__, target->remote, target->device);
return -4;
}
return 0;
}
int ping_send(struct target *target)
{
int ret;
/* resolve at least every 10th time */
if (target->ip == 0 || target->cnt_sent % 10 == 0) {
if (ping_resolve(target)) {
LOGGING_ERR("%s: unable to reslove target '%s'",
__func__, target->remote);
return -1;
}
}
if (target->ping.fd <= 0) {
LOGGING_ERR("%s: ping not init on '%s'", target->remote);
return -2;
}
ret = icmp_echo_send(target->ping.fd, target->ip, target->cnt_sent);
if (ret < 0) {
LOGGING_ERR("%s: could not send ping on '%s'", __func__,
target->remote);
return -3;
}
target->cnt_sent++;
clock_gettime(CLOCK_MONOTONIC, &target->time_sent);
return 0;
}
void ping_stop(struct target *target)
{
uloop_timeout_cancel(&target->timeout_offline);
uloop_timeout_cancel(&target->timeout_send);
ping_uloop_fd_close(&target->ping);
}