mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-24 03:31:07 +00:00
gaih_inet: Split result generation into its own function
Simplify the loop a wee bit and clean up variable names too. Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org> Reviewed-by: DJ Delorie <dj@redhat.com>
This commit is contained in:
parent
657472b2a5
commit
ac4653ef50
@ -1030,6 +1030,87 @@ get_local_addresses (const struct addrinfo *req, struct gaih_result *res)
|
||||
}
|
||||
}
|
||||
|
||||
/* Generate results in PAI and its count in NADDRS. Return 0 on success or an
|
||||
error code on failure. */
|
||||
|
||||
static int
|
||||
generate_addrinfo (const struct addrinfo *req, struct gaih_result *res,
|
||||
const struct gaih_servtuple *st, struct addrinfo **pai,
|
||||
unsigned int *naddrs)
|
||||
{
|
||||
size_t socklen;
|
||||
sa_family_t family;
|
||||
|
||||
/* Buffer is the size of an unformatted IPv6 address in printable format. */
|
||||
for (struct gaih_addrtuple *at = res->at; at != NULL; at = at->next)
|
||||
{
|
||||
family = at->family;
|
||||
if (family == AF_INET6)
|
||||
{
|
||||
socklen = sizeof (struct sockaddr_in6);
|
||||
|
||||
/* If we looked up IPv4 mapped address discard them here if
|
||||
the caller isn't interested in all address and we have
|
||||
found at least one IPv6 address. */
|
||||
if (res->got_ipv6
|
||||
&& (req->ai_flags & (AI_V4MAPPED|AI_ALL)) == AI_V4MAPPED
|
||||
&& IN6_IS_ADDR_V4MAPPED (at->addr))
|
||||
continue;
|
||||
}
|
||||
else
|
||||
socklen = sizeof (struct sockaddr_in);
|
||||
|
||||
for (int i = 0; st[i].set; i++)
|
||||
{
|
||||
struct addrinfo *ai;
|
||||
ai = *pai = malloc (sizeof (struct addrinfo) + socklen);
|
||||
if (ai == NULL)
|
||||
return -EAI_MEMORY;
|
||||
|
||||
ai->ai_flags = req->ai_flags;
|
||||
ai->ai_family = family;
|
||||
ai->ai_socktype = st[i].socktype;
|
||||
ai->ai_protocol = st[i].protocol;
|
||||
ai->ai_addrlen = socklen;
|
||||
ai->ai_addr = (void *) (ai + 1);
|
||||
|
||||
/* We only add the canonical name once. */
|
||||
ai->ai_canonname = res->canon;
|
||||
res->canon = NULL;
|
||||
|
||||
#ifdef _HAVE_SA_LEN
|
||||
ai->ai_addr->sa_len = socklen;
|
||||
#endif /* _HAVE_SA_LEN */
|
||||
ai->ai_addr->sa_family = family;
|
||||
|
||||
/* In case of an allocation error the list must be NULL
|
||||
terminated. */
|
||||
ai->ai_next = NULL;
|
||||
|
||||
if (family == AF_INET6)
|
||||
{
|
||||
struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *) ai->ai_addr;
|
||||
sin6p->sin6_port = st[i].port;
|
||||
sin6p->sin6_flowinfo = 0;
|
||||
memcpy (&sin6p->sin6_addr, at->addr, sizeof (struct in6_addr));
|
||||
sin6p->sin6_scope_id = at->scopeid;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct sockaddr_in *sinp = (struct sockaddr_in *) ai->ai_addr;
|
||||
sinp->sin_port = st[i].port;
|
||||
memcpy (&sinp->sin_addr, at->addr, sizeof (struct in_addr));
|
||||
memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
|
||||
}
|
||||
|
||||
pai = &(ai->ai_next);
|
||||
}
|
||||
|
||||
++*naddrs;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
gaih_inet (const char *name, const struct gaih_service *service,
|
||||
const struct addrinfo *req, struct addrinfo **pai,
|
||||
@ -1096,98 +1177,13 @@ gaih_inet (const char *name, const struct gaih_service *service,
|
||||
goto free_and_return;
|
||||
|
||||
process_list:
|
||||
{
|
||||
/* Set up the canonical name if we need it. */
|
||||
if ((result = process_canonname (req, orig_name, &res)) != 0)
|
||||
goto free_and_return;
|
||||
/* Set up the canonical name if we need it. */
|
||||
if ((result = process_canonname (req, orig_name, &res)) != 0)
|
||||
goto free_and_return;
|
||||
|
||||
struct gaih_addrtuple *at2 = res.at;
|
||||
size_t socklen;
|
||||
sa_family_t family;
|
||||
result = generate_addrinfo (req, &res, st, pai, naddrs);
|
||||
|
||||
/*
|
||||
buffer is the size of an unformatted IPv6 address in printable format.
|
||||
*/
|
||||
while (at2 != NULL)
|
||||
{
|
||||
family = at2->family;
|
||||
if (family == AF_INET6)
|
||||
{
|
||||
socklen = sizeof (struct sockaddr_in6);
|
||||
|
||||
/* If we looked up IPv4 mapped address discard them here if
|
||||
the caller isn't interested in all address and we have
|
||||
found at least one IPv6 address. */
|
||||
if (res.got_ipv6
|
||||
&& (req->ai_flags & (AI_V4MAPPED|AI_ALL)) == AI_V4MAPPED
|
||||
&& IN6_IS_ADDR_V4MAPPED (at2->addr))
|
||||
goto ignore;
|
||||
}
|
||||
else
|
||||
socklen = sizeof (struct sockaddr_in);
|
||||
|
||||
for (int i = 0; st[i].set; i++)
|
||||
{
|
||||
struct addrinfo *ai;
|
||||
ai = *pai = malloc (sizeof (struct addrinfo) + socklen);
|
||||
if (ai == NULL)
|
||||
{
|
||||
result = -EAI_MEMORY;
|
||||
goto free_and_return;
|
||||
}
|
||||
|
||||
ai->ai_flags = req->ai_flags;
|
||||
ai->ai_family = family;
|
||||
ai->ai_socktype = st[i].socktype;
|
||||
ai->ai_protocol = st[i].protocol;
|
||||
ai->ai_addrlen = socklen;
|
||||
ai->ai_addr = (void *) (ai + 1);
|
||||
|
||||
/* We only add the canonical name once. */
|
||||
ai->ai_canonname = res.canon;
|
||||
res.canon = NULL;
|
||||
|
||||
#ifdef _HAVE_SA_LEN
|
||||
ai->ai_addr->sa_len = socklen;
|
||||
#endif /* _HAVE_SA_LEN */
|
||||
ai->ai_addr->sa_family = family;
|
||||
|
||||
/* In case of an allocation error the list must be NULL
|
||||
terminated. */
|
||||
ai->ai_next = NULL;
|
||||
|
||||
if (family == AF_INET6)
|
||||
{
|
||||
struct sockaddr_in6 *sin6p =
|
||||
(struct sockaddr_in6 *) ai->ai_addr;
|
||||
|
||||
sin6p->sin6_port = st[i].port;
|
||||
sin6p->sin6_flowinfo = 0;
|
||||
memcpy (&sin6p->sin6_addr,
|
||||
at2->addr, sizeof (struct in6_addr));
|
||||
sin6p->sin6_scope_id = at2->scopeid;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct sockaddr_in *sinp =
|
||||
(struct sockaddr_in *) ai->ai_addr;
|
||||
sinp->sin_port = st[i].port;
|
||||
memcpy (&sinp->sin_addr,
|
||||
at2->addr, sizeof (struct in_addr));
|
||||
memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
|
||||
}
|
||||
|
||||
pai = &(ai->ai_next);
|
||||
}
|
||||
|
||||
++*naddrs;
|
||||
|
||||
ignore:
|
||||
at2 = at2->next;
|
||||
}
|
||||
}
|
||||
|
||||
free_and_return:
|
||||
free_and_return:
|
||||
if (malloc_name)
|
||||
free ((char *) name);
|
||||
free (addrmem);
|
||||
|
Loading…
Reference in New Issue
Block a user