2
0

curl_msh3.c 31 KB

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