]>
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"
89 #define READ_BUF_SZ 4096
91 static int proxy_port
= PROXY_PORT
;
92 static char *proxy_addr
= PROXY_ADDR
;
93 static char *progname
;
96 static int opt_ims
= 0;
97 static int opt_range
= 0;
98 static int max_connections
= 64;
99 static time_t lifetime
= 60;
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;
105 typedef void (CB
) (int, void *);
116 char buf
[READ_BUF_SZ
* 2 + 1];
125 struct _f FD
[MAX_FDS
];
134 static char buf
[128];
135 struct tm
*gmt
= gmtime(t
);
137 (void) strftime(buf
, 127, "%A, %d-%b-%y %H:%M:%S GMT", gmt
);
146 FD
[fd
].ccb(fd
, FD
[fd
].data
);
152 while (fd
> 0 && FD
[fd
].cb
== NULL
)
159 fd_open(int fd
, CB
* cb
, void *data
, CB
*ccb
)
164 FD
[fd
].start
= now
.tv_sec
;
175 printf("\rWaiting for open connections to finish...\n");
176 signal(sig
, SIG_DFL
);
180 read_reply(int fd
, void *data
)
182 struct _request
*r
= data
;
183 static unsigned char buf
[READ_BUF_SZ
];
185 if ((len
=read(fd
, buf
, READ_BUF_SZ
)) <= 0) {
191 total_bytes_read
+=len
;
192 if (r
->headfound
< 2) {
193 char *p
,*header
= NULL
;
194 int oldlen
= strlen(r
->buf
);
195 int newlen
= oldlen
+ len
;
196 assert(oldlen
<= READ_BUF_SZ
);
197 memcpy(r
->buf
+oldlen
, buf
, len
);
198 r
->buf
[newlen
+1]='\0';
199 for(p
=r
->buf
; r
->headfound
< 2 && used
<newlen
; p
++,used
++) {
205 if (strncasecmp(header
,"Content-Length:",15)==0)
206 r
->content_length
= atoi(header
+15);
207 if (strncasecmp(header
,"X-Request-URI:",14)==0) {
209 if (strncmp(r
->url
, header
+15, strcspn(header
+15,"\r\n"))) {
211 strncpy(url
, header
+15, strcspn(header
+15,"\r\n"));
212 url
[strcspn(header
+15, "\r\n")]='\n';
213 fprintf(stderr
,"ERROR: Sent %s received %s\n",
230 memmove(r
->buf
, header
, newlen
- (header
- r
->buf
) + 1);
233 r
->bodysize
+=len
-used
;
235 for (; used
<len
; used
++) {
243 reply_done(int fd
, void *data
)
245 struct _request
*r
= data
;
247 ; /* skip size checks for now */
249 if (r
->bodysize
!= r
->content_length
)
250 fprintf(stderr
,"ERROR: %s expected %d bytes got %d\n",
251 r
->url
, r
->content_length
, r
->bodysize
);
252 else if (r
->validsize
>= 0) {
253 if (r
->validsize
!= r
->bodysize
)
254 fprintf(stderr
,"WARNING: %s size mismatch wanted %d bytes got %d\n",
255 r
->url
, r
->validsize
, r
->bodysize
);
256 else if (opt_checksum
&& r
->validsum
!= r
->sum
)
257 fprintf(stderr
,"WARNING: %s invalid checksum wanted %d got %d\n",
258 r
->url
, r
->validsum
, r
->sum
);
259 } else if (opt_checksum
) {
260 fprintf(stderr
,"DONE: %s checksum %d size %d\n",
261 r
->url
, r
->sum
, r
->bodysize
);
272 char *method
, *url
, *file
, *size
, *checksum
;
277 struct sockaddr_in S
;
279 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
283 memset(&S
, '\0', sizeof(struct sockaddr_in
));
284 S
.sin_family
= AF_INET
;
285 S
.sin_port
= htons(proxy_port
);
286 S
.sin_addr
.s_addr
= inet_addr(proxy_addr
);
287 if (connect(s
, (struct sockaddr
*) &S
, sizeof(S
)) < 0) {
292 strcpy(urlbuf
,urlin
);
293 method
=strtok(urlbuf
," ");
294 url
=strtok(NULL
," ");
295 file
=strtok(NULL
," ");
296 size
=strtok(NULL
," ");
297 checksum
=strtok(NULL
," ");
302 r
=calloc(1,sizeof *r
);
306 if (size
&& strcmp(size
,"-")!=0)
307 r
->validsize
=atoi(size
);
309 r
->validsize
=-1; /* Unknown */
310 if (checksum
&& strcmp(checksum
,"-")!=0)
311 r
->validsum
=atoi(checksum
);
313 sprintf(buf
,"%s %s HTTP/1.0\r\n", method
, url
);
315 strcat(msg
, "Accept: */*\r\n");
316 if (opt_ims
&& (lrand48() & 0x03) == 0) {
317 w
= time(NULL
) - (lrand48() & 0x3FFFF);
318 sprintf(buf
, "If-Modified-Since: %s\r\n", mkrfc850(&w
));
321 if (file
&& strcmp(file
, "-")!=0) {
322 f
= open(file
,O_RDONLY
);
328 sprintf(buf
,"Content-Length: %d\r\n", st
.st_size
);
331 if (opt_range
&& (lrand48() & 0x03) == 0) {
334 strcat(msg
, "Range: bytes=");
335 while (((len
= (int)lrand48()) & 0x03) == 0 || !count
) {
336 const int offset
= (int) lrand48();
339 switch (lrand48() & 0x03) {
341 sprintf(buf
, "-%d", len
);
344 sprintf(buf
, "%d-", offset
);
347 sprintf(buf
, "%d-%d", offset
, offset
+len
);
357 if ((len2
=write(s
, msg
, len
)) != len
) {
359 perror("write request");
363 total_bytes_written
+= len2
;
365 while ((len
= read(f
, buf
, sizeof(buf
)))>0) {
366 len2
= write(s
, buf
, len
);
368 perror("write body");
380 * if (fcntl(s, F_SETFL, O_NDELAY) < 0)
381 * perror("fcntl O_NDELAY");
387 read_url(int fd
, void *junk
)
390 static char buf
[8192];
392 if (fgets(buf
, 8191, stdin
) == NULL
) {
393 printf("Done Reading URLS\n");
398 if ((t
= strchr(buf
, '\n')))
402 max_connections
= nfds
- 1;
403 printf("NOTE: max_connections set at %d\n", max_connections
);
405 fd_open(r
->fd
, read_reply
, r
, reply_done
);
412 fprintf(stderr
, "usage: %s: [-cir] -p port -h host -n max\n", progname
);
425 struct timeval start
;
428 setbuf(stdout
, NULL
);
429 setbuf(stderr
, NULL
);
430 progname
= strdup(argv
[0]);
431 gettimeofday(&now
, NULL
);
433 while ((c
= getopt(argc
, argv
, "p:h:n:icrl:")) != -1) {
436 proxy_port
= atoi(optarg
);
439 proxy_addr
= strdup(optarg
);
442 max_connections
= atoi(optarg
);
448 lifetime
= (time_t) atoi(optarg
);
461 fd_open(0, read_url
, NULL
, NULL
);
463 signal(SIGINT
, sig_intr
);
464 signal(SIGPIPE
, SIG_IGN
);
466 while (nfds
|| FD
[0].cb
) {
470 if (nfds
< max_connections
&& FD
[0].cb
)
472 for (i
= 1; i
<= maxfd
; i
++) {
473 if (FD
[i
].cb
== NULL
)
475 if (now
.tv_sec
- FD
[i
].start
> lifetime
) {
476 fprintf(stderr
, "WARNING: fd %d lifetime timeout\n", i
);
482 if (select(maxfd
+ 1, &R
, NULL
, NULL
, &to
) < 0) {
483 fprintf(stderr
, "maxfd=%d\n", maxfd
);
488 gettimeofday(&now
, NULL
);
489 for (i
= 0; i
<= maxfd
; i
++) {
490 if (!FD_ISSET(i
, &R
))
492 FD
[i
].cb(i
, FD
[i
].data
);
493 if (nfds
< max_connections
&& FD
[0].cb
) {
498 if(select(1,&R2
,NULL
,NULL
,&to
) == 1)
499 FD
[0].cb(0, FD
[0].data
);
502 if (now
.tv_sec
> last
.tv_sec
) {
504 dt
= (int) (now
.tv_sec
- start
.tv_sec
);
505 printf("T+ %6d: %9d req (%+4d), %4d conn, %3d/sec avg, %dmb, %dkb/sec avg\n",
510 (int) (nrequests
/ dt
),
511 (int)total_bytes_read
/ 1024 / 1024,
512 (int)total_bytes_read
/ 1024 / dt
);