]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/tcp-banger2.c
4 * On some systems, FD_SETSIZE is set to something lower than the
5 * actual number of files which can be opened. IRIX is one case,
6 * NetBSD is another. So here we increase FD_SETSIZE to our
7 * configure-discovered maximum *before* any system includes.
9 #define CHANGE_FD_SETSIZE 1
11 /* Cannot increase FD_SETSIZE on Linux */
12 #if defined(_SQUID_LINUX_)
13 #undef CHANGE_FD_SETSIZE
14 #define CHANGE_FD_SETSIZE 0
17 /* Cannot increase FD_SETSIZE on FreeBSD before 2.2.0, causes select(2)
18 * to return EINVAL. */
19 /* Marian Durkovic <marian@svf.stuba.sk> */
20 /* Peter Wemm <peter@spinner.DIALix.COM> */
21 #if defined(_SQUID_FREEBSD_)
22 #include <osreldate.h>
23 #if __FreeBSD_version < 220000
24 #undef CHANGE_FD_SETSIZE
25 #define CHANGE_FD_SETSIZE 0
29 /* Increase FD_SETSIZE if SQUID_MAXFD is bigger */
30 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
31 #define FD_SETSIZE SQUID_MAXFD
53 #include <sys/types.h>
56 #include <sys/select.h>
68 #include <sys/socket.h>
71 #include <netinet/in.h>
74 #include <arpa/inet.h>
86 #define PROXY_PORT 3128
87 #define PROXY_ADDR "127.0.0.1"
88 #define READ_BUF_SZ 4096
90 static int proxy_port
= PROXY_PORT
;
91 static char *proxy_addr
= PROXY_ADDR
;
92 static char *progname
;
95 static int opt_ims
= 0;
96 static int opt_range
= 0;
97 static int max_connections
= 64;
98 static time_t lifetime
= 60;
99 static time_t process_lifetime
= 86400;
100 static struct timeval now
;
101 static long total_bytes_written
= 0;
102 static long total_bytes_read
= 0;
103 static int opt_checksum
= 0;
104 FILE *trace_file
= NULL
;
106 typedef void (CB
) (int, void *);
118 char requestbodyfile
[256];
119 char buf
[READ_BUF_SZ
* 2 + 1];
128 struct _f FD
[SQUID_MAXFD
];
137 static char buf
[128];
138 struct tm
*gmt
= gmtime(t
);
140 (void) strftime(buf
, 127, "%A, %d-%b-%y %H:%M:%S GMT", gmt
);
149 FD
[fd
].ccb(fd
, FD
[fd
].data
);
155 while (fd
> 0 && FD
[fd
].cb
== NULL
)
162 fd_open(int fd
, CB
* cb
, void *data
, CB
* ccb
)
164 assert(fd
< SQUID_MAXFD
);
168 FD
[fd
].start
= now
.tv_sec
;
179 printf("\rWaiting for open connections to finish...\n");
180 signal(sig
, SIG_DFL
);
184 read_reply(int fd
, void *data
)
186 struct _request
*r
= data
;
187 static unsigned char buf
[READ_BUF_SZ
];
189 if ((len
= read(fd
, buf
, READ_BUF_SZ
)) <= 0) {
195 total_bytes_read
+= len
;
196 if (r
->headfound
< 2) {
197 char *p
, *header
= NULL
;
198 int oldlen
= strlen(r
->buf
);
199 int newlen
= oldlen
+ len
;
200 assert(oldlen
<= READ_BUF_SZ
);
201 memcpy(r
->buf
+ oldlen
, buf
, len
);
202 r
->buf
[newlen
+ 1] = '\0';
203 for (p
= r
->buf
; r
->headfound
< 2 && used
< newlen
; p
++, used
++) {
209 if (strncasecmp(header
, "Content-Length:", 15) == 0)
210 r
->content_length
= atoi(header
+ 15);
211 if (strncasecmp(header
, "X-Request-URI:", 14) == 0) {
213 if (strncmp(r
->url
, header
+ 15, strcspn(header
+ 15, "\r\n"))) {
215 strncpy(url
, header
+ 15, strcspn(header
+ 15, "\r\n"));
216 url
[strcspn(header
+ 15, "\r\n")] = '\n';
217 fprintf(stderr
, "ERROR: Sent %s received %s\n",
234 memmove(r
->buf
, header
, newlen
- (header
- r
->buf
) + 1);
236 assert(used
>= oldlen
);
239 r
->bodysize
+= len
- used
;
241 for (; used
< len
; used
++) {
249 reply_done(int fd
, void *data
)
251 struct _request
*r
= data
;
252 if (opt_range
); /* skip size checks for now */
253 else if (r
->bodysize
!= r
->content_length
&& r
->content_length
>= 0)
254 fprintf(stderr
, "ERROR: %s got %d of %d bytes\n",
255 r
->url
, r
->bodysize
, r
->content_length
);
256 else if (r
->validsize
>= 0) {
257 if (r
->validsize
!= r
->bodysize
)
258 fprintf(stderr
, "WARNING: %s size mismatch wanted %d bytes got %d\n",
259 r
->url
, r
->validsize
, r
->bodysize
);
260 else if (opt_checksum
&& r
->validsum
!= r
->sum
)
261 fprintf(stderr
, "WARNING: %s invalid checksum wanted 0x%lx got 0x%lx\n",
262 r
->url
, r
->validsum
, r
->sum
);
265 fprintf(trace_file
, "%s %s %s %d 0x%lx\n",
266 r
->method
, r
->url
, r
->requestbodyfile
, r
->bodysize
, r
->sum
);
277 char *method
, *url
, *file
, *size
, *checksum
;
282 struct sockaddr_in S
;
286 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
290 memset(&S
, '\0', sizeof(struct sockaddr_in
));
291 S
.sin_family
= AF_INET
;
292 S
.sin_port
= htons(proxy_port
);
293 S
.sin_addr
.s_addr
= inet_addr(proxy_addr
);
294 if (connect(s
, (struct sockaddr
*) &S
, sizeof(S
)) < 0) {
299 strcpy(urlbuf
, urlin
);
300 method
= strtok(urlbuf
, " ");
301 url
= strtok(NULL
, " ");
302 file
= strtok(NULL
, " ");
303 size
= strtok(NULL
, " ");
304 checksum
= strtok(NULL
, " ");
315 r
= calloc(1, sizeof *r
);
318 strcpy(r
->method
, method
);
319 strcpy(r
->requestbodyfile
, file
);
321 if (size
&& strcmp(size
, "-") != 0)
322 r
->validsize
= atoi(size
);
324 r
->validsize
= -1; /* Unknown */
325 if (checksum
&& strcmp(checksum
, "-") != 0)
326 r
->validsum
= strtoul(checksum
, NULL
, 0);
327 r
->content_length
= -1; /* Unknown */
329 sprintf(buf
, "%s %s HTTP/1.0\r\n", method
, url
);
331 strcat(msg
, "Accept: */*\r\n");
332 if (opt_ims
&& (lrand48() & 0x03) == 0) {
333 w
= time(NULL
) - (lrand48() & 0x3FFFF);
334 sprintf(buf
, "If-Modified-Since: %s\r\n", mkrfc850(&w
));
337 if (file
&& strcmp(file
, "-") != 0) {
338 f
= open(file
, O_RDONLY
);
344 sprintf(buf
, "Content-Length: %d\r\n", (int) st
.st_size
);
347 if (opt_range
&& (lrand48() & 0x03) == 0) {
350 strcat(msg
, "Range: bytes=");
351 while (((len
= (int) lrand48()) & 0x03) == 0 || !count
) {
352 const int offset
= (int) lrand48();
355 switch (lrand48() & 0x03) {
357 sprintf(buf
, "-%d", len
);
360 sprintf(buf
, "%d-", offset
);
363 sprintf(buf
, "%d-%d", offset
, offset
+ len
);
373 if ((len2
= write(s
, msg
, len
)) != len
) {
375 perror("write request");
379 total_bytes_written
+= len2
;
381 while ((len
= read(f
, buf
, sizeof(buf
))) > 0) {
382 len2
= write(s
, buf
, len
);
384 perror("write body");
395 * if (fcntl(s, F_SETFL, O_NDELAY) < 0)
396 * perror("fcntl O_NDELAY");
402 read_url(int fd
, void *junk
)
405 static char buf
[8192];
408 if (fgets(buf
, 8191, stdin
) == NULL
) {
409 printf("Done Reading URLS\n");
416 if ((t
= strchr(buf
, '\n')))
420 max_connections
= nfds
- 1;
421 printf("NOTE: max_connections set at %d\n", max_connections
);
423 fd_open(r
->fd
, read_reply
, r
, reply_done
);
430 fprintf(stderr
, "usage: %s: -p port -h host -n max\n", progname
);
431 fprintf(stderr
, " -t <tracefile> Save request trace\n");
432 fprintf(stderr
, " -c Check checksum agains trace\n");
433 fprintf(stderr
, " -i Send random If-Modified-Since times\n");
434 fprintf(stderr
, " -l <seconds> Connection lifetime timeout (default 60)\n");
447 struct timeval start
;
450 setbuf(stdout
, NULL
);
451 setbuf(stderr
, NULL
);
452 progname
= strdup(argv
[0]);
453 gettimeofday(&now
, NULL
);
455 while ((c
= getopt(argc
, argv
, "p:h:n:icrl:L:t:")) != -1) {
458 proxy_port
= atoi(optarg
);
461 proxy_addr
= strdup(optarg
);
464 max_connections
= atoi(optarg
);
470 lifetime
= (time_t) atoi(optarg
);
473 process_lifetime
= (time_t) atoi(optarg
);
479 opt_checksum
= 1; /* Tracing requires checksums */
480 trace_file
= fopen(optarg
, "w");
491 fd_open(0, read_url
, NULL
, NULL
);
493 signal(SIGINT
, sig_intr
);
494 signal(SIGPIPE
, SIG_IGN
);
496 while (nfds
|| FD
[0].cb
) {
500 if (nfds
< max_connections
&& FD
[0].cb
)
502 for (i
= 1; i
<= maxfd
; i
++) {
503 if (FD
[i
].cb
== NULL
)
505 if (now
.tv_sec
- FD
[i
].start
> lifetime
) {
506 fprintf(stderr
, "WARNING: fd %d lifetime timeout\n", i
);
512 if (select(maxfd
+ 1, &R
, NULL
, NULL
, &to
) < 0) {
513 fprintf(stderr
, "maxfd=%d\n", maxfd
);
518 gettimeofday(&now
, NULL
);
519 for (i
= 0; i
<= maxfd
; i
++) {
520 if (!FD_ISSET(i
, &R
))
522 FD
[i
].cb(i
, FD
[i
].data
);
523 if (nfds
< max_connections
&& FD
[0].cb
) {
528 if (select(1, &R2
, NULL
, NULL
, &to
) == 1)
529 FD
[0].cb(0, FD
[0].data
);
532 if (now
.tv_sec
> last
.tv_sec
) {
534 dt
= (int) (now
.tv_sec
- start
.tv_sec
);
535 printf("T+ %6d: %9d req (%+4d), %4d conn, %3d/sec avg, %dmb, %dkb/sec avg\n",
540 (int) (nrequests
/ dt
),
541 (int) total_bytes_read
/ 1024 / 1024,
542 (int) total_bytes_read
/ 1024 / dt
);
544 if (dt
> process_lifetime
)