You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
209 lines
5.9 KiB
209 lines
5.9 KiB
commit 92478a808f477480adbc5ca3d9a4a1bc27fc13ae
|
|
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
|
|
Date: Mon Mar 7 20:38:31 2022 +0530
|
|
|
|
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>
|
|
(cherry picked from commit ac4653ef503d1e87893d1a6714748a1cdf4bf7ad)
|
|
|
|
diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
|
|
index 827c43b369836de9..1008f247365ea009 100644
|
|
--- a/sysdeps/posix/getaddrinfo.c
|
|
+++ b/sysdeps/posix/getaddrinfo.c
|
|
@@ -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;
|
|
-
|
|
- struct gaih_addrtuple *at2 = res.at;
|
|
- size_t socklen;
|
|
- sa_family_t family;
|
|
-
|
|
- /*
|
|
- 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;
|
|
+ /* Set up the canonical name if we need it. */
|
|
+ if ((result = process_canonname (req, orig_name, &res)) != 0)
|
|
+ goto free_and_return;
|
|
|
|
- ignore:
|
|
- at2 = at2->next;
|
|
- }
|
|
- }
|
|
+ result = generate_addrinfo (req, &res, st, pai, naddrs);
|
|
|
|
- free_and_return:
|
|
+free_and_return:
|
|
if (malloc_name)
|
|
free ((char *) name);
|
|
free (addrmem);
|