conncache.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012, 2016, Linus Nielsen Feltzing, <[email protected]>
  9. * Copyright (C) 2012 - 2015, Daniel Stenberg, <[email protected]>, 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.haxx.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. ***************************************************************************/
  23. #include "curl_setup.h"
  24. #include <curl/curl.h>
  25. #include "urldata.h"
  26. #include "url.h"
  27. #include "progress.h"
  28. #include "multiif.h"
  29. #include "sendf.h"
  30. #include "conncache.h"
  31. /* The last 3 #include files should be in this order */
  32. #include "curl_printf.h"
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. static void conn_llist_dtor(void *user, void *element)
  36. {
  37. struct connectdata *data = element;
  38. (void)user;
  39. data->bundle = NULL;
  40. }
  41. static CURLcode bundle_create(struct Curl_easy *data,
  42. struct connectbundle **cb_ptr)
  43. {
  44. (void)data;
  45. DEBUGASSERT(*cb_ptr == NULL);
  46. *cb_ptr = malloc(sizeof(struct connectbundle));
  47. if(!*cb_ptr)
  48. return CURLE_OUT_OF_MEMORY;
  49. (*cb_ptr)->num_connections = 0;
  50. (*cb_ptr)->multiuse = BUNDLE_UNKNOWN;
  51. (*cb_ptr)->conn_list = Curl_llist_alloc((curl_llist_dtor) conn_llist_dtor);
  52. if(!(*cb_ptr)->conn_list) {
  53. Curl_safefree(*cb_ptr);
  54. return CURLE_OUT_OF_MEMORY;
  55. }
  56. return CURLE_OK;
  57. }
  58. static void bundle_destroy(struct connectbundle *cb_ptr)
  59. {
  60. if(!cb_ptr)
  61. return;
  62. if(cb_ptr->conn_list) {
  63. Curl_llist_destroy(cb_ptr->conn_list, NULL);
  64. cb_ptr->conn_list = NULL;
  65. }
  66. free(cb_ptr);
  67. }
  68. /* Add a connection to a bundle */
  69. static CURLcode bundle_add_conn(struct connectbundle *cb_ptr,
  70. struct connectdata *conn)
  71. {
  72. if(!Curl_llist_insert_next(cb_ptr->conn_list, cb_ptr->conn_list->tail, conn))
  73. return CURLE_OUT_OF_MEMORY;
  74. conn->bundle = cb_ptr;
  75. cb_ptr->num_connections++;
  76. return CURLE_OK;
  77. }
  78. /* Remove a connection from a bundle */
  79. static int bundle_remove_conn(struct connectbundle *cb_ptr,
  80. struct connectdata *conn)
  81. {
  82. struct curl_llist_element *curr;
  83. curr = cb_ptr->conn_list->head;
  84. while(curr) {
  85. if(curr->ptr == conn) {
  86. Curl_llist_remove(cb_ptr->conn_list, curr, NULL);
  87. cb_ptr->num_connections--;
  88. conn->bundle = NULL;
  89. return 1; /* we removed a handle */
  90. }
  91. curr = curr->next;
  92. }
  93. return 0;
  94. }
  95. static void free_bundle_hash_entry(void *freethis)
  96. {
  97. struct connectbundle *b = (struct connectbundle *) freethis;
  98. bundle_destroy(b);
  99. }
  100. int Curl_conncache_init(struct conncache *connc, int size)
  101. {
  102. return Curl_hash_init(&connc->hash, size, Curl_hash_str,
  103. Curl_str_key_compare, free_bundle_hash_entry);
  104. }
  105. void Curl_conncache_destroy(struct conncache *connc)
  106. {
  107. if(connc)
  108. Curl_hash_destroy(&connc->hash);
  109. }
  110. /* returns an allocated key to find a bundle for this connection */
  111. static char *hashkey(struct connectdata *conn)
  112. {
  113. const char *hostname;
  114. if(conn->bits.socksproxy)
  115. hostname = conn->socks_proxy.host.name;
  116. else if(conn->bits.httpproxy)
  117. hostname = conn->http_proxy.host.name;
  118. else if(conn->bits.conn_to_host)
  119. hostname = conn->conn_to_host.name;
  120. else
  121. hostname = conn->host.name;
  122. return aprintf("%s:%ld", hostname, conn->port);
  123. }
  124. /* Look up the bundle with all the connections to the same host this
  125. connectdata struct is setup to use. */
  126. struct connectbundle *Curl_conncache_find_bundle(struct connectdata *conn,
  127. struct conncache *connc)
  128. {
  129. struct connectbundle *bundle = NULL;
  130. if(connc) {
  131. char *key = hashkey(conn);
  132. if(key) {
  133. bundle = Curl_hash_pick(&connc->hash, key, strlen(key));
  134. free(key);
  135. }
  136. }
  137. return bundle;
  138. }
  139. static bool conncache_add_bundle(struct conncache *connc,
  140. char *key,
  141. struct connectbundle *bundle)
  142. {
  143. void *p = Curl_hash_add(&connc->hash, key, strlen(key), bundle);
  144. return p?TRUE:FALSE;
  145. }
  146. static void conncache_remove_bundle(struct conncache *connc,
  147. struct connectbundle *bundle)
  148. {
  149. struct curl_hash_iterator iter;
  150. struct curl_hash_element *he;
  151. if(!connc)
  152. return;
  153. Curl_hash_start_iterate(&connc->hash, &iter);
  154. he = Curl_hash_next_element(&iter);
  155. while(he) {
  156. if(he->ptr == bundle) {
  157. /* The bundle is destroyed by the hash destructor function,
  158. free_bundle_hash_entry() */
  159. Curl_hash_delete(&connc->hash, he->key, he->key_len);
  160. return;
  161. }
  162. he = Curl_hash_next_element(&iter);
  163. }
  164. }
  165. CURLcode Curl_conncache_add_conn(struct conncache *connc,
  166. struct connectdata *conn)
  167. {
  168. CURLcode result;
  169. struct connectbundle *bundle;
  170. struct connectbundle *new_bundle = NULL;
  171. struct Curl_easy *data = conn->data;
  172. bundle = Curl_conncache_find_bundle(conn, data->state.conn_cache);
  173. if(!bundle) {
  174. char *key;
  175. int rc;
  176. result = bundle_create(data, &new_bundle);
  177. if(result)
  178. return result;
  179. key = hashkey(conn);
  180. if(!key) {
  181. bundle_destroy(new_bundle);
  182. return CURLE_OUT_OF_MEMORY;
  183. }
  184. rc = conncache_add_bundle(data->state.conn_cache, key, new_bundle);
  185. free(key);
  186. if(!rc) {
  187. bundle_destroy(new_bundle);
  188. return CURLE_OUT_OF_MEMORY;
  189. }
  190. bundle = new_bundle;
  191. }
  192. result = bundle_add_conn(bundle, conn);
  193. if(result) {
  194. if(new_bundle)
  195. conncache_remove_bundle(data->state.conn_cache, new_bundle);
  196. return result;
  197. }
  198. conn->connection_id = connc->next_connection_id++;
  199. connc->num_connections++;
  200. DEBUGF(infof(conn->data, "Added connection %ld. "
  201. "The cache now contains %" CURL_FORMAT_CURL_OFF_TU " members\n",
  202. conn->connection_id, (curl_off_t) connc->num_connections));
  203. return CURLE_OK;
  204. }
  205. void Curl_conncache_remove_conn(struct conncache *connc,
  206. struct connectdata *conn)
  207. {
  208. struct connectbundle *bundle = conn->bundle;
  209. /* The bundle pointer can be NULL, since this function can be called
  210. due to a failed connection attempt, before being added to a bundle */
  211. if(bundle) {
  212. bundle_remove_conn(bundle, conn);
  213. if(bundle->num_connections == 0) {
  214. conncache_remove_bundle(connc, bundle);
  215. }
  216. if(connc) {
  217. connc->num_connections--;
  218. DEBUGF(infof(conn->data, "The cache now contains %"
  219. CURL_FORMAT_CURL_OFF_TU " members\n",
  220. (curl_off_t) connc->num_connections));
  221. }
  222. }
  223. }
  224. /* This function iterates the entire connection cache and calls the
  225. function func() with the connection pointer as the first argument
  226. and the supplied 'param' argument as the other,
  227. Return 0 from func() to continue the loop, return 1 to abort it.
  228. */
  229. void Curl_conncache_foreach(struct conncache *connc,
  230. void *param,
  231. int (*func)(struct connectdata *conn, void *param))
  232. {
  233. struct curl_hash_iterator iter;
  234. struct curl_llist_element *curr;
  235. struct curl_hash_element *he;
  236. if(!connc)
  237. return;
  238. Curl_hash_start_iterate(&connc->hash, &iter);
  239. he = Curl_hash_next_element(&iter);
  240. while(he) {
  241. struct connectbundle *bundle;
  242. bundle = he->ptr;
  243. he = Curl_hash_next_element(&iter);
  244. curr = bundle->conn_list->head;
  245. while(curr) {
  246. /* Yes, we need to update curr before calling func(), because func()
  247. might decide to remove the connection */
  248. struct connectdata *conn = curr->ptr;
  249. curr = curr->next;
  250. if(1 == func(conn, param))
  251. return;
  252. }
  253. }
  254. }
  255. /* Return the first connection found in the cache. Used when closing all
  256. connections */
  257. struct connectdata *
  258. Curl_conncache_find_first_connection(struct conncache *connc)
  259. {
  260. struct curl_hash_iterator iter;
  261. struct curl_hash_element *he;
  262. struct connectbundle *bundle;
  263. Curl_hash_start_iterate(&connc->hash, &iter);
  264. he = Curl_hash_next_element(&iter);
  265. while(he) {
  266. struct curl_llist_element *curr;
  267. bundle = he->ptr;
  268. curr = bundle->conn_list->head;
  269. if(curr) {
  270. return curr->ptr;
  271. }
  272. he = Curl_hash_next_element(&iter);
  273. }
  274. return NULL;
  275. }
  276. #if 0
  277. /* Useful for debugging the connection cache */
  278. void Curl_conncache_print(struct conncache *connc)
  279. {
  280. struct curl_hash_iterator iter;
  281. struct curl_llist_element *curr;
  282. struct curl_hash_element *he;
  283. if(!connc)
  284. return;
  285. fprintf(stderr, "=Bundle cache=\n");
  286. Curl_hash_start_iterate(connc->hash, &iter);
  287. he = Curl_hash_next_element(&iter);
  288. while(he) {
  289. struct connectbundle *bundle;
  290. struct connectdata *conn;
  291. bundle = he->ptr;
  292. fprintf(stderr, "%s -", he->key);
  293. curr = bundle->conn_list->head;
  294. while(curr) {
  295. conn = curr->ptr;
  296. fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
  297. curr = curr->next;
  298. }
  299. fprintf(stderr, "\n");
  300. he = Curl_hash_next_element(&iter);
  301. }
  302. }
  303. #endif