curl_msh3.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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]) < 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_get_host,
  912. cf_msh3_adjust_pollset,
  913. cf_msh3_data_pending,
  914. cf_msh3_send,
  915. cf_msh3_recv,
  916. cf_msh3_data_event,
  917. cf_msh3_conn_is_alive,
  918. Curl_cf_def_conn_keep_alive,
  919. cf_msh3_query,
  920. };
  921. static struct cf_msh3_ctx *h3_get_msh3_ctx(struct Curl_easy *data)
  922. {
  923. if(data && data->conn) {
  924. struct Curl_cfilter *cf = data->conn->cfilter[FIRSTSOCKET];
  925. while(cf) {
  926. if(cf->cft == &Curl_cft_http3)
  927. return cf->ctx;
  928. cf = cf->next;
  929. }
  930. }
  931. DEBUGF(infof(data, "no filter context found"));
  932. return NULL;
  933. }
  934. CURLcode Curl_cf_msh3_create(struct Curl_cfilter **pcf,
  935. struct Curl_easy *data,
  936. struct connectdata *conn,
  937. const struct Curl_addrinfo *ai)
  938. {
  939. struct cf_msh3_ctx *ctx = NULL;
  940. struct Curl_cfilter *cf = NULL;
  941. CURLcode result;
  942. (void)data;
  943. (void)conn;
  944. (void)ai; /* TODO: msh3 resolves itself? */
  945. ctx = calloc(1, sizeof(*ctx));
  946. if(!ctx) {
  947. result = CURLE_OUT_OF_MEMORY;
  948. goto out;
  949. }
  950. Curl_sock_assign_addr(&ctx->addr, ai, TRNSPRT_QUIC);
  951. ctx->sock[SP_LOCAL] = CURL_SOCKET_BAD;
  952. ctx->sock[SP_REMOTE] = CURL_SOCKET_BAD;
  953. result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
  954. out:
  955. *pcf = (!result)? cf : NULL;
  956. if(result) {
  957. Curl_safefree(cf);
  958. Curl_safefree(ctx);
  959. }
  960. return result;
  961. }
  962. bool Curl_conn_is_msh3(const struct Curl_easy *data,
  963. const struct connectdata *conn,
  964. int sockindex)
  965. {
  966. struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
  967. (void)data;
  968. for(; cf; cf = cf->next) {
  969. if(cf->cft == &Curl_cft_http3)
  970. return TRUE;
  971. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  972. return FALSE;
  973. }
  974. return FALSE;
  975. }
  976. #endif /* USE_MSH3 */