]> git.ipfire.org Git - thirdparty/squid.git/blame - test-suite/tcp-banger2.c
Portability: log_file_daemon compile dependencies
[thirdparty/squid.git] / test-suite / tcp-banger2.c
CommitLineData
58a96f63 1#include "config.h"
6a54c60e 2
262a0e14 3/* $Id$
56792124 4
5/*
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.
10 */
11#define CHANGE_FD_SETSIZE 1
12
13/* Cannot increase FD_SETSIZE on Linux */
14#if defined(_SQUID_LINUX_)
15#undef CHANGE_FD_SETSIZE
16#define CHANGE_FD_SETSIZE 0
17#endif
18
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
28#endif
29#endif
58a96f63 30
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
34#endif
35
36#if HAVE_UNISTD_H
735246e8 37#include <unistd.h>
58a96f63 38#endif
58a96f63 39#if HAVE_STDIO_H
40#include <stdio.h>
41#endif
42#if HAVE_FCNTL_H
735246e8 43#include <fcntl.h>
58a96f63 44#endif
32d002cb 45#if HAVE_STRING_H
735246e8 46#include <string.h>
58a96f63 47#endif
32d002cb 48#if HAVE_STRINGS_H
735246e8 49#include <strings.h>
58a96f63 50#endif
58a96f63 51#if HAVE_SIGNAL_H
735246e8 52#include <signal.h>
58a96f63 53#endif
54#if HAVE_TIME_H
735246e8 55#include <time.h>
58a96f63 56#endif
58a96f63 57#if HAVE_SYS_SOCKET_H
735246e8 58#include <sys/socket.h>
58a96f63 59#endif
60#if HAVE_NETINET_IN_H
735246e8 61#include <netinet/in.h>
58a96f63 62#endif
63#if HAVE_ARPA_INET_H
735246e8 64#include <arpa/inet.h>
58a96f63 65#endif
66#if HAVE_ERRNO_H
735246e8 67#include <errno.h>
58a96f63 68#endif
14ff08f1 69#if HAVE_SYS_STAT_H
70#include <sys/stat.h>
71#endif
72#if HAVE_ASSERT_H
73#include <assert.h>
74#endif
b6a2f15e 75#if HAVE_CTYPE_H
76#include <ctype.h>
77#endif
735246e8 78
79#define PROXY_PORT 3128
80#define PROXY_ADDR "127.0.0.1"
735246e8 81#define READ_BUF_SZ 4096
82
83static int proxy_port = PROXY_PORT;
84static char *proxy_addr = PROXY_ADDR;
85static char *progname;
86static int reqpersec;
87static int nrequests;
88static int opt_ims = 0;
c4248266 89static int opt_range = 0;
6f821399 90static int opt_accel = 0;
735246e8 91static int max_connections = 64;
33fda8cd 92static time_t lifetime = 60;
15dd95c1 93static time_t process_lifetime = 86400;
33fda8cd 94static struct timeval now;
56792124 95static long long total_bytes_written = 0;
96static long long total_bytes_read = 0;
14ff08f1 97static int opt_checksum = 0;
56792124 98static char *custom_header = NULL;
ed2ab7db 99FILE *trace_file = NULL;
735246e8 100
101typedef void (CB) (int, void *);
102
103struct _f {
104 CB *cb;
14ff08f1 105 CB *ccb;
735246e8 106 void *data;
33fda8cd 107 time_t start;
735246e8 108};
14ff08f1 109struct _request {
110 int fd;
7d97d03c 111 char *url;
ed2ab7db 112 char method[16];
113 char requestbodyfile[256];
14ff08f1 114 char buf[READ_BUF_SZ * 2 + 1];
115 int headfound;
2444b689 116 int validsize;
117 int bodysize;
118 int content_length;
55095043 119 int status;
14ff08f1 120 long validsum;
14ff08f1 121 long sum;
56792124 122 int validstatus;
14ff08f1 123};
735246e8 124
b61d6f3d 125struct _f FD[SQUID_MAXFD];
735246e8 126int nfds = 0;
127int maxfd = 0;
128
129
7d97d03c 130static void
131free_request(struct _request *r)
132{
55095043 133 if (r->url)
26ac0430 134 free(r->url);
55095043 135 free(r);
7d97d03c 136}
137
528b2c61 138#define RFC1123_STRFTIME "%a, %d %b %Y %H:%M:%S GMT"
735246e8 139char *
528b2c61 140mkrfc1123(t)
26ac0430 141time_t *t;
735246e8 142{
143 static char buf[128];
144 struct tm *gmt = gmtime(t);
145 buf[0] = '\0';
528b2c61 146 (void) strftime(buf, 127, RFC1123_STRFTIME, gmt);
735246e8 147 return buf;
148}
149
150void
151fd_close(int fd)
152{
153 close(fd);
14ff08f1 154 if (FD[fd].ccb)
26ac0430 155 FD[fd].ccb(fd, FD[fd].data);
14ff08f1 156 FD[fd].ccb = NULL;
735246e8 157 FD[fd].cb = NULL;
158 FD[fd].data = NULL;
159 nfds--;
160 if (fd == maxfd) {
26ac0430
AJ
161 while (fd > 0 && FD[fd].cb == NULL)
162 fd--;
163 maxfd = fd;
735246e8 164 }
165}
166
167void
3cd80ef8 168fd_open(int fd, CB * cb, void *data, CB * ccb)
735246e8 169{
b61d6f3d 170 assert(fd < SQUID_MAXFD);
735246e8 171 FD[fd].cb = cb;
14ff08f1 172 FD[fd].ccb = ccb;
735246e8 173 FD[fd].data = data;
33fda8cd 174 FD[fd].start = now.tv_sec;
735246e8 175 if (fd > maxfd)
26ac0430 176 maxfd = fd;
735246e8 177 nfds++;
178}
179
180void
181sig_intr(int sig)
182{
6a54c60e 183 fd_close(0);
14ff08f1 184 nfds++;
6a54c60e 185 printf("\rWaiting for open connections to finish...\n");
186 signal(sig, SIG_DFL);
735246e8 187}
188
6a54c60e 189void
735246e8 190read_reply(int fd, void *data)
191{
14ff08f1 192 struct _request *r = data;
193 static unsigned char buf[READ_BUF_SZ];
194 int len;
1707acbe 195 int used = 0;
3cd80ef8 196 if ((len = read(fd, buf, READ_BUF_SZ)) <= 0) {
26ac0430
AJ
197 fd_close(fd);
198 reqpersec++;
199 nrequests++;
200 return;
1707acbe 201 }
202 total_bytes_read += len;
203 if (r->headfound < 2) {
26ac0430
AJ
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++) {
211 switch (*p) {
212 case '\n':
213 r->headfound++;
214 if (!header)
215 break;
216 /* Decode header */
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) {
222 /* Check URI */
223 if (strncmp(r->url, header + 15, strcspn(header + 15, "\r\n"))) {
224 char url[8192];
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",
228 r->url, url);
229 }
230 }
231 header = NULL;
232 break;
233 case '\r':
234 break;
235 default:
236 r->headfound = 0;
237 if (!header)
238 header = p;
239 break;
240 }
241 }
242 if (header) {
243 memmove(r->buf, header, newlen - (header - r->buf) + 1);
244 }
245 assert(used >= oldlen);
246 used -= oldlen;
1707acbe 247 }
248 r->bodysize += len - used;
249 if (opt_checksum) {
26ac0430
AJ
250 for (; used < len; used++) {
251 r->sum += buf[used];
252 }
6a54c60e 253 }
735246e8 254}
255
14ff08f1 256void
257reply_done(int fd, void *data)
735246e8 258{
14ff08f1 259 struct _request *r = data;
3cd80ef8 260 if (opt_range); /* skip size checks for now */
56792124 261 else if (strcmp(r->method, "HEAD") == 0);
2444b689 262 else if (r->bodysize != r->content_length && r->content_length >= 0)
26ac0430
AJ
263 fprintf(stderr, "ERROR: %s got %d of %d bytes\n",
264 r->url, r->bodysize, r->content_length);
14ff08f1 265 else if (r->validsize >= 0) {
26ac0430
AJ
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);
ed2ab7db 272 }
56792124 273 if (r->validstatus && r->status != r->validstatus) {
26ac0430
AJ
274 fprintf(stderr, "WARNING: %s status mismatch wanted %d got %d\n",
275 r->url, r->validstatus, r->status);
56792124 276 }
ed2ab7db 277 if (trace_file) {
26ac0430
AJ
278 if (opt_checksum)
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);
281 else
282 fprintf(trace_file, "%s %s %d %s %d\n",
283 r->method, r->url, r->status, r->requestbodyfile, r->bodysize);
14ff08f1 284 }
7d97d03c 285 free_request(r);
14ff08f1 286}
287
288struct _request *
e1381638 289request(char *urlin) {
3cd80ef8 290 int s = -1, f = -1;
735246e8 291 char buf[4096];
14ff08f1 292 char msg[8192];
56792124 293 char *method, *url, *file, *size, *checksum, *status;
6f821399 294 char *host;
14ff08f1 295 char urlbuf[8192];
3cd80ef8 296 int len, len2;
735246e8 297 time_t w;
14ff08f1 298 struct stat st;
735246e8 299 struct sockaddr_in S;
14ff08f1 300 struct _request *r;
206b24db 301 if (*urlin == '\0')
26ac0430 302 return NULL;
735246e8 303 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
26ac0430
AJ
304 perror("socket");
305 return NULL;
735246e8 306 }
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);
6a54c60e 311 if (connect(s, (struct sockaddr *) &S, sizeof(S)) < 0) {
26ac0430
AJ
312 close(s);
313 perror("connect");
314 return NULL;
735246e8 315 }
3cd80ef8 316 strcpy(urlbuf, urlin);
317 method = strtok(urlbuf, " ");
318 url = strtok(NULL, " ");
56792124 319 status = strtok(NULL, " ");
3cd80ef8 320 file = strtok(NULL, " ");
321 size = strtok(NULL, " ");
322 checksum = strtok(NULL, " ");
14ff08f1 323 if (!url) {
26ac0430
AJ
324 url = method;
325 method = "GET";
14ff08f1 326 }
ed2ab7db 327 if (!file)
26ac0430 328 file = "-";
ed2ab7db 329 if (!size)
26ac0430 330 size = "-";
ed2ab7db 331 if (!checksum)
26ac0430 332 checksum = "-";
3cd80ef8 333 r = calloc(1, sizeof *r);
334 assert(r != NULL);
7d97d03c 335 r->url = strdup(url);
336 assert(r->url);
ed2ab7db 337 strcpy(r->method, method);
338 strcpy(r->requestbodyfile, file);
14ff08f1 339 r->fd = s;
3cd80ef8 340 if (size && strcmp(size, "-") != 0)
26ac0430 341 r->validsize = atoi(size);
14ff08f1 342 else
26ac0430 343 r->validsize = -1; /* Unknown */
3cd80ef8 344 if (checksum && strcmp(checksum, "-") != 0)
26ac0430 345 r->validsum = strtoul(checksum, NULL, 0);
56792124 346 if (status)
26ac0430 347 r->validstatus = atoi(status);
3cd80ef8 348 r->content_length = -1; /* Unknown */
6f821399 349 if (opt_accel) {
26ac0430
AJ
350 host = strchr(url, '/') + 2;
351 url = strchr(host, '/');
6f821399 352 } else {
26ac0430 353 host = NULL;
6f821399 354 }
355 sprintf(msg, "%s %s HTTP/1.0\r\n", method, url);
356 if (host) {
26ac0430
AJ
357 url[0] = '\0';
358 sprintf(buf, "Host: %s\r\n", host);
359 strcat(msg, buf);
360 url[0] = '/';
6f821399 361 }
14ff08f1 362 strcat(msg, "Accept: */*\r\n");
735246e8 363 if (opt_ims && (lrand48() & 0x03) == 0) {
26ac0430
AJ
364 w = time(NULL) - (lrand48() & 0x3FFFF);
365 sprintf(buf, "If-Modified-Since: %s\r\n", mkrfc850(&w));
366 strcat(msg, buf);
735246e8 367 }
3cd80ef8 368 if (file && strcmp(file, "-") != 0) {
26ac0430
AJ
369 f = open(file, O_RDONLY);
370 if (f < 0) {
371 perror("open file");
372 exit(1);
373 }
374 fstat(f, &st);
375 sprintf(buf, "Content-Length: %d\r\n", (int) st.st_size);
376 strcat(msg, buf);
14ff08f1 377 }
c4248266 378 if (opt_range && (lrand48() & 0x03) == 0) {
26ac0430
AJ
379 int len;
380 int count = 0;
381 strcat(msg, "Range: bytes=");
382 while (((len = (int) lrand48()) & 0x03) == 0 || !count) {
383 const int offset = (int) lrand48();
384 if (count)
385 strcat(msg, ",");
386 switch (lrand48() & 0x03) {
387 case 0:
388 sprintf(buf, "-%d", len);
389 break;
390 case 1:
391 sprintf(buf, "%d-", offset);
392 break;
393 default:
394 sprintf(buf, "%d-%d", offset, offset + len);
395 break;
396 }
397 strcat(msg, buf);
398 count++;
399 }
400 strcat(msg, "\r\n");
c4248266 401 }
56792124 402 strcat(msg, custom_header);
14ff08f1 403 strcat(msg, "\r\n");
404 len = strlen(msg);
3cd80ef8 405 if ((len2 = write(s, msg, len)) != len) {
26ac0430
AJ
406 close(s);
407 perror("write request");
408 free_request(r);
409 return NULL;
14ff08f1 410 } else
26ac0430 411 total_bytes_written += len2;
3cd80ef8 412 if (f >= 0) {
26ac0430
AJ
413 while ((len = read(f, buf, sizeof(buf))) > 0) {
414 len2 = write(s, buf, len);
415 if (len2 < 0) {
416 perror("write body");
417 close(s);
418 free_request(r);
419 }
420 }
421 if (len < 0) {
422 perror("read body");
423 exit(1);
424 }
735246e8 425 }
26ac0430
AJ
426 /*
427 * if (fcntl(s, F_SETFL, O_NDELAY) < 0)
428 * perror("fcntl O_NDELAY");
429 */
14ff08f1 430 return r;
735246e8 431}
432
433void
434read_url(int fd, void *junk)
435{
14ff08f1 436 struct _request *r;
735246e8 437 static char buf[8192];
438 char *t;
206b24db 439 buf[0] = '\0';
735246e8 440 if (fgets(buf, 8191, stdin) == NULL) {
26ac0430
AJ
441 printf("Done Reading URLS\n");
442 fd_close(0);
443 nfds++;
444 return;
735246e8 445 }
206b24db 446 if (buf[0] == '\0')
26ac0430 447 return;
735246e8 448 if ((t = strchr(buf, '\n')))
26ac0430 449 *t = '\0';
14ff08f1 450 r = request(buf);
451 if (!r) {
26ac0430
AJ
452 max_connections = nfds - 1;
453 printf("NOTE: max_connections set at %d\n", max_connections);
14ff08f1 454 } else {
26ac0430 455 fd_open(r->fd, read_reply, r, reply_done);
735246e8 456 }
735246e8 457}
458
459void
460usage(void)
461{
ed2ab7db 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");
6f821399 467 fprintf(stderr, " -a Accelerator mode\n");
56792124 468 fprintf(stderr, " -H <string> Custom header\n");
735246e8 469}
470
6a54c60e 471int
735246e8 472main(argc, argv)
26ac0430
AJ
473int argc;
474char *argv[];
735246e8 475{
476 int i;
477 int c;
478 int dt;
14ff08f1 479 int j;
3cd80ef8 480 fd_set R, R2;
735246e8 481 struct timeval start;
735246e8 482 struct timeval last;
483 struct timeval to;
484 setbuf(stdout, NULL);
485 setbuf(stderr, NULL);
486 progname = strdup(argv[0]);
14ff08f1 487 gettimeofday(&now, NULL);
488 start = last = now;
56792124 489 custom_header = strdup("");
490 while ((c = getopt(argc, argv, "ap:h:H:n:icrl:L:t:")) != -1) {
26ac0430
AJ
491 switch (c) {
492 case 'a':
493 opt_accel = 1;
494 break;
495 case 'p':
496 proxy_port = atoi(optarg);
497 break;
498 case 'h':
499 proxy_addr = strdup(optarg);
500 break;
501 case 'n':
502 max_connections = atoi(optarg);
503 break;
504 case 'i':
505 opt_ims = 1;
506 break;
507 case 'l':
508 lifetime = (time_t) atoi(optarg);
509 break;
510 case 'L':
511 process_lifetime = (time_t) atoi(optarg);
512 break;
513 case 'c':
514 opt_checksum = 1;
515 break;
516 case 't':
517 trace_file = fopen(optarg, "a");
518 assert(trace_file);
519 setbuf(trace_file, NULL);
520 break;
521 case 'r':
522 opt_range = 1;
523 break;
524 case 'H':
525 custom_header = realloc(custom_header, strlen(custom_header) + strlen(optarg) + 2 + 1);
526 strcat(custom_header, optarg);
527 strcat(custom_header, "\r\n");
528 break;
529 default:
530 usage();
531 return 1;
532 }
735246e8 533 }
14ff08f1 534 fd_open(0, read_url, NULL, NULL);
535 nfds--;
735246e8 536 signal(SIGINT, sig_intr);
5616e319 537 signal(SIGPIPE, SIG_IGN);
14ff08f1 538 FD_ZERO(&R2);
539 while (nfds || FD[0].cb) {
26ac0430
AJ
540 FD_ZERO(&R);
541 to.tv_sec = 0;
542 to.tv_usec = 100000;
543 if (nfds < max_connections && FD[0].cb)
544 FD_SET(0, &R);
545 for (i = 1; i <= maxfd; i++) {
546 if (FD[i].cb == NULL)
547 continue;
548 if (now.tv_sec - FD[i].start > lifetime) {
549 fprintf(stderr, "WARNING: fd %d lifetime timeout\n", i);
550 fd_close(i);
551 continue;
552 }
553 FD_SET(i, &R);
554 }
555 if (select(maxfd + 1, &R, NULL, NULL, &to) < 0) {
556 fprintf(stderr, "maxfd=%d\n", maxfd);
557 if (errno != EINTR)
558 perror("select");
559 continue;
560 }
561 gettimeofday(&now, NULL);
562 for (i = 0; i <= maxfd; i++) {
563 if (!FD_ISSET(i, &R))
564 continue;
565 FD[i].cb(i, FD[i].data);
566 if (nfds < max_connections && FD[0].cb) {
567 j = 0;
568 FD_SET(0, &R2);
569 to.tv_sec = 0;
570 to.tv_usec = 0;
571 if (select(1, &R2, NULL, NULL, &to) == 1)
572 FD[0].cb(0, FD[0].data);
573 }
574 }
575 if (now.tv_sec > last.tv_sec) {
576 last = now;
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",
579 dt,
580 nrequests,
581 reqpersec,
582 nfds,
583 (int) (nrequests / dt),
584 (int) (total_bytes_read / 1024 / 1024),
585 (int) (total_bytes_read / 1024 / dt));
586 reqpersec = 0;
587 /*
588 * if (dt > process_lifetime)
589 * exit(0);
590 */
591 }
735246e8 592 }
b6a2f15e 593 printf("Exiting normally\n");
735246e8 594 return 0;
595}