]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/tcp-banger2.c
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 long total_bytes_written
= 0;
108 static long long total_bytes_read
= 0;
109 static int opt_checksum
= 0;
110 static char *custom_header
= NULL
;
111 FILE *trace_file
= NULL
;
113 typedef void (CB
) (int, void *);
125 char requestbodyfile
[256];
126 char buf
[READ_BUF_SZ
* 2 + 1];
137 struct _f FD
[SQUID_MAXFD
];
143 free_request(struct _request
*r
)
150 #define RFC1123_STRFTIME "%a, %d %b %Y %H:%M:%S GMT"
155 static char buf
[128];
156 struct tm
*gmt
= gmtime(t
);
158 (void) strftime(buf
, 127, RFC1123_STRFTIME
, gmt
);
167 FD
[fd
].ccb(fd
, FD
[fd
].data
);
173 while (fd
> 0 && FD
[fd
].cb
== NULL
)
180 fd_open(int fd
, CB
* cb
, void *data
, CB
* ccb
)
182 assert(fd
< SQUID_MAXFD
);
186 FD
[fd
].start
= now
.tv_sec
;
197 printf("\rWaiting for open connections to finish...\n");
198 signal(sig
, SIG_DFL
);
202 read_reply(int fd
, void *data
)
204 struct _request
*r
= data
;
205 static unsigned char buf
[READ_BUF_SZ
];
208 if ((len
= read(fd
, buf
, READ_BUF_SZ
)) <= 0) {
214 total_bytes_read
+= len
;
215 if (r
->headfound
< 2) {
216 char *p
, *header
= NULL
;
217 int oldlen
= strlen(r
->buf
);
218 int newlen
= oldlen
+ len
;
219 assert(oldlen
<= READ_BUF_SZ
);
220 memcpy(r
->buf
+ oldlen
, buf
, len
);
221 r
->buf
[newlen
+ 1] = '\0';
222 for (p
= r
->buf
; r
->headfound
< 2 && used
< newlen
; p
++, used
++) {
229 if (strncmp(header
, "HTTP", 4) == 0)
230 r
->status
= atoi(header
+ 8);
231 else if (strncasecmp(header
, "Content-Length:", 15) == 0)
232 r
->content_length
= atoi(header
+ 15);
233 else if (strncasecmp(header
, "X-Request-URI:", 14) == 0) {
235 if (strncmp(r
->url
, header
+ 15, strcspn(header
+ 15, "\r\n"))) {
237 strncpy(url
, header
+ 15, strcspn(header
+ 15, "\r\n"));
238 url
[strcspn(header
+ 15, "\r\n")] = '\n';
239 fprintf(stderr
, "ERROR: Sent %s received %s\n",
255 memmove(r
->buf
, header
, newlen
- (header
- r
->buf
) + 1);
257 assert(used
>= oldlen
);
260 r
->bodysize
+= len
- used
;
262 for (; used
< len
; used
++) {
269 reply_done(int fd
, void *data
)
271 struct _request
*r
= data
;
272 if (opt_range
); /* skip size checks for now */
273 else if (strcmp(r
->method
, "HEAD") == 0);
274 else if (r
->bodysize
!= r
->content_length
&& r
->content_length
>= 0)
275 fprintf(stderr
, "ERROR: %s got %d of %d bytes\n",
276 r
->url
, r
->bodysize
, r
->content_length
);
277 else if (r
->validsize
>= 0) {
278 if (r
->validsize
!= r
->bodysize
)
279 fprintf(stderr
, "WARNING: %s size mismatch wanted %d bytes got %d\n",
280 r
->url
, r
->validsize
, r
->bodysize
);
281 else if (opt_checksum
&& r
->validsum
!= r
->sum
)
282 fprintf(stderr
, "WARNING: %s invalid checksum wanted 0x%lx got 0x%lx\n",
283 r
->url
, r
->validsum
, r
->sum
);
285 if (r
->validstatus
&& r
->status
!= r
->validstatus
) {
286 fprintf(stderr
, "WARNING: %s status mismatch wanted %d got %d\n",
287 r
->url
, r
->validstatus
, r
->status
);
291 fprintf(trace_file
, "%s %s %d %s %d 0x%lx\n",
292 r
->method
, r
->url
, r
->status
, r
->requestbodyfile
, r
->bodysize
, r
->sum
);
294 fprintf(trace_file
, "%s %s %d %s %d\n",
295 r
->method
, r
->url
, r
->status
, r
->requestbodyfile
, r
->bodysize
);
301 request(char *urlin
) {
305 char *method
, *url
, *file
, *size
, *checksum
, *status
;
311 struct sockaddr_in S
;
315 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
319 memset(&S
, '\0', sizeof(struct sockaddr_in
));
320 S
.sin_family
= AF_INET
;
321 S
.sin_port
= htons(proxy_port
);
322 S
.sin_addr
.s_addr
= inet_addr(proxy_addr
);
323 if (connect(s
, (struct sockaddr
*) &S
, sizeof(S
)) < 0) {
328 strcpy(urlbuf
, urlin
);
329 method
= strtok(urlbuf
, " ");
330 url
= strtok(NULL
, " ");
331 status
= strtok(NULL
, " ");
332 file
= strtok(NULL
, " ");
333 size
= strtok(NULL
, " ");
334 checksum
= strtok(NULL
, " ");
345 r
= calloc(1, sizeof *r
);
347 r
->url
= strdup(url
);
349 strcpy(r
->method
, method
);
350 strcpy(r
->requestbodyfile
, file
);
352 if (size
&& strcmp(size
, "-") != 0)
353 r
->validsize
= atoi(size
);
355 r
->validsize
= -1; /* Unknown */
356 if (checksum
&& strcmp(checksum
, "-") != 0)
357 r
->validsum
= strtoul(checksum
, NULL
, 0);
359 r
->validstatus
= atoi(status
);
360 r
->content_length
= -1; /* Unknown */
362 host
= strchr(url
, '/') + 2;
363 url
= strchr(host
, '/');
367 sprintf(msg
, "%s %s HTTP/1.0\r\n", method
, url
);
370 sprintf(buf
, "Host: %s\r\n", host
);
374 strcat(msg
, "Accept: */*\r\n");
375 if (opt_ims
&& (lrand48() & 0x03) == 0) {
376 w
= time(NULL
) - (lrand48() & 0x3FFFF);
377 sprintf(buf
, "If-Modified-Since: %s\r\n", mkrfc850(&w
));
380 if (file
&& strcmp(file
, "-") != 0) {
381 f
= open(file
, O_RDONLY
);
387 sprintf(buf
, "Content-Length: %d\r\n", (int) st
.st_size
);
390 if (opt_range
&& (lrand48() & 0x03) == 0) {
393 strcat(msg
, "Range: bytes=");
394 while (((len
= (int) lrand48()) & 0x03) == 0 || !count
) {
395 const int offset
= (int) lrand48();
398 switch (lrand48() & 0x03) {
400 sprintf(buf
, "-%d", len
);
403 sprintf(buf
, "%d-", offset
);
406 sprintf(buf
, "%d-%d", offset
, offset
+ len
);
414 strcat(msg
, custom_header
);
417 if ((len2
= write(s
, msg
, len
)) != len
) {
419 perror("write request");
423 total_bytes_written
+= len2
;
425 while ((len
= read(f
, buf
, sizeof(buf
))) > 0) {
426 len2
= write(s
, buf
, len
);
428 perror("write body");
439 * if (fcntl(s, F_SETFL, O_NDELAY) < 0)
440 * perror("fcntl O_NDELAY");
446 read_url(int fd
, void *junk
)
449 static char buf
[8192];
452 if (fgets(buf
, 8191, stdin
) == NULL
) {
453 printf("Done Reading URLS\n");
460 if ((t
= strchr(buf
, '\n')))
464 max_connections
= nfds
- 1;
465 printf("NOTE: max_connections set at %d\n", max_connections
);
467 fd_open(r
->fd
, read_reply
, r
, reply_done
);
474 fprintf(stderr
, "usage: %s: -p port -h host -n max\n", progname
);
475 fprintf(stderr
, " -t <tracefile> Save request trace\n");
476 fprintf(stderr
, " -c Check checksum agains trace\n");
477 fprintf(stderr
, " -i Send random If-Modified-Since times\n");
478 fprintf(stderr
, " -l <seconds> Connection lifetime timeout (default 60)\n");
479 fprintf(stderr
, " -a Accelerator mode\n");
480 fprintf(stderr
, " -H <string> Custom header\n");
493 struct timeval start
;
496 setbuf(stdout
, NULL
);
497 setbuf(stderr
, NULL
);
498 progname
= strdup(argv
[0]);
499 gettimeofday(&now
, NULL
);
501 custom_header
= strdup("");
502 while ((c
= getopt(argc
, argv
, "ap:h:H:n:icrl:L:t:")) != -1) {
508 proxy_port
= atoi(optarg
);
511 proxy_addr
= strdup(optarg
);
514 max_connections
= atoi(optarg
);
520 lifetime
= (time_t) atoi(optarg
);
523 process_lifetime
= (time_t) atoi(optarg
);
529 trace_file
= fopen(optarg
, "a");
531 setbuf(trace_file
, NULL
);
537 custom_header
= realloc(custom_header
, strlen(custom_header
) + strlen(optarg
) + 2 + 1);
538 strcat(custom_header
, optarg
);
539 strcat(custom_header
, "\r\n");
546 fd_open(0, read_url
, NULL
, NULL
);
548 signal(SIGINT
, sig_intr
);
549 signal(SIGPIPE
, SIG_IGN
);
551 while (nfds
|| FD
[0].cb
) {
555 if (nfds
< max_connections
&& FD
[0].cb
)
557 for (i
= 1; i
<= maxfd
; i
++) {
558 if (FD
[i
].cb
== NULL
)
560 if (now
.tv_sec
- FD
[i
].start
> lifetime
) {
561 fprintf(stderr
, "WARNING: fd %d lifetime timeout\n", i
);
567 if (select(maxfd
+ 1, &R
, NULL
, NULL
, &to
) < 0) {
568 fprintf(stderr
, "maxfd=%d\n", maxfd
);
573 gettimeofday(&now
, NULL
);
574 for (i
= 0; i
<= maxfd
; i
++) {
575 if (!FD_ISSET(i
, &R
))
577 FD
[i
].cb(i
, FD
[i
].data
);
578 if (nfds
< max_connections
&& FD
[0].cb
) {
583 if (select(1, &R2
, NULL
, NULL
, &to
) == 1)
584 FD
[0].cb(0, FD
[0].data
);
587 if (now
.tv_sec
> last
.tv_sec
) {
589 dt
= (int) (now
.tv_sec
- start
.tv_sec
);
590 printf("T+ %6d: %9d req (%+4d), %4d conn, %3d/sec avg, %dmb, %dkb/sec avg\n",
595 (int) (nrequests
/ dt
),
596 (int) (total_bytes_read
/ 1024 / 1024),
597 (int) (total_bytes_read
/ 1024 / dt
));
600 * if (dt > process_lifetime)
605 printf("Exiting normally\n");