Coverage Report

Created: 2024-02-25 06:14

/src/PROJ/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->state.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 %" CURL_FORMAT_CURL_OFF_T ". "
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
0
  unsigned int maxconnects = !data->multi->maxconnects ?
383
0
    data->multi->num_easy * 4: data->multi->maxconnects;
384
0
  struct connectdata *conn_candidate = NULL;
385
386
0
  conn->lastused = Curl_now(); /* it was used up until now */
387
0
  if(maxconnects && Curl_conncache_size(data) > maxconnects) {
388
0
    infof(data, "Connection cache is full, closing the oldest one");
389
390
0
    conn_candidate = Curl_conncache_extract_oldest(data);
391
0
    if(conn_candidate) {
392
      /* Use the closure handle for this disconnect so that anything that
393
         happens during the disconnect is not stored and associated with the
394
         'data' handle which already just finished a transfer and it is
395
         important that details from this (unrelated) disconnect does not
396
         taint meta-data in the data handle. */
397
0
      struct conncache *connc = data->state.conn_cache;
398
0
      Curl_disconnect(connc->closure_handle, conn_candidate,
399
                      /* dead_connection */ FALSE);
400
0
    }
401
0
  }
402
403
0
  return (conn_candidate == conn) ? FALSE : TRUE;
404
405
0
}
406
407
/*
408
 * This function finds the connection in the connection bundle that has been
409
 * unused for the longest time.
410
 *
411
 * Does not lock the connection cache!
412
 *
413
 * Returns the pointer to the oldest idle connection, or NULL if none was
414
 * found.
415
 */
416
struct connectdata *
417
Curl_conncache_extract_bundle(struct Curl_easy *data,
418
                              struct connectbundle *bundle)
419
0
{
420
0
  struct Curl_llist_element *curr;
421
0
  timediff_t highscore = -1;
422
0
  timediff_t score;
423
0
  struct curltime now;
424
0
  struct connectdata *conn_candidate = NULL;
425
0
  struct connectdata *conn;
426
427
0
  (void)data;
428
429
0
  now = Curl_now();
430
431
0
  curr = bundle->conn_list.head;
432
0
  while(curr) {
433
0
    conn = curr->ptr;
434
435
0
    if(!CONN_INUSE(conn)) {
436
      /* Set higher score for the age passed since the connection was used */
437
0
      score = Curl_timediff(now, conn->lastused);
438
439
0
      if(score > highscore) {
440
0
        highscore = score;
441
0
        conn_candidate = conn;
442
0
      }
443
0
    }
444
0
    curr = curr->next;
445
0
  }
446
0
  if(conn_candidate) {
447
    /* remove it to prevent another thread from nicking it */
448
0
    bundle_remove_conn(bundle, conn_candidate);
449
0
    data->state.conn_cache->num_conn--;
450
0
    DEBUGF(infof(data, "The cache now contains %zu members",
451
0
                 data->state.conn_cache->num_conn));
452
0
  }
453
454
0
  return conn_candidate;
455
0
}
456
457
/*
458
 * This function finds the connection in the connection cache that has been
459
 * unused for the longest time and extracts that from the bundle.
460
 *
461
 * Returns the pointer to the connection, or NULL if none was found.
462
 */
463
struct connectdata *
464
Curl_conncache_extract_oldest(struct Curl_easy *data)
465
0
{
466
0
  struct conncache *connc = data->state.conn_cache;
467
0
  struct Curl_hash_iterator iter;
468
0
  struct Curl_llist_element *curr;
469
0
  struct Curl_hash_element *he;
470
0
  timediff_t highscore =- 1;
471
0
  timediff_t score;
472
0
  struct curltime now;
473
0
  struct connectdata *conn_candidate = NULL;
474
0
  struct connectbundle *bundle;
475
0
  struct connectbundle *bundle_candidate = NULL;
476
477
0
  now = Curl_now();
478
479
0
  CONNCACHE_LOCK(data);
480
0
  Curl_hash_start_iterate(&connc->hash, &iter);
481
482
0
  he = Curl_hash_next_element(&iter);
483
0
  while(he) {
484
0
    struct connectdata *conn;
485
486
0
    bundle = he->ptr;
487
488
0
    curr = bundle->conn_list.head;
489
0
    while(curr) {
490
0
      conn = curr->ptr;
491
492
0
      if(!CONN_INUSE(conn) && !conn->bits.close &&
493
0
         !conn->connect_only) {
494
        /* Set higher score for the age passed since the connection was used */
495
0
        score = Curl_timediff(now, conn->lastused);
496
497
0
        if(score > highscore) {
498
0
          highscore = score;
499
0
          conn_candidate = conn;
500
0
          bundle_candidate = bundle;
501
0
        }
502
0
      }
503
0
      curr = curr->next;
504
0
    }
505
506
0
    he = Curl_hash_next_element(&iter);
507
0
  }
508
0
  if(conn_candidate) {
509
    /* remove it to prevent another thread from nicking it */
510
0
    bundle_remove_conn(bundle_candidate, conn_candidate);
511
0
    connc->num_conn--;
512
0
    DEBUGF(infof(data, "The cache now contains %zu members",
513
0
                 connc->num_conn));
514
0
  }
515
0
  CONNCACHE_UNLOCK(data);
516
517
0
  return conn_candidate;
518
0
}
519
520
void Curl_conncache_close_all_connections(struct conncache *connc)
521
0
{
522
0
  struct connectdata *conn;
523
0
  SIGPIPE_VARIABLE(pipe_st);
524
0
  if(!connc->closure_handle)
525
0
    return;
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
  sigpipe_ignore(connc->closure_handle, &pipe_st);
540
541
0
  Curl_hostcache_clean(connc->closure_handle,
542
0
                       connc->closure_handle->dns.hostcache);
543
0
  Curl_close(&connc->closure_handle);
544
0
  sigpipe_restore(&pipe_st);
545
0
}
546
547
#if 0
548
/* Useful for debugging the connection cache */
549
void Curl_conncache_print(struct conncache *connc)
550
{
551
  struct Curl_hash_iterator iter;
552
  struct Curl_llist_element *curr;
553
  struct Curl_hash_element *he;
554
555
  if(!connc)
556
    return;
557
558
  fprintf(stderr, "=Bundle cache=\n");
559
560
  Curl_hash_start_iterate(connc->hash, &iter);
561
562
  he = Curl_hash_next_element(&iter);
563
  while(he) {
564
    struct connectbundle *bundle;
565
    struct connectdata *conn;
566
567
    bundle = he->ptr;
568
569
    fprintf(stderr, "%s -", he->key);
570
    curr = bundle->conn_list->head;
571
    while(curr) {
572
      conn = curr->ptr;
573
574
      fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
575
      curr = curr->next;
576
    }
577
    fprintf(stderr, "\n");
578
579
    he = Curl_hash_next_element(&iter);
580
  }
581
}
582
#endif