Commit d2b36e46 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

global dns cache: didn't work [regression]

CURLOPT_DNS_USE_GLOBAL_CACHE broke in commit c4312741 (been
broken since the libcurl 7.29.0 release). While this option has been
documented as deprecated for almost a decade and nobody even reported
this bug, it should remain functional.

Added test case 1512 to verify
parent 27f8c93d
Loading
Loading
Loading
Loading
+15 −12
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
@@ -681,12 +681,14 @@ clean_up:
 * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
 * made, the struct may be destroyed due to pruning. It is important that only
 * one unlock is made for each Curl_resolv() call.
 *
 * May be called with 'data' == NULL for global cache.
 */
void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
{
  DEBUGASSERT(dns && (dns->inuse>0));

  if(data->share)
  if(data && data->share)
    Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);

  dns->inuse--;
@@ -697,7 +699,7 @@ void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
    free(dns);
  }

  if(data->share)
  if(data && data->share)
    Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
}

@@ -734,22 +736,23 @@ static int hostcache_inuse(void *data, void *hc)
  return 1; /* free all entries */
}

void Curl_hostcache_clean(struct SessionHandle *data)
/*
 * Curl_hostcache_clean()
 *
 * This _can_ be called with 'data' == NULL but then of course no locking
 * can be done!
 */

void Curl_hostcache_clean(struct SessionHandle *data,
                          struct curl_hash *hash)
{
  /* Entries added to the hostcache with the CURLOPT_RESOLVE function are
   * still present in the cache with the inuse counter set to 1. Detect them
   * and cleanup!
   */
  Curl_hash_clean_with_criterium(data->dns.hostcache, data, hostcache_inuse);
  Curl_hash_clean_with_criterium(hash, data, hostcache_inuse);
}

void Curl_hostcache_destroy(struct SessionHandle *data)
{
  Curl_hostcache_clean(data);
  Curl_hash_destroy(data->dns.hostcache);
  data->dns.hostcachetype = HCACHE_NONE;
  data->dns.hostcache = NULL;
}

CURLcode Curl_loadhostpairs(struct SessionHandle *data)
{
+2 −2
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
@@ -203,7 +203,7 @@ CURLcode Curl_set_dns_servers(struct SessionHandle *data, char *servers);
/*
 * Clean off entries from the cache
 */
void Curl_hostcache_clean(struct SessionHandle *data);
void Curl_hostcache_clean(struct SessionHandle *data, struct curl_hash *hash);

/*
 * Destroy the hostcache of this handle.
+33 −25
Original line number Diff line number Diff line
@@ -334,7 +334,6 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
                                CURL *easy_handle)
{
  struct curl_llist *timeoutlist;
  struct SessionHandle *easy;
  struct Curl_multi *multi = (struct Curl_multi *)multi_handle;
  struct SessionHandle *data = (struct SessionHandle *)easy_handle;
  struct SessionHandle *new_closure = NULL;
@@ -348,8 +347,8 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
  if(!GOOD_EASY_HANDLE(easy_handle))
    return CURLM_BAD_EASY_HANDLE;

  /* Prevent users from adding same easy handle more than
     once and prevent adding to more than one multi stack */
  /* Prevent users from adding same easy handle more than once and prevent
     adding to more than one multi stack */
  if(data->multi)
    /* possibly we should create a new unique error code for this condition */
    return CURLM_BAD_EASY_HANDLE;
@@ -359,13 +358,11 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
  if(!timeoutlist)
    return CURLM_OUT_OF_MEMORY;

  easy = data;

  /* In case multi handle has no hostcache yet, allocate one */
  if(!multi->hostcache) {
    hostcache = Curl_mk_dnscache();
    if(!hostcache) {
      free(easy);
      free(data);
      Curl_llist_destroy(timeoutlist, NULL);
      return CURLM_OUT_OF_MEMORY;
    }
@@ -377,7 +374,7 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
    new_closure = (struct SessionHandle *)curl_easy_init();
    if(!new_closure) {
      Curl_hash_destroy(hostcache);
      free(easy);
      free(data);
      Curl_llist_destroy(timeoutlist, NULL);
      return CURLM_OUT_OF_MEMORY;
    }
@@ -408,18 +405,28 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
  timeoutlist = NULL;

  /* set the easy handle */
  multistate(easy, CURLM_STATE_INIT);
  multistate(data, CURLM_STATE_INIT);

  if((data->set.global_dns_cache) &&
     (data->dns.hostcachetype != HCACHE_GLOBAL)) {
    /* global dns cache was requested but still isn't */
    struct curl_hash *global = Curl_global_host_cache_init();
    if(global) {
      /* only do this if the global cache init works */
      data->dns.hostcache = global;
      data->dns.hostcachetype = HCACHE_GLOBAL;
    }
  }
  /* for multi interface connections, we share DNS cache automatically if the
     easy handle's one is currently not set. */
  if(!easy->dns.hostcache ||
     (easy->dns.hostcachetype == HCACHE_NONE)) {
    easy->dns.hostcache = multi->hostcache;
    easy->dns.hostcachetype = HCACHE_MULTI;
  else if(!data->dns.hostcache ||
     (data->dns.hostcachetype == HCACHE_NONE)) {
    data->dns.hostcache = multi->hostcache;
    data->dns.hostcachetype = HCACHE_MULTI;
  }

  /* Point to the multi's connection cache */
  easy->state.conn_cache = multi->conn_cache;
  data->state.conn_cache = multi->conn_cache;

  /* This adds the new entry at the 'end' of the doubly-linked circular
     list of SessionHandle structs to try and maintain a FIFO queue so
@@ -427,22 +434,22 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,

  /* We add this new entry last in the list. */

  easy->next = NULL; /* end of the line */
  data->next = NULL; /* end of the line */
  if(multi->easyp) {
    struct SessionHandle *last = multi->easylp;
    last->next = easy;
    easy->prev = last;
    multi->easylp = easy; /* the new last node */
    last->next = data;
    data->prev = last;
    multi->easylp = data; /* the new last node */
  }
  else {
    /* first node, make both prev and next be NULL! */
    easy->next = NULL;
    easy->prev = NULL;
    multi->easylp = multi->easyp = easy; /* both first and last */
    data->next = NULL;
    data->prev = NULL;
    multi->easylp = multi->easyp = data; /* both first and last */
  }

  /* make the SessionHandle refer back to this multi handle */
  Curl_easy_addmulti(easy_handle, multi_handle);
  Curl_easy_addmulti(data, multi_handle);

  /* Set the timeout for this handle to expire really soon so that it will
     be taken care of even when this handle is added in the midst of operation
@@ -450,7 +457,7 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
     sockets that time-out or have actions will be dealt with. Since this
     handle has no action yet, we make sure it times out to get things to
     happen. */
  Curl_expire(easy, 1);
  Curl_expire(data, 1);

  /* increase the node-counter */
  multi->num_easy++;
@@ -1811,7 +1818,8 @@ CURLMcode curl_multi_cleanup(CURLM *multi_handle)

    if(multi->closure_handle) {
      multi->closure_handle->dns.hostcache = multi->hostcache;
      Curl_hostcache_clean(multi->closure_handle);
      Curl_hostcache_clean(multi->closure_handle,
                           multi->closure_handle->dns.hostcache);

      Curl_close(multi->closure_handle);
      multi->closure_handle = NULL;
@@ -1833,7 +1841,7 @@ CURLMcode curl_multi_cleanup(CURLM *multi_handle)
      nexteasy=easy->next;
      if(easy->dns.hostcachetype == HCACHE_MULTI) {
        /* clear out the usage of the shared DNS cache */
        Curl_hostcache_clean(easy);
        Curl_hostcache_clean(easy, easy->dns.hostcache);
        easy->dns.hostcache = NULL;
        easy->dns.hostcachetype = HCACHE_NONE;
      }
+1 −1
Original line number Diff line number Diff line
@@ -113,7 +113,7 @@ test1400 test1401 test1402 test1403 test1404 test1405 test1406 test1407 \
test1408 test1409 test1410          test1412 test1413 test1414 \
\
test1500 test1501 test1502 test1503 test1504 test1505 test1506 test1507 \
test1508 test1509 test1510 test1511 \
test1508 test1509 test1510 test1511 test1512 \
\
test1900 test1901 test1902 test1903 \
\

tests/data/test1512

0 → 100644
+80 −0
Original line number Diff line number Diff line
<testcase>
<info>
<keywords>
HTTP
GLOBAL DNS CACHE
</keywords>
</info>

# Server-side
<reply>
<data1>
HTTP/1.1 200 OK
Date: Thu, 09 Nov 2010 14:49:00 GMT
Server: test-server/fake
Content-Length: 47

file contents should appear once for each file
</data1>
<data2>
HTTP/1.1 200 OK
Date: Thu, 09 Nov 2010 14:49:00 GMT
Server: test-server/fake
Content-Length: 47

file contents should appear once for each file
</data2>
<data3>
HTTP/1.1 200 OK
Date: Thu, 09 Nov 2010 14:49:00 GMT
Server: test-server/fake
Content-Length: 47

file contents should appear once for each file
</data3>
<data4>
HTTP/1.1 200 OK
Date: Thu, 09 Nov 2010 14:49:00 GMT
Server: test-server/fake
Content-Length: 47

file contents should appear once for each file
</data4>
</reply>

# Client-side
<client>
<server>
http
</server>
<tool>
lib1512
</tool>
 <name>
GLOBAL CACHE test over two easy performs
 </name>
 <command>
http://%HOSTIP:%HTTPPORT/path/1512 %HOSTIP %HTTPPORT
</command>
</client>

# Verify data after the test has been "shot"
<verify>
<protocol>
GET /path/15120001 HTTP/1.1
Host: %HOSTIP:%HTTPPORT
Accept: */*

GET /path/15120002 HTTP/1.1
Host: %HOSTIP:%HTTPPORT
Accept: */*

</protocol>
<strip>
^Host:.*
</strip>
<stripfile>
$_ = '' if (($_ !~ /left intact/) && ($_ !~ /Closing connection/))
</stripfile>
</verify>
</testcase>
Loading