-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpspray.c
executable file
·406 lines (327 loc) · 8.91 KB
/
tcpspray.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
/*
* tcpspray.c - v1.1
*
* measure throughput of half (via discard) and full (via echo) duplex
* tcp connection
*
* Greg Christy <[email protected]>
*
* 12/15/90 - initial hack
* 8/7/91 - clean-up time, first real version
* 1/15/92 - add inter-buffer delay option
* 1/16/92 - clean up echo mode code, add #ifdef for AIX
*/
#include <netdb.h>
#include <stdio.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#define DEFBLKSIZE 1024 /* default blocksize is 1k */
#define DEFNBLKS 100 /* default number of blocks is 100 */
extern char *optarg; /* external vars used by getopt(3) */
extern int optind, opterr;
/*
* neat macro from ka9q to "do the right thing" with ansi prototypes
*/
#ifndef __ARGS
#ifdef __STDC__
#define __ARGS(x) x
#else
#define __ARGS(x) ()
#endif
#endif
static void usage __ARGS((char *argv[])); /* forward declaration */
#if !defined(sun) && !defined(_AIX) /* sunos and aix define pid_t, */
/* do others? */
typedef int pid_t;
#endif
#ifndef TRUE
#define TRUE (1)
#endif /*TRUE*/
#ifndef FALSE
#define FALSE (0)
#endif /*FALSE*/
int
main(argc, argv)
unsigned int argc;
char *argv[];
{
int eflag = FALSE; /* use echo instead of discard for full duplex*/
int vflag = FALSE; /* verbosity*/
int fflag = FALSE; /* preload buffer with file */
int delay = 0; /* inter-buffer delay in usecs */
pid_t pid = 0; /* pid of receiver child */
int status; /* return status of child */
struct timeval start, end; /* used to store start and end time of I/O */
#ifndef sun
struct timeval timeout; /* used for timeout in select call */
#endif /*sun*/
double delta; /* stores delta of start and end in sec */
/* generic counter */
register int cnt;
int c; /* used to return options from getopt(3) */
struct sockaddr_in sin; /* sockaddr for socket */
struct hostent *hp; /* hostent for host name lookups */
struct servent *serv; /* service entry for port lookup */
int sock; /* socket descriptor */
int fd[2]; /* descriptors for pipe */
unsigned int blksize = DEFBLKSIZE; /* block size (1k default) */
unsigned int nblks = DEFNBLKS; /* number of blocks (100 default)*/
unsigned int nbytes; /* number of bytes to transfer */
register unsigned int bytes_left; /* keep track of bytes left to */
/* read/write */
register char *buf; /* input and output buffer (malloced) */
register char *bufp; /* placeholder pointer */
FILE *infile; /* used to preload buffer */
while ((c = getopt(argc, argv, "vehb:n:f:d:")) != -1) {
switch (c) {
case 'v':
vflag = TRUE;
break;
case 'e':
eflag = TRUE;
break;
case 'h':
usage(argv);
break;
case 'n':
nblks = atoi(optarg);
break;
case 'b':
blksize = atoi(optarg);
break;
case 'f':
fflag = TRUE;
if ((infile = fopen(optarg, "r")) == NULL) {
fprintf(stderr, "%s: Can't open file %s.\n", argv[0], optarg);
exit(2);
}
break;
case 'd':
delay = atoi(optarg);
#ifndef sun
/* we need to fake usleep() with select() on non-sun machines */
timeout.tv_usec = atoi(optarg) ;
timeout.tv_sec = 0;
#endif /*sun*/
break;
default:
usage(argv);
break;
}
}
if ((argc - optind) != 1) /* we better have a host name */
usage(argv);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
bzero((char *) &sin, sizeof(sin));
sin.sin_family = AF_INET;
if (bind(sock, &sin, sizeof (sin)) == -1) {
perror("bind");
exit(1);
}
hp = gethostbyname(argv[optind]);
if (hp) { /* try name first */
sin.sin_family = hp->h_addrtype;
bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
}
else { /* maybe it's a numeric address ?*/
sin.sin_family = AF_INET;
if ((sin.sin_addr.s_addr = inet_addr(argv[optind])) == -1) {
/* nope, sorry we lose */
fprintf(stderr, "host not found: %s\n", argv[optind]);
exit(1);
}
}
serv = getservbyname(eflag ? "echo" : "discard", "tcp"); /* get port */
sin.sin_port = serv->s_port;
if (connect(sock, &sin, sizeof(sin)) == -1) {
perror("connect");
exit(1);
}
nbytes = nblks * blksize; /* number of bytes to send/receive */
if (vflag)
printf("Sending %d bytes with blocksize %d bytes\n",
nbytes, blksize);
if ((buf = malloc((blksize < 4096) ? 4096 : blksize)) == NULL) {
perror("malloc buf");
exit(1);
}
bzero(buf, blksize); /* clean out the buffer */
if (fflag) { /* preload buffer with file */
cnt = fread(buf, sizeof(u_char), blksize, infile);
if (cnt < blksize)
fprintf(stderr, "Warning! File is smaller than blocksize.\n");
}
if (eflag) {
if (pipe(fd) == -1) { /* create pipe for return */
perror("pipe");
exit(1);
}
if ((pid = fork()) == -1) { /* fork off a receiver */
perror("fork()");
exit(1);
}
}
if (eflag && pid != 0) { /* we are the receiver */
close (fd[1]);
bytes_left = nblks * blksize;
if (gettimeofday(&start, NULL) == -1) {
perror("gettimeofday");
exit(1);
}
if (vflag) {
register int flush_flag = FALSE, blk_boundry = bytes_left - blksize;
while (bytes_left) {
if ((cnt = read(sock, buf, bytes_left)) == -1) {
perror("receive:");
exit(2);
}
bytes_left -= cnt;
while (blk_boundry > bytes_left) {
fputs("\b \b", stdout);
flush_flag = TRUE;
blk_boundry -= blksize;
}
if (flush_flag) {
fflush(stdout);
flush_flag = FALSE;
}
}
if (gettimeofday(&end, NULL) == -1) {
perror("gettimeofday");
exit(1);
}
printf("\b");
fflush(stdout);
}
else { /* not vflag */
while (bytes_left) {
if ((cnt = read(sock, buf, bytes_left)) == -1) {
perror("receive:");
exit(2);
}
bytes_left -= cnt;
}
if (gettimeofday(&end, NULL) == -1) {
perror("gettimeofday");
_exit(1);
}
}
delta = (double) (end.tv_sec - start.tv_sec) + (double)
((double) (end.tv_usec - start.tv_usec) / 1000000.0);
printf("Received %d bytes in %f seconds (%0.3f kbytes/s)\n",
nbytes, delta, (double) (nbytes / delta) /1024);
if (wait(&status) == -1) {/* wait for child lest we */
/* unleash zombies */
perror("wait");
exit(1);
}
if (status) {
fprintf(stderr, "child returned non-zero status %d\n", status);
}
/* get delta from transmitter process */
if ((cnt = read(fd[0], (char *) &delta, sizeof(delta))) == -1) {
perror("read pipe");
exit(1);
}
printf("Transmitted %d bytes in %f seconds (%0.3f kbytes/s)\n",
nbytes, delta, (double) (nbytes / delta) / 1024);
}
else { /* we are the transmitter */
if (vflag) {
if (gettimeofday(&start, NULL) == -1) {
perror("gettimeofday");
exit(1);
}
while (nblks--) {
cnt = 0;
bufp = buf;
bytes_left = blksize;
do {
if ((cnt = write(sock, bufp, bytes_left)) == -1) {
perror("receive:");
exit(2);
}
bufp += cnt;
bytes_left -= cnt;
#ifdef sun
if (delay)
usleep(delay);
#else /*sun*/
if (delay)
select(0, NULL, NULL, NULL, timeout);
#endif /*sun*/
} while (bytes_left);
putchar('.');
fflush(stdout);
}
if (gettimeofday(&end, NULL) == -1) {
perror("gettimeofday");
exit(1);
}
}
else { /* not vflag */
if (gettimeofday(&start, NULL) == -1) {
perror("gettimeofday");
exit(1);
}
while (nblks--) {
cnt = 0;
bufp = buf;
bytes_left = blksize;
do {
if ((cnt = write(sock, bufp, bytes_left)) == -1) {
perror("receive:");
exit(2);
}
bufp += cnt;
bytes_left -= cnt;
#ifdef sun
if (delay)
usleep(delay);
#else /*sun*/
if (delay)
select(0, NULL, NULL, NULL, timeout);
#endif /*sun*/
} while (bytes_left);
}
if (gettimeofday(&end, NULL) == -1) {
perror("gettimeofday");
exit(1);
}
}
delta = (double) (end.tv_sec - start.tv_sec) + (double)
((double) (end.tv_usec - start.tv_usec) / 1000000.0);
/* write delta value to pipe */
if (eflag) {
if ((cnt = write(fd[1], (char *) &delta, sizeof(delta))) == -1) {
perror("write pipe");
exit(1);
}
exit(0);
}
else
printf("\nTransmitted %d bytes in %f seconds (%0.3f kbytes/s)\n",
nbytes, delta, (double) (nbytes / delta) / 1024);
}
exit(0);
}
static void
usage(argv)
char *argv[];
{
fprintf(stderr, "usage: %s [-v] [-e] [-h] [-b blksize] [-n nblks] [-f file] host\n", argv[0]);
fprintf(stderr, " -v verbose\n");
fprintf(stderr, " -e use echo service for full duplex transfer\n");
fprintf(stderr, " -h print this message\n");
fprintf(stderr, " -b blocksize in bytes (default 1024)\n");
fprintf(stderr, " -n number of blocks (default 100)\n");
fprintf(stderr, " -f file to preload buffer (zero buffer by default)\n");
fprintf(stderr, " -d inter-buffer transmission delay in usecs (default 0)\n");
exit(1);
}