Skip to content

Commit 8e427d9

Browse files
committed
resolved: cache - only allow putting a single question key at a time
Only one key is allowed per transaction now, so let's simplify things and only allow putting one question key into the cache at a time.
1 parent 36d9205 commit 8e427d9

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

src/resolve/resolved-dns-cache.c

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -403,24 +403,24 @@ static int dns_cache_put_negative(
403403

404404
int dns_cache_put(
405405
DnsCache *c,
406-
DnsQuestion *q,
406+
DnsResourceKey *key,
407407
int rcode,
408408
DnsAnswer *answer,
409409
unsigned max_rrs,
410410
usec_t timestamp,
411411
int owner_family,
412412
const union in_addr_union *owner_address) {
413413

414+
DnsResourceRecord *soa = NULL;
414415
unsigned cache_keys, i;
415416
int r;
416417

417418
assert(c);
418419

419-
if (q) {
420-
/* First, if we were passed a question, delete all matching old RRs,
420+
if (key) {
421+
/* First, if we were passed a key, delete all matching old RRs,
421422
* so that we only keep complete by_key in place. */
422-
for (i = 0; i < q->n_keys; i++)
423-
dns_cache_remove(c, q->keys[i]);
423+
dns_cache_remove(c, key);
424424
}
425425

426426
if (!answer)
@@ -438,8 +438,8 @@ int dns_cache_put(
438438

439439
cache_keys = answer->n_rrs;
440440

441-
if (q)
442-
cache_keys += q->n_keys;
441+
if (key)
442+
cache_keys ++;
443443

444444
/* Make some space for our new entries */
445445
dns_cache_make_space(c, cache_keys);
@@ -454,44 +454,38 @@ int dns_cache_put(
454454
goto fail;
455455
}
456456

457-
if (!q)
457+
if (!key)
458458
return 0;
459459

460-
/* Third, add in negative entries for all keys with no RR */
461-
for (i = 0; i < q->n_keys; i++) {
462-
DnsResourceRecord *soa = NULL;
463-
464-
r = dns_answer_contains(answer, q->keys[i]);
465-
if (r < 0)
466-
goto fail;
467-
if (r > 0)
468-
continue;
460+
/* Third, add in negative entries if the key has no RR */
461+
r = dns_answer_contains(answer, key);
462+
if (r < 0)
463+
goto fail;
464+
if (r > 0)
465+
return 0;
469466

470-
/* See https://tools.ietf.org/html/rfc2308, which
471-
* say that a matching SOA record in the packet
472-
* is used to to enable negative caching. */
467+
/* See https://tools.ietf.org/html/rfc2308, which
468+
* say that a matching SOA record in the packet
469+
* is used to to enable negative caching. */
473470

474-
r = dns_answer_find_soa(answer, q->keys[i], &soa);
475-
if (r < 0)
476-
goto fail;
477-
if (r == 0)
478-
continue;
471+
r = dns_answer_find_soa(answer, key, &soa);
472+
if (r < 0)
473+
goto fail;
474+
if (r == 0)
475+
return 0;
479476

480-
r = dns_cache_put_negative(c, q->keys[i], rcode, timestamp, MIN(soa->soa.minimum, soa->ttl), owner_family, owner_address);
481-
if (r < 0)
482-
goto fail;
483-
}
477+
r = dns_cache_put_negative(c, key, rcode, timestamp, MIN(soa->soa.minimum, soa->ttl), owner_family, owner_address);
478+
if (r < 0)
479+
goto fail;
484480

485481
return 0;
486482

487483
fail:
488484
/* Adding all RRs failed. Let's clean up what we already
489485
* added, just in case */
490486

491-
if (q) {
492-
for (i = 0; i < q->n_keys; i++)
493-
dns_cache_remove(c, q->keys[i]);
494-
}
487+
if (key)
488+
dns_cache_remove(c, key);
495489

496490
for (i = 0; i < answer->n_rrs; i++)
497491
dns_cache_remove(c, answer->items[i].rr->key);

src/resolve/resolved-dns-cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef struct DnsCache {
3939
void dns_cache_flush(DnsCache *c);
4040
void dns_cache_prune(DnsCache *c);
4141

42-
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);
42+
int dns_cache_put(DnsCache *c, DnsResourceKey *key, int rcode, DnsAnswer *answer, unsigned max_rrs, usec_t timestamp, int owner_family, const union in_addr_union *owner_address);
4343
int dns_cache_lookup(DnsCache *c, DnsResourceKey *key, int *rcode, DnsAnswer **answer);
4444

4545
int dns_cache_check_conflicts(DnsCache *cache, DnsResourceRecord *rr, int owner_family, const union in_addr_union *owner_address);

src/resolve/resolved-dns-transaction.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p) {
458458
}
459459

460460
/* According to RFC 4795, section 2.9. only the RRs from the answer section shall be cached */
461-
dns_cache_put(&t->scope->cache, p->question, DNS_PACKET_RCODE(p), p->answer, DNS_PACKET_ANCOUNT(p), 0, p->family, &p->sender);
461+
dns_cache_put(&t->scope->cache, t->key, DNS_PACKET_RCODE(p), p->answer, DNS_PACKET_ANCOUNT(p), 0, p->family, &p->sender);
462462

463463
if (DNS_PACKET_RCODE(p) == DNS_RCODE_SUCCESS)
464464
dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);

0 commit comments

Comments
 (0)