[6.0] d02d21971 Add a VSS_ResolveFirst primitive as well
Dridi Boukelmoune
dridi.boukelmoune at gmail.com
Wed May 22 09:03:11 UTC 2019
commit d02d21971a99d696e2976b82e254d4da8ff299e8
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date: Thu May 9 20:06:42 2019 +0200
Add a VSS_ResolveFirst primitive as well
This is how std.ip is documented, so VSS_ResolveOne doesn't work there.
It might not be the only migration to VSS_ResolveOne that requires
attention.
Speaking of attention, VSA_Malloc may require some.
Conflicts:
lib/libvmod_std/vmod_std_conversions.c
diff --git a/include/vss.h b/include/vss.h
index ab4cdc33d..bf1fb2b6d 100644
--- a/include/vss.h
+++ b/include/vss.h
@@ -37,3 +37,6 @@ int VSS_resolver_socktype(const char *addr, const char *def_port,
struct suckaddr *VSS_ResolveOne(void *dst,
const char *addr, const char *port,
int family, int socktype, int flags);
+struct suckaddr *VSS_ResolveFirst(void *dst,
+ const char *addr, const char *port,
+ int family, int socktype, int flags);
diff --git a/lib/libvarnish/vsa.c b/lib/libvarnish/vsa.c
index 40d84fa83..296f01374 100644
--- a/lib/libvarnish/vsa.c
+++ b/lib/libvarnish/vsa.c
@@ -252,6 +252,10 @@ VSA_Malloc(const void *s, unsigned sal)
}
if (l != 0) {
ALLOC_OBJ(sua, SUCKADDR_MAGIC);
+ /* XXX: shouldn't we AN(sua) instead of mixing up failed
+ * allocations with unsupported address family or bogus
+ * sockaddr?
+ */
if (sua != NULL)
memcpy(&sua->sa, s, l);
}
diff --git a/lib/libvarnish/vss.c b/lib/libvarnish/vss.c
index 206b5acb3..a37879a97 100644
--- a/lib/libvarnish/vss.c
+++ b/lib/libvarnish/vss.c
@@ -135,6 +135,17 @@ vss_resolve(const char *addr, const char *def_port, int family, int socktype,
return (ret);
}
+static struct suckaddr *
+vss_alloc_suckaddr(void *dst, const struct addrinfo *ai)
+{
+
+ AN(ai);
+ if (dst == NULL)
+ return (VSA_Malloc(ai->ai_addr, ai->ai_addrlen));
+
+ return (VSA_Build(dst, ai->ai_addr, ai->ai_addrlen));
+}
+
/*
* Look up an address, using a default port if provided, and call
* the callback function with the suckaddrs we find.
@@ -193,12 +204,33 @@ VSS_ResolveOne(void *dst, const char *addr, const char *def_port,
ret = vss_resolve(addr, def_port, family, socktype, flags, &res, &err);
if (ret == 0 && res != NULL && res->ai_next == NULL) {
AZ(err);
- if (dst == NULL)
- retval = VSA_Malloc(res->ai_addr, res->ai_addrlen);
- else
- retval = VSA_Build(dst, res->ai_addr, res->ai_addrlen);
+ retval = vss_alloc_suckaddr(dst, res);
}
if (res != NULL)
freeaddrinfo(res);
return (retval);
}
+
+struct suckaddr *
+VSS_ResolveFirst(void *dst, const char *addr, const char *def_port,
+ int family, int socktype, int flags)
+{
+ struct addrinfo *res0 = NULL, *res;
+ struct suckaddr *retval = NULL;
+ const char *err;
+ int ret;
+
+ AN(addr);
+ ret = vss_resolve(addr, def_port, family, socktype, flags, &res0, &err);
+ if (ret == 0)
+ AZ(err);
+
+ for (res = res0; res != NULL; res = res->ai_next) {
+ retval = vss_alloc_suckaddr(dst, res);
+ if (retval != NULL)
+ break;
+ }
+ if (res0 != NULL)
+ freeaddrinfo(res0);
+ return (retval);
+}
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index 49fe95e61..f3bfec539 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -94,7 +94,7 @@ vmod_ip(VRT_CTX, VCL_STRING s, VCL_IP d, VCL_BOOL n)
return (NULL);
}
- retval = VSS_ResolveOne(p, s, "80", PF_UNSPEC, SOCK_STREAM,
+ retval = VSS_ResolveFirst(p, s, "80", PF_UNSPEC, SOCK_STREAM,
n ? 0 : AI_NUMERICHOST);
if (retval != NULL)
return (retval);
More information about the varnish-commit
mailing list