From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dog.birch.relay.mailchannels.net (dog.birch.relay.mailchannels.net [23.83.209.48]) by sourceware.org (Postfix) with ESMTPS id E334A385AC22 for ; Mon, 14 Mar 2022 09:49:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E334A385AC22 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 7838A880896 for ; Mon, 14 Mar 2022 09:49:12 +0000 (UTC) Received: from pdx1-sub0-mail-a305.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 1A7DE88150B for ; Mon, 14 Mar 2022 09:49:12 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1647251352; a=rsa-sha256; cv=none; b=8IdMPml3e87xMsrFTYqmNtye44cEjS47OgKQ42CHC7gLNhUZ4gunUW41IPdyDbqiSQ31Jr zn6S8ckIGnn2tAeBL3CqqX4soIUL/8UPGU/WyQQf2hzlhNHkol4YKjRbkjQvya2+hkXGIj xbhRP3JtVaZx4m2pHAE2s5nEI5h7gUkEnNA6B6HMxBfJpQ8mDA+TIoayLRAOEmUJxOvKBg AD3fnYfBI2VzK522axyUp6YlOjGR6F5yDYhAWrYrznk+qRfkrBNeoKpLkCXCamMB4E3lX4 xIteFuxcOFLSlhBcwIu6YYXGYWImGkAWYYrLTREK2YxtRJQXa9CunyZv8AXCxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1647251352; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i7U15UcL1n+7fDVL15qnZ6JjwMQJIiOgGQOiGKkrP5w=; b=WpajN2+JBgowCJOkN77BZWBDBGqSTwSwjytMUO4Ym/+vGZi9D9u07uuYqMMXTdsFhee9rV M7BCTUrnEjD9O8Q2kVJpgxR6Ndnkj0ALYgKQ4LyMh49Ap3uNjKRK3Xu8DN1zQyz92omlul yAKudgnCVTwW2FjWYzyZ35wDU8JdjDZkE2ouEUOpmfSDc2LiruBbM92ELLJ+ON7NubNSG9 KQDRrZRndiCE9G0p7kXWtEZlBRymyeO3QU/umfGNAImDd9c59hU+WBUMr0q8/OcwoUEive 0V1ikicWMR3SjO3hel036mYY586gEXUDdtNO4qvQScYWQqNPh28O9+QcMWWe4g== ARC-Authentication-Results: i=1; rspamd-c9cb649d9-tk4s9; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a305.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.119.141.198 (trex/6.5.3); Mon, 14 Mar 2022 09:49:12 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Belong-Reign: 0e089db240e3691c_1647251352324_1093344645 X-MC-Loop-Signature: 1647251352323:1124669608 X-MC-Ingress-Time: 1647251352323 Received: from rhbox.redhat.com (unknown [106.220.192.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a305.dreamhost.com (Postfix) with ESMTPSA id 4KHBZH5Qxkz1ND for ; Mon, 14 Mar 2022 02:48:55 -0700 (PDT) From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Subject: [PATCH v2 06/12] gaih_inet: Split simple gethostbyname into its own function Date: Mon, 14 Mar 2022 15:18:29 +0530 Message-Id: <20220314094835.1159523-7-siddhesh@sourceware.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314094835.1159523-1-siddhesh@sourceware.org> References: <20220308100717.1006126-1-siddhesh@sourceware.org> <20220314094835.1159523-1-siddhesh@sourceware.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1171.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Mar 2022 09:49:16 -0000 Add a free_at flag in gaih_result to indicate if res.at needs to be freed by the caller. Signed-off-by: Siddhesh Poyarekar --- sysdeps/posix/getaddrinfo.c | 125 ++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 63 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index d7b6eae9fc..bcceab7d07 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -120,6 +120,7 @@ struct gaih_result { struct gaih_addrtuple *at; char *canon; + bool free_at; }; /* Values for `protoflag'. */ @@ -565,6 +566,60 @@ out: return result; } +/* If possible, call the simple, old functions, which do not support IPv6 scope + ids, nor retrieving the canonical name. */ + +static int +try_simple_gethostbyname (const char *name, const struct addrinfo *req, + struct scratch_buffer *tmpbuf, + struct gaih_result *res) +{ + res->at = NULL; + + if (req->ai_family != AF_INET || (req->ai_flags & AI_CANONNAME) != 0) + return 0; + + int rc; + struct hostent th; + struct hostent *h; + + while (1) + { + rc = __gethostbyname2_r (name, AF_INET, &th, tmpbuf->data, + tmpbuf->length, &h, &h_errno); + if (rc != ERANGE || h_errno != NETDB_INTERNAL) + break; + if (!scratch_buffer_grow (tmpbuf)) + return -EAI_MEMORY; + } + + if (rc == 0) + { + if (h != NULL) + { + /* We found data, convert it. */ + if (!convert_hostent_to_gaih_addrtuple (req, AF_INET, h, &res->at)) + return -EAI_MEMORY; + + res->free_at = true; + return 0; + } + if (h_errno == NO_DATA) + return -EAI_NODATA; + + return -EAI_NONAME; + } + + if (h_errno == NETDB_INTERNAL) + return -EAI_SYSTEM; + if (h_errno == TRY_AGAIN) + return -EAI_AGAIN; + + /* We made requests but they turned out no data. + The name is known, though. */ + return -EAI_NODATA; +} + static int gaih_inet (const char *name, const struct gaih_service *service, const struct addrinfo *req, struct addrinfo **pai, @@ -610,6 +665,11 @@ gaih_inet (const char *name, const struct gaih_service *service, else if (res.at != NULL) goto process_list; + if ((result = try_simple_gethostbyname (name, req, tmpbuf, &res)) != 0) + goto free_and_return; + else if (res.at != NULL) + goto process_list; + int no_data = 0; int no_inet6_data = 0; nss_action_list nip; @@ -619,69 +679,6 @@ gaih_inet (const char *name, const struct gaih_service *service, struct resolv_context *res_ctx = NULL; bool do_merge = false; - /* If we do not have to look for IPv6 addresses or the canonical - name, use the simple, old functions, which do not support - IPv6 scope ids, nor retrieving the canonical name. */ - if (req->ai_family == AF_INET - && (req->ai_flags & AI_CANONNAME) == 0) - { - int rc; - struct hostent th; - struct hostent *h; - - while (1) - { - rc = __gethostbyname2_r (name, AF_INET, &th, - tmpbuf->data, tmpbuf->length, - &h, &h_errno); - if (rc != ERANGE || h_errno != NETDB_INTERNAL) - break; - if (!scratch_buffer_grow (tmpbuf)) - { - result = -EAI_MEMORY; - goto free_and_return; - } - } - - if (rc == 0) - { - if (h != NULL) - { - /* We found data, convert it. */ - if (!convert_hostent_to_gaih_addrtuple - (req, AF_INET, h, &addrmem)) - { - result = -EAI_MEMORY; - goto free_and_return; - } - res.at = addrmem; - } - else - { - if (h_errno == NO_DATA) - result = -EAI_NODATA; - else - result = -EAI_NONAME; - goto free_and_return; - } - } - else - { - if (h_errno == NETDB_INTERNAL) - result = -EAI_SYSTEM; - else if (h_errno == TRY_AGAIN) - result = -EAI_AGAIN; - else - /* We made requests but they turned out no data. - The name is known, though. */ - result = -EAI_NODATA; - - goto free_and_return; - } - - goto process_list; - } - #ifdef USE_NSCD if (__nss_not_use_nscd_hosts > 0 && ++__nss_not_use_nscd_hosts > NSS_NSCD_RETRY) @@ -1164,6 +1161,8 @@ gaih_inet (const char *name, const struct gaih_service *service, if (malloc_name) free ((char *) name); free (addrmem); + if (res.free_at) + free (res.at); free (res.canon); return result; -- 2.35.1