From be1d976a2ab998aaaa824e4396e43c647a17cdb2 Mon Sep 17 00:00:00 2001 From: Stefan Eissing Date: Mon, 22 Jun 2026 14:45:25 +0200 Subject: [PATCH] peer: fix ipv6 detection When trying to detect ipv6 addresses, ipv4 addresses were also flagged as ipv6. Add test2413 to check. Closes #22134 --- lib/peer.c | 9 +++- tests/data/Makefile.am | 2 +- tests/data/test2413 | 19 +++++++ tests/unit/Makefile.inc | 1 + tests/unit/unit2413.c | 112 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 tests/data/test2413 create mode 100644 tests/unit/unit2413.c diff --git a/lib/peer.c b/lib/peer.c index d559d1dee3..44bcb52f23 100644 --- a/lib/peer.c +++ b/lib/peer.c @@ -222,7 +222,14 @@ static CURLcode peer_parse_host(struct Curl_easy *data, if(scan_for_ipv6 && Curl_looks_like_ipv6(pp->host_user.str, pp->host_user.len, TRUE, &pp->host, &pp->zoneid)) { - pp->ipv6 = TRUE; + if(pp->host_user.len < MAX_IPADR_LEN) { + char tmp[MAX_IPADR_LEN]; + memcpy(tmp, pp->host_user.str, pp->host_user.len); + tmp[pp->host_user.len] = 0; + pp->ipv6 = !Curl_is_ipv4addr(tmp); + } + else + pp->ipv6 = TRUE; } else pp->host = pp->host_user; diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am index 6e8eca22bd..9fb1720f7c 100644 --- a/tests/data/Makefile.am +++ b/tests/data/Makefile.am @@ -263,7 +263,7 @@ test2300 test2301 test2302 test2303 test2304 test2306 test2307 test2308 \ test2309 test2310 test2311 \ \ test2400 test2401 test2402 test2403 test2404 test2405 test2406 test2407 \ -test2408 test2409 test2410 test2411 test2412 \ +test2408 test2409 test2410 test2411 test2412 test2413 \ \ test2500 test2501 test2502 test2503 test2504 test2505 test2506 \ \ diff --git a/tests/data/test2413 b/tests/data/test2413 new file mode 100644 index 0000000000..0b0e8a08aa --- /dev/null +++ b/tests/data/test2413 @@ -0,0 +1,19 @@ + + + + +unittest +Curl_peer + + + +# Client-side + + +unittest + + +Curl_peer unit tests + + + diff --git a/tests/unit/Makefile.inc b/tests/unit/Makefile.inc index c6c75c781d..18d97b051c 100644 --- a/tests/unit/Makefile.inc +++ b/tests/unit/Makefile.inc @@ -44,6 +44,7 @@ TESTS_C = \ unit1666.c unit1667.c unit1668.c unit1669.c \ unit1674.c unit1675.c unit1676.c \ unit1979.c unit1980.c \ + unit2413.c \ unit2600.c unit2601.c unit2602.c unit2603.c unit2604.c unit2605.c \ unit3200.c unit3205.c \ unit3211.c unit3212.c unit3213.c unit3214.c unit3216.c unit3219.c \ diff --git a/tests/unit/unit2413.c b/tests/unit/unit2413.c new file mode 100644 index 0000000000..a800971ae1 --- /dev/null +++ b/tests/unit/unit2413.c @@ -0,0 +1,112 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at https://curl.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + * SPDX-License-Identifier: curl + * + ***************************************************************************/ +#include "unitcheck.h" +#include "urldata.h" + +static CURLcode test_create2413(const char *name, + CURL *curl, + const struct Curl_scheme *scheme, + const char *hostname, + uint16_t port, + const char *exp_hostname, + bool exp_ipv6, + const char *exp_zoneid) +{ + struct Curl_peer *peer = NULL; + CURLcode result; + + result = Curl_peer_create((struct Curl_easy *)curl, + scheme, hostname, port, &peer); + if(result) { + curl_mfprintf(stderr, "%s: create failed %d", name, (int)result); + goto out; + } + + result = CURLE_FAILED_INIT; + if(peer->scheme != scheme) { + curl_mfprintf(stderr, "%s: has wrong scheme", name); + } + else if(!curl_strequal(peer->user_hostname, hostname)) { + curl_mfprintf(stderr, "%s: user_hostname=%s, expected %s", name, + peer->user_hostname, exp_hostname); + } + else if(exp_hostname && !curl_strequal(peer->hostname, exp_hostname)) + curl_mfprintf(stderr, "%s: hostname=%s, expected %s", name, + peer->hostname, exp_hostname); + else if(peer->port != port) + curl_mfprintf(stderr, "%s: port=%u, expected %u", name, + peer->port, port); + else if((bool)peer->ipv6 != exp_ipv6) + curl_mfprintf(stderr, "%s: ipv6=%d, expected %d", name, + peer->ipv6, exp_ipv6); + else if(exp_zoneid && + (!peer->zoneid || !curl_strequal(exp_zoneid, peer->zoneid))) + curl_mfprintf(stderr, "%s: zoneid=%s, expected %s", name, + peer->zoneid, exp_zoneid); + else if(!exp_zoneid && peer->zoneid) + curl_mfprintf(stderr, "%s: zoneid=%s, expected nothing", name, + peer->zoneid); + else + result = CURLE_OK; + +out: + Curl_peer_unlink(&peer); + fail_unless(!result, "check failed"); + return result; +} + +static CURLcode test_unit2413(const char *arg) +{ + UNITTEST_BEGIN_SIMPLE + CURL *curl; + struct Curl_peer *peer = NULL; + + curl_global_init(CURL_GLOBAL_ALL); + curl = curl_easy_init(); + if(!curl) { + curl_global_cleanup(); + goto unit_test_abort; + } + + test_create2413("peer1", curl, &Curl_scheme_https, "test.curl.se", 1234, + "test.curl.se", FALSE, NULL); + test_create2413("peer2", curl, &Curl_scheme_https, "127.0.0.1", 1234, + "127.0.0.1", FALSE, NULL); + test_create2413("peer3", curl, &Curl_scheme_https, "::1", 1234, + "::1", TRUE, NULL); + test_create2413("peer3", curl, &Curl_scheme_https, "[::1]", 1234, + "::1", TRUE, NULL); + test_create2413("peer4", curl, &Curl_scheme_https, "test.curl.se.", 1234, + "test.curl.se.", FALSE, NULL); + test_create2413("peer5", curl, &Curl_scheme_https, "[::1%tada]", 1234, + "::1", TRUE, "tada"); + test_create2413("peer6", curl, &Curl_scheme_https, "::1%tada", 1234, + "::1", TRUE, "tada"); + + curl_easy_cleanup(curl); + Curl_peer_unlink(&peer); + curl_global_cleanup(); + + UNITTEST_END_SIMPLE +} -- 2.47.3