]>
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
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>
88 #define READ_BUF_SZ 4096
89 #define URL_BUF_SZ 4096
99 typedef struct _thing thing
;
101 static thing
*things
= NULL
;
103 static int maxfd
= 0;
104 static struct timeval now
;
106 static int debug
= 1;
108 static int debug
= 0;
112 tvSubMsec(struct timeval t1
, struct timeval t2
)
114 return (t2
.tv_sec
- t1
.tv_sec
) * 1000 + (t2
.tv_usec
- t1
.tv_usec
) / 1000;
118 get_url(const char *url
)
120 char host
[URL_BUF_SZ
];
121 char path
[URL_BUF_SZ
];
122 char request
[URL_BUF_SZ
];
123 char reply
[READ_BUF_SZ
];
129 struct sockaddr_in S
;
130 unsigned short port
= 80;
131 assert(!strncmp(url
, "http://", 7));
132 strncpy(host
, url
+ 7, URL_BUF_SZ
);
133 if ((t
= strchr(host
, '/')))
135 if ((t
= strchr(host
, ':'))) {
137 port
= (unsigned short) atoi(t
+ 1);
140 if ((int) port
!= 80)
143 t
= strchr(url
+ 7, '/');
144 strncpy(path
, (t
? t
: "/"), URL_BUF_SZ
);
145 memset(&S
, '\0', sizeof(S
));
146 h
= gethostbyname(host
);
149 memcpy(&S
.sin_addr
.s_addr
, h
->h_addr_list
[0], sizeof(S
.sin_addr
.s_addr
));
150 S
.sin_port
= htons(port
);
151 S
.sin_family
= AF_INET
;
153 fprintf(stderr
, "%s (%s) %d %s\n", host
, inet_ntoa(S
.sin_addr
), (int) port
, path
);
154 s
= socket(PF_INET
, SOCK_STREAM
, 0);
159 x
= connect(s
, (struct sockaddr
*) &S
, sizeof(S
));
164 snprintf(request
, URL_BUF_SZ
,
165 "GET %s HTTP/1.1\r\n"
168 "Connection: close\r\n"
172 x
= write(s
, request
, strlen(request
));
178 x
= read(s
, reply
, READ_BUF_SZ
);
187 child_main_loop(void)
189 char buf
[URL_BUF_SZ
];
195 fprintf(stderr
, "Child PID %d entering child_main_loop\n", (int) getpid());
197 setbuf(stdout
, NULL
);
198 setbuf(stderr
, NULL
);
199 while (fgets(buf
, URL_BUF_SZ
, stdin
)) {
200 t
= strchr(buf
, '\n');
204 if (strncmp(buf
, "http://", 7))
206 gettimeofday(&t1
, NULL
);
208 gettimeofday(&t2
, NULL
);
209 printf("%d %d\n", n
, tvSubMsec(t1
, t2
));
214 create_a_thing(char *argv
[])
218 int prfd
, pwfd
, crfd
, cwfd
;
229 if ((pid
= fork()) < 0)
231 if (pid
> 0) { /* parent */
232 /* close shared socket with child */
235 t
= calloc(1, sizeof(*t
));
257 create_children(char *argv
[])
262 for (i
= 0; i
< 20; i
++) {
263 t
= create_a_thing(argv
);
266 fprintf(stderr
, "Thing #%d on FD %d/%d\n",
274 parent_read_url(void)
276 static char buf
[URL_BUF_SZ
];
277 while (fgets(buf
, URL_BUF_SZ
, stdin
)) {
278 if (strncmp(buf
, "http://", 7))
299 dispatch(thing
* t
, char *url
)
303 assert(t
->state
== 0);
304 x
= write(t
->wfd
, url
, strlen(url
));
308 fprintf(stderr
, "dispatched URL to thing PID %d, %d bytes\n", (int) t
->pid
, x
);
309 strncpy(t
->url
, url
, URL_BUF_SZ
);
310 if ((s
= strchr(t
->url
, '\n')))
317 read_reply(thing
* t
)
323 x
= read(t
->rfd
, buf
, 128);
326 } else if (2 == sscanf(buf
, "%d %d", &i
, &j
)) {
327 gettimeofday(&now
, NULL
);
328 printf("%d.%06d %9d %9d %s\n", (int) now
.tv_sec
, (int) now
.tv_usec
, i
, j
, t
->url
);
335 parent_main_loop(void)
344 while ((t
= get_idle_thing()) && (url
= parent_read_url()))
349 x
= select(maxfd
+ 1, &R2
, NULL
, NULL
, &to
);
356 for (t
= things
; t
; t
= t
->next
) {
359 if (!FD_ISSET(t
->rfd
, &R2
))
372 pid
= waitpid(-1, &status
, WNOHANG
);
373 } while (pid
> 0 || (pid
< 0 && errno
== EINTR
));
374 signal(sig
, sig_child
);
379 main(int argc
, char *argv
[])
382 signal(SIGCHLD
, sig_child
);
383 create_children(argv
);
385 for (i
= 3; i
<= maxfd
; i
++)