cfilters.c 26 KB

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