]> git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/tcp-banger2.c
use SQUID_MAXFD
[thirdparty/squid.git] / test-suite / tcp-banger2.c
1 #include "config.h"
2
3 /*
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.
8 */
9 #define CHANGE_FD_SETSIZE 1
10
11 /* Cannot increase FD_SETSIZE on Linux */
12 #if defined(_SQUID_LINUX_)
13 #undef CHANGE_FD_SETSIZE
14 #define CHANGE_FD_SETSIZE 0
15 #endif
16
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
26 #endif
27 #endif
28
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
32 #endif
33
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #if HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40 #if HAVE_STDIO_H
41 #include <stdio.h>
42 #endif
43 #if HAVE_FCNTL_H
44 #include <fcntl.h>
45 #endif
46 #ifdef HAVE_STRING_H
47 #include <string.h>
48 #endif
49 #ifdef HAVE_STRINGS_H
50 #include <strings.h>
51 #endif
52 #if HAVE_SYS_TYPES_H
53 #include <sys/types.h>
54 #endif
55 #if HAVE_SYS_SELECT_H
56 #include <sys/select.h>
57 #endif
58 #if HAVE_SIGNAL_H
59 #include <signal.h>
60 #endif
61 #if HAVE_TIME_H
62 #include <time.h>
63 #endif
64 #if HAVE_SYS_TIME_H
65 #include <sys/time.h>
66 #endif
67 #if HAVE_SYS_SOCKET_H
68 #include <sys/socket.h>
69 #endif
70 #if HAVE_NETINET_IN_H
71 #include <netinet/in.h>
72 #endif
73 #if HAVE_ARPA_INET_H
74 #include <arpa/inet.h>
75 #endif
76 #if HAVE_ERRNO_H
77 #include <errno.h>
78 #endif
79 #if HAVE_SYS_STAT_H
80 #include <sys/stat.h>
81 #endif
82 #if HAVE_ASSERT_H
83 #include <assert.h>
84 #endif
85
86 #define PROXY_PORT 3128
87 #define PROXY_ADDR "127.0.0.1"
88 #define READ_BUF_SZ 4096
89
90 static int proxy_port = PROXY_PORT;
91 static char *proxy_addr = PROXY_ADDR;
92 static char *progname;
93 static int reqpersec;
94 static int nrequests;
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;
105
106 typedef void (CB) (int, void *);
107
108 struct _f {
109 CB *cb;
110 CB *ccb;
111 void *data;
112 time_t start;
113 };
114 struct _request {
115 int fd;
116 char url[8192];
117 char method[16];
118 char requestbodyfile[256];
119 char buf[READ_BUF_SZ * 2 + 1];
120 int headfound;
121 int validsize;
122 int bodysize;
123 int content_length;
124 long validsum;
125 long sum;
126 };
127
128 struct _f FD[SQUID_MAXFD];
129 int nfds = 0;
130 int maxfd = 0;
131
132
133 char *
134 mkrfc850(t)
135 time_t *t;
136 {
137 static char buf[128];
138 struct tm *gmt = gmtime(t);
139 buf[0] = '\0';
140 (void) strftime(buf, 127, "%A, %d-%b-%y %H:%M:%S GMT", gmt);
141 return buf;
142 }
143
144 void
145 fd_close(int fd)
146 {
147 close(fd);
148 if (FD[fd].ccb)
149 FD[fd].ccb(fd, FD[fd].data);
150 FD[fd].ccb = NULL;
151 FD[fd].cb = NULL;
152 FD[fd].data = NULL;
153 nfds--;
154 if (fd == maxfd) {
155 while (fd > 0 && FD[fd].cb == NULL)
156 fd--;
157 maxfd = fd;
158 }
159 }
160
161 void
162 fd_open(int fd, CB * cb, void *data, CB * ccb)
163 {
164 assert(fd < SQUID_MAXFD);
165 FD[fd].cb = cb;
166 FD[fd].ccb = ccb;
167 FD[fd].data = data;
168 FD[fd].start = now.tv_sec;
169 if (fd > maxfd)
170 maxfd = fd;
171 nfds++;
172 }
173
174 void
175 sig_intr(int sig)
176 {
177 fd_close(0);
178 nfds++;
179 printf("\rWaiting for open connections to finish...\n");
180 signal(sig, SIG_DFL);
181 }
182
183 void
184 read_reply(int fd, void *data)
185 {
186 struct _request *r = data;
187 static unsigned char buf[READ_BUF_SZ];
188 int len;
189 if ((len = read(fd, buf, READ_BUF_SZ)) <= 0) {
190 fd_close(fd);
191 reqpersec++;
192 nrequests++;
193 } else {
194 int used = 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++) {
204 switch (*p) {
205 case '\n':
206 r->headfound++;
207 if (header) {
208 /* Decode header */
209 if (strncasecmp(header, "Content-Length:", 15) == 0)
210 r->content_length = atoi(header + 15);
211 if (strncasecmp(header, "X-Request-URI:", 14) == 0) {
212 /* Check URI */
213 if (strncmp(r->url, header + 15, strcspn(header + 15, "\r\n"))) {
214 char url[8192];
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",
218 r->url, url);
219 }
220 }
221 header = NULL;
222 }
223 break;
224 case '\r':
225 break;
226 default:
227 r->headfound = 0;
228 if (!header)
229 header = p;
230 break;
231 }
232 }
233 if (header) {
234 memmove(r->buf, header, newlen - (header - r->buf) + 1);
235 }
236 assert(used >= oldlen);
237 used -= oldlen;
238 }
239 r->bodysize += len - used;
240 if (opt_checksum) {
241 for (; used < len; used++) {
242 r->sum += buf[used];
243 }
244 }
245 }
246 }
247
248 void
249 reply_done(int fd, void *data)
250 {
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);
263 }
264 if (trace_file) {
265 fprintf(trace_file, "%s %s %s %d 0x%lx\n",
266 r->method, r->url, r->requestbodyfile, r->bodysize, r->sum);
267 }
268 free(r);
269 }
270
271 struct _request *
272 request(char *urlin)
273 {
274 int s = -1, f = -1;
275 char buf[4096];
276 char msg[8192];
277 char *method, *url, *file, *size, *checksum;
278 char urlbuf[8192];
279 int len, len2;
280 time_t w;
281 struct stat st;
282 struct sockaddr_in S;
283 struct _request *r;
284 if (*urlin == '\0')
285 return NULL;
286 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
287 perror("socket");
288 return NULL;
289 }
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) {
295 close(s);
296 perror("connect");
297 return NULL;
298 }
299 strcpy(urlbuf, urlin);
300 method = strtok(urlbuf, " ");
301 url = strtok(NULL, " ");
302 file = strtok(NULL, " ");
303 size = strtok(NULL, " ");
304 checksum = strtok(NULL, " ");
305 if (!url) {
306 url = method;
307 method = "GET";
308 }
309 if (!file)
310 file = "-";
311 if (!size)
312 size = "-";
313 if (!checksum)
314 checksum = "-";
315 r = calloc(1, sizeof *r);
316 assert(r != NULL);
317 strcpy(r->url, url);
318 strcpy(r->method, method);
319 strcpy(r->requestbodyfile, file);
320 r->fd = s;
321 if (size && strcmp(size, "-") != 0)
322 r->validsize = atoi(size);
323 else
324 r->validsize = -1; /* Unknown */
325 if (checksum && strcmp(checksum, "-") != 0)
326 r->validsum = strtoul(checksum, NULL, 0);
327 r->content_length = -1; /* Unknown */
328 msg[0] = '\0';
329 sprintf(buf, "%s %s HTTP/1.0\r\n", method, url);
330 strcat(msg, buf);
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));
335 strcat(msg, buf);
336 }
337 if (file && strcmp(file, "-") != 0) {
338 f = open(file, O_RDONLY);
339 if (f < 0) {
340 perror("open file");
341 exit(1);
342 }
343 fstat(f, &st);
344 sprintf(buf, "Content-Length: %d\r\n", (int) st.st_size);
345 strcat(msg, buf);
346 }
347 if (opt_range && (lrand48() & 0x03) == 0) {
348 int len;
349 int count = 0;
350 strcat(msg, "Range: bytes=");
351 while (((len = (int) lrand48()) & 0x03) == 0 || !count) {
352 const int offset = (int) lrand48();
353 if (count)
354 strcat(msg, ",");
355 switch (lrand48() & 0x03) {
356 case 0:
357 sprintf(buf, "-%d", len);
358 break;
359 case 1:
360 sprintf(buf, "%d-", offset);
361 break;
362 default:
363 sprintf(buf, "%d-%d", offset, offset + len);
364 break;
365 }
366 strcat(msg, buf);
367 count++;
368 }
369 strcat(msg, "\r\n");
370 }
371 strcat(msg, "\r\n");
372 len = strlen(msg);
373 if ((len2 = write(s, msg, len)) != len) {
374 close(s);
375 perror("write request");
376 free(r);
377 return NULL;
378 } else
379 total_bytes_written += len2;
380 if (f >= 0) {
381 while ((len = read(f, buf, sizeof(buf))) > 0) {
382 len2 = write(s, buf, len);
383 if (len2 < 0) {
384 perror("write body");
385 close(s);
386 free(r);
387 }
388 }
389 if (len < 0) {
390 perror("read body");
391 exit(1);
392 }
393 }
394 /*
395 * if (fcntl(s, F_SETFL, O_NDELAY) < 0)
396 * perror("fcntl O_NDELAY");
397 */
398 return r;
399 }
400
401 void
402 read_url(int fd, void *junk)
403 {
404 struct _request *r;
405 static char buf[8192];
406 char *t;
407 buf[0] = '\0';
408 if (fgets(buf, 8191, stdin) == NULL) {
409 printf("Done Reading URLS\n");
410 fd_close(0);
411 nfds++;
412 return;
413 }
414 if (buf[0] == '\0')
415 return;
416 if ((t = strchr(buf, '\n')))
417 *t = '\0';
418 r = request(buf);
419 if (!r) {
420 max_connections = nfds - 1;
421 printf("NOTE: max_connections set at %d\n", max_connections);
422 } else {
423 fd_open(r->fd, read_reply, r, reply_done);
424 }
425 }
426
427 void
428 usage(void)
429 {
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");
435 }
436
437 int
438 main(argc, argv)
439 int argc;
440 char *argv[];
441 {
442 int i;
443 int c;
444 int dt;
445 int j;
446 fd_set R, R2;
447 struct timeval start;
448 struct timeval last;
449 struct timeval to;
450 setbuf(stdout, NULL);
451 setbuf(stderr, NULL);
452 progname = strdup(argv[0]);
453 gettimeofday(&now, NULL);
454 start = last = now;
455 while ((c = getopt(argc, argv, "p:h:n:icrl:L:t:")) != -1) {
456 switch (c) {
457 case 'p':
458 proxy_port = atoi(optarg);
459 break;
460 case 'h':
461 proxy_addr = strdup(optarg);
462 break;
463 case 'n':
464 max_connections = atoi(optarg);
465 break;
466 case 'i':
467 opt_ims = 1;
468 break;
469 case 'l':
470 lifetime = (time_t) atoi(optarg);
471 break;
472 case 'L':
473 process_lifetime = (time_t) atoi(optarg);
474 break;
475 case 'c':
476 opt_checksum = 1;
477 break;
478 case 't':
479 opt_checksum = 1; /* Tracing requires checksums */
480 trace_file = fopen(optarg, "w");
481 assert(trace_file);
482 break;
483 case 'r':
484 opt_range = 1;
485 break;
486 default:
487 usage();
488 return 1;
489 }
490 }
491 fd_open(0, read_url, NULL, NULL);
492 nfds--;
493 signal(SIGINT, sig_intr);
494 signal(SIGPIPE, SIG_IGN);
495 FD_ZERO(&R2);
496 while (nfds || FD[0].cb) {
497 FD_ZERO(&R);
498 to.tv_sec = 0;
499 to.tv_usec = 100000;
500 if (nfds < max_connections && FD[0].cb)
501 FD_SET(0, &R);
502 for (i = 1; i <= maxfd; i++) {
503 if (FD[i].cb == NULL)
504 continue;
505 if (now.tv_sec - FD[i].start > lifetime) {
506 fprintf(stderr, "WARNING: fd %d lifetime timeout\n", i);
507 fd_close(i);
508 continue;
509 }
510 FD_SET(i, &R);
511 }
512 if (select(maxfd + 1, &R, NULL, NULL, &to) < 0) {
513 fprintf(stderr, "maxfd=%d\n", maxfd);
514 if (errno != EINTR)
515 perror("select");
516 continue;
517 }
518 gettimeofday(&now, NULL);
519 for (i = 0; i <= maxfd; i++) {
520 if (!FD_ISSET(i, &R))
521 continue;
522 FD[i].cb(i, FD[i].data);
523 if (nfds < max_connections && FD[0].cb) {
524 j = 0;
525 FD_SET(0, &R2);
526 to.tv_sec = 0;
527 to.tv_usec = 0;
528 if (select(1, &R2, NULL, NULL, &to) == 1)
529 FD[0].cb(0, FD[0].data);
530 }
531 }
532 if (now.tv_sec > last.tv_sec) {
533 last = now;
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",
536 dt,
537 nrequests,
538 reqpersec,
539 nfds,
540 (int) (nrequests / dt),
541 (int) total_bytes_read / 1024 / 1024,
542 (int) total_bytes_read / 1024 / dt);
543 reqpersec = 0;
544 if (dt > process_lifetime)
545 exit(0);
546 }
547 }
548 return 0;
549 }