--- deadwood-3.1.03/src/DwHash.c 2011-12-21 13:44:38.000000000 -0600 +++ deadwood-3.1.04/src/DwHash.c 2012-01-04 11:00:21.439605684 -0600 @@ -1,4 +1,4 @@ -/* Copyright (c) 2007-2010 Sam Trenholme +/* Copyright (c) 2007-2012 Sam Trenholme * * TERMS * @@ -478,9 +478,11 @@ } /* Used by dwh_add: Initialize a new element in the dw_hash */ -dw_element *dwh_init_element(dw_str *key, dw_str *value, int32_t ttl) { +dw_element *dwh_init_element(dw_str *key, dw_str *value, int32_t ttl, + dw_hash *hash) { dw_element *new = 0; dw_str *key_copy = 0, *val_copy = 0; + dw_element *seek = 0; /* Initialize the new element */ /* Destructor: This element is destroyed when either the user @@ -500,12 +502,21 @@ new->fila = 0; /* About to be filled in, but not here */ new->immutable = 0; /* Normal element, can be deleted */ - if(ttl < 30) { + if(ttl < 30 && ttl != -2) { ttl = 30; } else if(ttl > 63072000) { /* Two years */ ttl = 63072000; } - new->expire = get_time() + ((int64_t)ttl << 8); + if(ttl != -2) { + new->expire = get_time() + ((int64_t)ttl << 8); + } else { /* Special case: Retain TTL */ + seek = dwh_grep(hash,key); + if(seek == 0) { /* Not found; use a short TTL */ + new->expire = get_time() + 30; + } else { /* Do not change TTL */ + new->expire = seek->expire; + } + } new->next = 0; /* About to get filled in, but not here */ new->prev = 0; /* About to be filled in, but not here */ @@ -569,7 +580,11 @@ /* Add a (key,value) pair to a given dw_hash. If an element with the * same name already exists, remove the element already in the - * hash and replace it with this element */ + * hash and replace it with this element. + * + * If ttl has a value of -2, this is a special value that means + * "however long an already existing element has; otherwise 30 seconds" + */ int dwh_add(dw_hash *hash, dw_str *key, dw_str *value, int32_t ttl, int use_fila) { dw_element *new = 0; @@ -582,8 +597,8 @@ goto catch_dwh_add; } - if(ttl > 0) { - new = dwh_init_element(key,value,ttl); + if(ttl > 0 || ttl == -2) { + new = dwh_init_element(key,value,ttl,hash); if(new == 0) { goto catch_dwh_add; } --- deadwood-3.1.03/src/DwDnsStr.c 2011-12-21 13:44:38.000000000 -0600 +++ deadwood-3.1.04/src/DwDnsStr.c 2012-01-04 11:02:21.193599123 -0600 @@ -1,4 +1,4 @@ -/* Copyright (c) 2009-2010 Sam Trenholme +/* Copyright (c) 2009-2012 Sam Trenholme * * TERMS * @@ -463,7 +463,7 @@ if(dwc_ttl_age(fetch,ttl) == -1) { goto catch_dwc_process; } - dwh_add(cache,query,fetch,ttl,1); + dwh_add(cache,query,fetch,-2,1); dw_destroy(fetch); fetch = 0; } @@ -481,7 +481,7 @@ if(ttl == -1) { goto catch_dwc_process; } - dwh_add(cache,query,fetch,ttl,1); + dwh_add(cache,query,fetch,-2,1); } catch_dwc_process: --- deadwood-3.1.03/sqa/do.tests 2011-11-01 10:25:23.000000000 -0600 +++ deadwood-3.1.04/sqa/do.tests 2012-01-04 11:08:17.331282952 -0600 @@ -95,6 +95,7 @@ TESTS=$TESTS"sqa_cname_chain " TESTS=$TESTS"sqa_roy_arends " TESTS=$TESTS"sqa_tcp_buffering " +TESTS=$TESTS"sqa_ttl_expire " # This test is last since it recompiles Deadwood # We run it seven times because some memleaks only happen some of the time TESTS=$TESTS"sqa_valgrind "