conncache.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012, Linus Nielsen Feltzing, <linus@haxx.se>
  9. * Copyright (C) 2012 - 2013, 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. connc->num_connections--;
  139. DEBUGF(infof(conn->data, "The cache now contains %d members\n",
  140. connc->num_connections));
  141. }
  142. }
  143. /* This function iterates the entire connection cache and calls the
  144. function func() with the connection pointer as the first argument
  145. and the supplied 'param' argument as the other,
  146. Return 0 from func() to continue the loop, return 1 to abort it.
  147. */
  148. void Curl_conncache_foreach(struct conncache *connc,
  149. void *param,
  150. int (*func)(struct connectdata *conn, void *param))
  151. {
  152. struct curl_hash_iterator iter;
  153. struct curl_llist_element *curr;
  154. struct curl_hash_element *he;
  155. if(!connc)
  156. return;
  157. Curl_hash_start_iterate(connc->hash, &iter);
  158. he = Curl_hash_next_element(&iter);
  159. while(he) {
  160. struct connectbundle *bundle;
  161. struct connectdata *conn;
  162. bundle = he->ptr;
  163. curr = bundle->conn_list->head;
  164. while(curr) {
  165. /* Yes, we need to update curr before calling func(), because func()
  166. might decide to remove the connection */
  167. conn = curr->ptr;
  168. curr = curr->next;
  169. if(1 == func(conn, param))
  170. return;
  171. }
  172. he = Curl_hash_next_element(&iter);
  173. }
  174. }
  175. /* Return the first connection found in the cache. Used when closing all
  176. connections */
  177. struct connectdata *
  178. Curl_conncache_find_first_connection(struct conncache *connc)
  179. {
  180. struct curl_hash_iterator iter;
  181. struct curl_llist_element *curr;
  182. struct curl_hash_element *he;
  183. struct connectbundle *bundle;
  184. Curl_hash_start_iterate(connc->hash, &iter);
  185. he = Curl_hash_next_element(&iter);
  186. while(he) {
  187. bundle = he->ptr;
  188. curr = bundle->conn_list->head;
  189. if(curr) {
  190. return curr->ptr;
  191. }
  192. he = Curl_hash_next_element(&iter);
  193. }
  194. return NULL;
  195. }
  196. #if 0
  197. /* Useful for debugging the connection cache */
  198. void Curl_conncache_print(struct conncache *connc)
  199. {
  200. struct curl_hash_iterator iter;
  201. struct curl_llist_element *curr;
  202. struct curl_hash_element *he;
  203. if(!connc)
  204. return;
  205. fprintf(stderr, "=Bundle cache=\n");
  206. Curl_hash_start_iterate(connc->hash, &iter);
  207. he = Curl_hash_next_element(&iter);
  208. while(he) {
  209. struct connectbundle *bundle;
  210. struct connectdata *conn;
  211. bundle = he->ptr;
  212. fprintf(stderr, "%s -", he->key);
  213. curr = bundle->conn_list->head;
  214. while(curr) {
  215. conn = curr->ptr;
  216. fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
  217. curr = curr->next;
  218. }
  219. fprintf(stderr, "\n");
  220. he = Curl_hash_next_element(&iter);
  221. }
  222. }
  223. #endif