altsvc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /*
  23. * The Alt-Svc: header is defined in RFC 7838:
  24. * https://tools.ietf.org/html/rfc7838
  25. */
  26. #include "curl_setup.h"
  27. #if !defined(CURL_DISABLE_HTTP) && defined(USE_ALTSVC)
  28. #include <curl/curl.h>
  29. #include "urldata.h"
  30. #include "altsvc.h"
  31. #include "curl_get_line.h"
  32. #include "strcase.h"
  33. #include "parsedate.h"
  34. #include "sendf.h"
  35. #include "warnless.h"
  36. /* The last 3 #include files should be in this order */
  37. #include "curl_printf.h"
  38. #include "curl_memory.h"
  39. #include "memdebug.h"
  40. #define MAX_ALTSVC_LINE 4095
  41. #define MAX_ALTSVC_DATELENSTR "64"
  42. #define MAX_ALTSVC_DATELEN 64
  43. #define MAX_ALTSVC_HOSTLENSTR "512"
  44. #define MAX_ALTSVC_HOSTLEN 512
  45. #define MAX_ALTSVC_ALPNLENSTR "10"
  46. #define MAX_ALTSVC_ALPNLEN 10
  47. static enum alpnid alpn2alpnid(char *name)
  48. {
  49. if(strcasecompare(name, "h1"))
  50. return ALPN_h1;
  51. if(strcasecompare(name, "h2"))
  52. return ALPN_h2;
  53. #if (defined(USE_QUICHE) || defined(USE_NGHTTP2)) && !defined(UNITTESTS)
  54. if(strcasecompare(name, "h3-22"))
  55. return ALPN_h3;
  56. #else
  57. if(strcasecompare(name, "h3"))
  58. return ALPN_h3;
  59. #endif
  60. return ALPN_none; /* unknown, probably rubbish input */
  61. }
  62. /* Given the ALPN ID, return the name */
  63. const char *Curl_alpnid2str(enum alpnid id)
  64. {
  65. switch(id) {
  66. case ALPN_h1:
  67. return "h1";
  68. case ALPN_h2:
  69. return "h2";
  70. case ALPN_h3:
  71. #if (defined(USE_QUICHE) || defined(USE_NGHTTP2)) && !defined(UNITTESTS)
  72. return "h3-22";
  73. #else
  74. return "h3";
  75. #endif
  76. default:
  77. return ""; /* bad */
  78. }
  79. }
  80. static void altsvc_free(struct altsvc *as)
  81. {
  82. free(as->src.host);
  83. free(as->dst.host);
  84. free(as);
  85. }
  86. static struct altsvc *altsvc_createid(const char *srchost,
  87. const char *dsthost,
  88. enum alpnid srcalpnid,
  89. enum alpnid dstalpnid,
  90. unsigned int srcport,
  91. unsigned int dstport)
  92. {
  93. struct altsvc *as = calloc(sizeof(struct altsvc), 1);
  94. if(!as)
  95. return NULL;
  96. as->src.host = strdup(srchost);
  97. if(!as->src.host)
  98. goto error;
  99. as->dst.host = strdup(dsthost);
  100. if(!as->dst.host)
  101. goto error;
  102. as->src.alpnid = srcalpnid;
  103. as->dst.alpnid = dstalpnid;
  104. as->src.port = curlx_ultous(srcport);
  105. as->dst.port = curlx_ultous(dstport);
  106. return as;
  107. error:
  108. altsvc_free(as);
  109. return NULL;
  110. }
  111. static struct altsvc *altsvc_create(char *srchost,
  112. char *dsthost,
  113. char *srcalpn,
  114. char *dstalpn,
  115. unsigned int srcport,
  116. unsigned int dstport)
  117. {
  118. enum alpnid dstalpnid = alpn2alpnid(dstalpn);
  119. enum alpnid srcalpnid = alpn2alpnid(srcalpn);
  120. if(!srcalpnid || !dstalpnid)
  121. return NULL;
  122. return altsvc_createid(srchost, dsthost, srcalpnid, dstalpnid,
  123. srcport, dstport);
  124. }
  125. /* only returns SERIOUS errors */
  126. static CURLcode altsvc_add(struct altsvcinfo *asi, char *line)
  127. {
  128. /* Example line:
  129. h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1
  130. */
  131. char srchost[MAX_ALTSVC_HOSTLEN + 1];
  132. char dsthost[MAX_ALTSVC_HOSTLEN + 1];
  133. char srcalpn[MAX_ALTSVC_ALPNLEN + 1];
  134. char dstalpn[MAX_ALTSVC_ALPNLEN + 1];
  135. char date[MAX_ALTSVC_DATELEN + 1];
  136. unsigned int srcport;
  137. unsigned int dstport;
  138. unsigned int prio;
  139. unsigned int persist;
  140. int rc;
  141. rc = sscanf(line,
  142. "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u "
  143. "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u "
  144. "\"%" MAX_ALTSVC_DATELENSTR "[^\"]\" %u %u",
  145. srcalpn, srchost, &srcport,
  146. dstalpn, dsthost, &dstport,
  147. date, &persist, &prio);
  148. if(9 == rc) {
  149. struct altsvc *as;
  150. time_t expires = curl_getdate(date, NULL);
  151. as = altsvc_create(srchost, dsthost, srcalpn, dstalpn, srcport, dstport);
  152. if(as) {
  153. as->expires = expires;
  154. as->prio = prio;
  155. as->persist = persist ? 1 : 0;
  156. Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
  157. asi->num++; /* one more entry */
  158. }
  159. }
  160. return CURLE_OK;
  161. }
  162. /*
  163. * Load alt-svc entries from the given file. The text based line-oriented file
  164. * format is documented here:
  165. * https://github.com/curl/curl/wiki/QUIC-implementation
  166. *
  167. * This function only returns error on major problems that prevents alt-svc
  168. * handling to work completely. It will ignore individual syntactical errors
  169. * etc.
  170. */
  171. static CURLcode altsvc_load(struct altsvcinfo *asi, const char *file)
  172. {
  173. CURLcode result = CURLE_OK;
  174. char *line = NULL;
  175. FILE *fp = fopen(file, FOPEN_READTEXT);
  176. if(fp) {
  177. line = malloc(MAX_ALTSVC_LINE);
  178. if(!line)
  179. goto fail;
  180. while(Curl_get_line(line, MAX_ALTSVC_LINE, fp)) {
  181. char *lineptr = line;
  182. while(*lineptr && ISBLANK(*lineptr))
  183. lineptr++;
  184. if(*lineptr == '#')
  185. /* skip commented lines */
  186. continue;
  187. altsvc_add(asi, lineptr);
  188. }
  189. free(line); /* free the line buffer */
  190. fclose(fp);
  191. }
  192. return result;
  193. fail:
  194. free(line);
  195. fclose(fp);
  196. return CURLE_OUT_OF_MEMORY;
  197. }
  198. /*
  199. * Write this single altsvc entry to a single output line
  200. */
  201. static CURLcode altsvc_out(struct altsvc *as, FILE *fp)
  202. {
  203. struct tm stamp;
  204. CURLcode result = Curl_gmtime(as->expires, &stamp);
  205. if(result)
  206. return result;
  207. fprintf(fp,
  208. "%s %s %u "
  209. "%s %s %u "
  210. "\"%d%02d%02d "
  211. "%02d:%02d:%02d\" "
  212. "%u %d\n",
  213. Curl_alpnid2str(as->src.alpnid), as->src.host, as->src.port,
  214. Curl_alpnid2str(as->dst.alpnid), as->dst.host, as->dst.port,
  215. stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
  216. stamp.tm_hour, stamp.tm_min, stamp.tm_sec,
  217. as->persist, as->prio);
  218. return CURLE_OK;
  219. }
  220. /* ---- library-wide functions below ---- */
  221. /*
  222. * Curl_altsvc_init() creates a new altsvc cache.
  223. * It returns the new instance or NULL if something goes wrong.
  224. */
  225. struct altsvcinfo *Curl_altsvc_init(void)
  226. {
  227. struct altsvcinfo *asi = calloc(sizeof(struct altsvcinfo), 1);
  228. if(!asi)
  229. return NULL;
  230. Curl_llist_init(&asi->list, NULL);
  231. /* set default behavior */
  232. asi->flags = CURLALTSVC_H1
  233. #ifdef USE_NGHTTP2
  234. | CURLALTSVC_H2
  235. #endif
  236. #ifdef ENABLE_QUIC
  237. | CURLALTSVC_H3
  238. #endif
  239. ;
  240. return asi;
  241. }
  242. /*
  243. * Curl_altsvc_load() loads alt-svc from file.
  244. */
  245. CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file)
  246. {
  247. CURLcode result;
  248. DEBUGASSERT(asi);
  249. result = altsvc_load(asi, file);
  250. return result;
  251. }
  252. /*
  253. * Curl_altsvc_ctrl() passes on the external bitmask.
  254. */
  255. CURLcode Curl_altsvc_ctrl(struct altsvcinfo *asi, const long ctrl)
  256. {
  257. DEBUGASSERT(asi);
  258. if(!ctrl)
  259. /* unexpected */
  260. return CURLE_BAD_FUNCTION_ARGUMENT;
  261. asi->flags = ctrl;
  262. return CURLE_OK;
  263. }
  264. /*
  265. * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated
  266. * resources.
  267. */
  268. void Curl_altsvc_cleanup(struct altsvcinfo *altsvc)
  269. {
  270. struct curl_llist_element *e;
  271. struct curl_llist_element *n;
  272. if(altsvc) {
  273. for(e = altsvc->list.head; e; e = n) {
  274. struct altsvc *as = e->ptr;
  275. n = e->next;
  276. altsvc_free(as);
  277. }
  278. free(altsvc);
  279. }
  280. }
  281. /*
  282. * Curl_altsvc_save() writes the altsvc cache to a file.
  283. */
  284. CURLcode Curl_altsvc_save(struct altsvcinfo *altsvc, const char *file)
  285. {
  286. struct curl_llist_element *e;
  287. struct curl_llist_element *n;
  288. CURLcode result = CURLE_OK;
  289. FILE *out;
  290. if(!altsvc)
  291. /* no cache activated */
  292. return CURLE_OK;
  293. if((altsvc->flags & CURLALTSVC_READONLYFILE) || !file[0])
  294. /* marked as read-only or zero length file name */
  295. return CURLE_OK;
  296. out = fopen(file, FOPEN_WRITETEXT);
  297. if(!out)
  298. return CURLE_WRITE_ERROR;
  299. fputs("# Your alt-svc cache. https://curl.haxx.se/docs/alt-svc.html\n"
  300. "# This file was generated by libcurl! Edit at your own risk.\n",
  301. out);
  302. for(e = altsvc->list.head; e; e = n) {
  303. struct altsvc *as = e->ptr;
  304. n = e->next;
  305. result = altsvc_out(as, out);
  306. if(result)
  307. break;
  308. }
  309. fclose(out);
  310. return result;
  311. }
  312. static CURLcode getalnum(const char **ptr, char *alpnbuf, size_t buflen)
  313. {
  314. size_t len;
  315. const char *protop;
  316. const char *p = *ptr;
  317. while(*p && ISBLANK(*p))
  318. p++;
  319. protop = p;
  320. while(*p && !ISBLANK(*p) && (*p != ';') && (*p != '='))
  321. p++;
  322. len = p - protop;
  323. if(!len || (len >= buflen))
  324. return CURLE_BAD_FUNCTION_ARGUMENT;
  325. memcpy(alpnbuf, protop, len);
  326. alpnbuf[len] = 0;
  327. *ptr = p;
  328. return CURLE_OK;
  329. }
  330. /* altsvc_flush() removes all alternatives for this source origin from the
  331. list */
  332. static void altsvc_flush(struct altsvcinfo *asi, enum alpnid srcalpnid,
  333. const char *srchost, unsigned short srcport)
  334. {
  335. struct curl_llist_element *e;
  336. struct curl_llist_element *n;
  337. for(e = asi->list.head; e; e = n) {
  338. struct altsvc *as = e->ptr;
  339. n = e->next;
  340. if((srcalpnid == as->src.alpnid) &&
  341. (srcport == as->src.port) &&
  342. strcasecompare(srchost, as->src.host)) {
  343. Curl_llist_remove(&asi->list, e, NULL);
  344. altsvc_free(as);
  345. asi->num--;
  346. }
  347. }
  348. }
  349. #ifdef DEBUGBUILD
  350. /* to play well with debug builds, we can *set* a fixed time this will
  351. return */
  352. static time_t debugtime(void *unused)
  353. {
  354. char *timestr = getenv("CURL_TIME");
  355. (void)unused;
  356. if(timestr) {
  357. unsigned long val = strtol(timestr, NULL, 10);
  358. return (time_t)val;
  359. }
  360. return time(NULL);
  361. }
  362. #define time(x) debugtime(x)
  363. #endif
  364. /*
  365. * Curl_altsvc_parse() takes an incoming alt-svc response header and stores
  366. * the data correctly in the cache.
  367. *
  368. * 'value' points to the header *value*. That's contents to the right of the
  369. * header name.
  370. */
  371. CURLcode Curl_altsvc_parse(struct Curl_easy *data,
  372. struct altsvcinfo *asi, const char *value,
  373. enum alpnid srcalpnid, const char *srchost,
  374. unsigned short srcport)
  375. {
  376. const char *p = value;
  377. size_t len;
  378. enum alpnid dstalpnid = srcalpnid; /* the same by default */
  379. char namebuf[MAX_ALTSVC_HOSTLEN] = "";
  380. char alpnbuf[MAX_ALTSVC_ALPNLEN] = "";
  381. struct altsvc *as;
  382. unsigned short dstport = srcport; /* the same by default */
  383. const char *semip;
  384. time_t maxage = 24 * 3600; /* default is 24 hours */
  385. bool persist = FALSE;
  386. CURLcode result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
  387. if(result)
  388. return result;
  389. DEBUGASSERT(asi);
  390. /* Flush all cached alternatives for this source origin, if any */
  391. altsvc_flush(asi, srcalpnid, srchost, srcport);
  392. /* "clear" is a magic keyword */
  393. if(strcasecompare(alpnbuf, "clear")) {
  394. return CURLE_OK;
  395. }
  396. /* The 'ma' and 'persist' flags are annoyingly meant for all alternatives
  397. but are set after the list on the line. Scan for the semicolons and get
  398. those fields first! */
  399. semip = p;
  400. do {
  401. semip = strchr(semip, ';');
  402. if(semip) {
  403. char option[32];
  404. unsigned long num;
  405. char *end_ptr;
  406. semip++; /* pass the semicolon */
  407. result = getalnum(&semip, option, sizeof(option));
  408. if(result)
  409. break;
  410. while(*semip && ISBLANK(*semip))
  411. semip++;
  412. if(*semip != '=')
  413. continue;
  414. semip++;
  415. num = strtoul(semip, &end_ptr, 10);
  416. if(num < ULONG_MAX) {
  417. if(strcasecompare("ma", option))
  418. maxage = num;
  419. else if(strcasecompare("persist", option) && (num == 1))
  420. persist = TRUE;
  421. }
  422. semip = end_ptr;
  423. }
  424. } while(semip);
  425. do {
  426. if(*p == '=') {
  427. /* [protocol]="[host][:port]" */
  428. dstalpnid = alpn2alpnid(alpnbuf);
  429. if(!dstalpnid) {
  430. infof(data, "Unknown alt-svc protocol \"%s\", ignoring...\n", alpnbuf);
  431. return CURLE_OK;
  432. }
  433. p++;
  434. if(*p == '\"') {
  435. const char *dsthost;
  436. p++;
  437. if(*p != ':') {
  438. /* host name starts here */
  439. const char *hostp = p;
  440. while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-')))
  441. p++;
  442. len = p - hostp;
  443. if(!len || (len >= MAX_ALTSVC_HOSTLEN))
  444. return CURLE_BAD_FUNCTION_ARGUMENT;
  445. memcpy(namebuf, hostp, len);
  446. namebuf[len] = 0;
  447. dsthost = namebuf;
  448. }
  449. else {
  450. /* no destination name, use source host */
  451. dsthost = srchost;
  452. }
  453. if(*p == ':') {
  454. /* a port number */
  455. char *end_ptr;
  456. unsigned long port = strtoul(++p, &end_ptr, 10);
  457. if(port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') {
  458. infof(data, "Unknown alt-svc port number, ignoring...\n");
  459. return CURLE_OK;
  460. }
  461. p = end_ptr;
  462. dstport = curlx_ultous(port);
  463. }
  464. if(*p++ != '\"')
  465. return CURLE_BAD_FUNCTION_ARGUMENT;
  466. as = altsvc_createid(srchost, dsthost,
  467. srcalpnid, dstalpnid,
  468. srcport, dstport);
  469. if(as) {
  470. /* The expires time also needs to take the Age: value (if any) into
  471. account. [See RFC 7838 section 3.1] */
  472. as->expires = maxage + time(NULL);
  473. as->persist = persist;
  474. Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
  475. asi->num++; /* one more entry */
  476. infof(data, "Added alt-svc: %s:%d over %s\n", dsthost, dstport,
  477. Curl_alpnid2str(dstalpnid));
  478. }
  479. }
  480. /* after the double quote there can be a comma if there's another
  481. string or a semicolon if no more */
  482. if(*p == ',') {
  483. /* comma means another alternative is presented */
  484. p++;
  485. result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
  486. if(result)
  487. /* failed to parse, but since we already did at least one host we
  488. return OK */
  489. return CURLE_OK;
  490. }
  491. }
  492. } while(*p && (*p != ';') && (*p != '\n') && (*p != '\r'));
  493. return CURLE_OK;
  494. }
  495. /*
  496. * Return TRUE on a match
  497. */
  498. bool Curl_altsvc_lookup(struct altsvcinfo *asi,
  499. enum alpnid srcalpnid, const char *srchost,
  500. int srcport,
  501. struct altsvc **dstentry,
  502. const int versions) /* one or more bits */
  503. {
  504. struct curl_llist_element *e;
  505. struct curl_llist_element *n;
  506. time_t now = time(NULL);
  507. DEBUGASSERT(asi);
  508. DEBUGASSERT(srchost);
  509. DEBUGASSERT(dstentry);
  510. for(e = asi->list.head; e; e = n) {
  511. struct altsvc *as = e->ptr;
  512. n = e->next;
  513. if(as->expires < now) {
  514. /* an expired entry, remove */
  515. Curl_llist_remove(&asi->list, e, NULL);
  516. altsvc_free(as);
  517. continue;
  518. }
  519. if((as->src.alpnid == srcalpnid) &&
  520. strcasecompare(as->src.host, srchost) &&
  521. (as->src.port == srcport) &&
  522. (versions & as->dst.alpnid)) {
  523. /* match */
  524. *dstentry = as;
  525. return TRUE;
  526. }
  527. }
  528. return FALSE;
  529. }
  530. #endif /* CURL_DISABLE_HTTP || USE_ALTSVC */