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>
34 #include "conncache.h"
35 /* The last 3 #include files should be in this order */
36 #include "curl_printf.h"
37 #include "curl_memory.h"
40 static void conn_llist_dtor(void *user, void *element)
42 struct connectdata *data = element;
48 static CURLcode bundle_create(struct Curl_easy *data,
49 struct connectbundle **cb_ptr)
52 DEBUGASSERT(*cb_ptr == NULL);
53 *cb_ptr = malloc(sizeof(struct connectbundle));
55 return CURLE_OUT_OF_MEMORY;
57 (*cb_ptr)->num_connections = 0;
58 (*cb_ptr)->multiuse = BUNDLE_UNKNOWN;
60 (*cb_ptr)->conn_list = Curl_llist_alloc((curl_llist_dtor) conn_llist_dtor);
61 if(!(*cb_ptr)->conn_list) {
62 Curl_safefree(*cb_ptr);
63 return CURLE_OUT_OF_MEMORY;
68 static void bundle_destroy(struct connectbundle *cb_ptr)
73 if(cb_ptr->conn_list) {
74 Curl_llist_destroy(cb_ptr->conn_list, NULL);
75 cb_ptr->conn_list = NULL;
80 /* Add a connection to a bundle */
81 static CURLcode bundle_add_conn(struct connectbundle *cb_ptr,
82 struct connectdata *conn)
84 if(!Curl_llist_insert_next(cb_ptr->conn_list, cb_ptr->conn_list->tail, conn))
85 return CURLE_OUT_OF_MEMORY;
87 conn->bundle = cb_ptr;
89 cb_ptr->num_connections++;
93 /* Remove a connection from a bundle */
94 static int bundle_remove_conn(struct connectbundle *cb_ptr,
95 struct connectdata *conn)
97 struct curl_llist_element *curr;
99 curr = cb_ptr->conn_list->head;
101 if(curr->ptr == conn) {
102 Curl_llist_remove(cb_ptr->conn_list, curr, NULL);
103 cb_ptr->num_connections--;
105 return 1; /* we removed a handle */
112 static void free_bundle_hash_entry(void *freethis)
114 struct connectbundle *b = (struct connectbundle *) freethis;
119 int Curl_conncache_init(struct conncache *connc, int size)
121 return Curl_hash_init(&connc->hash, size, Curl_hash_str,
122 Curl_str_key_compare, free_bundle_hash_entry);
125 void Curl_conncache_destroy(struct conncache *connc)
128 Curl_hash_destroy(&connc->hash);
131 /* returns an allocated key to find a bundle for this connection */
132 static char *hashkey(struct connectdata *conn)
134 const char *hostname;
137 hostname = conn->proxy.name;
138 else if(conn->bits.conn_to_host)
139 hostname = conn->conn_to_host.name;
141 hostname = conn->host.name;
143 return aprintf("%s:%d", hostname, conn->port);
146 /* Look up the bundle with all the connections to the same host this
147 connectdata struct is setup to use. */
148 struct connectbundle *Curl_conncache_find_bundle(struct connectdata *conn,
149 struct conncache *connc)
151 struct connectbundle *bundle = NULL;
153 char *key = hashkey(conn);
155 bundle = Curl_hash_pick(&connc->hash, key, strlen(key));
163 static bool conncache_add_bundle(struct conncache *connc,
165 struct connectbundle *bundle)
167 void *p = Curl_hash_add(&connc->hash, key, strlen(key), bundle);
172 static void conncache_remove_bundle(struct conncache *connc,
173 struct connectbundle *bundle)
175 struct curl_hash_iterator iter;
176 struct curl_hash_element *he;
181 Curl_hash_start_iterate(&connc->hash, &iter);
183 he = Curl_hash_next_element(&iter);
185 if(he->ptr == bundle) {
186 /* The bundle is destroyed by the hash destructor function,
187 free_bundle_hash_entry() */
188 Curl_hash_delete(&connc->hash, he->key, he->key_len);
192 he = Curl_hash_next_element(&iter);
196 CURLcode Curl_conncache_add_conn(struct conncache *connc,
197 struct connectdata *conn)
200 struct connectbundle *bundle;
201 struct connectbundle *new_bundle = NULL;
202 struct Curl_easy *data = conn->data;
204 bundle = Curl_conncache_find_bundle(conn, data->state.conn_cache);
209 result = bundle_create(data, &new_bundle);
215 bundle_destroy(new_bundle);
216 return CURLE_OUT_OF_MEMORY;
219 rc = conncache_add_bundle(data->state.conn_cache, key, new_bundle);
222 bundle_destroy(new_bundle);
223 return CURLE_OUT_OF_MEMORY;
228 result = bundle_add_conn(bundle, conn);
231 conncache_remove_bundle(data->state.conn_cache, new_bundle);
235 conn->connection_id = connc->next_connection_id++;
236 connc->num_connections++;
238 DEBUGF(infof(conn->data, "Added connection %ld. "
239 "The cache now contains %" CURL_FORMAT_CURL_OFF_TU " members\n",
240 conn->connection_id, (curl_off_t) connc->num_connections));
245 void Curl_conncache_remove_conn(struct conncache *connc,
246 struct connectdata *conn)
248 struct connectbundle *bundle = conn->bundle;
250 /* The bundle pointer can be NULL, since this function can be called
251 due to a failed connection attempt, before being added to a bundle */
253 bundle_remove_conn(bundle, conn);
254 if(bundle->num_connections == 0) {
255 conncache_remove_bundle(connc, bundle);
259 connc->num_connections--;
261 DEBUGF(infof(conn->data, "The cache now contains %"
262 CURL_FORMAT_CURL_OFF_TU " members\n",
263 (curl_off_t) connc->num_connections));
268 /* This function iterates the entire connection cache and calls the
269 function func() with the connection pointer as the first argument
270 and the supplied 'param' argument as the other,
272 Return 0 from func() to continue the loop, return 1 to abort it.
274 void Curl_conncache_foreach(struct conncache *connc,
276 int (*func)(struct connectdata *conn, void *param))
278 struct curl_hash_iterator iter;
279 struct curl_llist_element *curr;
280 struct curl_hash_element *he;
285 Curl_hash_start_iterate(&connc->hash, &iter);
287 he = Curl_hash_next_element(&iter);
289 struct connectbundle *bundle;
292 he = Curl_hash_next_element(&iter);
294 curr = bundle->conn_list->head;
296 /* Yes, we need to update curr before calling func(), because func()
297 might decide to remove the connection */
298 struct connectdata *conn = curr->ptr;
301 if(1 == func(conn, param))
307 /* Return the first connection found in the cache. Used when closing all
310 Curl_conncache_find_first_connection(struct conncache *connc)
312 struct curl_hash_iterator iter;
313 struct curl_hash_element *he;
314 struct connectbundle *bundle;
316 Curl_hash_start_iterate(&connc->hash, &iter);
318 he = Curl_hash_next_element(&iter);
320 struct curl_llist_element *curr;
323 curr = bundle->conn_list->head;
328 he = Curl_hash_next_element(&iter);
336 /* Useful for debugging the connection cache */
337 void Curl_conncache_print(struct conncache *connc)
339 struct curl_hash_iterator iter;
340 struct curl_llist_element *curr;
341 struct curl_hash_element *he;
346 fprintf(stderr, "=Bundle cache=\n");
348 Curl_hash_start_iterate(connc->hash, &iter);
350 he = Curl_hash_next_element(&iter);
352 struct connectbundle *bundle;
353 struct connectdata *conn;
357 fprintf(stderr, "%s -", he->key);
358 curr = bundle->conn_list->head;
362 fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
365 fprintf(stderr, "\n");
367 he = Curl_hash_next_element(&iter);