Commit fc2aed27 authored by Dridi Boukelmoune's avatar Dridi Boukelmoune

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.
parent 937ef0ec
......@@ -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);
......@@ -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);
}
......
......@@ -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);
}
......@@ -206,7 +206,7 @@ vmod_ip(VRT_CTX, struct VARGS(ip) *a)
return (NULL);
}
retval = VSS_ResolveOne(p, a->s, "80", PF_UNSPEC, SOCK_STREAM,
retval = VSS_ResolveFirst(p, a->s, "80", PF_UNSPEC, SOCK_STREAM,
a->resolve ? 0 : AI_NUMERICHOST);
if (retval != NULL)
return (retval);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment