]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/tcp-banger3.c
079b0fb7bc79f34e22be9a007965dd3cc45063e2
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
12 * On some systems, FD_SETSIZE is set to something lower than the
13 * actual number of files which can be opened. IRIX is one case,
14 * NetBSD is another. So here we increase FD_SETSIZE to our
15 * configure-discovered maximum *before* any system includes.
17 #define CHANGE_FD_SETSIZE 1
19 /* Cannot increase FD_SETSIZE on Linux */
21 #undef CHANGE_FD_SETSIZE
22 #define CHANGE_FD_SETSIZE 0
25 /* Cannot increase FD_SETSIZE on FreeBSD before 2.2.0, causes select(2)
26 * to return EINVAL. */
27 /* Marian Durkovic <marian@svf.stuba.sk> */
28 /* Peter Wemm <peter@spinner.DIALix.COM> */
30 #include <osreldate.h>
31 #if __FreeBSD_version < 220000
32 #undef CHANGE_FD_SETSIZE
33 #define CHANGE_FD_SETSIZE 0
37 /* Increase FD_SETSIZE if SQUID_MAXFD is bigger */
38 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
39 #define FD_SETSIZE SQUID_MAXFD
61 #include <sys/socket.h>
64 #include <netinet/in.h>
67 #include <arpa/inet.h>
85 #define READ_BUF_SZ 4096
86 #define URL_BUF_SZ 4096
96 typedef struct _thing thing
;
98 static thing
*things
= NULL
;
100 static int maxfd
= 0;
101 static struct timeval now
;
103 static int debug
= 1;
105 static int debug
= 0;
109 tvSubMsec(struct timeval t1
, struct timeval t2
)
111 return (t2
.tv_sec
- t1
.tv_sec
) * 1000 + (t2
.tv_usec
- t1
.tv_usec
) / 1000;
115 get_url(const char *url
)
117 char host
[URL_BUF_SZ
];
118 char path
[URL_BUF_SZ
];
119 char request
[URL_BUF_SZ
];
120 char reply
[READ_BUF_SZ
];
126 struct sockaddr_in S
;
127 unsigned short port
= 80;
128 assert(!strncmp(url
, "http://", 7));
129 strncpy(host
, url
+ 7, URL_BUF_SZ
);
130 if ((t
= strchr(host
, '/')))
132 if ((t
= strchr(host
, ':'))) {
134 port
= (unsigned short) atoi(t
+ 1);
137 if ((int) port
!= 80)
140 t
= strchr(url
+ 7, '/');
141 strncpy(path
, (t
? t
: "/"), URL_BUF_SZ
);
142 memset(&S
, '\0', sizeof(S
));
143 h
= gethostbyname(host
);
146 memcpy(&S
.sin_addr
.s_addr
, h
->h_addr_list
[0], sizeof(S
.sin_addr
.s_addr
));
147 S
.sin_port
= htons(port
);
148 S
.sin_family
= AF_INET
;
151 fprintf(stderr
, "%s (%s) %d %s\n", host
, inet_ntop(AF_INET
, &S
.sin_addr
,tmp
,sizeof(tmp
)), (int) port
, path
);
153 s
= socket(PF_INET
, SOCK_STREAM
, 0);
158 x
= connect(s
, (struct sockaddr
*) &S
, sizeof(S
));
163 snprintf(request
, URL_BUF_SZ
,
164 "GET %s HTTP/1.1\r\n"
167 "Connection: close\r\n"
171 x
= write(s
, request
, strlen(request
));
177 x
= read(s
, reply
, READ_BUF_SZ
);
186 child_main_loop(void)
188 char buf
[URL_BUF_SZ
];
194 fprintf(stderr
, "Child PID %d entering child_main_loop\n", (int) getpid());
196 setbuf(stdout
, NULL
);
197 setbuf(stderr
, NULL
);
198 while (fgets(buf
, URL_BUF_SZ
, stdin
)) {
199 t
= strchr(buf
, '\n');
203 if (strncmp(buf
, "http://", 7))
205 gettimeofday(&t1
, NULL
);
207 gettimeofday(&t2
, NULL
);
208 printf("%d %d\n", n
, tvSubMsec(t1
, t2
));
213 create_a_thing(char *argv
[])
217 int prfd
, pwfd
, crfd
, cwfd
;
228 if ((pid
= fork()) < 0)
230 if (pid
> 0) { /* parent */
231 /* close shared socket with child */
234 t
= calloc(1, sizeof(*t
));
256 create_children(char *argv
[])
261 for (i
= 0; i
< 20; i
++) {
262 t
= create_a_thing(argv
);
265 fprintf(stderr
, "Thing #%d on FD %d/%d\n",
273 parent_read_url(void)
275 static char buf
[URL_BUF_SZ
];
276 while (fgets(buf
, URL_BUF_SZ
, stdin
)) {
277 if (strncmp(buf
, "http://", 7))
298 dispatch(thing
* t
, char *url
)
302 assert(t
->state
== 0);
303 x
= write(t
->wfd
, url
, strlen(url
));
307 fprintf(stderr
, "dispatched URL to thing PID %d, %d bytes\n", (int) t
->pid
, x
);
308 strncpy(t
->url
, url
, URL_BUF_SZ
);
309 if ((s
= strchr(t
->url
, '\n')))
316 read_reply(thing
* t
)
322 x
= read(t
->rfd
, buf
, 128);
325 } else if (2 == sscanf(buf
, "%d %d", &i
, &j
)) {
326 gettimeofday(&now
, NULL
);
327 printf("%d.%06d %9d %9d %s\n", (int) now
.tv_sec
, (int) now
.tv_usec
, i
, j
, t
->url
);
334 parent_main_loop(void)
343 while ((t
= get_idle_thing()) && (url
= parent_read_url()))
348 x
= select(maxfd
+ 1, &R2
, NULL
, NULL
, &to
);
355 for (t
= things
; t
; t
= t
->next
) {
358 if (!FD_ISSET(t
->rfd
, &R2
))
371 pid
= waitpid(-1, &status
, WNOHANG
);
372 } while (pid
> 0 || (pid
< 0 && errno
== EINTR
));
373 signal(sig
, sig_child
);
377 main(int argc
, char *argv
[])
380 signal(SIGCHLD
, sig_child
);
381 create_children(argv
);
383 for (i
= 3; i
<= maxfd
; i
++)