Coverage Report

Created: 2024-05-04 12:45

/proc/self/cwd/external/curl/lib/conncache.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Linus Nielsen Feltzing, <linus@haxx.se>
9
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
10
 *
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.se/docs/copyright.html.
14
 *
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.
18
 *
19
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20
 * KIND, either express or implied.
21
 *
22
 * SPDX-License-Identifier: curl
23
 *
24
 ***************************************************************************/
25
26
#include "curl_setup.h"
27
28
#include <curl/curl.h>
29
30
#include "urldata.h"
31
#include "url.h"
32
#include "progress.h"
33
#include "multiif.h"
34
#include "sendf.h"
35
#include "conncache.h"
36
#include "share.h"
37
#include "sigpipe.h"
38
#include "connect.h"
39
#include "strcase.h"
40
41
/* The last 3 #include files should be in this order */
42
#include "curl_printf.h"
43
#include "curl_memory.h"
44
#include "memdebug.h"
45
46
#define HASHKEY_SIZE 128
47
48
static CURLcode bundle_create(struct connectbundle **bundlep)
49
0
{
50
0
  DEBUGASSERT(*bundlep == NULL);
51
0
  *bundlep = malloc(sizeof(struct connectbundle));
52
0
  if(!*bundlep)
53
0
    return CURLE_OUT_OF_MEMORY;
54
55
0
  (*bundlep)->num_connections = 0;
56
0
  (*bundlep)->multiuse = BUNDLE_UNKNOWN;
57
58
0
  Curl_llist_init(&(*bundlep)->conn_list, NULL);
59
0
  return CURLE_OK;
60
0
}
61
62
static void bundle_destroy(struct connectbundle *bundle)
63
0
{
64
0
  free(bundle);
65
0
}
66
67
/* Add a connection to a bundle */
68
static void bundle_add_conn(struct connectbundle *bundle,
69
                            struct connectdata *conn)
70
0
{
71
0
  Curl_llist_insert_next(&bundle->conn_list, bundle->conn_list.tail, conn,
72
0
                         &conn->bundle_node);
73
0
  conn->bundle = bundle;
74
0
  bundle->num_connections++;
75
0
}
76
77
/* Remove a connection from a bundle */
78
static int bundle_remove_conn(struct connectbundle *bundle,
79
                              struct connectdata *conn)
80
0
{
81
0
  struct Curl_llist_element *curr;
82
83
0
  curr = bundle->conn_list.head;
84
0
  while(curr) {
85
0
    if(curr->ptr == conn) {
86
0
      Curl_llist_remove(&bundle->conn_list, curr, NULL);
87
0
      bundle->num_connections--;
88
0
      conn->bundle = NULL;
89
0
      return 1; /* we removed a handle */
90
0
    }
91
0
    curr = curr->next;
92
0
  }
93
0
  DEBUGASSERT(0);
94
0
  return 0;
95
0
}
96
97
static void free_bundle_hash_entry(void *freethis)
98
0
{
99
0
  struct connectbundle *b = (struct connectbundle *) freethis;
100
101
0
  bundle_destroy(b);
102
0
}
103
104
int Curl_conncache_init(struct conncache *connc, int size)
105
0
{
106
  /* allocate a new easy handle to use when closing cached connections */
107
0
  connc->closure_handle = curl_easy_init();
108
0
  if(!connc->closure_handle)
109
0
    return 1; /* bad */
110
0
  connc->closure_handle->internal = true;
111
112
0
  Curl_hash_init(&connc->hash, size, Curl_hash_str,
113
0
                 Curl_str_key_compare, free_bundle_hash_entry);
114
0
  connc->closure_handle->state.conn_cache = connc;
115
116
0
  return 0; /* good */
117
0
}
118
119
void Curl_conncache_destroy(struct conncache *connc)
120
0
{
121
0
  if(connc)
122
0
    Curl_hash_destroy(&connc->hash);
123
0
}
124
125
/* creates a key to find a bundle for this connection */
126
static void hashkey(struct connectdata *conn, char *buf, size_t len)
127
0
{
128
0
  const char *hostname;
129
0
  long port = conn->remote_port;
130
0
  DEBUGASSERT(len >= HASHKEY_SIZE);
131
0
#ifndef CURL_DISABLE_PROXY
132
0
  if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
133
0
    hostname = conn->http_proxy.host.name;
134
0
    port = conn->port;
135
0
  }
136
0
  else
137
0
#endif
138
0
    if(conn->bits.conn_to_host)
139
0
      hostname = conn->conn_to_host.name;
140
0
  else
141
0
    hostname = conn->host.name;
142
143
  /* put the numbers first so that the hostname gets cut off if too long */
144
0
#ifdef ENABLE_IPV6
145
0
  msnprintf(buf, len, "%u/%ld/%s", conn->scope_id, port, hostname);
146
#else
147
  msnprintf(buf, len, "%ld/%s", port, hostname);
148
#endif
149
0
  Curl_strntolower(buf, buf, len);
150
0
}
151
152
/* Returns number of connections currently held in the connection cache.
153
   Locks/unlocks the cache itself!
154
*/
155
size_t Curl_conncache_size(struct Curl_easy *data)
156
0
{
157
0
  size_t num;
158
0
  CONNCACHE_LOCK(data);
159
0
  num = data->state.conn_cache->num_conn;
160
0
  CONNCACHE_UNLOCK(data);
161
0
  return num;
162
0
}
163
164
/* Look up the bundle with all the connections to the same host this
165
   connectdata struct is setup to use.
166
167
   **NOTE**: When it returns, it holds the connection cache lock! */
168
struct connectbundle *
169
Curl_conncache_find_bundle(struct Curl_easy *data,
170
                           struct connectdata *conn,
171
                           struct conncache *connc)
172
0
{
173
0
  struct connectbundle *bundle = NULL;
174
0
  CONNCACHE_LOCK(data);
175
0
  if(connc) {
176
0
    char key[HASHKEY_SIZE];
177
0
    hashkey(conn, key, sizeof(key));
178
0
    bundle = Curl_hash_pick(&connc->hash, key, strlen(key));
179
0
  }
180
181
0
  return bundle;
182
0
}
183
184
static void *conncache_add_bundle(struct conncache *connc,
185
                                  char *key,
186
                                  struct connectbundle *bundle)
187
0
{
188
0
  return Curl_hash_add(&connc->hash, key, strlen(key), bundle);
189
0
}
190
191
static void conncache_remove_bundle(struct conncache *connc,
192
                                    struct connectbundle *bundle)
193
0
{
194
0
  struct Curl_hash_iterator iter;
195
0
  struct Curl_hash_element *he;
196
197
0
  if(!connc)
198
0
    return;
199
200
0
  Curl_hash_start_iterate(&connc->hash, &iter);
201
202
0
  he = Curl_hash_next_element(&iter);
203
0
  while(he) {
204
0
    if(he->ptr == bundle) {
205
      /* The bundle is destroyed by the hash destructor function,
206
         free_bundle_hash_entry() */
207
0
      Curl_hash_delete(&connc->hash, he->key, he->key_len);
208
0
      return;
209
0
    }
210
211
0
    he = Curl_hash_next_element(&iter);
212
0
  }
213
0
}
214
215
CURLcode Curl_conncache_add_conn(struct Curl_easy *data)
216
0
{
217
0
  CURLcode result = CURLE_OK;
218
0
  struct connectbundle *bundle = NULL;
219
0
  struct connectdata *conn = data->conn;
220
0
  struct conncache *connc = data->state.conn_cache;
221
0
  DEBUGASSERT(conn);
222
223
  /* *find_bundle() locks the connection cache */
224
0
  bundle = Curl_conncache_find_bundle(data, conn, data->state.conn_cache);
225
0
  if(!bundle) {
226
0
    char key[HASHKEY_SIZE];
227
228
0
    result = bundle_create(&bundle);
229
0
    if(result) {
230
0
      goto unlock;
231
0
    }
232
233
0
    hashkey(conn, key, sizeof(key));
234
235
0
    if(!conncache_add_bundle(data->state.conn_cache, key, bundle)) {
236
0
      bundle_destroy(bundle);
237
0
      result = CURLE_OUT_OF_MEMORY;
238
0
      goto unlock;
239
0
    }
240
0
  }
241
242
0
  bundle_add_conn(bundle, conn);
243
0
  conn->connection_id = connc->next_connection_id++;
244
0
  connc->num_conn++;
245
246
0
  DEBUGF(infof(data, "Added connection %ld. "
247
0
               "The cache now contains %zu members",
248
0
               conn->connection_id, connc->num_conn));
249
250
0
unlock:
251
0
  CONNCACHE_UNLOCK(data);
252
253
0
  return result;
254
0
}
255
256
/*
257
 * Removes the connectdata object from the connection cache, but the transfer
258
 * still owns this connection.
259
 *
260
 * Pass TRUE/FALSE in the 'lock' argument depending on if the parent function
261
 * already holds the lock or not.
262
 */
263
void Curl_conncache_remove_conn(struct Curl_easy *data,
264
                                struct connectdata *conn, bool lock)
265
0
{
266
0
  struct connectbundle *bundle = conn->bundle;
267
0
  struct conncache *connc = data->state.conn_cache;
268
269
  /* The bundle pointer can be NULL, since this function can be called
270
     due to a failed connection attempt, before being added to a bundle */
271
0
  if(bundle) {
272
0
    if(lock) {
273
0
      CONNCACHE_LOCK(data);
274
0
    }
275
0
    bundle_remove_conn(bundle, conn);
276
0
    if(bundle->num_connections == 0)
277
0
      conncache_remove_bundle(connc, bundle);
278
0
    conn->bundle = NULL; /* removed from it */
279
0
    if(connc) {
280
0
      connc->num_conn--;
281
0
      DEBUGF(infof(data, "The cache now contains %zu members",
282
0
                   connc->num_conn));
283
0
    }
284
0
    if(lock) {
285
0
      CONNCACHE_UNLOCK(data);
286
0
    }
287
0
  }
288
0
}
289
290
/* This function iterates the entire connection cache and calls the function
291
   func() with the connection pointer as the first argument and the supplied
292
   'param' argument as the other.
293
294
   The conncache lock is still held when the callback is called. It needs it,
295
   so that it can safely continue traversing the lists once the callback
296
   returns.
297
298
   Returns 1 if the loop was aborted due to the callback's return code.
299
300
   Return 0 from func() to continue the loop, return 1 to abort it.
301
 */
302
bool Curl_conncache_foreach(struct Curl_easy *data,
303
                            struct conncache *connc,
304
                            void *param,
305
                            int (*func)(struct Curl_easy *data,
306
                                        struct connectdata *conn, void *param))
307
0
{
308
0
  struct Curl_hash_iterator iter;
309
0
  struct Curl_llist_element *curr;
310
0
  struct Curl_hash_element *he;
311
312
0
  if(!connc)
313
0
    return FALSE;
314
315
0
  CONNCACHE_LOCK(data);
316
0
  Curl_hash_start_iterate(&connc->hash, &iter);
317
318
0
  he = Curl_hash_next_element(&iter);
319
0
  while(he) {
320
0
    struct connectbundle *bundle;
321
322
0
    bundle = he->ptr;
323
0
    he = Curl_hash_next_element(&iter);
324
325
0
    curr = bundle->conn_list.head;
326
0
    while(curr) {
327
      /* Yes, we need to update curr before calling func(), because func()
328
         might decide to remove the connection */
329
0
      struct connectdata *conn = curr->ptr;
330
0
      curr = curr->next;
331
332
0
      if(1 == func(data, conn, param)) {
333
0
        CONNCACHE_UNLOCK(data);
334
0
        return TRUE;
335
0
      }
336
0
    }
337
0
  }
338
0
  CONNCACHE_UNLOCK(data);
339
0
  return FALSE;
340
0
}
341
342
/* Return the first connection found in the cache. Used when closing all
343
   connections.
344
345
   NOTE: no locking is done here as this is presumably only done when cleaning
346
   up a cache!
347
*/
348
static struct connectdata *
349
conncache_find_first_connection(struct conncache *connc)
350
0
{
351
0
  struct Curl_hash_iterator iter;
352
0
  struct Curl_hash_element *he;
353
0
  struct connectbundle *bundle;
354
355
0
  Curl_hash_start_iterate(&connc->hash, &iter);
356
357
0
  he = Curl_hash_next_element(&iter);
358
0
  while(he) {
359
0
    struct Curl_llist_element *curr;
360
0
    bundle = he->ptr;
361
362
0
    curr = bundle->conn_list.head;
363
0
    if(curr) {
364
0
      return curr->ptr;
365
0
    }
366
367
0
    he = Curl_hash_next_element(&iter);
368
0
  }
369
370
0
  return NULL;
371
0
}
372
373
/*
374
 * Give ownership of a connection back to the connection cache. Might
375
 * disconnect the oldest existing in there to make space.
376
 *
377
 * Return TRUE if stored, FALSE if closed.
378
 */
379
bool Curl_conncache_return_conn(struct Curl_easy *data,
380
                                struct connectdata *conn)
381
0
{
382
  /* data->multi->maxconnects can be negative, deal with it. */
383
0
  size_t maxconnects =
384
0
    (data->multi->maxconnects < 0) ? data->multi->num_easy * 4:
385
0
    data->multi->maxconnects;
386
0
  struct connectdata *conn_candidate = NULL;
387
388
0
  conn->lastused = Curl_now(); /* it was used up until now */
389
0
  if(maxconnects > 0 &&
390
0
     Curl_conncache_size(data) > maxconnects) {
391
0
    infof(data, "Connection cache is full, closing the oldest one");
392
393
0
    conn_candidate = Curl_conncache_extract_oldest(data);
394
0
    if(conn_candidate) {
395
      /* the winner gets the honour of being disconnected */
396
0
      Curl_disconnect(data, conn_candidate, /* dead_connection */ FALSE);
397
0
    }
398
0
  }
399
400
0
  return (conn_candidate == conn) ? FALSE : TRUE;
401
402
0
}
403
404
/*
405
 * This function finds the connection in the connection bundle that has been
406
 * unused for the longest time.
407
 *
408
 * Does not lock the connection cache!
409
 *
410
 * Returns the pointer to the oldest idle connection, or NULL if none was
411
 * found.
412
 */
413
struct connectdata *
414
Curl_conncache_extract_bundle(struct Curl_easy *data,
415
                              struct connectbundle *bundle)
416
0
{
417
0
  struct Curl_llist_element *curr;
418
0
  timediff_t highscore = -1;
419
0
  timediff_t score;
420
0
  struct curltime now;
421
0
  struct connectdata *conn_candidate = NULL;
422
0
  struct connectdata *conn;
423
424
0
  (void)data;
425
426
0
  now = Curl_now();
427
428
0
  curr = bundle->conn_list.head;
429
0
  while(curr) {
430
0
    conn = curr->ptr;
431
432
0
    if(!CONN_INUSE(conn)) {
433
      /* Set higher score for the age passed since the connection was used */
434
0
      score = Curl_timediff(now, conn->lastused);
435
436
0
      if(score > highscore) {
437
0
        highscore = score;
438
0
        conn_candidate = conn;
439
0
      }
440
0
    }
441
0
    curr = curr->next;
442
0
  }
443
0
  if(conn_candidate) {
444
    /* remove it to prevent another thread from nicking it */
445
0
    bundle_remove_conn(bundle, conn_candidate);
446
0
    data->state.conn_cache->num_conn--;
447
0
    DEBUGF(infof(data, "The cache now contains %zu members",
448
0
                 data->state.conn_cache->num_conn));
449
0
  }
450
451
0
  return conn_candidate;
452
0
}
453
454
/*
455
 * This function finds the connection in the connection cache that has been
456
 * unused for the longest time and extracts that from the bundle.
457
 *
458
 * Returns the pointer to the connection, or NULL if none was found.
459
 */
460
struct connectdata *
461
Curl_conncache_extract_oldest(struct Curl_easy *data)
462
0
{
463
0
  struct conncache *connc = data->state.conn_cache;
464
0
  struct Curl_hash_iterator iter;
465
0
  struct Curl_llist_element *curr;
466
0
  struct Curl_hash_element *he;
467
0
  timediff_t highscore =- 1;
468
0
  timediff_t score;
469
0
  struct curltime now;
470
0
  struct connectdata *conn_candidate = NULL;
471
0
  struct connectbundle *bundle;
472
0
  struct connectbundle *bundle_candidate = NULL;
473
474
0
  now = Curl_now();
475
476
0
  CONNCACHE_LOCK(data);
477
0
  Curl_hash_start_iterate(&connc->hash, &iter);
478
479
0
  he = Curl_hash_next_element(&iter);
480
0
  while(he) {
481
0
    struct connectdata *conn;
482
483
0
    bundle = he->ptr;
484
485
0
    curr = bundle->conn_list.head;
486
0
    while(curr) {
487
0
      conn = curr->ptr;
488
489
0
      if(!CONN_INUSE(conn) && !conn->bits.close &&
490
0
         !conn->connect_only) {
491
        /* Set higher score for the age passed since the connection was used */
492
0
        score = Curl_timediff(now, conn->lastused);
493
494
0
        if(score > highscore) {
495
0
          highscore = score;
496
0
          conn_candidate = conn;
497
0
          bundle_candidate = bundle;
498
0
        }
499
0
      }
500
0
      curr = curr->next;
501
0
    }
502
503
0
    he = Curl_hash_next_element(&iter);
504
0
  }
505
0
  if(conn_candidate) {
506
    /* remove it to prevent another thread from nicking it */
507
0
    bundle_remove_conn(bundle_candidate, conn_candidate);
508
0
    connc->num_conn--;
509
0
    DEBUGF(infof(data, "The cache now contains %zu members",
510
0
                 connc->num_conn));
511
0
  }
512
0
  CONNCACHE_UNLOCK(data);
513
514
0
  return conn_candidate;
515
0
}
516
517
void Curl_conncache_close_all_connections(struct conncache *connc)
518
0
{
519
0
  struct connectdata *conn;
520
0
  char buffer[READBUFFER_MIN + 1];
521
0
  SIGPIPE_VARIABLE(pipe_st);
522
0
  if(!connc->closure_handle)
523
0
    return;
524
0
  connc->closure_handle->state.buffer = buffer;
525
0
  connc->closure_handle->set.buffer_size = READBUFFER_MIN;
526
527
0
  conn = conncache_find_first_connection(connc);
528
0
  while(conn) {
529
0
    sigpipe_ignore(connc->closure_handle, &pipe_st);
530
    /* This will remove the connection from the cache */
531
0
    connclose(conn, "kill all");
532
0
    Curl_conncache_remove_conn(connc->closure_handle, conn, TRUE);
533
0
    Curl_disconnect(connc->closure_handle, conn, FALSE);
534
0
    sigpipe_restore(&pipe_st);
535
536
0
    conn = conncache_find_first_connection(connc);
537
0
  }
538
539
0
  connc->closure_handle->state.buffer = NULL;
540
0
  sigpipe_ignore(connc->closure_handle, &pipe_st);
541
542
0
  Curl_hostcache_clean(connc->closure_handle,
543
0
                       connc->closure_handle->dns.hostcache);
544
0
  Curl_close(&connc->closure_handle);
545
0
  sigpipe_restore(&pipe_st);
546
0
}
547
548
#if 0
549
/* Useful for debugging the connection cache */
550
void Curl_conncache_print(struct conncache *connc)
551
{
552
  struct Curl_hash_iterator iter;
553
  struct Curl_llist_element *curr;
554
  struct Curl_hash_element *he;
555
556
  if(!connc)
557
    return;
558
559
  fprintf(stderr, "=Bundle cache=\n");
560
561
  Curl_hash_start_iterate(connc->hash, &iter);
562
563
  he = Curl_hash_next_element(&iter);
564
  while(he) {
565
    struct connectbundle *bundle;
566
    struct connectdata *conn;
567
568
    bundle = he->ptr;
569
570
    fprintf(stderr, "%s -", he->key);
571
    curr = bundle->conn_list->head;
572
    while(curr) {
573
      conn = curr->ptr;
574
575
      fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
576
      curr = curr->next;
577
    }
578
    fprintf(stderr, "\n");
579
580
    he = Curl_hash_next_element(&iter);
581
  }
582
}
583
#endif