1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2012, Linus Nielsen Feltzing, <linus@haxx.se>
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #include <curl/curl.h>
34 #include "conncache.h"
36 #include "curl_memory.h"
37 /* The last #include file should be: */
40 #define CONNECTION_HASH_SIZE 97
42 static void free_bundle_hash_entry(void *freethis)
44 struct connectbundle *b = (struct connectbundle *) freethis;
46 Curl_bundle_destroy(b);
49 struct conncache *Curl_conncache_init(int type)
51 struct conncache *connc;
53 connc = calloc(1, sizeof(struct conncache));
57 connc->hash = Curl_hash_alloc(CONNECTION_HASH_SIZE, Curl_hash_str,
58 Curl_str_key_compare, free_bundle_hash_entry);
66 connc->num_connections = 0;
71 void Curl_conncache_destroy(struct conncache *connc)
73 Curl_hash_destroy(connc->hash);
77 struct connectbundle *Curl_conncache_find_bundle(struct conncache *connc,
80 struct connectbundle *bundle = NULL;
83 bundle = Curl_hash_pick(connc->hash, hostname, strlen(hostname)+1);
88 static bool conncache_add_bundle(struct conncache *connc,
90 struct connectbundle *bundle)
94 p = Curl_hash_add(connc->hash, hostname, strlen(hostname)+1, bundle);
99 static void conncache_remove_bundle(struct conncache *connc,
100 struct connectbundle *bundle)
102 struct curl_hash_iterator iter;
103 struct curl_hash_element *he;
108 Curl_hash_start_iterate(connc->hash, &iter);
110 he = Curl_hash_next_element(&iter);
112 if(he->ptr == bundle) {
113 /* The bundle is destroyed by the hash destructor function,
114 free_bundle_hash_entry() */
115 Curl_hash_delete(connc->hash, he->key, he->key_len);
119 he = Curl_hash_next_element(&iter);
123 CURLcode Curl_conncache_add_conn(struct conncache *connc,
124 struct connectdata *conn)
127 struct connectbundle *bundle;
128 struct SessionHandle *data = conn->data;
130 bundle = Curl_conncache_find_bundle(data->state.conn_cache,
133 result = Curl_bundle_create(data, &bundle);
134 if(result != CURLE_OK)
137 if(!conncache_add_bundle(data->state.conn_cache,
138 conn->host.name, bundle))
139 return CURLE_OUT_OF_MEMORY;
142 result = Curl_bundle_add_conn(bundle, conn);
143 if(result != CURLE_OK)
146 connc->num_connections++;
151 void Curl_conncache_remove_conn(struct conncache *connc,
152 struct connectdata *conn)
154 struct connectbundle *bundle = conn->bundle;
156 /* The bundle pointer can be NULL, since this function can be called
157 due to a failed connection attempt, before being added to a bundle */
159 Curl_bundle_remove_conn(bundle, conn);
160 if(bundle->num_connections == 0) {
161 conncache_remove_bundle(connc, bundle);
163 connc->num_connections--;
165 DEBUGF(infof(conn->data, "The cache now contains %d members\n",
166 connc->num_connections));
170 /* This function iterates the entire connection cache and calls the
171 function func() with the connection pointer as the first argument
172 and the supplied 'param' argument as the other */
173 void Curl_conncache_foreach(struct conncache *connc,
175 void (*func)(void *conn, void *param))
177 struct curl_hash_iterator iter;
178 struct curl_llist_element *curr;
179 struct curl_hash_element *he;
184 Curl_hash_start_iterate(connc->hash, &iter);
186 he = Curl_hash_next_element(&iter);
188 struct connectbundle *bundle;
189 struct connectdata *conn;
193 curr = bundle->conn_list->head;
195 /* Yes, we need to update curr before calling func(), because func()
196 might decide to remove the connection */
203 he = Curl_hash_next_element(&iter);
207 /* Return the first connection found in the cache. Used when closing all
210 Curl_conncache_find_first_connection(struct conncache *connc)
212 struct curl_hash_iterator iter;
213 struct curl_llist_element *curr;
214 struct curl_hash_element *he;
215 struct connectbundle *bundle;
217 Curl_hash_start_iterate(connc->hash, &iter);
219 he = Curl_hash_next_element(&iter);
223 curr = bundle->conn_list->head;
228 he = Curl_hash_next_element(&iter);
236 /* Useful for debugging the connection cache */
237 void Curl_conncache_print(struct conncache *connc)
239 struct curl_hash_iterator iter;
240 struct curl_llist_element *curr;
241 struct curl_hash_element *he;
246 fprintf(stderr, "=Bundle cache=\n");
248 Curl_hash_start_iterate(connc->hash, &iter);
250 he = Curl_hash_next_element(&iter);
252 struct connectbundle *bundle;
253 struct connectdata *conn;
257 fprintf(stderr, "%s -", he->key);
258 curr = bundle->conn_list->head;
262 fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
265 fprintf(stderr, "\n");
267 he = Curl_hash_next_element(&iter);