cfilters.c 28 KB

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