2
0

conncache.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  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 CPOOL_IS_LOCKED(c) ((c) && (c)->locked)
  46. #define CPOOL_LOCK(c) \
  47. do { \
  48. if((c)) { \
  49. if(CURL_SHARE_KEEP_CONNECT((c)->share)) \
  50. Curl_share_lock(((c)->idata), CURL_LOCK_DATA_CONNECT, \
  51. CURL_LOCK_ACCESS_SINGLE); \
  52. DEBUGASSERT(!(c)->locked); \
  53. (c)->locked = TRUE; \
  54. } \
  55. } while(0)
  56. #define CPOOL_UNLOCK(c) \
  57. do { \
  58. if((c)) { \
  59. DEBUGASSERT((c)->locked); \
  60. (c)->locked = FALSE; \
  61. if(CURL_SHARE_KEEP_CONNECT((c)->share)) \
  62. Curl_share_unlock((c)->idata, CURL_LOCK_DATA_CONNECT); \
  63. } \
  64. } while(0)
  65. /* A list of connections to the same destination. */
  66. struct cpool_bundle {
  67. struct Curl_llist conns; /* connections in the bundle */
  68. size_t dest_len; /* total length of destination, including NUL */
  69. char *dest[1]; /* destination of bundle, allocated to keep dest_len bytes */
  70. };
  71. static void cpool_discard_conn(struct cpool *cpool,
  72. struct Curl_easy *data,
  73. struct connectdata *conn,
  74. bool aborted);
  75. static void cpool_close_and_destroy(struct cpool *cpool,
  76. struct connectdata *conn,
  77. struct Curl_easy *data,
  78. bool do_shutdown);
  79. static void cpool_run_conn_shutdown(struct Curl_easy *data,
  80. struct connectdata *conn,
  81. bool *done);
  82. static void cpool_run_conn_shutdown_handler(struct Curl_easy *data,
  83. struct connectdata *conn);
  84. static CURLMcode cpool_update_shutdown_ev(struct Curl_multi *multi,
  85. struct Curl_easy *data,
  86. struct connectdata *conn);
  87. static void cpool_shutdown_all(struct cpool *cpool,
  88. struct Curl_easy *data, int timeout_ms);
  89. static void cpool_close_and_destroy_all(struct cpool *cpool);
  90. static struct connectdata *cpool_get_oldest_idle(struct cpool *cpool);
  91. static struct cpool_bundle *cpool_bundle_create(const char *dest,
  92. size_t dest_len)
  93. {
  94. struct cpool_bundle *bundle;
  95. bundle = calloc(1, sizeof(*bundle) + dest_len);
  96. if(!bundle)
  97. return NULL;
  98. Curl_llist_init(&bundle->conns, NULL);
  99. bundle->dest_len = dest_len;
  100. memcpy(bundle->dest, dest, dest_len);
  101. return bundle;
  102. }
  103. static void cpool_bundle_destroy(struct cpool_bundle *bundle)
  104. {
  105. DEBUGASSERT(!Curl_llist_count(&bundle->conns));
  106. free(bundle);
  107. }
  108. /* Add a connection to a bundle */
  109. static void cpool_bundle_add(struct cpool_bundle *bundle,
  110. struct connectdata *conn)
  111. {
  112. DEBUGASSERT(!Curl_node_llist(&conn->cpool_node));
  113. Curl_llist_append(&bundle->conns, conn, &conn->cpool_node);
  114. conn->bits.in_cpool = TRUE;
  115. }
  116. /* Remove a connection from a bundle */
  117. static void cpool_bundle_remove(struct cpool_bundle *bundle,
  118. struct connectdata *conn)
  119. {
  120. (void)bundle;
  121. DEBUGASSERT(Curl_node_llist(&conn->cpool_node) == &bundle->conns);
  122. Curl_node_remove(&conn->cpool_node);
  123. conn->bits.in_cpool = FALSE;
  124. }
  125. static void cpool_bundle_free_entry(void *freethis)
  126. {
  127. cpool_bundle_destroy((struct cpool_bundle *)freethis);
  128. }
  129. int Curl_cpool_init(struct cpool *cpool,
  130. Curl_cpool_disconnect_cb *disconnect_cb,
  131. struct Curl_multi *multi,
  132. struct Curl_share *share,
  133. size_t size)
  134. {
  135. DEBUGASSERT(!!multi != !!share); /* either one */
  136. Curl_hash_init(&cpool->dest2bundle, size, Curl_hash_str,
  137. Curl_str_key_compare, cpool_bundle_free_entry);
  138. Curl_llist_init(&cpool->shutdowns, NULL);
  139. DEBUGASSERT(disconnect_cb);
  140. if(!disconnect_cb)
  141. return 1;
  142. /* allocate a new easy handle to use when closing cached connections */
  143. cpool->idata = curl_easy_init();
  144. if(!cpool->idata)
  145. return 1; /* bad */
  146. cpool->idata->state.internal = TRUE;
  147. /* TODO: this is quirky. We need an internal handle for certain
  148. * operations, but we do not add it to the multi (if there is one).
  149. * But we give it the multi so that socket event operations can work.
  150. * Probably better to have an internal handle owned by the multi that
  151. * can be used for cpool operations. */
  152. cpool->idata->multi = multi;
  153. #ifdef DEBUGBUILD
  154. if(getenv("CURL_DEBUG"))
  155. cpool->idata->set.verbose = TRUE;
  156. #endif
  157. cpool->disconnect_cb = disconnect_cb;
  158. cpool->idata->multi = cpool->multi = multi;
  159. cpool->idata->share = cpool->share = share;
  160. return 0; /* good */
  161. }
  162. void Curl_cpool_destroy(struct cpool *cpool)
  163. {
  164. if(cpool) {
  165. if(cpool->idata) {
  166. cpool_close_and_destroy_all(cpool);
  167. /* The internal closure handle is special and we need to
  168. * disconnect it from multi/share before closing it down. */
  169. cpool->idata->multi = NULL;
  170. cpool->idata->share = NULL;
  171. Curl_close(&cpool->idata);
  172. }
  173. Curl_hash_destroy(&cpool->dest2bundle);
  174. cpool->multi = NULL;
  175. }
  176. }
  177. static struct cpool *cpool_get_instance(struct Curl_easy *data)
  178. {
  179. if(data) {
  180. if(CURL_SHARE_KEEP_CONNECT(data->share))
  181. return &data->share->cpool;
  182. else if(data->multi_easy)
  183. return &data->multi_easy->cpool;
  184. else if(data->multi)
  185. return &data->multi->cpool;
  186. }
  187. return NULL;
  188. }
  189. void Curl_cpool_xfer_init(struct Curl_easy *data)
  190. {
  191. struct cpool *cpool = cpool_get_instance(data);
  192. DEBUGASSERT(cpool);
  193. if(cpool) {
  194. CPOOL_LOCK(cpool);
  195. /* the identifier inside the connection cache */
  196. data->id = cpool->next_easy_id++;
  197. if(cpool->next_easy_id <= 0)
  198. cpool->next_easy_id = 0;
  199. data->state.lastconnect_id = -1;
  200. /* The closure handle only ever has default timeouts set. To improve the
  201. state somewhat we clone the timeouts from each added handle so that the
  202. closure handle always has the same timeouts as the most recently added
  203. easy handle. */
  204. cpool->idata->set.timeout = data->set.timeout;
  205. cpool->idata->set.server_response_timeout =
  206. data->set.server_response_timeout;
  207. cpool->idata->set.no_signal = data->set.no_signal;
  208. CPOOL_UNLOCK(cpool);
  209. }
  210. else {
  211. /* We should not get here, but in a non-debug build, do something */
  212. data->id = 0;
  213. data->state.lastconnect_id = -1;
  214. }
  215. }
  216. static struct cpool_bundle *cpool_find_bundle(struct cpool *cpool,
  217. struct connectdata *conn)
  218. {
  219. return Curl_hash_pick(&cpool->dest2bundle,
  220. conn->destination, conn->destination_len);
  221. }
  222. static struct cpool_bundle *
  223. cpool_add_bundle(struct cpool *cpool, struct connectdata *conn)
  224. {
  225. struct cpool_bundle *bundle;
  226. bundle = cpool_bundle_create(conn->destination, conn->destination_len);
  227. if(!bundle)
  228. return NULL;
  229. if(!Curl_hash_add(&cpool->dest2bundle,
  230. bundle->dest, bundle->dest_len, bundle)) {
  231. cpool_bundle_destroy(bundle);
  232. return NULL;
  233. }
  234. return bundle;
  235. }
  236. static void cpool_remove_bundle(struct cpool *cpool,
  237. struct cpool_bundle *bundle)
  238. {
  239. if(!cpool)
  240. return;
  241. Curl_hash_delete(&cpool->dest2bundle, bundle->dest, bundle->dest_len);
  242. }
  243. static struct connectdata *
  244. cpool_bundle_get_oldest_idle(struct cpool_bundle *bundle);
  245. int Curl_cpool_check_limits(struct Curl_easy *data,
  246. struct connectdata *conn)
  247. {
  248. struct cpool *cpool = cpool_get_instance(data);
  249. struct cpool_bundle *bundle;
  250. size_t dest_limit = 0;
  251. size_t total_limit = 0;
  252. int result = CPOOL_LIMIT_OK;
  253. if(!cpool)
  254. return CPOOL_LIMIT_OK;
  255. if(data && data->multi) {
  256. dest_limit = data->multi->max_host_connections;
  257. total_limit = data->multi->max_total_connections;
  258. }
  259. if(!dest_limit && !total_limit)
  260. return CPOOL_LIMIT_OK;
  261. CPOOL_LOCK(cpool);
  262. if(dest_limit) {
  263. bundle = cpool_find_bundle(cpool, conn);
  264. while(bundle && (Curl_llist_count(&bundle->conns) >= dest_limit)) {
  265. struct connectdata *oldest_idle = NULL;
  266. /* The bundle is full. Extract the oldest connection that may
  267. * be removed now, if there is one. */
  268. oldest_idle = cpool_bundle_get_oldest_idle(bundle);
  269. if(!oldest_idle)
  270. break;
  271. /* disconnect the old conn and continue */
  272. DEBUGF(infof(data, "Discarding connection #%"
  273. FMT_OFF_T " from %zu to reach destination "
  274. "limit of %zu", oldest_idle->connection_id,
  275. Curl_llist_count(&bundle->conns), dest_limit));
  276. Curl_cpool_disconnect(data, oldest_idle, FALSE);
  277. /* in case the bundle was destroyed in disconnect, look it up again */
  278. bundle = cpool_find_bundle(cpool, conn);
  279. }
  280. if(bundle && (Curl_llist_count(&bundle->conns) >= dest_limit)) {
  281. result = CPOOL_LIMIT_DEST;
  282. goto out;
  283. }
  284. }
  285. if(total_limit) {
  286. while(cpool->num_conn >= total_limit) {
  287. struct connectdata *oldest_idle = cpool_get_oldest_idle(cpool);
  288. if(!oldest_idle)
  289. break;
  290. /* disconnect the old conn and continue */
  291. DEBUGF(infof(data, "Discarding connection #%"
  292. FMT_OFF_T " from %zu to reach total "
  293. "limit of %zu",
  294. oldest_idle->connection_id, cpool->num_conn, total_limit));
  295. Curl_cpool_disconnect(data, oldest_idle, FALSE);
  296. }
  297. if(cpool->num_conn >= total_limit) {
  298. result = CPOOL_LIMIT_TOTAL;
  299. goto out;
  300. }
  301. }
  302. out:
  303. CPOOL_UNLOCK(cpool);
  304. return result;
  305. }
  306. CURLcode Curl_cpool_add_conn(struct Curl_easy *data,
  307. struct connectdata *conn)
  308. {
  309. CURLcode result = CURLE_OK;
  310. struct cpool_bundle *bundle = NULL;
  311. struct cpool *cpool = cpool_get_instance(data);
  312. DEBUGASSERT(conn);
  313. DEBUGASSERT(cpool);
  314. if(!cpool)
  315. return CURLE_FAILED_INIT;
  316. CPOOL_LOCK(cpool);
  317. bundle = cpool_find_bundle(cpool, conn);
  318. if(!bundle) {
  319. bundle = cpool_add_bundle(cpool, conn);
  320. if(!bundle) {
  321. result = CURLE_OUT_OF_MEMORY;
  322. goto out;
  323. }
  324. }
  325. cpool_bundle_add(bundle, conn);
  326. conn->connection_id = cpool->next_connection_id++;
  327. cpool->num_conn++;
  328. DEBUGF(infof(data, "Added connection %" FMT_OFF_T ". "
  329. "The cache now contains %zu members",
  330. conn->connection_id, cpool->num_conn));
  331. out:
  332. CPOOL_UNLOCK(cpool);
  333. return result;
  334. }
  335. static void cpool_remove_conn(struct cpool *cpool,
  336. struct connectdata *conn)
  337. {
  338. struct Curl_llist *list = Curl_node_llist(&conn->cpool_node);
  339. DEBUGASSERT(cpool);
  340. if(list) {
  341. /* The connection is certainly in the pool, but where? */
  342. struct cpool_bundle *bundle = cpool_find_bundle(cpool, conn);
  343. if(bundle && (list == &bundle->conns)) {
  344. cpool_bundle_remove(bundle, conn);
  345. if(!Curl_llist_count(&bundle->conns))
  346. cpool_remove_bundle(cpool, bundle);
  347. conn->bits.in_cpool = FALSE;
  348. cpool->num_conn--;
  349. }
  350. else {
  351. /* Not in a bundle, already in the shutdown list? */
  352. DEBUGASSERT(list == &cpool->shutdowns);
  353. }
  354. }
  355. }
  356. /* This function iterates the entire connection pool and calls the function
  357. func() with the connection pointer as the first argument and the supplied
  358. 'param' argument as the other.
  359. The cpool lock is still held when the callback is called. It needs it,
  360. so that it can safely continue traversing the lists once the callback
  361. returns.
  362. Returns TRUE if the loop was aborted due to the callback's return code.
  363. Return 0 from func() to continue the loop, return 1 to abort it.
  364. */
  365. static bool cpool_foreach(struct Curl_easy *data,
  366. struct cpool *cpool,
  367. void *param,
  368. int (*func)(struct Curl_easy *data,
  369. struct connectdata *conn, void *param))
  370. {
  371. struct Curl_hash_iterator iter;
  372. struct Curl_hash_element *he;
  373. if(!cpool)
  374. return FALSE;
  375. Curl_hash_start_iterate(&cpool->dest2bundle, &iter);
  376. he = Curl_hash_next_element(&iter);
  377. while(he) {
  378. struct Curl_llist_node *curr;
  379. struct cpool_bundle *bundle = he->ptr;
  380. he = Curl_hash_next_element(&iter);
  381. curr = Curl_llist_head(&bundle->conns);
  382. while(curr) {
  383. /* Yes, we need to update curr before calling func(), because func()
  384. might decide to remove the connection */
  385. struct connectdata *conn = Curl_node_elem(curr);
  386. curr = Curl_node_next(curr);
  387. if(1 == func(data, conn, param)) {
  388. return TRUE;
  389. }
  390. }
  391. }
  392. return FALSE;
  393. }
  394. /* Return a live connection in the pool or NULL. */
  395. static struct connectdata *cpool_get_live_conn(struct cpool *cpool)
  396. {
  397. struct Curl_hash_iterator iter;
  398. struct Curl_hash_element *he;
  399. struct cpool_bundle *bundle;
  400. struct Curl_llist_node *conn_node;
  401. Curl_hash_start_iterate(&cpool->dest2bundle, &iter);
  402. for(he = Curl_hash_next_element(&iter); he;
  403. he = Curl_hash_next_element(&iter)) {
  404. bundle = he->ptr;
  405. conn_node = Curl_llist_head(&bundle->conns);
  406. if(conn_node)
  407. return Curl_node_elem(conn_node);
  408. }
  409. return NULL;
  410. }
  411. /*
  412. * A connection (already in the pool) has become idle. Do any
  413. * cleanups in regard to the pool's limits.
  414. *
  415. * Return TRUE if idle connection kept in pool, FALSE if closed.
  416. */
  417. bool Curl_cpool_conn_now_idle(struct Curl_easy *data,
  418. struct connectdata *conn)
  419. {
  420. unsigned int maxconnects = !data->multi->maxconnects ?
  421. data->multi->num_easy * 4 : data->multi->maxconnects;
  422. struct connectdata *oldest_idle = NULL;
  423. struct cpool *cpool = cpool_get_instance(data);
  424. bool kept = TRUE;
  425. conn->lastused = Curl_now(); /* it was used up until now */
  426. if(cpool && maxconnects) {
  427. /* may be called form a callback already under lock */
  428. bool do_lock = !CPOOL_IS_LOCKED(cpool);
  429. if(do_lock)
  430. CPOOL_LOCK(cpool);
  431. if(cpool->num_conn > maxconnects) {
  432. infof(data, "Connection pool is full, closing the oldest one");
  433. oldest_idle = cpool_get_oldest_idle(cpool);
  434. kept = (oldest_idle != conn);
  435. if(oldest_idle) {
  436. Curl_cpool_disconnect(cpool->idata, oldest_idle, FALSE);
  437. }
  438. }
  439. if(do_lock)
  440. CPOOL_UNLOCK(cpool);
  441. }
  442. return kept;
  443. }
  444. /*
  445. * This function finds the connection in the connection bundle that has been
  446. * unused for the longest time.
  447. */
  448. static struct connectdata *
  449. cpool_bundle_get_oldest_idle(struct cpool_bundle *bundle)
  450. {
  451. struct Curl_llist_node *curr;
  452. timediff_t highscore = -1;
  453. timediff_t score;
  454. struct curltime now;
  455. struct connectdata *oldest_idle = NULL;
  456. struct connectdata *conn;
  457. now = Curl_now();
  458. curr = Curl_llist_head(&bundle->conns);
  459. while(curr) {
  460. conn = Curl_node_elem(curr);
  461. if(!CONN_INUSE(conn)) {
  462. /* Set higher score for the age passed since the connection was used */
  463. score = Curl_timediff(now, conn->lastused);
  464. if(score > highscore) {
  465. highscore = score;
  466. oldest_idle = conn;
  467. }
  468. }
  469. curr = Curl_node_next(curr);
  470. }
  471. return oldest_idle;
  472. }
  473. static struct connectdata *cpool_get_oldest_idle(struct cpool *cpool)
  474. {
  475. struct Curl_hash_iterator iter;
  476. struct Curl_llist_node *curr;
  477. struct Curl_hash_element *he;
  478. struct connectdata *oldest_idle = NULL;
  479. struct cpool_bundle *bundle;
  480. struct curltime now;
  481. timediff_t highscore =- 1;
  482. timediff_t score;
  483. now = Curl_now();
  484. Curl_hash_start_iterate(&cpool->dest2bundle, &iter);
  485. for(he = Curl_hash_next_element(&iter); he;
  486. he = Curl_hash_next_element(&iter)) {
  487. struct connectdata *conn;
  488. bundle = he->ptr;
  489. for(curr = Curl_llist_head(&bundle->conns); curr;
  490. curr = Curl_node_next(curr)) {
  491. conn = Curl_node_elem(curr);
  492. if(CONN_INUSE(conn) || conn->bits.close || conn->connect_only)
  493. continue;
  494. /* Set higher score for the age passed since the connection was used */
  495. score = Curl_timediff(now, conn->lastused);
  496. if(score > highscore) {
  497. highscore = score;
  498. oldest_idle = conn;
  499. }
  500. }
  501. }
  502. return oldest_idle;
  503. }
  504. bool Curl_cpool_find(struct Curl_easy *data,
  505. const char *destination, size_t dest_len,
  506. Curl_cpool_conn_match_cb *conn_cb,
  507. Curl_cpool_done_match_cb *done_cb,
  508. void *userdata)
  509. {
  510. struct cpool *cpool = cpool_get_instance(data);
  511. struct cpool_bundle *bundle;
  512. bool result = FALSE;
  513. DEBUGASSERT(cpool);
  514. DEBUGASSERT(conn_cb);
  515. if(!cpool)
  516. return FALSE;
  517. CPOOL_LOCK(cpool);
  518. bundle = Curl_hash_pick(&cpool->dest2bundle, (void *)destination, dest_len);
  519. if(bundle) {
  520. struct Curl_llist_node *curr = Curl_llist_head(&bundle->conns);
  521. while(curr) {
  522. struct connectdata *conn = Curl_node_elem(curr);
  523. /* Get next node now. callback might discard current */
  524. curr = Curl_node_next(curr);
  525. if(conn_cb(conn, userdata)) {
  526. result = TRUE;
  527. break;
  528. }
  529. }
  530. }
  531. if(done_cb) {
  532. result = done_cb(result, userdata);
  533. }
  534. CPOOL_UNLOCK(cpool);
  535. return result;
  536. }
  537. static void cpool_shutdown_discard_all(struct cpool *cpool)
  538. {
  539. struct Curl_llist_node *e = Curl_llist_head(&cpool->shutdowns);
  540. struct connectdata *conn;
  541. if(!e)
  542. return;
  543. DEBUGF(infof(cpool->idata, "cpool_shutdown_discard_all"));
  544. while(e) {
  545. conn = Curl_node_elem(e);
  546. Curl_node_remove(e);
  547. DEBUGF(infof(cpool->idata, "discard connection #%" FMT_OFF_T,
  548. conn->connection_id));
  549. cpool_close_and_destroy(cpool, conn, NULL, FALSE);
  550. e = Curl_llist_head(&cpool->shutdowns);
  551. }
  552. }
  553. static void cpool_close_and_destroy_all(struct cpool *cpool)
  554. {
  555. struct connectdata *conn;
  556. int timeout_ms = 0;
  557. SIGPIPE_VARIABLE(pipe_st);
  558. DEBUGASSERT(cpool);
  559. /* Move all connections to the shutdown list */
  560. sigpipe_init(&pipe_st);
  561. CPOOL_LOCK(cpool);
  562. conn = cpool_get_live_conn(cpool);
  563. while(conn) {
  564. cpool_remove_conn(cpool, conn);
  565. sigpipe_apply(cpool->idata, &pipe_st);
  566. connclose(conn, "kill all");
  567. cpool_discard_conn(cpool, cpool->idata, conn, FALSE);
  568. conn = cpool_get_live_conn(cpool);
  569. }
  570. CPOOL_UNLOCK(cpool);
  571. /* Just for testing, run graceful shutdown */
  572. #ifdef DEBUGBUILD
  573. {
  574. char *p = getenv("CURL_GRACEFUL_SHUTDOWN");
  575. if(p) {
  576. long l = strtol(p, NULL, 10);
  577. if(l > 0 && l < INT_MAX)
  578. timeout_ms = (int)l;
  579. }
  580. }
  581. #endif
  582. sigpipe_apply(cpool->idata, &pipe_st);
  583. cpool_shutdown_all(cpool, cpool->idata, timeout_ms);
  584. /* discard all connections in the shutdown list */
  585. cpool_shutdown_discard_all(cpool);
  586. Curl_hostcache_clean(cpool->idata, cpool->idata->dns.hostcache);
  587. sigpipe_restore(&pipe_st);
  588. }
  589. static void cpool_shutdown_destroy_oldest(struct cpool *cpool)
  590. {
  591. struct Curl_llist_node *e;
  592. struct connectdata *conn;
  593. e = Curl_llist_head(&cpool->shutdowns);
  594. if(e) {
  595. SIGPIPE_VARIABLE(pipe_st);
  596. conn = Curl_node_elem(e);
  597. Curl_node_remove(e);
  598. sigpipe_init(&pipe_st);
  599. sigpipe_apply(cpool->idata, &pipe_st);
  600. cpool_close_and_destroy(cpool, conn, NULL, FALSE);
  601. sigpipe_restore(&pipe_st);
  602. }
  603. }
  604. static void cpool_discard_conn(struct cpool *cpool,
  605. struct Curl_easy *data,
  606. struct connectdata *conn,
  607. bool aborted)
  608. {
  609. bool done = FALSE;
  610. DEBUGASSERT(data);
  611. DEBUGASSERT(cpool);
  612. DEBUGASSERT(!conn->bits.in_cpool);
  613. /*
  614. * If this connection is not marked to force-close, leave it open if there
  615. * are other users of it
  616. */
  617. if(CONN_INUSE(conn) && !aborted) {
  618. DEBUGF(infof(data, "[CCACHE] not discarding #%" FMT_OFF_T
  619. " still in use by %zu transfers", conn->connection_id,
  620. CONN_INUSE(conn)));
  621. return;
  622. }
  623. /* treat the connection as aborted in CONNECT_ONLY situations, we do
  624. * not know what the APP did with it. */
  625. if(conn->connect_only)
  626. aborted = TRUE;
  627. conn->bits.aborted = aborted;
  628. /* We do not shutdown dead connections. The term 'dead' can be misleading
  629. * here, as we also mark errored connections/transfers as 'dead'.
  630. * If we do a shutdown for an aborted transfer, the server might think
  631. * it was successful otherwise (for example an ftps: upload). This is
  632. * not what we want. */
  633. if(aborted)
  634. done = TRUE;
  635. if(!done) {
  636. /* Attempt to shutdown the connection right away. */
  637. Curl_attach_connection(data, conn);
  638. cpool_run_conn_shutdown(data, conn, &done);
  639. DEBUGF(infof(data, "[CCACHE] shutdown #%" FMT_OFF_T ", done=%d",
  640. conn->connection_id, done));
  641. Curl_detach_connection(data);
  642. }
  643. if(done) {
  644. cpool_close_and_destroy(cpool, conn, data, FALSE);
  645. return;
  646. }
  647. /* Add the connection to our shutdown list for non-blocking shutdown
  648. * during multi processing. */
  649. if(data->multi && data->multi->max_shutdown_connections > 0 &&
  650. (data->multi->max_shutdown_connections >=
  651. (long)Curl_llist_count(&cpool->shutdowns))) {
  652. DEBUGF(infof(data, "[CCACHE] discarding oldest shutdown connection "
  653. "due to limit of %ld",
  654. data->multi->max_shutdown_connections));
  655. cpool_shutdown_destroy_oldest(cpool);
  656. }
  657. if(data->multi && data->multi->socket_cb) {
  658. DEBUGASSERT(cpool == &data->multi->cpool);
  659. /* Start with an empty shutdown pollset, so out internal closure handle
  660. * is added to the sockets. */
  661. memset(&conn->shutdown_poll, 0, sizeof(conn->shutdown_poll));
  662. if(cpool_update_shutdown_ev(data->multi, cpool->idata, conn)) {
  663. DEBUGF(infof(data, "[CCACHE] update events for shutdown failed, "
  664. "discarding #%" FMT_OFF_T,
  665. conn->connection_id));
  666. cpool_close_and_destroy(cpool, conn, data, FALSE);
  667. return;
  668. }
  669. }
  670. Curl_llist_append(&cpool->shutdowns, conn, &conn->cpool_node);
  671. DEBUGF(infof(data, "[CCACHE] added #%" FMT_OFF_T
  672. " to shutdown list of length %zu", conn->connection_id,
  673. Curl_llist_count(&cpool->shutdowns)));
  674. }
  675. void Curl_cpool_disconnect(struct Curl_easy *data,
  676. struct connectdata *conn,
  677. bool aborted)
  678. {
  679. struct cpool *cpool = cpool_get_instance(data);
  680. bool do_lock;
  681. DEBUGASSERT(cpool);
  682. DEBUGASSERT(data && !data->conn);
  683. if(!cpool)
  684. return;
  685. /* If this connection is not marked to force-close, leave it open if there
  686. * are other users of it */
  687. if(CONN_INUSE(conn) && !aborted) {
  688. DEBUGASSERT(0); /* does this ever happen? */
  689. DEBUGF(infof(data, "Curl_disconnect when inuse: %zu", CONN_INUSE(conn)));
  690. return;
  691. }
  692. /* This method may be called while we are under lock, e.g. from a
  693. * user callback in find. */
  694. do_lock = !CPOOL_IS_LOCKED(cpool);
  695. if(do_lock)
  696. CPOOL_LOCK(cpool);
  697. if(conn->bits.in_cpool) {
  698. cpool_remove_conn(cpool, conn);
  699. DEBUGASSERT(!conn->bits.in_cpool);
  700. }
  701. /* Run the callback to let it clean up anything it wants to. */
  702. aborted = cpool->disconnect_cb(data, conn, aborted);
  703. if(data->multi) {
  704. /* Add it to the multi's cpool for shutdown handling */
  705. infof(data, "%s connection #%" FMT_OFF_T,
  706. aborted ? "closing" : "shutting down", conn->connection_id);
  707. cpool_discard_conn(&data->multi->cpool, data, conn, aborted);
  708. }
  709. else {
  710. /* No multi available. Make a best-effort shutdown + close */
  711. infof(data, "closing connection #%" FMT_OFF_T, conn->connection_id);
  712. cpool_close_and_destroy(NULL, conn, data, !aborted);
  713. }
  714. if(do_lock)
  715. CPOOL_UNLOCK(cpool);
  716. }
  717. static void cpool_run_conn_shutdown_handler(struct Curl_easy *data,
  718. struct connectdata *conn)
  719. {
  720. if(!conn->bits.shutdown_handler) {
  721. if(conn->dns_entry)
  722. Curl_resolv_unlink(data, &conn->dns_entry);
  723. /* Cleanup NTLM connection-related data */
  724. Curl_http_auth_cleanup_ntlm(conn);
  725. /* Cleanup NEGOTIATE connection-related data */
  726. Curl_http_auth_cleanup_negotiate(conn);
  727. if(conn->handler && conn->handler->disconnect) {
  728. /* This is set if protocol-specific cleanups should be made */
  729. DEBUGF(infof(data, "connection #%" FMT_OFF_T
  730. ", shutdown protocol handler (aborted=%d)",
  731. conn->connection_id, conn->bits.aborted));
  732. conn->handler->disconnect(data, conn, conn->bits.aborted);
  733. }
  734. /* possible left-overs from the async name resolvers */
  735. Curl_resolver_cancel(data);
  736. conn->bits.shutdown_handler = TRUE;
  737. }
  738. }
  739. static void cpool_run_conn_shutdown(struct Curl_easy *data,
  740. struct connectdata *conn,
  741. bool *done)
  742. {
  743. CURLcode r1, r2;
  744. bool done1, done2;
  745. /* We expect to be attached when called */
  746. DEBUGASSERT(data->conn == conn);
  747. cpool_run_conn_shutdown_handler(data, conn);
  748. if(conn->bits.shutdown_filters) {
  749. *done = TRUE;
  750. return;
  751. }
  752. if(!conn->connect_only && Curl_conn_is_connected(conn, FIRSTSOCKET))
  753. r1 = Curl_conn_shutdown(data, FIRSTSOCKET, &done1);
  754. else {
  755. r1 = CURLE_OK;
  756. done1 = TRUE;
  757. }
  758. if(!conn->connect_only && Curl_conn_is_connected(conn, SECONDARYSOCKET))
  759. r2 = Curl_conn_shutdown(data, SECONDARYSOCKET, &done2);
  760. else {
  761. r2 = CURLE_OK;
  762. done2 = TRUE;
  763. }
  764. /* we are done when any failed or both report success */
  765. *done = (r1 || r2 || (done1 && done2));
  766. if(*done)
  767. conn->bits.shutdown_filters = TRUE;
  768. }
  769. static CURLcode cpool_add_pollfds(struct cpool *cpool,
  770. struct curl_pollfds *cpfds)
  771. {
  772. CURLcode result = CURLE_OK;
  773. if(Curl_llist_head(&cpool->shutdowns)) {
  774. struct Curl_llist_node *e;
  775. struct easy_pollset ps;
  776. struct connectdata *conn;
  777. for(e = Curl_llist_head(&cpool->shutdowns); e;
  778. e = Curl_node_next(e)) {
  779. conn = Curl_node_elem(e);
  780. memset(&ps, 0, sizeof(ps));
  781. Curl_attach_connection(cpool->idata, conn);
  782. Curl_conn_adjust_pollset(cpool->idata, &ps);
  783. Curl_detach_connection(cpool->idata);
  784. result = Curl_pollfds_add_ps(cpfds, &ps);
  785. if(result) {
  786. Curl_pollfds_cleanup(cpfds);
  787. goto out;
  788. }
  789. }
  790. }
  791. out:
  792. return result;
  793. }
  794. CURLcode Curl_cpool_add_pollfds(struct cpool *cpool,
  795. struct curl_pollfds *cpfds)
  796. {
  797. CURLcode result;
  798. CPOOL_LOCK(cpool);
  799. result = cpool_add_pollfds(cpool, cpfds);
  800. CPOOL_UNLOCK(cpool);
  801. return result;
  802. }
  803. CURLcode Curl_cpool_add_waitfds(struct cpool *cpool,
  804. struct curl_waitfds *cwfds)
  805. {
  806. CURLcode result = CURLE_OK;
  807. CPOOL_LOCK(cpool);
  808. if(Curl_llist_head(&cpool->shutdowns)) {
  809. struct Curl_llist_node *e;
  810. struct easy_pollset ps;
  811. struct connectdata *conn;
  812. for(e = Curl_llist_head(&cpool->shutdowns); e;
  813. e = Curl_node_next(e)) {
  814. conn = Curl_node_elem(e);
  815. memset(&ps, 0, sizeof(ps));
  816. Curl_attach_connection(cpool->idata, conn);
  817. Curl_conn_adjust_pollset(cpool->idata, &ps);
  818. Curl_detach_connection(cpool->idata);
  819. result = Curl_waitfds_add_ps(cwfds, &ps);
  820. if(result)
  821. goto out;
  822. }
  823. }
  824. out:
  825. CPOOL_UNLOCK(cpool);
  826. return result;
  827. }
  828. static void cpool_perform(struct cpool *cpool)
  829. {
  830. struct Curl_easy *data = cpool->idata;
  831. struct Curl_llist_node *e = Curl_llist_head(&cpool->shutdowns);
  832. struct Curl_llist_node *enext;
  833. struct connectdata *conn;
  834. struct curltime *nowp = NULL;
  835. struct curltime now;
  836. timediff_t next_from_now_ms = 0, ms;
  837. bool done;
  838. if(!e)
  839. return;
  840. DEBUGASSERT(data);
  841. DEBUGF(infof(data, "[CCACHE] perform, %zu connections being shutdown",
  842. Curl_llist_count(&cpool->shutdowns)));
  843. while(e) {
  844. enext = Curl_node_next(e);
  845. conn = Curl_node_elem(e);
  846. Curl_attach_connection(data, conn);
  847. cpool_run_conn_shutdown(data, conn, &done);
  848. DEBUGF(infof(data, "[CCACHE] shutdown #%" FMT_OFF_T ", done=%d",
  849. conn->connection_id, done));
  850. Curl_detach_connection(data);
  851. if(done) {
  852. Curl_node_remove(e);
  853. cpool_close_and_destroy(cpool, conn, NULL, FALSE);
  854. }
  855. else {
  856. /* Not done, when does this connection time out? */
  857. if(!nowp) {
  858. now = Curl_now();
  859. nowp = &now;
  860. }
  861. ms = Curl_conn_shutdown_timeleft(conn, nowp);
  862. if(ms && ms < next_from_now_ms)
  863. next_from_now_ms = ms;
  864. }
  865. e = enext;
  866. }
  867. if(next_from_now_ms)
  868. Curl_expire(data, next_from_now_ms, EXPIRE_RUN_NOW);
  869. }
  870. void Curl_cpool_multi_perform(struct Curl_multi *multi)
  871. {
  872. CPOOL_LOCK(&multi->cpool);
  873. cpool_perform(&multi->cpool);
  874. CPOOL_UNLOCK(&multi->cpool);
  875. }
  876. /*
  877. * Close and destroy the connection. Run the shutdown sequence once,
  878. * of so requested.
  879. */
  880. static void cpool_close_and_destroy(struct cpool *cpool,
  881. struct connectdata *conn,
  882. struct Curl_easy *data,
  883. bool do_shutdown)
  884. {
  885. bool done;
  886. /* there must be a connection to close */
  887. DEBUGASSERT(conn);
  888. /* it must be removed from the connection pool */
  889. DEBUGASSERT(!conn->bits.in_cpool);
  890. /* there must be an associated transfer */
  891. DEBUGASSERT(data || cpool);
  892. if(!data)
  893. data = cpool->idata;
  894. /* the transfer must be detached from the connection */
  895. DEBUGASSERT(data && !data->conn);
  896. Curl_attach_connection(data, conn);
  897. cpool_run_conn_shutdown_handler(data, conn);
  898. if(do_shutdown) {
  899. /* Make a last attempt to shutdown handlers and filters, if
  900. * not done so already. */
  901. cpool_run_conn_shutdown(data, conn, &done);
  902. }
  903. if(cpool)
  904. DEBUGF(infof(data, "[CCACHE] closing #%" FMT_OFF_T,
  905. conn->connection_id));
  906. else
  907. DEBUGF(infof(data, "closing connection #%" FMT_OFF_T,
  908. conn->connection_id));
  909. Curl_conn_close(data, SECONDARYSOCKET);
  910. Curl_conn_close(data, FIRSTSOCKET);
  911. Curl_detach_connection(data);
  912. Curl_conn_free(data, conn);
  913. }
  914. static CURLMcode cpool_update_shutdown_ev(struct Curl_multi *multi,
  915. struct Curl_easy *data,
  916. struct connectdata *conn)
  917. {
  918. struct easy_pollset ps;
  919. CURLMcode mresult;
  920. DEBUGASSERT(data);
  921. DEBUGASSERT(multi);
  922. DEBUGASSERT(multi->socket_cb);
  923. memset(&ps, 0, sizeof(ps));
  924. Curl_attach_connection(data, conn);
  925. Curl_conn_adjust_pollset(data, &ps);
  926. Curl_detach_connection(data);
  927. mresult = Curl_multi_pollset_ev(multi, data, &ps, &conn->shutdown_poll);
  928. if(!mresult) /* Remember for next time */
  929. memcpy(&conn->shutdown_poll, &ps, sizeof(ps));
  930. return mresult;
  931. }
  932. void Curl_cpool_multi_socket(struct Curl_multi *multi,
  933. curl_socket_t s, int ev_bitmask)
  934. {
  935. struct cpool *cpool = &multi->cpool;
  936. struct Curl_easy *data = cpool->idata;
  937. struct Curl_llist_node *e;
  938. struct connectdata *conn;
  939. bool done;
  940. (void)ev_bitmask;
  941. DEBUGASSERT(multi->socket_cb);
  942. CPOOL_LOCK(cpool);
  943. e = Curl_llist_head(&cpool->shutdowns);
  944. while(e) {
  945. conn = Curl_node_elem(e);
  946. if(s == conn->sock[FIRSTSOCKET] || s == conn->sock[SECONDARYSOCKET]) {
  947. Curl_attach_connection(data, conn);
  948. cpool_run_conn_shutdown(data, conn, &done);
  949. DEBUGF(infof(data, "[CCACHE] shutdown #%" FMT_OFF_T ", done=%d",
  950. conn->connection_id, done));
  951. Curl_detach_connection(data);
  952. if(done || cpool_update_shutdown_ev(multi, data, conn)) {
  953. Curl_node_remove(e);
  954. cpool_close_and_destroy(cpool, conn, NULL, FALSE);
  955. }
  956. break;
  957. }
  958. e = Curl_node_next(e);
  959. }
  960. CPOOL_UNLOCK(cpool);
  961. }
  962. #define NUM_POLLS_ON_STACK 10
  963. static CURLcode cpool_shutdown_wait(struct cpool *cpool, int timeout_ms)
  964. {
  965. struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
  966. struct curl_pollfds cpfds;
  967. CURLcode result;
  968. Curl_pollfds_init(&cpfds, a_few_on_stack, NUM_POLLS_ON_STACK);
  969. result = cpool_add_pollfds(cpool, &cpfds);
  970. if(result)
  971. goto out;
  972. Curl_poll(cpfds.pfds, cpfds.n, CURLMIN(timeout_ms, 1000));
  973. out:
  974. Curl_pollfds_cleanup(&cpfds);
  975. return result;
  976. }
  977. static void cpool_shutdown_all(struct cpool *cpool,
  978. struct Curl_easy *data, int timeout_ms)
  979. {
  980. struct connectdata *conn;
  981. struct curltime started = Curl_now();
  982. if(!data)
  983. return;
  984. (void)data;
  985. DEBUGF(infof(data, "cpool shutdown all"));
  986. /* Move all connections into the shutdown queue */
  987. for(conn = cpool_get_live_conn(cpool); conn;
  988. conn = cpool_get_live_conn(cpool)) {
  989. /* Move conn from live set to shutdown or destroy right away */
  990. DEBUGF(infof(data, "moving connection #%" FMT_OFF_T
  991. " to shutdown queue", conn->connection_id));
  992. cpool_remove_conn(cpool, conn);
  993. cpool_discard_conn(cpool, data, conn, FALSE);
  994. }
  995. while(Curl_llist_head(&cpool->shutdowns)) {
  996. timediff_t timespent;
  997. int remain_ms;
  998. cpool_perform(cpool);
  999. if(!Curl_llist_head(&cpool->shutdowns)) {
  1000. DEBUGF(infof(data, "cpool shutdown ok"));
  1001. break;
  1002. }
  1003. /* wait for activity, timeout or "nothing" */
  1004. timespent = Curl_timediff(Curl_now(), started);
  1005. if(timespent >= (timediff_t)timeout_ms) {
  1006. DEBUGF(infof(data, "cpool shutdown %s",
  1007. (timeout_ms > 0) ? "timeout" : "best effort done"));
  1008. break;
  1009. }
  1010. remain_ms = timeout_ms - (int)timespent;
  1011. if(cpool_shutdown_wait(cpool, remain_ms)) {
  1012. DEBUGF(infof(data, "cpool shutdown all, abort"));
  1013. break;
  1014. }
  1015. }
  1016. /* Due to errors/timeout, we might come here without being done. */
  1017. cpool_shutdown_discard_all(cpool);
  1018. }
  1019. struct cpool_reaper_ctx {
  1020. struct curltime now;
  1021. };
  1022. static int cpool_reap_dead_cb(struct Curl_easy *data,
  1023. struct connectdata *conn, void *param)
  1024. {
  1025. struct cpool_reaper_ctx *rctx = param;
  1026. if(Curl_conn_seems_dead(conn, data, &rctx->now)) {
  1027. /* stop the iteration here, pass back the connection that was pruned */
  1028. Curl_cpool_disconnect(data, conn, FALSE);
  1029. return 1;
  1030. }
  1031. return 0; /* continue iteration */
  1032. }
  1033. /*
  1034. * This function scans the data's connection pool for half-open/dead
  1035. * connections, closes and removes them.
  1036. * The cleanup is done at most once per second.
  1037. *
  1038. * When called, this transfer has no connection attached.
  1039. */
  1040. void Curl_cpool_prune_dead(struct Curl_easy *data)
  1041. {
  1042. struct cpool *cpool = cpool_get_instance(data);
  1043. struct cpool_reaper_ctx rctx;
  1044. timediff_t elapsed;
  1045. if(!cpool)
  1046. return;
  1047. rctx.now = Curl_now();
  1048. CPOOL_LOCK(cpool);
  1049. elapsed = Curl_timediff(rctx.now, cpool->last_cleanup);
  1050. if(elapsed >= 1000L) {
  1051. while(cpool_foreach(data, cpool, &rctx, cpool_reap_dead_cb))
  1052. ;
  1053. cpool->last_cleanup = rctx.now;
  1054. }
  1055. CPOOL_UNLOCK(cpool);
  1056. }
  1057. static int conn_upkeep(struct Curl_easy *data,
  1058. struct connectdata *conn,
  1059. void *param)
  1060. {
  1061. struct curltime *now = param;
  1062. /* TODO, shall we reap connections that return an error here? */
  1063. Curl_conn_upkeep(data, conn, now);
  1064. return 0; /* continue iteration */
  1065. }
  1066. CURLcode Curl_cpool_upkeep(void *data)
  1067. {
  1068. struct cpool *cpool = cpool_get_instance(data);
  1069. struct curltime now = Curl_now();
  1070. if(!cpool)
  1071. return CURLE_OK;
  1072. CPOOL_LOCK(cpool);
  1073. cpool_foreach(data, cpool, &now, conn_upkeep);
  1074. CPOOL_UNLOCK(cpool);
  1075. return CURLE_OK;
  1076. }
  1077. struct cpool_find_ctx {
  1078. curl_off_t id;
  1079. struct connectdata *conn;
  1080. };
  1081. static int cpool_find_conn(struct Curl_easy *data,
  1082. struct connectdata *conn, void *param)
  1083. {
  1084. struct cpool_find_ctx *fctx = param;
  1085. (void)data;
  1086. if(conn->connection_id == fctx->id) {
  1087. fctx->conn = conn;
  1088. return 1;
  1089. }
  1090. return 0;
  1091. }
  1092. struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data,
  1093. curl_off_t conn_id)
  1094. {
  1095. struct cpool *cpool = cpool_get_instance(data);
  1096. struct cpool_find_ctx fctx;
  1097. if(!cpool)
  1098. return NULL;
  1099. fctx.id = conn_id;
  1100. fctx.conn = NULL;
  1101. CPOOL_LOCK(cpool);
  1102. cpool_foreach(cpool->idata, cpool, &fctx, cpool_find_conn);
  1103. CPOOL_UNLOCK(cpool);
  1104. return fctx.conn;
  1105. }
  1106. struct cpool_do_conn_ctx {
  1107. curl_off_t id;
  1108. Curl_cpool_conn_do_cb *cb;
  1109. void *cbdata;
  1110. };
  1111. static int cpool_do_conn(struct Curl_easy *data,
  1112. struct connectdata *conn, void *param)
  1113. {
  1114. struct cpool_do_conn_ctx *dctx = param;
  1115. (void)data;
  1116. if(conn->connection_id == dctx->id) {
  1117. dctx->cb(conn, data, dctx->cbdata);
  1118. return 1;
  1119. }
  1120. return 0;
  1121. }
  1122. void Curl_cpool_do_by_id(struct Curl_easy *data, curl_off_t conn_id,
  1123. Curl_cpool_conn_do_cb *cb, void *cbdata)
  1124. {
  1125. struct cpool *cpool = cpool_get_instance(data);
  1126. struct cpool_do_conn_ctx dctx;
  1127. if(!cpool)
  1128. return;
  1129. dctx.id = conn_id;
  1130. dctx.cb = cb;
  1131. dctx.cbdata = cbdata;
  1132. CPOOL_LOCK(cpool);
  1133. cpool_foreach(data, cpool, &dctx, cpool_do_conn);
  1134. CPOOL_UNLOCK(cpool);
  1135. }
  1136. void Curl_cpool_do_locked(struct Curl_easy *data,
  1137. struct connectdata *conn,
  1138. Curl_cpool_conn_do_cb *cb, void *cbdata)
  1139. {
  1140. struct cpool *cpool = cpool_get_instance(data);
  1141. if(cpool) {
  1142. CPOOL_LOCK(cpool);
  1143. cb(conn, data, cbdata);
  1144. CPOOL_UNLOCK(cpool);
  1145. }
  1146. else
  1147. cb(conn, data, cbdata);
  1148. }
  1149. #if 0
  1150. /* Useful for debugging the connection pool */
  1151. void Curl_cpool_print(struct cpool *cpool)
  1152. {
  1153. struct Curl_hash_iterator iter;
  1154. struct Curl_llist_node *curr;
  1155. struct Curl_hash_element *he;
  1156. if(!cpool)
  1157. return;
  1158. fprintf(stderr, "=Bundle cache=\n");
  1159. Curl_hash_start_iterate(cpool->dest2bundle, &iter);
  1160. he = Curl_hash_next_element(&iter);
  1161. while(he) {
  1162. struct cpool_bundle *bundle;
  1163. struct connectdata *conn;
  1164. bundle = he->ptr;
  1165. fprintf(stderr, "%s -", he->key);
  1166. curr = Curl_llist_head(bundle->conns);
  1167. while(curr) {
  1168. conn = Curl_node_elem(curr);
  1169. fprintf(stderr, " [%p %d]", (void *)conn, conn->refcount);
  1170. curr = Curl_node_next(curr);
  1171. }
  1172. fprintf(stderr, "\n");
  1173. he = Curl_hash_next_element(&iter);
  1174. }
  1175. }
  1176. #endif