113498266Sopenharmony_ci#ifndef HEADER_CURL_CONNCACHE_H
213498266Sopenharmony_ci#define HEADER_CURL_CONNCACHE_H
313498266Sopenharmony_ci/***************************************************************************
413498266Sopenharmony_ci *                                  _   _ ____  _
513498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
613498266Sopenharmony_ci *                             / __| | | | |_) | |
713498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
813498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1113498266Sopenharmony_ci * Copyright (C) Linus Nielsen Feltzing, <linus@haxx.se>
1213498266Sopenharmony_ci *
1313498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1413498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1513498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1613498266Sopenharmony_ci *
1713498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1813498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1913498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2213498266Sopenharmony_ci * KIND, either express or implied.
2313498266Sopenharmony_ci *
2413498266Sopenharmony_ci * SPDX-License-Identifier: curl
2513498266Sopenharmony_ci *
2613498266Sopenharmony_ci ***************************************************************************/
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci/*
2913498266Sopenharmony_ci * All accesses to struct fields and changing of data in the connection cache
3013498266Sopenharmony_ci * and connectbundles must be done with the conncache LOCKED. The cache might
3113498266Sopenharmony_ci * be shared.
3213498266Sopenharmony_ci */
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci#include <curl/curl.h>
3513498266Sopenharmony_ci#include "timeval.h"
3613498266Sopenharmony_ci
3713498266Sopenharmony_cistruct connectdata;
3813498266Sopenharmony_ci
3913498266Sopenharmony_cistruct conncache {
4013498266Sopenharmony_ci  struct Curl_hash hash;
4113498266Sopenharmony_ci  size_t num_conn;
4213498266Sopenharmony_ci  curl_off_t next_connection_id;
4313498266Sopenharmony_ci  curl_off_t next_easy_id;
4413498266Sopenharmony_ci  struct curltime last_cleanup;
4513498266Sopenharmony_ci  /* handle used for closing cached connections */
4613498266Sopenharmony_ci  struct Curl_easy *closure_handle;
4713498266Sopenharmony_ci};
4813498266Sopenharmony_ci
4913498266Sopenharmony_ci#define BUNDLE_NO_MULTIUSE -1
5013498266Sopenharmony_ci#define BUNDLE_UNKNOWN     0  /* initial value */
5113498266Sopenharmony_ci#define BUNDLE_MULTIPLEX   2
5213498266Sopenharmony_ci
5313498266Sopenharmony_ci#ifdef CURLDEBUG
5413498266Sopenharmony_ci/* the debug versions of these macros make extra certain that the lock is
5513498266Sopenharmony_ci   never doubly locked or unlocked */
5613498266Sopenharmony_ci#define CONNCACHE_LOCK(x)                                               \
5713498266Sopenharmony_ci  do {                                                                  \
5813498266Sopenharmony_ci    if((x)->share) {                                                    \
5913498266Sopenharmony_ci      Curl_share_lock((x), CURL_LOCK_DATA_CONNECT,                      \
6013498266Sopenharmony_ci                      CURL_LOCK_ACCESS_SINGLE);                         \
6113498266Sopenharmony_ci      DEBUGASSERT(!(x)->state.conncache_lock);                          \
6213498266Sopenharmony_ci      (x)->state.conncache_lock = TRUE;                                 \
6313498266Sopenharmony_ci    }                                                                   \
6413498266Sopenharmony_ci  } while(0)
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci#define CONNCACHE_UNLOCK(x)                                             \
6713498266Sopenharmony_ci  do {                                                                  \
6813498266Sopenharmony_ci    if((x)->share) {                                                    \
6913498266Sopenharmony_ci      DEBUGASSERT((x)->state.conncache_lock);                           \
7013498266Sopenharmony_ci      (x)->state.conncache_lock = FALSE;                                \
7113498266Sopenharmony_ci      Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT);                   \
7213498266Sopenharmony_ci    }                                                                   \
7313498266Sopenharmony_ci  } while(0)
7413498266Sopenharmony_ci#else
7513498266Sopenharmony_ci#define CONNCACHE_LOCK(x) if((x)->share)                                \
7613498266Sopenharmony_ci    Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, CURL_LOCK_ACCESS_SINGLE)
7713498266Sopenharmony_ci#define CONNCACHE_UNLOCK(x) if((x)->share)              \
7813498266Sopenharmony_ci    Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT)
7913498266Sopenharmony_ci#endif
8013498266Sopenharmony_ci
8113498266Sopenharmony_cistruct connectbundle {
8213498266Sopenharmony_ci  int multiuse;                 /* supports multi-use */
8313498266Sopenharmony_ci  size_t num_connections;       /* Number of connections in the bundle */
8413498266Sopenharmony_ci  struct Curl_llist conn_list;  /* The connectdata members of the bundle */
8513498266Sopenharmony_ci};
8613498266Sopenharmony_ci
8713498266Sopenharmony_ci/* returns 1 on error, 0 is fine */
8813498266Sopenharmony_ciint Curl_conncache_init(struct conncache *, int size);
8913498266Sopenharmony_civoid Curl_conncache_destroy(struct conncache *connc);
9013498266Sopenharmony_ci
9113498266Sopenharmony_ci/* return the correct bundle, to a host or a proxy */
9213498266Sopenharmony_cistruct connectbundle *Curl_conncache_find_bundle(struct Curl_easy *data,
9313498266Sopenharmony_ci                                                 struct connectdata *conn,
9413498266Sopenharmony_ci                                                 struct conncache *connc);
9513498266Sopenharmony_ci/* returns number of connections currently held in the connection cache */
9613498266Sopenharmony_cisize_t Curl_conncache_size(struct Curl_easy *data);
9713498266Sopenharmony_ci
9813498266Sopenharmony_cibool Curl_conncache_return_conn(struct Curl_easy *data,
9913498266Sopenharmony_ci                                struct connectdata *conn);
10013498266Sopenharmony_ciCURLcode Curl_conncache_add_conn(struct Curl_easy *data) WARN_UNUSED_RESULT;
10113498266Sopenharmony_civoid Curl_conncache_remove_conn(struct Curl_easy *data,
10213498266Sopenharmony_ci                                struct connectdata *conn,
10313498266Sopenharmony_ci                                bool lock);
10413498266Sopenharmony_cibool Curl_conncache_foreach(struct Curl_easy *data,
10513498266Sopenharmony_ci                            struct conncache *connc,
10613498266Sopenharmony_ci                            void *param,
10713498266Sopenharmony_ci                            int (*func)(struct Curl_easy *data,
10813498266Sopenharmony_ci                                        struct connectdata *conn,
10913498266Sopenharmony_ci                                        void *param));
11013498266Sopenharmony_ci
11113498266Sopenharmony_cistruct connectdata *
11213498266Sopenharmony_ciCurl_conncache_find_first_connection(struct conncache *connc);
11313498266Sopenharmony_ci
11413498266Sopenharmony_cistruct connectdata *
11513498266Sopenharmony_ciCurl_conncache_extract_bundle(struct Curl_easy *data,
11613498266Sopenharmony_ci                              struct connectbundle *bundle);
11713498266Sopenharmony_cistruct connectdata *
11813498266Sopenharmony_ciCurl_conncache_extract_oldest(struct Curl_easy *data);
11913498266Sopenharmony_civoid Curl_conncache_close_all_connections(struct conncache *connc);
12013498266Sopenharmony_civoid Curl_conncache_print(struct conncache *connc);
12113498266Sopenharmony_ci
12213498266Sopenharmony_ci#endif /* HEADER_CURL_CONNCACHE_H */
123