]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - test-suite/tcp-banger3.c
SourceFormat Enforcement
[thirdparty/squid.git] / test-suite / tcp-banger3.c
index 5d00758eb51f58e541bcece3a5431e549a86d0d9..1fec98e47a44a575f0b992b482712c900582208a 100644 (file)
@@ -1,4 +1,4 @@
-#include "config.h"
+#include "squid.h"
 
 /*
  * On some systems, FD_SETSIZE is set to something lower than the
@@ -9,7 +9,7 @@
 #define CHANGE_FD_SETSIZE 1
 
 /* Cannot increase FD_SETSIZE on Linux */
-#if defined(_SQUID_LINUX_)
+#if _SQUID_LINUX_
 #undef CHANGE_FD_SETSIZE
 #define CHANGE_FD_SETSIZE 0
 #endif
@@ -18,7 +18,7 @@
  * to return EINVAL. */
 /* Marian Durkovic <marian@svf.stuba.sk> */
 /* Peter Wemm <peter@spinner.DIALix.COM> */
-#if defined(_SQUID_FREEBSD_)
+#if _SQUID_FREEBSD_
 #include <osreldate.h>
 #if __FreeBSD_version < 220000
 #undef CHANGE_FD_SETSIZE
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#if HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
 #if HAVE_STDIO_H
 #include <stdio.h>
 #endif
 #if HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
-#ifdef HAVE_STRING_H
+#if HAVE_STRING_H
 #include <string.h>
 #endif
-#ifdef HAVE_STRINGS_H
+#if HAVE_STRINGS_H
 #include <strings.h>
 #endif
-#if HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
-#if HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
 #if HAVE_SIGNAL_H
 #include <signal.h>
 #endif
 #if HAVE_TIME_H
 #include <time.h>
 #endif
-#if HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
 #if HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
 #if HAVE_ASSERT_H
 #include <assert.h>
 #endif
+#if HAVE_NETDB_H
 #include <netdb.h>
+#endif
+#if HAVE_SYS_WAIT_H
 #include <sys/wait.h>
+#endif
 
 #define READ_BUF_SZ 4096
 #define URL_BUF_SZ 4096
@@ -108,6 +100,12 @@ static int debug = 1;
 static int debug = 0;
 #endif
 
+int
+tvSubMsec(struct timeval t1, struct timeval t2)
+{
+    return (t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec) / 1000;
+}
+
 static int
 get_url(const char *url)
 {
@@ -125,47 +123,55 @@ get_url(const char *url)
     assert(!strncmp(url, "http://", 7));
     strncpy(host, url + 7, URL_BUF_SZ);
     if ((t = strchr(host, '/')))
-       *t = '\0';
+        *t = '\0';
     if ((t = strchr(host, ':'))) {
-       *t = '\0';
-       port = (unsigned short) atoi(t + 1);
+        *t = '\0';
+        port = (unsigned short) atoi(t + 1);
     }
+#if 0
     if ((int) port != 80)
-       return 0;
+        return 0;
+#endif
     t = strchr(url + 7, '/');
     strncpy(path, (t ? t : "/"), URL_BUF_SZ);
     memset(&S, '\0', sizeof(S));
     h = gethostbyname(host);
     if (!h)
-       return 0;
+        return 0;
     memcpy(&S.sin_addr.s_addr, h->h_addr_list[0], sizeof(S.sin_addr.s_addr));
     S.sin_port = htons(port);
     S.sin_family = AF_INET;
-    if (debug)
-    fprintf(stderr, "%s (%s) %d %s\n", host, inet_ntoa(S.sin_addr), (int) port, path);
+    if (debug) {
+        char tmp[16];
+        fprintf(stderr, "%s (%s) %d %s\n", host, inet_ntop(AF_INET, &S.sin_addr,tmp,sizeof(tmp)), (int) port, path);
+    }
     s = socket(PF_INET, SOCK_STREAM, 0);
+    if (s < 0) {
+        perror("socket");
+        return -errno;
+    }
     x = connect(s, (struct sockaddr *) &S, sizeof(S));
     if (x < 0) {
-       perror(host);
-       return 0;
+        perror(host);
+        return -errno;
     }
     snprintf(request, URL_BUF_SZ,
-       "GET %s HTTP/1.1\r\n"
-       "Accept: */*\r\n"
-       "Host: %s\r\n"
-       "Connection: close\r\n"
-       "\r\n",
-       path,
-       host);
+             "GET %s HTTP/1.1\r\n"
+             "Accept: */*\r\n"
+             "Host: %s\r\n"
+             "Connection: close\r\n"
+             "\r\n",
+             path,
+             host);
     x = write(s, request, strlen(request));
     if (x < 0) {
-       perror("write");
-       return 0;
+        perror("write");
+        return -errno;
     }
     do {
-       x = read(s, reply, READ_BUF_SZ);
-       if (x > 0)
-           nr += x;
+        x = read(s, reply, READ_BUF_SZ);
+        if (x > 0)
+            nr += x;
     } while (x > 0);
     close(s);
     return nr;
@@ -177,20 +183,24 @@ child_main_loop(void)
     char buf[URL_BUF_SZ];
     char *t;
     int n;
+    struct timeval t1;
+    struct timeval t2;
     if (debug)
-    fprintf(stderr, "Child PID %d entering child_main_loop\n", (int) getpid());
+        fprintf(stderr, "Child PID %d entering child_main_loop\n", (int) getpid());
     setbuf(stdin, NULL);
     setbuf(stdout, NULL);
     setbuf(stderr, NULL);
     while (fgets(buf, URL_BUF_SZ, stdin)) {
-       t = strchr(buf, '\n');
-       if (t == NULL)
-           continue;
-       *t = '\0';
-       if (strncmp(buf, "http://", 7))
-           continue;
-       n = get_url(buf);
-       printf ("%d\n", n);
+        t = strchr(buf, '\n');
+        if (t == NULL)
+            continue;
+        *t = '\0';
+        if (strncmp(buf, "http://", 7))
+            continue;
+        gettimeofday(&t1, NULL);
+        n = get_url(buf);
+        gettimeofday(&t2, NULL);
+        printf("%d %d\n", n, tvSubMsec(t1, t2));
     }
 }
 
@@ -203,28 +213,28 @@ create_a_thing(char *argv[])
     pid_t pid;
     thing *t;
     if (pipe(p2c) < 0)
-       abort();
+        abort();
     if (pipe(c2p) < 0)
-       abort();
+        abort();
     prfd = p2c[0];
     cwfd = p2c[1];
     crfd = c2p[0];
     pwfd = c2p[1];
     if ((pid = fork()) < 0)
-       abort();
+        abort();
     if (pid > 0) {             /* parent */
-       /* close shared socket with child */
-       close(crfd);
-       close(cwfd);
-       t = calloc(1, sizeof(*t));
-       t->wfd = pwfd;
-       t->rfd = prfd;
-       if (pwfd > maxfd)
-           maxfd = pwfd;
-       if (prfd > maxfd)
-           maxfd = prfd;
-       t->pid = pid;
-       return t;
+        /* close shared socket with child */
+        close(crfd);
+        close(cwfd);
+        t = calloc(1, sizeof(*t));
+        t->wfd = pwfd;
+        t->rfd = prfd;
+        if (pwfd > maxfd)
+            maxfd = pwfd;
+        if (prfd > maxfd)
+            maxfd = prfd;
+        t->pid = pid;
+        return t;
     }
     /* child */
     close(prfd);
@@ -244,13 +254,13 @@ create_children(char *argv[])
     thing **T = &things;
     int i;
     for (i = 0; i < 20; i++) {
-       t = create_a_thing(argv);
-       assert(t);
-    if (debug)
-       fprintf(stderr, "Thing #%d on FD %d/%d\n",
-           i, t->rfd, t->wfd);
-       *T = t;
-       T = &t->next;
+        t = create_a_thing(argv);
+        assert(t);
+        if (debug)
+            fprintf(stderr, "Thing #%d on FD %d/%d\n",
+                    i, t->rfd, t->wfd);
+        *T = t;
+        T = &t->next;
     }
 }
 
@@ -259,9 +269,9 @@ parent_read_url(void)
 {
     static char buf[URL_BUF_SZ];
     while (fgets(buf, URL_BUF_SZ, stdin)) {
-       if (strncmp(buf, "http://", 7))
-           continue;
-       return buf;
+        if (strncmp(buf, "http://", 7))
+            continue;
+        return buf;
     }
     return NULL;
 }
@@ -272,9 +282,9 @@ get_idle_thing(void)
     thing *t;
     thing *n = things;
     while ((t = n)) {
-       n = t->next;
-       if (t->state == 0)
-           break;
+        n = t->next;
+        if (t->state == 0)
+            break;
     }
     return t;
 }
@@ -287,12 +297,12 @@ dispatch(thing * t, char *url)
     assert(t->state == 0);
     x = write(t->wfd, url, strlen(url));
     if (x < 0)
-       perror("write");
+        perror("write");
     if (debug)
-    fprintf(stderr, "dispatched URL to thing PID %d, %d bytes\n", (int) t->pid, x);
+        fprintf(stderr, "dispatched URL to thing PID %d, %d bytes\n", (int) t->pid, x);
     strncpy(t->url, url, URL_BUF_SZ);
     if ((s = strchr(t->url, '\n')))
-       *s = '\0';
+        *s = '\0';
     t->state = 1;
     FD_SET(t->rfd, &R1);
 }
@@ -303,13 +313,13 @@ read_reply(thing * t)
     char buf[128];
     int i;
     int x;
+    int j;
     x = read(t->rfd, buf, 128);
     if (x < 0) {
-       perror("read");
-    } else {
-       i = atoi(buf);
-       gettimeofday(&now, NULL);
-       printf("%d.%06d %9d %s\n", (int) now.tv_sec, (int) now.tv_usec, i, t->url);
+        perror("read");
+    } else if (2 == sscanf(buf, "%d %d", &i, &j)) {
+        gettimeofday(&now, NULL);
+        printf("%d.%06d %9d %9d %s\n", (int) now.tv_sec, (int) now.tv_usec, i, j, t->url);
     }
     t->state = 0;
     FD_CLR(t->rfd, &R1);
@@ -322,23 +332,28 @@ parent_main_loop(void)
     char *url;
     fd_set R2;
     int x;
+    struct timeval to;
     FD_ZERO(&R1);
     for (;;) {
-       while ((t = get_idle_thing()) && (url = parent_read_url()))
-           dispatch(t, url);
-       R2 = R1;
-       x = select(maxfd + 1, &R2, NULL, NULL, NULL);
-       if (x < 0) {
-           perror("select");
-           continue;
-       }
-       for (t = things; t; t = t->next) {
-           if (t->state != 1)
-               continue;
-           if (!FD_ISSET(t->rfd, &R2))
-               continue;
-           read_reply(t);
-       }
+        while ((t = get_idle_thing()) && (url = parent_read_url()))
+            dispatch(t, url);
+        R2 = R1;
+        to.tv_sec = 60;
+        to.tv_usec = 0;
+        x = select(maxfd + 1, &R2, NULL, NULL, &to);
+        if (x < 0) {
+            perror("select");
+            continue;
+        } else if (x == 0) {
+            return;
+        }
+        for (t = things; t; t = t->next) {
+            if (t->state != 1)
+                continue;
+            if (!FD_ISSET(t->rfd, &R2))
+                continue;
+            read_reply(t);
+        }
     }
 }
 
@@ -348,17 +363,19 @@ sig_child(int sig)
     int status;
     pid_t pid;
     do {
-       pid = waitpid(-1, &status, WNOHANG);
+        pid = waitpid(-1, &status, WNOHANG);
     } while (pid > 0 || (pid < 0 && errno == EINTR));
     signal(sig, sig_child);
 }
 
-
 int
 main(int argc, char *argv[])
 {
+    int i;
     signal(SIGCHLD, sig_child);
     create_children(argv);
     parent_main_loop();
-    return 0;
+    for (i = 3; i <= maxfd; i++)
+        close(i);
+    sleep(1);
 }