Commit a7f46d14 authored by Dridi Boukelmoune's avatar Dridi Boukelmoune

Extract the resolution logic to vss_resolve

It can now be shared by established callback-based resolvers and the new
VSS_ResolveOne. This also changes the semantics of VSS_ResolveOne in the
sense that the port is now a default port, overriden by the address if
it contains one.

Also make it clear that VTCP was already relying on a VSS function that
didn't and still doesn't allow a null errp argument, while conversely
all VTCP_open call sites provide a valid errp argument.
parent bea21ef0
...@@ -110,6 +110,8 @@ client_tcp_connect(struct vtclog *vl, const char *addr, double tmo, ...@@ -110,6 +110,8 @@ client_tcp_connect(struct vtclog *vl, const char *addr, double tmo,
int fd; int fd;
char mabuf[32], mpbuf[32]; char mabuf[32], mpbuf[32];
AN(addr);
AN(errp);
fd = VTCP_open(addr, NULL, tmo, errp); fd = VTCP_open(addr, NULL, tmo, errp);
if (fd < 0) if (fd < 0)
return fd; return fd;
......
...@@ -120,6 +120,8 @@ haproxy_cli_tcp_connect(struct vtclog *vl, const char *addr, vtim_dur tmo, ...@@ -120,6 +120,8 @@ haproxy_cli_tcp_connect(struct vtclog *vl, const char *addr, vtim_dur tmo,
int fd; int fd;
char mabuf[32], mpbuf[32]; char mabuf[32], mpbuf[32];
AN(addr);
AN(errp);
fd = VTCP_open(addr, NULL, tmo, errp); fd = VTCP_open(addr, NULL, tmo, errp);
if (fd < 0) if (fd < 0)
return fd; return fd;
......
...@@ -98,6 +98,43 @@ vss_parse(char *str, char **addr, char **port) ...@@ -98,6 +98,43 @@ vss_parse(char *str, char **addr, char **port)
return (NULL); return (NULL);
} }
static int
vss_resolve(const char *addr, const char *def_port, int family, int socktype,
int flags, struct addrinfo **res, const char **errp)
{
struct addrinfo hints;
char *p = NULL, *hp, *pp;
int ret;
AN(addr);
AN(res);
AZ(*res);
AN(errp);
*errp = NULL;
memset(&hints, 0, sizeof hints);
hints.ai_family = family;
hints.ai_socktype = socktype;
hints.ai_flags = flags;
p = strdup(addr);
AN(p);
*errp = vss_parse(p, &hp, &pp);
if (*errp != NULL) {
free(p);
return (-1);
}
if (pp != NULL)
def_port = pp;
ret = getaddrinfo(hp, def_port, &hints, res);
free(p);
if (ret != 0)
*errp = gai_strerror(ret);
return (ret);
}
/* /*
* Look up an address, using a default port if provided, and call * Look up an address, using a default port if provided, and call
* the callback function with the suckaddrs we find. * the callback function with the suckaddrs we find.
...@@ -107,34 +144,21 @@ vss_parse(char *str, char **addr, char **port) ...@@ -107,34 +144,21 @@ vss_parse(char *str, char **addr, char **port)
int int
VSS_resolver_socktype(const char *addr, const char *def_port, VSS_resolver_socktype(const char *addr, const char *def_port,
vss_resolved_f *func, void *priv, const char **err, int socktype) vss_resolved_f *func, void *priv, const char **errp, int socktype)
{ {
struct addrinfo hints, *res0, *res; struct addrinfo *res0 = NULL, *res;
struct suckaddr *vsa; struct suckaddr *vsa;
char *h;
char *adp, *hop;
int ret; int ret;
*err = NULL; AN(addr);
h = strdup(addr); AN(func);
AN(h); AN(errp);
*err = vss_parse(h, &hop, &adp);
if (*err != NULL) {
free(h);
return (-1);
}
if (adp != NULL)
def_port = adp;
memset(&hints, 0, sizeof hints); ret = vss_resolve(addr, def_port, AF_UNSPEC, socktype, AI_PASSIVE,
hints.ai_socktype = socktype; &res0, errp);
hints.ai_flags = AI_PASSIVE; if (ret != 0)
ret = getaddrinfo(hop, def_port, &hints, &res0);
free(h);
if (ret != 0) {
*err = gai_strerror(ret);
return (-1); return (-1);
}
for (res = res0; res != NULL; res = res->ai_next) { for (res = res0; res != NULL; res = res->ai_next) {
vsa = VSA_Malloc(res->ai_addr, res->ai_addrlen); vsa = VSA_Malloc(res->ai_addr, res->ai_addrlen);
if (vsa != NULL) { if (vsa != NULL) {
...@@ -150,40 +174,25 @@ VSS_resolver_socktype(const char *addr, const char *def_port, ...@@ -150,40 +174,25 @@ VSS_resolver_socktype(const char *addr, const char *def_port,
int int
VSS_resolver(const char *addr, const char *def_port, vss_resolved_f *func, VSS_resolver(const char *addr, const char *def_port, vss_resolved_f *func,
void *priv, const char **err) void *priv, const char **errp)
{ {
return (VSS_resolver_socktype( return (VSS_resolver_socktype(
addr, def_port, func, priv, err, SOCK_STREAM)); addr, def_port, func, priv, errp, SOCK_STREAM));
} }
struct suckaddr * struct suckaddr *
VSS_ResolveOne(void *dst, const char *addr, const char *port, VSS_ResolveOne(void *dst, const char *addr, const char *def_port,
int family, int socktype, int flags) int family, int socktype, int flags)
{ {
struct addrinfo hints, *res = NULL; struct addrinfo *res = NULL;
struct suckaddr *retval = NULL; struct suckaddr *retval = NULL;
char *p = NULL, *hp, *pp; const char *err;
int error; int ret;
memset(&hints, 0, sizeof hints);
hints.ai_family = family;
hints.ai_socktype = socktype;
hints.ai_flags = flags;
AN(addr); AN(addr);
if (port != NULL) { ret = vss_resolve(addr, def_port, family, socktype, flags, &res, &err);
error = getaddrinfo(addr, port, &hints, &res); if (ret == 0 && res != NULL && res->ai_next == NULL) {
} else { AZ(err);
p = strdup(addr);
AN(p);
if (vss_parse(p, &hp, &pp) != NULL || pp == NULL) {
free(p);
return (NULL);
}
error = getaddrinfo(hp, pp, &hints, &res);
free(p);
}
if (!error && res != NULL && res->ai_next == NULL) {
if (dst == NULL) if (dst == NULL)
retval = VSA_Malloc(res->ai_addr, res->ai_addrlen); retval = VSA_Malloc(res->ai_addr, res->ai_addrlen);
else else
......
...@@ -381,20 +381,12 @@ int ...@@ -381,20 +381,12 @@ int
VTCP_open(const char *addr, const char *def_port, vtim_dur timeout, VTCP_open(const char *addr, const char *def_port, vtim_dur timeout,
const char **errp) const char **errp)
{ {
int error;
const char *err;
if (errp != NULL) AN(errp);
*errp = NULL;
assert(timeout >= 0); assert(timeout >= 0);
error = VSS_resolver(addr, def_port, vtcp_open_callback,
&timeout, &err); return (VSS_resolver(addr, def_port, vtcp_open_callback,
if (err != NULL) { &timeout, errp));
if (errp != NULL)
*errp = err;
return (-1);
}
return (error);
} }
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
...@@ -513,6 +505,7 @@ VTCP_listen_on(const char *addr, const char *def_port, int depth, ...@@ -513,6 +505,7 @@ VTCP_listen_on(const char *addr, const char *def_port, int depth,
struct helper h; struct helper h;
int sock; int sock;
AN(errp);
h.depth = depth; h.depth = depth;
h.errp = errp; h.errp = errp;
......
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