1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2012, Linus Nielsen Feltzing, <linus@haxx.se>
9 * Copyright (C) 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ***************************************************************************/
26 #include <curl/curl.h>
35 #include "conncache.h"
37 #include "curl_memory.h"
38 /* The last #include file should be: */
41 #define CONNECTION_HASH_SIZE 97
43 static void free_bundle_hash_entry(void *freethis)
45 struct connectbundle *b = (struct connectbundle *) freethis;
47 Curl_bundle_destroy(b);
50 struct conncache *Curl_conncache_init(conncachetype type)
52 struct conncache *connc;
54 connc = calloc(1, sizeof(struct conncache));
58 connc->hash = Curl_hash_alloc(CONNECTION_HASH_SIZE, Curl_hash_str,
59 Curl_str_key_compare, free_bundle_hash_entry);
67 connc->num_connections = 0;
72 void Curl_conncache_destroy(struct conncache *connc)
75 Curl_hash_destroy(connc->hash);
81 struct connectbundle *Curl_conncache_find_bundle(struct conncache *connc,
84 struct connectbundle *bundle = NULL;
87 bundle = Curl_hash_pick(connc->hash, hostname, strlen(hostname)+1);
92 static bool conncache_add_bundle(struct conncache *connc,
94 struct connectbundle *bundle)
98 p = Curl_hash_add(connc->hash, hostname, strlen(hostname)+1, bundle);
103 static void conncache_remove_bundle(struct conncache *connc,
104 struct connectbundle *bundle)
106 struct curl_hash_iterator iter;
107 struct curl_hash_element *he;
112 Curl_hash_start_iterate(connc->hash, &iter);
114 he = Curl_hash_next_element(&iter);
116 if(he->ptr == bundle) {
117 /* The bundle is destroyed by the hash destructor function,
118 free_bundle_hash_entry() */
119 Curl_hash_delete(connc->hash, he->key, he->key_len);
123 he = Curl_hash_next_element(&iter);
127 CURLcode Curl_conncache_add_conn(struct conncache *connc,
128 struct connectdata *conn)
131 struct connectbundle *bundle;
132 struct connectbundle *new_bundle = NULL;
133 struct SessionHandle *data = conn->data;
135 bundle = Curl_conncache_find_bundle(data->state.conn_cache,
138 result = Curl_bundle_create(data, &new_bundle);
139 if(result != CURLE_OK)
142 if(!conncache_add_bundle(data->state.conn_cache,
143 conn->host.name, new_bundle)) {
144 Curl_bundle_destroy(new_bundle);
145 return CURLE_OUT_OF_MEMORY;
150 result = Curl_bundle_add_conn(bundle, conn);
151 if(result != CURLE_OK) {
153 conncache_remove_bundle(data->state.conn_cache, new_bundle);
157 connc->num_connections++;
162 void Curl_conncache_remove_conn(struct conncache *connc,
163 struct connectdata *conn)
165 struct connectbundle *bundle = conn->bundle;
167 /* The bundle pointer can be NULL, since this function can be called
168 due to a failed connection attempt, before being added to a bundle */
170 Curl_bundle_remove_conn(bundle, conn);
171 if(bundle->num_connections == 0) {
172 conncache_remove_bundle(connc, bundle);
174 connc->num_connections--;
176 DEBUGF(infof(conn->data, "The cache now contains %d members\n",
177 connc->num_connections));
181 /* This function iterates the entire connection cache and calls the
182 function func() with the connection pointer as the first argument
183 and the supplied 'param' argument as the other */
184 void Curl_conncache_foreach(struct conncache *connc,
186 void (*func)(void *conn, void *param))
188 struct curl_hash_iterator iter;
189 struct curl_llist_element *curr;
190 struct curl_hash_element *he;
195 Curl_hash_start_iterate(connc->hash, &iter);
197 he = Curl_hash_next_element(&iter);
199 struct connectbundle *bundle;
200 struct connectdata *conn;
204 curr = bundle->conn_list->head;
206 /* Yes, we need to update curr before calling func(), because func()
207 might decide to remove the connection */
214 he = Curl_hash_next_element(&iter);
218 /* Return the first connection found in the cache. Used when closing all
221 Curl_conncache_find_first_connection(struct conncache *connc)
223 struct curl_hash_iterator iter;
224 struct curl_llist_element *curr;
225 struct curl_hash_element *he;
226 struct connectbundle *bundle;
228 Curl_hash_start_iterate(connc->hash, &iter);
230 he = Curl_hash_next_element(&iter);
234 curr = bundle->conn_list->head;
239 he = Curl_hash_next_element(&iter);
247 /* Useful for debugging the connection cache */
248 void Curl_conncache_print(struct conncache *connc)
250 struct curl_hash_iterator iter;
251 struct curl_llist_element *curr;
252 struct curl_hash_element *he;
257 fprintf(stderr, "=Bundle cache=\n");
259 Curl_hash_start_iterate(connc->hash, &iter);
261 he = Curl_hash_next_element(&iter);
263 struct connectbundle *bundle;
264 struct connectdata *conn;
268 fprintf(stderr, "%s -", he->key);
269 curr = bundle->conn_list->head;
273 fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
276 fprintf(stderr, "\n");
278 he = Curl_hash_next_element(&iter);