curl_msh3.c 30 KB

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