cfilters.c 24 KB

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