conncache.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Linus Nielsen Feltzing, <linus@haxx.se>
  9. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #include <curl/curl.h>
  27. #include "urldata.h"
  28. #include "url.h"
  29. #include "cfilters.h"
  30. #include "progress.h"
  31. #include "multiif.h"
  32. #include "sendf.h"
  33. #include "conncache.h"
  34. #include "http_negotiate.h"
  35. #include "http_ntlm.h"
  36. #include "share.h"
  37. #include "sigpipe.h"
  38. #include "connect.h"
  39. #include "select.h"
  40. #include "strcase.h"
  41. /* The last 3 #include files should be in this order */
  42. #include "curl_printf.h"
  43. #include "curl_memory.h"
  44. #include "memdebug.h"
  45. #define HASHKEY_SIZE 128
  46. static void connc_discard_conn(struct conncache *connc,
  47. struct Curl_easy *last_data,
  48. struct connectdata *conn,
  49. bool aborted);
  50. static void connc_disconnect(struct Curl_easy *data,
  51. struct connectdata *conn,
  52. struct conncache *connc,
  53. bool do_shutdown);
  54. static void connc_run_conn_shutdown(struct Curl_easy *data,
  55. struct connectdata *conn,
  56. bool *done);
  57. static void connc_run_conn_shutdown_handler(struct Curl_easy *data,
  58. struct connectdata *conn);
  59. static CURLMcode connc_update_shutdown_ev(struct Curl_multi *multi,
  60. struct Curl_easy *data,
  61. struct connectdata *conn);
  62. static void connc_shutdown_all(struct conncache *connc, int timeout_ms);
  63. static CURLcode bundle_create(struct connectbundle **bundlep)
  64. {
  65. DEBUGASSERT(*bundlep == NULL);
  66. *bundlep = malloc(sizeof(struct connectbundle));
  67. if(!*bundlep)
  68. return CURLE_OUT_OF_MEMORY;
  69. (*bundlep)->num_connections = 0;
  70. (*bundlep)->multiuse = BUNDLE_UNKNOWN;
  71. Curl_llist_init(&(*bundlep)->conn_list, NULL);
  72. return CURLE_OK;
  73. }
  74. static void bundle_destroy(struct connectbundle *bundle)
  75. {
  76. free(bundle);
  77. }
  78. /* Add a connection to a bundle */
  79. static void bundle_add_conn(struct connectbundle *bundle,
  80. struct connectdata *conn)
  81. {
  82. Curl_llist_append(&bundle->conn_list, conn, &conn->bundle_node);
  83. conn->bundle = bundle;
  84. bundle->num_connections++;
  85. }
  86. /* Remove a connection from a bundle */
  87. static int bundle_remove_conn(struct connectbundle *bundle,
  88. struct connectdata *conn)
  89. {
  90. struct Curl_llist_element *curr;
  91. curr = bundle->conn_list.head;
  92. while(curr) {
  93. if(curr->ptr == conn) {
  94. Curl_llist_remove(&bundle->conn_list, curr, NULL);
  95. bundle->num_connections--;
  96. conn->bundle = NULL;
  97. return 1; /* we removed a handle */
  98. }
  99. curr = curr->next;
  100. }
  101. DEBUGASSERT(0);
  102. return 0;
  103. }
  104. static void free_bundle_hash_entry(void *freethis)
  105. {
  106. struct connectbundle *b = (struct connectbundle *) freethis;
  107. bundle_destroy(b);
  108. }
  109. int Curl_conncache_init(struct conncache *connc,
  110. struct Curl_multi *multi, size_t size)
  111. {
  112. /* allocate a new easy handle to use when closing cached connections */
  113. connc->closure_handle = curl_easy_init();
  114. if(!connc->closure_handle)
  115. return 1; /* bad */
  116. connc->closure_handle->state.internal = true;
  117. #ifdef DEBUGBUILD
  118. if(getenv("CURL_DEBUG"))
  119. connc->closure_handle->set.verbose = true;
  120. #endif
  121. Curl_hash_init(&connc->hash, size, Curl_hash_str,
  122. Curl_str_key_compare, free_bundle_hash_entry);
  123. connc->closure_handle->state.conn_cache = connc;
  124. connc->multi = multi;
  125. Curl_llist_init(&connc->shutdowns.conn_list, NULL);
  126. return 0; /* good */
  127. }
  128. void Curl_conncache_destroy(struct conncache *connc)
  129. {
  130. if(connc) {
  131. Curl_hash_destroy(&connc->hash);
  132. connc->multi = NULL;
  133. DEBUGASSERT(!Curl_llist_count(&connc->shutdowns.conn_list));
  134. }
  135. }
  136. /* creates a key to find a bundle for this connection */
  137. static void hashkey(struct connectdata *conn, char *buf, size_t len)
  138. {
  139. const char *hostname;
  140. long port = conn->remote_port;
  141. DEBUGASSERT(len >= HASHKEY_SIZE);
  142. #ifndef CURL_DISABLE_PROXY
  143. if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
  144. hostname = conn->http_proxy.host.name;
  145. port = conn->primary.remote_port;
  146. }
  147. else
  148. #endif
  149. if(conn->bits.conn_to_host)
  150. hostname = conn->conn_to_host.name;
  151. else
  152. hostname = conn->host.name;
  153. /* put the numbers first so that the hostname gets cut off if too long */
  154. #ifdef USE_IPV6
  155. msnprintf(buf, len, "%u/%ld/%s", conn->scope_id, port, hostname);
  156. #else
  157. msnprintf(buf, len, "%ld/%s", port, hostname);
  158. #endif
  159. Curl_strntolower(buf, buf, len);
  160. }
  161. /* Returns number of connections currently held in the connection cache.
  162. Locks/unlocks the cache itself!
  163. */
  164. size_t Curl_conncache_size(struct Curl_easy *data)
  165. {
  166. size_t num;
  167. CONNCACHE_LOCK(data);
  168. num = data->state.conn_cache->num_conn;
  169. CONNCACHE_UNLOCK(data);
  170. return num;
  171. }
  172. /* Look up the bundle with all the connections to the same host this
  173. connectdata struct is setup to use.
  174. **NOTE**: When it returns, it holds the connection cache lock! */
  175. struct connectbundle *
  176. Curl_conncache_find_bundle(struct Curl_easy *data,
  177. struct connectdata *conn,
  178. struct conncache *connc)
  179. {
  180. struct connectbundle *bundle = NULL;
  181. CONNCACHE_LOCK(data);
  182. if(connc) {
  183. char key[HASHKEY_SIZE];
  184. hashkey(conn, key, sizeof(key));
  185. bundle = Curl_hash_pick(&connc->hash, key, strlen(key));
  186. }
  187. return bundle;
  188. }
  189. static void *connc_add_bundle(struct conncache *connc,
  190. char *key, struct connectbundle *bundle)
  191. {
  192. return Curl_hash_add(&connc->hash, key, strlen(key), bundle);
  193. }
  194. static void connc_remove_bundle(struct conncache *connc,
  195. struct connectbundle *bundle)
  196. {
  197. struct Curl_hash_iterator iter;
  198. struct Curl_hash_element *he;
  199. if(!connc)
  200. return;
  201. Curl_hash_start_iterate(&connc->hash, &iter);
  202. he = Curl_hash_next_element(&iter);
  203. while(he) {
  204. if(he->ptr == bundle) {
  205. /* The bundle is destroyed by the hash destructor function,
  206. free_bundle_hash_entry() */
  207. Curl_hash_delete(&connc->hash, he->key, he->key_len);
  208. return;
  209. }
  210. he = Curl_hash_next_element(&iter);
  211. }
  212. }
  213. CURLcode Curl_conncache_add_conn(struct Curl_easy *data)
  214. {
  215. CURLcode result = CURLE_OK;
  216. struct connectbundle *bundle = NULL;
  217. struct connectdata *conn = data->conn;
  218. struct conncache *connc = data->state.conn_cache;
  219. DEBUGASSERT(conn);
  220. /* *find_bundle() locks the connection cache */
  221. bundle = Curl_conncache_find_bundle(data, conn, data->state.conn_cache);
  222. if(!bundle) {
  223. char key[HASHKEY_SIZE];
  224. result = bundle_create(&bundle);
  225. if(result) {
  226. goto unlock;
  227. }
  228. hashkey(conn, key, sizeof(key));
  229. if(!connc_add_bundle(data->state.conn_cache, key, bundle)) {
  230. bundle_destroy(bundle);
  231. result = CURLE_OUT_OF_MEMORY;
  232. goto unlock;
  233. }
  234. }
  235. bundle_add_conn(bundle, conn);
  236. conn->connection_id = connc->next_connection_id++;
  237. connc->num_conn++;
  238. DEBUGF(infof(data, "Added connection %" CURL_FORMAT_CURL_OFF_T ". "
  239. "The cache now contains %zu members",
  240. conn->connection_id, connc->num_conn));
  241. unlock:
  242. CONNCACHE_UNLOCK(data);
  243. return result;
  244. }
  245. static void connc_remove_conn(struct conncache *connc,
  246. struct connectdata *conn)
  247. {
  248. struct connectbundle *bundle = conn->bundle;
  249. /* The bundle pointer can be NULL, since this function can be called
  250. due to a failed connection attempt, before being added to a bundle */
  251. if(bundle) {
  252. bundle_remove_conn(bundle, conn);
  253. if(connc && bundle->num_connections == 0)
  254. connc_remove_bundle(connc, bundle);
  255. conn->bundle = NULL; /* removed from it */
  256. if(connc)
  257. connc->num_conn--;
  258. }
  259. }
  260. /*
  261. * Removes the connectdata object from the connection cache, but the transfer
  262. * still owns this connection.
  263. *
  264. * Pass TRUE/FALSE in the 'lock' argument depending on if the parent function
  265. * already holds the lock or not.
  266. */
  267. void Curl_conncache_remove_conn(struct Curl_easy *data,
  268. struct connectdata *conn, bool lock)
  269. {
  270. struct conncache *connc = data->state.conn_cache;
  271. if(lock)
  272. CONNCACHE_LOCK(data);
  273. connc_remove_conn(connc, conn);
  274. if(lock)
  275. CONNCACHE_UNLOCK(data);
  276. if(connc)
  277. DEBUGF(infof(data, "The cache now contains %zu members",
  278. connc->num_conn));
  279. }
  280. /* This function iterates the entire connection cache and calls the function
  281. func() with the connection pointer as the first argument and the supplied
  282. 'param' argument as the other.
  283. The conncache lock is still held when the callback is called. It needs it,
  284. so that it can safely continue traversing the lists once the callback
  285. returns.
  286. Returns 1 if the loop was aborted due to the callback's return code.
  287. Return 0 from func() to continue the loop, return 1 to abort it.
  288. */
  289. bool Curl_conncache_foreach(struct Curl_easy *data,
  290. struct conncache *connc,
  291. void *param,
  292. int (*func)(struct Curl_easy *data,
  293. struct connectdata *conn, void *param))
  294. {
  295. struct Curl_hash_iterator iter;
  296. struct Curl_llist_element *curr;
  297. struct Curl_hash_element *he;
  298. if(!connc)
  299. return FALSE;
  300. CONNCACHE_LOCK(data);
  301. Curl_hash_start_iterate(&connc->hash, &iter);
  302. he = Curl_hash_next_element(&iter);
  303. while(he) {
  304. struct connectbundle *bundle;
  305. bundle = he->ptr;
  306. he = Curl_hash_next_element(&iter);
  307. curr = bundle->conn_list.head;
  308. while(curr) {
  309. /* Yes, we need to update curr before calling func(), because func()
  310. might decide to remove the connection */
  311. struct connectdata *conn = curr->ptr;
  312. curr = curr->next;
  313. if(1 == func(data, conn, param)) {
  314. CONNCACHE_UNLOCK(data);
  315. return TRUE;
  316. }
  317. }
  318. }
  319. CONNCACHE_UNLOCK(data);
  320. return FALSE;
  321. }
  322. /* Return the first connection found in the cache. Used when closing all
  323. connections.
  324. NOTE: no locking is done here as this is presumably only done when cleaning
  325. up a cache!
  326. */
  327. static struct connectdata *
  328. connc_find_first_connection(struct conncache *connc)
  329. {
  330. struct Curl_hash_iterator iter;
  331. struct Curl_hash_element *he;
  332. struct connectbundle *bundle;
  333. Curl_hash_start_iterate(&connc->hash, &iter);
  334. he = Curl_hash_next_element(&iter);
  335. while(he) {
  336. struct Curl_llist_element *curr;
  337. bundle = he->ptr;
  338. curr = bundle->conn_list.head;
  339. if(curr) {
  340. return curr->ptr;
  341. }
  342. he = Curl_hash_next_element(&iter);
  343. }
  344. return NULL;
  345. }
  346. /*
  347. * Give ownership of a connection back to the connection cache. Might
  348. * disconnect the oldest existing in there to make space.
  349. *
  350. * Return TRUE if stored, FALSE if closed.
  351. */
  352. bool Curl_conncache_return_conn(struct Curl_easy *data,
  353. struct connectdata *conn)
  354. {
  355. unsigned int maxconnects = !data->multi->maxconnects ?
  356. data->multi->num_easy * 4: data->multi->maxconnects;
  357. struct connectdata *conn_candidate = NULL;
  358. conn->lastused = Curl_now(); /* it was used up until now */
  359. if(maxconnects && Curl_conncache_size(data) > maxconnects) {
  360. infof(data, "Connection cache is full, closing the oldest one");
  361. conn_candidate = Curl_conncache_extract_oldest(data);
  362. if(conn_candidate) {
  363. /* Use the closure handle for this disconnect so that anything that
  364. happens during the disconnect is not stored and associated with the
  365. 'data' handle which already just finished a transfer and it is
  366. important that details from this (unrelated) disconnect does not
  367. taint meta-data in the data handle. */
  368. struct conncache *connc = data->state.conn_cache;
  369. connc_disconnect(NULL, conn_candidate, connc, TRUE);
  370. }
  371. }
  372. return (conn_candidate == conn) ? FALSE : TRUE;
  373. }
  374. /*
  375. * This function finds the connection in the connection bundle that has been
  376. * unused for the longest time.
  377. *
  378. * Does not lock the connection cache!
  379. *
  380. * Returns the pointer to the oldest idle connection, or NULL if none was
  381. * found.
  382. */
  383. struct connectdata *
  384. Curl_conncache_extract_bundle(struct Curl_easy *data,
  385. struct connectbundle *bundle)
  386. {
  387. struct Curl_llist_element *curr;
  388. timediff_t highscore = -1;
  389. timediff_t score;
  390. struct curltime now;
  391. struct connectdata *conn_candidate = NULL;
  392. struct connectdata *conn;
  393. (void)data;
  394. now = Curl_now();
  395. curr = bundle->conn_list.head;
  396. while(curr) {
  397. conn = curr->ptr;
  398. if(!CONN_INUSE(conn)) {
  399. /* Set higher score for the age passed since the connection was used */
  400. score = Curl_timediff(now, conn->lastused);
  401. if(score > highscore) {
  402. highscore = score;
  403. conn_candidate = conn;
  404. }
  405. }
  406. curr = curr->next;
  407. }
  408. if(conn_candidate) {
  409. /* remove it to prevent another thread from nicking it */
  410. bundle_remove_conn(bundle, conn_candidate);
  411. data->state.conn_cache->num_conn--;
  412. DEBUGF(infof(data, "The cache now contains %zu members",
  413. data->state.conn_cache->num_conn));
  414. }
  415. return conn_candidate;
  416. }
  417. /*
  418. * This function finds the connection in the connection cache that has been
  419. * unused for the longest time and extracts that from the bundle.
  420. *
  421. * Returns the pointer to the connection, or NULL if none was found.
  422. */
  423. struct connectdata *
  424. Curl_conncache_extract_oldest(struct Curl_easy *data)
  425. {
  426. struct conncache *connc = data->state.conn_cache;
  427. struct Curl_hash_iterator iter;
  428. struct Curl_llist_element *curr;
  429. struct Curl_hash_element *he;
  430. timediff_t highscore =- 1;
  431. timediff_t score;
  432. struct curltime now;
  433. struct connectdata *conn_candidate = NULL;
  434. struct connectbundle *bundle;
  435. struct connectbundle *bundle_candidate = NULL;
  436. now = Curl_now();
  437. CONNCACHE_LOCK(data);
  438. Curl_hash_start_iterate(&connc->hash, &iter);
  439. he = Curl_hash_next_element(&iter);
  440. while(he) {
  441. struct connectdata *conn;
  442. bundle = he->ptr;
  443. curr = bundle->conn_list.head;
  444. while(curr) {
  445. conn = curr->ptr;
  446. if(!CONN_INUSE(conn) && !conn->bits.close &&
  447. !conn->connect_only) {
  448. /* Set higher score for the age passed since the connection was used */
  449. score = Curl_timediff(now, conn->lastused);
  450. if(score > highscore) {
  451. highscore = score;
  452. conn_candidate = conn;
  453. bundle_candidate = bundle;
  454. }
  455. }
  456. curr = curr->next;
  457. }
  458. he = Curl_hash_next_element(&iter);
  459. }
  460. if(conn_candidate) {
  461. /* remove it to prevent another thread from nicking it */
  462. bundle_remove_conn(bundle_candidate, conn_candidate);
  463. connc->num_conn--;
  464. DEBUGF(infof(data, "The cache now contains %zu members",
  465. connc->num_conn));
  466. }
  467. CONNCACHE_UNLOCK(data);
  468. return conn_candidate;
  469. }
  470. static void connc_shutdown_discard_all(struct conncache *connc)
  471. {
  472. struct Curl_llist_element *e = connc->shutdowns.conn_list.head;
  473. struct connectdata *conn;
  474. if(!e)
  475. return;
  476. DEBUGF(infof(connc->closure_handle, "conncache_shutdown_discard_all"));
  477. DEBUGASSERT(!connc->shutdowns.iter_locked);
  478. connc->shutdowns.iter_locked = TRUE;
  479. while(e) {
  480. conn = e->ptr;
  481. Curl_llist_remove(&connc->shutdowns.conn_list, e, NULL);
  482. DEBUGF(infof(connc->closure_handle, "discard connection #%"
  483. CURL_FORMAT_CURL_OFF_T, conn->connection_id));
  484. connc_disconnect(NULL, conn, connc, FALSE);
  485. e = connc->shutdowns.conn_list.head;
  486. }
  487. connc->shutdowns.iter_locked = FALSE;
  488. }
  489. static void connc_close_all(struct conncache *connc)
  490. {
  491. struct Curl_easy *data = connc->closure_handle;
  492. struct connectdata *conn;
  493. int timeout_ms = 0;
  494. SIGPIPE_VARIABLE(pipe_st);
  495. if(!data)
  496. return;
  497. /* Move all connections to the shutdown list */
  498. sigpipe_init(&pipe_st);
  499. conn = connc_find_first_connection(connc);
  500. while(conn) {
  501. connc_remove_conn(connc, conn);
  502. sigpipe_apply(data, &pipe_st);
  503. /* This will remove the connection from the cache */
  504. connclose(conn, "kill all");
  505. Curl_conncache_remove_conn(connc->closure_handle, conn, TRUE);
  506. connc_discard_conn(connc, connc->closure_handle, conn, FALSE);
  507. conn = connc_find_first_connection(connc);
  508. }
  509. /* Just for testing, run graceful shutdown */
  510. #ifdef DEBUGBUILD
  511. {
  512. char *p = getenv("CURL_GRACEFUL_SHUTDOWN");
  513. if(p) {
  514. long l = strtol(p, NULL, 10);
  515. if(l > 0 && l < INT_MAX)
  516. timeout_ms = (int)l;
  517. }
  518. }
  519. #endif
  520. connc_shutdown_all(connc, timeout_ms);
  521. /* discard all connections in the shutdown list */
  522. connc_shutdown_discard_all(connc);
  523. sigpipe_apply(data, &pipe_st);
  524. Curl_hostcache_clean(data, data->dns.hostcache);
  525. Curl_close(&data);
  526. sigpipe_restore(&pipe_st);
  527. }
  528. void Curl_conncache_close_all_connections(struct conncache *connc)
  529. {
  530. connc_close_all(connc);
  531. }
  532. static void connc_shutdown_discard_oldest(struct conncache *connc)
  533. {
  534. struct Curl_llist_element *e;
  535. struct connectdata *conn;
  536. DEBUGASSERT(!connc->shutdowns.iter_locked);
  537. if(connc->shutdowns.iter_locked)
  538. return;
  539. e = connc->shutdowns.conn_list.head;
  540. if(e) {
  541. SIGPIPE_VARIABLE(pipe_st);
  542. conn = e->ptr;
  543. Curl_llist_remove(&connc->shutdowns.conn_list, e, NULL);
  544. sigpipe_init(&pipe_st);
  545. sigpipe_apply(connc->closure_handle, &pipe_st);
  546. connc_disconnect(NULL, conn, connc, FALSE);
  547. sigpipe_restore(&pipe_st);
  548. }
  549. }
  550. static void connc_discard_conn(struct conncache *connc,
  551. struct Curl_easy *last_data,
  552. struct connectdata *conn,
  553. bool aborted)
  554. {
  555. /* `last_data`, if present, is the transfer that last worked with
  556. * the connection. It is present when the connection is being shut down
  557. * via `Curl_conncache_discard_conn()`, e.g. when the transfer failed
  558. * or does not allow connection reuse.
  559. * Using the original handle is necessary for shutting down the protocol
  560. * handler belonging to the connection. Protocols like 'file:' rely on
  561. * being invoked to clean up their allocations in the easy handle.
  562. * When a connection comes from the cache, the transfer is no longer
  563. * there and we use the cache is own closure handle.
  564. */
  565. struct Curl_easy *data = last_data? last_data : connc->closure_handle;
  566. bool done = FALSE;
  567. DEBUGASSERT(data);
  568. DEBUGASSERT(connc);
  569. DEBUGASSERT(!conn->bundle);
  570. /*
  571. * If this connection is not marked to force-close, leave it open if there
  572. * are other users of it
  573. */
  574. if(CONN_INUSE(conn) && !aborted) {
  575. DEBUGF(infof(data, "[CCACHE] not discarding #%" CURL_FORMAT_CURL_OFF_T
  576. " still in use by %zu transfers", conn->connection_id,
  577. CONN_INUSE(conn)));
  578. return;
  579. }
  580. /* treat the connection as aborted in CONNECT_ONLY situations, we do
  581. * not know what the APP did with it. */
  582. if(conn->connect_only)
  583. aborted = TRUE;
  584. conn->bits.aborted = aborted;
  585. /* We do not shutdown dead connections. The term 'dead' can be misleading
  586. * here, as we also mark errored connections/transfers as 'dead'.
  587. * If we do a shutdown for an aborted transfer, the server might think
  588. * it was successful otherwise (for example an ftps: upload). This is
  589. * not what we want. */
  590. if(aborted)
  591. done = TRUE;
  592. if(!done) {
  593. /* Attempt to shutdown the connection right away. */
  594. Curl_attach_connection(data, conn);
  595. connc_run_conn_shutdown(data, conn, &done);
  596. DEBUGF(infof(data, "[CCACHE] shutdown #%" CURL_FORMAT_CURL_OFF_T
  597. ", done=%d",conn->connection_id, done));
  598. Curl_detach_connection(data);
  599. }
  600. if(done) {
  601. connc_disconnect(data, conn, connc, FALSE);
  602. return;
  603. }
  604. DEBUGASSERT(!connc->shutdowns.iter_locked);
  605. if(connc->shutdowns.iter_locked) {
  606. DEBUGF(infof(data, "[CCACHE] discarding #%" CURL_FORMAT_CURL_OFF_T
  607. ", list locked", conn->connection_id));
  608. connc_disconnect(data, conn, connc, FALSE);
  609. return;
  610. }
  611. /* Add the connection to our shutdown list for non-blocking shutdown
  612. * during multi processing. */
  613. if(data->multi && data->multi->max_shutdown_connections > 0 &&
  614. (data->multi->max_shutdown_connections >=
  615. (long)Curl_llist_count(&connc->shutdowns.conn_list))) {
  616. DEBUGF(infof(data, "[CCACHE] discarding oldest shutdown connection "
  617. "due to limit of %ld",
  618. data->multi->max_shutdown_connections));
  619. connc_shutdown_discard_oldest(connc);
  620. }
  621. if(data->multi && data->multi->socket_cb) {
  622. DEBUGASSERT(connc == &data->multi->conn_cache);
  623. /* Start with an empty shutdown pollset, so out internal closure handle
  624. * is added to the sockets. */
  625. memset(&conn->shutdown_poll, 0, sizeof(conn->shutdown_poll));
  626. if(connc_update_shutdown_ev(data->multi, connc->closure_handle, conn)) {
  627. DEBUGF(infof(data, "[CCACHE] update events for shutdown failed, "
  628. "discarding #%" CURL_FORMAT_CURL_OFF_T,
  629. conn->connection_id));
  630. connc_disconnect(data, conn, connc, FALSE);
  631. return;
  632. }
  633. }
  634. Curl_llist_append(&connc->shutdowns.conn_list, conn, &conn->bundle_node);
  635. DEBUGF(infof(data, "[CCACHE] added #%" CURL_FORMAT_CURL_OFF_T
  636. " to shutdown list of length %zu", conn->connection_id,
  637. Curl_llist_count(&connc->shutdowns.conn_list)));
  638. }
  639. void Curl_conncache_disconnect(struct Curl_easy *data,
  640. struct connectdata *conn,
  641. bool aborted)
  642. {
  643. DEBUGASSERT(data);
  644. /* Connection must no longer be in and connection cache */
  645. DEBUGASSERT(!conn->bundle);
  646. if(data->multi) {
  647. /* Add it to the multi's conncache for shutdown handling */
  648. infof(data, "%s connection #%" CURL_FORMAT_CURL_OFF_T,
  649. aborted? "closing" : "shutting down", conn->connection_id);
  650. connc_discard_conn(&data->multi->conn_cache, data, conn, aborted);
  651. }
  652. else {
  653. /* No multi available. Make a best-effort shutdown + close */
  654. infof(data, "closing connection #%" CURL_FORMAT_CURL_OFF_T,
  655. conn->connection_id);
  656. DEBUGASSERT(!conn->bundle);
  657. connc_run_conn_shutdown_handler(data, conn);
  658. connc_disconnect(data, conn, NULL, !aborted);
  659. }
  660. }
  661. static void connc_run_conn_shutdown_handler(struct Curl_easy *data,
  662. struct connectdata *conn)
  663. {
  664. if(!conn->bits.shutdown_handler) {
  665. if(conn->dns_entry) {
  666. Curl_resolv_unlock(data, conn->dns_entry);
  667. conn->dns_entry = NULL;
  668. }
  669. /* Cleanup NTLM connection-related data */
  670. Curl_http_auth_cleanup_ntlm(conn);
  671. /* Cleanup NEGOTIATE connection-related data */
  672. Curl_http_auth_cleanup_negotiate(conn);
  673. if(conn->handler && conn->handler->disconnect) {
  674. /* This is set if protocol-specific cleanups should be made */
  675. DEBUGF(infof(data, "connection #%" CURL_FORMAT_CURL_OFF_T
  676. ", shutdown protocol handler (aborted=%d)",
  677. conn->connection_id, conn->bits.aborted));
  678. conn->handler->disconnect(data, conn, conn->bits.aborted);
  679. }
  680. /* possible left-overs from the async name resolvers */
  681. Curl_resolver_cancel(data);
  682. conn->bits.shutdown_handler = TRUE;
  683. }
  684. }
  685. static void connc_run_conn_shutdown(struct Curl_easy *data,
  686. struct connectdata *conn,
  687. bool *done)
  688. {
  689. CURLcode r1, r2;
  690. bool done1, done2;
  691. /* We expect to be attached when called */
  692. DEBUGASSERT(data->conn == conn);
  693. connc_run_conn_shutdown_handler(data, conn);
  694. if(conn->bits.shutdown_filters) {
  695. *done = TRUE;
  696. return;
  697. }
  698. if(!conn->connect_only && Curl_conn_is_connected(conn, FIRSTSOCKET))
  699. r1 = Curl_conn_shutdown(data, FIRSTSOCKET, &done1);
  700. else {
  701. r1 = CURLE_OK;
  702. done1 = TRUE;
  703. }
  704. if(!conn->connect_only && Curl_conn_is_connected(conn, SECONDARYSOCKET))
  705. r2 = Curl_conn_shutdown(data, SECONDARYSOCKET, &done2);
  706. else {
  707. r2 = CURLE_OK;
  708. done2 = TRUE;
  709. }
  710. /* we are done when any failed or both report success */
  711. *done = (r1 || r2 || (done1 && done2));
  712. if(*done)
  713. conn->bits.shutdown_filters = TRUE;
  714. }
  715. CURLcode Curl_conncache_add_pollfds(struct conncache *connc,
  716. struct curl_pollfds *cpfds)
  717. {
  718. CURLcode result = CURLE_OK;
  719. DEBUGASSERT(!connc->shutdowns.iter_locked);
  720. connc->shutdowns.iter_locked = TRUE;
  721. if(connc->shutdowns.conn_list.head) {
  722. struct Curl_llist_element *e;
  723. struct easy_pollset ps;
  724. struct connectdata *conn;
  725. for(e = connc->shutdowns.conn_list.head; e; e = e->next) {
  726. conn = e->ptr;
  727. memset(&ps, 0, sizeof(ps));
  728. Curl_attach_connection(connc->closure_handle, conn);
  729. Curl_conn_adjust_pollset(connc->closure_handle, &ps);
  730. Curl_detach_connection(connc->closure_handle);
  731. result = Curl_pollfds_add_ps(cpfds, &ps);
  732. if(result) {
  733. Curl_pollfds_cleanup(cpfds);
  734. goto out;
  735. }
  736. }
  737. }
  738. out:
  739. connc->shutdowns.iter_locked = FALSE;
  740. return result;
  741. }
  742. CURLcode Curl_conncache_add_waitfds(struct conncache *connc,
  743. struct curl_waitfds *cwfds)
  744. {
  745. CURLcode result = CURLE_OK;
  746. DEBUGASSERT(!connc->shutdowns.iter_locked);
  747. connc->shutdowns.iter_locked = TRUE;
  748. if(connc->shutdowns.conn_list.head) {
  749. struct Curl_llist_element *e;
  750. struct easy_pollset ps;
  751. struct connectdata *conn;
  752. for(e = connc->shutdowns.conn_list.head; e; e = e->next) {
  753. conn = e->ptr;
  754. memset(&ps, 0, sizeof(ps));
  755. Curl_attach_connection(connc->closure_handle, conn);
  756. Curl_conn_adjust_pollset(connc->closure_handle, &ps);
  757. Curl_detach_connection(connc->closure_handle);
  758. result = Curl_waitfds_add_ps(cwfds, &ps);
  759. if(result)
  760. goto out;
  761. }
  762. }
  763. out:
  764. connc->shutdowns.iter_locked = FALSE;
  765. return result;
  766. }
  767. static void connc_perform(struct conncache *connc)
  768. {
  769. struct Curl_easy *data = connc->closure_handle;
  770. struct Curl_llist_element *e = connc->shutdowns.conn_list.head;
  771. struct Curl_llist_element *enext;
  772. struct connectdata *conn;
  773. struct curltime *nowp = NULL;
  774. struct curltime now;
  775. timediff_t next_from_now_ms = 0, ms;
  776. bool done;
  777. if(!e)
  778. return;
  779. DEBUGASSERT(data);
  780. DEBUGASSERT(!connc->shutdowns.iter_locked);
  781. DEBUGF(infof(data, "[CCACHE] perform, %zu connections being shutdown",
  782. Curl_llist_count(&connc->shutdowns.conn_list)));
  783. connc->shutdowns.iter_locked = TRUE;
  784. while(e) {
  785. enext = e->next;
  786. conn = e->ptr;
  787. Curl_attach_connection(data, conn);
  788. connc_run_conn_shutdown(data, conn, &done);
  789. DEBUGF(infof(data, "[CCACHE] shutdown #%" CURL_FORMAT_CURL_OFF_T
  790. ", done=%d", conn->connection_id, done));
  791. Curl_detach_connection(data);
  792. if(done) {
  793. Curl_llist_remove(&connc->shutdowns.conn_list, e, NULL);
  794. connc_disconnect(NULL, conn, connc, FALSE);
  795. }
  796. else {
  797. /* Not done, when does this connection time out? */
  798. if(!nowp) {
  799. now = Curl_now();
  800. nowp = &now;
  801. }
  802. ms = Curl_conn_shutdown_timeleft(conn, nowp);
  803. if(ms && ms < next_from_now_ms)
  804. next_from_now_ms = ms;
  805. }
  806. e = enext;
  807. }
  808. connc->shutdowns.iter_locked = FALSE;
  809. if(next_from_now_ms)
  810. Curl_expire(data, next_from_now_ms, EXPIRE_RUN_NOW);
  811. }
  812. void Curl_conncache_multi_perform(struct Curl_multi *multi)
  813. {
  814. connc_perform(&multi->conn_cache);
  815. }
  816. /*
  817. * Disconnects the given connection. Note the connection may not be the
  818. * primary connection, like when freeing room in the connection cache or
  819. * killing of a dead old connection.
  820. *
  821. * A connection needs an easy handle when closing down. We support this passed
  822. * in separately since the connection to get closed here is often already
  823. * disassociated from an easy handle.
  824. *
  825. * This function MUST NOT reset state in the Curl_easy struct if that
  826. * is not strictly bound to the life-time of *this* particular connection.
  827. *
  828. */
  829. static void connc_disconnect(struct Curl_easy *data,
  830. struct connectdata *conn,
  831. struct conncache *connc,
  832. bool do_shutdown)
  833. {
  834. bool done;
  835. /* there must be a connection to close */
  836. DEBUGASSERT(conn);
  837. /* it must be removed from the connection cache */
  838. DEBUGASSERT(!conn->bundle);
  839. /* there must be an associated transfer */
  840. DEBUGASSERT(data || connc);
  841. if(!data)
  842. data = connc->closure_handle;
  843. /* the transfer must be detached from the connection */
  844. DEBUGASSERT(data && !data->conn);
  845. Curl_attach_connection(data, conn);
  846. if(connc && connc->multi && connc->multi->socket_cb) {
  847. struct easy_pollset ps;
  848. /* With an empty pollset, all previously polled sockets will be removed
  849. * via the multi_socket API callback. */
  850. memset(&ps, 0, sizeof(ps));
  851. (void)Curl_multi_pollset_ev(connc->multi, data, &ps, &conn->shutdown_poll);
  852. }
  853. connc_run_conn_shutdown_handler(data, conn);
  854. if(do_shutdown) {
  855. /* Make a last attempt to shutdown handlers and filters, if
  856. * not done so already. */
  857. connc_run_conn_shutdown(data, conn, &done);
  858. }
  859. if(connc)
  860. DEBUGF(infof(data, "[CCACHE] closing #%" CURL_FORMAT_CURL_OFF_T,
  861. conn->connection_id));
  862. else
  863. DEBUGF(infof(data, "closing connection #%" CURL_FORMAT_CURL_OFF_T,
  864. conn->connection_id));
  865. Curl_conn_close(data, SECONDARYSOCKET);
  866. Curl_conn_close(data, FIRSTSOCKET);
  867. Curl_detach_connection(data);
  868. Curl_conn_free(data, conn);
  869. }
  870. static CURLMcode connc_update_shutdown_ev(struct Curl_multi *multi,
  871. struct Curl_easy *data,
  872. struct connectdata *conn)
  873. {
  874. struct easy_pollset ps;
  875. CURLMcode mresult;
  876. DEBUGASSERT(data);
  877. DEBUGASSERT(multi);
  878. DEBUGASSERT(multi->socket_cb);
  879. memset(&ps, 0, sizeof(ps));
  880. Curl_attach_connection(data, conn);
  881. Curl_conn_adjust_pollset(data, &ps);
  882. Curl_detach_connection(data);
  883. mresult = Curl_multi_pollset_ev(multi, data, &ps, &conn->shutdown_poll);
  884. if(!mresult) /* Remember for next time */
  885. memcpy(&conn->shutdown_poll, &ps, sizeof(ps));
  886. return mresult;
  887. }
  888. void Curl_conncache_multi_socket(struct Curl_multi *multi,
  889. curl_socket_t s, int ev_bitmask)
  890. {
  891. struct conncache *connc = &multi->conn_cache;
  892. struct Curl_easy *data = connc->closure_handle;
  893. struct Curl_llist_element *e = connc->shutdowns.conn_list.head;
  894. struct connectdata *conn;
  895. bool done;
  896. (void)ev_bitmask;
  897. DEBUGASSERT(multi->socket_cb);
  898. if(!e)
  899. return;
  900. connc->shutdowns.iter_locked = TRUE;
  901. while(e) {
  902. conn = e->ptr;
  903. if(s == conn->sock[FIRSTSOCKET] || s == conn->sock[SECONDARYSOCKET]) {
  904. Curl_attach_connection(data, conn);
  905. connc_run_conn_shutdown(data, conn, &done);
  906. DEBUGF(infof(data, "[CCACHE] shutdown #%" CURL_FORMAT_CURL_OFF_T
  907. ", done=%d", conn->connection_id, done));
  908. Curl_detach_connection(data);
  909. if(done || connc_update_shutdown_ev(multi, data, conn)) {
  910. Curl_llist_remove(&connc->shutdowns.conn_list, e, NULL);
  911. connc_disconnect(NULL, conn, connc, FALSE);
  912. }
  913. break;
  914. }
  915. e = e->next;
  916. }
  917. connc->shutdowns.iter_locked = FALSE;
  918. }
  919. void Curl_conncache_multi_close_all(struct Curl_multi *multi)
  920. {
  921. connc_close_all(&multi->conn_cache);
  922. }
  923. #define NUM_POLLS_ON_STACK 10
  924. static CURLcode connc_shutdown_wait(struct conncache *connc, int timeout_ms)
  925. {
  926. struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
  927. struct curl_pollfds cpfds;
  928. CURLcode result;
  929. Curl_pollfds_init(&cpfds, a_few_on_stack, NUM_POLLS_ON_STACK);
  930. result = Curl_conncache_add_pollfds(connc, &cpfds);
  931. if(result)
  932. goto out;
  933. Curl_poll(cpfds.pfds, cpfds.n, CURLMIN(timeout_ms, 1000));
  934. out:
  935. Curl_pollfds_cleanup(&cpfds);
  936. return result;
  937. }
  938. static void connc_shutdown_all(struct conncache *connc, int timeout_ms)
  939. {
  940. struct Curl_easy *data = connc->closure_handle;
  941. struct connectdata *conn;
  942. struct curltime started = Curl_now();
  943. if(!data)
  944. return;
  945. (void)data;
  946. DEBUGF(infof(data, "conncache shutdown all"));
  947. /* Move all connections into the shutdown queue */
  948. conn = connc_find_first_connection(connc);
  949. while(conn) {
  950. /* This will remove the connection from the cache */
  951. DEBUGF(infof(data, "moving connection %" CURL_FORMAT_CURL_OFF_T
  952. " to shutdown queue", conn->connection_id));
  953. connc_remove_conn(connc, conn);
  954. connc_discard_conn(connc, NULL, conn, FALSE);
  955. conn = connc_find_first_connection(connc);
  956. }
  957. DEBUGASSERT(!connc->shutdowns.iter_locked);
  958. while(connc->shutdowns.conn_list.head) {
  959. timediff_t timespent;
  960. int remain_ms;
  961. connc_perform(connc);
  962. if(!connc->shutdowns.conn_list.head) {
  963. DEBUGF(infof(data, "conncache shutdown ok"));
  964. break;
  965. }
  966. /* wait for activity, timeout or "nothing" */
  967. timespent = Curl_timediff(Curl_now(), started);
  968. if(timespent >= (timediff_t)timeout_ms) {
  969. DEBUGF(infof(data, "conncache shutdown %s",
  970. (timeout_ms > 0)? "timeout" : "best effort done"));
  971. break;
  972. }
  973. remain_ms = timeout_ms - (int)timespent;
  974. if(connc_shutdown_wait(connc, remain_ms)) {
  975. DEBUGF(infof(data, "conncache shutdown all, abort"));
  976. break;
  977. }
  978. }
  979. /* Due to errors/timeout, we might come here without being full ydone. */
  980. connc_shutdown_discard_all(connc);
  981. }
  982. #if 0
  983. /* Useful for debugging the connection cache */
  984. void Curl_conncache_print(struct conncache *connc)
  985. {
  986. struct Curl_hash_iterator iter;
  987. struct Curl_llist_element *curr;
  988. struct Curl_hash_element *he;
  989. if(!connc)
  990. return;
  991. fprintf(stderr, "=Bundle cache=\n");
  992. Curl_hash_start_iterate(connc->hash, &iter);
  993. he = Curl_hash_next_element(&iter);
  994. while(he) {
  995. struct connectbundle *bundle;
  996. struct connectdata *conn;
  997. bundle = he->ptr;
  998. fprintf(stderr, "%s -", he->key);
  999. curr = bundle->conn_list->head;
  1000. while(curr) {
  1001. conn = curr->ptr;
  1002. fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
  1003. curr = curr->next;
  1004. }
  1005. fprintf(stderr, "\n");
  1006. he = Curl_hash_next_element(&iter);
  1007. }
  1008. }
  1009. #endif