| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "af-list.h" |
| 6 |
#include "alloc-util.h" |
| 7 |
#include "bitmap.h" |
| 8 |
#include "dns-answer.h" |
| 9 |
#include "dns-domain.h" |
| 10 |
#include "dns-packet.h" |
| 11 |
#include "dns-rr.h" |
| 12 |
#include "format-ifname.h" |
| 13 |
#include "log.h" |
| 14 |
#include "prioq.h" |
| 15 |
#include "resolve-util.h" |
| 16 |
#include "resolved-dns-cache.h" |
| 17 |
#include "resolved-dns-dnssec.h" |
| 18 |
#include "string-util.h" |
| 19 |
#include "time-util.h" |
| 20 |
|
| 21 |
/* We never keep any item longer than 2h in our cache unless StaleRetentionSec is greater than zero. */ |
| 22 |
#define CACHE_TTL_MAX_USEC (2 * USEC_PER_HOUR) |
| 23 |
|
| 24 |
/* The max TTL for stale data is set to 30 seconds. See RFC 8767, Section 6. */ |
| 25 |
#define CACHE_STALE_TTL_MAX_USEC (30 * USEC_PER_SEC) |
| 26 |
|
| 27 |
/* How long to cache strange rcodes, i.e. rcodes != SUCCESS and != NXDOMAIN (specifically: that's only SERVFAIL for |
| 28 |
* now) */ |
| 29 |
#define CACHE_TTL_STRANGE_RCODE_USEC (10 * USEC_PER_SEC) |
| 30 |
|
| 31 |
#define CACHEABLE_QUERY_FLAGS (SD_RESOLVED_AUTHENTICATED|SD_RESOLVED_CONFIDENTIAL) |
| 32 |
|
| 33 |
typedef enum DnsCacheItemType DnsCacheItemType; |
| 34 |
typedef struct DnsCacheItem DnsCacheItem; |
| 35 |
|
| 36 |
enum DnsCacheItemType { |
| 37 |
DNS_CACHE_POSITIVE, |
| 38 |
DNS_CACHE_NODATA, |
| 39 |
DNS_CACHE_NXDOMAIN, |
| 40 |
DNS_CACHE_RCODE, /* "strange" RCODE (effective only SERVFAIL for now) */ |
| 41 |
}; |
| 42 |
|
| 43 |
struct DnsCacheItem { |
| 44 |
DnsCacheItemType type; |
| 45 |
int rcode; |
| 46 |
DnsResourceKey *key; /* The key for this item, i.e. the lookup key */ |
| 47 |
DnsResourceRecord *rr; /* The RR for this item, i.e. the lookup value for positive queries */ |
| 48 |
DnsAnswer *answer; /* The full validated answer, if this is an RRset acquired via a "primary" lookup */ |
| 49 |
DnsPacket *full_packet; /* The full packet this information was acquired with */ |
| 50 |
|
| 51 |
usec_t until; /* If StaleRetentionSec is greater than zero, until is set to a duration of StaleRetentionSec from the time of TTL expiry. If StaleRetentionSec is zero, both until and until_valid will be set to ttl. */ |
| 52 |
usec_t until_valid; /* The key is for storing the time when the TTL set to expire. */ |
| 53 |
uint64_t query_flags; /* SD_RESOLVED_AUTHENTICATED and/or SD_RESOLVED_CONFIDENTIAL */ |
| 54 |
DnssecResult dnssec_result; |
| 55 |
|
| 56 |
int ifindex; |
| 57 |
int owner_family; |
| 58 |
union in_addr_union owner_address; |
| 59 |
|
| 60 |
unsigned prioq_idx; |
| 61 |
LIST_FIELDS(DnsCacheItem, by_key); |
| 62 |
|
| 63 |
bool shared_owner; |
| 64 |
}; |
| 65 |
|
| 66 |
/* Returns true if this is a cache item created as result of an explicit lookup, or created as "side-effect" |
| 67 |
* of another request. "Primary" entries will carry the full answer data (with NSEC, …) that can also prove |
| 68 |
* wildcard expansion, non-existence and such, while entries that were created as "side-effect" just contain |
| 69 |
* immediate RR data for the specified RR key, but nothing else. */ |
| 70 |
#define DNS_CACHE_ITEM_IS_PRIMARY(item) (!!(item)->answer) |
| 71 |
|
| 72 |
static const char *dns_cache_item_type_to_string(DnsCacheItem *item) { |
| 73 |
assert(item); |
| 74 |
|
| 75 |
switch (item->type) { |
| 76 |
|
| 77 |
case DNS_CACHE_POSITIVE: |
| 78 |
return "POSITIVE"; |
| 79 |
|
| 80 |
case DNS_CACHE_NODATA: |
| 81 |
return "NODATA"; |
| 82 |
|
| 83 |
case DNS_CACHE_NXDOMAIN: |
| 84 |
return "NXDOMAIN"; |
| 85 |
|
| 86 |
case DNS_CACHE_RCODE: |
| 87 |
return dns_rcode_to_string(item->rcode); |
| 88 |
} |
| 89 |
|
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
static DnsCacheItem* dns_cache_item_free(DnsCacheItem *i) { |
| 94 |
if (!i) |
| 95 |
return NULL; |
| 96 |
|
| 97 |
dns_resource_record_unref(i->rr); |
| 98 |
dns_resource_key_unref(i->key); |
| 99 |
dns_answer_unref(i->answer); |
| 100 |
dns_packet_unref(i->full_packet); |
| 101 |
return mfree(i); |
| 102 |
} |
| 103 |
DEFINE_TRIVIAL_CLEANUP_FUNC(DnsCacheItem*, dns_cache_item_free); |
| 104 |
|
| 105 |
static void dns_cache_item_unlink_and_free(DnsCache *c, DnsCacheItem *i) { |
| 106 |
DnsCacheItem *first; |
| 107 |
|
| 108 |
assert(c); |
| 109 |
|
| 110 |
if (!i) |
| 111 |
return; |
| 112 |
|
| 113 |
first = hashmap_get(c->by_key, i->key); |
| 114 |
LIST_REMOVE(by_key, first, i); |
| 115 |
|
| 116 |
if (first) |
| 117 |
assert_se(hashmap_replace(c->by_key, first->key, first) >= 0); |
| 118 |
else |
| 119 |
hashmap_remove(c->by_key, i->key); |
| 120 |
|
| 121 |
prioq_remove(c->by_expiry, i, &i->prioq_idx); |
| 122 |
|
| 123 |
dns_cache_item_free(i); |
| 124 |
} |
| 125 |
|
| 126 |
static bool dns_cache_remove_by_rr(DnsCache *c, DnsResourceRecord *rr) { |
| 127 |
DnsCacheItem *first; |
| 128 |
int r; |
| 129 |
|
| 130 |
first = hashmap_get(c->by_key, rr->key); |
| 131 |
LIST_FOREACH(by_key, i, first) { |
| 132 |
r = dns_resource_record_equal(i->rr, rr); |
| 133 |
if (r < 0) |
| 134 |
return r; |
| 135 |
if (r > 0) { |
| 136 |
dns_cache_item_unlink_and_free(c, i); |
| 137 |
return true; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
static bool dns_cache_remove_by_key(DnsCache *c, DnsResourceKey *key) { |
| 145 |
DnsCacheItem *first; |
| 146 |
|
| 147 |
assert(c); |
| 148 |
assert(key); |
| 149 |
|
| 150 |
first = hashmap_remove(c->by_key, key); |
| 151 |
if (!first) |
| 152 |
return false; |
| 153 |
|
| 154 |
LIST_FOREACH(by_key, i, first) { |
| 155 |
prioq_remove(c->by_expiry, i, &i->prioq_idx); |
| 156 |
dns_cache_item_free(i); |
| 157 |
} |
| 158 |
|
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
void dns_cache_flush(DnsCache *c) { |
| 163 |
DnsResourceKey *key; |
| 164 |
|
| 165 |
assert(c); |
| 166 |
|
| 167 |
while ((key = hashmap_first_key(c->by_key))) |
| 168 |
dns_cache_remove_by_key(c, key); |
| 169 |
|
| 170 |
assert(hashmap_isempty(c->by_key)); |
| 171 |
assert(prioq_isempty(c->by_expiry)); |
| 172 |
|
| 173 |
c->by_key = hashmap_free(c->by_key); |
| 174 |
c->by_expiry = prioq_free(c->by_expiry); |
| 175 |
} |
| 176 |
|
| 177 |
static void dns_cache_make_space(DnsCache *c, unsigned add) { |
| 178 |
assert(c); |
| 179 |
|
| 180 |
if (add <= 0) |
| 181 |
return; |
| 182 |
|
| 183 |
if (c->cache_max == 0) |
| 184 |
return; |
| 185 |
|
| 186 |
/* Makes space for n new entries. Note that we actually allow |
| 187 |
* the cache to grow beyond cache_max, but only when we shall |
| 188 |
* add more RRs to the cache than cache_max at once. In that |
| 189 |
* case the cache will be emptied completely otherwise. */ |
| 190 |
|
| 191 |
for (;;) { |
| 192 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 193 |
DnsCacheItem *i; |
| 194 |
|
| 195 |
if (prioq_isempty(c->by_expiry)) |
| 196 |
break; |
| 197 |
|
| 198 |
if (prioq_size(c->by_expiry) + add < c->cache_max) |
| 199 |
break; |
| 200 |
|
| 201 |
i = prioq_peek(c->by_expiry); |
| 202 |
assert(i); |
| 203 |
|
| 204 |
/* Take an extra reference to the key so that it |
| 205 |
* doesn't go away in the middle of the remove call */ |
| 206 |
key = dns_resource_key_ref(i->key); |
| 207 |
dns_cache_remove_by_key(c, key); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void dns_cache_prune(DnsCache *c) { |
| 212 |
usec_t t = 0; |
| 213 |
|
| 214 |
assert(c); |
| 215 |
|
| 216 |
/* Remove all entries that are past their TTL */ |
| 217 |
|
| 218 |
for (;;) { |
| 219 |
DnsCacheItem *i; |
| 220 |
char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
| 221 |
|
| 222 |
i = prioq_peek(c->by_expiry); |
| 223 |
if (!i) |
| 224 |
break; |
| 225 |
|
| 226 |
if (t <= 0) |
| 227 |
t = now(CLOCK_BOOTTIME); |
| 228 |
|
| 229 |
if (i->until > t) |
| 230 |
break; |
| 231 |
|
| 232 |
/* Depending whether this is an mDNS shared entry |
| 233 |
* either remove only this one RR or the whole RRset */ |
| 234 |
log_debug("Removing %scache entry for %s (expired "USEC_FMT"s ago)", |
| 235 |
i->shared_owner ? "shared " : "", |
| 236 |
dns_resource_key_to_string(i->key, key_str, sizeof key_str), |
| 237 |
(t - i->until) / USEC_PER_SEC); |
| 238 |
|
| 239 |
if (i->shared_owner) |
| 240 |
dns_cache_item_unlink_and_free(c, i); |
| 241 |
else { |
| 242 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 243 |
|
| 244 |
/* Take an extra reference to the key so that it |
| 245 |
* doesn't go away in the middle of the remove call */ |
| 246 |
key = dns_resource_key_ref(i->key); |
| 247 |
dns_cache_remove_by_key(c, key); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
bool dns_cache_expiry_in_one_second(DnsCache *c, usec_t t) { |
| 253 |
DnsCacheItem *i; |
| 254 |
|
| 255 |
assert(c); |
| 256 |
|
| 257 |
/* Check if any items expire within the next second */ |
| 258 |
i = prioq_peek(c->by_expiry); |
| 259 |
if (!i) |
| 260 |
return false; |
| 261 |
|
| 262 |
if (i->until <= usec_add(t, USEC_PER_SEC)) |
| 263 |
return true; |
| 264 |
|
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
static int dns_cache_item_prioq_compare_func(const void *a, const void *b) { |
| 269 |
const DnsCacheItem *x = a, *y = b; |
| 270 |
|
| 271 |
return CMP(x->until, y->until); |
| 272 |
} |
| 273 |
|
| 274 |
static int dns_cache_init(DnsCache *c) { |
| 275 |
int r; |
| 276 |
|
| 277 |
assert(c); |
| 278 |
|
| 279 |
r = prioq_ensure_allocated(&c->by_expiry, dns_cache_item_prioq_compare_func); |
| 280 |
if (r < 0) |
| 281 |
return r; |
| 282 |
|
| 283 |
r = hashmap_ensure_allocated(&c->by_key, &dns_resource_key_hash_ops); |
| 284 |
if (r < 0) |
| 285 |
return r; |
| 286 |
|
| 287 |
return r; |
| 288 |
} |
| 289 |
|
| 290 |
static int dns_cache_link_item(DnsCache *c, DnsCacheItem *i) { |
| 291 |
DnsCacheItem *first; |
| 292 |
int r; |
| 293 |
|
| 294 |
assert(c); |
| 295 |
assert(i); |
| 296 |
|
| 297 |
r = prioq_put(c->by_expiry, i, &i->prioq_idx); |
| 298 |
if (r < 0) |
| 299 |
return r; |
| 300 |
|
| 301 |
first = hashmap_get(c->by_key, i->key); |
| 302 |
if (first) { |
| 303 |
_unused_ _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL; |
| 304 |
|
| 305 |
/* Keep a reference to the original key, while we manipulate the list. */ |
| 306 |
k = dns_resource_key_ref(first->key); |
| 307 |
|
| 308 |
/* Now, try to reduce the number of keys we keep */ |
| 309 |
dns_resource_key_reduce(&first->key, &i->key); |
| 310 |
|
| 311 |
if (first->rr) |
| 312 |
dns_resource_key_reduce(&first->rr->key, &i->key); |
| 313 |
if (i->rr) |
| 314 |
dns_resource_key_reduce(&i->rr->key, &i->key); |
| 315 |
|
| 316 |
LIST_PREPEND(by_key, first, i); |
| 317 |
assert_se(hashmap_replace(c->by_key, first->key, first) >= 0); |
| 318 |
} else { |
| 319 |
r = hashmap_put(c->by_key, i->key, i); |
| 320 |
if (r < 0) { |
| 321 |
prioq_remove(c->by_expiry, i, &i->prioq_idx); |
| 322 |
return r; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
static DnsCacheItem* dns_cache_get(DnsCache *c, DnsResourceRecord *rr) { |
| 330 |
assert(c); |
| 331 |
assert(rr); |
| 332 |
|
| 333 |
LIST_FOREACH(by_key, i, (DnsCacheItem*) hashmap_get(c->by_key, rr->key)) |
| 334 |
if (i->rr && dns_resource_record_equal(i->rr, rr) > 0) |
| 335 |
return i; |
| 336 |
|
| 337 |
return NULL; |
| 338 |
} |
| 339 |
|
| 340 |
static usec_t calculate_until_valid( |
| 341 |
DnsResourceRecord *rr, |
| 342 |
uint32_t min_ttl, |
| 343 |
uint32_t nsec_ttl, |
| 344 |
usec_t timestamp, |
| 345 |
bool use_soa_minimum) { |
| 346 |
|
| 347 |
uint32_t ttl; |
| 348 |
usec_t u; |
| 349 |
|
| 350 |
assert(rr); |
| 351 |
|
| 352 |
ttl = MIN(min_ttl, nsec_ttl); |
| 353 |
if (rr->key->type == DNS_TYPE_SOA && use_soa_minimum) { |
| 354 |
/* If this is a SOA RR, and it is requested, clamp to the SOA's minimum field. This is used |
| 355 |
* when we do negative caching, to determine the TTL for the negative caching entry. See RFC |
| 356 |
* 2308, Section 5. */ |
| 357 |
|
| 358 |
if (ttl > rr->soa.minimum) |
| 359 |
ttl = rr->soa.minimum; |
| 360 |
} |
| 361 |
|
| 362 |
u = ttl * USEC_PER_SEC; |
| 363 |
if (u > CACHE_TTL_MAX_USEC) |
| 364 |
u = CACHE_TTL_MAX_USEC; |
| 365 |
|
| 366 |
if (rr->expiry != USEC_INFINITY) { |
| 367 |
usec_t left; |
| 368 |
|
| 369 |
/* Make use of the DNSSEC RRSIG expiry info, if we have it */ |
| 370 |
|
| 371 |
left = LESS_BY(rr->expiry, now(CLOCK_REALTIME)); |
| 372 |
if (u > left) |
| 373 |
u = left; |
| 374 |
} |
| 375 |
|
| 376 |
return timestamp + u; |
| 377 |
} |
| 378 |
|
| 379 |
static usec_t calculate_until( |
| 380 |
usec_t until_valid, |
| 381 |
usec_t stale_retention_usec) { |
| 382 |
|
| 383 |
return stale_retention_usec > 0 ? usec_add(until_valid, stale_retention_usec) : until_valid; |
| 384 |
} |
| 385 |
|
| 386 |
static void dns_cache_item_update_positive( |
| 387 |
DnsCache *c, |
| 388 |
DnsCacheItem *i, |
| 389 |
DnsResourceRecord *rr, |
| 390 |
DnsAnswer *answer, |
| 391 |
DnsPacket *full_packet, |
| 392 |
uint32_t min_ttl, |
| 393 |
uint64_t query_flags, |
| 394 |
bool shared_owner, |
| 395 |
DnssecResult dnssec_result, |
| 396 |
usec_t timestamp, |
| 397 |
int ifindex, |
| 398 |
int owner_family, |
| 399 |
const union in_addr_union *owner_address, |
| 400 |
usec_t stale_retention_usec) { |
| 401 |
|
| 402 |
assert(c); |
| 403 |
assert(i); |
| 404 |
assert(rr); |
| 405 |
assert(owner_address); |
| 406 |
|
| 407 |
i->type = DNS_CACHE_POSITIVE; |
| 408 |
|
| 409 |
if (!i->by_key_prev) |
| 410 |
/* We are the first item in the list, we need to |
| 411 |
* update the key used in the hashmap */ |
| 412 |
|
| 413 |
assert_se(hashmap_replace(c->by_key, rr->key, i) >= 0); |
| 414 |
|
| 415 |
DNS_RR_REPLACE(i->rr, dns_resource_record_ref(rr)); |
| 416 |
|
| 417 |
DNS_RESOURCE_KEY_REPLACE(i->key, dns_resource_key_ref(rr->key)); |
| 418 |
|
| 419 |
DNS_ANSWER_REPLACE(i->answer, dns_answer_ref(answer)); |
| 420 |
|
| 421 |
DNS_PACKET_REPLACE(i->full_packet, dns_packet_ref(full_packet)); |
| 422 |
|
| 423 |
i->until_valid = calculate_until_valid(rr, min_ttl, UINT32_MAX, timestamp, false); |
| 424 |
i->until = calculate_until(i->until_valid, stale_retention_usec); |
| 425 |
i->query_flags = query_flags & CACHEABLE_QUERY_FLAGS; |
| 426 |
i->shared_owner = shared_owner; |
| 427 |
i->dnssec_result = dnssec_result; |
| 428 |
|
| 429 |
i->ifindex = ifindex; |
| 430 |
|
| 431 |
i->owner_family = owner_family; |
| 432 |
i->owner_address = *owner_address; |
| 433 |
|
| 434 |
prioq_reshuffle(c->by_expiry, i, &i->prioq_idx); |
| 435 |
} |
| 436 |
|
| 437 |
static int dns_cache_put_positive( |
| 438 |
DnsCache *c, |
| 439 |
DnsProtocol protocol, |
| 440 |
DnsResourceRecord *rr, |
| 441 |
DnsAnswer *answer, |
| 442 |
DnsPacket *full_packet, |
| 443 |
uint64_t query_flags, |
| 444 |
bool shared_owner, |
| 445 |
DnssecResult dnssec_result, |
| 446 |
usec_t timestamp, |
| 447 |
int ifindex, |
| 448 |
int owner_family, |
| 449 |
const union in_addr_union *owner_address, |
| 450 |
usec_t stale_retention_usec) { |
| 451 |
|
| 452 |
char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
| 453 |
DnsCacheItem *existing; |
| 454 |
uint32_t min_ttl; |
| 455 |
int r; |
| 456 |
|
| 457 |
assert(c); |
| 458 |
assert(rr); |
| 459 |
assert(owner_address); |
| 460 |
|
| 461 |
/* Never cache pseudo RRs */ |
| 462 |
if (dns_class_is_pseudo(rr->key->class)) |
| 463 |
return 0; |
| 464 |
if (dns_type_is_pseudo(rr->key->type)) |
| 465 |
return 0; |
| 466 |
|
| 467 |
/* Determine the minimal TTL of all RRs in the answer plus the one by the main RR we are supposed to |
| 468 |
* cache. Since we cache whole answers to questions we should never return answers where only some |
| 469 |
* RRs are still valid, hence find the lowest here */ |
| 470 |
min_ttl = MIN(dns_answer_min_ttl(answer), rr->ttl); |
| 471 |
|
| 472 |
/* New TTL is 0? Delete this specific entry... */ |
| 473 |
if (min_ttl <= 0) { |
| 474 |
r = dns_cache_remove_by_rr(c, rr); |
| 475 |
log_debug("%s: %s", |
| 476 |
r > 0 ? "Removed zero TTL entry from cache" : "Not caching zero TTL cache entry", |
| 477 |
dns_resource_key_to_string(rr->key, key_str, sizeof key_str)); |
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
/* Entry exists already? Update TTL, timestamp and owner */ |
| 482 |
existing = dns_cache_get(c, rr); |
| 483 |
if (existing) { |
| 484 |
dns_cache_item_update_positive( |
| 485 |
c, |
| 486 |
existing, |
| 487 |
rr, |
| 488 |
answer, |
| 489 |
full_packet, |
| 490 |
min_ttl, |
| 491 |
query_flags, |
| 492 |
shared_owner, |
| 493 |
dnssec_result, |
| 494 |
timestamp, |
| 495 |
ifindex, |
| 496 |
owner_family, |
| 497 |
owner_address, |
| 498 |
stale_retention_usec); |
| 499 |
return 0; |
| 500 |
} |
| 501 |
|
| 502 |
/* Do not cache mDNS goodbye packet. */ |
| 503 |
if (protocol == DNS_PROTOCOL_MDNS && rr->ttl <= 1) |
| 504 |
return 0; |
| 505 |
|
| 506 |
/* Otherwise, add the new RR */ |
| 507 |
r = dns_cache_init(c); |
| 508 |
if (r < 0) |
| 509 |
return r; |
| 510 |
|
| 511 |
dns_cache_make_space(c, 1); |
| 512 |
|
| 513 |
_cleanup_(dns_cache_item_freep) DnsCacheItem *i = new(DnsCacheItem, 1); |
| 514 |
if (!i) |
| 515 |
return -ENOMEM; |
| 516 |
|
| 517 |
/* If StaleRetentionSec is greater than zero, the 'until' property is set to a duration |
| 518 |
* of StaleRetentionSec from the time of TTL expiry. |
| 519 |
* If StaleRetentionSec is zero, both the 'until' and 'until_valid' are set to the TTL duration, |
| 520 |
* leading to the eviction of the record once the TTL expires. */ |
| 521 |
usec_t until_valid = calculate_until_valid(rr, min_ttl, UINT32_MAX, timestamp, false); |
| 522 |
*i = (DnsCacheItem) { |
| 523 |
.type = DNS_CACHE_POSITIVE, |
| 524 |
.key = dns_resource_key_ref(rr->key), |
| 525 |
.rr = dns_resource_record_ref(rr), |
| 526 |
.answer = dns_answer_ref(answer), |
| 527 |
.full_packet = dns_packet_ref(full_packet), |
| 528 |
.until = calculate_until(until_valid, stale_retention_usec), |
| 529 |
.until_valid = until_valid, |
| 530 |
.query_flags = query_flags & CACHEABLE_QUERY_FLAGS, |
| 531 |
.shared_owner = shared_owner, |
| 532 |
.dnssec_result = dnssec_result, |
| 533 |
.ifindex = ifindex, |
| 534 |
.owner_family = owner_family, |
| 535 |
.owner_address = *owner_address, |
| 536 |
.prioq_idx = PRIOQ_IDX_NULL, |
| 537 |
}; |
| 538 |
|
| 539 |
r = dns_cache_link_item(c, i); |
| 540 |
if (r < 0) |
| 541 |
return r; |
| 542 |
|
| 543 |
log_debug("Added positive %s %s%s cache entry for %s "USEC_FMT"s on %s/%s/%s", |
| 544 |
FLAGS_SET(i->query_flags, SD_RESOLVED_AUTHENTICATED) ? "authenticated" : "unauthenticated", |
| 545 |
FLAGS_SET(i->query_flags, SD_RESOLVED_CONFIDENTIAL) ? "confidential" : "non-confidential", |
| 546 |
i->shared_owner ? " shared" : "", |
| 547 |
dns_resource_key_to_string(i->key, key_str, sizeof key_str), |
| 548 |
(i->until - timestamp) / USEC_PER_SEC, |
| 549 |
i->ifindex == 0 ? "*" : FORMAT_IFNAME(i->ifindex), |
| 550 |
af_to_name_short(i->owner_family), |
| 551 |
IN_ADDR_TO_STRING(i->owner_family, &i->owner_address)); |
| 552 |
|
| 553 |
TAKE_PTR(i); |
| 554 |
return 0; |
| 555 |
} |
| 556 |
/* https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml */ |
| 557 |
/* https://www.iana.org/assignments/locally-served-dns-zones/locally-served-dns-zones.xhtml#transport-independent */ |
| 558 |
static bool dns_special_use_domain_invalid_answer(DnsResourceKey *key, int rcode) { |
| 559 |
/* Sometimes we know a domain exists, even if broken nameservers say otherwise. Make sure not to |
| 560 |
* cache any answers we know are wrong. */ |
| 561 |
|
| 562 |
/* RFC9462 § 6.4: resolvers SHOULD respond to queries of any type other than SVCB for |
| 563 |
* _dns.resolver.arpa. with NODATA and queries of any type for any domain name under resolver.arpa |
| 564 |
* with NODATA. */ |
| 565 |
if (dns_name_endswith(dns_resource_key_name(key), "resolver.arpa") > 0 && rcode == DNS_RCODE_NXDOMAIN) |
| 566 |
return true; |
| 567 |
|
| 568 |
return false; |
| 569 |
} |
| 570 |
|
| 571 |
static int dns_cache_put_negative( |
| 572 |
DnsCache *c, |
| 573 |
DnsResourceKey *key, |
| 574 |
int rcode, |
| 575 |
DnsAnswer *answer, |
| 576 |
DnsPacket *full_packet, |
| 577 |
uint64_t query_flags, |
| 578 |
DnssecResult dnssec_result, |
| 579 |
uint32_t nsec_ttl, |
| 580 |
usec_t timestamp, |
| 581 |
DnsResourceRecord *soa, |
| 582 |
int owner_family, |
| 583 |
const union in_addr_union *owner_address) { |
| 584 |
|
| 585 |
_cleanup_(dns_cache_item_freep) DnsCacheItem *i = NULL; |
| 586 |
char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
| 587 |
int r; |
| 588 |
|
| 589 |
assert(c); |
| 590 |
assert(key); |
| 591 |
assert(owner_address); |
| 592 |
|
| 593 |
/* Never cache pseudo RR keys. DNS_TYPE_ANY is particularly |
| 594 |
* important to filter out as we use this as a pseudo-type for |
| 595 |
* NXDOMAIN entries */ |
| 596 |
if (dns_class_is_pseudo(key->class)) |
| 597 |
return 0; |
| 598 |
if (dns_type_is_pseudo(key->type)) |
| 599 |
return 0; |
| 600 |
if (dns_special_use_domain_invalid_answer(key, rcode)) |
| 601 |
return 0; |
| 602 |
|
| 603 |
if (IN_SET(rcode, DNS_RCODE_SUCCESS, DNS_RCODE_NXDOMAIN)) { |
| 604 |
if (!soa) |
| 605 |
return 0; |
| 606 |
|
| 607 |
/* For negative replies, check if we have a TTL of a SOA */ |
| 608 |
if (nsec_ttl <= 0 || soa->soa.minimum <= 0 || soa->ttl <= 0) { |
| 609 |
log_debug("Not caching negative entry with zero SOA/NSEC/NSEC3 TTL: %s", |
| 610 |
dns_resource_key_to_string(key, key_str, sizeof key_str)); |
| 611 |
return 0; |
| 612 |
} |
| 613 |
} else if (rcode != DNS_RCODE_SERVFAIL) |
| 614 |
return 0; |
| 615 |
|
| 616 |
r = dns_cache_init(c); |
| 617 |
if (r < 0) |
| 618 |
return r; |
| 619 |
|
| 620 |
dns_cache_make_space(c, 1); |
| 621 |
|
| 622 |
i = new(DnsCacheItem, 1); |
| 623 |
if (!i) |
| 624 |
return -ENOMEM; |
| 625 |
|
| 626 |
*i = (DnsCacheItem) { |
| 627 |
.type = |
| 628 |
rcode == DNS_RCODE_SUCCESS ? DNS_CACHE_NODATA : |
| 629 |
rcode == DNS_RCODE_NXDOMAIN ? DNS_CACHE_NXDOMAIN : DNS_CACHE_RCODE, |
| 630 |
.query_flags = query_flags & CACHEABLE_QUERY_FLAGS, |
| 631 |
.dnssec_result = dnssec_result, |
| 632 |
.owner_family = owner_family, |
| 633 |
.owner_address = *owner_address, |
| 634 |
.prioq_idx = PRIOQ_IDX_NULL, |
| 635 |
.rcode = rcode, |
| 636 |
.answer = dns_answer_ref(answer), |
| 637 |
.full_packet = dns_packet_ref(full_packet), |
| 638 |
}; |
| 639 |
|
| 640 |
/* Determine how long to cache this entry. In case we have some RRs in the answer use the lowest TTL |
| 641 |
* of any of them. Typically that's the SOA's TTL, which is OK, but could possibly be lower because |
| 642 |
* of some other RR. Let's better take the lowest option here than a needlessly high one */ |
| 643 |
i->until = i->until_valid = |
| 644 |
i->type == DNS_CACHE_RCODE ? timestamp + CACHE_TTL_STRANGE_RCODE_USEC : |
| 645 |
calculate_until_valid(soa, dns_answer_min_ttl(answer), nsec_ttl, timestamp, true); |
| 646 |
|
| 647 |
if (i->type == DNS_CACHE_NXDOMAIN) { |
| 648 |
/* NXDOMAIN entries should apply equally to all types, so we use ANY as |
| 649 |
* a pseudo type for this purpose here. */ |
| 650 |
i->key = dns_resource_key_new(key->class, DNS_TYPE_ANY, dns_resource_key_name(key)); |
| 651 |
if (!i->key) |
| 652 |
return -ENOMEM; |
| 653 |
|
| 654 |
/* Make sure to remove any previous entry for this |
| 655 |
* specific ANY key. (For non-ANY keys the cache data |
| 656 |
* is already cleared by the caller.) Note that we |
| 657 |
* don't bother removing positive or NODATA cache |
| 658 |
* items in this case, because it would either be slow |
| 659 |
* or require explicit indexing by name */ |
| 660 |
dns_cache_remove_by_key(c, key); |
| 661 |
} else |
| 662 |
i->key = dns_resource_key_ref(key); |
| 663 |
|
| 664 |
r = dns_cache_link_item(c, i); |
| 665 |
if (r < 0) |
| 666 |
return r; |
| 667 |
|
| 668 |
log_debug("Added %s cache entry for %s "USEC_FMT"s", |
| 669 |
dns_cache_item_type_to_string(i), |
| 670 |
dns_resource_key_to_string(i->key, key_str, sizeof key_str), |
| 671 |
(i->until - timestamp) / USEC_PER_SEC); |
| 672 |
|
| 673 |
i = NULL; |
| 674 |
return 0; |
| 675 |
} |
| 676 |
|
| 677 |
static void dns_cache_remove_previous( |
| 678 |
DnsCache *c, |
| 679 |
DnsResourceKey *key, |
| 680 |
DnsAnswer *answer) { |
| 681 |
|
| 682 |
DnsResourceRecord *rr; |
| 683 |
DnsAnswerFlags flags; |
| 684 |
|
| 685 |
assert(c); |
| 686 |
|
| 687 |
/* First, if we were passed a key (i.e. on LLMNR/DNS, but |
| 688 |
* not on mDNS), delete all matching old RRs, so that we only |
| 689 |
* keep complete by_key in place. */ |
| 690 |
if (key) |
| 691 |
dns_cache_remove_by_key(c, key); |
| 692 |
|
| 693 |
/* Second, flush all entries matching the answer, unless this |
| 694 |
* is an RR that is explicitly marked to be "shared" between |
| 695 |
* peers (i.e. mDNS RRs without the flush-cache bit set). */ |
| 696 |
DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) { |
| 697 |
if ((flags & DNS_ANSWER_CACHEABLE) == 0) |
| 698 |
continue; |
| 699 |
|
| 700 |
if (flags & DNS_ANSWER_SHARED_OWNER) |
| 701 |
continue; |
| 702 |
|
| 703 |
dns_cache_remove_by_key(c, rr->key); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
static bool rr_eligible(DnsResourceRecord *rr) { |
| 708 |
assert(rr); |
| 709 |
|
| 710 |
/* When we see an NSEC/NSEC3 RR, we'll only cache it if it is from the lower zone, not the upper zone, since |
| 711 |
* that's where the interesting bits are (with exception of DS RRs). Of course, this way we cannot derive DS |
| 712 |
* existence from any cached NSEC/NSEC3, but that should be fine. */ |
| 713 |
|
| 714 |
switch (rr->key->type) { |
| 715 |
|
| 716 |
case DNS_TYPE_NSEC: |
| 717 |
return !bitmap_isset(rr->nsec.types, DNS_TYPE_NS) || |
| 718 |
bitmap_isset(rr->nsec.types, DNS_TYPE_SOA); |
| 719 |
|
| 720 |
case DNS_TYPE_NSEC3: |
| 721 |
return !bitmap_isset(rr->nsec3.types, DNS_TYPE_NS) || |
| 722 |
bitmap_isset(rr->nsec3.types, DNS_TYPE_SOA); |
| 723 |
|
| 724 |
default: |
| 725 |
return true; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
int dns_cache_put( |
| 730 |
DnsCache *c, |
| 731 |
DnsCacheMode cache_mode, |
| 732 |
DnsProtocol protocol, |
| 733 |
DnsResourceKey *key, |
| 734 |
int rcode, |
| 735 |
DnsAnswer *answer, |
| 736 |
DnsPacket *full_packet, |
| 737 |
uint64_t query_flags, |
| 738 |
DnssecResult dnssec_result, |
| 739 |
uint32_t nsec_ttl, |
| 740 |
int owner_family, |
| 741 |
const union in_addr_union *owner_address, |
| 742 |
usec_t stale_retention_usec) { |
| 743 |
|
| 744 |
DnsResourceRecord *soa = NULL; |
| 745 |
bool weird_rcode = false; |
| 746 |
DnsAnswerItem *item; |
| 747 |
DnsAnswerFlags flags; |
| 748 |
unsigned cache_keys; |
| 749 |
usec_t timestamp; |
| 750 |
int r; |
| 751 |
|
| 752 |
assert(c); |
| 753 |
assert(owner_address); |
| 754 |
|
| 755 |
/* Check cache mode here too, since the mDNS caller doesn't guard against Cache=no. */ |
| 756 |
if (cache_mode == DNS_CACHE_MODE_NO || c->cache_max == 0) |
| 757 |
return 0; |
| 758 |
|
| 759 |
dns_cache_remove_previous(c, key, answer); |
| 760 |
|
| 761 |
/* We only care for positive replies and NXDOMAINs, on all other replies we will simply flush the respective |
| 762 |
* entries, and that's it. (Well, with one further exception: since some DNS zones (akamai!) return SERVFAIL |
| 763 |
* consistently for some lookups, and forwarders tend to propagate that we'll cache that too, but only for a |
| 764 |
* short time.) */ |
| 765 |
|
| 766 |
if (IN_SET(rcode, DNS_RCODE_SUCCESS, DNS_RCODE_NXDOMAIN)) { |
| 767 |
if (dns_answer_isempty(answer)) { |
| 768 |
if (key) { |
| 769 |
char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
| 770 |
|
| 771 |
log_debug("Not caching negative entry without a SOA record: %s", |
| 772 |
dns_resource_key_to_string(key, key_str, sizeof key_str)); |
| 773 |
} |
| 774 |
|
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
} else { |
| 779 |
/* Only cache SERVFAIL as "weird" rcode for now. We can add more later, should that turn out to be |
| 780 |
* beneficial. */ |
| 781 |
if (rcode != DNS_RCODE_SERVFAIL) |
| 782 |
return 0; |
| 783 |
|
| 784 |
weird_rcode = true; |
| 785 |
} |
| 786 |
|
| 787 |
cache_keys = dns_answer_size(answer); |
| 788 |
if (key) |
| 789 |
cache_keys++; |
| 790 |
|
| 791 |
/* Make some space for our new entries */ |
| 792 |
dns_cache_make_space(c, cache_keys); |
| 793 |
|
| 794 |
timestamp = now(CLOCK_BOOTTIME); |
| 795 |
|
| 796 |
/* Second, add in positive entries for all contained RRs */ |
| 797 |
DNS_ANSWER_FOREACH_ITEM(item, answer) { |
| 798 |
int primary = false; |
| 799 |
|
| 800 |
if (!FLAGS_SET(item->flags, DNS_ANSWER_CACHEABLE) || |
| 801 |
!rr_eligible(item->rr)) |
| 802 |
continue; |
| 803 |
|
| 804 |
if (key) { |
| 805 |
/* We store the auxiliary RRs and packet data in the cache only if they were in |
| 806 |
* direct response to the original query. If we cache an RR we also received, and |
| 807 |
* that is just auxiliary information we can't use the data, hence don't. */ |
| 808 |
|
| 809 |
primary = dns_resource_key_match_rr(key, item->rr, NULL); |
| 810 |
if (primary < 0) |
| 811 |
return primary; |
| 812 |
if (primary == 0) { |
| 813 |
primary = dns_resource_key_match_cname_or_dname(key, item->rr->key, NULL); |
| 814 |
if (primary < 0) |
| 815 |
return primary; |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
if (!primary) { |
| 820 |
DnsCacheItem *first; |
| 821 |
|
| 822 |
/* Do not replace existing cache items for primary lookups with non-primary |
| 823 |
* data. After all the primary lookup data is a lot more useful. */ |
| 824 |
first = hashmap_get(c->by_key, item->rr->key); |
| 825 |
if (first && DNS_CACHE_ITEM_IS_PRIMARY(first)) |
| 826 |
return 0; |
| 827 |
} |
| 828 |
|
| 829 |
r = dns_cache_put_positive( |
| 830 |
c, |
| 831 |
protocol, |
| 832 |
item->rr, |
| 833 |
primary ? answer : NULL, |
| 834 |
primary ? full_packet : NULL, |
| 835 |
((item->flags & DNS_ANSWER_AUTHENTICATED) ? SD_RESOLVED_AUTHENTICATED : 0) | |
| 836 |
(query_flags & SD_RESOLVED_CONFIDENTIAL), |
| 837 |
item->flags & DNS_ANSWER_SHARED_OWNER, |
| 838 |
dnssec_result, |
| 839 |
timestamp, |
| 840 |
item->ifindex, |
| 841 |
owner_family, |
| 842 |
owner_address, |
| 843 |
stale_retention_usec); |
| 844 |
if (r < 0) |
| 845 |
goto fail; |
| 846 |
} |
| 847 |
|
| 848 |
if (!key) /* mDNS doesn't know negative caching, really */ |
| 849 |
return 0; |
| 850 |
|
| 851 |
/* Third, add in negative entries if the key has no RR */ |
| 852 |
r = dns_answer_match_key(answer, key, NULL); |
| 853 |
if (r < 0) |
| 854 |
goto fail; |
| 855 |
if (r > 0) |
| 856 |
return 0; |
| 857 |
|
| 858 |
/* But not if it has a matching CNAME/DNAME (the negative caching will be done on the canonical name, |
| 859 |
* not on the alias) */ |
| 860 |
r = dns_answer_find_cname_or_dname(answer, key, NULL, NULL); |
| 861 |
if (r < 0) |
| 862 |
goto fail; |
| 863 |
if (r > 0) |
| 864 |
return 0; |
| 865 |
|
| 866 |
/* See https://tools.ietf.org/html/rfc2308, which say that a matching SOA record in the packet is used to |
| 867 |
* enable negative caching. We apply one exception though: if we are about to cache a weird rcode we do so |
| 868 |
* regardless of a SOA. */ |
| 869 |
r = dns_answer_find_soa(answer, key, &soa, &flags); |
| 870 |
if (r < 0) |
| 871 |
goto fail; |
| 872 |
if (r == 0 && !weird_rcode) |
| 873 |
return 0; |
| 874 |
if (r > 0) { |
| 875 |
/* Refuse using the SOA data if it is unsigned, but the key is signed */ |
| 876 |
if (FLAGS_SET(query_flags, SD_RESOLVED_AUTHENTICATED) && |
| 877 |
(flags & DNS_ANSWER_AUTHENTICATED) == 0) |
| 878 |
return 0; |
| 879 |
} |
| 880 |
|
| 881 |
if (cache_mode == DNS_CACHE_MODE_NO_NEGATIVE) { |
| 882 |
char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
| 883 |
log_debug("Not caching negative entry for: %s, cache mode set to no-negative", |
| 884 |
dns_resource_key_to_string(key, key_str, sizeof key_str)); |
| 885 |
return 0; |
| 886 |
} |
| 887 |
|
| 888 |
r = dns_cache_put_negative( |
| 889 |
c, |
| 890 |
key, |
| 891 |
rcode, |
| 892 |
answer, |
| 893 |
full_packet, |
| 894 |
query_flags, |
| 895 |
dnssec_result, |
| 896 |
nsec_ttl, |
| 897 |
timestamp, |
| 898 |
soa, |
| 899 |
owner_family, |
| 900 |
owner_address); |
| 901 |
if (r < 0) |
| 902 |
goto fail; |
| 903 |
|
| 904 |
return 0; |
| 905 |
|
| 906 |
fail: |
| 907 |
/* Adding all RRs failed. Let's clean up what we already |
| 908 |
* added, just in case */ |
| 909 |
|
| 910 |
if (key) |
| 911 |
dns_cache_remove_by_key(c, key); |
| 912 |
|
| 913 |
DNS_ANSWER_FOREACH_ITEM(item, answer) { |
| 914 |
if ((item->flags & DNS_ANSWER_CACHEABLE) == 0) |
| 915 |
continue; |
| 916 |
|
| 917 |
dns_cache_remove_by_key(c, item->rr->key); |
| 918 |
} |
| 919 |
|
| 920 |
return r; |
| 921 |
} |
| 922 |
|
| 923 |
static DnsCacheItem *dns_cache_get_by_key_follow_cname_dname_nsec( |
| 924 |
DnsCache *c, |
| 925 |
DnsResourceKey *k, |
| 926 |
uint64_t query_flags) { |
| 927 |
|
| 928 |
DnsCacheItem *i; |
| 929 |
const char *n; |
| 930 |
int r; |
| 931 |
|
| 932 |
assert(c); |
| 933 |
assert(k); |
| 934 |
|
| 935 |
/* If we hit some OOM error, or suchlike, we don't care too |
| 936 |
* much, after all this is just a cache */ |
| 937 |
|
| 938 |
i = hashmap_get(c->by_key, k); |
| 939 |
if (i) |
| 940 |
return i; |
| 941 |
|
| 942 |
n = dns_resource_key_name(k); |
| 943 |
|
| 944 |
/* Check if we have an NXDOMAIN cache item for the name, notice that we use |
| 945 |
* the pseudo-type ANY for NXDOMAIN cache items. */ |
| 946 |
i = hashmap_get(c->by_key, &DNS_RESOURCE_KEY_CONST(k->class, DNS_TYPE_ANY, n)); |
| 947 |
if (i && i->type == DNS_CACHE_NXDOMAIN) |
| 948 |
return i; |
| 949 |
|
| 950 |
if (dns_type_may_redirect(k->type) && !FLAGS_SET(query_flags, SD_RESOLVED_NO_CNAME)) { |
| 951 |
/* Check if we have a CNAME record instead */ |
| 952 |
i = hashmap_get(c->by_key, &DNS_RESOURCE_KEY_CONST(k->class, DNS_TYPE_CNAME, n)); |
| 953 |
if (i && i->type != DNS_CACHE_NODATA) |
| 954 |
return i; |
| 955 |
|
| 956 |
/* OK, let's look for cached DNAME records. */ |
| 957 |
for (;;) { |
| 958 |
if (isempty(n)) |
| 959 |
return NULL; |
| 960 |
|
| 961 |
i = hashmap_get(c->by_key, &DNS_RESOURCE_KEY_CONST(k->class, DNS_TYPE_DNAME, n)); |
| 962 |
if (i && i->type != DNS_CACHE_NODATA) |
| 963 |
return i; |
| 964 |
|
| 965 |
/* Jump one label ahead */ |
| 966 |
r = dns_name_parent(&n); |
| 967 |
if (r <= 0) |
| 968 |
return NULL; |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
if (k->type != DNS_TYPE_NSEC) { |
| 973 |
/* Check if we have an NSEC record instead for the name. */ |
| 974 |
i = hashmap_get(c->by_key, &DNS_RESOURCE_KEY_CONST(k->class, DNS_TYPE_NSEC, n)); |
| 975 |
if (i) |
| 976 |
return i; |
| 977 |
} |
| 978 |
|
| 979 |
return NULL; |
| 980 |
} |
| 981 |
|
| 982 |
static int answer_add_clamp_ttl( |
| 983 |
DnsAnswer **answer, |
| 984 |
DnsResourceRecord *rr, |
| 985 |
int ifindex, |
| 986 |
DnsAnswerFlags answer_flags, |
| 987 |
DnsResourceRecord *rrsig, |
| 988 |
uint64_t query_flags, |
| 989 |
usec_t until, |
| 990 |
usec_t current) { |
| 991 |
|
| 992 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *patched = NULL, *patched_rrsig = NULL; |
| 993 |
int r; |
| 994 |
|
| 995 |
assert(answer); |
| 996 |
assert(rr); |
| 997 |
|
| 998 |
if (FLAGS_SET(query_flags, SD_RESOLVED_CLAMP_TTL)) { |
| 999 |
uint32_t left_ttl; |
| 1000 |
|
| 1001 |
assert(current > 0); |
| 1002 |
|
| 1003 |
/* Let's determine how much time is left for this cache entry. Note that we round down, but |
| 1004 |
* clamp this to be 1s at minimum, since we usually want records to remain cached better too |
| 1005 |
* short a time than too long a time, but otoh don't want to return 0 ever, since that has |
| 1006 |
* special semantics in various contexts — in particular in mDNS */ |
| 1007 |
|
| 1008 |
left_ttl = MAX(1U, LESS_BY(until, current) / USEC_PER_SEC); |
| 1009 |
|
| 1010 |
patched = dns_resource_record_ref(rr); |
| 1011 |
|
| 1012 |
r = dns_resource_record_clamp_ttl(&patched, left_ttl); |
| 1013 |
if (r < 0) |
| 1014 |
return r; |
| 1015 |
|
| 1016 |
rr = patched; |
| 1017 |
|
| 1018 |
if (rrsig) { |
| 1019 |
patched_rrsig = dns_resource_record_ref(rrsig); |
| 1020 |
r = dns_resource_record_clamp_ttl(&patched_rrsig, left_ttl); |
| 1021 |
if (r < 0) |
| 1022 |
return r; |
| 1023 |
|
| 1024 |
rrsig = patched_rrsig; |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
r = dns_answer_add_extend_full(answer, rr, ifindex, answer_flags, rrsig, until); |
| 1029 |
if (r < 0) |
| 1030 |
return r; |
| 1031 |
|
| 1032 |
return 0; |
| 1033 |
} |
| 1034 |
|
| 1035 |
int dns_cache_lookup( |
| 1036 |
DnsCache *c, |
| 1037 |
DnsResourceKey *key, |
| 1038 |
uint64_t query_flags, |
| 1039 |
int *ret_rcode, |
| 1040 |
DnsAnswer **ret_answer, |
| 1041 |
DnsPacket **ret_full_packet, |
| 1042 |
uint64_t *ret_query_flags, |
| 1043 |
DnssecResult *ret_dnssec_result) { |
| 1044 |
|
| 1045 |
_cleanup_(dns_packet_unrefp) DnsPacket *full_packet = NULL; |
| 1046 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 1047 |
char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
| 1048 |
unsigned n = 0; |
| 1049 |
int r; |
| 1050 |
bool nxdomain = false; |
| 1051 |
DnsCacheItem *first, *nsec = NULL; |
| 1052 |
bool have_authenticated = false, have_non_authenticated = false, have_confidential = false, have_non_confidential = false; |
| 1053 |
usec_t current = 0; |
| 1054 |
int found_rcode = -1; |
| 1055 |
DnssecResult dnssec_result = -1; |
| 1056 |
int have_dnssec_result = -1; |
| 1057 |
|
| 1058 |
assert(c); |
| 1059 |
assert(key); |
| 1060 |
|
| 1061 |
if (key->type == DNS_TYPE_ANY || key->class == DNS_CLASS_ANY) { |
| 1062 |
/* If we have ANY lookups we don't use the cache, so that the caller refreshes via the |
| 1063 |
* network. */ |
| 1064 |
|
| 1065 |
log_debug("Ignoring cache for ANY lookup: %s", |
| 1066 |
dns_resource_key_to_string(key, key_str, sizeof key_str)); |
| 1067 |
goto miss; |
| 1068 |
} |
| 1069 |
|
| 1070 |
first = dns_cache_get_by_key_follow_cname_dname_nsec(c, key, query_flags); |
| 1071 |
if (!first) { |
| 1072 |
/* If one question cannot be answered we need to refresh */ |
| 1073 |
|
| 1074 |
log_debug("Cache miss for %s", |
| 1075 |
dns_resource_key_to_string(key, key_str, sizeof key_str)); |
| 1076 |
goto miss; |
| 1077 |
} |
| 1078 |
|
| 1079 |
if ((query_flags & (SD_RESOLVED_CLAMP_TTL | SD_RESOLVED_NO_STALE)) != 0) { |
| 1080 |
/* 'current' is always passed to answer_add_clamp_ttl(), but is only used conditionally. |
| 1081 |
* We'll do the same assert there to make sure that it was initialized properly. |
| 1082 |
* 'current' is also used below when SD_RESOLVED_NO_STALE is set. */ |
| 1083 |
current = now(CLOCK_BOOTTIME); |
| 1084 |
assert(current > 0); |
| 1085 |
} |
| 1086 |
|
| 1087 |
LIST_FOREACH(by_key, j, first) { |
| 1088 |
/* If the caller doesn't allow us to answer questions from cache data learned from |
| 1089 |
* "side-effect", skip this entry. */ |
| 1090 |
if (FLAGS_SET(query_flags, SD_RESOLVED_REQUIRE_PRIMARY) && |
| 1091 |
!DNS_CACHE_ITEM_IS_PRIMARY(j)) { |
| 1092 |
log_debug("Primary answer was requested for cache lookup for %s, which we don't have.", |
| 1093 |
dns_resource_key_to_string(key, key_str, sizeof key_str)); |
| 1094 |
|
| 1095 |
goto miss; |
| 1096 |
} |
| 1097 |
|
| 1098 |
/* Skip the next part if ttl is expired and requested with no stale flag. */ |
| 1099 |
if (FLAGS_SET(query_flags, SD_RESOLVED_NO_STALE) && j->until_valid < current) { |
| 1100 |
log_debug("Requested with no stale and TTL expired for %s", |
| 1101 |
dns_resource_key_to_string(key, key_str, sizeof key_str)); |
| 1102 |
|
| 1103 |
goto miss; |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (j->type == DNS_CACHE_NXDOMAIN) |
| 1107 |
nxdomain = true; |
| 1108 |
else if (j->type == DNS_CACHE_RCODE) |
| 1109 |
found_rcode = j->rcode; |
| 1110 |
else if (j->rr) { |
| 1111 |
if (j->rr->key->type == DNS_TYPE_NSEC) |
| 1112 |
nsec = j; |
| 1113 |
|
| 1114 |
n++; |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (FLAGS_SET(j->query_flags, SD_RESOLVED_AUTHENTICATED)) |
| 1118 |
have_authenticated = true; |
| 1119 |
else |
| 1120 |
have_non_authenticated = true; |
| 1121 |
|
| 1122 |
if (FLAGS_SET(j->query_flags, SD_RESOLVED_CONFIDENTIAL)) |
| 1123 |
have_confidential = true; |
| 1124 |
else |
| 1125 |
have_non_confidential = true; |
| 1126 |
|
| 1127 |
if (j->dnssec_result < 0) { |
| 1128 |
have_dnssec_result = false; /* an entry without dnssec result? then invalidate things for good */ |
| 1129 |
dnssec_result = _DNSSEC_RESULT_INVALID; |
| 1130 |
} else if (have_dnssec_result < 0) { |
| 1131 |
have_dnssec_result = true; /* So far no result seen, let's pick this one up */ |
| 1132 |
dnssec_result = j->dnssec_result; |
| 1133 |
} else if (have_dnssec_result > 0 && j->dnssec_result != dnssec_result) { |
| 1134 |
have_dnssec_result = false; /* conflicting result seen? then invalidate for good */ |
| 1135 |
dnssec_result = _DNSSEC_RESULT_INVALID; |
| 1136 |
} |
| 1137 |
|
| 1138 |
/* If the question is being resolved using stale data, the clamp TTL will be set to CACHE_STALE_TTL_MAX_USEC. */ |
| 1139 |
usec_t until = FLAGS_SET(query_flags, SD_RESOLVED_NO_STALE) ? j->until_valid |
| 1140 |
: usec_add(current, CACHE_STALE_TTL_MAX_USEC); |
| 1141 |
|
| 1142 |
/* Append the answer RRs to our answer. Ideally we have the answer object, which we |
| 1143 |
* preferably use. But if the cached entry was generated as "side-effect" of a reply, |
| 1144 |
* i.e. from validated auxiliary records rather than from the main reply, then we use the |
| 1145 |
* individual RRs only instead. */ |
| 1146 |
if (j->answer) { |
| 1147 |
|
| 1148 |
/* Minor optimization, if the full answer object of this and the previous RR is the |
| 1149 |
* same, don't bother adding it again. Typically we store a full RRset here, hence |
| 1150 |
* that should be the case. */ |
| 1151 |
if (!j->by_key_prev || j->answer != j->by_key_prev->answer) { |
| 1152 |
DnsAnswerItem *item; |
| 1153 |
|
| 1154 |
DNS_ANSWER_FOREACH_ITEM(item, j->answer) { |
| 1155 |
r = answer_add_clamp_ttl( |
| 1156 |
&answer, |
| 1157 |
item->rr, |
| 1158 |
item->ifindex, |
| 1159 |
item->flags, |
| 1160 |
item->rrsig, |
| 1161 |
query_flags, |
| 1162 |
until, |
| 1163 |
current); |
| 1164 |
if (r < 0) |
| 1165 |
return r; |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
} else if (j->rr) { |
| 1170 |
r = answer_add_clamp_ttl( |
| 1171 |
&answer, |
| 1172 |
j->rr, |
| 1173 |
j->ifindex, |
| 1174 |
FLAGS_SET(j->query_flags, SD_RESOLVED_AUTHENTICATED) ? DNS_ANSWER_AUTHENTICATED : 0, |
| 1175 |
NULL, |
| 1176 |
query_flags, |
| 1177 |
until, |
| 1178 |
current); |
| 1179 |
if (r < 0) |
| 1180 |
return r; |
| 1181 |
} |
| 1182 |
|
| 1183 |
/* We'll return any packet we have for this. Typically all cache entries for the same key |
| 1184 |
* should come from the same packet anyway, hence it doesn't really matter which packet we |
| 1185 |
* return here, they should all resolve to the same anyway. */ |
| 1186 |
if (!full_packet && j->full_packet) |
| 1187 |
full_packet = dns_packet_ref(j->full_packet); |
| 1188 |
} |
| 1189 |
|
| 1190 |
if (found_rcode >= 0) { |
| 1191 |
log_debug("RCODE %s cache hit for %s", |
| 1192 |
FORMAT_DNS_RCODE(found_rcode), |
| 1193 |
dns_resource_key_to_string(key, key_str, sizeof(key_str))); |
| 1194 |
|
| 1195 |
if (ret_rcode) |
| 1196 |
*ret_rcode = found_rcode; |
| 1197 |
if (ret_answer) |
| 1198 |
*ret_answer = TAKE_PTR(answer); |
| 1199 |
if (ret_full_packet) |
| 1200 |
*ret_full_packet = TAKE_PTR(full_packet); |
| 1201 |
if (ret_query_flags) |
| 1202 |
*ret_query_flags = 0; |
| 1203 |
if (ret_dnssec_result) |
| 1204 |
*ret_dnssec_result = dnssec_result; |
| 1205 |
|
| 1206 |
c->n_hit++; |
| 1207 |
return 1; |
| 1208 |
} |
| 1209 |
|
| 1210 |
if (nsec && !IN_SET(key->type, DNS_TYPE_NSEC, DNS_TYPE_DS)) { |
| 1211 |
/* Note that we won't derive information for DS RRs from an NSEC, because we only cache NSEC |
| 1212 |
* RRs from the lower-zone of a zone cut, but the DS RRs are on the upper zone. */ |
| 1213 |
|
| 1214 |
log_debug("NSEC NODATA cache hit for %s", |
| 1215 |
dns_resource_key_to_string(key, key_str, sizeof key_str)); |
| 1216 |
|
| 1217 |
/* We only found an NSEC record that matches our name. If it says the type doesn't exist |
| 1218 |
* report NODATA. Otherwise report a cache miss. */ |
| 1219 |
|
| 1220 |
if (ret_rcode) |
| 1221 |
*ret_rcode = DNS_RCODE_SUCCESS; |
| 1222 |
if (ret_answer) |
| 1223 |
*ret_answer = TAKE_PTR(answer); |
| 1224 |
if (ret_full_packet) |
| 1225 |
*ret_full_packet = TAKE_PTR(full_packet); |
| 1226 |
if (ret_query_flags) |
| 1227 |
*ret_query_flags = nsec->query_flags; |
| 1228 |
if (ret_dnssec_result) |
| 1229 |
*ret_dnssec_result = nsec->dnssec_result; |
| 1230 |
|
| 1231 |
if (!bitmap_isset(nsec->rr->nsec.types, key->type) && |
| 1232 |
!bitmap_isset(nsec->rr->nsec.types, DNS_TYPE_CNAME) && |
| 1233 |
!bitmap_isset(nsec->rr->nsec.types, DNS_TYPE_DNAME)) { |
| 1234 |
c->n_hit++; |
| 1235 |
return 1; |
| 1236 |
} |
| 1237 |
|
| 1238 |
c->n_miss++; |
| 1239 |
return 0; |
| 1240 |
} |
| 1241 |
|
| 1242 |
log_debug("%s cache hit for %s", |
| 1243 |
n > 0 ? "Positive" : |
| 1244 |
nxdomain ? "NXDOMAIN" : "NODATA", |
| 1245 |
dns_resource_key_to_string(key, key_str, sizeof key_str)); |
| 1246 |
|
| 1247 |
if (n <= 0) { |
| 1248 |
c->n_hit++; |
| 1249 |
|
| 1250 |
if (ret_rcode) |
| 1251 |
*ret_rcode = nxdomain ? DNS_RCODE_NXDOMAIN : DNS_RCODE_SUCCESS; |
| 1252 |
if (ret_answer) |
| 1253 |
*ret_answer = TAKE_PTR(answer); |
| 1254 |
if (ret_full_packet) |
| 1255 |
*ret_full_packet = TAKE_PTR(full_packet); |
| 1256 |
if (ret_query_flags) |
| 1257 |
*ret_query_flags = |
| 1258 |
((have_authenticated && !have_non_authenticated) ? SD_RESOLVED_AUTHENTICATED : 0) | |
| 1259 |
((have_confidential && !have_non_confidential) ? SD_RESOLVED_CONFIDENTIAL : 0); |
| 1260 |
if (ret_dnssec_result) |
| 1261 |
*ret_dnssec_result = dnssec_result; |
| 1262 |
|
| 1263 |
return 1; |
| 1264 |
} |
| 1265 |
|
| 1266 |
c->n_hit++; |
| 1267 |
|
| 1268 |
if (ret_rcode) |
| 1269 |
*ret_rcode = DNS_RCODE_SUCCESS; |
| 1270 |
if (ret_answer) |
| 1271 |
*ret_answer = TAKE_PTR(answer); |
| 1272 |
if (ret_full_packet) |
| 1273 |
*ret_full_packet = TAKE_PTR(full_packet); |
| 1274 |
if (ret_query_flags) |
| 1275 |
*ret_query_flags = |
| 1276 |
((have_authenticated && !have_non_authenticated) ? SD_RESOLVED_AUTHENTICATED : 0) | |
| 1277 |
((have_confidential && !have_non_confidential) ? SD_RESOLVED_CONFIDENTIAL : 0); |
| 1278 |
if (ret_dnssec_result) |
| 1279 |
*ret_dnssec_result = dnssec_result; |
| 1280 |
|
| 1281 |
return n; |
| 1282 |
|
| 1283 |
miss: |
| 1284 |
if (ret_rcode) |
| 1285 |
*ret_rcode = DNS_RCODE_SUCCESS; |
| 1286 |
if (ret_answer) |
| 1287 |
*ret_answer = NULL; |
| 1288 |
if (ret_full_packet) |
| 1289 |
*ret_full_packet = NULL; |
| 1290 |
if (ret_query_flags) |
| 1291 |
*ret_query_flags = 0; |
| 1292 |
if (ret_dnssec_result) |
| 1293 |
*ret_dnssec_result = _DNSSEC_RESULT_INVALID; |
| 1294 |
|
| 1295 |
c->n_miss++; |
| 1296 |
return 0; |
| 1297 |
} |
| 1298 |
|
| 1299 |
int dns_cache_check_conflicts(DnsCache *cache, DnsResourceRecord *rr, int owner_family, const union in_addr_union *owner_address) { |
| 1300 |
DnsCacheItem *first; |
| 1301 |
bool same_owner = true; |
| 1302 |
|
| 1303 |
assert(cache); |
| 1304 |
assert(rr); |
| 1305 |
|
| 1306 |
dns_cache_prune(cache); |
| 1307 |
|
| 1308 |
/* See if there's a cache entry for the same key. If there |
| 1309 |
* isn't there's no conflict */ |
| 1310 |
first = hashmap_get(cache->by_key, rr->key); |
| 1311 |
if (!first) |
| 1312 |
return 0; |
| 1313 |
|
| 1314 |
/* See if the RR key is owned by the same owner, if so, there |
| 1315 |
* isn't a conflict either */ |
| 1316 |
LIST_FOREACH(by_key, i, first) { |
| 1317 |
if (i->owner_family != owner_family || |
| 1318 |
!in_addr_equal(owner_family, &i->owner_address, owner_address)) { |
| 1319 |
same_owner = false; |
| 1320 |
break; |
| 1321 |
} |
| 1322 |
} |
| 1323 |
if (same_owner) |
| 1324 |
return 0; |
| 1325 |
|
| 1326 |
/* See if there's the exact same RR in the cache. If yes, then |
| 1327 |
* there's no conflict. */ |
| 1328 |
if (dns_cache_get(cache, rr)) |
| 1329 |
return 0; |
| 1330 |
|
| 1331 |
/* There's a conflict */ |
| 1332 |
return 1; |
| 1333 |
} |
| 1334 |
|
| 1335 |
int dns_cache_export_shared_to_packet(DnsCache *cache, DnsPacket *p, usec_t ts, unsigned max_rr) { |
| 1336 |
unsigned ancount = 0; |
| 1337 |
DnsCacheItem *i; |
| 1338 |
int r; |
| 1339 |
|
| 1340 |
assert(cache); |
| 1341 |
assert(p); |
| 1342 |
assert(p->protocol == DNS_PROTOCOL_MDNS); |
| 1343 |
|
| 1344 |
HASHMAP_FOREACH(i, cache->by_key) |
| 1345 |
LIST_FOREACH(by_key, j, i) { |
| 1346 |
if (!j->rr) |
| 1347 |
continue; |
| 1348 |
|
| 1349 |
if (!j->shared_owner) |
| 1350 |
continue; |
| 1351 |
|
| 1352 |
/* Ignore cached goodby packet. See on_mdns_packet() and RFC 6762 section 10.1. */ |
| 1353 |
if (j->rr->ttl <= 1) |
| 1354 |
continue; |
| 1355 |
|
| 1356 |
/* RFC6762 7.1: Don't append records with less than half the TTL remaining |
| 1357 |
* as known answers. */ |
| 1358 |
if (usec_sub_unsigned(j->until, ts) < j->rr->ttl * USEC_PER_SEC / 2) |
| 1359 |
continue; |
| 1360 |
|
| 1361 |
if (max_rr > 0 && ancount >= max_rr) { |
| 1362 |
DNS_PACKET_HEADER(p)->ancount = htobe16(ancount); |
| 1363 |
ancount = 0; |
| 1364 |
|
| 1365 |
r = dns_packet_new_query(&p->more, p->protocol, 0, true); |
| 1366 |
if (r < 0) |
| 1367 |
return r; |
| 1368 |
|
| 1369 |
p = p->more; |
| 1370 |
|
| 1371 |
max_rr = UINT_MAX; |
| 1372 |
} |
| 1373 |
|
| 1374 |
r = dns_packet_append_rr(p, j->rr, 0, NULL, NULL); |
| 1375 |
if (r == -EMSGSIZE) { |
| 1376 |
if (max_rr == 0) |
| 1377 |
/* If max_rr == 0, do not allocate more packets. */ |
| 1378 |
goto finalize; |
| 1379 |
|
| 1380 |
/* If we're unable to stuff all known answers into the given packet, allocate |
| 1381 |
* a new one, push the RR into that one and link it to the current one. */ |
| 1382 |
|
| 1383 |
DNS_PACKET_HEADER(p)->ancount = htobe16(ancount); |
| 1384 |
ancount = 0; |
| 1385 |
|
| 1386 |
r = dns_packet_new_query(&p->more, p->protocol, 0, true); |
| 1387 |
if (r < 0) |
| 1388 |
return r; |
| 1389 |
|
| 1390 |
/* continue with new packet */ |
| 1391 |
p = p->more; |
| 1392 |
r = dns_packet_append_rr(p, j->rr, 0, NULL, NULL); |
| 1393 |
} |
| 1394 |
|
| 1395 |
if (r < 0) |
| 1396 |
return r; |
| 1397 |
|
| 1398 |
ancount++; |
| 1399 |
} |
| 1400 |
|
| 1401 |
finalize: |
| 1402 |
DNS_PACKET_HEADER(p)->ancount = htobe16(ancount); |
| 1403 |
|
| 1404 |
return 0; |
| 1405 |
} |
| 1406 |
|
| 1407 |
void dns_cache_dump(DnsCache *cache, FILE *f) { |
| 1408 |
DnsCacheItem *i; |
| 1409 |
|
| 1410 |
if (!cache) |
| 1411 |
return; |
| 1412 |
|
| 1413 |
if (!f) |
| 1414 |
f = stdout; |
| 1415 |
|
| 1416 |
HASHMAP_FOREACH(i, cache->by_key) |
| 1417 |
LIST_FOREACH(by_key, j, i) { |
| 1418 |
|
| 1419 |
fputc('\t', f); |
| 1420 |
|
| 1421 |
if (j->rr) { |
| 1422 |
const char *t; |
| 1423 |
t = dns_resource_record_to_string(j->rr); |
| 1424 |
if (!t) { |
| 1425 |
log_oom(); |
| 1426 |
continue; |
| 1427 |
} |
| 1428 |
|
| 1429 |
fputs(t, f); |
| 1430 |
fputc('\n', f); |
| 1431 |
} else { |
| 1432 |
char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
| 1433 |
|
| 1434 |
fputs(dns_resource_key_to_string(j->key, key_str, sizeof key_str), f); |
| 1435 |
fputs(" -- ", f); |
| 1436 |
fputs(dns_cache_item_type_to_string(j), f); |
| 1437 |
fputc('\n', f); |
| 1438 |
} |
| 1439 |
} |
| 1440 |
} |
| 1441 |
|
| 1442 |
int dns_cache_dump_to_json(DnsCache *cache, sd_json_variant **ret) { |
| 1443 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *c = NULL; |
| 1444 |
DnsCacheItem *i; |
| 1445 |
int r; |
| 1446 |
|
| 1447 |
assert(cache); |
| 1448 |
assert(ret); |
| 1449 |
|
| 1450 |
HASHMAP_FOREACH(i, cache->by_key) { |
| 1451 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *d = NULL, *k = NULL; |
| 1452 |
|
| 1453 |
r = dns_resource_key_to_json(i->key, &k); |
| 1454 |
if (r < 0) |
| 1455 |
return r; |
| 1456 |
|
| 1457 |
if (i->rr) { |
| 1458 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL; |
| 1459 |
|
| 1460 |
LIST_FOREACH(by_key, j, i) { |
| 1461 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *rj = NULL; |
| 1462 |
|
| 1463 |
assert(j->rr); |
| 1464 |
|
| 1465 |
r = dns_resource_record_to_json(j->rr, &rj); |
| 1466 |
if (r < 0) |
| 1467 |
return r; |
| 1468 |
|
| 1469 |
r = dns_resource_record_to_wire_format(j->rr, /* canonical= */ false); /* don't use DNSSEC canonical format, since it removes casing, but we want that for DNS_SD compat */ |
| 1470 |
if (r < 0) |
| 1471 |
return r; |
| 1472 |
|
| 1473 |
r = sd_json_variant_append_arraybo( |
| 1474 |
&l, |
| 1475 |
SD_JSON_BUILD_PAIR_VARIANT("rr", rj), |
| 1476 |
SD_JSON_BUILD_PAIR_BASE64("raw", j->rr->wire_format, j->rr->wire_format_size)); |
| 1477 |
if (r < 0) |
| 1478 |
return r; |
| 1479 |
} |
| 1480 |
|
| 1481 |
if (!l) { |
| 1482 |
r = sd_json_variant_new_array(&l, NULL, 0); |
| 1483 |
if (r < 0) |
| 1484 |
return r; |
| 1485 |
} |
| 1486 |
|
| 1487 |
r = sd_json_buildo( |
| 1488 |
&d, |
| 1489 |
SD_JSON_BUILD_PAIR_VARIANT("key", k), |
| 1490 |
SD_JSON_BUILD_PAIR_VARIANT("rrs", l), |
| 1491 |
SD_JSON_BUILD_PAIR_UNSIGNED("until", i->until)); |
| 1492 |
} else if (i->type == DNS_CACHE_NODATA) { |
| 1493 |
r = sd_json_buildo( |
| 1494 |
&d, |
| 1495 |
SD_JSON_BUILD_PAIR_VARIANT("key", k), |
| 1496 |
SD_JSON_BUILD_PAIR_EMPTY_ARRAY("rrs"), |
| 1497 |
SD_JSON_BUILD_PAIR_UNSIGNED("until", i->until)); |
| 1498 |
} else |
| 1499 |
r = sd_json_buildo( |
| 1500 |
&d, |
| 1501 |
SD_JSON_BUILD_PAIR_VARIANT("key", k), |
| 1502 |
SD_JSON_BUILD_PAIR_STRING("type", dns_cache_item_type_to_string(i)), |
| 1503 |
SD_JSON_BUILD_PAIR_UNSIGNED("until", i->until)); |
| 1504 |
if (r < 0) |
| 1505 |
return r; |
| 1506 |
|
| 1507 |
r = sd_json_variant_append_array(&c, d); |
| 1508 |
if (r < 0) |
| 1509 |
return r; |
| 1510 |
} |
| 1511 |
|
| 1512 |
if (!c) |
| 1513 |
return sd_json_variant_new_array(ret, NULL, 0); |
| 1514 |
|
| 1515 |
*ret = TAKE_PTR(c); |
| 1516 |
return 0; |
| 1517 |
} |
| 1518 |
|
| 1519 |
bool dns_cache_is_empty(DnsCache *cache) { |
| 1520 |
if (!cache) |
| 1521 |
return true; |
| 1522 |
|
| 1523 |
return hashmap_isempty(cache->by_key); |
| 1524 |
} |
| 1525 |
|
| 1526 |
unsigned dns_cache_size(DnsCache *cache) { |
| 1527 |
if (!cache) |
| 1528 |
return 0; |
| 1529 |
|
| 1530 |
return hashmap_size(cache->by_key); |
| 1531 |
} |