1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2012, 2016, Linus Nielsen Feltzing, <linus@haxx.se>
9 * Copyright (C) 2012 - 2015, 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 https://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 ***************************************************************************/
24 #include "curl_setup.h"
26 #include <curl/curl.h>
33 #include "conncache.h"
34 /* The last 3 #include files should be in this order */
35 #include "curl_printf.h"
36 #include "curl_memory.h"
39 static void conn_llist_dtor(void *user, void *element)
41 struct connectdata *data = element;
47 static CURLcode bundle_create(struct Curl_easy *data,
48 struct connectbundle **cb_ptr)
51 DEBUGASSERT(*cb_ptr == NULL);
52 *cb_ptr = malloc(sizeof(struct connectbundle));
54 return CURLE_OUT_OF_MEMORY;
56 (*cb_ptr)->num_connections = 0;
57 (*cb_ptr)->multiuse = BUNDLE_UNKNOWN;
59 (*cb_ptr)->conn_list = Curl_llist_alloc((curl_llist_dtor) conn_llist_dtor);
60 if(!(*cb_ptr)->conn_list) {
61 Curl_safefree(*cb_ptr);
62 return CURLE_OUT_OF_MEMORY;
67 static void bundle_destroy(struct connectbundle *cb_ptr)
72 if(cb_ptr->conn_list) {
73 Curl_llist_destroy(cb_ptr->conn_list, NULL);
74 cb_ptr->conn_list = NULL;
79 /* Add a connection to a bundle */
80 static CURLcode bundle_add_conn(struct connectbundle *cb_ptr,
81 struct connectdata *conn)
83 if(!Curl_llist_insert_next(cb_ptr->conn_list, cb_ptr->conn_list->tail, conn))
84 return CURLE_OUT_OF_MEMORY;
86 conn->bundle = cb_ptr;
88 cb_ptr->num_connections++;
92 /* Remove a connection from a bundle */
93 static int bundle_remove_conn(struct connectbundle *cb_ptr,
94 struct connectdata *conn)
96 struct curl_llist_element *curr;
98 curr = cb_ptr->conn_list->head;
100 if(curr->ptr == conn) {
101 Curl_llist_remove(cb_ptr->conn_list, curr, NULL);
102 cb_ptr->num_connections--;
104 return 1; /* we removed a handle */
111 static void free_bundle_hash_entry(void *freethis)
113 struct connectbundle *b = (struct connectbundle *) freethis;
118 int Curl_conncache_init(struct conncache *connc, int size)
120 return Curl_hash_init(&connc->hash, size, Curl_hash_str,
121 Curl_str_key_compare, free_bundle_hash_entry);
124 void Curl_conncache_destroy(struct conncache *connc)
127 Curl_hash_destroy(&connc->hash);
130 /* returns an allocated key to find a bundle for this connection */
131 static char *hashkey(struct connectdata *conn)
133 const char *hostname;
135 if(conn->bits.socksproxy)
136 hostname = conn->socks_proxy.host.name;
137 else if(conn->bits.httpproxy)
138 hostname = conn->http_proxy.host.name;
139 else if(conn->bits.conn_to_host)
140 hostname = conn->conn_to_host.name;
142 hostname = conn->host.name;
144 return aprintf("%s:%ld", hostname, conn->port);
147 /* Look up the bundle with all the connections to the same host this
148 connectdata struct is setup to use. */
149 struct connectbundle *Curl_conncache_find_bundle(struct connectdata *conn,
150 struct conncache *connc)
152 struct connectbundle *bundle = NULL;
154 char *key = hashkey(conn);
156 bundle = Curl_hash_pick(&connc->hash, key, strlen(key));
164 static bool conncache_add_bundle(struct conncache *connc,
166 struct connectbundle *bundle)
168 void *p = Curl_hash_add(&connc->hash, key, strlen(key), bundle);
173 static void conncache_remove_bundle(struct conncache *connc,
174 struct connectbundle *bundle)
176 struct curl_hash_iterator iter;
177 struct curl_hash_element *he;
182 Curl_hash_start_iterate(&connc->hash, &iter);
184 he = Curl_hash_next_element(&iter);
186 if(he->ptr == bundle) {
187 /* The bundle is destroyed by the hash destructor function,
188 free_bundle_hash_entry() */
189 Curl_hash_delete(&connc->hash, he->key, he->key_len);
193 he = Curl_hash_next_element(&iter);
197 CURLcode Curl_conncache_add_conn(struct conncache *connc,
198 struct connectdata *conn)
201 struct connectbundle *bundle;
202 struct connectbundle *new_bundle = NULL;
203 struct Curl_easy *data = conn->data;
205 bundle = Curl_conncache_find_bundle(conn, data->state.conn_cache);
210 result = bundle_create(data, &new_bundle);
216 bundle_destroy(new_bundle);
217 return CURLE_OUT_OF_MEMORY;
220 rc = conncache_add_bundle(data->state.conn_cache, key, new_bundle);
223 bundle_destroy(new_bundle);
224 return CURLE_OUT_OF_MEMORY;
229 result = bundle_add_conn(bundle, conn);
232 conncache_remove_bundle(data->state.conn_cache, new_bundle);
236 conn->connection_id = connc->next_connection_id++;
237 connc->num_connections++;
239 DEBUGF(infof(conn->data, "Added connection %ld. "
240 "The cache now contains %" CURL_FORMAT_CURL_OFF_TU " members\n",
241 conn->connection_id, (curl_off_t) connc->num_connections));
246 void Curl_conncache_remove_conn(struct conncache *connc,
247 struct connectdata *conn)
249 struct connectbundle *bundle = conn->bundle;
251 /* The bundle pointer can be NULL, since this function can be called
252 due to a failed connection attempt, before being added to a bundle */
254 bundle_remove_conn(bundle, conn);
255 if(bundle->num_connections == 0) {
256 conncache_remove_bundle(connc, bundle);
260 connc->num_connections--;
262 DEBUGF(infof(conn->data, "The cache now contains %"
263 CURL_FORMAT_CURL_OFF_TU " members\n",
264 (curl_off_t) connc->num_connections));
269 /* This function iterates the entire connection cache and calls the
270 function func() with the connection pointer as the first argument
271 and the supplied 'param' argument as the other,
273 Return 0 from func() to continue the loop, return 1 to abort it.
275 void Curl_conncache_foreach(struct conncache *connc,
277 int (*func)(struct connectdata *conn, void *param))
279 struct curl_hash_iterator iter;
280 struct curl_llist_element *curr;
281 struct curl_hash_element *he;
286 Curl_hash_start_iterate(&connc->hash, &iter);
288 he = Curl_hash_next_element(&iter);
290 struct connectbundle *bundle;
293 he = Curl_hash_next_element(&iter);
295 curr = bundle->conn_list->head;
297 /* Yes, we need to update curr before calling func(), because func()
298 might decide to remove the connection */
299 struct connectdata *conn = curr->ptr;
302 if(1 == func(conn, param))
308 /* Return the first connection found in the cache. Used when closing all
311 Curl_conncache_find_first_connection(struct conncache *connc)
313 struct curl_hash_iterator iter;
314 struct curl_hash_element *he;
315 struct connectbundle *bundle;
317 Curl_hash_start_iterate(&connc->hash, &iter);
319 he = Curl_hash_next_element(&iter);
321 struct curl_llist_element *curr;
324 curr = bundle->conn_list->head;
329 he = Curl_hash_next_element(&iter);
337 /* Useful for debugging the connection cache */
338 void Curl_conncache_print(struct conncache *connc)
340 struct curl_hash_iterator iter;
341 struct curl_llist_element *curr;
342 struct curl_hash_element *he;
347 fprintf(stderr, "=Bundle cache=\n");
349 Curl_hash_start_iterate(connc->hash, &iter);
351 he = Curl_hash_next_element(&iter);
353 struct connectbundle *bundle;
354 struct connectdata *conn;
358 fprintf(stderr, "%s -", he->key);
359 curr = bundle->conn_list->head;
363 fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
366 fprintf(stderr, "\n");
368 he = Curl_hash_next_element(&iter);