-
Notifications
You must be signed in to change notification settings - Fork 5
/
tcp-send-test.c
94 lines (82 loc) · 2.46 KB
/
tcp-send-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
#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"
int do_connect(struct sockaddr_in *dst) {
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;
}
/* for faster debugging
// specifies the maximum amount of time in milliseconds
// that transmitted data may remain unacknowledged before
// TCP will forcibly close the corresponding connection
// and return ETIMEDOUT to the application.
if (setsockopt(s, IPPROTO_TCP, TCP_USER_TIMEOUT,
(int []){ 10 * 1000 }, sizeof(int)) == -1) {
perror("setsockopt TCP_USER_TIMEOUT");
return -1;
}
*/
return s;
}
#define NCONNECTIONS 130
int main(int argc, char *argv[]){
int tcpsessions[NCONNECTIONS] = {0};
char buf;
struct sockaddr_in dst_addr = {
.sin_family = AF_INET,
.sin_port = htons(31415),
.sin_addr = inet_addr("130.225.254.111"),
};
msg("[+] Trying to establish connections: ");
for (int i = 0; i < NCONNECTIONS; i++) {
printf("%d ", i);
fflush(stdout);
tcpsessions[i] = do_connect(&dst_addr);
if (tcpsessions[i] == -1) {
return EXIT_FAILURE;
}
}
printf("\n");
msg("[+] All connections established\n");
for (int i = 0; i < NCONNECTIONS; i++) {
// This is not exact - we'll gradually drift
// by the time it takes to write() and read()
sleep(60);
if (write(tcpsessions[i], "A", 1) != 1) {
if (errno != ETIMEDOUT) {
perror("write");
return EXIT_FAILURE;
}
msg("[-] Connection %d is dead (write)\n", i);
close(tcpsessions[i]);
continue;
}
if (read(tcpsessions[i], &buf, 1) != 1) {
if (errno != ETIMEDOUT) {
perror("read");
return EXIT_FAILURE;
}
msg("[-] Connection %d is dead (read)\n", i);
close(tcpsessions[i]);
continue;
}
msg("[+] Connection %d worked\n", i);
close(tcpsessions[i]);
}
return 0;
}