From patchwork Mon Mar 14 17:30:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1605215 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=CbQch+Ze; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KHP1s4WSFz9sCD for ; Tue, 15 Mar 2022 04:40:01 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E31D53858433 for ; Mon, 14 Mar 2022 17:39:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E31D53858433 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647279598; bh=/EDnUm4b3Bukyk1pnFzRLariZr2GSmNS/5mwpm/Kua0=; 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=CbQch+ZeAWd4lpuFdgx/DqZ4sIw7dgDCIOFIC9c64OwK6M4s2FIeJwWl0Go8xA0RK HP/6er4Clz8VBXeDEoMHi37BeVVPRQ7o7p0zniATg9d3o37lRJmldjiao+JQuJm0xN o0qTWD1MgAhGRuSIib+2LQGh3bIcN2t+K3y0LKAg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from anteater.oak.relay.mailchannels.net (anteater.oak.relay.mailchannels.net [23.83.215.3]) by sourceware.org (Postfix) with ESMTPS id A7F743858D20 for ; Mon, 14 Mar 2022 17:39:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A7F743858D20 Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 1D128281800 for ; Mon, 14 Mar 2022 17:31:46 +0000 (UTC) Received: from pdx1-sub0-mail-a307.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id CFB14281C2B for ; Mon, 14 Mar 2022 17:31:11 +0000 (UTC) Received: from pdx1-sub0-mail-a307.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.102.18.7 (trex/6.5.3); Mon, 14 Mar 2022 17:31:45 +0000 Received: from rhbox.redhat.com (unknown [1.186.122.132]) (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-a307.dreamhost.com (Postfix) with ESMTPSA id 4KHNqM1ZWhz1PT for ; Mon, 14 Mar 2022 10:30:54 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 1/3] gaiconf_init: Refactor some bits for readability Date: Mon, 14 Mar 2022 23:00:37 +0530 Message-Id: <20220314173039.1060650-2-siddhesh@sourceware.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314173039.1060650-1-siddhesh@sourceware.org> References: <20220314173039.1060650-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.2 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_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL, 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-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Split out line processing for `label`, `precedence` and `scopev4` into separate functions instead of the gotos. Signed-off-by: Siddhesh Poyarekar --- 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 18dccd5924..95a61d7238 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 Mon Mar 14 17:30:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1605208 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=fIlYq+fF; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (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) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KHNrS3Cqbz9s0m for ; Tue, 15 Mar 2022 04:31:52 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D148D385840E for ; Mon, 14 Mar 2022 17:31:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D148D385840E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647279109; bh=nXDSugfAAcN3FN5VlWCZM+aT0piyklbYwo0z9b6Ne/Y=; 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=fIlYq+fFWHAeIOsR/G0eRG61jNlON7ixaUPQDkBnEEZr2l1TemSgpb5ysYaqwntOQ 3ItjXM+6AGeOGWOsoYZTujbQPXpe48dPMZQQJLkLqfodv9/ObwSacUdenXQDPM8Dh+ 9aqUHRnoiK2rrwVH/YLOsfZrLqL9ucOt7atZCl9k= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from crane.ash.relay.mailchannels.net (crane.ash.relay.mailchannels.net [23.83.222.43]) by sourceware.org (Postfix) with ESMTPS id A5EE83858D20 for ; Mon, 14 Mar 2022 17:31:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A5EE83858D20 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 0FBDA622B3D for ; Mon, 14 Mar 2022 17:31:30 +0000 (UTC) Received: from pdx1-sub0-mail-a307.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 52CC062285E for ; Mon, 14 Mar 2022 17:30:59 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1647279089; a=rsa-sha256; cv=none; b=JIvOPrezbXM9YdFtvxgGxmzbOHlzJjCJCT/MkB0yjFR2ZO71Fypv2duWRUfLz8QgQj9rST xbYWeOCUZtqkcbSQaGVZKXFz3UOhS4WeQk/jlJXLKDRd1OJiRAgVgo7LLzciIsrpaqtO9w HIIOq7bxYuNev9dfJjHSdwhje5ZhVl83eB2nzFLE+x3ImJ5ONTsP41PNYf3ypgpQV5tpyi N5ixVloMaLNaL0JbINOi/DFDux4Ajk3HQU67w+LScK18ocnig737pD8wOzwrtqscDWBDys Q0+tY0Q24Td7+8r6JwGUnl3bEX3j/gFM25BMRDlxrlVY+d0EfnewWaj4uD92ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1647279089; 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=nXDSugfAAcN3FN5VlWCZM+aT0piyklbYwo0z9b6Ne/Y=; b=2Kus86AfwIUvuCd9gHwT2KVmOhIW/d5L/qY9X5fx2cmtk/1lLrTpTwTttb6m6/m6a2sd/k J3cZ/BE226+QEA8EBtT8TPjGGiGmf+Ryyt13Sgz4tTTKYaFWhpbDzi0/6P2J1z+31cFl1N EQ4O4tRhB8C8VUF4VWP41vnPDTDKGK1h39ajf0xwMT6dvWtnvfGJdI78JaajKfXnMEUhab z+0QRSWgJnt2mrRgLSvcBxigrFxN72CqgFdd80Eb466gTp4Hw9ljUxhhNd5i7Q6LKTk6Qn esWrSNJjox1Oh+5p7ep+C3PXQwwSUeSfTc2eQWxB8pu/Dxvo/OYAbteozx9HmA== ARC-Authentication-Results: i=1; rspamd-c9cb649d9-fqgbb; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a307.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.112.20.197 (trex/6.5.3); Mon, 14 Mar 2022 17:31:29 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Whimsical-Befitting: 47df6300761e0000_1647279089602_1287946829 X-MC-Loop-Signature: 1647279089602:3145602291 X-MC-Ingress-Time: 1647279089602 Received: from rhbox.redhat.com (unknown [1.186.122.132]) (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-a307.dreamhost.com (Postfix) with ESMTPSA id 4KHNqP2wT9z1Pd for ; Mon, 14 Mar 2022 10:30:56 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 2/3] gai_init: Avoid jumping from if condition to its else counterpart Date: Mon, 14 Mar 2022 23:00:38 +0530 Message-Id: <20220314173039.1060650-3-siddhesh@sourceware.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314173039.1060650-1-siddhesh@sourceware.org> References: <20220314173039.1060650-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.2 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_H5, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL, 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-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.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. Signed-off-by: Siddhesh Poyarekar --- 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 95a61d7238..4e947024ec 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 Mon Mar 14 17:30:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1605210 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=BAACrP5F; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (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) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KHNt42KJNz9s0m for ; Tue, 15 Mar 2022 04:33:16 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 700033857C4F for ; Mon, 14 Mar 2022 17:33:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 700033857C4F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647279194; bh=2BRYpWEgI3ZPcQdjnYFr4QIH/Hk4H487akYSTtAuZwA=; 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=BAACrP5FYnF7TVmbnA9VAERtk/FeF5WXrme3mfZwvwXaCdRsKgzVXqgqIPsCGNJbL wD8v4qYd2cMrr8MjWo33BhLewzILToy7NQHpbzjmnMZmqCL7NYUMuUvAu6dJgnVooM s+DvjBmOb4Sn4HUDUychnhzKq4JvGm19StHpXN4I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from quail.birch.relay.mailchannels.net (quail.birch.relay.mailchannels.net [23.83.209.151]) by sourceware.org (Postfix) with ESMTPS id 09FD23858D20 for ; Mon, 14 Mar 2022 17:31:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 09FD23858D20 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 661516226DE for ; Mon, 14 Mar 2022 17:31:31 +0000 (UTC) Received: from pdx1-sub0-mail-a307.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 08A3D62278A for ; Mon, 14 Mar 2022 17:31:15 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1647279091; a=rsa-sha256; cv=none; b=CLEo02kRRaJorp11i7CuExKUAqYZ9Lj3tSyspALmmcGWwoL3F/sF3bgGSJhGQaNoPEAPEj pCGSYrdS2IStkPw8ZWLbdYIpKfs2mQfqmdshKruNmjAltQSMrZ03UCNQ8wZvHcsgboFYhx vCifUoRjN59Knogz92+c8rcd02JTX7t2I0Ie6wWSqqGZ/BrCay7ZyduidOoCYiNvNdFaO/ CAuoUhqa/xtLKIh85W31ssPhr8SqNnjxygTqjdi0G7tlSugV88OLuL+HZqbtqsMBlOEsjW wSjUQoe/8rWiMFvfdK0OAWDTi5+LTy3NcUZ0tZ9jEyGPVcVQz2WF0Txg2MUSUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1647279091; 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=2BRYpWEgI3ZPcQdjnYFr4QIH/Hk4H487akYSTtAuZwA=; b=W+1PvfHwiiZzucAESbaGwdz8auIkHxygOCV1zUlm/QC5aMAl1BCPtnJ1dXU/S8XcSiedmu VTcVN0jURBl2uX9kKW4ECA7BLwFEwV8UEFsKUb8wPlKNuRZ6+LFKITbvDhNaxMpOgHahQ1 c5TVSCKHTPpcKfBkcMehycE50x0tPNCAgL6eSsGMYJVlNkJUMltV9uWUGGcgVcdxQJ/bLi Jo6RttUDYqAFXIXrmaqyJZoNECtm3YL8t9xnlP+mxLgk/jsb/SHwNq7NcWzAoT5IsT8sTb cyxQ40AdrL5pGt4jA/8RUAHbRME+CSzkS/xAuD7syVMa9YraTyXcncKdNnItyg== ARC-Authentication-Results: i=1; rspamd-c9cb649d9-h8hch; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a307.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.112.144.9 (trex/6.5.3); Mon, 14 Mar 2022 17:31:31 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Snatch-Spicy: 2e9cd5802f82b0fb_1647279091286_2597702305 X-MC-Loop-Signature: 1647279091286:457273194 X-MC-Ingress-Time: 1647279091286 Received: from rhbox.redhat.com (unknown [1.186.122.132]) (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-a307.dreamhost.com (Postfix) with ESMTPSA id 4KHNqR5Jgsz1Pl for ; Mon, 14 Mar 2022 10:30:59 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 3/3] getaddrinfo: Refactor code for readability Date: Mon, 14 Mar 2022 23:00:39 +0530 Message-Id: <20220314173039.1060650-4-siddhesh@sourceware.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314173039.1060650-1-siddhesh@sourceware.org> References: <20220314173039.1060650-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.2 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_H5, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL, 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-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.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. Signed-off-by: Siddhesh Poyarekar --- 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 4e947024ec..5556876108 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. */