]>
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
;
154 fprintf(stderr
, "%s (%s) %d %s\n", host
, inet_ntop(AF_INET
, &S
.sin_addr
,tmp
,sizeof(tmp
)), (int) port
, path
);
156 s
= socket(PF_INET
, SOCK_STREAM
, 0);
161 x
= connect(s
, (struct sockaddr
*) &S
, sizeof(S
));
166 snprintf(request
, URL_BUF_SZ
,
167 "GET %s HTTP/1.1\r\n"
170 "Connection: close\r\n"
174 x
= write(s
, request
, strlen(request
));
180 x
= read(s
, reply
, READ_BUF_SZ
);
189 child_main_loop(void)
191 char buf
[URL_BUF_SZ
];
197 fprintf(stderr
, "Child PID %d entering child_main_loop\n", (int) getpid());
199 setbuf(stdout
, NULL
);
200 setbuf(stderr
, NULL
);
201 while (fgets(buf
, URL_BUF_SZ
, stdin
)) {
202 t
= strchr(buf
, '\n');
206 if (strncmp(buf
, "http://", 7))
208 gettimeofday(&t1
, NULL
);
210 gettimeofday(&t2
, NULL
);
211 printf("%d %d\n", n
, tvSubMsec(t1
, t2
));
216 create_a_thing(char *argv
[])
220 int prfd
, pwfd
, crfd
, cwfd
;
231 if ((pid
= fork()) < 0)
233 if (pid
> 0) { /* parent */
234 /* close shared socket with child */
237 t
= calloc(1, sizeof(*t
));
259 create_children(char *argv
[])
264 for (i
= 0; i
< 20; i
++) {
265 t
= create_a_thing(argv
);
268 fprintf(stderr
, "Thing #%d on FD %d/%d\n",
276 parent_read_url(void)
278 static char buf
[URL_BUF_SZ
];
279 while (fgets(buf
, URL_BUF_SZ
, stdin
)) {
280 if (strncmp(buf
, "http://", 7))
301 dispatch(thing
* t
, char *url
)
305 assert(t
->state
== 0);
306 x
= write(t
->wfd
, url
, strlen(url
));
310 fprintf(stderr
, "dispatched URL to thing PID %d, %d bytes\n", (int) t
->pid
, x
);
311 strncpy(t
->url
, url
, URL_BUF_SZ
);
312 if ((s
= strchr(t
->url
, '\n')))
319 read_reply(thing
* t
)
325 x
= read(t
->rfd
, buf
, 128);
328 } else if (2 == sscanf(buf
, "%d %d", &i
, &j
)) {
329 gettimeofday(&now
, NULL
);
330 printf("%d.%06d %9d %9d %s\n", (int) now
.tv_sec
, (int) now
.tv_usec
, i
, j
, t
->url
);
337 parent_main_loop(void)
346 while ((t
= get_idle_thing()) && (url
= parent_read_url()))
351 x
= select(maxfd
+ 1, &R2
, NULL
, NULL
, &to
);
358 for (t
= things
; t
; t
= t
->next
) {
361 if (!FD_ISSET(t
->rfd
, &R2
))
374 pid
= waitpid(-1, &status
, WNOHANG
);
375 } while (pid
> 0 || (pid
< 0 && errno
== EINTR
));
376 signal(sig
, sig_child
);
381 main(int argc
, char *argv
[])
384 signal(SIGCHLD
, sig_child
);
385 create_children(argv
);
387 for (i
= 3; i
<= maxfd
; i
++)