1 #ifndef HEADER_CURL_CONNCACHE_H
2 #define HEADER_CURL_CONNCACHE_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 2015 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
11  * Copyright (C) 2012 - 2014, Linus Nielsen Feltzing, <linus@haxx.se>
12  *
13  * This software is licensed as described in the file COPYING, which
14  * you should have received as part of this distribution. The terms
15  * are also available at https://curl.se/docs/copyright.html.
16  *
17  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
18  * copies of the Software, and permit persons to whom the Software is
19  * furnished to do so, under the terms of the COPYING file.
20  *
21  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22  * KIND, either express or implied.
23  *
24  ***************************************************************************/
25 
26 /*
27  * All accesses to struct fields and changing of data in the connection cache
28  * and connectbundles must be done with the conncache LOCKED. The cache might
29  * be shared.
30  */
31 
32 #include "timeval.h"
33 
34 struct connectdata;
35 
36 struct conncache {
37   struct Curl_hash hash;
38   size_t num_conn;
39   long next_connection_id;
40   struct curltime last_cleanup;
41   /* handle used for closing cached connections */
42   struct Curl_easy *closure_handle;
43 };
44 
45 #define BUNDLE_NO_MULTIUSE -1
46 #define BUNDLE_UNKNOWN     0  /* initial value */
47 #define BUNDLE_MULTIPLEX   2
48 
49 #ifdef CURLDEBUG
50 /* the debug versions of these macros make extra certain that the lock is
51    never doubly locked or unlocked */
52 #define CONNCACHE_LOCK(x)                                               \
53   do {                                                                  \
54     if((x)->share) {                                                    \
55       Curl_share_lock((x), CURL_LOCK_DATA_CONNECT,                      \
56                       CURL_LOCK_ACCESS_SINGLE);                         \
57       DEBUGASSERT(!(x)->state.conncache_lock);                          \
58       (x)->state.conncache_lock = TRUE;                                 \
59     }                                                                   \
60   } while(0)
61 
62 #define CONNCACHE_UNLOCK(x)                                             \
63   do {                                                                  \
64     if((x)->share) {                                                    \
65       DEBUGASSERT((x)->state.conncache_lock);                           \
66       (x)->state.conncache_lock = FALSE;                                \
67       Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT);                   \
68     }                                                                   \
69   } while(0)
70 #else
71 #define CONNCACHE_LOCK(x) if((x)->share)                                \
72     Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, CURL_LOCK_ACCESS_SINGLE)
73 #define CONNCACHE_UNLOCK(x) if((x)->share)              \
74     Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT)
75 #endif
76 
77 struct connectbundle {
78   int multiuse;                 /* supports multi-use */
79   size_t num_connections;       /* Number of connections in the bundle */
80   struct Curl_llist conn_list;  /* The connectdata members of the bundle */
81 };
82 
83 /* returns 1 on error, 0 is fine */
84 int Curl_conncache_init(struct conncache *, int size);
85 void Curl_conncache_destroy(struct conncache *connc);
86 
87 /* return the correct bundle, to a host or a proxy */
88 struct connectbundle *Curl_conncache_find_bundle(struct Curl_easy *data,
89                                                  struct connectdata *conn,
90                                                  struct conncache *connc,
91                                                  const char **hostp);
92 /* returns number of connections currently held in the connection cache */
93 size_t Curl_conncache_size(struct Curl_easy *data);
94 
95 bool Curl_conncache_return_conn(struct Curl_easy *data,
96                                 struct connectdata *conn);
97 CURLcode Curl_conncache_add_conn(struct Curl_easy *data) WARN_UNUSED_RESULT;
98 void Curl_conncache_remove_conn(struct Curl_easy *data,
99                                 struct connectdata *conn,
100                                 bool lock);
101 bool Curl_conncache_foreach(struct Curl_easy *data,
102                             struct conncache *connc,
103                             void *param,
104                             int (*func)(struct Curl_easy *data,
105                                         struct connectdata *conn,
106                                         void *param));
107 
108 struct connectdata *
109 Curl_conncache_find_first_connection(struct conncache *connc);
110 
111 struct connectdata *
112 Curl_conncache_extract_bundle(struct Curl_easy *data,
113                               struct connectbundle *bundle);
114 struct connectdata *
115 Curl_conncache_extract_oldest(struct Curl_easy *data);
116 void Curl_conncache_close_all_connections(struct conncache *connc);
117 void Curl_conncache_print(struct conncache *connc);
118 
119 #endif /* HEADER_CURL_CONNCACHE_H */
120