]> err.no Git - varnish/commitdiff
Open TCP sockets
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 23 Mar 2006 08:45:44 +0000 (08:45 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 23 Mar 2006 08:45:44 +0000 (08:45 +0000)
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@60 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/Makefile.am
varnish-cache/bin/varnishd/heritage.h
varnish-cache/bin/varnishd/mgt.h
varnish-cache/bin/varnishd/tcp.c [new file with mode: 0644]
varnish-cache/bin/varnishd/varnishd.c

index 32d8553cea276222b4de1f84392510e9a5c7b6ac..4f89f09fad6871f7416553d2f0dcf33038f5d280 100644 (file)
@@ -8,6 +8,7 @@ varnishd_SOURCES = \
        cache_main.c \
        cli_event.c \
        mgt_child.c \
+       tcp.c \
        varnishd.c
 
 varnishd_LDADD = \
index c1b8e78b90e484707f1bf745b4f9d629307835d9..e555b79fb68abd6653638e16772b6379cd574523 100644 (file)
@@ -5,7 +5,21 @@
  */
 
 struct heritage {
+
+       /*
+        * Two pipe(2)'s for CLI connection between cache and mgt.
+        * cache reads [2] and writes [1].  Mgt reads [0] and writes [3].
+        */
        int     fds[4];
+
+       /*
+        * Two sockets from which to accept connections, one bound to
+        * loopback only and one bound for wildcard (or possibly a specific
+        * interface IP number).
+        */
+#define HERITAGE_NSOCKS                2       /* IPv4 + IPv6 */
+       int     sock_local[HERITAGE_NSOCKS];
+       int     sock_remote[HERITAGE_NSOCKS];
 };
 
 extern struct heritage heritage;
index 255c23d0f5e4daaaba2cf19d3a0d181adb059877..9b6a7063c0f9a0d8041a95994c4290d4225e19b5 100644 (file)
@@ -10,3 +10,6 @@ void mgt_sigchld(int, short, void *);
 
 typedef void mgt_ccb_f(unsigned, const char *, void *);
 void mgt_child_request(mgt_ccb_f *, void *, char **argv, const char *fmt, ...);
+
+/* tcp.c */
+int open_tcp(const char *port);
diff --git a/varnish-cache/bin/varnishd/tcp.c b/varnish-cache/bin/varnishd/tcp.c
new file mode 100644 (file)
index 0000000..3db866d
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * $Id$
+ */
+
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+
+#include "heritage.h"
+
+static void
+create_listen_socket(const char *addr, const char *port, int *sp, int nsp)
+{
+       struct addrinfo ai, *r0, *r1;
+       int i, j, s;
+
+       memset(&ai, 0, sizeof ai);
+       ai.ai_family = PF_UNSPEC;
+       ai.ai_socktype = SOCK_STREAM;
+       ai.ai_flags = AI_PASSIVE;
+       i = getaddrinfo(addr, port, &ai, &r0);
+
+       if (i) {
+               fprintf("getaddrinfo failed: %s\n", gai_strerror(i));
+               return;
+       }
+
+       for (r1 = r0; r1 != NULL && nsp > 0; r1 = r1->ai_next) {
+               s = socket(r1->ai_family, r1->ai_socktype, r1->ai_protocol);
+               if (s < 0)
+                       continue;
+               j = 1;
+               i = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &j, sizeof j);
+               assert(i == 0);
+
+               i = bind(s, r1->ai_addr, r1->ai_addrlen);
+               assert(i == 0);
+               *sp = s;
+               sp++;
+               nsp--;
+       }
+
+       freeaddrinfo(r0);
+}
+
+int
+open_tcp(const char *port)
+{
+       unsigned u;
+
+       for (u = 0; u < HERITAGE_NSOCKS; u++) {
+               heritage.sock_local[u] = -1;
+               heritage.sock_remote[u] = -1;
+       }
+
+       create_listen_socket("localhost", port,
+           &heritage.sock_local[0], HERITAGE_NSOCKS);
+
+       create_listen_socket(NULL, port,
+           &heritage.sock_remote[0], HERITAGE_NSOCKS);
+
+       return (0);
+}
index ee340958b7464f0a08d874d4bca14352ee61f72e..6f77ca532ed0dc509b79e2635e1f2ba0bec29182 100644 (file)
@@ -169,6 +169,9 @@ usage(void)
 
 /*--------------------------------------------------------------------*/
 
+/* for development purposes */
+#include <printf.h>
+
 int
 main(int argc, char *argv[])
 {
@@ -176,6 +179,10 @@ main(int argc, char *argv[])
        unsigned portnumber = 8080;
        unsigned dflag = 1;     /* XXX: debug=on for now */
 
+       register_printf_render_std((const unsigned char *)"HVQ");
+
+       open_tcp("8080");
+
        while ((o = getopt(argc, argv, "dp:")) != -1)
                switch (o) {
                case 'd':