]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/tcp-banger3.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
56 #include <sys/socket.h>
59 #include <netinet/in.h>
62 #include <arpa/inet.h>
80 #define READ_BUF_SZ 4096
81 #define URL_BUF_SZ 4096
91 typedef struct _thing thing
;
93 static thing
*things
= NULL
;
96 static struct timeval now
;
100 static int debug
= 0;
104 tvSubMsec(struct timeval t1
, struct timeval t2
)
106 return (t2
.tv_sec
- t1
.tv_sec
) * 1000 + (t2
.tv_usec
- t1
.tv_usec
) / 1000;
110 get_url(const char *url
)
112 char host
[URL_BUF_SZ
];
113 char path
[URL_BUF_SZ
];
114 char request
[URL_BUF_SZ
];
115 char reply
[READ_BUF_SZ
];
121 struct sockaddr_in S
;
122 unsigned short port
= 80;
123 assert(!strncmp(url
, "http://", 7));
124 strncpy(host
, url
+ 7, URL_BUF_SZ
);
125 if ((t
= strchr(host
, '/')))
127 if ((t
= strchr(host
, ':'))) {
129 port
= (unsigned short) atoi(t
+ 1);
132 if ((int) port
!= 80)
135 t
= strchr(url
+ 7, '/');
136 strncpy(path
, (t
? t
: "/"), URL_BUF_SZ
);
137 memset(&S
, '\0', sizeof(S
));
138 h
= gethostbyname(host
);
141 memcpy(&S
.sin_addr
.s_addr
, h
->h_addr_list
[0], sizeof(S
.sin_addr
.s_addr
));
142 S
.sin_port
= htons(port
);
143 S
.sin_family
= AF_INET
;
146 fprintf(stderr
, "%s (%s) %d %s\n", host
, inet_ntop(AF_INET
, &S
.sin_addr
,tmp
,sizeof(tmp
)), (int) port
, path
);
148 s
= socket(PF_INET
, SOCK_STREAM
, 0);
153 x
= connect(s
, (struct sockaddr
*) &S
, sizeof(S
));
158 snprintf(request
, URL_BUF_SZ
,
159 "GET %s HTTP/1.1\r\n"
162 "Connection: close\r\n"
166 x
= write(s
, request
, strlen(request
));
172 x
= read(s
, reply
, READ_BUF_SZ
);
181 child_main_loop(void)
183 char buf
[URL_BUF_SZ
];
189 fprintf(stderr
, "Child PID %d entering child_main_loop\n", (int) getpid());
191 setbuf(stdout
, NULL
);
192 setbuf(stderr
, NULL
);
193 while (fgets(buf
, URL_BUF_SZ
, stdin
)) {
194 t
= strchr(buf
, '\n');
198 if (strncmp(buf
, "http://", 7))
200 gettimeofday(&t1
, NULL
);
202 gettimeofday(&t2
, NULL
);
203 printf("%d %d\n", n
, tvSubMsec(t1
, t2
));
208 create_a_thing(char *argv
[])
212 int prfd
, pwfd
, crfd
, cwfd
;
223 if ((pid
= fork()) < 0)
225 if (pid
> 0) { /* parent */
226 /* close shared socket with child */
229 t
= calloc(1, sizeof(*t
));
251 create_children(char *argv
[])
256 for (i
= 0; i
< 20; i
++) {
257 t
= create_a_thing(argv
);
260 fprintf(stderr
, "Thing #%d on FD %d/%d\n",
268 parent_read_url(void)
270 static char buf
[URL_BUF_SZ
];
271 while (fgets(buf
, URL_BUF_SZ
, stdin
)) {
272 if (strncmp(buf
, "http://", 7))
293 dispatch(thing
* t
, char *url
)
297 assert(t
->state
== 0);
298 x
= write(t
->wfd
, url
, strlen(url
));
302 fprintf(stderr
, "dispatched URL to thing PID %d, %d bytes\n", (int) t
->pid
, x
);
303 strncpy(t
->url
, url
, URL_BUF_SZ
);
304 if ((s
= strchr(t
->url
, '\n')))
311 read_reply(thing
* t
)
317 x
= read(t
->rfd
, buf
, 128);
320 } else if (2 == sscanf(buf
, "%d %d", &i
, &j
)) {
321 gettimeofday(&now
, NULL
);
322 printf("%d.%06d %9d %9d %s\n", (int) now
.tv_sec
, (int) now
.tv_usec
, i
, j
, t
->url
);
329 parent_main_loop(void)
338 while ((t
= get_idle_thing()) && (url
= parent_read_url()))
343 x
= select(maxfd
+ 1, &R2
, NULL
, NULL
, &to
);
350 for (t
= things
; t
; t
= t
->next
) {
353 if (!FD_ISSET(t
->rfd
, &R2
))
366 pid
= waitpid(-1, &status
, WNOHANG
);
367 } while (pid
> 0 || (pid
< 0 && errno
== EINTR
));
368 signal(sig
, sig_child
);
373 main(int argc
, char *argv
[])
376 signal(SIGCHLD
, sig_child
);
377 create_children(argv
);
379 for (i
= 3; i
<= maxfd
; i
++)