2012-12-06 19:12:04 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2016-04-29 21:46:40 +08:00
|
|
|
* Copyright (C) 2012, 2016, Linus Nielsen Feltzing, <linus@haxx.se>
|
2015-03-11 18:54:22 +08:00
|
|
|
* Copyright (C) 2012 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2012-12-06 19:12:04 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-03 07:19:02 +08:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2012-12-06 19:12:04 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "multiif.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "rawstr.h"
|
|
|
|
#include "conncache.h"
|
2016-04-29 21:46:40 +08:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-05-13 05:40:10 +08:00
|
|
|
#include "curl_printf.h"
|
2012-12-06 19:12:04 +08:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2012-12-06 19:12:04 +08:00
|
|
|
|
2015-05-13 05:06:11 +08:00
|
|
|
static void conn_llist_dtor(void *user, void *element)
|
|
|
|
{
|
|
|
|
struct connectdata *data = element;
|
|
|
|
(void)user;
|
|
|
|
|
|
|
|
data->bundle = NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:47:12 +08:00
|
|
|
static CURLcode bundle_create(struct Curl_easy *data,
|
2015-05-13 05:06:11 +08:00
|
|
|
struct connectbundle **cb_ptr)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
DEBUGASSERT(*cb_ptr == NULL);
|
|
|
|
*cb_ptr = malloc(sizeof(struct connectbundle));
|
|
|
|
if(!*cb_ptr)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
(*cb_ptr)->num_connections = 0;
|
2015-05-18 14:56:29 +08:00
|
|
|
(*cb_ptr)->multiuse = BUNDLE_UNKNOWN;
|
2015-05-13 05:06:11 +08:00
|
|
|
|
|
|
|
(*cb_ptr)->conn_list = Curl_llist_alloc((curl_llist_dtor) conn_llist_dtor);
|
|
|
|
if(!(*cb_ptr)->conn_list) {
|
|
|
|
Curl_safefree(*cb_ptr);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bundle_destroy(struct connectbundle *cb_ptr)
|
|
|
|
{
|
|
|
|
if(!cb_ptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(cb_ptr->conn_list) {
|
|
|
|
Curl_llist_destroy(cb_ptr->conn_list, NULL);
|
|
|
|
cb_ptr->conn_list = NULL;
|
|
|
|
}
|
|
|
|
free(cb_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a connection to a bundle */
|
|
|
|
static CURLcode bundle_add_conn(struct connectbundle *cb_ptr,
|
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
|
|
|
if(!Curl_llist_insert_next(cb_ptr->conn_list, cb_ptr->conn_list->tail, conn))
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
conn->bundle = cb_ptr;
|
|
|
|
|
|
|
|
cb_ptr->num_connections++;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove a connection from a bundle */
|
|
|
|
static int bundle_remove_conn(struct connectbundle *cb_ptr,
|
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct curl_llist_element *curr;
|
|
|
|
|
|
|
|
curr = cb_ptr->conn_list->head;
|
|
|
|
while(curr) {
|
|
|
|
if(curr->ptr == conn) {
|
|
|
|
Curl_llist_remove(cb_ptr->conn_list, curr, NULL);
|
|
|
|
cb_ptr->num_connections--;
|
|
|
|
conn->bundle = NULL;
|
|
|
|
return 1; /* we removed a handle */
|
|
|
|
}
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-06 19:12:04 +08:00
|
|
|
static void free_bundle_hash_entry(void *freethis)
|
|
|
|
{
|
|
|
|
struct connectbundle *b = (struct connectbundle *) freethis;
|
|
|
|
|
2015-05-13 05:06:11 +08:00
|
|
|
bundle_destroy(b);
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 15:15:02 +08:00
|
|
|
int Curl_conncache_init(struct conncache *connc, int size)
|
2012-12-06 19:12:04 +08:00
|
|
|
{
|
2015-05-12 15:15:02 +08:00
|
|
|
return Curl_hash_init(&connc->hash, size, Curl_hash_str,
|
|
|
|
Curl_str_key_compare, free_bundle_hash_entry);
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_conncache_destroy(struct conncache *connc)
|
|
|
|
{
|
2015-05-12 15:15:02 +08:00
|
|
|
if(connc)
|
2015-05-03 02:49:55 +08:00
|
|
|
Curl_hash_destroy(&connc->hash);
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
2015-05-13 05:40:10 +08:00
|
|
|
/* returns an allocated key to find a bundle for this connection */
|
|
|
|
static char *hashkey(struct connectdata *conn)
|
|
|
|
{
|
2016-01-25 21:37:24 +08:00
|
|
|
const char *hostname;
|
|
|
|
|
|
|
|
if(conn->bits.proxy)
|
|
|
|
hostname = conn->proxy.name;
|
|
|
|
else if(conn->bits.conn_to_host)
|
|
|
|
hostname = conn->conn_to_host.name;
|
|
|
|
else
|
|
|
|
hostname = conn->host.name;
|
|
|
|
|
2016-05-03 05:15:05 +08:00
|
|
|
return aprintf("%s:%d", hostname, conn->port);
|
2015-05-13 05:40:10 +08:00
|
|
|
}
|
|
|
|
|
2015-05-13 05:06:11 +08:00
|
|
|
/* Look up the bundle with all the connections to the same host this
|
|
|
|
connectdata struct is setup to use. */
|
2015-03-11 18:54:22 +08:00
|
|
|
struct connectbundle *Curl_conncache_find_bundle(struct connectdata *conn,
|
|
|
|
struct conncache *connc)
|
2012-12-06 19:12:04 +08:00
|
|
|
{
|
|
|
|
struct connectbundle *bundle = NULL;
|
2015-05-13 05:40:10 +08:00
|
|
|
if(connc) {
|
|
|
|
char *key = hashkey(conn);
|
|
|
|
if(key) {
|
|
|
|
bundle = Curl_hash_pick(&connc->hash, key, strlen(key));
|
|
|
|
free(key);
|
|
|
|
}
|
|
|
|
}
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
return bundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool conncache_add_bundle(struct conncache *connc,
|
2015-05-13 05:40:10 +08:00
|
|
|
char *key,
|
2012-12-06 19:12:04 +08:00
|
|
|
struct connectbundle *bundle)
|
|
|
|
{
|
2015-05-13 05:40:10 +08:00
|
|
|
void *p = Curl_hash_add(&connc->hash, key, strlen(key), bundle);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
return p?TRUE:FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void conncache_remove_bundle(struct conncache *connc,
|
|
|
|
struct connectbundle *bundle)
|
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
|
|
|
|
if(!connc)
|
|
|
|
return;
|
|
|
|
|
2015-05-12 15:15:02 +08:00
|
|
|
Curl_hash_start_iterate(&connc->hash, &iter);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
if(he->ptr == bundle) {
|
|
|
|
/* The bundle is destroyed by the hash destructor function,
|
|
|
|
free_bundle_hash_entry() */
|
2015-05-12 15:15:02 +08:00
|
|
|
Curl_hash_delete(&connc->hash, he->key, he->key_len);
|
2012-12-06 19:12:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode Curl_conncache_add_conn(struct conncache *connc,
|
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
|
|
|
struct connectbundle *bundle;
|
2012-12-20 02:52:11 +08:00
|
|
|
struct connectbundle *new_bundle = NULL;
|
2016-06-21 21:47:12 +08:00
|
|
|
struct Curl_easy *data = conn->data;
|
2012-12-06 19:12:04 +08:00
|
|
|
|
2015-03-11 18:54:22 +08:00
|
|
|
bundle = Curl_conncache_find_bundle(conn, data->state.conn_cache);
|
2012-12-06 19:12:04 +08:00
|
|
|
if(!bundle) {
|
2015-05-13 05:40:10 +08:00
|
|
|
char *key;
|
|
|
|
int rc;
|
2015-03-11 18:54:22 +08:00
|
|
|
|
2015-05-13 05:06:11 +08:00
|
|
|
result = bundle_create(data, &new_bundle);
|
2014-10-24 04:56:35 +08:00
|
|
|
if(result)
|
2012-12-06 19:12:04 +08:00
|
|
|
return result;
|
|
|
|
|
2015-05-13 05:40:10 +08:00
|
|
|
key = hashkey(conn);
|
2015-05-24 17:19:07 +08:00
|
|
|
if(!key) {
|
|
|
|
bundle_destroy(new_bundle);
|
2015-05-13 05:40:10 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2015-05-24 17:19:07 +08:00
|
|
|
}
|
2015-05-13 05:40:10 +08:00
|
|
|
|
|
|
|
rc = conncache_add_bundle(data->state.conn_cache, key, new_bundle);
|
|
|
|
free(key);
|
|
|
|
if(!rc) {
|
2015-05-13 05:06:11 +08:00
|
|
|
bundle_destroy(new_bundle);
|
2012-12-06 19:12:04 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2012-12-20 02:52:11 +08:00
|
|
|
}
|
|
|
|
bundle = new_bundle;
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
2015-05-13 05:06:11 +08:00
|
|
|
result = bundle_add_conn(bundle, conn);
|
2014-10-24 04:56:35 +08:00
|
|
|
if(result) {
|
2012-12-20 02:52:11 +08:00
|
|
|
if(new_bundle)
|
|
|
|
conncache_remove_bundle(data->state.conn_cache, new_bundle);
|
2012-12-06 19:12:04 +08:00
|
|
|
return result;
|
2012-12-20 02:52:11 +08:00
|
|
|
}
|
2012-12-06 19:12:04 +08:00
|
|
|
|
2014-06-13 02:36:41 +08:00
|
|
|
conn->connection_id = connc->next_connection_id++;
|
2012-12-06 19:12:04 +08:00
|
|
|
connc->num_connections++;
|
2014-11-19 06:02:40 +08:00
|
|
|
|
2014-11-20 04:04:21 +08:00
|
|
|
DEBUGF(infof(conn->data, "Added connection %ld. "
|
|
|
|
"The cache now contains %" CURL_FORMAT_CURL_OFF_TU " members\n",
|
|
|
|
conn->connection_id, (curl_off_t) connc->num_connections));
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_conncache_remove_conn(struct conncache *connc,
|
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct connectbundle *bundle = conn->bundle;
|
|
|
|
|
|
|
|
/* The bundle pointer can be NULL, since this function can be called
|
|
|
|
due to a failed connection attempt, before being added to a bundle */
|
|
|
|
if(bundle) {
|
2015-05-13 05:06:11 +08:00
|
|
|
bundle_remove_conn(bundle, conn);
|
2012-12-06 19:12:04 +08:00
|
|
|
if(bundle->num_connections == 0) {
|
|
|
|
conncache_remove_bundle(connc, bundle);
|
|
|
|
}
|
|
|
|
|
2014-01-04 23:34:58 +08:00
|
|
|
if(connc) {
|
|
|
|
connc->num_connections--;
|
|
|
|
|
2014-11-20 04:04:21 +08:00
|
|
|
DEBUGF(infof(conn->data, "The cache now contains %"
|
|
|
|
CURL_FORMAT_CURL_OFF_TU " members\n",
|
|
|
|
(curl_off_t) connc->num_connections));
|
2014-01-04 23:34:58 +08:00
|
|
|
}
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function iterates the entire connection cache and calls the
|
|
|
|
function func() with the connection pointer as the first argument
|
2012-12-12 03:13:29 +08:00
|
|
|
and the supplied 'param' argument as the other,
|
|
|
|
|
|
|
|
Return 0 from func() to continue the loop, return 1 to abort it.
|
|
|
|
*/
|
2012-12-06 19:12:04 +08:00
|
|
|
void Curl_conncache_foreach(struct conncache *connc,
|
|
|
|
void *param,
|
2012-12-12 03:13:29 +08:00
|
|
|
int (*func)(struct connectdata *conn, void *param))
|
2012-12-06 19:12:04 +08:00
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_llist_element *curr;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
|
|
|
|
if(!connc)
|
|
|
|
return;
|
|
|
|
|
2015-05-12 15:15:02 +08:00
|
|
|
Curl_hash_start_iterate(&connc->hash, &iter);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
struct connectbundle *bundle;
|
|
|
|
|
|
|
|
bundle = he->ptr;
|
2014-08-11 06:06:20 +08:00
|
|
|
he = Curl_hash_next_element(&iter);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
curr = bundle->conn_list->head;
|
|
|
|
while(curr) {
|
|
|
|
/* Yes, we need to update curr before calling func(), because func()
|
|
|
|
might decide to remove the connection */
|
2014-10-14 15:44:06 +08:00
|
|
|
struct connectdata *conn = curr->ptr;
|
2012-12-06 19:12:04 +08:00
|
|
|
curr = curr->next;
|
|
|
|
|
2012-12-12 03:13:29 +08:00
|
|
|
if(1 == func(conn, param))
|
|
|
|
return;
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the first connection found in the cache. Used when closing all
|
|
|
|
connections */
|
|
|
|
struct connectdata *
|
|
|
|
Curl_conncache_find_first_connection(struct conncache *connc)
|
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
struct connectbundle *bundle;
|
|
|
|
|
2015-05-12 15:15:02 +08:00
|
|
|
Curl_hash_start_iterate(&connc->hash, &iter);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
2014-10-14 15:44:06 +08:00
|
|
|
struct curl_llist_element *curr;
|
2012-12-06 19:12:04 +08:00
|
|
|
bundle = he->ptr;
|
|
|
|
|
|
|
|
curr = bundle->conn_list->head;
|
|
|
|
if(curr) {
|
|
|
|
return curr->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Useful for debugging the connection cache */
|
|
|
|
void Curl_conncache_print(struct conncache *connc)
|
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_llist_element *curr;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
|
|
|
|
if(!connc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(stderr, "=Bundle cache=\n");
|
|
|
|
|
|
|
|
Curl_hash_start_iterate(connc->hash, &iter);
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
struct connectbundle *bundle;
|
|
|
|
struct connectdata *conn;
|
|
|
|
|
|
|
|
bundle = he->ptr;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s -", he->key);
|
|
|
|
curr = bundle->conn_list->head;
|
|
|
|
while(curr) {
|
|
|
|
conn = curr->ptr;
|
|
|
|
|
|
|
|
fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|