conncache.c 9.4 KB

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