aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/curl/lib/altsvc.c
diff options
context:
space:
mode:
authorAlexSm <alex@ydb.tech>2024-01-18 11:28:56 +0100
committerGitHub <noreply@github.com>2024-01-18 11:28:56 +0100
commit9d0a3761b3201e0d9db879a7adf91876ebdb0564 (patch)
tree541d11ac878c18efd7ebca81e35112aa0fef995b /contrib/libs/curl/lib/altsvc.c
parent404ef8886ecc9736bc58ade6da2fbd83b486a408 (diff)
downloadydb-9d0a3761b3201e0d9db879a7adf91876ebdb0564.tar.gz
Library import 8 (#1074)
* Library import 8 * Add contrib/libs/cxxsupp/libcxx/include/__verbose_abort
Diffstat (limited to 'contrib/libs/curl/lib/altsvc.c')
-rw-r--r--contrib/libs/curl/lib/altsvc.c111
1 files changed, 86 insertions, 25 deletions
diff --git a/contrib/libs/curl/lib/altsvc.c b/contrib/libs/curl/lib/altsvc.c
index 7bca840151..35450d6b1c 100644
--- a/contrib/libs/curl/lib/altsvc.c
+++ b/contrib/libs/curl/lib/altsvc.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 2019 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 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
@@ -38,6 +38,8 @@
#include "warnless.h"
#include "fopen.h"
#include "rename.h"
+#include "strdup.h"
+#include "inet_pton.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
@@ -95,19 +97,37 @@ static struct altsvc *altsvc_createid(const char *srchost,
unsigned int srcport,
unsigned int dstport)
{
- struct altsvc *as = calloc(sizeof(struct altsvc), 1);
+ struct altsvc *as = calloc(1, sizeof(struct altsvc));
size_t hlen;
+ size_t dlen;
if(!as)
return NULL;
hlen = strlen(srchost);
+ dlen = strlen(dsthost);
DEBUGASSERT(hlen);
- as->src.host = strdup(srchost);
+ DEBUGASSERT(dlen);
+ if(!hlen || !dlen)
+ /* bad input */
+ return NULL;
+ if((hlen > 2) && srchost[0] == '[') {
+ /* IPv6 address, strip off brackets */
+ srchost++;
+ hlen -= 2;
+ }
+ else if(srchost[hlen - 1] == '.')
+ /* strip off trailing dot */
+ hlen--;
+ if((dlen > 2) && dsthost[0] == '[') {
+ /* IPv6 address, strip off brackets */
+ dsthost++;
+ dlen -= 2;
+ }
+
+ as->src.host = Curl_strndup(srchost, hlen);
if(!as->src.host)
goto error;
- if(hlen && (srchost[hlen - 1] == '.'))
- /* strip off trailing any dot */
- as->src.host[--hlen] = 0;
- as->dst.host = strdup(dsthost);
+
+ as->dst.host = Curl_strndup(dsthost, dlen);
if(!as->dst.host)
goto error;
@@ -117,7 +137,7 @@ static struct altsvc *altsvc_createid(const char *srchost,
as->dst.port = curlx_ultous(dstport);
return as;
- error:
+error:
altsvc_free(as);
return NULL;
}
@@ -217,7 +237,7 @@ static CURLcode altsvc_load(struct altsvcinfo *asi, const char *file)
}
return result;
- fail:
+fail:
Curl_safefree(asi->filename);
free(line);
fclose(fp);
@@ -231,18 +251,40 @@ static CURLcode altsvc_load(struct altsvcinfo *asi, const char *file)
static CURLcode altsvc_out(struct altsvc *as, FILE *fp)
{
struct tm stamp;
+ const char *dst6_pre = "";
+ const char *dst6_post = "";
+ const char *src6_pre = "";
+ const char *src6_post = "";
CURLcode result = Curl_gmtime(as->expires, &stamp);
if(result)
return result;
-
+#ifdef ENABLE_IPV6
+ else {
+ char ipv6_unused[16];
+ if(1 == Curl_inet_pton(AF_INET6, as->dst.host, ipv6_unused)) {
+ dst6_pre = "[";
+ dst6_post = "]";
+ }
+ if(1 == Curl_inet_pton(AF_INET6, as->src.host, ipv6_unused)) {
+ src6_pre = "[";
+ src6_post = "]";
+ }
+ }
+#endif
fprintf(fp,
- "%s %s %u "
- "%s %s %u "
+ "%s %s%s%s %u "
+ "%s %s%s%s %u "
"\"%d%02d%02d "
"%02d:%02d:%02d\" "
"%u %d\n",
- Curl_alpnid2str(as->src.alpnid), as->src.host, as->src.port,
- Curl_alpnid2str(as->dst.alpnid), as->dst.host, as->dst.port,
+ Curl_alpnid2str(as->src.alpnid),
+ src6_pre, as->src.host, src6_post,
+ as->src.port,
+
+ Curl_alpnid2str(as->dst.alpnid),
+ dst6_pre, as->dst.host, dst6_post,
+ as->dst.port,
+
stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
stamp.tm_hour, stamp.tm_min, stamp.tm_sec,
as->persist, as->prio);
@@ -257,7 +299,7 @@ static CURLcode altsvc_out(struct altsvc *as, FILE *fp)
*/
struct altsvcinfo *Curl_altsvc_init(void)
{
- struct altsvcinfo *asi = calloc(sizeof(struct altsvcinfo), 1);
+ struct altsvcinfo *asi = calloc(1, sizeof(struct altsvcinfo));
if(!asi)
return NULL;
Curl_llist_init(&asi->list, NULL);
@@ -424,7 +466,7 @@ static void altsvc_flush(struct altsvcinfo *asi, enum alpnid srcalpnid,
#ifdef DEBUGBUILD
/* to play well with debug builds, we can *set* a fixed time this will
return */
-static time_t debugtime(void *unused)
+static time_t altsvc_debugtime(void *unused)
{
char *timestr = getenv("CURL_TIME");
(void)unused;
@@ -434,7 +476,8 @@ static time_t debugtime(void *unused)
}
return time(NULL);
}
-#define time(x) debugtime(x)
+#undef time
+#define time(x) altsvc_debugtime(x)
#endif
#define ISNEWLINE(x) (((x) == '\n') || (x) == '\r')
@@ -499,9 +542,21 @@ CURLcode Curl_altsvc_parse(struct Curl_easy *data,
if(*p != ':') {
/* host name starts here */
const char *hostp = p;
- while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-')))
- p++;
- len = p - hostp;
+ if(*p == '[') {
+ /* pass all valid IPv6 letters - does not handle zone id */
+ len = strspn(++p, "0123456789abcdefABCDEF:.");
+ if(p[len] != ']')
+ /* invalid host syntax, bail out */
+ break;
+ /* we store the IPv6 numerical address *with* brackets */
+ len += 2;
+ p = &p[len-1];
+ }
+ else {
+ while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-')))
+ p++;
+ len = p - hostp;
+ }
if(!len || (len >= MAX_ALTSVC_HOSTLEN)) {
infof(data, "Excessive alt-svc host name, ignoring.");
valid = FALSE;
@@ -517,15 +572,21 @@ CURLcode Curl_altsvc_parse(struct Curl_easy *data,
dsthost = srchost;
}
if(*p == ':') {
- /* a port number */
- unsigned long port = strtoul(++p, &end_ptr, 10);
- if(port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') {
+ unsigned long port = 0;
+ p++;
+ if(ISDIGIT(*p))
+ /* a port number */
+ port = strtoul(p, &end_ptr, 10);
+ else
+ end_ptr = (char *)p; /* not left uninitialized */
+ if(!port || port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') {
infof(data, "Unknown alt-svc port number, ignoring.");
valid = FALSE;
}
- else
+ else {
dstport = curlx_ultous(port);
- p = end_ptr;
+ p = end_ptr;
+ }
}
if(*p++ != '\"')
break;