forked from yarrick/pingfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
268 lines (233 loc) · 6.48 KB
/
net.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (c) 2013-2015 Erik Ekman <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "net.h"
#include "icmp.h"
#include "chunk.h"
#include <netinet/ip_icmp.h>
#include <netinet/icmp6.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <sys/param.h>
#include <pthread.h>
static int sockv4;
static int sockv6;
struct pkt_stats {
long long unsigned int packets;
long long unsigned int bytes;
};
static struct net_data {
pthread_t responder;
pthread_t status;
pthread_mutex_t stats_mutex;
struct pkt_stats tx;
struct pkt_stats rx;
} netdata;
static void inc_stats(struct pkt_stats *stats, int packetsize)
{
pthread_mutex_lock(&netdata.stats_mutex);
stats->packets++;
stats->bytes += packetsize + ICMP_HDRLEN;
pthread_mutex_unlock(&netdata.stats_mutex);
}
static void net_inc_tx(int packetsize)
{
inc_stats(&netdata.tx, packetsize);
}
void net_inc_rx(int packetsize)
{
inc_stats(&netdata.rx, packetsize);
}
int net_open_sockets()
{
/* 1MB receive buffer per socket */
int rcvbuf = 1024*1024;
// v4 socket will return full IP header
sockv4 = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockv4 < 0) {
perror("Failed to open IPv4 socket");
} else {
int res = setsockopt(sockv4, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
if (res < 0) {
perror("Failed to set receive buffer size on IPv4 socket");
}
}
// v6 socket will just give ICMPv6 data, no IP header
sockv6 = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
if (sockv6 >= 0) {
struct icmp6_filter filter;
int res = setsockopt(sockv6, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
if (res < 0) {
perror("Failed to set receive buffer size on IPv6 socket");
}
ICMP6_FILTER_SETBLOCKALL(&filter);
ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
res = setsockopt(sockv6, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter));
if (res < 0) {
perror("Failed to set ICMP filters on IPv6 socket");
}
} else {
perror("Failed to open IPv6 socket");
}
if (sockv4 < 0 && sockv6 < 0)
return 1;
return 0;
}
void net_send(struct host *host, uint16_t id, uint16_t seqno, const uint8_t *data, size_t len)
{
int sock;
struct icmp_packet pkt;
memcpy(&pkt.peer, &host->sockaddr, host->sockaddr_len);
pkt.peer_len = host->sockaddr_len;
pkt.type = ICMP_REQUEST;
pkt.id = id;
pkt.seqno = seqno;
pkt.payload = (uint8_t *) data;
pkt.payload_len = len;
if (ICMP_ADDRFAMILY(&pkt) == AF_INET) {
sock = sockv4;
} else {
sock = sockv6;
}
if (sock >= 0) {
net_inc_tx(pkt.payload_len);
if (!icmp_send(sock, &pkt))
perror("Failed sending data packet");
}
}
static void handle_recv(int sock, net_recv_fn_t recv_fn, void *recv_data)
{
struct icmp_packet mypkt;
mypkt.peer_len = sizeof(struct sockaddr_storage);
uint8_t buf[BUFSIZ];
int len;
len = recvfrom(sock, buf, sizeof(buf), 0,
(struct sockaddr *) &mypkt.peer, &mypkt.peer_len);
if (len > 0 && icmp_parse(&mypkt, buf, len) == 0) {
if (mypkt.type == ICMP_REPLY) {
recv_fn(recv_data, &mypkt.peer, mypkt.peer_len, mypkt.id,
mypkt.seqno, &mypkt.payload, mypkt.payload_len);
}
free(mypkt.payload);
}
}
int net_recv(struct timeval *tv, net_recv_fn_t recv_fn, void *recv_data)
{
int maxfd;
fd_set fds;
int i;
FD_ZERO(&fds);
if (sockv4 >= 0) FD_SET(sockv4, &fds);
if (sockv6 >= 0) FD_SET(sockv6, &fds);
maxfd = MAX(sockv4, sockv6);
i = select(maxfd+1, &fds, NULL, NULL, tv);
if ((sockv4 >= 0) && FD_ISSET(sockv4, &fds))
handle_recv(sockv4, recv_fn, recv_data);
if ((sockv6 >= 0) && FD_ISSET(sockv6, &fds))
handle_recv(sockv6, recv_fn, recv_data);
return i;
}
static void *responder_thread(void *arg)
{
for (;;) {
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
net_recv(&tv, chunk_reply, NULL);
}
return NULL;
}
static void get_stats(struct pkt_stats *rx, struct pkt_stats *tx)
{
pthread_mutex_lock(&netdata.stats_mutex);
memcpy(rx, &netdata.rx, sizeof(netdata.rx));
memcpy(tx, &netdata.tx, sizeof(netdata.tx));
pthread_mutex_unlock(&netdata.stats_mutex);
}
static float format_bytes(unsigned long long bytes, const char **suffix)
{
const char *suffixes[] = {
"B",
"kB",
"MB",
"GB",
NULL,
};
int i = 0;
float bps = (float) bytes;
while (suffixes[i + 1] && bps > 1300) {
bps /= 1000.0f;
i++;
}
*suffix = suffixes[i];
return bps;
}
static void diff_stats(struct pkt_stats *new, struct pkt_stats *old)
{
struct pkt_stats diff;
float bytes;
const char *byte_suffix;
diff.packets = new->packets - old->packets;
diff.bytes = new->bytes - old->bytes;
memcpy(old, new, sizeof(*old));
bytes = format_bytes(diff.bytes, &byte_suffix);
printf("%6llu pkt/s, %7.01f %2s/s", diff.packets, bytes, byte_suffix);
}
static void *status_thread(void *arg)
{
const struct timespec status_sleep = {
.tv_sec = 1,
.tv_nsec = 0,
};
static struct pkt_stats prev_rx, prev_tx;
get_stats(&prev_rx, &prev_tx);
nanosleep(&status_sleep, NULL);
for (;;) {
struct pkt_stats rx, tx;
get_stats(&rx, &tx);
printf("\rICMP in: ");
diff_stats(&rx, &prev_rx);
printf(" ICMP out: ");
diff_stats(&tx, &prev_tx);
fflush(stdout);
nanosleep(&status_sleep, NULL);
}
return NULL;
}
void net_start()
{
if (pthread_mutex_init(&netdata.stats_mutex, NULL)) {
perror("Fatal, failed to create a mutex");
exit(EXIT_FAILURE);
}
pthread_create(&netdata.responder, NULL, responder_thread, NULL);
pthread_create(&netdata.status, NULL, status_thread, NULL);
}
void net_stop()
{
pthread_cancel(netdata.responder);
pthread_join(netdata.responder, NULL);
pthread_cancel(netdata.status);
pthread_join(netdata.status, NULL);
pthread_mutex_lock(&netdata.stats_mutex);
printf("\n\nTotal network resources consumed:\n"
"in: %10llu packets, %10llu bytes\n"
"out: %10llu packets, %10llu bytes\n"
" (bytes counted above IP level)\n",
netdata.rx.packets, netdata.rx.bytes,
netdata.tx.packets, netdata.tx.bytes
);
pthread_mutex_unlock(&netdata.stats_mutex);
}