]>
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
58 #include <sys/socket.h>
61 #include <netinet/in.h>
64 #include <arpa/inet.h>
79 #define PROXY_PORT 3128
80 #define PROXY_ADDR "127.0.0.1"
81 #define READ_BUF_SZ 4096
83 static int proxy_port
= PROXY_PORT
;
84 static char *proxy_addr
= PROXY_ADDR
;
85 static char *progname
;
88 static int opt_ims
= 0;
89 static int opt_range
= 0;
90 static int opt_accel
= 0;
91 static int max_connections
= 64;
92 static time_t lifetime
= 60;
93 static time_t process_lifetime
= 86400;
94 static struct timeval now
;
95 static long long total_bytes_written
= 0;
96 static long long total_bytes_read
= 0;
97 static int opt_checksum
= 0;
98 static char *custom_header
= NULL
;
99 FILE *trace_file
= NULL
;
101 typedef void (CB
) (int, void *);
113 char requestbodyfile
[256];
114 char buf
[READ_BUF_SZ
* 2 + 1];
125 struct _f FD
[SQUID_MAXFD
];
131 free_request(struct _request
*r
)
138 #define RFC1123_STRFTIME "%a, %d %b %Y %H:%M:%S GMT"
143 static char buf
[128];
144 struct tm
*gmt
= gmtime(t
);
146 (void) strftime(buf
, 127, RFC1123_STRFTIME
, gmt
);
155 FD
[fd
].ccb(fd
, FD
[fd
].data
);
161 while (fd
> 0 && FD
[fd
].cb
== NULL
)
168 fd_open(int fd
, CB
* cb
, void *data
, CB
* ccb
)
170 assert(fd
< SQUID_MAXFD
);
174 FD
[fd
].start
= now
.tv_sec
;
185 printf("\rWaiting for open connections to finish...\n");
186 signal(sig
, SIG_DFL
);
190 read_reply(int fd
, void *data
)
192 struct _request
*r
= data
;
193 static unsigned char buf
[READ_BUF_SZ
];
196 if ((len
= read(fd
, buf
, READ_BUF_SZ
)) <= 0) {
202 total_bytes_read
+= len
;
203 if (r
->headfound
< 2) {
204 char *p
, *header
= NULL
;
205 int oldlen
= strlen(r
->buf
);
206 int newlen
= oldlen
+ len
;
207 assert(oldlen
<= READ_BUF_SZ
);
208 memcpy(r
->buf
+ oldlen
, buf
, len
);
209 r
->buf
[newlen
+ 1] = '\0';
210 for (p
= r
->buf
; r
->headfound
< 2 && used
< newlen
; p
++, used
++) {
217 if (strncmp(header
, "HTTP", 4) == 0)
218 r
->status
= atoi(header
+ 8);
219 else if (strncasecmp(header
, "Content-Length:", 15) == 0)
220 r
->content_length
= atoi(header
+ 15);
221 else if (strncasecmp(header
, "X-Request-URI:", 14) == 0) {
223 if (strncmp(r
->url
, header
+ 15, strcspn(header
+ 15, "\r\n"))) {
225 strncpy(url
, header
+ 15, strcspn(header
+ 15, "\r\n"));
226 url
[strcspn(header
+ 15, "\r\n")] = '\n';
227 fprintf(stderr
, "ERROR: Sent %s received %s\n",
243 memmove(r
->buf
, header
, newlen
- (header
- r
->buf
) + 1);
245 assert(used
>= oldlen
);
248 r
->bodysize
+= len
- used
;
250 for (; used
< len
; used
++) {
257 reply_done(int fd
, void *data
)
259 struct _request
*r
= data
;
260 if (opt_range
); /* skip size checks for now */
261 else if (strcmp(r
->method
, "HEAD") == 0);
262 else if (r
->bodysize
!= r
->content_length
&& r
->content_length
>= 0)
263 fprintf(stderr
, "ERROR: %s got %d of %d bytes\n",
264 r
->url
, r
->bodysize
, r
->content_length
);
265 else if (r
->validsize
>= 0) {
266 if (r
->validsize
!= r
->bodysize
)
267 fprintf(stderr
, "WARNING: %s size mismatch wanted %d bytes got %d\n",
268 r
->url
, r
->validsize
, r
->bodysize
);
269 else if (opt_checksum
&& r
->validsum
!= r
->sum
)
270 fprintf(stderr
, "WARNING: %s invalid checksum wanted 0x%lx got 0x%lx\n",
271 r
->url
, r
->validsum
, r
->sum
);
273 if (r
->validstatus
&& r
->status
!= r
->validstatus
) {
274 fprintf(stderr
, "WARNING: %s status mismatch wanted %d got %d\n",
275 r
->url
, r
->validstatus
, r
->status
);
279 fprintf(trace_file
, "%s %s %d %s %d 0x%lx\n",
280 r
->method
, r
->url
, r
->status
, r
->requestbodyfile
, r
->bodysize
, r
->sum
);
282 fprintf(trace_file
, "%s %s %d %s %d\n",
283 r
->method
, r
->url
, r
->status
, r
->requestbodyfile
, r
->bodysize
);
289 request(char *urlin
) {
293 char *method
, *url
, *file
, *size
, *checksum
, *status
;
299 struct sockaddr_in S
;
303 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
307 memset(&S
, '\0', sizeof(struct sockaddr_in
));
308 S
.sin_family
= AF_INET
;
309 S
.sin_port
= htons(proxy_port
);
310 S
.sin_addr
.s_addr
= inet_addr(proxy_addr
);
311 if (connect(s
, (struct sockaddr
*) &S
, sizeof(S
)) < 0) {
316 strcpy(urlbuf
, urlin
);
317 method
= strtok(urlbuf
, " ");
318 url
= strtok(NULL
, " ");
319 status
= strtok(NULL
, " ");
320 file
= strtok(NULL
, " ");
321 size
= strtok(NULL
, " ");
322 checksum
= strtok(NULL
, " ");
333 r
= calloc(1, sizeof *r
);
335 r
->url
= strdup(url
);
337 strcpy(r
->method
, method
);
338 strcpy(r
->requestbodyfile
, file
);
340 if (size
&& strcmp(size
, "-") != 0)
341 r
->validsize
= atoi(size
);
343 r
->validsize
= -1; /* Unknown */
344 if (checksum
&& strcmp(checksum
, "-") != 0)
345 r
->validsum
= strtoul(checksum
, NULL
, 0);
347 r
->validstatus
= atoi(status
);
348 r
->content_length
= -1; /* Unknown */
350 host
= strchr(url
, '/') + 2;
351 url
= strchr(host
, '/');
355 sprintf(msg
, "%s %s HTTP/1.0\r\n", method
, url
);
358 sprintf(buf
, "Host: %s\r\n", host
);
362 strcat(msg
, "Accept: */*\r\n");
363 if (opt_ims
&& (lrand48() & 0x03) == 0) {
364 w
= time(NULL
) - (lrand48() & 0x3FFFF);
365 sprintf(buf
, "If-Modified-Since: %s\r\n", mkrfc850(&w
));
368 if (file
&& strcmp(file
, "-") != 0) {
369 f
= open(file
, O_RDONLY
);
375 sprintf(buf
, "Content-Length: %d\r\n", (int) st
.st_size
);
378 if (opt_range
&& (lrand48() & 0x03) == 0) {
381 strcat(msg
, "Range: bytes=");
382 while (((len
= (int) lrand48()) & 0x03) == 0 || !count
) {
383 const int offset
= (int) lrand48();
386 switch (lrand48() & 0x03) {
388 sprintf(buf
, "-%d", len
);
391 sprintf(buf
, "%d-", offset
);
394 sprintf(buf
, "%d-%d", offset
, offset
+ len
);
402 strcat(msg
, custom_header
);
405 if ((len2
= write(s
, msg
, len
)) != len
) {
407 perror("write request");
411 total_bytes_written
+= len2
;
413 while ((len
= read(f
, buf
, sizeof(buf
))) > 0) {
414 len2
= write(s
, buf
, len
);
416 perror("write body");
427 * if (fcntl(s, F_SETFL, O_NDELAY) < 0)
428 * perror("fcntl O_NDELAY");
434 read_url(int fd
, void *junk
)
437 static char buf
[8192];
440 if (fgets(buf
, 8191, stdin
) == NULL
) {
441 printf("Done Reading URLS\n");
448 if ((t
= strchr(buf
, '\n')))
452 max_connections
= nfds
- 1;
453 printf("NOTE: max_connections set at %d\n", max_connections
);
455 fd_open(r
->fd
, read_reply
, r
, reply_done
);
462 fprintf(stderr
, "usage: %s: -p port -h host -n max\n", progname
);
463 fprintf(stderr
, " -t <tracefile> Save request trace\n");
464 fprintf(stderr
, " -c Check checksum agains trace\n");
465 fprintf(stderr
, " -i Send random If-Modified-Since times\n");
466 fprintf(stderr
, " -l <seconds> Connection lifetime timeout (default 60)\n");
467 fprintf(stderr
, " -a Accelerator mode\n");
468 fprintf(stderr
, " -H <string> Custom header\n");
481 struct timeval start
;
484 setbuf(stdout
, NULL
);
485 setbuf(stderr
, NULL
);
486 progname
= strdup(argv
[0]);
487 gettimeofday(&now
, NULL
);
489 custom_header
= strdup("");
490 while ((c
= getopt(argc
, argv
, "ap:h:H:n:icrl:L:t:")) != -1) {
496 proxy_port
= atoi(optarg
);
499 proxy_addr
= strdup(optarg
);
502 max_connections
= atoi(optarg
);
508 lifetime
= (time_t) atoi(optarg
);
511 process_lifetime
= (time_t) atoi(optarg
);
517 trace_file
= fopen(optarg
, "a");
519 setbuf(trace_file
, NULL
);
525 custom_header
= realloc(custom_header
, strlen(custom_header
) + strlen(optarg
) + 2 + 1);
526 strcat(custom_header
, optarg
);
527 strcat(custom_header
, "\r\n");
534 fd_open(0, read_url
, NULL
, NULL
);
536 signal(SIGINT
, sig_intr
);
537 signal(SIGPIPE
, SIG_IGN
);
539 while (nfds
|| FD
[0].cb
) {
543 if (nfds
< max_connections
&& FD
[0].cb
)
545 for (i
= 1; i
<= maxfd
; i
++) {
546 if (FD
[i
].cb
== NULL
)
548 if (now
.tv_sec
- FD
[i
].start
> lifetime
) {
549 fprintf(stderr
, "WARNING: fd %d lifetime timeout\n", i
);
555 if (select(maxfd
+ 1, &R
, NULL
, NULL
, &to
) < 0) {
556 fprintf(stderr
, "maxfd=%d\n", maxfd
);
561 gettimeofday(&now
, NULL
);
562 for (i
= 0; i
<= maxfd
; i
++) {
563 if (!FD_ISSET(i
, &R
))
565 FD
[i
].cb(i
, FD
[i
].data
);
566 if (nfds
< max_connections
&& FD
[0].cb
) {
571 if (select(1, &R2
, NULL
, NULL
, &to
) == 1)
572 FD
[0].cb(0, FD
[0].data
);
575 if (now
.tv_sec
> last
.tv_sec
) {
577 dt
= (int) (now
.tv_sec
- start
.tv_sec
);
578 printf("T+ %6d: %9d req (%+4d), %4d conn, %3d/sec avg, %dmb, %dkb/sec avg\n",
583 (int) (nrequests
/ dt
),
584 (int) (total_bytes_read
/ 1024 / 1024),
585 (int) (total_bytes_read
/ 1024 / dt
));
588 * if (dt > process_lifetime)
593 printf("Exiting normally\n");