]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/tcp-banger2.c
3 /* $Id: tcp-banger2.c,v 1.23 1999/04/15 06:16:15 wessels Exp $ */
6 * On some systems, FD_SETSIZE is set to something lower than the
7 * actual number of files which can be opened. IRIX is one case,
8 * NetBSD is another. So here we increase FD_SETSIZE to our
9 * configure-discovered maximum *before* any system includes.
11 #define CHANGE_FD_SETSIZE 1
13 /* Cannot increase FD_SETSIZE on Linux */
14 #if defined(_SQUID_LINUX_)
15 #undef CHANGE_FD_SETSIZE
16 #define CHANGE_FD_SETSIZE 0
19 /* Cannot increase FD_SETSIZE on FreeBSD before 2.2.0, causes select(2)
20 * to return EINVAL. */
21 /* Marian Durkovic <marian@svf.stuba.sk> */
22 /* Peter Wemm <peter@spinner.DIALix.COM> */
23 #if defined(_SQUID_FREEBSD_)
24 #include <osreldate.h>
25 #if __FreeBSD_version < 220000
26 #undef CHANGE_FD_SETSIZE
27 #define CHANGE_FD_SETSIZE 0
31 /* Increase FD_SETSIZE if SQUID_MAXFD is bigger */
32 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
33 #define FD_SETSIZE SQUID_MAXFD
55 #include <sys/types.h>
58 #include <sys/select.h>
70 #include <sys/socket.h>
73 #include <netinet/in.h>
76 #include <arpa/inet.h>
91 #define PROXY_PORT 3128
92 #define PROXY_ADDR "127.0.0.1"
93 #define READ_BUF_SZ 4096
95 static int proxy_port
= PROXY_PORT
;
96 static char *proxy_addr
= PROXY_ADDR
;
97 static char *progname
;
100 static int opt_ims
= 0;
101 static int opt_range
= 0;
102 static int opt_accel
= 0;
103 static int max_connections
= 64;
104 static time_t lifetime
= 60;
105 static time_t process_lifetime
= 86400;
106 static struct timeval now
;
107 static long total_bytes_written
= 0;
108 static long total_bytes_read
= 0;
109 static int opt_checksum
= 0;
110 FILE *trace_file
= NULL
;
112 typedef void (CB
) (int, void *);
124 char requestbodyfile
[256];
125 char buf
[READ_BUF_SZ
* 2 + 1];
135 struct _f FD
[SQUID_MAXFD
];
141 free_request(struct _request
*r
)
152 static char buf
[128];
153 struct tm
*gmt
= gmtime(t
);
155 (void) strftime(buf
, 127, "%A, %d-%b-%y %H:%M:%S GMT", gmt
);
164 FD
[fd
].ccb(fd
, FD
[fd
].data
);
170 while (fd
> 0 && FD
[fd
].cb
== NULL
)
177 fd_open(int fd
, CB
* cb
, void *data
, CB
* ccb
)
179 assert(fd
< SQUID_MAXFD
);
183 FD
[fd
].start
= now
.tv_sec
;
194 printf("\rWaiting for open connections to finish...\n");
195 signal(sig
, SIG_DFL
);
199 read_reply(int fd
, void *data
)
201 struct _request
*r
= data
;
202 static unsigned char buf
[READ_BUF_SZ
];
205 if ((len
= read(fd
, buf
, READ_BUF_SZ
)) <= 0) {
211 total_bytes_read
+= len
;
212 if (r
->headfound
< 2) {
213 char *p
, *header
= NULL
;
214 int oldlen
= strlen(r
->buf
);
215 int newlen
= oldlen
+ len
;
216 assert(oldlen
<= READ_BUF_SZ
);
217 memcpy(r
->buf
+ oldlen
, buf
, len
);
218 r
->buf
[newlen
+ 1] = '\0';
219 for (p
= r
->buf
; r
->headfound
< 2 && used
< newlen
; p
++, used
++) {
226 if (strncmp(header
, "HTTP", 4) == 0)
227 r
->status
= atoi(header
+ 8);
228 else if (strncasecmp(header
, "Content-Length:", 15) == 0)
229 r
->content_length
= atoi(header
+ 15);
230 else if (strncasecmp(header
, "X-Request-URI:", 14) == 0) {
232 if (strncmp(r
->url
, header
+ 15, strcspn(header
+ 15, "\r\n"))) {
234 strncpy(url
, header
+ 15, strcspn(header
+ 15, "\r\n"));
235 url
[strcspn(header
+ 15, "\r\n")] = '\n';
236 fprintf(stderr
, "ERROR: Sent %s received %s\n",
252 memmove(r
->buf
, header
, newlen
- (header
- r
->buf
) + 1);
254 assert(used
>= oldlen
);
257 r
->bodysize
+= len
- used
;
259 for (; used
< len
; used
++) {
266 reply_done(int fd
, void *data
)
268 struct _request
*r
= data
;
269 if (opt_range
); /* skip size checks for now */
270 else if (r
->bodysize
!= r
->content_length
&& r
->content_length
>= 0)
271 fprintf(stderr
, "ERROR: %s got %d of %d bytes\n",
272 r
->url
, r
->bodysize
, r
->content_length
);
273 else if (r
->validsize
>= 0) {
274 if (r
->validsize
!= r
->bodysize
)
275 fprintf(stderr
, "WARNING: %s size mismatch wanted %d bytes got %d\n",
276 r
->url
, r
->validsize
, r
->bodysize
);
277 else if (opt_checksum
&& r
->validsum
!= r
->sum
)
278 fprintf(stderr
, "WARNING: %s invalid checksum wanted 0x%lx got 0x%lx\n",
279 r
->url
, r
->validsum
, r
->sum
);
283 fprintf(trace_file
, "%s %s %d %s %d 0x%lx\n",
284 r
->method
, r
->url
, r
->status
, r
->requestbodyfile
, r
->bodysize
, r
->sum
);
286 fprintf(trace_file
, "%s %s %d %s %d\n",
287 r
->method
, r
->url
, r
->status
, r
->requestbodyfile
, r
->bodysize
);
298 char *method
, *url
, *file
, *size
, *checksum
;
304 struct sockaddr_in S
;
308 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
312 memset(&S
, '\0', sizeof(struct sockaddr_in
));
313 S
.sin_family
= AF_INET
;
314 S
.sin_port
= htons(proxy_port
);
315 S
.sin_addr
.s_addr
= inet_addr(proxy_addr
);
316 if (connect(s
, (struct sockaddr
*) &S
, sizeof(S
)) < 0) {
321 strcpy(urlbuf
, urlin
);
322 method
= strtok(urlbuf
, " ");
323 url
= strtok(NULL
, " ");
324 file
= strtok(NULL
, " ");
325 size
= strtok(NULL
, " ");
326 checksum
= strtok(NULL
, " ");
337 r
= calloc(1, sizeof *r
);
339 r
->url
= strdup(url
);
341 strcpy(r
->method
, method
);
342 strcpy(r
->requestbodyfile
, file
);
344 if (size
&& strcmp(size
, "-") != 0)
345 r
->validsize
= atoi(size
);
347 r
->validsize
= -1; /* Unknown */
348 if (checksum
&& strcmp(checksum
, "-") != 0)
349 r
->validsum
= strtoul(checksum
, NULL
, 0);
350 r
->content_length
= -1; /* Unknown */
352 host
= strchr(url
, '/') + 2;
353 url
= strchr(host
, '/');
357 sprintf(msg
, "%s %s HTTP/1.0\r\n", method
, url
);
360 sprintf(buf
, "Host: %s\r\n", host
);
364 strcat(msg
, "Accept: */*\r\n");
365 if (opt_ims
&& (lrand48() & 0x03) == 0) {
366 w
= time(NULL
) - (lrand48() & 0x3FFFF);
367 sprintf(buf
, "If-Modified-Since: %s\r\n", mkrfc850(&w
));
370 if (file
&& strcmp(file
, "-") != 0) {
371 f
= open(file
, O_RDONLY
);
377 sprintf(buf
, "Content-Length: %d\r\n", (int) st
.st_size
);
380 if (opt_range
&& (lrand48() & 0x03) == 0) {
383 strcat(msg
, "Range: bytes=");
384 while (((len
= (int) lrand48()) & 0x03) == 0 || !count
) {
385 const int offset
= (int) lrand48();
388 switch (lrand48() & 0x03) {
390 sprintf(buf
, "-%d", len
);
393 sprintf(buf
, "%d-", offset
);
396 sprintf(buf
, "%d-%d", offset
, offset
+ len
);
406 if ((len2
= write(s
, msg
, len
)) != len
) {
408 perror("write request");
412 total_bytes_written
+= len2
;
414 while ((len
= read(f
, buf
, sizeof(buf
))) > 0) {
415 len2
= write(s
, buf
, len
);
417 perror("write body");
428 * if (fcntl(s, F_SETFL, O_NDELAY) < 0)
429 * perror("fcntl O_NDELAY");
435 read_url(int fd
, void *junk
)
438 static char buf
[8192];
441 if (fgets(buf
, 8191, stdin
) == NULL
) {
442 printf("Done Reading URLS\n");
449 if ((t
= strchr(buf
, '\n')))
453 max_connections
= nfds
- 1;
454 printf("NOTE: max_connections set at %d\n", max_connections
);
456 fd_open(r
->fd
, read_reply
, r
, reply_done
);
463 fprintf(stderr
, "usage: %s: -p port -h host -n max\n", progname
);
464 fprintf(stderr
, " -t <tracefile> Save request trace\n");
465 fprintf(stderr
, " -c Check checksum agains trace\n");
466 fprintf(stderr
, " -i Send random If-Modified-Since times\n");
467 fprintf(stderr
, " -l <seconds> Connection lifetime timeout (default 60)\n");
468 fprintf(stderr
, " -a Accelerator mode\n");
481 struct timeval start
;
484 setbuf(stdout
, NULL
);
485 setbuf(stderr
, NULL
);
486 progname
= strdup(argv
[0]);
487 gettimeofday(&now
, NULL
);
489 while ((c
= getopt(argc
, argv
, "ap:h:n:icrl:L:t:")) != -1) {
495 proxy_port
= atoi(optarg
);
498 proxy_addr
= strdup(optarg
);
501 max_connections
= atoi(optarg
);
507 lifetime
= (time_t) atoi(optarg
);
510 process_lifetime
= (time_t) atoi(optarg
);
516 trace_file
= fopen(optarg
, "a");
518 setbuf(trace_file
, NULL
);
528 fd_open(0, read_url
, NULL
, NULL
);
530 signal(SIGINT
, sig_intr
);
531 signal(SIGPIPE
, SIG_IGN
);
533 while (nfds
|| FD
[0].cb
) {
537 if (nfds
< max_connections
&& FD
[0].cb
)
539 for (i
= 1; i
<= maxfd
; i
++) {
540 if (FD
[i
].cb
== NULL
)
542 if (now
.tv_sec
- FD
[i
].start
> lifetime
) {
543 fprintf(stderr
, "WARNING: fd %d lifetime timeout\n", i
);
549 if (select(maxfd
+ 1, &R
, NULL
, NULL
, &to
) < 0) {
550 fprintf(stderr
, "maxfd=%d\n", maxfd
);
555 gettimeofday(&now
, NULL
);
556 for (i
= 0; i
<= maxfd
; i
++) {
557 if (!FD_ISSET(i
, &R
))
559 FD
[i
].cb(i
, FD
[i
].data
);
560 if (nfds
< max_connections
&& FD
[0].cb
) {
565 if (select(1, &R2
, NULL
, NULL
, &to
) == 1)
566 FD
[0].cb(0, FD
[0].data
);
569 if (now
.tv_sec
> last
.tv_sec
) {
571 dt
= (int) (now
.tv_sec
- start
.tv_sec
);
572 printf("T+ %6d: %9d req (%+4d), %4d conn, %3d/sec avg, %dmb, %dkb/sec avg\n",
577 (int) (nrequests
/ dt
),
578 (int) total_bytes_read
/ 1024 / 1024,
579 (int) total_bytes_read
/ 1024 / dt
);
582 * if (dt > process_lifetime)
587 printf("Exiting normally\n");