]> err.no Git - varnish/commitdiff
get us closer to a connection to the backend
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 30 Mar 2006 11:16:27 +0000 (11:16 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 30 Mar 2006 11:16:27 +0000 (11:16 +0000)
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@88 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/cache_backend.c
varnish-cache/bin/varnishd/varnishd.c

index 84f8fe08c676a4de5afae54435398d70739ce7fd..f1ffc39019ec1340b4602dcbf150677b64fd48ec 100644 (file)
@@ -3,8 +3,15 @@
  */
 
 #include <assert.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
 #include <stdlib.h>
 #include <sys/queue.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+
 #include "libvarnish.h"
 #include "vcl_lang.h"
 
@@ -32,10 +39,47 @@ static TAILQ_HEAD(,vbe) vbe_head = TAILQ_HEAD_INITIALIZER(vbe_head);
 
 static pthread_mutex_t vbemtx;
 
-/*--------------------------------------------------------------------*/
+/*--------------------------------------------------------------------
+ * XXX: we should not call getaddrinfo() every time, we should cache
+ * and apply round-robin with blacklisting of entries that do not respond
+ * etc.  Periodic re-lookups to capture changed DNS records would also 
+ * be a good thing in that case.
+ */
+
 void
 connect_to_backend(struct vbe_conn *vc, struct backend *bp)
 {
+       struct addrinfo *res, *res0, hint;
+       int error, s;
+
+       assert(bp != NULL);
+       assert(bp->hostname != NULL);
+       memset(&hint, 0, sizeof hint);
+       hint.ai_family = PF_UNSPEC;
+       hint.ai_socktype = SOCK_STREAM;
+       error = getaddrinfo(bp->hostname,
+           bp->portname == NULL ? "http" : bp->portname,
+           &hint, &res);
+       if (error) {
+               fprintf(stderr, "getaddrinfo: %s\n", 
+                   gai_strerror(error));
+               return;
+       }
+       res0 = res;
+       do {
+               s = socket(res0->ai_family, res0->ai_socktype,
+                   res0->ai_protocol);
+               if (s < 0)
+                       continue;
+               error = connect(s, res0->ai_addr, res0->ai_addrlen);
+               if (!error)
+                       break;
+               close(s);
+               s = -1;
+       } while ((res0 = res0->ai_next) != NULL);
+       freeaddrinfo(res);
+       vc->fd = s;
+       return;
 }
 
 /*--------------------------------------------------------------------*/
@@ -73,6 +117,7 @@ VBE_GetFd(struct backend *bp)
        vc = calloc(sizeof *vc, 1);
        assert(vc != NULL);
        vc->vbe = vp;
+       vc->fd = -1;
        TAILQ_INSERT_TAIL(&vp->bconn, vc, list);
        AZ(pthread_mutex_unlock(&vbemtx));
        connect_to_backend(vc, bp);
@@ -81,9 +126,6 @@ VBE_GetFd(struct backend *bp)
        return (-1);
 }
 
-
-
-
 void
 VBE_Init(void)
 {
index 3ee9f9b8b894b584cd56881ed18f2f8a7a480aac..f2c9e482145dbf261de36a5cedbd1ec2f490e4b2 100644 (file)
@@ -68,7 +68,9 @@ vcl_default(const char *bflag)
 
        buf = NULL;
        asprintf(&buf,
-           "backend default { set backend.host = \"%s\"; }\n"
+           "backend default {\n"
+           "    set backend.host = \"%s\";\n"
+           "}\n"
            "sub main {\n"
            "    pass;\n"
 #if 0