X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fresolve%2Fresolved-dns-cache.c;h=33ca4d1a4549fc184b8a53c1e3efb4c291c7bac5;hp=6ea5d49fc49e290f115aa2957c7bf921d2257e40;hb=30fbcf24460263961855e0ce4d8867fd2af3e149;hpb=7e8e0422aeb16f2a09a40546c61df753d10029b6 diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c index 6ea5d49fc..33ca4d1a4 100644 --- a/src/resolve/resolved-dns-cache.c +++ b/src/resolve/resolved-dns-cache.c @@ -28,6 +28,26 @@ /* We never keep any item longer than 10min in our cache */ #define CACHE_TTL_MAX_USEC (10 * USEC_PER_MINUTE) +typedef enum DnsCacheItemType DnsCacheItemType; +typedef struct DnsCacheItem DnsCacheItem; + +enum DnsCacheItemType { + DNS_CACHE_POSITIVE, + DNS_CACHE_NODATA, + DNS_CACHE_NXDOMAIN, +}; + +struct DnsCacheItem { + DnsResourceKey *key; + DnsResourceRecord *rr; + usec_t until; + DnsCacheItemType type; + unsigned prioq_idx; + int owner_family; + union in_addr_union owner_address; + LIST_FIELDS(DnsCacheItem, by_key); +}; + static void dns_cache_item_free(DnsCacheItem *i) { if (!i) return; @@ -135,7 +155,7 @@ void dns_cache_prune(DnsCache *c) { break; if (t <= 0) - t = now(CLOCK_MONOTONIC); + t = now(CLOCK_BOOTTIME); if (i->until > t) break; @@ -157,14 +177,16 @@ static int dns_cache_item_prioq_compare_func(const void *a, const void *b) { return 0; } -static int init_cache(DnsCache *c) { +static int dns_cache_init(DnsCache *c) { int r; + assert(c); + r = prioq_ensure_allocated(&c->by_expiry, dns_cache_item_prioq_compare_func); if (r < 0) return r; - r = hashmap_ensure_allocated(&c->by_key, dns_resource_key_hash_func, dns_resource_key_compare_func); + r = hashmap_ensure_allocated(&c->by_key, &dns_resource_key_hash_ops); if (r < 0) return r; @@ -204,7 +226,7 @@ static DnsCacheItem* dns_cache_get(DnsCache *c, DnsResourceRecord *rr) { assert(rr); LIST_FOREACH(by_key, i, hashmap_get(c->by_key, rr->key)) - if (i->rr && dns_resource_record_equal(i->rr, rr)) + if (i->rr && dns_resource_record_equal(i->rr, rr) > 0) return i; return NULL; @@ -236,13 +258,20 @@ static void dns_cache_item_update_positive(DnsCache *c, DnsCacheItem *i, DnsReso prioq_reshuffle(c->by_expiry, i, &i->prioq_idx); } -static int dns_cache_put_positive(DnsCache *c, DnsResourceRecord *rr, usec_t timestamp) { +static int dns_cache_put_positive( + DnsCache *c, + DnsResourceRecord *rr, + usec_t timestamp, + int owner_family, + const union in_addr_union *owner_address) { + _cleanup_(dns_cache_item_freep) DnsCacheItem *i = NULL; DnsCacheItem *existing; int r; assert(c); assert(rr); + assert(owner_address); /* New TTL is 0? Delete the entry... */ if (rr->ttl <= 0) { @@ -250,6 +279,11 @@ static int dns_cache_put_positive(DnsCache *c, DnsResourceRecord *rr, usec_t tim return 0; } + if (rr->key->class == DNS_CLASS_ANY) + return 0; + if (rr->key->type == DNS_TYPE_ANY) + return 0; + /* Entry exists already? Update TTL and timestamp */ existing = dns_cache_get(c, rr); if (existing) { @@ -258,7 +292,7 @@ static int dns_cache_put_positive(DnsCache *c, DnsResourceRecord *rr, usec_t tim } /* Otherwise, add the new RR */ - r = init_cache(c); + r = dns_cache_init(c); if (r < 0) return r; @@ -273,6 +307,8 @@ static int dns_cache_put_positive(DnsCache *c, DnsResourceRecord *rr, usec_t tim i->rr = dns_resource_record_ref(rr); i->until = timestamp + MIN(i->rr->ttl * USEC_PER_SEC, CACHE_TTL_MAX_USEC); i->prioq_idx = PRIOQ_IDX_NULL; + i->owner_family = owner_family; + i->owner_address = *owner_address; r = dns_cache_link_item(c, i); if (r < 0) @@ -282,19 +318,35 @@ static int dns_cache_put_positive(DnsCache *c, DnsResourceRecord *rr, usec_t tim return 0; } -static int dns_cache_put_negative(DnsCache *c, DnsResourceKey *key, int rcode, usec_t timestamp, uint32_t soa_ttl) { +static int dns_cache_put_negative( + DnsCache *c, + DnsResourceKey *key, + int rcode, + usec_t timestamp, + uint32_t soa_ttl, + int owner_family, + const union in_addr_union *owner_address) { + _cleanup_(dns_cache_item_freep) DnsCacheItem *i = NULL; int r; assert(c); assert(key); + assert(owner_address); dns_cache_remove(c, key); + if (key->class == DNS_CLASS_ANY) + return 0; + if (key->type == DNS_TYPE_ANY) + return 0; + if (soa_ttl <= 0) + return 0; + if (!IN_SET(rcode, DNS_RCODE_SUCCESS, DNS_RCODE_NXDOMAIN)) return 0; - r = init_cache(c); + r = dns_cache_init(c); if (r < 0) return r; @@ -308,6 +360,8 @@ static int dns_cache_put_negative(DnsCache *c, DnsResourceKey *key, int rcode, u i->key = dns_resource_key_ref(key); i->until = timestamp + MIN(soa_ttl * USEC_PER_SEC, CACHE_TTL_MAX_USEC); i->prioq_idx = PRIOQ_IDX_NULL; + i->owner_family = owner_family; + i->owner_address = *owner_address; r = dns_cache_link_item(c, i); if (r < 0) @@ -317,17 +371,30 @@ static int dns_cache_put_negative(DnsCache *c, DnsResourceKey *key, int rcode, u return 0; } -int dns_cache_put(DnsCache *c, DnsQuestion *q, int rcode, DnsAnswer *answer, usec_t timestamp) { +int dns_cache_put( + DnsCache *c, + DnsQuestion *q, + int rcode, + DnsAnswer *answer, + unsigned max_rrs, + usec_t timestamp, + int owner_family, + const union in_addr_union *owner_address) { + unsigned i; int r; assert(c); - assert(answer); + assert(q); /* First, delete all matching old RRs, so that we only keep * complete by_key in place. */ for (i = 0; i < q->n_keys; i++) dns_cache_remove(c, q->keys[i]); + + if (!answer) + return 0; + for (i = 0; i < answer->n_rrs; i++) dns_cache_remove(c, answer->rrs[i]->key); @@ -342,11 +409,11 @@ int dns_cache_put(DnsCache *c, DnsQuestion *q, int rcode, DnsAnswer *answer, use dns_cache_make_space(c, answer->n_rrs + q->n_keys); if (timestamp <= 0) - timestamp = now(CLOCK_MONOTONIC); + timestamp = now(CLOCK_BOOTTIME); /* Second, add in positive entries for all contained RRs */ - for (i = 0; i < answer->n_rrs; i++) { - r = dns_cache_put_positive(c, answer->rrs[i], timestamp); + for (i = 0; i < MIN(max_rrs, answer->n_rrs); i++) { + r = dns_cache_put_positive(c, answer->rrs[i], timestamp, owner_family, owner_address); if (r < 0) goto fail; } @@ -367,7 +434,7 @@ int dns_cache_put(DnsCache *c, DnsQuestion *q, int rcode, DnsAnswer *answer, use if (r == 0) continue; - r = dns_cache_put_negative(c, q->keys[i], rcode, timestamp, MIN(soa->soa.minimum, soa->ttl)); + r = dns_cache_put_negative(c, q->keys[i], rcode, timestamp, MIN(soa->soa.minimum, soa->ttl), owner_family, owner_address); if (r < 0) goto fail; } @@ -394,6 +461,7 @@ int dns_cache_lookup(DnsCache *c, DnsQuestion *q, int *rcode, DnsAnswer **ret) { assert(c); assert(q); + assert(rcode); assert(ret); if (q->n_keys <= 0) { @@ -405,6 +473,14 @@ int dns_cache_lookup(DnsCache *c, DnsQuestion *q, int *rcode, DnsAnswer **ret) { for (i = 0; i < q->n_keys; i++) { DnsCacheItem *j; + if (q->keys[i]->type == DNS_TYPE_ANY || + q->keys[i]->class == DNS_CLASS_ANY) { + /* If we have ANY lookups we simply refresh */ + *ret = NULL; + *rcode = 0; + return 0; + } + j = hashmap_get(c->by_key, q->keys[i]); if (!j) { /* If one question cannot be answered we need to refresh */ @@ -450,3 +526,39 @@ int dns_cache_lookup(DnsCache *c, DnsQuestion *q, int *rcode, DnsAnswer **ret) { return n; } + +int dns_cache_check_conflicts(DnsCache *cache, DnsResourceRecord *rr, int owner_family, const union in_addr_union *owner_address) { + DnsCacheItem *i, *first; + bool same_owner = true; + + assert(cache); + assert(rr); + + dns_cache_prune(cache); + + /* See if there's a cache entry for the same key. If there + * isn't there's no conflict */ + first = hashmap_get(cache->by_key, rr->key); + if (!first) + return 0; + + /* See if the RR key is owned by the same owner, if so, there + * isn't a conflict either */ + LIST_FOREACH(by_key, i, first) { + if (i->owner_family != owner_family || + !in_addr_equal(owner_family, &i->owner_address, owner_address)) { + same_owner = false; + break; + } + } + if (same_owner) + return 0; + + /* See if there's the exact same RR in the cache. If yes, then + * there's no conflict. */ + if (dns_cache_get(cache, rr)) + return 0; + + /* There's a conflict */ + return 1; +}