forked from aguinet/mcats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcats.c
367 lines (334 loc) · 8.4 KB
/
mcats.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* This file is part of mcats.
*
* Copyright 2012 Adrien Guinet <[email protected]>
*
* mcats is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mcats is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mcats. If not, see <http://www.gnu.org/licenses/>. 2
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <assert.h>
#include <limits.h>
#include <fcntl.h>
#define BUFSIZE 1024
static size_t g_wait_ms = 0;
// set = ~set
void fd_set_not(fd_set* set)
{
for (size_t i = 0; i < FD_SETSIZE/__NFDBITS; i++) {
__FDS_BITS(set)[i] = ~__FDS_BITS(set)[i];
}
}
// res = a & b
void fd_set_and(fd_set* res, fd_set const* a, fd_set const* b)
{
for (size_t i = 0; i < FD_SETSIZE/__NFDBITS; i++) {
__FDS_BITS(res)[i] = __FDS_BITS(a)[i] & __FDS_BITS(b)[i];
}
}
bool fd_is_empty(fd_set const* set)
{
for (size_t i = 0; i < FD_SETSIZE/__NFDBITS; i++) {
if (__FDS_BITS(set)[i] != 0) {
return false;
}
}
return true;
}
int* create_sockets(size_t n, int* phighest_fd, fd_set* ret_fd_set)
{
int* sockets = (int*) malloc(n*sizeof(int));
if (sockets == NULL) {
return NULL;
}
FD_ZERO(ret_fd_set);
int max_fd = 0;
for (size_t i = 0; i < n; i++) {
int s = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (s == -1) {
perror("socket");
free(sockets);
return NULL;
}
if (s > max_fd) {
max_fd = s;
}
FD_SET(s, ret_fd_set);
sockets[i] = s;
}
*phighest_fd = max_fd;
return sockets;
}
void write_stdin_all_sockets(int* sockets, size_t n, fd_set* all_read_set)
{
char buf[BUFSIZE];
size_t sum = 0;
while (true) {
int r = read(STDIN_FILENO, buf, BUFSIZE);
if (r < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
break;
}
perror("read stdin");
exit(1);
}
if (r == 0) {
// stdin has been closed. Do not "select" it anymore.
fprintf(stderr, "stdin: pipeline closed.\n");
FD_CLR(0, all_read_set);
break;
}
// Write buf into sockets
//if (g_wait_ms == 0) {
for (size_t i = 0; i < n; i++) {
if (sockets[i] >= 0) {
if (write(sockets[i], buf, r) < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
perror("write socket");
sockets[i] = -1;
}
}
}
/*}
else {
for (int ibuf = 0; ibuf < r; ibuf++) {
for (size_t i = 0; i < n; i++) {
if (sockets[i] >= 0) {
if (write(sockets[i], &buf[ibuf], 1) < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
perror("write socket");
sockets[i] = -1;
}
}
}
usleep(g_wait_ms*1000);
}
}*/
sum += r;
}
if (sum > 0) {
fprintf(stderr, "%lu bytes read from stdin.\n", sum);
}
}
int read_sockets_to_files(int* sockets, size_t n, fd_set const* fdset, int* res_files, fd_set* all_read_set)
{
char buf[BUFSIZE];
int nclosed = 0;
for (size_t i = 0; i < n; i++) {
int s = sockets[i];
if (s == -1) {
continue;
}
if (!(FD_ISSET(s, fdset))) {
continue;
}
// Process this socket !
size_t sum = 0;
while (true) {
int r;
//if (g_wait_ms == 0) {
r = read(s, buf, BUFSIZE);
/*}
else {
r;
int rem = BUFSIZE;
usleep(g_wait_ms*1000);
while (((r = read(s, buf, 1)) > 0) && rem > 0) {
rem--;
usleep(g_wait_ms*1000);
}
}*/
if (r < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
break;
}
perror("read socket");
sockets[i] = -1;
break;
}
if (r == 0) {
// Socket has been closed. Do not "select" it anymore.
fprintf(stderr, "Sock %d: connection closed.\n", s);
FD_CLR(s, all_read_set);
sockets[i] = -1;
nclosed++;
break;
}
write(res_files[i], buf, r);
sum += r;
}
if (sum > 0) {
fprintf(stderr, "Sock %d: %lu bytes received.\n", s, sum);
}
}
return nclosed;
}
int main(int argc, char **argv)
{
if (argc <= 2) {
fprintf(stderr, "Usage: %s host port [n] [wait_msec]\n", argv[0]);
return 1;
}
const char* host_str = argv[1];
size_t nconn = 1;
if (argc >= 4) {
nconn = atoll(argv[3]);
}
size_t wait_ms = 0;
if (argc >= 5) {
wait_ms = atoll(argv[4]);
}
g_wait_ms = wait_ms;
uint16_t port = atoi(argv[2]);
if (port == 0) {
return 1;
}
struct hostent* hp = gethostbyname(host_str);
if (hp == NULL) {
perror("gethostbyname");
return 1;
}
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
memcpy(&sin.sin_addr, hp->h_addr_list[0], hp->h_length);
sin.sin_family = hp->h_addrtype;
sin.sin_port = htons(port);
// Create sockets
int max_sock = 0;
fd_set set_socks;
int* sockets = create_sockets(nconn, &max_sock, &set_socks);
if (sockets == NULL) {
return 1;
}
fprintf(stderr, "[*] Initializing %lu connections to %s:%d...\n[*] ", nconn, host_str, port);
for (size_t i = 0; i < nconn; i++) {
int r = connect(sockets[i], (struct sockaddr*) &sin, sizeof(struct sockaddr_in));
if (r < 0 && errno != EINPROGRESS) {
perror("connect");
free(sockets);
return errno;
}
fprintf(stderr, ".");
}
fprintf(stderr, "\n[*] %lu connections to %s:%d have been initiated... Waiting for completion...\n[*] ", nconn, host_str, port);
size_t nconn_success = 0;
size_t nconn_ret = 0;
int* sockets_success = (int*) malloc(nconn*sizeof(int));
fd_set fd_rem_socks = set_socks;
FD_ZERO(&set_socks);
int success_max_sock = 0;
while (nconn_ret < nconn) {
fd_set fd_sel_socks = fd_rem_socks;
int ret_sel = select(max_sock+1, NULL, &fd_sel_socks, NULL, NULL);
if (ret_sel < 0) {
perror("select");
free(sockets);
return errno;
}
for (size_t i = 0; i < nconn; i++) {
int socket = sockets[i];
if (FD_ISSET(socket, &fd_sel_socks)) {
// Call(s) to connect has return. Check whether it has succedded !
nconn_ret++;
assert(nconn_ret <= nconn);
int conn_err = 0;
socklen_t tmp_size = sizeof(int);
getsockopt(socket, SOL_SOCKET, SO_ERROR, &conn_err, &tmp_size);
if (conn_err != 0) {
fprintf(stderr, "Error for a connection: %s\n", strerror(conn_err));
close(socket);
}
else {
sockets_success[nconn_success] = socket;
nconn_success++;
FD_SET(socket, &set_socks);
if (socket > success_max_sock) {
success_max_sock = socket;
}
fprintf(stderr, ".");
}
}
}
// fd_rem_socks &= ~fd_sel_socks;
fd_set_not(&fd_sel_socks);
fd_set_and(&fd_rem_socks, &fd_rem_socks, &fd_sel_socks);
}
fprintf(stderr, "\n[*] %lu connections were successfull.\n", nconn_success);
/*if (wait_ms > 0) {
fprintf(stderr, "\n[*] Waiting %lu ms...\n", wait_ms);
usleep(wait_ms*1000);
}*/
free(sockets);
sockets = sockets_success;
max_sock = success_max_sock;
if (nconn_success == 0) {
return 1;
}
// Open result files
int* res_files = (int*) malloc(nconn_success*sizeof(int));
for (size_t i = 0; i < nconn_success; i++) {
char path[PATH_MAX];
snprintf(path, PATH_MAX-1, "sock.out.%lu", i);
int fd_file = open(path, O_CREAT|O_TRUNC|O_WRONLY);
if (fd_file < 0) {
perror("open");
return 1;
}
fchmod(fd_file, 0640);
res_files[i] = fd_file;
}
// Set stdin to non-blocking mode.
fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
// Forward all data from stdin to all connections.
// Write data from all connections to their respective fd.
FD_SET(STDIN_FILENO, &set_socks);
fd_set read_set_fd;
int nsocks_alive = nconn_success;
while (true) {
read_set_fd = set_socks;
int ret_sel = select(max_sock+1, &read_set_fd, NULL, NULL, NULL);
if (ret_sel < 0) {
perror("select");
return 1;
}
if (FD_ISSET(STDIN_FILENO, &read_set_fd)) {
write_stdin_all_sockets(sockets, nconn_success, &set_socks);
ret_sel--;
}
if (ret_sel > 0) {
int nsocks_closed = read_sockets_to_files(sockets, nconn_success, &read_set_fd, res_files, &set_socks);
nsocks_alive -= nsocks_closed;
if (nsocks_alive == 0) {
break;
}
}
}
for (size_t i = 0; i < nconn_success; i++) {
close(res_files[i]);
close(sockets[i]);
}
free(sockets);
free(res_files);
return 0;
}