From nobody Tue Oct 10 22:48:01 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4S4rdt0S64z4wRlM; Tue, 10 Oct 2023 22:48:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4S4rds74H1z4LxL; Tue, 10 Oct 2023 22:48:01 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696978082; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=kMDTIyLdr7x7X3z0MNFOQP4fYqg8qpEcUmNXdN4Ng2A=; b=p17ol6MLFMAz1lFhTsC4iFNSn3hZUhok+HZEg8t4tTkqbIPtIjzdAciOj/mzb/PBMLh+Ji 7M+Zg85JERpZxIDHiUpnik1s4mWsLfE1pyU6HvxYZGAZ7q7ILPEm+E/eWwGhuWG5yNDcbP 0ipuG8D8IUEKcRYFeaoLEPRrcOXokDf6Hx7n9B4dpDcdMCkcR/x4BCL+iMxDShZnyW7o74 wUyjHCG62GkVefl3vuHLGJHN8KrX13bJ/iEGC/bmotjohzU9ZFlTfAoGiUPMNha4lC1hOz 2UVyqMkfnNQ1MQznGYvLuIa9AXZiq1EgRDDJ3r2BMVEEGWyTZK+fCoTKC6hcXg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1696978082; a=rsa-sha256; cv=none; b=CJdllMxEn4pAqISeLBR3BXc8skxQgG7PbYeXAmBdxJrLJLj+ugH/mVKl9MkSWdwBe0C8ft Y6nU/2SDSdqmpBKkZqUtS6yp4NNuxHaUs+SOQY07gczKy6Ts5+Hkc414XuczsWwHLe/GUe tOiwecGEfbMYY8B2HCqC9BhbxoeK8DSDGdkKFcd7djG9xYMJUp5hbLVV3/YpjO27A4But1 8FBqF6hPcttavtgOKyJWNVqEZ88gp9bq+xY/9LLG/gmXaor/JCaU981n+yRdgorCymwDrY HYxtLoJJ2V2pOPvRhv6niQkcv9yJJVWOwjHo28WaMn/y8LwzmFeLR0K37X4niw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696978082; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=kMDTIyLdr7x7X3z0MNFOQP4fYqg8qpEcUmNXdN4Ng2A=; b=tHq9wXjBpt0nGRPHnRr546j6omZucyCUPD0apCSVBXZcNUgzYBr/NYY0Uu0GVnPUP1Yb9X dQCZgU8tpqB+oE15lVHhUehiY2A5Wkgcqu33hFVFGg/bwHCXDgzXjnZfgxaekc8IF3O9oU 2T+MbHX5ozPHTviCfjpxFpov+v/mQvQ4l5vhAVwp2Gk51NsSGjOhBRigE7iQgIw6SfWYql E6IwyqTdzctqp2nz7kkGGaEJNn0RSmfnOwAo5+6fDffCmB+4bB3hXzxakQn3sH6qpigrSl e1AHxeSyxNZG9/86lcXn83x5wdMGtllaS8TgbxToW1aJotzCE4frTz8j1P7HXw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4S4rds685Nzx9T; Tue, 10 Oct 2023 22:48:01 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 39AMm1KB019061; Tue, 10 Oct 2023 22:48:01 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 39AMm1vN019058; Tue, 10 Oct 2023 22:48:01 GMT (envelope-from git) Date: Tue, 10 Oct 2023 22:48:01 GMT Message-Id: <202310102248.39AMm1vN019058@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 7fd2c91a291b - main - ping: Simplify protocol selection. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7fd2c91a291bd518e012b438d6ca6fdd04d39dbf Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=7fd2c91a291bd518e012b438d6ca6fdd04d39dbf commit 7fd2c91a291bd518e012b438d6ca6fdd04d39dbf Author: Dag-Erling Smørgrav AuthorDate: 2023-10-10 22:47:46 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-10-10 22:47:59 +0000 ping: Simplify protocol selection. * Interrupt the option loop as soon as we have an indication of which protocol is intended. * If we end up having to perform a DNS lookup, loop over the entire result looking for either IPv4 or IPv6 addresses. Sponsored by: NetApp, Inc. Sponsored by: Klara, Inc. Reviewed by: rscheff, kevans, allanjude Differential Revision: https://reviews.freebsd.org/D42137 --- sbin/ping/main.c | 125 +++++++++++++++++++------------------------ sbin/ping/tests/ping_test.sh | 26 ++++++--- 2 files changed, 76 insertions(+), 75 deletions(-) diff --git a/sbin/ping/main.c b/sbin/ping/main.c index 6321178e1228..e07b30952199 100644 --- a/sbin/ping/main.c +++ b/sbin/ping/main.c @@ -62,112 +62,99 @@ int main(int argc, char *argv[]) { -#if defined(INET) && defined(INET6) +#if defined(INET) struct in_addr a; - struct in6_addr a6; #endif -#if defined(INET) || defined(INET6) - struct addrinfo hints; +#if defined(INET6) + struct in6_addr a6; #endif - int ch; -#ifdef INET - bool ipv4 = false; +#if defined(INET) && defined(INET6) + struct addrinfo hints, *res, *ai; + int error; #endif -#ifdef INET6 - bool ipv6 = false; + int opt; +#ifdef INET6 if (strcmp(getprogname(), "ping6") == 0) - ipv6 = true; + return ping6(argc, argv); #endif - while ((ch = getopt(argc, argv, ":" OPTSTR)) != -1) { - switch(ch) { + while ((opt = getopt(argc, argv, ":" OPTSTR)) != -1) { + switch (opt) { #ifdef INET case '4': - ipv4 = true; - break; + goto ping4; #endif #ifdef INET6 case '6': - ipv6 = true; - break; + goto ping6; #endif -#if defined(INET) && defined(INET6) case 'S': /* * If -S is given with a numeric parameter, * force use of the corresponding version. */ +#ifdef INET if (inet_pton(AF_INET, optarg, &a) == 1) - ipv4 = true; - else if (inet_pton(AF_INET6, optarg, &a6) == 1) - ipv6 = true; - break; + goto ping4; +#endif +#ifdef INET6 + if (inet_pton(AF_INET6, optarg, &a6) == 1) + goto ping6; #endif + break; default: break; } } + /* + * For IPv4, only one positional argument, the target, is allowed. + * For IPv6, multiple positional argument are allowed; the last + * one is the target, and preceding ones are intermediate hops. + * This nuance is lost here, but the only case where it matters is + * an error. + */ if (optind >= argc) usage(); - optreset = 1; - optind = 1; #if defined(INET) && defined(INET6) - if (ipv4 && ipv6) - errx(1, "-4 and -6 cannot be used simultaneously"); -#endif - -#if defined(INET) && defined(INET6) - if (inet_pton(AF_INET, argv[argc - 1], &a) == 1) { - if (ipv6) - errx(1, "IPv6 requested but IPv4 target address " - "provided"); + memset(&hints, 0, sizeof(hints)); + hints.ai_socktype = SOCK_RAW; + if (feature_present("inet") && !feature_present("inet6")) hints.ai_family = AF_INET; - } - else if (inet_pton(AF_INET6, argv[argc - 1], &a6) == 1) { - if (ipv4) - errx(1, "IPv4 requested but IPv6 target address " - "provided"); - hints.ai_family = AF_INET6; - } else if (ipv6) + if (feature_present("inet6") && !feature_present("inet")) hints.ai_family = AF_INET6; - else if (ipv4) - hints.ai_family = AF_INET; - else { - if (!feature_present("inet6")) - hints.ai_family = AF_INET; - else if (!feature_present("inet")) - hints.ai_family = AF_INET6; - else { - struct addrinfo *res; - - memset(&hints, 0, sizeof(hints)); - hints.ai_socktype = SOCK_RAW; - hints.ai_family = AF_UNSPEC; - getaddrinfo(argv[argc - 1], NULL, &hints, &res); - if (res != NULL) { - hints.ai_family = res[0].ai_family; - freeaddrinfo(res); - } + else + hints.ai_family = AF_UNSPEC; + error = getaddrinfo(argv[argc - 1], NULL, &hints, &res); + if (res == NULL) + errx(1, "%s", gai_strerror(error)); + for (ai = res; ai != NULL; ai = ai->ai_next) { + if (ai->ai_family == AF_INET) { + freeaddrinfo(res); + goto ping4; + } + if (ai->ai_family == AF_INET6) { + freeaddrinfo(res); + goto ping6; } } -#elif defined(INET) - hints.ai_family = AF_INET; -#elif defined(INET6) - hints.ai_family = AF_INET6; + freeaddrinfo(res); + errx(1, "Unknown host"); #endif - #ifdef INET - if (hints.ai_family == AF_INET) - return ping(argc, argv); -#endif /* INET */ +ping4: + optreset = 1; + optind = 1; + return ping(argc, argv); +#endif #ifdef INET6 - if (hints.ai_family == AF_INET6) - return ping6(argc, argv); -#endif /* INET6 */ - errx(1, "Unknown host"); +ping6: + optreset = 1; + optind = 1; + return ping6(argc, argv); +#endif } void diff --git a/sbin/ping/tests/ping_test.sh b/sbin/ping/tests/ping_test.sh index 4a2dda0ebcce..1dff25bd43a6 100644 --- a/sbin/ping/tests/ping_test.sh +++ b/sbin/ping/tests/ping_test.sh @@ -135,21 +135,34 @@ ping_46_body() require_ipv4 require_ipv6 atf_check -s exit:1 \ - -e match:"-4 and -6 cannot be used simultaneously" \ + -e match:"illegal option -- 6" \ ping -4 -6 localhost } -ping6_46_head() +ping_64_head() { atf_set "descr" "-4 and -6 cannot be used simultaneously" } -ping6_46_body() +ping_64_body() { require_ipv4 require_ipv6 atf_check -s exit:1 \ - -e match:"-4 and -6 cannot be used simultaneously" \ - ping6 -4 -6 localhost + -e match:"illegal option -- 4" \ + ping -6 -4 localhost +} + +ping6_4_head() +{ + atf_set "descr" "ping6 does not accept -4" +} +ping6_4_body() +{ + require_ipv4 + require_ipv6 + atf_check -s exit:1 \ + -e match:"illegal option -- 4" \ + ping6 -4 localhost } atf_test_case "inject_opts" "cleanup" @@ -212,7 +225,8 @@ atf_init_test_cases() atf_add_test_case ping_c1t6 atf_add_test_case ping6_c1t4 atf_add_test_case ping_46 - atf_add_test_case ping6_46 + atf_add_test_case ping_64 + atf_add_test_case ping6_4 atf_add_test_case inject_opts atf_add_test_case inject_pip atf_add_test_case inject_reply