From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from earwig.elm.relay.mailchannels.net (earwig.elm.relay.mailchannels.net [23.83.212.54]) by sourceware.org (Postfix) with ESMTPS id C74D63857B9B for ; Sat, 16 Sep 2023 00:49:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C74D63857B9B Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=sourceware.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id D12DE2C169A; Sat, 16 Sep 2023 00:49:15 +0000 (UTC) Received: from pdx1-sub0-mail-a258.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 559F92C196F; Sat, 16 Sep 2023 00:49:15 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1694825355; a=rsa-sha256; cv=none; b=0WSVFOnmo6ho5c9zdVtKWxmmsCH3C3AK3sczWVNgoORrm7DSWYrobWe5UQM2Nn6WTezT42 KKV9rxvWu24hRLkVqBH8QYdsmSAhg+nU9iH/x7B9xwwgH2pvWR7e2SCUm1lRH1gQv/+YPi KgOSTCql3ek5RbhJsx6eK6nMctp/UI6f6NXmm7WZXW+BL88BAZua5mnMb0ZpU1wxPO5aPH +OCMSy5OmV9Yi+0gXbkxDkxDec9fG2EXhNeP05muuorjrAp54NwNY3CyUGRrNqqZ8Kj6/D 81OXFfnNsRU0hbyc1alfdpnem0ogo7qCjX7taHjFHotar56LgLTg7E0g7o0mtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1694825355; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=El43pOl9sBdAc08GNvTEU0fHTr9SlhqN8Crr/Y8Xn84=; b=2sxC5v31RVnWwDYhXl6Fs9zVChbgj1/65gE+l+2BQwip4Z6XttQ03nbkRppjKkaGyDUTDY a40pHtdg1Ttt9GZeTGYVFGpwwmiVXe68Jo+lc3g1AaCsNN17lMRlEvsUHFx9nbWxoLiH3G dwNe/yp5VIeSYBvb9rrwOFJQxOMZtRLCO0z0o0lg2F2Ux5vswumi16QA346Mp9L19wti4E UOKx2tKx8L9uef6oFcGMoafLpvCowEDdJDkeFLlJ9kUn/TSoo9/n0Msg3h1kEwjfFkSKfS MJCMPxgwaga+N3f5CYdZ9MP9lscOSuMyUhcOOlpEfaEv3szy0q02qze2Q1dAUg== ARC-Authentication-Results: i=1; rspamd-7c449d4847-tb2tm; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Good X-MC-Copy: stored-urls X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Bubble-Print: 6883fd7900ed38b6_1694825355581_1620070375 X-MC-Loop-Signature: 1694825355581:1402383641 X-MC-Ingress-Time: 1694825355581 Received: from pdx1-sub0-mail-a258.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.125.77.17 (trex/6.9.1); Sat, 16 Sep 2023 00:49:15 +0000 Received: from fedora.redhat.com (bras-vprn-toroon4834w-lp130-02-142-113-138-41.dsl.bell.ca [142.113.138.41]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a258.dreamhost.com (Postfix) with ESMTPSA id 4RnXWG6mCFz9F; Fri, 15 Sep 2023 17:49:14 -0700 (PDT) From: Siddhesh Poyarekar To: libc-stable@sourceware.org Cc: DJ Delorie Subject: [pushed 2.34 12/14] gaih_inet: Split result generation into its own function Date: Fri, 15 Sep 2023 20:48:59 -0400 Message-ID: <20230916004902.1151607-13-siddhesh@sourceware.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230916004902.1151607-1-siddhesh@sourceware.org> References: <20230916004902.1151607-1-siddhesh@sourceware.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1172.4 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_NONE,KAM_DMARC_STATUS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_SOFTFAIL,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Simplify the loop a wee bit and clean up variable names too. Signed-off-by: Siddhesh Poyarekar Reviewed-by: DJ Delorie (cherry picked from commit ac4653ef503d1e87893d1a6714748a1cdf4bf7ad) --- sysdeps/posix/getaddrinfo.c | 176 ++++++++++++++++++------------------ 1 file changed, 86 insertions(+), 90 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index 827c43b369..1008f24736 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); -- 2.41.0