diff options
author | Daniel Stenberg <daniel@haxx.se> | 2022-01-01 22:33:56 +0100 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2022-01-02 17:19:05 +0100 |
commit | a3c6e04303834eacc846202efc73730dec87b6dc (patch) | |
tree | 380f4f4f3041dae5e4068e14c2bf28be0db9f322 | |
parent | 78dcbe40a6cb36f5fa56c9c479989b3dca36de6d (diff) | |
download | curl-a3c6e04303834eacc846202efc73730dec87b6dc.tar.gz |
urlapi: if possible, shorten given numerical IPv6 addressesbagder/urlapi-shorten-ipv6
Extended test 1560 to verify
Closes #8206
-rw-r--r-- | lib/urlapi.c | 26 | ||||
-rw-r--r-- | tests/libtest/lib1560.c | 5 |
2 files changed, 22 insertions, 9 deletions
diff --git a/lib/urlapi.c b/lib/urlapi.c index d88db2978..d29aeb238 100644 --- a/lib/urlapi.c +++ b/lib/urlapi.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -30,6 +30,7 @@ #include "escape.h" #include "curl_ctype.h" #include "inet_pton.h" +#include "inet_ntop.h" /* The last 3 #include files should be in this order */ #include "curl_printf.h" @@ -630,9 +631,6 @@ static CURLUcode hostname_check(struct Curl_URL *u, char *hostname) size_t hlen = strlen(hostname); if(hostname[0] == '[') { -#ifdef ENABLE_IPV6 - char dest[16]; /* fits a binary IPv6 address */ -#endif const char *l = "0123456789abcdefABCDEF:."; if(hlen < 4) /* '[::]' is the shortest possible valid string */ return CURLUE_BAD_IPV6; @@ -671,10 +669,22 @@ static CURLUcode hostname_check(struct Curl_URL *u, char *hostname) /* hostname is fine */ } #ifdef ENABLE_IPV6 - hostname[hlen] = 0; /* end the address there */ - if(1 != Curl_inet_pton(AF_INET6, hostname, dest)) - return CURLUE_BAD_IPV6; - hostname[hlen] = ']'; /* restore ending bracket */ + { + char dest[16]; /* fits a binary IPv6 address */ + char norm[MAX_IPADR_LEN]; + hostname[hlen] = 0; /* end the address there */ + if(1 != Curl_inet_pton(AF_INET6, hostname, dest)) + return CURLUE_BAD_IPV6; + + /* check if it can be done shorter */ + if(Curl_inet_ntop(AF_INET6, dest, norm, sizeof(norm)) && + (strlen(norm) < hlen)) { + strcpy(hostname, norm); + hlen = strlen(norm); + hostname[hlen + 1] = 0; + } + hostname[hlen] = ']'; /* restore ending bracket */ + } #endif } else { diff --git a/tests/libtest/lib1560.c b/tests/libtest/lib1560.c index 7a4e12b78..76148499d 100644 --- a/tests/libtest/lib1560.c +++ b/tests/libtest/lib1560.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -164,6 +164,9 @@ static const struct testcase get_parts_list[] ={ "https | user | password | [13] | example.net | [15] | /ge%20t | " "this=and-what | [17]", CURLU_ALLOW_SPACE | CURLU_URLENCODE, 0, CURLUE_OK}, + {"[0:0:0:0:0:0:0:1]", + "http | [11] | [12] | [13] | [::1] | [15] | / | [16] | [17]", + CURLU_GUESS_SCHEME, 0, CURLUE_OK }, {"[::1]", "http | [11] | [12] | [13] | [::1] | [15] | / | [16] | [17]", CURLU_GUESS_SCHEME, 0, CURLUE_OK }, |