From patchwork Tue Aug 3 21:29:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 44561 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4BF193861036 for ; Tue, 3 Aug 2021 21:30:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4BF193861036 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628026244; bh=SbkLEiIYQNvbuitWbLR7pM2NImGOvC57Tte1LIaWgUY=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=XE0FNC3tDTCUtUX0lOMveFBJTyRSgoEli49xDqydbczOFMQurS0Njt4qXsNrkbjis YPZqHESYT+0ZHOGNOk9HuamO4+iYlZMLLC1+6PC3lT0SraMqQLnmkzu0gdOTq1PwWS n91v/5bjP+ZDHB2QCcn/wKe2mplEQS8TYZz5beaI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from dormouse.elm.relay.mailchannels.net (dormouse.elm.relay.mailchannels.net [23.83.212.50]) by sourceware.org (Postfix) with ESMTPS id ADF5A3858C27 for ; Tue, 3 Aug 2021 21:29:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ADF5A3858C27 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 40A45101B12 for ; Tue, 3 Aug 2021 21:29:32 +0000 (UTC) Received: from pdx1-sub0-mail-a4.g.dreamhost.com (100-105-161-178.trex.outbound.svc.cluster.local [100.105.161.178]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id B6B9A101C06 for ; Tue, 3 Aug 2021 21:29:31 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a4.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.105.161.178 (trex/6.3.3); Tue, 03 Aug 2021 21:29:32 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Desert-Vacuous: 636462b06a14624f_1628026171977_1102994129 X-MC-Loop-Signature: 1628026171977:4279582436 X-MC-Ingress-Time: 1628026171977 Received: from pdx1-sub0-mail-a4.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTP id 57F858AAA8 for ; Tue, 3 Aug 2021 14:29:31 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTPSA id 304A58AAAA for ; Tue, 3 Aug 2021 14:29:29 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a4 To: libc-alpha@sourceware.org Subject: [PATCH 1/5] gaiconf_init: Refactor some bits for readability Date: Wed, 4 Aug 2021 02:59:15 +0530 Message-Id: <20210803212919.3059194-2-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210803212919.3059194-1-siddhesh@sourceware.org> References: <20210803212919.3059194-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP 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-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Split out line processing for `label`, `precedence` and `scopev4` into separate functions instead of the gotos. --- sysdeps/posix/getaddrinfo.c | 149 ++++++++++++++++++++---------------- 1 file changed, 84 insertions(+), 65 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index 9f1cde27b5..f222c2a62f 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -1761,6 +1761,66 @@ scopecmp (const void *p1, const void *p2) return 1; } +static bool +add_prefixlist (struct prefixlist **listp, size_t *lenp, bool *nullbitsp, + char *val1, char *val2, char **pos) +{ + struct in6_addr prefix; + unsigned long int bits; + unsigned long int val; + char *endp; + + bits = 128; + __set_errno (0); + char *cp = strchr (val1, '/'); + if (cp != NULL) + *cp++ = '\0'; + *pos = cp; + if (inet_pton (AF_INET6, val1, &prefix) + && (cp == NULL + || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && bits <= 128 + && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && val <= INT_MAX) + { + struct prefixlist *newp = malloc (sizeof (*newp)); + if (newp == NULL) + return false; + + memcpy (&newp->entry.prefix, &prefix, sizeof (prefix)); + newp->entry.bits = bits; + newp->entry.val = val; + newp->next = *listp; + *listp = newp; + ++*lenp; + *nullbitsp |= bits == 0; + } + return true; +} + +static bool +add_scopelist (struct scopelist **listp, size_t *lenp, bool *nullbitsp, + const struct in6_addr *prefixp, unsigned long int bits, + unsigned long int val) +{ + struct scopelist *newp = malloc (sizeof (*newp)); + if (newp == NULL) + return false; + + newp->entry.netmask = htonl (bits != 96 ? (0xffffffff << (128 - bits)) : 0); + newp->entry.addr32 = (prefixp->s6_addr32[3] & newp->entry.netmask); + newp->entry.scope = val; + newp->next = *listp; + *listp = newp; + ++*lenp; + *nullbitsp |= bits == 96; + + return true; +} static void gaiconf_init (void) @@ -1836,55 +1896,17 @@ gaiconf_init (void) /* Ignore the rest of the line. */ *cp = '\0'; - struct prefixlist **listp; - size_t *lenp; - bool *nullbitsp; switch (cmdlen) { case 5: if (strcmp (cmd, "label") == 0) { - struct in6_addr prefix; - unsigned long int bits; - unsigned long int val; - char *endp; - - listp = &labellist; - lenp = &nlabellist; - nullbitsp = &labellist_nullbits; - - new_elem: - bits = 128; - __set_errno (0); - cp = strchr (val1, '/'); - if (cp != NULL) - *cp++ = '\0'; - if (inet_pton (AF_INET6, val1, &prefix) - && (cp == NULL - || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && bits <= 128 - && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && val <= INT_MAX) + if (!add_prefixlist (&labellist, &nlabellist, + &labellist_nullbits, val1, val2, &cp)) { - struct prefixlist *newp = malloc (sizeof (*newp)); - if (newp == NULL) - { - free (line); - fclose (fp); - goto no_file; - } - - memcpy (&newp->entry.prefix, &prefix, sizeof (prefix)); - newp->entry.bits = bits; - newp->entry.val = val; - newp->next = *listp; - *listp = newp; - ++*lenp; - *nullbitsp |= bits == 0; + free (line); + fclose (fp); + goto no_file; } } break; @@ -1926,27 +1948,14 @@ gaiconf_init (void) && *endp == '\0' && val <= INT_MAX) { - struct scopelist *newp; - new_scope: - newp = malloc (sizeof (*newp)); - if (newp == NULL) + if (!add_scopelist (&scopelist, &nscopelist, + &scopelist_nullbits, &prefix, + bits, val)) { free (line); fclose (fp); goto no_file; } - - newp->entry.netmask = htonl (bits != 96 - ? (0xffffffff - << (128 - bits)) - : 0); - newp->entry.addr32 = (prefix.s6_addr32[3] - & newp->entry.netmask); - newp->entry.scope = val; - newp->next = scopelist; - scopelist = newp; - ++nscopelist; - scopelist_nullbits |= bits == 96; } } else if (inet_pton (AF_INET, val1, &prefix.s6_addr32[3]) @@ -1960,8 +1969,14 @@ gaiconf_init (void) && *endp == '\0' && val <= INT_MAX) { - bits += 96; - goto new_scope; + if (!add_scopelist (&scopelist, &nscopelist, + &scopelist_nullbits, &prefix, + bits + 96, val)) + { + free (line); + fclose (fp); + goto no_file; + } } } break; @@ -1969,10 +1984,14 @@ gaiconf_init (void) case 10: if (strcmp (cmd, "precedence") == 0) { - listp = &precedencelist; - lenp = &nprecedencelist; - nullbitsp = &precedencelist_nullbits; - goto new_elem; + if (!add_prefixlist (&precedencelist, &nprecedencelist, + &precedencelist_nullbits, val1, val2, + &cp)) + { + free (line); + fclose (fp); + goto no_file; + } } break; } From patchwork Tue Aug 3 21:29:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 44563 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 240F7396DC2F for ; Tue, 3 Aug 2021 21:32:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 240F7396DC2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628026342; bh=94Gt02XKY9yrmUT2KTAI91bgICC63a/mJM3rjN8jpqc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=fH45VzUBFr0fCdHj3H4bpEUh0KJDl0Id050ioEEkFlJEXOUu+PtcW4HgQlztIg57c OyZtd8bBA8MaqWeZGtbtRCIC8ckmM37u0LTZYzbs29ON2OrhuSETjNjaYpyEML/FeG PAhv8tcTNw+eDuk2odwlnsGC8s+BmWalY0CebWk8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from bumble.birch.relay.mailchannels.net (bumble.birch.relay.mailchannels.net [23.83.209.25]) by sourceware.org (Postfix) with ESMTPS id A0B81385800F for ; Tue, 3 Aug 2021 21:29:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A0B81385800F 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 35CC75A15E5 for ; Tue, 3 Aug 2021 21:29:34 +0000 (UTC) Received: from pdx1-sub0-mail-a4.g.dreamhost.com (100-101-162-78.trex-nlb.outbound.svc.cluster.local [100.101.162.78]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id C52A65A17E2 for ; Tue, 3 Aug 2021 21:29:33 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a4.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.101.162.78 (trex/6.3.3); Tue, 03 Aug 2021 21:29:34 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Spot-Plucky: 72ae9f4951c939cb_1628026174034_3392630672 X-MC-Loop-Signature: 1628026174034:1654015545 X-MC-Ingress-Time: 1628026174034 Received: from pdx1-sub0-mail-a4.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTP id 541D98AAB1 for ; Tue, 3 Aug 2021 14:29:33 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTPSA id 14EDA8AAB3 for ; Tue, 3 Aug 2021 14:29:31 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a4 To: libc-alpha@sourceware.org Subject: [PATCH 2/5] gai_init: Avoid jumping from if condition to its else counterpart Date: Wed, 4 Aug 2021 02:59:16 +0530 Message-Id: <20210803212919.3059194-3-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210803212919.3059194-1-siddhesh@sourceware.org> References: <20210803212919.3059194-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NEUTRAL, TXREP 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-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Clean up another antipattern where code flows from an if condition to its else counterpart with a goto. Most of the change in this patch is whitespace-only; a `git diff -b` ought to show the actual logic changes. --- sysdeps/posix/getaddrinfo.c | 516 ++++++++++++++++++------------------ 1 file changed, 257 insertions(+), 259 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index f222c2a62f..180048eec2 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -1836,142 +1836,122 @@ gaiconf_init (void) bool scopelist_nullbits = false; FILE *fp = fopen (GAICONF_FNAME, "rce"); - if (fp != NULL) + if (fp == NULL) + goto no_file; + + struct __stat64_t64 st; + if (__fstat64_time64 (fileno (fp), &st) != 0) { - struct __stat64_t64 st; - if (__fstat64_time64 (fileno (fp), &st) != 0) - { - fclose (fp); - goto no_file; - } + fclose (fp); + goto no_file; + } - char *line = NULL; - size_t linelen = 0; + char *line = NULL; + size_t linelen = 0; - __fsetlocking (fp, FSETLOCKING_BYCALLER); + __fsetlocking (fp, FSETLOCKING_BYCALLER); - while (!feof_unlocked (fp)) + while (!feof_unlocked (fp)) + { + ssize_t n = __getline (&line, &linelen, fp); + if (n <= 0) + break; + + /* Handle comments. No escaping possible so this is easy. */ + char *cp = strchr (line, '#'); + if (cp != NULL) + *cp = '\0'; + + cp = line; + while (isspace (*cp)) + ++cp; + + char *cmd = cp; + while (*cp != '\0' && !isspace (*cp)) + ++cp; + size_t cmdlen = cp - cmd; + + if (*cp != '\0') + *cp++ = '\0'; + while (isspace (*cp)) + ++cp; + + char *val1 = cp; + while (*cp != '\0' && !isspace (*cp)) + ++cp; + size_t val1len = cp - cmd; + + /* We always need at least two values. */ + if (val1len == 0) + continue; + + if (*cp != '\0') + *cp++ = '\0'; + while (isspace (*cp)) + ++cp; + + char *val2 = cp; + while (*cp != '\0' && !isspace (*cp)) + ++cp; + + /* Ignore the rest of the line. */ + *cp = '\0'; + + switch (cmdlen) { - ssize_t n = __getline (&line, &linelen, fp); - if (n <= 0) - break; - - /* Handle comments. No escaping possible so this is easy. */ - char *cp = strchr (line, '#'); - if (cp != NULL) - *cp = '\0'; - - cp = line; - while (isspace (*cp)) - ++cp; - - char *cmd = cp; - while (*cp != '\0' && !isspace (*cp)) - ++cp; - size_t cmdlen = cp - cmd; - - if (*cp != '\0') - *cp++ = '\0'; - while (isspace (*cp)) - ++cp; - - char *val1 = cp; - while (*cp != '\0' && !isspace (*cp)) - ++cp; - size_t val1len = cp - cmd; - - /* We always need at least two values. */ - if (val1len == 0) - continue; - - if (*cp != '\0') - *cp++ = '\0'; - while (isspace (*cp)) - ++cp; - - char *val2 = cp; - while (*cp != '\0' && !isspace (*cp)) - ++cp; - - /* Ignore the rest of the line. */ - *cp = '\0'; - - switch (cmdlen) + case 5: + if (strcmp (cmd, "label") == 0) { - case 5: - if (strcmp (cmd, "label") == 0) + if (!add_prefixlist (&labellist, &nlabellist, + &labellist_nullbits, val1, val2, &cp)) { - if (!add_prefixlist (&labellist, &nlabellist, - &labellist_nullbits, val1, val2, &cp)) - { - free (line); - fclose (fp); - goto no_file; - } + free (line); + fclose (fp); + goto no_file; } - break; + } + break; - case 6: - if (strcmp (cmd, "reload") == 0) - { - gaiconf_reload_flag = strcmp (val1, "yes") == 0; - if (gaiconf_reload_flag) - gaiconf_reload_flag_ever_set = 1; - } - break; + case 6: + if (strcmp (cmd, "reload") == 0) + { + gaiconf_reload_flag = strcmp (val1, "yes") == 0; + if (gaiconf_reload_flag) + gaiconf_reload_flag_ever_set = 1; + } + break; - case 7: - if (strcmp (cmd, "scopev4") == 0) + case 7: + if (strcmp (cmd, "scopev4") == 0) + { + struct in6_addr prefix; + unsigned long int bits; + unsigned long int val; + char *endp; + + bits = 32; + __set_errno (0); + cp = strchr (val1, '/'); + if (cp != NULL) + *cp++ = '\0'; + if (inet_pton (AF_INET6, val1, &prefix)) { - struct in6_addr prefix; - unsigned long int bits; - unsigned long int val; - char *endp; - - bits = 32; - __set_errno (0); - cp = strchr (val1, '/'); - if (cp != NULL) - *cp++ = '\0'; - if (inet_pton (AF_INET6, val1, &prefix)) - { - bits = 128; - if (IN6_IS_ADDR_V4MAPPED (&prefix) - && (cp == NULL - || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && bits >= 96 - && bits <= 128 - && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && val <= INT_MAX) - { - if (!add_scopelist (&scopelist, &nscopelist, - &scopelist_nullbits, &prefix, - bits, val)) - { - free (line); - fclose (fp); - goto no_file; - } - } - } - else if (inet_pton (AF_INET, val1, &prefix.s6_addr32[3]) - && (cp == NULL - || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && bits <= 32 - && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && val <= INT_MAX) + bits = 128; + if (IN6_IS_ADDR_V4MAPPED (&prefix) + && (cp == NULL + || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && bits >= 96 + && bits <= 128 + && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && val <= INT_MAX) { if (!add_scopelist (&scopelist, &nscopelist, &scopelist_nullbits, &prefix, - bits + 96, val)) + bits, val)) { free (line); fclose (fp); @@ -1979,173 +1959,191 @@ gaiconf_init (void) } } } - break; - - case 10: - if (strcmp (cmd, "precedence") == 0) + else if (inet_pton (AF_INET, val1, &prefix.s6_addr32[3]) + && (cp == NULL + || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && bits <= 32 + && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && val <= INT_MAX) { - if (!add_prefixlist (&precedencelist, &nprecedencelist, - &precedencelist_nullbits, val1, val2, - &cp)) + if (!add_scopelist (&scopelist, &nscopelist, + &scopelist_nullbits, &prefix, + bits + 96, val)) { free (line); fclose (fp); goto no_file; } } - break; - } - } - - free (line); - - fclose (fp); - - /* Create the array for the labels. */ - struct prefixentry *new_labels; - if (nlabellist > 0) - { - if (!labellist_nullbits) - ++nlabellist; - new_labels = malloc (nlabellist * sizeof (*new_labels)); - if (new_labels == NULL) - goto no_file; - - int i = nlabellist; - if (!labellist_nullbits) - { - --i; - memset (&new_labels[i].prefix, '\0', sizeof (struct in6_addr)); - new_labels[i].bits = 0; - new_labels[i].val = 1; } + break; - struct prefixlist *l = labellist; - while (i-- > 0) + case 10: + if (strcmp (cmd, "precedence") == 0) { - new_labels[i] = l->entry; - l = l->next; + if (!add_prefixlist (&precedencelist, &nprecedencelist, + &precedencelist_nullbits, val1, val2, + &cp)) + { + free (line); + fclose (fp); + goto no_file; + } } - free_prefixlist (labellist); - labellist = NULL; - - /* Sort the entries so that the most specific ones are at - the beginning. */ - qsort (new_labels, nlabellist, sizeof (*new_labels), prefixcmp); + break; } - else - new_labels = (struct prefixentry *) default_labels; - - struct prefixentry *new_precedence; - if (nprecedencelist > 0) - { - if (!precedencelist_nullbits) - ++nprecedencelist; - new_precedence = malloc (nprecedencelist * sizeof (*new_precedence)); - if (new_precedence == NULL) - { - if (new_labels != default_labels) - free (new_labels); - goto no_file; - } + } - int i = nprecedencelist; - if (!precedencelist_nullbits) - { - --i; - memset (&new_precedence[i].prefix, '\0', - sizeof (struct in6_addr)); - new_precedence[i].bits = 0; - new_precedence[i].val = 40; - } + free (line); - struct prefixlist *l = precedencelist; - while (i-- > 0) - { - new_precedence[i] = l->entry; - l = l->next; - } - free_prefixlist (precedencelist); - precedencelist = NULL; + fclose (fp); - /* Sort the entries so that the most specific ones are at - the beginning. */ - qsort (new_precedence, nprecedencelist, sizeof (*new_precedence), - prefixcmp); + /* Create the array for the labels. */ + struct prefixentry *new_labels; + if (nlabellist > 0) + { + if (!labellist_nullbits) + ++nlabellist; + new_labels = malloc (nlabellist * sizeof (*new_labels)); + if (new_labels == NULL) + goto no_file; + + int i = nlabellist; + if (!labellist_nullbits) + { + --i; + memset (&new_labels[i].prefix, '\0', sizeof (struct in6_addr)); + new_labels[i].bits = 0; + new_labels[i].val = 1; } - else - new_precedence = (struct prefixentry *) default_precedence; - struct scopeentry *new_scopes; - if (nscopelist > 0) + struct prefixlist *l = labellist; + while (i-- > 0) { - if (!scopelist_nullbits) - ++nscopelist; - new_scopes = malloc (nscopelist * sizeof (*new_scopes)); - if (new_scopes == NULL) - { - if (new_labels != default_labels) - free (new_labels); - if (new_precedence != default_precedence) - free (new_precedence); - goto no_file; - } - - int i = nscopelist; - if (!scopelist_nullbits) - { - --i; - new_scopes[i].addr32 = 0; - new_scopes[i].netmask = 0; - new_scopes[i].scope = 14; - } + new_labels[i] = l->entry; + l = l->next; + } + free_prefixlist (labellist); + labellist = NULL; - struct scopelist *l = scopelist; - while (i-- > 0) - { - new_scopes[i] = l->entry; - l = l->next; - } - free_scopelist (scopelist); + /* Sort the entries so that the most specific ones are at + the beginning. */ + qsort (new_labels, nlabellist, sizeof (*new_labels), prefixcmp); + } + else + new_labels = (struct prefixentry *) default_labels; - /* Sort the entries so that the most specific ones are at - the beginning. */ - qsort (new_scopes, nscopelist, sizeof (*new_scopes), - scopecmp); + struct prefixentry *new_precedence; + if (nprecedencelist > 0) + { + if (!precedencelist_nullbits) + ++nprecedencelist; + new_precedence = malloc (nprecedencelist * sizeof (*new_precedence)); + if (new_precedence == NULL) + { + if (new_labels != default_labels) + free (new_labels); + goto no_file; } - else - new_scopes = (struct scopeentry *) default_scopes; - - /* Now we are ready to replace the values. */ - const struct prefixentry *old = labels; - labels = new_labels; - if (old != default_labels) - free ((void *) old); - old = precedence; - precedence = new_precedence; - if (old != default_precedence) - free ((void *) old); + int i = nprecedencelist; + if (!precedencelist_nullbits) + { + --i; + memset (&new_precedence[i].prefix, '\0', + sizeof (struct in6_addr)); + new_precedence[i].bits = 0; + new_precedence[i].val = 40; + } - const struct scopeentry *oldscope = scopes; - scopes = new_scopes; - if (oldscope != default_scopes) - free ((void *) oldscope); + struct prefixlist *l = precedencelist; + while (i-- > 0) + { + new_precedence[i] = l->entry; + l = l->next; + } + free_prefixlist (precedencelist); + precedencelist = NULL; - save_gaiconf_mtime (&st); + /* Sort the entries so that the most specific ones are at + the beginning. */ + qsort (new_precedence, nprecedencelist, sizeof (*new_precedence), + prefixcmp); } else + new_precedence = (struct prefixentry *) default_precedence; + + struct scopeentry *new_scopes; + if (nscopelist > 0) { - no_file: - free_prefixlist (labellist); - free_prefixlist (precedencelist); + if (!scopelist_nullbits) + ++nscopelist; + new_scopes = malloc (nscopelist * sizeof (*new_scopes)); + if (new_scopes == NULL) + { + if (new_labels != default_labels) + free (new_labels); + if (new_precedence != default_precedence) + free (new_precedence); + goto no_file; + } + + int i = nscopelist; + if (!scopelist_nullbits) + { + --i; + new_scopes[i].addr32 = 0; + new_scopes[i].netmask = 0; + new_scopes[i].scope = 14; + } + + struct scopelist *l = scopelist; + while (i-- > 0) + { + new_scopes[i] = l->entry; + l = l->next; + } free_scopelist (scopelist); - /* If we previously read the file but it is gone now, free the - old data and use the builtin one. Leave the reload flag - alone. */ - fini (); + /* Sort the entries so that the most specific ones are at + the beginning. */ + qsort (new_scopes, nscopelist, sizeof (*new_scopes), + scopecmp); } + else + new_scopes = (struct scopeentry *) default_scopes; + + /* Now we are ready to replace the values. */ + const struct prefixentry *old = labels; + labels = new_labels; + if (old != default_labels) + free ((void *) old); + + old = precedence; + precedence = new_precedence; + if (old != default_precedence) + free ((void *) old); + + const struct scopeentry *oldscope = scopes; + scopes = new_scopes; + if (oldscope != default_scopes) + free ((void *) oldscope); + + save_gaiconf_mtime (&st); + return; + +no_file: + free_prefixlist (labellist); + free_prefixlist (precedencelist); + free_scopelist (scopelist); + + /* If we previously read the file but it is gone now, free the old data and + use the builtin one. Leave the reload flag alone. */ + fini (); } From patchwork Tue Aug 3 21:29:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 44562 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E5A5B3853C2B for ; Tue, 3 Aug 2021 21:31:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E5A5B3853C2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628026296; bh=Fp5xza+7b9PBb8/BUpAwLr7GSGsd15RVgfHEeyCSekk=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=mAOJ83UObSpP2/ApHT4PzJEml39saQn152F1BPZZVkSL85/ZdjgDf6ZOoUO2C0kgt 0ZQNoLEaaDlEN07+WsSjyoc2p22I7VILEnG2DPF1ldL7o3nbzuK03aoubg6tIlRHSE HlsXMiMuzTImnYtDonoWvqtfbeeZdbU5xW35sl5g= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from bird.elm.relay.mailchannels.net (bird.elm.relay.mailchannels.net [23.83.212.17]) by sourceware.org (Postfix) with ESMTPS id 97A70385DC29 for ; Tue, 3 Aug 2021 21:29:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 97A70385DC29 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 633D41E2364 for ; Tue, 3 Aug 2021 21:29:36 +0000 (UTC) Received: from pdx1-sub0-mail-a4.g.dreamhost.com (100-105-161-178.trex.outbound.svc.cluster.local [100.105.161.178]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id F2FF41E2739 for ; Tue, 3 Aug 2021 21:29:35 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a4.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.105.161.178 (trex/6.3.3); Tue, 03 Aug 2021 21:29:36 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Bitter-Reign: 01ff9c752e4d7017_1628026176258_4078069738 X-MC-Loop-Signature: 1628026176258:2895288300 X-MC-Ingress-Time: 1628026176258 Received: from pdx1-sub0-mail-a4.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTP id C05608AAAA for ; Tue, 3 Aug 2021 14:29:35 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTPSA id 9D8C98AAA8 for ; Tue, 3 Aug 2021 14:29:33 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a4 To: libc-alpha@sourceware.org Subject: [PATCH 3/5] getaddrinfo: Refactor code for readability Date: Wed, 4 Aug 2021 02:59:17 +0530 Message-Id: <20210803212919.3059194-4-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210803212919.3059194-1-siddhesh@sourceware.org> References: <20210803212919.3059194-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP 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-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The close_retry goto jump is confusing and clumsy to read, so refactor the code a bit to make it easier to follow. --- sysdeps/posix/getaddrinfo.c | 46 +++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index 180048eec2..fd05d53c1a 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -2156,6 +2156,37 @@ gaiconf_reload (void) gaiconf_init (); } +static bool +try_connect (int *fdp, int *afp, struct sockaddr_in6 *source_addrp, + const struct sockaddr *addr, socklen_t addrlen, int family) +{ + int fd = *fdp; + int af = *afp; + socklen_t sl = sizeof (*source_addrp); + bool retry = false; + + do + { + if (fd != -1 && __connect (fd, addr, addrlen) == 0 + && __getsockname (fd, (struct sockaddr *) source_addrp, &sl) == 0) + return true; + else if (errno == EAFNOSUPPORT && af == AF_INET6 && family == AF_INET) + { + /* This could mean IPv6 sockets are IPv6-only. */ + if (fd != -1) + __close_nocancel_nostatus (fd); + *afp = af = AF_INET; + *fdp = fd = __socket (AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, + IPPROTO_IP); + retry = true; + } + else + return false; + } + while (retry); + + __builtin_unreachable (); +} int getaddrinfo (const char *name, const char *service, @@ -2346,7 +2377,6 @@ getaddrinfo (const char *name, const char *service, if (fd == -1 || (af == AF_INET && q->ai_family == AF_INET6)) { if (fd != -1) - close_retry: __close_nocancel_nostatus (fd); af = q->ai_family; fd = __socket (af, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_IP); @@ -2358,14 +2388,10 @@ getaddrinfo (const char *name, const char *service, __connect (fd, &sa, sizeof (sa)); } - socklen_t sl = sizeof (results[i].source_addr); - if (fd != -1 - && __connect (fd, q->ai_addr, q->ai_addrlen) == 0 - && __getsockname (fd, - (struct sockaddr *) &results[i].source_addr, - &sl) == 0) + if (try_connect (&fd, &af, &results[i].source_addr, q->ai_addr, + q->ai_addrlen, q->ai_family)) { - results[i].source_addr_len = sl; + results[i].source_addr_len = sizeof (results[i].source_addr); results[i].got_source_addr = true; if (in6ai != NULL) @@ -2430,10 +2456,6 @@ getaddrinfo (const char *name, const char *service, results[i].source_addr_len = sizeof (struct sockaddr_in); } } - else if (errno == EAFNOSUPPORT && af == AF_INET6 - && q->ai_family == AF_INET) - /* This could mean IPv6 sockets are IPv6-only. */ - goto close_retry; else /* Just make sure that if we have to process the same address again we do not copy any memory. */ From patchwork Tue Aug 3 21:29:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 44564 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7CD44396DC1A for ; Tue, 3 Aug 2021 21:33:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7CD44396DC1A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628026387; bh=ARxWvGSbzcY+vHvbh4nFY8RNmOP51dURe+/yDmcvzmw=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=aJljPVTHxxp1KbDqUqSdcSHJJKbPjic0ebqmfGsGvZqM/uveBEGgc5og4MBny77tF SwX3dx5GCueipMvBRALBvmfG/ujsrUWJc9kKtxgTj3OjxQYJYO5fq77jmFL5UFQ4HH BlmaYoXcPeMB9isOQWAdXj+R/FQgItbttOld9gk4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from antelope.elm.relay.mailchannels.net (antelope.elm.relay.mailchannels.net [23.83.212.4]) by sourceware.org (Postfix) with ESMTPS id 3856E3851C17 for ; Tue, 3 Aug 2021 21:29:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3856E3851C17 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 09C017E2CF0 for ; Tue, 3 Aug 2021 21:29:39 +0000 (UTC) Received: from pdx1-sub0-mail-a4.g.dreamhost.com (100-105-161-178.trex-nlb.outbound.svc.cluster.local [100.105.161.178]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id A353E7E2A75 for ; Tue, 3 Aug 2021 21:29:38 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a4.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.105.161.178 (trex/6.3.3); Tue, 03 Aug 2021 21:29:38 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Cooing-Thread: 3b06033e6c215e37_1628026178889_4131000370 X-MC-Loop-Signature: 1628026178889:3391034243 X-MC-Ingress-Time: 1628026178889 Received: from pdx1-sub0-mail-a4.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTP id 6B58F8AAAA for ; Tue, 3 Aug 2021 14:29:38 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTPSA id 3FCB88AAA8 for ; Tue, 3 Aug 2021 14:29:36 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a4 To: libc-alpha@sourceware.org Subject: [PATCH 4/5] gaih_inet: Consolidate got_port code Date: Wed, 4 Aug 2021 02:59:18 +0530 Message-Id: <20210803212919.3059194-5-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210803212919.3059194-1-siddhesh@sourceware.org> References: <20210803212919.3059194-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP 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-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Refactor the code to remove the unnecessary got_port goto across conditional branches. --- sysdeps/posix/getaddrinfo.c | 105 +++++++++++++++++------------------- 1 file changed, 48 insertions(+), 57 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index fd05d53c1a..220cd41cde 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -359,65 +359,12 @@ gaih_inet (const char *name, const struct gaih_service *service, } } - int port = 0; - if (service != NULL) - { - if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0) - return -EAI_SERVICE; - - if (service->num < 0) - { - if (tp->name[0]) - { - st = (struct gaih_servtuple *) - alloca_account (sizeof (struct gaih_servtuple), alloca_used); - - int rc = gaih_inet_serv (service->name, tp, req, st, tmpbuf); - if (__glibc_unlikely (rc != 0)) - return rc; - } - else - { - struct gaih_servtuple **pst = &st; - for (tp++; tp->name[0]; tp++) - { - struct gaih_servtuple *newp; - - if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0) - continue; + if (service != NULL && (tp->protoflag & GAI_PROTO_NOSERVICE) != 0) + return -EAI_SERVICE; - if (req->ai_socktype != 0 - && req->ai_socktype != tp->socktype) - continue; - if (req->ai_protocol != 0 - && !(tp->protoflag & GAI_PROTO_PROTOANY) - && req->ai_protocol != tp->protocol) - continue; - - newp = (struct gaih_servtuple *) - alloca_account (sizeof (struct gaih_servtuple), - alloca_used); - - if (gaih_inet_serv (service->name, - tp, req, newp, tmpbuf) != 0) - continue; - - *pst = newp; - pst = &(newp->next); - } - if (st == (struct gaih_servtuple *) &nullserv) - return -EAI_SERVICE; - } - } - else - { - port = htons (service->num); - goto got_port; - } - } - else + if (service == NULL || service->num >= 0) { - got_port: + int port = service != NULL ? htons (service->num) : 0; if (req->ai_socktype || req->ai_protocol) { @@ -450,6 +397,50 @@ gaih_inet (const char *name, const struct gaih_service *service, } } } + else + { + if (tp->name[0]) + { + st = (struct gaih_servtuple *) + alloca_account (sizeof (struct gaih_servtuple), alloca_used); + + int rc = gaih_inet_serv (service->name, tp, req, st, tmpbuf); + if (__glibc_unlikely (rc != 0)) + return rc; + } + else + { + struct gaih_servtuple **pst = &st; + for (tp++; tp->name[0]; tp++) + { + struct gaih_servtuple *newp; + + if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0) + continue; + + if (req->ai_socktype != 0 + && req->ai_socktype != tp->socktype) + continue; + if (req->ai_protocol != 0 + && !(tp->protoflag & GAI_PROTO_PROTOANY) + && req->ai_protocol != tp->protocol) + continue; + + newp = (struct gaih_servtuple *) + alloca_account (sizeof (struct gaih_servtuple), + alloca_used); + + if (gaih_inet_serv (service->name, + tp, req, newp, tmpbuf) != 0) + continue; + + *pst = newp; + pst = &(newp->next); + } + if (st == (struct gaih_servtuple *) &nullserv) + return -EAI_SERVICE; + } + } bool malloc_name = false; struct gaih_addrtuple *addrmem = NULL; From patchwork Tue Aug 3 21:29:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 44565 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 025D8384F014 for ; Tue, 3 Aug 2021 21:33:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 025D8384F014 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628026438; bh=t64dqFSZhayjTngTeHwHEPUM3MS+UB0vv0e3ISTzRkI=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=FUOqUpIEeSTpwyrYvJXSMk0lzFzuFaBF7y0xEXEONSm63hqqRz8fcw+KYRiGS6fmJ sJFhmCSG6EnQryWZpPr5KOKSd7M7qriXye7BDui9VsDjlqIPaiu89IutsW17ZwlDHi ugSO+IC5in8EZov+qKmoldVA+qvkK9qo+5UZzZIA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from buffalo.birch.relay.mailchannels.net (buffalo.birch.relay.mailchannels.net [23.83.209.24]) by sourceware.org (Postfix) with ESMTPS id 0C8FE384F014 for ; Tue, 3 Aug 2021 21:29:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0C8FE384F014 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 D521248150B for ; Tue, 3 Aug 2021 21:29:40 +0000 (UTC) Received: from pdx1-sub0-mail-a4.g.dreamhost.com (100-96-13-120.trex.outbound.svc.cluster.local [100.96.13.120]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 82296482501 for ; Tue, 3 Aug 2021 21:29:40 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a4.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.13.120 (trex/6.3.3); Tue, 03 Aug 2021 21:29:40 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Power-Exultant: 533138f8203a1f67_1628026180771_1465703800 X-MC-Loop-Signature: 1628026180771:2189012621 X-MC-Ingress-Time: 1628026180770 Received: from pdx1-sub0-mail-a4.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTP id 44DFE8AAA8 for ; Tue, 3 Aug 2021 14:29:40 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTPSA id 3D7DD8AAAA for ; Tue, 3 Aug 2021 14:29:38 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a4 To: libc-alpha@sourceware.org Subject: [PATCH 5/5] gaih_inet: Make process_list label into a function Date: Wed, 4 Aug 2021 02:59:19 +0530 Message-Id: <20210803212919.3059194-6-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210803212919.3059194-1-siddhesh@sourceware.org> References: <20210803212919.3059194-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP 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-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" process_list is the final operation in gaih_inet where the result is written out. Factor it out into a separate function and call it at the end if there have been no errors. Rename the free_and_return label to done and use it to jump to the end of gaih_inet, where process_list is called if return is non-zero. This change makes the code more linear and slightly clearer to follow. This needs still more rework to make the function simpler to understand. --- sysdeps/posix/getaddrinfo.c | 311 +++++++++++++++++------------------- 1 file changed, 149 insertions(+), 162 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index 220cd41cde..3afac87fa2 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -256,7 +256,7 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, { \ __resolv_context_put (res_ctx); \ result = -EAI_MEMORY; \ - goto free_and_return; \ + goto done; \ } \ } \ if (status == NSS_STATUS_NOTFOUND \ @@ -266,7 +266,7 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, { \ __resolv_context_put (res_ctx); \ result = -EAI_SYSTEM; \ - goto free_and_return; \ + goto done; \ } \ if (h_errno == TRY_AGAIN) \ no_data = EAI_AGAIN; \ @@ -279,7 +279,7 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, { \ __resolv_context_put (res_ctx); \ result = -EAI_SYSTEM; \ - goto free_and_return; \ + goto done; \ } \ *pat = addrmem; \ \ @@ -290,7 +290,7 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, { \ __resolv_context_put (res_ctx); \ result = -EAI_SYSTEM; \ - goto free_and_return; \ + goto done; \ } \ canon = canonbuf; \ } \ @@ -323,6 +323,123 @@ getcanonname (nss_action_list nip, struct gaih_addrtuple *at, const char *name) return __strdup (name); } +static int +process_list (const struct addrinfo *req, struct gaih_addrtuple *at, + struct gaih_servtuple *st, const char *canon, bool canon_alloc, + bool got_ipv6, struct addrinfo **pai, unsigned int *naddrs) +{ + struct gaih_servtuple *st2; + struct gaih_addrtuple *at2 = at; + size_t socklen; + sa_family_t family; + + /* + buffer is the size of an unformatted IPv6 address in printable format. + */ + for (at2 = at; at2 != NULL; at2 = at2->next) + { + /* Only the first entry gets the canonical name. */ + if (at2 == at && (req->ai_flags & AI_CANONNAME) != 0) + { + bool do_idn = req->ai_flags & AI_CANONIDN; + if (do_idn) + { + char *out; + int rc = __idna_from_dns_encoding (canon, &out); + if (rc == 0) + canon = out; + else if (rc == EAI_IDN_ENCODE) + /* Use the punycode name as a fallback. */ + do_idn = false; + else + return -rc; + } + if (!do_idn) + { + if (!canon_alloc) + { + canon = __strdup (canon); + if (canon == NULL) + return -EAI_MEMORY; + } + } + } + + 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 (got_ipv6 + && (req->ai_flags & (AI_V4MAPPED|AI_ALL)) == AI_V4MAPPED + && IN6_IS_ADDR_V4MAPPED (at2->addr)) + continue; + } + else + socklen = sizeof (struct sockaddr_in); + + for (st2 = st; st2 != NULL; st2 = st2->next) + { + struct addrinfo *ai; + ai = *pai = malloc (sizeof (struct addrinfo) + socklen); + if (ai == NULL) + { + free ((char *) canon); + return -EAI_MEMORY; + } + + ai->ai_flags = req->ai_flags; + ai->ai_family = family; + ai->ai_socktype = st2->socktype; + ai->ai_protocol = st2->protocol; + ai->ai_addrlen = socklen; + ai->ai_addr = (void *) (ai + 1); + + /* We only add the canonical name once. */ + ai->ai_canonname = (char *) canon; + 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 = st2->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 = st2->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; + } + return 0; +} + static int gaih_inet (const char *name, const struct gaih_service *service, const struct addrinfo *req, struct addrinfo **pai, @@ -479,7 +596,7 @@ gaih_inet (const char *name, const struct gaih_service *service, else { result = -EAI_ADDRFAMILY; - goto free_and_return; + goto done; } if (req->ai_flags & AI_CANONNAME) @@ -507,7 +624,7 @@ gaih_inet (const char *name, const struct gaih_service *service, else { result = -EAI_ADDRFAMILY; - goto free_and_return; + goto done; } if (scope_delim != NULL @@ -516,7 +633,7 @@ gaih_inet (const char *name, const struct gaih_service *service, &at->scopeid) != 0) { result = -EAI_NONAME; - goto free_and_return; + goto done; } if (req->ai_flags & AI_CANONNAME) @@ -555,7 +672,7 @@ gaih_inet (const char *name, const struct gaih_service *service, if (!scratch_buffer_grow (tmpbuf)) { result = -EAI_MEMORY; - goto free_and_return; + goto done; } } @@ -568,7 +685,7 @@ gaih_inet (const char *name, const struct gaih_service *service, (req, AF_INET, h, &addrmem)) { result = -EAI_MEMORY; - goto free_and_return; + goto done; } *pat = addrmem; } @@ -578,7 +695,7 @@ gaih_inet (const char *name, const struct gaih_service *service, result = -EAI_NODATA; else result = -EAI_NONAME; - goto free_and_return; + goto done; } } else @@ -592,10 +709,13 @@ gaih_inet (const char *name, const struct gaih_service *service, The name is known, though. */ result = -EAI_NODATA; - goto free_and_return; + goto done; } - goto process_list; + if (at->family == AF_UNSPEC) + result = -EAI_NONAME; + + goto done; } #ifdef USE_NSCD @@ -619,7 +739,7 @@ gaih_inet (const char *name, const struct gaih_service *service, if (addrmem == NULL) { result = -EAI_MEMORY; - goto free_and_return; + goto done; } struct gaih_addrtuple *addrfree = addrmem; @@ -654,7 +774,7 @@ gaih_inet (const char *name, const struct gaih_service *service, if (canonbuf == NULL) { result = -EAI_MEMORY; - goto free_and_return; + goto done; } canon = (*pat)->name = canonbuf; } @@ -687,16 +807,13 @@ gaih_inet (const char *name, const struct gaih_service *service, free (air); if (at->family == AF_UNSPEC) - { - result = -EAI_NONAME; - goto free_and_return; - } + result = -EAI_NONAME; - goto process_list; + goto done; } else if (err == 0) /* The database contains a negative entry. */ - goto free_and_return; + goto done; else if (__nss_not_use_nscd_hosts == 0) { if (h_errno == NETDB_INTERNAL && errno == ENOMEM) @@ -706,7 +823,7 @@ gaih_inet (const char *name, const struct gaih_service *service, else result = -EAI_SYSTEM; - goto free_and_return; + goto done; } } #endif @@ -755,7 +872,7 @@ gaih_inet (const char *name, const struct gaih_service *service, { __resolv_context_put (res_ctx); result = -EAI_MEMORY; - goto free_and_return; + goto done; } } @@ -854,7 +971,7 @@ gaih_inet (const char *name, const struct gaih_service *service, { __resolv_context_put (res_ctx); result = -EAI_MEMORY; - goto free_and_return; + goto done; } canon = canonbuf; } @@ -903,7 +1020,7 @@ gaih_inet (const char *name, const struct gaih_service *service, && h_errno == NETDB_INTERNAL) { result = -EAI_SYSTEM; - goto free_and_return; + goto done; } if (no_data != 0 && no_inet6_data != 0) @@ -916,16 +1033,12 @@ gaih_inet (const char *name, const struct gaih_service *service, is known, though. */ result = -EAI_NODATA; - goto free_and_return; + goto done; } } - process_list: if (at->family == AF_UNSPEC) - { - result = -EAI_NONAME; - goto free_and_return; - } + result = -EAI_NONAME; } else { @@ -955,142 +1068,16 @@ gaih_inet (const char *name, const struct gaih_service *service, } } - { - struct gaih_servtuple *st2; - struct gaih_addrtuple *at2 = at; - size_t socklen; - sa_family_t family; - - /* - buffer is the size of an unformatted IPv6 address in printable format. - */ - while (at2 != NULL) - { - /* Only the first entry gets the canonical name. */ - if (at2 == at && (req->ai_flags & AI_CANONNAME) != 0) - { - if (canon == NULL) - /* If the canonical name cannot be determined, use - the passed in string. */ - canon = orig_name; - - bool do_idn = req->ai_flags & AI_CANONIDN; - if (do_idn) - { - char *out; - int rc = __idna_from_dns_encoding (canon, &out); - if (rc == 0) - canon = out; - else if (rc == EAI_IDN_ENCODE) - /* Use the punycode name as a fallback. */ - do_idn = false; - else - { - result = -rc; - goto free_and_return; - } - } - if (!do_idn) - { - if (canonbuf != NULL) - /* We already allocated the string using malloc, but - the buffer is now owned by canon. */ - canonbuf = NULL; - else - { - canon = __strdup (canon); - if (canon == NULL) - { - result = -EAI_MEMORY; - goto free_and_return; - } - } - } - } - - 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 (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 (st2 = st; st2 != NULL; st2 = st2->next) - { - struct addrinfo *ai; - ai = *pai = malloc (sizeof (struct addrinfo) + socklen); - if (ai == NULL) - { - free ((char *) canon); - result = -EAI_MEMORY; - goto free_and_return; - } - - ai->ai_flags = req->ai_flags; - ai->ai_family = family; - ai->ai_socktype = st2->socktype; - ai->ai_protocol = st2->protocol; - ai->ai_addrlen = socklen; - ai->ai_addr = (void *) (ai + 1); - - /* We only add the canonical name once. */ - ai->ai_canonname = (char *) canon; - 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 = st2->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 = st2->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; - } - } +done: + if (result == 0) + result = process_list (req, at, st, canon ?: orig_name, canonbuf != NULL, + got_ipv6, pai, naddrs); - free_and_return: if (malloc_name) free ((char *) name); free (addrmem); - free (canonbuf); + if (result != 0) + free (canonbuf); return result; }