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
|
/*
Posluša na TCP vratih 6969, prejme eno povezavo, vsako sekundo nanjo izpiše LF in piše statistiko, dobljeno iz jedrnega modula tcp_times.
*/
#include <stdint.h>
#include "tcp_times.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <signal.h>
int samomor = 0;
void handler (int sig __attribute__((unused))) {
samomor = 1;
}
int main (void) {
if (signal(SIGINT, handler) == SIG_ERR) {
perror("signal");
return 1;
}
if (signal(SIGHUP, handler) == SIG_ERR) {
perror("signal");
return 1;
}
if (signal(SIGTERM, handler) == SIG_ERR) {
perror("signal");
return 1;
}
int tcp_socket = socket(AF_INET6, SOCK_STREAM, 0);
if (tcp_socket == -1) {
perror("socket");
return 1;
}
struct sockaddr_in6 sa6 = {
.sin6_family = AF_INET6,
.sin6_port = htons(6969),
.sin6_addr = IN6ADDR_ANY_INIT,
};
if (bind(tcp_socket, (struct sockaddr *) &sa6, sizeof sa6) == -1) {
perror("bind");
return 1;
}
if (listen(tcp_socket, 1 /* only one client is handled*/) == -1) {
perror("listen");
goto die;
}
int flow = accept(tcp_socket, NULL, NULL);
if (flow == -1) {
perror("accept");
goto die;
}
int tcp_times = open("/proc/tcp_times", O_RDWR);
struct tcp_times tt = {
.fd = flow,
};
char buf = '\n';
while (true) {
if (ioctl(tcp_times, 0, &tt) == -1) {
perror("ioctl");
break;
}
printf(TCP_TIMES_PRINTF_FORMAT "\n", TCP_TIMES_PRINTF_VARIABLES(tt.));
if (send(flow, &buf, 1, MSG_NOSIGNAL) == -1) {
perror("write");
break;
}
if (samomor)
break;
sleep(1);
}
die:
close(tcp_times);
close(flow);
close(tcp_socket);
return 1;
}
|