2
0

curl_msh3.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef USE_MSH3
  26. #include "urldata.h"
  27. #include "timeval.h"
  28. #include "multiif.h"
  29. #include "sendf.h"
  30. #include "curl_log.h"
  31. #include "cfilters.h"
  32. #include "cf-socket.h"
  33. #include "connect.h"
  34. #include "progress.h"
  35. #include "http1.h"
  36. #include "curl_msh3.h"
  37. #include "socketpair.h"
  38. #include "vquic/vquic.h"
  39. /* The last 3 #include files should be in this order */
  40. #include "curl_printf.h"
  41. #include "curl_memory.h"
  42. #include "memdebug.h"
  43. #define H3_STREAM_WINDOW_SIZE (128 * 1024)
  44. #define H3_STREAM_CHUNK_SIZE (16 * 1024)
  45. #define H3_STREAM_RECV_CHUNKS \
  46. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
  47. #ifdef _WIN32
  48. #define msh3_lock CRITICAL_SECTION
  49. #define msh3_lock_initialize(lock) InitializeCriticalSection(lock)
  50. #define msh3_lock_uninitialize(lock) DeleteCriticalSection(lock)
  51. #define msh3_lock_acquire(lock) EnterCriticalSection(lock)
  52. #define msh3_lock_release(lock) LeaveCriticalSection(lock)
  53. #else /* !_WIN32 */
  54. #include <pthread.h>
  55. #define msh3_lock pthread_mutex_t
  56. #define msh3_lock_initialize(lock) do { \
  57. pthread_mutexattr_t attr; \
  58. pthread_mutexattr_init(&attr); \
  59. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
  60. pthread_mutex_init(lock, &attr); \
  61. pthread_mutexattr_destroy(&attr); \
  62. }while(0)
  63. #define msh3_lock_uninitialize(lock) pthread_mutex_destroy(lock)
  64. #define msh3_lock_acquire(lock) pthread_mutex_lock(lock)
  65. #define msh3_lock_release(lock) pthread_mutex_unlock(lock)
  66. #endif /* _WIN32 */
  67. static void MSH3_CALL msh3_conn_connected(MSH3_CONNECTION *Connection,
  68. void *IfContext);
  69. static void MSH3_CALL msh3_conn_shutdown_complete(MSH3_CONNECTION *Connection,
  70. void *IfContext);
  71. static void MSH3_CALL msh3_conn_new_request(MSH3_CONNECTION *Connection,
  72. void *IfContext,
  73. MSH3_REQUEST *Request);
  74. static void MSH3_CALL msh3_header_received(MSH3_REQUEST *Request,
  75. void *IfContext,
  76. const MSH3_HEADER *Header);
  77. static bool MSH3_CALL msh3_data_received(MSH3_REQUEST *Request,
  78. void *IfContext, uint32_t *Length,
  79. const uint8_t *Data);
  80. static void MSH3_CALL msh3_complete(MSH3_REQUEST *Request, void *IfContext,
  81. bool Aborted, uint64_t AbortError);
  82. static void MSH3_CALL msh3_shutdown_complete(MSH3_REQUEST *Request,
  83. void *IfContext);
  84. static void MSH3_CALL msh3_data_sent(MSH3_REQUEST *Request,
  85. void *IfContext, void *SendContext);
  86. void Curl_msh3_ver(char *p, size_t len)
  87. {
  88. uint32_t v[4];
  89. MsH3Version(v);
  90. (void)msnprintf(p, len, "msh3/%d.%d.%d.%d", v[0], v[1], v[2], v[3]);
  91. }
  92. #define SP_LOCAL 0
  93. #define SP_REMOTE 1
  94. struct cf_msh3_ctx {
  95. MSH3_API *api;
  96. MSH3_CONNECTION *qconn;
  97. struct Curl_sockaddr_ex addr;
  98. curl_socket_t sock[2]; /* fake socket pair until we get support in msh3 */
  99. char l_ip[MAX_IPADR_LEN]; /* local IP as string */
  100. int l_port; /* local port number */
  101. struct cf_call_data call_data;
  102. struct curltime connect_started; /* time the current attempt started */
  103. struct curltime handshake_at; /* time connect handshake finished */
  104. /* Flags written by msh3/msquic thread */
  105. bool handshake_complete;
  106. bool handshake_succeeded;
  107. bool connected;
  108. /* Flags written by curl thread */
  109. BIT(verbose);
  110. BIT(active);
  111. };
  112. /* How to access `call_data` from a cf_msh3 filter */
  113. #undef CF_CTX_CALL_DATA
  114. #define CF_CTX_CALL_DATA(cf) \
  115. ((struct cf_msh3_ctx *)(cf)->ctx)->call_data
  116. /**
  117. * All about the H3 internals of a stream
  118. */
  119. struct stream_ctx {
  120. struct MSH3_REQUEST *req;
  121. struct bufq recvbuf; /* h3 response */
  122. #ifdef _WIN32
  123. CRITICAL_SECTION recv_lock;
  124. #else /* !_WIN32 */
  125. pthread_mutex_t recv_lock;
  126. #endif /* _WIN32 */
  127. uint64_t error3; /* HTTP/3 stream error code */
  128. int status_code; /* HTTP status code */
  129. CURLcode recv_error;
  130. bool closed;
  131. bool reset;
  132. bool upload_done;
  133. bool firstheader; /* FALSE until headers arrive */
  134. bool recv_header_complete;
  135. };
  136. #define H3_STREAM_CTX(d) ((struct stream_ctx *)(((d) && (d)->req.p.http)? \
  137. ((struct HTTP *)(d)->req.p.http)->h3_ctx \
  138. : NULL))
  139. #define H3_STREAM_LCTX(d) ((struct HTTP *)(d)->req.p.http)->h3_ctx
  140. #define H3_STREAM_ID(d) (H3_STREAM_CTX(d)? \
  141. H3_STREAM_CTX(d)->id : -2)
  142. static CURLcode h3_data_setup(struct Curl_cfilter *cf,
  143. struct Curl_easy *data)
  144. {
  145. struct stream_ctx *stream = H3_STREAM_CTX(data);
  146. if(stream)
  147. return CURLE_OK;
  148. stream = calloc(1, sizeof(*stream));
  149. if(!stream)
  150. return CURLE_OUT_OF_MEMORY;
  151. H3_STREAM_LCTX(data) = stream;
  152. stream->req = ZERO_NULL;
  153. msh3_lock_initialize(&stream->recv_lock);
  154. Curl_bufq_init2(&stream->recvbuf, H3_STREAM_CHUNK_SIZE,
  155. H3_STREAM_RECV_CHUNKS, BUFQ_OPT_SOFT_LIMIT);
  156. DEBUGF(LOG_CF(data, cf, "data setup"));
  157. return CURLE_OK;
  158. }
  159. static void h3_data_done(struct Curl_cfilter *cf, struct Curl_easy *data)
  160. {
  161. struct stream_ctx *stream = H3_STREAM_CTX(data);
  162. (void)cf;
  163. if(stream) {
  164. DEBUGF(LOG_CF(data, cf, "easy handle is done"));
  165. Curl_bufq_free(&stream->recvbuf);
  166. free(stream);
  167. H3_STREAM_LCTX(data) = NULL;
  168. }
  169. }
  170. static void drain_stream_from_other_thread(struct Curl_easy *data,
  171. struct stream_ctx *stream)
  172. {
  173. unsigned char bits;
  174. /* risky */
  175. bits = CURL_CSELECT_IN;
  176. if(stream && !stream->upload_done)
  177. bits |= CURL_CSELECT_OUT;
  178. if(data->state.dselect_bits != bits) {
  179. data->state.dselect_bits = bits;
  180. /* cannot expire from other thread */
  181. }
  182. }
  183. static void drain_stream(struct Curl_cfilter *cf,
  184. struct Curl_easy *data)
  185. {
  186. struct stream_ctx *stream = H3_STREAM_CTX(data);
  187. unsigned char bits;
  188. (void)cf;
  189. bits = CURL_CSELECT_IN;
  190. if(stream && !stream->upload_done)
  191. bits |= CURL_CSELECT_OUT;
  192. if(data->state.dselect_bits != bits) {
  193. data->state.dselect_bits = bits;
  194. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  195. }
  196. }
  197. static const MSH3_CONNECTION_IF msh3_conn_if = {
  198. msh3_conn_connected,
  199. msh3_conn_shutdown_complete,
  200. msh3_conn_new_request
  201. };
  202. static void MSH3_CALL msh3_conn_connected(MSH3_CONNECTION *Connection,
  203. void *IfContext)
  204. {
  205. struct Curl_cfilter *cf = IfContext;
  206. struct cf_msh3_ctx *ctx = cf->ctx;
  207. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  208. (void)Connection;
  209. DEBUGF(LOG_CF(data, cf, "[MSH3] connected"));
  210. ctx->handshake_succeeded = true;
  211. ctx->connected = true;
  212. ctx->handshake_complete = true;
  213. }
  214. static void MSH3_CALL msh3_conn_shutdown_complete(MSH3_CONNECTION *Connection,
  215. void *IfContext)
  216. {
  217. struct Curl_cfilter *cf = IfContext;
  218. struct cf_msh3_ctx *ctx = cf->ctx;
  219. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  220. (void)Connection;
  221. DEBUGF(LOG_CF(data, cf, "[MSH3] shutdown complete"));
  222. ctx->connected = false;
  223. ctx->handshake_complete = true;
  224. }
  225. static void MSH3_CALL msh3_conn_new_request(MSH3_CONNECTION *Connection,
  226. void *IfContext,
  227. MSH3_REQUEST *Request)
  228. {
  229. (void)Connection;
  230. (void)IfContext;
  231. (void)Request;
  232. }
  233. static const MSH3_REQUEST_IF msh3_request_if = {
  234. msh3_header_received,
  235. msh3_data_received,
  236. msh3_complete,
  237. msh3_shutdown_complete,
  238. msh3_data_sent
  239. };
  240. /* Decode HTTP status code. Returns -1 if no valid status code was
  241. decoded. (duplicate from http2.c) */
  242. static int decode_status_code(const char *value, size_t len)
  243. {
  244. int i;
  245. int res;
  246. if(len != 3) {
  247. return -1;
  248. }
  249. res = 0;
  250. for(i = 0; i < 3; ++i) {
  251. char c = value[i];
  252. if(c < '0' || c > '9') {
  253. return -1;
  254. }
  255. res *= 10;
  256. res += c - '0';
  257. }
  258. return res;
  259. }
  260. /*
  261. * write_resp_raw() copies response data in raw format to the `data`'s
  262. * receive buffer. If not enough space is available, it appends to the
  263. * `data`'s overflow buffer.
  264. */
  265. static CURLcode write_resp_raw(struct Curl_easy *data,
  266. const void *mem, size_t memlen)
  267. {
  268. struct stream_ctx *stream = H3_STREAM_CTX(data);
  269. CURLcode result = CURLE_OK;
  270. ssize_t nwritten;
  271. if(!stream)
  272. return CURLE_RECV_ERROR;
  273. nwritten = Curl_bufq_write(&stream->recvbuf, mem, memlen, &result);
  274. if(nwritten < 0) {
  275. return result;
  276. }
  277. if((size_t)nwritten < memlen) {
  278. /* This MUST not happen. Our recbuf is dimensioned to hold the
  279. * full max_stream_window and then some for this very reason. */
  280. DEBUGASSERT(0);
  281. return CURLE_RECV_ERROR;
  282. }
  283. return result;
  284. }
  285. static void MSH3_CALL msh3_header_received(MSH3_REQUEST *Request,
  286. void *userp,
  287. const MSH3_HEADER *hd)
  288. {
  289. struct Curl_easy *data = userp;
  290. struct stream_ctx *stream = H3_STREAM_CTX(data);
  291. CURLcode result;
  292. (void)Request;
  293. if(!stream || stream->recv_header_complete) {
  294. return;
  295. }
  296. msh3_lock_acquire(&stream->recv_lock);
  297. if((hd->NameLength == 7) &&
  298. !strncmp(HTTP_PSEUDO_STATUS, (char *)hd->Name, 7)) {
  299. char line[14]; /* status line is always 13 characters long */
  300. size_t ncopy;
  301. DEBUGASSERT(!stream->firstheader);
  302. stream->status_code = decode_status_code(hd->Value, hd->ValueLength);
  303. DEBUGASSERT(stream->status_code != -1);
  304. ncopy = msnprintf(line, sizeof(line), "HTTP/3 %03d \r\n",
  305. stream->status_code);
  306. result = write_resp_raw(data, line, ncopy);
  307. if(result)
  308. stream->recv_error = result;
  309. stream->firstheader = TRUE;
  310. }
  311. else {
  312. /* store as an HTTP1-style header */
  313. DEBUGASSERT(stream->firstheader);
  314. result = write_resp_raw(data, hd->Name, hd->NameLength);
  315. if(!result)
  316. result = write_resp_raw(data, ": ", 2);
  317. if(!result)
  318. result = write_resp_raw(data, hd->Value, hd->ValueLength);
  319. if(!result)
  320. result = write_resp_raw(data, "\r\n", 2);
  321. if(result) {
  322. stream->recv_error = result;
  323. }
  324. }
  325. drain_stream_from_other_thread(data, stream);
  326. msh3_lock_release(&stream->recv_lock);
  327. }
  328. static bool MSH3_CALL msh3_data_received(MSH3_REQUEST *Request,
  329. void *IfContext, uint32_t *buflen,
  330. const uint8_t *buf)
  331. {
  332. struct Curl_easy *data = IfContext;
  333. struct stream_ctx *stream = H3_STREAM_CTX(data);
  334. CURLcode result;
  335. bool rv = FALSE;
  336. /* TODO: we would like to limit the amount of data we are buffer here.
  337. * There seems to be no mechanism in msh3 to adjust flow control and
  338. * it is undocumented what happens if we return FALSE here or less
  339. * length (buflen is an inout parameter).
  340. */
  341. (void)Request;
  342. if(!stream)
  343. return FALSE;
  344. msh3_lock_acquire(&stream->recv_lock);
  345. if(!stream->recv_header_complete) {
  346. result = write_resp_raw(data, "\r\n", 2);
  347. if(result) {
  348. stream->recv_error = result;
  349. goto out;
  350. }
  351. stream->recv_header_complete = true;
  352. }
  353. result = write_resp_raw(data, buf, *buflen);
  354. if(result) {
  355. stream->recv_error = result;
  356. }
  357. rv = TRUE;
  358. out:
  359. msh3_lock_release(&stream->recv_lock);
  360. return rv;
  361. }
  362. static void MSH3_CALL msh3_complete(MSH3_REQUEST *Request, void *IfContext,
  363. bool aborted, uint64_t error)
  364. {
  365. struct Curl_easy *data = IfContext;
  366. struct stream_ctx *stream = H3_STREAM_CTX(data);
  367. (void)Request;
  368. if(!stream)
  369. return;
  370. msh3_lock_acquire(&stream->recv_lock);
  371. stream->closed = TRUE;
  372. stream->recv_header_complete = true;
  373. if(error)
  374. stream->error3 = error;
  375. if(aborted)
  376. stream->reset = TRUE;
  377. msh3_lock_release(&stream->recv_lock);
  378. }
  379. static void MSH3_CALL msh3_shutdown_complete(MSH3_REQUEST *Request,
  380. void *IfContext)
  381. {
  382. struct Curl_easy *data = IfContext;
  383. struct stream_ctx *stream = H3_STREAM_CTX(data);
  384. if(!stream)
  385. return;
  386. (void)Request;
  387. (void)stream;
  388. }
  389. static void MSH3_CALL msh3_data_sent(MSH3_REQUEST *Request,
  390. void *IfContext, void *SendContext)
  391. {
  392. struct Curl_easy *data = IfContext;
  393. struct stream_ctx *stream = H3_STREAM_CTX(data);
  394. if(!stream)
  395. return;
  396. (void)Request;
  397. (void)stream;
  398. (void)SendContext;
  399. }
  400. static ssize_t recv_closed_stream(struct Curl_cfilter *cf,
  401. struct Curl_easy *data,
  402. CURLcode *err)
  403. {
  404. struct stream_ctx *stream = H3_STREAM_CTX(data);
  405. ssize_t nread = -1;
  406. if(!stream) {
  407. *err = CURLE_RECV_ERROR;
  408. return -1;
  409. }
  410. (void)cf;
  411. if(stream->reset) {
  412. failf(data, "HTTP/3 stream reset by server");
  413. *err = CURLE_PARTIAL_FILE;
  414. DEBUGF(LOG_CF(data, cf, "cf_recv, was reset -> %d", *err));
  415. goto out;
  416. }
  417. else if(stream->error3) {
  418. failf(data, "HTTP/3 stream was not closed cleanly: (error %zd)",
  419. (ssize_t)stream->error3);
  420. *err = CURLE_HTTP3;
  421. DEBUGF(LOG_CF(data, cf, "cf_recv, closed uncleanly -> %d", *err));
  422. goto out;
  423. }
  424. else {
  425. DEBUGF(LOG_CF(data, cf, "cf_recv, closed ok -> %d", *err));
  426. }
  427. *err = CURLE_OK;
  428. nread = 0;
  429. out:
  430. return nread;
  431. }
  432. static void set_quic_expire(struct Curl_cfilter *cf, struct Curl_easy *data)
  433. {
  434. struct stream_ctx *stream = H3_STREAM_CTX(data);
  435. /* we have no indication from msh3 when it would be a good time
  436. * to juggle the connection again. So, we compromise by calling
  437. * us again every some milliseconds. */
  438. (void)cf;
  439. if(stream && stream->req && !stream->closed) {
  440. Curl_expire(data, 10, EXPIRE_QUIC);
  441. }
  442. else {
  443. Curl_expire(data, 50, EXPIRE_QUIC);
  444. }
  445. }
  446. static ssize_t cf_msh3_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  447. char *buf, size_t len, CURLcode *err)
  448. {
  449. struct stream_ctx *stream = H3_STREAM_CTX(data);
  450. ssize_t nread = -1;
  451. struct cf_call_data save;
  452. (void)cf;
  453. if(!stream) {
  454. *err = CURLE_RECV_ERROR;
  455. return -1;
  456. }
  457. CF_DATA_SAVE(save, cf, data);
  458. DEBUGF(LOG_CF(data, cf, "req: recv with %zu byte buffer", len));
  459. msh3_lock_acquire(&stream->recv_lock);
  460. if(stream->recv_error) {
  461. failf(data, "request aborted");
  462. *err = stream->recv_error;
  463. goto out;
  464. }
  465. *err = CURLE_OK;
  466. if(!Curl_bufq_is_empty(&stream->recvbuf)) {
  467. nread = Curl_bufq_read(&stream->recvbuf,
  468. (unsigned char *)buf, len, err);
  469. DEBUGF(LOG_CF(data, cf, "read recvbuf(len=%zu) -> %zd, %d",
  470. len, nread, *err));
  471. if(nread < 0)
  472. goto out;
  473. if(stream->closed)
  474. drain_stream(cf, data);
  475. }
  476. else if(stream->closed) {
  477. nread = recv_closed_stream(cf, data, err);
  478. goto out;
  479. }
  480. else {
  481. DEBUGF(LOG_CF(data, cf, "req: nothing here, call again"));
  482. *err = CURLE_AGAIN;
  483. }
  484. out:
  485. msh3_lock_release(&stream->recv_lock);
  486. set_quic_expire(cf, data);
  487. CF_DATA_RESTORE(cf, save);
  488. return nread;
  489. }
  490. static ssize_t cf_msh3_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  491. const void *buf, size_t len, CURLcode *err)
  492. {
  493. struct cf_msh3_ctx *ctx = cf->ctx;
  494. struct stream_ctx *stream = H3_STREAM_CTX(data);
  495. struct h1_req_parser h1;
  496. struct dynhds h2_headers;
  497. MSH3_HEADER *nva = NULL;
  498. size_t nheader, i;
  499. ssize_t nwritten = -1;
  500. struct cf_call_data save;
  501. bool eos;
  502. CF_DATA_SAVE(save, cf, data);
  503. Curl_h1_req_parse_init(&h1, H1_PARSE_DEFAULT_MAX_LINE_LEN);
  504. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  505. /* Sizes must match for cast below to work" */
  506. DEBUGASSERT(stream);
  507. DEBUGF(LOG_CF(data, cf, "req: send %zu bytes", len));
  508. if(!stream->req) {
  509. /* The first send on the request contains the headers and possibly some
  510. data. Parse out the headers and create the request, then if there is
  511. any data left over go ahead and send it too. */
  512. nwritten = Curl_h1_req_parse_read(&h1, buf, len, NULL, 0, err);
  513. if(nwritten < 0)
  514. goto out;
  515. DEBUGASSERT(h1.done);
  516. DEBUGASSERT(h1.req);
  517. *err = Curl_http_req_to_h2(&h2_headers, h1.req, data);
  518. if(*err) {
  519. nwritten = -1;
  520. goto out;
  521. }
  522. nheader = Curl_dynhds_count(&h2_headers);
  523. nva = malloc(sizeof(MSH3_HEADER) * nheader);
  524. if(!nva) {
  525. *err = CURLE_OUT_OF_MEMORY;
  526. nwritten = -1;
  527. goto out;
  528. }
  529. for(i = 0; i < nheader; ++i) {
  530. struct dynhds_entry *e = Curl_dynhds_getn(&h2_headers, i);
  531. nva[i].Name = e->name;
  532. nva[i].NameLength = e->namelen;
  533. nva[i].Value = e->value;
  534. nva[i].ValueLength = e->valuelen;
  535. }
  536. switch(data->state.httpreq) {
  537. case HTTPREQ_POST:
  538. case HTTPREQ_POST_FORM:
  539. case HTTPREQ_POST_MIME:
  540. case HTTPREQ_PUT:
  541. /* known request body size or -1 */
  542. eos = FALSE;
  543. break;
  544. default:
  545. /* there is not request body */
  546. eos = TRUE;
  547. stream->upload_done = TRUE;
  548. break;
  549. }
  550. DEBUGF(LOG_CF(data, cf, "req: send %zu headers", nheader));
  551. stream->req = MsH3RequestOpen(ctx->qconn, &msh3_request_if, data,
  552. nva, nheader,
  553. eos ? MSH3_REQUEST_FLAG_FIN :
  554. MSH3_REQUEST_FLAG_NONE);
  555. if(!stream->req) {
  556. failf(data, "request open failed");
  557. *err = CURLE_SEND_ERROR;
  558. goto out;
  559. }
  560. *err = CURLE_OK;
  561. nwritten = len;
  562. goto out;
  563. }
  564. else {
  565. /* request is open */
  566. DEBUGF(LOG_CF(data, cf, "req: send %zu body bytes", len));
  567. if(len > 0xFFFFFFFF) {
  568. len = 0xFFFFFFFF;
  569. }
  570. if(!MsH3RequestSend(stream->req, MSH3_REQUEST_FLAG_NONE, buf,
  571. (uint32_t)len, stream)) {
  572. *err = CURLE_SEND_ERROR;
  573. goto out;
  574. }
  575. /* TODO - msh3/msquic will hold onto this memory until the send complete
  576. event. How do we make sure curl doesn't free it until then? */
  577. *err = CURLE_OK;
  578. nwritten = len;
  579. }
  580. out:
  581. set_quic_expire(cf, data);
  582. free(nva);
  583. Curl_h1_req_parse_free(&h1);
  584. Curl_dynhds_free(&h2_headers);
  585. CF_DATA_RESTORE(cf, save);
  586. return nwritten;
  587. }
  588. static int cf_msh3_get_select_socks(struct Curl_cfilter *cf,
  589. struct Curl_easy *data,
  590. curl_socket_t *socks)
  591. {
  592. struct cf_msh3_ctx *ctx = cf->ctx;
  593. struct stream_ctx *stream = H3_STREAM_CTX(data);
  594. int bitmap = GETSOCK_BLANK;
  595. struct cf_call_data save;
  596. CF_DATA_SAVE(save, cf, data);
  597. if(stream && ctx->sock[SP_LOCAL] != CURL_SOCKET_BAD) {
  598. socks[0] = ctx->sock[SP_LOCAL];
  599. if(stream->recv_error) {
  600. bitmap |= GETSOCK_READSOCK(0);
  601. drain_stream(cf, data);
  602. }
  603. else if(stream->req) {
  604. bitmap |= GETSOCK_READSOCK(0);
  605. drain_stream(cf, data);
  606. }
  607. }
  608. DEBUGF(LOG_CF(data, cf, "select_sock -> %d", bitmap));
  609. CF_DATA_RESTORE(cf, save);
  610. return bitmap;
  611. }
  612. static bool cf_msh3_data_pending(struct Curl_cfilter *cf,
  613. const struct Curl_easy *data)
  614. {
  615. struct stream_ctx *stream = H3_STREAM_CTX(data);
  616. struct cf_call_data save;
  617. bool pending = FALSE;
  618. CF_DATA_SAVE(save, cf, data);
  619. (void)cf;
  620. if(stream && stream->req) {
  621. msh3_lock_acquire(&stream->recv_lock);
  622. DEBUGF(LOG_CF((struct Curl_easy *)data, cf, "data pending = %zu",
  623. Curl_bufq_len(&stream->recvbuf)));
  624. pending = !Curl_bufq_is_empty(&stream->recvbuf);
  625. msh3_lock_release(&stream->recv_lock);
  626. if(pending)
  627. drain_stream(cf, (struct Curl_easy *)data);
  628. }
  629. CF_DATA_RESTORE(cf, save);
  630. return pending;
  631. }
  632. static void cf_msh3_active(struct Curl_cfilter *cf, struct Curl_easy *data)
  633. {
  634. struct cf_msh3_ctx *ctx = cf->ctx;
  635. /* use this socket from now on */
  636. cf->conn->sock[cf->sockindex] = ctx->sock[SP_LOCAL];
  637. /* the first socket info gets set at conn and data */
  638. if(cf->sockindex == FIRSTSOCKET) {
  639. cf->conn->remote_addr = &ctx->addr;
  640. #ifdef ENABLE_IPV6
  641. cf->conn->bits.ipv6 = (ctx->addr.family == AF_INET6)? TRUE : FALSE;
  642. #endif
  643. Curl_persistconninfo(data, cf->conn, ctx->l_ip, ctx->l_port);
  644. }
  645. ctx->active = TRUE;
  646. }
  647. static CURLcode h3_data_pause(struct Curl_cfilter *cf,
  648. struct Curl_easy *data,
  649. bool pause)
  650. {
  651. if(!pause) {
  652. drain_stream(cf, data);
  653. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  654. }
  655. return CURLE_OK;
  656. }
  657. static CURLcode cf_msh3_data_event(struct Curl_cfilter *cf,
  658. struct Curl_easy *data,
  659. int event, int arg1, void *arg2)
  660. {
  661. struct stream_ctx *stream = H3_STREAM_CTX(data);
  662. struct cf_call_data save;
  663. CURLcode result = CURLE_OK;
  664. CF_DATA_SAVE(save, cf, data);
  665. (void)arg1;
  666. (void)arg2;
  667. switch(event) {
  668. case CF_CTRL_DATA_SETUP:
  669. result = h3_data_setup(cf, data);
  670. break;
  671. case CF_CTRL_DATA_PAUSE:
  672. result = h3_data_pause(cf, data, (arg1 != 0));
  673. break;
  674. case CF_CTRL_DATA_DONE:
  675. h3_data_done(cf, data);
  676. break;
  677. case CF_CTRL_DATA_DONE_SEND:
  678. DEBUGF(LOG_CF(data, cf, "req: send done"));
  679. if(stream) {
  680. stream->upload_done = TRUE;
  681. if(stream->req) {
  682. char buf[1];
  683. if(!MsH3RequestSend(stream->req, MSH3_REQUEST_FLAG_FIN,
  684. buf, 0, data)) {
  685. result = CURLE_SEND_ERROR;
  686. }
  687. }
  688. }
  689. break;
  690. case CF_CTRL_CONN_INFO_UPDATE:
  691. DEBUGF(LOG_CF(data, cf, "req: update info"));
  692. cf_msh3_active(cf, data);
  693. break;
  694. default:
  695. break;
  696. }
  697. CF_DATA_RESTORE(cf, save);
  698. return result;
  699. }
  700. static CURLcode cf_connect_start(struct Curl_cfilter *cf,
  701. struct Curl_easy *data)
  702. {
  703. struct cf_msh3_ctx *ctx = cf->ctx;
  704. bool verify = !!cf->conn->ssl_config.verifypeer;
  705. MSH3_ADDR addr = {0};
  706. CURLcode result;
  707. memcpy(&addr, &ctx->addr.sa_addr, ctx->addr.addrlen);
  708. MSH3_SET_PORT(&addr, (uint16_t)cf->conn->remote_port);
  709. if(verify && (cf->conn->ssl_config.CAfile || cf->conn->ssl_config.CApath)) {
  710. /* TODO: need a way to provide trust anchors to MSH3 */
  711. #ifdef DEBUGBUILD
  712. /* we need this for our test cases to run */
  713. DEBUGF(LOG_CF(data, cf, "non-standard CA not supported, "
  714. "switching off verifypeer in DEBUG mode"));
  715. verify = 0;
  716. #else
  717. DEBUGF(LOG_CF(data, cf, "non-standard CA not supported, "
  718. "attempting with built-in verification"));
  719. #endif
  720. }
  721. DEBUGF(LOG_CF(data, cf, "connecting to %s:%d (verify=%d)",
  722. cf->conn->host.name, (int)cf->conn->remote_port, verify));
  723. ctx->api = MsH3ApiOpen();
  724. if(!ctx->api) {
  725. failf(data, "can't create msh3 api");
  726. return CURLE_FAILED_INIT;
  727. }
  728. ctx->qconn = MsH3ConnectionOpen(ctx->api,
  729. &msh3_conn_if,
  730. cf,
  731. cf->conn->host.name,
  732. &addr,
  733. !verify);
  734. if(!ctx->qconn) {
  735. failf(data, "can't create msh3 connection");
  736. if(ctx->api) {
  737. MsH3ApiClose(ctx->api);
  738. ctx->api = NULL;
  739. }
  740. return CURLE_FAILED_INIT;
  741. }
  742. result = h3_data_setup(cf, data);
  743. if(result)
  744. return result;
  745. return CURLE_OK;
  746. }
  747. static CURLcode cf_msh3_connect(struct Curl_cfilter *cf,
  748. struct Curl_easy *data,
  749. bool blocking, bool *done)
  750. {
  751. struct cf_msh3_ctx *ctx = cf->ctx;
  752. struct cf_call_data save;
  753. CURLcode result = CURLE_OK;
  754. (void)blocking;
  755. if(cf->connected) {
  756. *done = TRUE;
  757. return CURLE_OK;
  758. }
  759. CF_DATA_SAVE(save, cf, data);
  760. if(ctx->sock[SP_LOCAL] == CURL_SOCKET_BAD) {
  761. if(Curl_socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx->sock[0]) < 0) {
  762. ctx->sock[SP_LOCAL] = CURL_SOCKET_BAD;
  763. ctx->sock[SP_REMOTE] = CURL_SOCKET_BAD;
  764. return CURLE_COULDNT_CONNECT;
  765. }
  766. }
  767. *done = FALSE;
  768. if(!ctx->qconn) {
  769. ctx->connect_started = Curl_now();
  770. result = cf_connect_start(cf, data);
  771. if(result)
  772. goto out;
  773. }
  774. if(ctx->handshake_complete) {
  775. ctx->handshake_at = Curl_now();
  776. if(ctx->handshake_succeeded) {
  777. DEBUGF(LOG_CF(data, cf, "handshake succeeded"));
  778. cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  779. cf->conn->httpversion = 30;
  780. cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  781. cf->connected = TRUE;
  782. cf->conn->alpn = CURL_HTTP_VERSION_3;
  783. *done = TRUE;
  784. connkeep(cf->conn, "HTTP/3 default");
  785. Curl_pgrsTime(data, TIMER_APPCONNECT);
  786. }
  787. else {
  788. failf(data, "failed to connect, handshake failed");
  789. result = CURLE_COULDNT_CONNECT;
  790. }
  791. }
  792. out:
  793. CF_DATA_RESTORE(cf, save);
  794. return result;
  795. }
  796. static void cf_msh3_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  797. {
  798. struct cf_msh3_ctx *ctx = cf->ctx;
  799. struct cf_call_data save;
  800. (void)data;
  801. CF_DATA_SAVE(save, cf, data);
  802. if(ctx) {
  803. DEBUGF(LOG_CF(data, cf, "destroying"));
  804. if(ctx->qconn) {
  805. MsH3ConnectionClose(ctx->qconn);
  806. ctx->qconn = NULL;
  807. }
  808. if(ctx->api) {
  809. MsH3ApiClose(ctx->api);
  810. ctx->api = NULL;
  811. }
  812. if(ctx->active) {
  813. /* We share our socket at cf->conn->sock[cf->sockindex] when active.
  814. * If it is no longer there, someone has stolen (and hopefully
  815. * closed it) and we just forget about it.
  816. */
  817. ctx->active = FALSE;
  818. if(ctx->sock[SP_LOCAL] == cf->conn->sock[cf->sockindex]) {
  819. DEBUGF(LOG_CF(data, cf, "cf_msh3_close(%d) active",
  820. (int)ctx->sock[SP_LOCAL]));
  821. cf->conn->sock[cf->sockindex] = CURL_SOCKET_BAD;
  822. }
  823. else {
  824. DEBUGF(LOG_CF(data, cf, "cf_socket_close(%d) no longer at "
  825. "conn->sock[], discarding", (int)ctx->sock[SP_LOCAL]));
  826. ctx->sock[SP_LOCAL] = CURL_SOCKET_BAD;
  827. }
  828. if(cf->sockindex == FIRSTSOCKET)
  829. cf->conn->remote_addr = NULL;
  830. }
  831. if(ctx->sock[SP_LOCAL] != CURL_SOCKET_BAD) {
  832. sclose(ctx->sock[SP_LOCAL]);
  833. }
  834. if(ctx->sock[SP_REMOTE] != CURL_SOCKET_BAD) {
  835. sclose(ctx->sock[SP_REMOTE]);
  836. }
  837. ctx->sock[SP_LOCAL] = CURL_SOCKET_BAD;
  838. ctx->sock[SP_REMOTE] = CURL_SOCKET_BAD;
  839. }
  840. CF_DATA_RESTORE(cf, save);
  841. }
  842. static void cf_msh3_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  843. {
  844. struct cf_call_data save;
  845. CF_DATA_SAVE(save, cf, data);
  846. cf_msh3_close(cf, data);
  847. free(cf->ctx);
  848. cf->ctx = NULL;
  849. /* no CF_DATA_RESTORE(cf, save); its gone */
  850. }
  851. static CURLcode cf_msh3_query(struct Curl_cfilter *cf,
  852. struct Curl_easy *data,
  853. int query, int *pres1, void *pres2)
  854. {
  855. struct cf_msh3_ctx *ctx = cf->ctx;
  856. switch(query) {
  857. case CF_QUERY_MAX_CONCURRENT: {
  858. /* TODO: we do not have access to this so far, fake it */
  859. (void)ctx;
  860. *pres1 = 100;
  861. return CURLE_OK;
  862. }
  863. case CF_QUERY_TIMER_CONNECT: {
  864. struct curltime *when = pres2;
  865. /* we do not know when the first byte arrived */
  866. if(cf->connected)
  867. *when = ctx->handshake_at;
  868. return CURLE_OK;
  869. }
  870. case CF_QUERY_TIMER_APPCONNECT: {
  871. struct curltime *when = pres2;
  872. if(cf->connected)
  873. *when = ctx->handshake_at;
  874. return CURLE_OK;
  875. }
  876. default:
  877. break;
  878. }
  879. return cf->next?
  880. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  881. CURLE_UNKNOWN_OPTION;
  882. }
  883. static bool cf_msh3_conn_is_alive(struct Curl_cfilter *cf,
  884. struct Curl_easy *data,
  885. bool *input_pending)
  886. {
  887. struct cf_msh3_ctx *ctx = cf->ctx;
  888. (void)data;
  889. *input_pending = FALSE;
  890. return ctx && ctx->sock[SP_LOCAL] != CURL_SOCKET_BAD && ctx->qconn &&
  891. ctx->connected;
  892. }
  893. struct Curl_cftype Curl_cft_http3 = {
  894. "HTTP/3",
  895. CF_TYPE_IP_CONNECT | CF_TYPE_SSL | CF_TYPE_MULTIPLEX,
  896. 0,
  897. cf_msh3_destroy,
  898. cf_msh3_connect,
  899. cf_msh3_close,
  900. Curl_cf_def_get_host,
  901. cf_msh3_get_select_socks,
  902. cf_msh3_data_pending,
  903. cf_msh3_send,
  904. cf_msh3_recv,
  905. cf_msh3_data_event,
  906. cf_msh3_conn_is_alive,
  907. Curl_cf_def_conn_keep_alive,
  908. cf_msh3_query,
  909. };
  910. CURLcode Curl_cf_msh3_create(struct Curl_cfilter **pcf,
  911. struct Curl_easy *data,
  912. struct connectdata *conn,
  913. const struct Curl_addrinfo *ai)
  914. {
  915. struct cf_msh3_ctx *ctx = NULL;
  916. struct Curl_cfilter *cf = NULL;
  917. CURLcode result;
  918. (void)data;
  919. (void)conn;
  920. (void)ai; /* TODO: msh3 resolves itself? */
  921. ctx = calloc(sizeof(*ctx), 1);
  922. if(!ctx) {
  923. result = CURLE_OUT_OF_MEMORY;
  924. goto out;
  925. }
  926. Curl_sock_assign_addr(&ctx->addr, ai, TRNSPRT_QUIC);
  927. ctx->sock[SP_LOCAL] = CURL_SOCKET_BAD;
  928. ctx->sock[SP_REMOTE] = CURL_SOCKET_BAD;
  929. result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
  930. out:
  931. *pcf = (!result)? cf : NULL;
  932. if(result) {
  933. Curl_safefree(cf);
  934. Curl_safefree(ctx);
  935. }
  936. return result;
  937. }
  938. bool Curl_conn_is_msh3(const struct Curl_easy *data,
  939. const struct connectdata *conn,
  940. int sockindex)
  941. {
  942. struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
  943. (void)data;
  944. for(; cf; cf = cf->next) {
  945. if(cf->cft == &Curl_cft_http3)
  946. return TRUE;
  947. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  948. return FALSE;
  949. }
  950. return FALSE;
  951. }
  952. #endif /* USE_MSH3 */