conncache.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012, Linus Nielsen Feltzing, <linus@haxx.se>
  9. * Copyright (C) 2012 - 2014, 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 http://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 "rawstr.h"
  31. #include "bundles.h"
  32. #include "conncache.h"
  33. #include "curl_memory.h"
  34. /* The last #include file should be: */
  35. #include "memdebug.h"
  36. static void free_bundle_hash_entry(void *freethis)
  37. {
  38. struct connectbundle *b = (struct connectbundle *) freethis;
  39. Curl_bundle_destroy(b);
  40. }
  41. struct conncache *Curl_conncache_init(int size)
  42. {
  43. struct conncache *connc;
  44. connc = calloc(1, sizeof(struct conncache));
  45. if(!connc)
  46. return NULL;
  47. connc->hash = Curl_hash_alloc(size, Curl_hash_str,
  48. Curl_str_key_compare, free_bundle_hash_entry);
  49. if(!connc->hash) {
  50. free(connc);
  51. return NULL;
  52. }
  53. return connc;
  54. }
  55. void Curl_conncache_destroy(struct conncache *connc)
  56. {
  57. if(connc) {
  58. Curl_hash_destroy(connc->hash);
  59. connc->hash = NULL;
  60. free(connc);
  61. }
  62. }
  63. struct connectbundle *Curl_conncache_find_bundle(struct conncache *connc,
  64. char *hostname)
  65. {
  66. struct connectbundle *bundle = NULL;
  67. if(connc)
  68. bundle = Curl_hash_pick(connc->hash, hostname, strlen(hostname)+1);
  69. return bundle;
  70. }
  71. static bool conncache_add_bundle(struct conncache *connc,
  72. char *hostname,
  73. struct connectbundle *bundle)
  74. {
  75. void *p;
  76. p = Curl_hash_add(connc->hash, hostname, strlen(hostname)+1, bundle);
  77. return p?TRUE:FALSE;
  78. }
  79. static void conncache_remove_bundle(struct conncache *connc,
  80. struct connectbundle *bundle)
  81. {
  82. struct curl_hash_iterator iter;
  83. struct curl_hash_element *he;
  84. if(!connc)
  85. return;
  86. Curl_hash_start_iterate(connc->hash, &iter);
  87. he = Curl_hash_next_element(&iter);
  88. while(he) {
  89. if(he->ptr == bundle) {
  90. /* The bundle is destroyed by the hash destructor function,
  91. free_bundle_hash_entry() */
  92. Curl_hash_delete(connc->hash, he->key, he->key_len);
  93. return;
  94. }
  95. he = Curl_hash_next_element(&iter);
  96. }
  97. }
  98. CURLcode Curl_conncache_add_conn(struct conncache *connc,
  99. struct connectdata *conn)
  100. {
  101. CURLcode result;
  102. struct connectbundle *bundle;
  103. struct connectbundle *new_bundle = NULL;
  104. struct SessionHandle *data = conn->data;
  105. bundle = Curl_conncache_find_bundle(data->state.conn_cache,
  106. conn->host.name);
  107. if(!bundle) {
  108. result = Curl_bundle_create(data, &new_bundle);
  109. if(result)
  110. return result;
  111. if(!conncache_add_bundle(data->state.conn_cache,
  112. conn->host.name, new_bundle)) {
  113. Curl_bundle_destroy(new_bundle);
  114. return CURLE_OUT_OF_MEMORY;
  115. }
  116. bundle = new_bundle;
  117. }
  118. result = Curl_bundle_add_conn(bundle, conn);
  119. if(result) {
  120. if(new_bundle)
  121. conncache_remove_bundle(data->state.conn_cache, new_bundle);
  122. return result;
  123. }
  124. conn->connection_id = connc->next_connection_id++;
  125. connc->num_connections++;
  126. return CURLE_OK;
  127. }
  128. void Curl_conncache_remove_conn(struct conncache *connc,
  129. struct connectdata *conn)
  130. {
  131. struct connectbundle *bundle = conn->bundle;
  132. /* The bundle pointer can be NULL, since this function can be called
  133. due to a failed connection attempt, before being added to a bundle */
  134. if(bundle) {
  135. Curl_bundle_remove_conn(bundle, conn);
  136. if(bundle->num_connections == 0) {
  137. conncache_remove_bundle(connc, bundle);
  138. }
  139. if(connc) {
  140. connc->num_connections--;
  141. DEBUGF(infof(conn->data, "The cache now contains %d members\n",
  142. connc->num_connections));
  143. }
  144. }
  145. }
  146. /* This function iterates the entire connection cache and calls the
  147. function func() with the connection pointer as the first argument
  148. and the supplied 'param' argument as the other,
  149. Return 0 from func() to continue the loop, return 1 to abort it.
  150. */
  151. void Curl_conncache_foreach(struct conncache *connc,
  152. void *param,
  153. int (*func)(struct connectdata *conn, void *param))
  154. {
  155. struct curl_hash_iterator iter;
  156. struct curl_llist_element *curr;
  157. struct curl_hash_element *he;
  158. if(!connc)
  159. return;
  160. Curl_hash_start_iterate(connc->hash, &iter);
  161. he = Curl_hash_next_element(&iter);
  162. while(he) {
  163. struct connectbundle *bundle;
  164. bundle = he->ptr;
  165. he = Curl_hash_next_element(&iter);
  166. curr = bundle->conn_list->head;
  167. while(curr) {
  168. /* Yes, we need to update curr before calling func(), because func()
  169. might decide to remove the connection */
  170. struct connectdata *conn = curr->ptr;
  171. curr = curr->next;
  172. if(1 == func(conn, param))
  173. return;
  174. }
  175. }
  176. }
  177. /* Return the first connection found in the cache. Used when closing all
  178. connections */
  179. struct connectdata *
  180. Curl_conncache_find_first_connection(struct conncache *connc)
  181. {
  182. struct curl_hash_iterator iter;
  183. struct curl_hash_element *he;
  184. struct connectbundle *bundle;
  185. Curl_hash_start_iterate(connc->hash, &iter);
  186. he = Curl_hash_next_element(&iter);
  187. while(he) {
  188. struct curl_llist_element *curr;
  189. bundle = he->ptr;
  190. curr = bundle->conn_list->head;
  191. if(curr) {
  192. return curr->ptr;
  193. }
  194. he = Curl_hash_next_element(&iter);
  195. }
  196. return NULL;
  197. }
  198. #if 0
  199. /* Useful for debugging the connection cache */
  200. void Curl_conncache_print(struct conncache *connc)
  201. {
  202. struct curl_hash_iterator iter;
  203. struct curl_llist_element *curr;
  204. struct curl_hash_element *he;
  205. if(!connc)
  206. return;
  207. fprintf(stderr, "=Bundle cache=\n");
  208. Curl_hash_start_iterate(connc->hash, &iter);
  209. he = Curl_hash_next_element(&iter);
  210. while(he) {
  211. struct connectbundle *bundle;
  212. struct connectdata *conn;
  213. bundle = he->ptr;
  214. fprintf(stderr, "%s -", he->key);
  215. curr = bundle->conn_list->head;
  216. while(curr) {
  217. conn = curr->ptr;
  218. fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
  219. curr = curr->next;
  220. }
  221. fprintf(stderr, "\n");
  222. he = Curl_hash_next_element(&iter);
  223. }
  224. }
  225. #endif