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