cfilters.c 28 KB

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