cfilters.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. #include "urldata.h"
  26. #include "strerror.h"
  27. #include "cfilters.h"
  28. #include "connect.h"
  29. #include "url.h" /* for Curl_safefree() */
  30. #include "sendf.h"
  31. #include "sockaddr.h" /* required for Curl_sockaddr_storage */
  32. #include "multiif.h"
  33. #include "progress.h"
  34. #include "select.h"
  35. #include "warnless.h"
  36. /* The last 3 #include files should be in this order */
  37. #include "curl_printf.h"
  38. #include "curl_memory.h"
  39. #include "memdebug.h"
  40. #ifndef ARRAYSIZE
  41. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  42. #endif
  43. #ifdef UNITTESTS
  44. /* used by unit2600.c */
  45. void Curl_cf_def_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  46. {
  47. cf->connected = FALSE;
  48. if(cf->next)
  49. cf->next->cft->do_close(cf->next, data);
  50. }
  51. #endif
  52. CURLcode Curl_cf_def_shutdown(struct Curl_cfilter *cf,
  53. struct Curl_easy *data, bool *done)
  54. {
  55. (void)cf;
  56. (void)data;
  57. *done = TRUE;
  58. return CURLE_OK;
  59. }
  60. static void conn_report_connect_stats(struct Curl_easy *data,
  61. struct connectdata *conn);
  62. void Curl_cf_def_get_host(struct Curl_cfilter *cf, struct Curl_easy *data,
  63. const char **phost, const char **pdisplay_host,
  64. int *pport)
  65. {
  66. if(cf->next)
  67. cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
  68. else {
  69. *phost = cf->conn->host.name;
  70. *pdisplay_host = cf->conn->host.dispname;
  71. *pport = cf->conn->primary.remote_port;
  72. }
  73. }
  74. void Curl_cf_def_adjust_pollset(struct Curl_cfilter *cf,
  75. struct Curl_easy *data,
  76. struct easy_pollset *ps)
  77. {
  78. /* NOP */
  79. (void)cf;
  80. (void)data;
  81. (void)ps;
  82. }
  83. bool Curl_cf_def_data_pending(struct Curl_cfilter *cf,
  84. const struct Curl_easy *data)
  85. {
  86. return cf->next?
  87. cf->next->cft->has_data_pending(cf->next, data) : FALSE;
  88. }
  89. ssize_t Curl_cf_def_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  90. const void *buf, size_t len, CURLcode *err)
  91. {
  92. return cf->next?
  93. cf->next->cft->do_send(cf->next, data, buf, len, err) :
  94. CURLE_RECV_ERROR;
  95. }
  96. ssize_t Curl_cf_def_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  97. char *buf, size_t len, CURLcode *err)
  98. {
  99. return cf->next?
  100. cf->next->cft->do_recv(cf->next, data, buf, len, err) :
  101. CURLE_SEND_ERROR;
  102. }
  103. bool Curl_cf_def_conn_is_alive(struct Curl_cfilter *cf,
  104. struct Curl_easy *data,
  105. bool *input_pending)
  106. {
  107. return cf->next?
  108. cf->next->cft->is_alive(cf->next, data, input_pending) :
  109. FALSE; /* pessimistic in absence of data */
  110. }
  111. CURLcode Curl_cf_def_conn_keep_alive(struct Curl_cfilter *cf,
  112. struct Curl_easy *data)
  113. {
  114. return cf->next?
  115. cf->next->cft->keep_alive(cf->next, data) :
  116. CURLE_OK;
  117. }
  118. CURLcode Curl_cf_def_query(struct Curl_cfilter *cf,
  119. struct Curl_easy *data,
  120. int query, int *pres1, void *pres2)
  121. {
  122. return cf->next?
  123. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  124. CURLE_UNKNOWN_OPTION;
  125. }
  126. void Curl_conn_cf_discard_chain(struct Curl_cfilter **pcf,
  127. struct Curl_easy *data)
  128. {
  129. struct Curl_cfilter *cfn, *cf = *pcf;
  130. if(cf) {
  131. *pcf = NULL;
  132. while(cf) {
  133. cfn = cf->next;
  134. /* prevent destroying filter to mess with its sub-chain, since
  135. * we have the reference now and will call destroy on it.
  136. */
  137. cf->next = NULL;
  138. cf->cft->destroy(cf, data);
  139. free(cf);
  140. cf = cfn;
  141. }
  142. }
  143. }
  144. void Curl_conn_cf_discard_all(struct Curl_easy *data,
  145. struct connectdata *conn, int index)
  146. {
  147. Curl_conn_cf_discard_chain(&conn->cfilter[index], data);
  148. }
  149. void Curl_conn_close(struct Curl_easy *data, int index)
  150. {
  151. struct Curl_cfilter *cf;
  152. DEBUGASSERT(data->conn);
  153. /* it is valid to call that without filters being present */
  154. cf = data->conn->cfilter[index];
  155. if(cf) {
  156. cf->cft->do_close(cf, data);
  157. }
  158. Curl_shutdown_clear(data, index);
  159. }
  160. CURLcode Curl_conn_shutdown(struct Curl_easy *data, int sockindex, bool *done)
  161. {
  162. struct Curl_cfilter *cf;
  163. CURLcode result = CURLE_OK;
  164. timediff_t timeout_ms;
  165. struct curltime now;
  166. DEBUGASSERT(data->conn);
  167. /* Get the first connected filter that is not shut down already. */
  168. cf = data->conn->cfilter[sockindex];
  169. while(cf && (!cf->connected || cf->shutdown))
  170. cf = cf->next;
  171. if(!cf) {
  172. *done = TRUE;
  173. return CURLE_OK;
  174. }
  175. *done = FALSE;
  176. now = Curl_now();
  177. if(!Curl_shutdown_started(data, sockindex)) {
  178. DEBUGF(infof(data, "shutdown start on%s connection",
  179. sockindex? " secondary" : ""));
  180. Curl_shutdown_start(data, sockindex, &now);
  181. }
  182. else {
  183. timeout_ms = Curl_shutdown_timeleft(data->conn, sockindex, &now);
  184. if(timeout_ms < 0) {
  185. failf(data, "SSL shutdown timeout");
  186. return CURLE_OPERATION_TIMEDOUT;
  187. }
  188. }
  189. while(cf) {
  190. if(!cf->shutdown) {
  191. bool cfdone = FALSE;
  192. result = cf->cft->do_shutdown(cf, data, &cfdone);
  193. if(result) {
  194. CURL_TRC_CF(data, cf, "shut down failed with %d", result);
  195. return result;
  196. }
  197. else if(!cfdone) {
  198. CURL_TRC_CF(data, cf, "shut down not done yet");
  199. return CURLE_OK;
  200. }
  201. CURL_TRC_CF(data, cf, "shut down successfully");
  202. cf->shutdown = TRUE;
  203. }
  204. cf = cf->next;
  205. }
  206. *done = (!result);
  207. return result;
  208. }
  209. ssize_t Curl_cf_recv(struct Curl_easy *data, int num, char *buf,
  210. size_t len, CURLcode *code)
  211. {
  212. struct Curl_cfilter *cf;
  213. DEBUGASSERT(data);
  214. DEBUGASSERT(data->conn);
  215. *code = CURLE_OK;
  216. cf = data->conn->cfilter[num];
  217. while(cf && !cf->connected) {
  218. cf = cf->next;
  219. }
  220. if(cf) {
  221. ssize_t nread = cf->cft->do_recv(cf, data, buf, len, code);
  222. DEBUGASSERT(nread >= 0 || *code);
  223. DEBUGASSERT(nread < 0 || !*code);
  224. return nread;
  225. }
  226. failf(data, "recv: no filter connected");
  227. *code = CURLE_FAILED_INIT;
  228. return -1;
  229. }
  230. ssize_t Curl_cf_send(struct Curl_easy *data, int num,
  231. const void *mem, size_t len, CURLcode *code)
  232. {
  233. struct Curl_cfilter *cf;
  234. DEBUGASSERT(data);
  235. DEBUGASSERT(data->conn);
  236. *code = CURLE_OK;
  237. cf = data->conn->cfilter[num];
  238. while(cf && !cf->connected) {
  239. cf = cf->next;
  240. }
  241. if(cf) {
  242. ssize_t nwritten = cf->cft->do_send(cf, data, mem, len, code);
  243. DEBUGASSERT(nwritten >= 0 || *code);
  244. DEBUGASSERT(nwritten < 0 || !*code || !len);
  245. return nwritten;
  246. }
  247. failf(data, "send: no filter connected");
  248. DEBUGASSERT(0);
  249. *code = CURLE_FAILED_INIT;
  250. return -1;
  251. }
  252. CURLcode Curl_cf_create(struct Curl_cfilter **pcf,
  253. const struct Curl_cftype *cft,
  254. void *ctx)
  255. {
  256. struct Curl_cfilter *cf;
  257. CURLcode result = CURLE_OUT_OF_MEMORY;
  258. DEBUGASSERT(cft);
  259. cf = calloc(1, sizeof(*cf));
  260. if(!cf)
  261. goto out;
  262. cf->cft = cft;
  263. cf->ctx = ctx;
  264. result = CURLE_OK;
  265. out:
  266. *pcf = cf;
  267. return result;
  268. }
  269. void Curl_conn_cf_add(struct Curl_easy *data,
  270. struct connectdata *conn,
  271. int index,
  272. struct Curl_cfilter *cf)
  273. {
  274. (void)data;
  275. DEBUGASSERT(conn);
  276. DEBUGASSERT(!cf->conn);
  277. DEBUGASSERT(!cf->next);
  278. cf->next = conn->cfilter[index];
  279. cf->conn = conn;
  280. cf->sockindex = index;
  281. conn->cfilter[index] = cf;
  282. CURL_TRC_CF(data, cf, "added");
  283. }
  284. void Curl_conn_cf_insert_after(struct Curl_cfilter *cf_at,
  285. struct Curl_cfilter *cf_new)
  286. {
  287. struct Curl_cfilter *tail, **pnext;
  288. DEBUGASSERT(cf_at);
  289. DEBUGASSERT(cf_new);
  290. DEBUGASSERT(!cf_new->conn);
  291. tail = cf_at->next;
  292. cf_at->next = cf_new;
  293. do {
  294. cf_new->conn = cf_at->conn;
  295. cf_new->sockindex = cf_at->sockindex;
  296. pnext = &cf_new->next;
  297. cf_new = cf_new->next;
  298. } while(cf_new);
  299. *pnext = tail;
  300. }
  301. bool Curl_conn_cf_discard_sub(struct Curl_cfilter *cf,
  302. struct Curl_cfilter *discard,
  303. struct Curl_easy *data,
  304. bool destroy_always)
  305. {
  306. struct Curl_cfilter **pprev = &cf->next;
  307. bool found = FALSE;
  308. /* remove from sub-chain and destroy */
  309. DEBUGASSERT(cf);
  310. while(*pprev) {
  311. if(*pprev == cf) {
  312. *pprev = discard->next;
  313. discard->next = NULL;
  314. found = TRUE;
  315. break;
  316. }
  317. pprev = &((*pprev)->next);
  318. }
  319. if(found || destroy_always) {
  320. discard->next = NULL;
  321. discard->cft->destroy(discard, data);
  322. free(discard);
  323. }
  324. return found;
  325. }
  326. CURLcode Curl_conn_cf_connect(struct Curl_cfilter *cf,
  327. struct Curl_easy *data,
  328. bool blocking, bool *done)
  329. {
  330. if(cf)
  331. return cf->cft->do_connect(cf, data, blocking, done);
  332. return CURLE_FAILED_INIT;
  333. }
  334. void Curl_conn_cf_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  335. {
  336. if(cf)
  337. cf->cft->do_close(cf, data);
  338. }
  339. ssize_t Curl_conn_cf_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  340. const void *buf, size_t len, CURLcode *err)
  341. {
  342. if(cf)
  343. return cf->cft->do_send(cf, data, buf, len, err);
  344. *err = CURLE_SEND_ERROR;
  345. return -1;
  346. }
  347. ssize_t Curl_conn_cf_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  348. char *buf, size_t len, CURLcode *err)
  349. {
  350. if(cf)
  351. return cf->cft->do_recv(cf, data, buf, len, err);
  352. *err = CURLE_RECV_ERROR;
  353. return -1;
  354. }
  355. CURLcode Curl_conn_connect(struct Curl_easy *data,
  356. int sockindex,
  357. bool blocking,
  358. bool *done)
  359. {
  360. struct Curl_cfilter *cf;
  361. CURLcode result = CURLE_OK;
  362. DEBUGASSERT(data);
  363. DEBUGASSERT(data->conn);
  364. cf = data->conn->cfilter[sockindex];
  365. DEBUGASSERT(cf);
  366. if(!cf) {
  367. *done = FALSE;
  368. return CURLE_FAILED_INIT;
  369. }
  370. *done = cf->connected;
  371. if(!*done) {
  372. result = cf->cft->do_connect(cf, data, blocking, done);
  373. if(!result && *done) {
  374. Curl_conn_ev_update_info(data, data->conn);
  375. conn_report_connect_stats(data, data->conn);
  376. data->conn->keepalive = Curl_now();
  377. }
  378. else if(result) {
  379. conn_report_connect_stats(data, data->conn);
  380. }
  381. }
  382. return result;
  383. }
  384. bool Curl_conn_is_connected(struct connectdata *conn, int sockindex)
  385. {
  386. struct Curl_cfilter *cf;
  387. cf = conn->cfilter[sockindex];
  388. return cf && cf->connected;
  389. }
  390. bool Curl_conn_is_ip_connected(struct Curl_easy *data, int sockindex)
  391. {
  392. struct Curl_cfilter *cf;
  393. cf = data->conn->cfilter[sockindex];
  394. while(cf) {
  395. if(cf->connected)
  396. return TRUE;
  397. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  398. return FALSE;
  399. cf = cf->next;
  400. }
  401. return FALSE;
  402. }
  403. bool Curl_conn_cf_is_ssl(struct Curl_cfilter *cf)
  404. {
  405. for(; cf; cf = cf->next) {
  406. if(cf->cft->flags & CF_TYPE_SSL)
  407. return TRUE;
  408. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  409. return FALSE;
  410. }
  411. return FALSE;
  412. }
  413. bool Curl_conn_is_ssl(struct connectdata *conn, int sockindex)
  414. {
  415. return conn? Curl_conn_cf_is_ssl(conn->cfilter[sockindex]) : FALSE;
  416. }
  417. bool Curl_conn_is_multiplex(struct connectdata *conn, int sockindex)
  418. {
  419. struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
  420. for(; cf; cf = cf->next) {
  421. if(cf->cft->flags & CF_TYPE_MULTIPLEX)
  422. return TRUE;
  423. if(cf->cft->flags & CF_TYPE_IP_CONNECT
  424. || cf->cft->flags & CF_TYPE_SSL)
  425. return FALSE;
  426. }
  427. return FALSE;
  428. }
  429. bool Curl_conn_data_pending(struct Curl_easy *data, int sockindex)
  430. {
  431. struct Curl_cfilter *cf;
  432. (void)data;
  433. DEBUGASSERT(data);
  434. DEBUGASSERT(data->conn);
  435. cf = data->conn->cfilter[sockindex];
  436. while(cf && !cf->connected) {
  437. cf = cf->next;
  438. }
  439. if(cf) {
  440. return cf->cft->has_data_pending(cf, data);
  441. }
  442. return FALSE;
  443. }
  444. void Curl_conn_cf_adjust_pollset(struct Curl_cfilter *cf,
  445. struct Curl_easy *data,
  446. struct easy_pollset *ps)
  447. {
  448. /* Get the lowest not-connected filter, if there are any */
  449. while(cf && !cf->connected && cf->next && !cf->next->connected)
  450. cf = cf->next;
  451. /* Skip all filters that have already shut down */
  452. while(cf && cf->shutdown)
  453. cf = cf->next;
  454. /* From there on, give all filters a chance to adjust the pollset.
  455. * Lower filters are called later, so they may override */
  456. while(cf) {
  457. cf->cft->adjust_pollset(cf, data, ps);
  458. cf = cf->next;
  459. }
  460. }
  461. void Curl_conn_adjust_pollset(struct Curl_easy *data,
  462. struct easy_pollset *ps)
  463. {
  464. int i;
  465. DEBUGASSERT(data);
  466. DEBUGASSERT(data->conn);
  467. for(i = 0; i < 2; ++i) {
  468. Curl_conn_cf_adjust_pollset(data->conn->cfilter[i], data, ps);
  469. }
  470. }
  471. int Curl_conn_cf_poll(struct Curl_cfilter *cf,
  472. struct Curl_easy *data,
  473. timediff_t timeout_ms)
  474. {
  475. struct easy_pollset ps;
  476. struct pollfd pfds[MAX_SOCKSPEREASYHANDLE];
  477. unsigned int i, npfds = 0;
  478. DEBUGASSERT(cf);
  479. DEBUGASSERT(data);
  480. DEBUGASSERT(data->conn);
  481. memset(&ps, 0, sizeof(ps));
  482. memset(pfds, 0, sizeof(pfds));
  483. Curl_conn_cf_adjust_pollset(cf, data, &ps);
  484. DEBUGASSERT(ps.num <= MAX_SOCKSPEREASYHANDLE);
  485. for(i = 0; i < ps.num; ++i) {
  486. short events = 0;
  487. if(ps.actions[i] & CURL_POLL_IN) {
  488. events |= POLLIN;
  489. }
  490. if(ps.actions[i] & CURL_POLL_OUT) {
  491. events |= POLLOUT;
  492. }
  493. if(events) {
  494. pfds[npfds].fd = ps.sockets[i];
  495. pfds[npfds].events = events;
  496. ++npfds;
  497. }
  498. }
  499. if(!npfds)
  500. DEBUGF(infof(data, "no sockets to poll!"));
  501. return Curl_poll(pfds, npfds, timeout_ms);
  502. }
  503. void Curl_conn_get_host(struct Curl_easy *data, int sockindex,
  504. const char **phost, const char **pdisplay_host,
  505. int *pport)
  506. {
  507. struct Curl_cfilter *cf;
  508. DEBUGASSERT(data->conn);
  509. cf = data->conn->cfilter[sockindex];
  510. if(cf) {
  511. cf->cft->get_host(cf, data, phost, pdisplay_host, pport);
  512. }
  513. else {
  514. /* Some filter ask during shutdown for this, mainly for debugging
  515. * purposes. We hand out the defaults, however this is not always
  516. * accurate, as the connection might be tunneled, etc. But all that
  517. * state is already gone here. */
  518. *phost = data->conn->host.name;
  519. *pdisplay_host = data->conn->host.dispname;
  520. *pport = data->conn->remote_port;
  521. }
  522. }
  523. CURLcode Curl_cf_def_cntrl(struct Curl_cfilter *cf,
  524. struct Curl_easy *data,
  525. int event, int arg1, void *arg2)
  526. {
  527. (void)cf;
  528. (void)data;
  529. (void)event;
  530. (void)arg1;
  531. (void)arg2;
  532. return CURLE_OK;
  533. }
  534. CURLcode Curl_conn_cf_cntrl(struct Curl_cfilter *cf,
  535. struct Curl_easy *data,
  536. bool ignore_result,
  537. int event, int arg1, void *arg2)
  538. {
  539. CURLcode result = CURLE_OK;
  540. for(; cf; cf = cf->next) {
  541. if(Curl_cf_def_cntrl == cf->cft->cntrl)
  542. continue;
  543. result = cf->cft->cntrl(cf, data, event, arg1, arg2);
  544. if(!ignore_result && result)
  545. break;
  546. }
  547. return result;
  548. }
  549. curl_socket_t Curl_conn_cf_get_socket(struct Curl_cfilter *cf,
  550. struct Curl_easy *data)
  551. {
  552. curl_socket_t sock;
  553. if(cf && !cf->cft->query(cf, data, CF_QUERY_SOCKET, NULL, &sock))
  554. return sock;
  555. return CURL_SOCKET_BAD;
  556. }
  557. curl_socket_t Curl_conn_get_socket(struct Curl_easy *data, int sockindex)
  558. {
  559. struct Curl_cfilter *cf;
  560. cf = data->conn? data->conn->cfilter[sockindex] : NULL;
  561. /* if the top filter has not connected, ask it (and its sub-filters)
  562. * for the socket. Otherwise conn->sock[sockindex] should have it.
  563. */
  564. if(cf && !cf->connected)
  565. return Curl_conn_cf_get_socket(cf, data);
  566. return data->conn? data->conn->sock[sockindex] : CURL_SOCKET_BAD;
  567. }
  568. void Curl_conn_forget_socket(struct Curl_easy *data, int sockindex)
  569. {
  570. if(data->conn) {
  571. struct Curl_cfilter *cf = data->conn->cfilter[sockindex];
  572. if(cf)
  573. (void)Curl_conn_cf_cntrl(cf, data, TRUE,
  574. CF_CTRL_FORGET_SOCKET, 0, NULL);
  575. fake_sclose(data->conn->sock[sockindex]);
  576. data->conn->sock[sockindex] = CURL_SOCKET_BAD;
  577. }
  578. }
  579. static CURLcode cf_cntrl_all(struct connectdata *conn,
  580. struct Curl_easy *data,
  581. bool ignore_result,
  582. int event, int arg1, void *arg2)
  583. {
  584. CURLcode result = CURLE_OK;
  585. size_t i;
  586. for(i = 0; i < ARRAYSIZE(conn->cfilter); ++i) {
  587. result = Curl_conn_cf_cntrl(conn->cfilter[i], data, ignore_result,
  588. event, arg1, arg2);
  589. if(!ignore_result && result)
  590. break;
  591. }
  592. return result;
  593. }
  594. void Curl_conn_ev_data_attach(struct connectdata *conn,
  595. struct Curl_easy *data)
  596. {
  597. cf_cntrl_all(conn, data, TRUE, CF_CTRL_DATA_ATTACH, 0, NULL);
  598. }
  599. void Curl_conn_ev_data_detach(struct connectdata *conn,
  600. struct Curl_easy *data)
  601. {
  602. cf_cntrl_all(conn, data, TRUE, CF_CTRL_DATA_DETACH, 0, NULL);
  603. }
  604. CURLcode Curl_conn_ev_data_setup(struct Curl_easy *data)
  605. {
  606. return cf_cntrl_all(data->conn, data, FALSE,
  607. CF_CTRL_DATA_SETUP, 0, NULL);
  608. }
  609. CURLcode Curl_conn_ev_data_idle(struct Curl_easy *data)
  610. {
  611. return cf_cntrl_all(data->conn, data, FALSE,
  612. CF_CTRL_DATA_IDLE, 0, NULL);
  613. }
  614. /**
  615. * Notify connection filters that the transfer represented by `data`
  616. * is done with sending data (e.g. has uploaded everything).
  617. */
  618. void Curl_conn_ev_data_done_send(struct Curl_easy *data)
  619. {
  620. cf_cntrl_all(data->conn, data, TRUE, CF_CTRL_DATA_DONE_SEND, 0, NULL);
  621. }
  622. /**
  623. * Notify connection filters that the transfer represented by `data`
  624. * is finished - eventually premature, e.g. before being complete.
  625. */
  626. void Curl_conn_ev_data_done(struct Curl_easy *data, bool premature)
  627. {
  628. cf_cntrl_all(data->conn, data, TRUE, CF_CTRL_DATA_DONE, premature, NULL);
  629. }
  630. CURLcode Curl_conn_ev_data_pause(struct Curl_easy *data, bool do_pause)
  631. {
  632. return cf_cntrl_all(data->conn, data, FALSE,
  633. CF_CTRL_DATA_PAUSE, do_pause, NULL);
  634. }
  635. void Curl_conn_ev_update_info(struct Curl_easy *data,
  636. struct connectdata *conn)
  637. {
  638. cf_cntrl_all(conn, data, TRUE, CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
  639. }
  640. /**
  641. * Update connection statistics
  642. */
  643. static void conn_report_connect_stats(struct Curl_easy *data,
  644. struct connectdata *conn)
  645. {
  646. struct Curl_cfilter *cf = conn->cfilter[FIRSTSOCKET];
  647. if(cf) {
  648. struct curltime connected;
  649. struct curltime appconnected;
  650. memset(&connected, 0, sizeof(connected));
  651. cf->cft->query(cf, data, CF_QUERY_TIMER_CONNECT, NULL, &connected);
  652. if(connected.tv_sec || connected.tv_usec)
  653. Curl_pgrsTimeWas(data, TIMER_CONNECT, connected);
  654. memset(&appconnected, 0, sizeof(appconnected));
  655. cf->cft->query(cf, data, CF_QUERY_TIMER_APPCONNECT, NULL, &appconnected);
  656. if(appconnected.tv_sec || appconnected.tv_usec)
  657. Curl_pgrsTimeWas(data, TIMER_APPCONNECT, appconnected);
  658. }
  659. }
  660. bool Curl_conn_is_alive(struct Curl_easy *data, struct connectdata *conn,
  661. bool *input_pending)
  662. {
  663. struct Curl_cfilter *cf = conn->cfilter[FIRSTSOCKET];
  664. return cf && !cf->conn->bits.close &&
  665. cf->cft->is_alive(cf, data, input_pending);
  666. }
  667. CURLcode Curl_conn_keep_alive(struct Curl_easy *data,
  668. struct connectdata *conn,
  669. int sockindex)
  670. {
  671. struct Curl_cfilter *cf = conn->cfilter[sockindex];
  672. return cf? cf->cft->keep_alive(cf, data) : CURLE_OK;
  673. }
  674. size_t Curl_conn_get_max_concurrent(struct Curl_easy *data,
  675. struct connectdata *conn,
  676. int sockindex)
  677. {
  678. CURLcode result;
  679. int n = 0;
  680. struct Curl_cfilter *cf = conn->cfilter[sockindex];
  681. result = cf? cf->cft->query(cf, data, CF_QUERY_MAX_CONCURRENT,
  682. &n, NULL) : CURLE_UNKNOWN_OPTION;
  683. return (result || n <= 0)? 1 : (size_t)n;
  684. }
  685. int Curl_conn_get_stream_error(struct Curl_easy *data,
  686. struct connectdata *conn,
  687. int sockindex)
  688. {
  689. CURLcode result;
  690. int n = 0;
  691. struct Curl_cfilter *cf = conn->cfilter[sockindex];
  692. result = cf? cf->cft->query(cf, data, CF_QUERY_STREAM_ERROR,
  693. &n, NULL) : CURLE_UNKNOWN_OPTION;
  694. return (result || n < 0)? 0 : n;
  695. }
  696. int Curl_conn_sockindex(struct Curl_easy *data, curl_socket_t sockfd)
  697. {
  698. if(data && data->conn &&
  699. sockfd != CURL_SOCKET_BAD && sockfd == data->conn->sock[SECONDARYSOCKET])
  700. return SECONDARYSOCKET;
  701. return FIRSTSOCKET;
  702. }
  703. CURLcode Curl_conn_recv(struct Curl_easy *data, int sockindex,
  704. char *buf, size_t blen, ssize_t *n)
  705. {
  706. CURLcode result = CURLE_OK;
  707. ssize_t nread;
  708. DEBUGASSERT(data->conn);
  709. nread = data->conn->recv[sockindex](data, sockindex, buf, blen, &result);
  710. DEBUGASSERT(nread >= 0 || result);
  711. DEBUGASSERT(nread < 0 || !result);
  712. *n = (nread >= 0)? (size_t)nread : 0;
  713. return result;
  714. }
  715. CURLcode Curl_conn_send(struct Curl_easy *data, int sockindex,
  716. const void *buf, size_t blen,
  717. size_t *pnwritten)
  718. {
  719. ssize_t nwritten;
  720. CURLcode result = CURLE_OK;
  721. struct connectdata *conn;
  722. DEBUGASSERT(sockindex >= 0 && sockindex < 2);
  723. DEBUGASSERT(pnwritten);
  724. DEBUGASSERT(data);
  725. DEBUGASSERT(data->conn);
  726. conn = data->conn;
  727. #ifdef DEBUGBUILD
  728. {
  729. /* Allow debug builds to override this logic to force short sends
  730. */
  731. char *p = getenv("CURL_SMALLSENDS");
  732. if(p) {
  733. size_t altsize = (size_t)strtoul(p, NULL, 10);
  734. if(altsize)
  735. blen = CURLMIN(blen, altsize);
  736. }
  737. }
  738. #endif
  739. nwritten = conn->send[sockindex](data, sockindex, buf, blen, &result);
  740. DEBUGASSERT((nwritten >= 0) || result);
  741. *pnwritten = (nwritten < 0)? 0 : (size_t)nwritten;
  742. return result;
  743. }
  744. void Curl_pollset_reset(struct Curl_easy *data,
  745. struct easy_pollset *ps)
  746. {
  747. size_t i;
  748. (void)data;
  749. memset(ps, 0, sizeof(*ps));
  750. for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++)
  751. ps->sockets[i] = CURL_SOCKET_BAD;
  752. }
  753. /**
  754. *
  755. */
  756. void Curl_pollset_change(struct Curl_easy *data,
  757. struct easy_pollset *ps, curl_socket_t sock,
  758. int add_flags, int remove_flags)
  759. {
  760. unsigned int i;
  761. (void)data;
  762. DEBUGASSERT(VALID_SOCK(sock));
  763. if(!VALID_SOCK(sock))
  764. return;
  765. DEBUGASSERT(add_flags <= (CURL_POLL_IN|CURL_POLL_OUT));
  766. DEBUGASSERT(remove_flags <= (CURL_POLL_IN|CURL_POLL_OUT));
  767. DEBUGASSERT((add_flags&remove_flags) == 0); /* no overlap */
  768. for(i = 0; i < ps->num; ++i) {
  769. if(ps->sockets[i] == sock) {
  770. ps->actions[i] &= (unsigned char)(~remove_flags);
  771. ps->actions[i] |= (unsigned char)add_flags;
  772. /* all gone? remove socket */
  773. if(!ps->actions[i]) {
  774. if((i + 1) < ps->num) {
  775. memmove(&ps->sockets[i], &ps->sockets[i + 1],
  776. (ps->num - (i + 1)) * sizeof(ps->sockets[0]));
  777. memmove(&ps->actions[i], &ps->actions[i + 1],
  778. (ps->num - (i + 1)) * sizeof(ps->actions[0]));
  779. }
  780. --ps->num;
  781. }
  782. return;
  783. }
  784. }
  785. /* not present */
  786. if(add_flags) {
  787. /* Having more SOCKETS per easy handle than what is defined
  788. * is a programming error. This indicates that we need
  789. * to raise this limit, making easy_pollset larger.
  790. * Since we use this in tight loops, we do not want to make
  791. * the pollset dynamic unnecessarily.
  792. * The current maximum in practise is HTTP/3 eyeballing where
  793. * we have up to 4 sockets involved in connection setup.
  794. */
  795. DEBUGASSERT(i < MAX_SOCKSPEREASYHANDLE);
  796. if(i < MAX_SOCKSPEREASYHANDLE) {
  797. ps->sockets[i] = sock;
  798. ps->actions[i] = (unsigned char)add_flags;
  799. ps->num = i + 1;
  800. }
  801. }
  802. }
  803. void Curl_pollset_set(struct Curl_easy *data,
  804. struct easy_pollset *ps, curl_socket_t sock,
  805. bool do_in, bool do_out)
  806. {
  807. Curl_pollset_change(data, ps, sock,
  808. (do_in?CURL_POLL_IN:0)|(do_out?CURL_POLL_OUT:0),
  809. (!do_in?CURL_POLL_IN:0)|(!do_out?CURL_POLL_OUT:0));
  810. }
  811. static void ps_add(struct Curl_easy *data, struct easy_pollset *ps,
  812. int bitmap, curl_socket_t *socks)
  813. {
  814. if(bitmap) {
  815. int i;
  816. for(i = 0; i < MAX_SOCKSPEREASYHANDLE; ++i) {
  817. if(!(bitmap & GETSOCK_MASK_RW(i)) || !VALID_SOCK((socks[i]))) {
  818. break;
  819. }
  820. if(bitmap & GETSOCK_READSOCK(i)) {
  821. if(bitmap & GETSOCK_WRITESOCK(i))
  822. Curl_pollset_add_inout(data, ps, socks[i]);
  823. else
  824. /* is READ, since we checked MASK_RW above */
  825. Curl_pollset_add_in(data, ps, socks[i]);
  826. }
  827. else
  828. Curl_pollset_add_out(data, ps, socks[i]);
  829. }
  830. }
  831. }
  832. void Curl_pollset_add_socks(struct Curl_easy *data,
  833. struct easy_pollset *ps,
  834. int (*get_socks_cb)(struct Curl_easy *data,
  835. curl_socket_t *socks))
  836. {
  837. curl_socket_t socks[MAX_SOCKSPEREASYHANDLE];
  838. int bitmap;
  839. bitmap = get_socks_cb(data, socks);
  840. ps_add(data, ps, bitmap, socks);
  841. }
  842. void Curl_pollset_check(struct Curl_easy *data,
  843. struct easy_pollset *ps, curl_socket_t sock,
  844. bool *pwant_read, bool *pwant_write)
  845. {
  846. unsigned int i;
  847. (void)data;
  848. DEBUGASSERT(VALID_SOCK(sock));
  849. for(i = 0; i < ps->num; ++i) {
  850. if(ps->sockets[i] == sock) {
  851. *pwant_read = !!(ps->actions[i] & CURL_POLL_IN);
  852. *pwant_write = !!(ps->actions[i] & CURL_POLL_OUT);
  853. return;
  854. }
  855. }
  856. *pwant_read = *pwant_write = FALSE;
  857. }