From 048b7f143deddd145c414dd6be55285c222cc91f Mon Sep 17 00:00:00 2001 From: phk Date: Mon, 16 Jun 2008 21:25:36 +0000 Subject: [PATCH] Add VSS_open(const char *str) for when you just want a connection and don't really care about addresses and all that. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2701 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/include/vss.h | 1 + varnish-cache/lib/libvarnish/vss.c | 39 +++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/varnish-cache/include/vss.h b/varnish-cache/include/vss.h index 49abeeac..f20194db 100644 --- a/varnish-cache/include/vss.h +++ b/varnish-cache/include/vss.h @@ -35,3 +35,4 @@ int VSS_resolve(const char *addr, const char *port, struct vss_addr ***ta); int VSS_bind(const struct vss_addr *addr); int VSS_listen(const struct vss_addr *addr, int depth); int VSS_connect(const struct vss_addr *addr); +int VSS_open(const char *str); diff --git a/varnish-cache/lib/libvarnish/vss.c b/varnish-cache/lib/libvarnish/vss.c index 2f9c201b..368d590f 100644 --- a/varnish-cache/lib/libvarnish/vss.c +++ b/varnish-cache/lib/libvarnish/vss.c @@ -124,6 +124,8 @@ VSS_parse(const char *str, char **addr, char **port) * freeing each individual struct vss_addr as well as the array. * * The return value is the number of addresses resoved, or zero. + * + * XXX: We need a function to free the allocated addresses. */ int VSS_resolve(const char *addr, const char *port, struct vss_addr ***vap) @@ -240,7 +242,8 @@ VSS_connect(const struct vss_addr *va) sd = socket(va->va_family, va->va_socktype, va->va_protocol); if (sd < 0) { - perror("socket()"); + if (errno != EPROTONOSUPPORT) + perror("socket()"); return (-1); } if (connect(sd, &va->va_addr.sa, va->va_addrlen) != 0) { @@ -250,3 +253,37 @@ VSS_connect(const struct vss_addr *va) } return (sd); } + +/* + * And the totally brutal version: Give me connection to this address + */ + +int +VSS_open(const char *str) +{ + int retval; + char *addr = NULL, *port = NULL; + int nvaddr, n; + struct vss_addr **vaddr; + + retval = VSS_parse(str, &addr, &port); + if (retval < 0) + return (retval); + nvaddr = VSS_resolve(addr, port, &vaddr); + if (nvaddr <= 0) { + free(addr); + free(port); + return (-1); + } + for (n = 0; n < nvaddr; n++) { + retval = VSS_connect(vaddr[n]); + if (retval >= 0) + break; + } + for (n = 0; n < nvaddr; n++) + free(vaddr[n]); + free(vaddr); + free(addr); + free(port); + return (retval); +} -- 2.39.5