-
Notifications
You must be signed in to change notification settings - Fork 5
/
tcp-keepalive-test.c
152 lines (136 loc) · 4.61 KB
/
tcp-keepalive-test.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include "util.h"
#ifdef __APPLE__
#define TCP_KEEPIDLE TCP_KEEPALIVE
#endif
int do_connect(struct sockaddr_in *dst, int keepalive_time) {
int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == -1) {
perror("socket");
return -1;
}
if (connect(s, (struct sockaddr *) dst, sizeof(struct sockaddr_in)) == -1) {
perror("connect");
return -1;
}
// Enable TCP keepalive
if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
(int []){1}, sizeof(int)) == -1) {
perror("setsockopt SO_KEEPALIVE");
return -1;
}
// The time (in seconds) the connection needs to remain idle before
// TCP starts sending keepalive probes, if the socket option
// SO_KEEPALIVE has been set on this socket.
if (setsockopt(s, IPPROTO_TCP, TCP_KEEPIDLE,
&keepalive_time, sizeof keepalive_time) == -1) {
perror("setsockopt SO_KEEPIDLE");
return -1;
}
/* For faster debugging
// The maximum number of keepalive probes TCP should send before
// dropping the connection.
if (setsockopt(s, IPPROTO_TCP, TCP_KEEPCNT,
(int []){3}, sizeof(int)) == -1) {
perror("setsockopt TCP_KEEPCNT");
return -1;
}
// The time (in seconds) between individual keepalive probes.
if (setsockopt(s, IPPROTO_TCP, TCP_KEEPINTVL,
(int []){5}, sizeof(int)) == -1) {
perror("setsockopt TCP_KEEPINTVL");
return -1;
}
*/
return s;
}
#define NCONNECTIONS 130
int main(int argc, char *argv[]){
int tcpsessions[NCONNECTIONS] = {0};
int keepalivetime[NCONNECTIONS] = {0};
char buf;
int ret;
struct sockaddr_in dst_addr = {
.sin_family = AF_INET,
.sin_port = htons(31415),
.sin_addr = inet_addr("130.225.254.111"),
};
printf("[+] Trying to establish connections: ");
for (int i = 0; i < NCONNECTIONS; i++) {
// The first connection will have a keepalivetime of 1 min,
// the second one 2 min, etc.
keepalivetime[i] = (i + 1) * 60;
printf("%d (%ds), ", i, keepalivetime[i]);
fflush(stdout);
tcpsessions[i] = do_connect(&dst_addr, keepalivetime[i]);
if (tcpsessions[i] == -1) {
return EXIT_FAILURE;
}
}
printf("\n");
msg("[+] All connections established\n");
// select() loop
for (;;) {
fd_set rfds;
FD_ZERO(&rfds);
int openconnections = 0;
int maxfd = -1;
for (int i = 0; i < NCONNECTIONS; i++) {
if (tcpsessions[i] == -1)
continue;
FD_SET(tcpsessions[i], &rfds);
maxfd = tcpsessions[i] > maxfd ? tcpsessions[i] : maxfd;
openconnections++;
}
if (openconnections == 0) {
msg("[+] No more alive connections left\n");
return EXIT_SUCCESS;
}
msg("[+] Waiting for a connection to timeout.\n");
msg("[+] Open connections: %d\n", openconnections);
ret = select(maxfd + 1, &rfds, NULL, NULL, NULL);
if (ret == -1) {
if (errno == EINTR)
continue;
perror("select");
return EXIT_FAILURE;
} else if (ret) {
for (int i = 0; i < NCONNECTIONS; i++) {
if (tcpsessions[i] != -1 && FD_ISSET(tcpsessions[i], &rfds)) {
ssize_t readret = read(tcpsessions[i], &buf, 1);
if (readret != -1 || errno != ETIMEDOUT) {
msg("[-] shouldn't happen:\n"
"readret: %ld\n"
"errno: %d (%m)\n"
"i: %d\n"
"tcpsessions[i]: %d\n",
readret, errno, i, tcpsessions[i]
);
close(tcpsessions[i]);
tcpsessions[i] = -1;
continue;
}
msg("[+] TCP connection died: ");
printf("Connection %d, keepalivetime: %dm %ds\n",
i, keepalivetime[i]/60, keepalivetime[i]%60);
fflush(stdout);
close(tcpsessions[i]);
tcpsessions[i] = -1;
}
}
} else {
msg("[+] No data\n");
}
}
return 0;
}