-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfwannoyer.c
427 lines (361 loc) · 8.61 KB
/
fwannoyer.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
* fwannoyer.c - traffic generator based on a simple echo server/client scheme.
* Copyright (C) 2011 Rafael Aquini <[email protected]>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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.
*
* compiles with: gcc -Wall -O2 -o fwannoyer fwannoyer.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <syslog.h>
#include <fcntl.h>
#include <netdb.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BACKLOG 1024
#define BUFSIZE 256
int bufsize = BUFSIZE;
const char *prg_name;
static void oops(const char *msg)
{
if (errno != 0) {
fprintf(stderr, "Oops: %s: %s\n", strerror(errno), msg);
} else
fprintf(stderr, "Oops: %s\n", msg);
exit(errno);
}
void print_usage(FILE *stream, int exit_code)
{
fprintf(stream, "Usage: %s <options>\n", prg_name);
fprintf(stream,
" -h --help Display this usage information.\n"
" -s --server Run in server mode.\n"
" -c --client <srv> Run in client mode.\n"
" -p --port <#> Set a port to server/client modes.\n"
" -b --bufsz <#> Set the buffer size to write/read.\n");
exit(exit_code);
}
static void chld_reaper(int sig)
{
int saved_errno;
saved_errno = errno;
while (waitpid(-1, NULL, WNOHANG) > 0)
continue;
errno = saved_errno;
}
int deamonize(void)
{
int fd, maxfd;
switch (fork()) {
case -1:
return -1;
case 0:
break;
default:
_exit(EXIT_SUCCESS);
}
if (setsid() == -1)
return -1;
switch (fork()) {
case -1:
return -1;
case 0:
break;
default:
_exit(EXIT_SUCCESS);
}
maxfd = sysconf(_SC_OPEN_MAX);
if (maxfd == -1)
maxfd = 1024;
for (fd=0; fd < maxfd; fd++)
close(fd);
open("/dev/null", O_RDWR);
dup(0);
dup(0);
return 0;
}
ssize_t readn(int fd, void *buffer, size_t n)
{
ssize_t reads;
size_t total_reads;
char *buf;
buf = buffer;
for (total_reads = 0; total_reads < n;) {
reads = read(fd, buf, n - total_reads);
if (reads == 0)
return total_reads;
if (reads == -1) {
if (errno == EINTR)
continue;
else
return -1;
}
total_reads += reads;
buf += reads;
}
return total_reads;
}
ssize_t writen(int fd, const void *buffer, size_t n)
{
ssize_t writes;
size_t total_writes;
const char *buf;
buf = buffer;
for (total_writes = 0; total_writes < n;) {
writes = write(fd, buf, n - total_writes);
if (writes <= 0) {
if (writes == -1 && errno == EINTR)
continue;
else
return -1;
}
total_writes += writes;
buf += writes;
}
return total_writes;
}
static void handle_request(int sock_d)
{
char buf[bufsize];
ssize_t reads;
struct timespec req = { .tv_sec = 0,
.tv_nsec = 1492000 };
struct timespec rem;
int ret, counter=0;
socklen_t len_inet;
struct sockaddr_in peer;
char client_str[bufsize];
len_inet = sizeof(peer);
if (getpeername(sock_d, (struct sockaddr *)&peer, &len_inet) == -1) {
snprintf(client_str, bufsize, "????????:????");
} else {
snprintf(client_str, bufsize, "%s:%u",
inet_ntoa(peer.sin_addr),
(unsigned)ntohs(peer.sin_port));
}
while ((reads = readn(sock_d, buf, bufsize)) > 0) {
/* lets sleep a little time pretending we are doing something
important -- but this just goes form 14,92 millisec to
149,2 millisec of dummy latency */
req.tv_nsec++;
if (req.tv_nsec >= 14920000) {
counter = 0;
req.tv_nsec = 1492000;
}
retry:
ret = nanosleep(&req, &rem);
if (ret) {
if (errno == EINTR) {
req.tv_sec = rem.tv_sec;
req.tv_nsec = rem.tv_nsec;
goto retry;
}
syslog(LOG_ERR, "nanosleep() failed: %s",
strerror(errno));
}
/* after some 'processing' write back to client */
if (writen(sock_d, buf, reads) != reads) {
syslog(LOG_ERR, "write() failed: %s: %s",
strerror(errno), client_str);
exit(EXIT_FAILURE);
}
if (reads == -1) {
syslog(LOG_ERR, "read() failed: %s: %s",
strerror(errno), client_str);
exit(EXIT_FAILURE);
}
}
}
void start_server(int port)
{
int svsock_d, clsock_d;
socklen_t len_inet;
struct sigaction sa;
struct sockaddr_in svaddr;
struct sockaddr_in claddr;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = chld_reaper;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
syslog(LOG_ERR, "error from sigaction(): %s", strerror(errno));
exit(EXIT_FAILURE);
}
svsock_d = socket(AF_INET, SOCK_STREAM, 0);
if (svsock_d == -1) {
syslog(LOG_ERR, "error from socket(): %s", strerror(errno));
exit(EXIT_FAILURE);
}
memset(&svaddr, 0, sizeof(svaddr));
svaddr.sin_family = AF_INET;
svaddr.sin_port = htons(port);
svaddr.sin_addr.s_addr = INADDR_ANY;
len_inet = sizeof(struct sockaddr_in);
if (bind(svsock_d, (struct sockaddr *)&svaddr, len_inet) == -1) {
syslog(LOG_ERR, "error from bind(): %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (listen(svsock_d, BACKLOG) == -1) {
syslog(LOG_ERR, "error from listen(): %s", strerror(errno));
exit(EXIT_FAILURE);
}
for (;;) {
clsock_d = accept(svsock_d, (struct sockaddr *)&claddr,
&len_inet);
if (clsock_d == -1) {
syslog(LOG_ERR, "error from accept(): %s",
strerror(errno));
exit(EXIT_FAILURE);
}
switch(fork()) {
case -1:
syslog(LOG_ERR, "cannot create child (%s)",
strerror(errno));
close(clsock_d);
break;
case 0:
close(svsock_d);
handle_request(clsock_d);
exit(EXIT_SUCCESS);
default:
close(clsock_d);
break;
}
}
}
void start_client(char *srv, int port)
{
int socket_d, ret;
socklen_t len_inet;
struct sigaction sa;
struct sockaddr_in in_addr;
char buf[bufsize];
ssize_t reads;
struct timespec rem;
struct timespec req = { .tv_sec = 0,
.tv_nsec = 1492000 };
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = chld_reaper;
if (sigaction(SIGCHLD, &sa, NULL) == -1)
oops("error from sigaction()");
socket_d = socket(AF_INET, SOCK_STREAM, 0);
if (socket_d == -1)
oops("error from socket()");
len_inet = sizeof(struct sockaddr_in);
memset(&in_addr, 0, len_inet);
in_addr.sin_family = AF_INET;
in_addr.sin_port = htons(port);
in_addr.sin_addr.s_addr = inet_addr(srv);
if (connect(socket_d, (struct sockaddr *)&in_addr, len_inet) == -1)
oops("error from connect()");
switch(fork()) {
case -1:
oops("error from fork()");
case 0: /* child -- read server's response */
for (;;) {
reads = readn(socket_d, buf, bufsize);
if (reads <= 0)
break;
printf("%.*s", (int) reads, buf);
}
exit(EXIT_SUCCESS);
default: /* parent -- write stdin to socket */
for (;;) {
req.tv_nsec++;
if (req.tv_nsec >= 14920000)
req.tv_nsec = 1492000;
reads = read(STDIN_FILENO, buf, bufsize);
if (reads <= 0)
break;
/* insert some sleep */
retry:
ret = nanosleep(&req, &rem);
if (ret) {
if (errno == EINTR) {
req.tv_sec = rem.tv_sec;
req.tv_nsec = rem.tv_nsec;
goto retry;
}
syslog(LOG_ERR, "nanosleep() failed: %s",
strerror(errno));
}
writen(socket_d, buf, bufsize);
}
exit(EXIT_SUCCESS);
}
}
int main(int argc, char *argv[])
{
int next_opt, mode = 0;
char *server = NULL;
const char *short_opts = "hsc:p:b:";
const struct option long_opts[] = {
{"help", 0, NULL, 'h'},
{"server", 0, NULL, 's'},
{"client", 1, NULL, 'c'},
{"port", 1, NULL, 'p'},
{"bufsz", 1, NULL, 'b'},
{NULL, 0, NULL, 0} } ;
int port_num = 9999;
prg_name = argv[0];
do {
next_opt = getopt_long(argc, argv, short_opts, long_opts, NULL);
switch (next_opt) {
case 'h':
print_usage(stdout, 0);
case 's':
mode += 1;
break;
case 'c':
mode += 2;
server = optarg;
break;
case 'p':
port_num = atoi(optarg);
break;
case 'b':
bufsize = atoi(optarg);
break;
case '?':
print_usage(stderr, 1);
case -1:
break;
default:
abort();
}
} while (next_opt != -1);
if (argc == 1)
print_usage(stderr, 2);
if (mode == 0 || mode > 2)
oops("Ok, I cannot realize what you want now...");
switch (mode) {
case 1: /* server mode */
if (deamonize() == -1)
oops("failed to become a deamon.");
start_server(port_num);
break;
case 2: /* client mode */
start_client(server, port_num);
break;
}
return 0;
}