conncache.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 != CURLE_OK)
  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 != CURLE_OK) {
  120. if(new_bundle)
  121. conncache_remove_bundle(data->state.conn_cache, new_bundle);
  122. return result;
  123. }
  124. connc->num_connections++;
  125. return CURLE_OK;
  126. }
  127. void Curl_conncache_remove_conn(struct conncache *connc,
  128. struct connectdata *conn)
  129. {
  130. struct connectbundle *bundle = conn->bundle;
  131. /* The bundle pointer can be NULL, since this function can be called
  132. due to a failed connection attempt, before being added to a bundle */
  133. if(bundle) {
  134. Curl_bundle_remove_conn(bundle, conn);
  135. if(bundle->num_connections == 0) {
  136. conncache_remove_bundle(connc, bundle);
  137. }
  138. if(connc) {
  139. connc->num_connections--;
  140. DEBUGF(infof(conn->data, "The cache now contains %d members\n",
  141. connc->num_connections));
  142. }
  143. }
  144. }
  145. /* This function iterates the entire connection cache and calls the
  146. function func() with the connection pointer as the first argument
  147. and the supplied 'param' argument as the other,
  148. Return 0 from func() to continue the loop, return 1 to abort it.
  149. */
  150. void Curl_conncache_foreach(struct conncache *connc,
  151. void *param,
  152. int (*func)(struct connectdata *conn, void *param))
  153. {
  154. struct curl_hash_iterator iter;
  155. struct curl_llist_element *curr;
  156. struct curl_hash_element *he;
  157. if(!connc)
  158. return;
  159. Curl_hash_start_iterate(connc->hash, &iter);
  160. he = Curl_hash_next_element(&iter);
  161. while(he) {
  162. struct connectbundle *bundle;
  163. struct connectdata *conn;
  164. bundle = he->ptr;
  165. curr = bundle->conn_list->head;
  166. while(curr) {
  167. /* Yes, we need to update curr before calling func(), because func()
  168. might decide to remove the connection */
  169. conn = curr->ptr;
  170. curr = curr->next;
  171. if(1 == func(conn, param))
  172. return;
  173. }
  174. he = Curl_hash_next_element(&iter);
  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_llist_element *curr;
  184. struct curl_hash_element *he;
  185. struct connectbundle *bundle;
  186. Curl_hash_start_iterate(connc->hash, &iter);
  187. he = Curl_hash_next_element(&iter);
  188. while(he) {
  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