]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/tcp-banger2.c
3 /* $Id: tcp-banger2.c,v 1.24 2003/01/23 00:38:34 robertc Exp $ */
5 /* Increase FD_SETSIZE if SQUID_MAXFD is bigger */
6 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
7 #define FD_SETSIZE SQUID_MAXFD
29 #include <sys/types.h>
32 #include <sys/select.h>
44 #include <sys/socket.h>
47 #include <netinet/in.h>
50 #include <arpa/inet.h>
65 #define PROXY_PORT 3128
66 #define PROXY_ADDR "127.0.0.1"
67 #define READ_BUF_SZ 4096
69 static int proxy_port
= PROXY_PORT
;
70 static char *proxy_addr
= PROXY_ADDR
;
71 static char *progname
;
74 static int opt_ims
= 0;
75 static int opt_range
= 0;
76 static int opt_accel
= 0;
77 static int max_connections
= 64;
78 static time_t lifetime
= 60;
79 static time_t process_lifetime
= 86400;
80 static struct timeval now
;
81 static long total_bytes_written
= 0;
82 static long total_bytes_read
= 0;
83 static int opt_checksum
= 0;
84 FILE *trace_file
= NULL
;
86 typedef void (CB
) (int, void *);
98 char requestbodyfile
[256];
99 char buf
[READ_BUF_SZ
* 2 + 1];
109 struct _f FD
[SQUID_MAXFD
];
115 free_request(struct _request
*r
)
122 #define RFC1123_STRFTIME "%a, %d %b %Y %H:%M:%S GMT"
127 static char buf
[128];
128 struct tm
*gmt
= gmtime(t
);
130 (void) strftime(buf
, 127, RFC1123_STRFTIME
, gmt
);
139 FD
[fd
].ccb(fd
, FD
[fd
].data
);
145 while (fd
> 0 && FD
[fd
].cb
== NULL
)
152 fd_open(int fd
, CB
* cb
, void *data
, CB
* ccb
)
154 assert(fd
< SQUID_MAXFD
);
158 FD
[fd
].start
= now
.tv_sec
;
169 printf("\rWaiting for open connections to finish...\n");
170 signal(sig
, SIG_DFL
);
174 read_reply(int fd
, void *data
)
176 struct _request
*r
= data
;
177 static unsigned char buf
[READ_BUF_SZ
];
180 if ((len
= read(fd
, buf
, READ_BUF_SZ
)) <= 0) {
186 total_bytes_read
+= len
;
187 if (r
->headfound
< 2) {
188 char *p
, *header
= NULL
;
189 int oldlen
= strlen(r
->buf
);
190 int newlen
= oldlen
+ len
;
191 assert(oldlen
<= READ_BUF_SZ
);
192 memcpy(r
->buf
+ oldlen
, buf
, len
);
193 r
->buf
[newlen
+ 1] = '\0';
194 for (p
= r
->buf
; r
->headfound
< 2 && used
< newlen
; p
++, used
++) {
201 if (strncmp(header
, "HTTP", 4) == 0)
202 r
->status
= atoi(header
+ 8);
203 else if (strncasecmp(header
, "Content-Length:", 15) == 0)
204 r
->content_length
= atoi(header
+ 15);
205 else if (strncasecmp(header
, "X-Request-URI:", 14) == 0) {
207 if (strncmp(r
->url
, header
+ 15, strcspn(header
+ 15, "\r\n"))) {
209 strncpy(url
, header
+ 15, strcspn(header
+ 15, "\r\n"));
210 url
[strcspn(header
+ 15, "\r\n")] = '\n';
211 fprintf(stderr
, "ERROR: Sent %s received %s\n",
227 memmove(r
->buf
, header
, newlen
- (header
- r
->buf
) + 1);
229 assert(used
>= oldlen
);
232 r
->bodysize
+= len
- used
;
234 for (; used
< len
; used
++) {
241 reply_done(int fd
, void *data
)
243 struct _request
*r
= data
;
244 if (opt_range
); /* skip size checks for now */
245 else if (r
->bodysize
!= r
->content_length
&& r
->content_length
>= 0)
246 fprintf(stderr
, "ERROR: %s got %d of %d bytes\n",
247 r
->url
, r
->bodysize
, r
->content_length
);
248 else if (r
->validsize
>= 0) {
249 if (r
->validsize
!= r
->bodysize
)
250 fprintf(stderr
, "WARNING: %s size mismatch wanted %d bytes got %d\n",
251 r
->url
, r
->validsize
, r
->bodysize
);
252 else if (opt_checksum
&& r
->validsum
!= r
->sum
)
253 fprintf(stderr
, "WARNING: %s invalid checksum wanted 0x%lx got 0x%lx\n",
254 r
->url
, r
->validsum
, r
->sum
);
258 fprintf(trace_file
, "%s %s %d %s %d 0x%lx\n",
259 r
->method
, r
->url
, r
->status
, r
->requestbodyfile
, r
->bodysize
, r
->sum
);
261 fprintf(trace_file
, "%s %s %d %s %d\n",
262 r
->method
, r
->url
, r
->status
, r
->requestbodyfile
, r
->bodysize
);
273 char *method
, *url
, *file
, *size
, *checksum
;
279 struct sockaddr_in S
;
283 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
287 memset(&S
, '\0', sizeof(struct sockaddr_in
));
288 S
.sin_family
= AF_INET
;
289 S
.sin_port
= htons(proxy_port
);
290 S
.sin_addr
.s_addr
= inet_addr(proxy_addr
);
291 if (connect(s
, (struct sockaddr
*) &S
, sizeof(S
)) < 0) {
296 strcpy(urlbuf
, urlin
);
297 method
= strtok(urlbuf
, " ");
298 url
= strtok(NULL
, " ");
299 file
= strtok(NULL
, " ");
300 size
= strtok(NULL
, " ");
301 checksum
= strtok(NULL
, " ");
312 r
= calloc(1, sizeof *r
);
314 r
->url
= strdup(url
);
316 strcpy(r
->method
, method
);
317 strcpy(r
->requestbodyfile
, file
);
319 if (size
&& strcmp(size
, "-") != 0)
320 r
->validsize
= atoi(size
);
322 r
->validsize
= -1; /* Unknown */
323 if (checksum
&& strcmp(checksum
, "-") != 0)
324 r
->validsum
= strtoul(checksum
, NULL
, 0);
325 r
->content_length
= -1; /* Unknown */
327 host
= strchr(url
, '/') + 2;
328 url
= strchr(host
, '/');
332 sprintf(msg
, "%s %s HTTP/1.0\r\n", method
, url
);
335 sprintf(buf
, "Host: %s\r\n", host
);
339 strcat(msg
, "Accept: */*\r\n");
340 if (opt_ims
&& (lrand48() & 0x03) == 0) {
341 w
= time(NULL
) - (lrand48() & 0x3FFFF);
342 sprintf(buf
, "If-Modified-Since: %s\r\n", mkrfc1123(&w
));
345 if (file
&& strcmp(file
, "-") != 0) {
346 f
= open(file
, O_RDONLY
);
352 sprintf(buf
, "Content-Length: %d\r\n", (int) st
.st_size
);
355 if (opt_range
&& (lrand48() & 0x03) == 0) {
358 strcat(msg
, "Range: bytes=");
359 while (((len
= (int) lrand48()) & 0x03) == 0 || !count
) {
360 const int offset
= (int) lrand48();
363 switch (lrand48() & 0x03) {
365 sprintf(buf
, "-%d", len
);
368 sprintf(buf
, "%d-", offset
);
371 sprintf(buf
, "%d-%d", offset
, offset
+ len
);
381 if ((len2
= write(s
, msg
, len
)) != len
) {
383 perror("write request");
387 total_bytes_written
+= len2
;
389 while ((len
= read(f
, buf
, sizeof(buf
))) > 0) {
390 len2
= write(s
, buf
, len
);
392 perror("write body");
403 * if (fcntl(s, F_SETFL, O_NDELAY) < 0)
404 * perror("fcntl O_NDELAY");
410 read_url(int fd
, void *junk
)
413 static char buf
[8192];
416 if (fgets(buf
, 8191, stdin
) == NULL
) {
417 printf("Done Reading URLS\n");
424 if ((t
= strchr(buf
, '\n')))
428 max_connections
= nfds
- 1;
429 printf("NOTE: max_connections set at %d\n", max_connections
);
431 fd_open(r
->fd
, read_reply
, r
, reply_done
);
438 fprintf(stderr
, "usage: %s: -p port -h host -n max\n", progname
);
439 fprintf(stderr
, " -t <tracefile> Save request trace\n");
440 fprintf(stderr
, " -c Check checksum agains trace\n");
441 fprintf(stderr
, " -i Send random If-Modified-Since times\n");
442 fprintf(stderr
, " -l <seconds> Connection lifetime timeout (default 60)\n");
443 fprintf(stderr
, " -a Accelerator mode\n");
456 struct timeval start
;
459 setbuf(stdout
, NULL
);
460 setbuf(stderr
, NULL
);
461 progname
= strdup(argv
[0]);
462 gettimeofday(&now
, NULL
);
464 while ((c
= getopt(argc
, argv
, "ap:h:n:icrl:L:t:")) != -1) {
470 proxy_port
= atoi(optarg
);
473 proxy_addr
= strdup(optarg
);
476 max_connections
= atoi(optarg
);
482 lifetime
= (time_t) atoi(optarg
);
485 process_lifetime
= (time_t) atoi(optarg
);
491 trace_file
= fopen(optarg
, "a");
493 setbuf(trace_file
, NULL
);
503 fd_open(0, read_url
, NULL
, NULL
);
505 signal(SIGINT
, sig_intr
);
506 signal(SIGPIPE
, SIG_IGN
);
508 while (nfds
|| FD
[0].cb
) {
512 if (nfds
< max_connections
&& FD
[0].cb
)
514 for (i
= 1; i
<= maxfd
; i
++) {
515 if (FD
[i
].cb
== NULL
)
517 if (now
.tv_sec
- FD
[i
].start
> lifetime
) {
518 fprintf(stderr
, "WARNING: fd %d lifetime timeout\n", i
);
524 if (select(maxfd
+ 1, &R
, NULL
, NULL
, &to
) < 0) {
525 fprintf(stderr
, "maxfd=%d\n", maxfd
);
530 gettimeofday(&now
, NULL
);
531 for (i
= 0; i
<= maxfd
; i
++) {
532 if (!FD_ISSET(i
, &R
))
534 FD
[i
].cb(i
, FD
[i
].data
);
535 if (nfds
< max_connections
&& FD
[0].cb
) {
540 if (select(1, &R2
, NULL
, NULL
, &to
) == 1)
541 FD
[0].cb(0, FD
[0].data
);
544 if (now
.tv_sec
> last
.tv_sec
) {
546 dt
= (int) (now
.tv_sec
- start
.tv_sec
);
547 printf("T+ %6d: %9d req (%+4d), %4d conn, %3d/sec avg, %dmb, %dkb/sec avg\n",
552 (int) (nrequests
/ dt
),
553 (int) total_bytes_read
/ 1024 / 1024,
554 (int) total_bytes_read
/ 1024 / dt
);
557 * if (dt > process_lifetime)
562 printf("Exiting normally\n");