summaryrefslogtreecommitdiff
path: root/src/resolve/resolved-dns-cache.c
blob: e42af41c78282c7a5e0d0f3876e7b1845d37159e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2014 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include "resolved-dns-cache.h"

/* Never cache more than 1K entries */
#define CACHE_MAX 1024

/* We never keep any item longer than 10min in our cache */
#define CACHE_TTL_MAX_USEC (10 * USEC_PER_MINUTE)

static void dns_cache_item_free(DnsCacheItem *i) {
        if (!i)
                return;

        dns_resource_record_unref(i->rr);
        free(i);
}

DEFINE_TRIVIAL_CLEANUP_FUNC(DnsCacheItem*, dns_cache_item_free);

static void dns_cache_item_remove_and_free(DnsCache *c, DnsCacheItem *i) {
        DnsCacheItem *first;

        assert(c);

        if (!i)
                return;

        first = hashmap_get(c->rrsets, i->rr->key);
        LIST_REMOVE(rrsets, first, i);

        if (first)
                assert_se(hashmap_replace(c->rrsets, first->rr->key, first) >= 0);
        else
                hashmap_remove(c->rrsets, i->rr->key);

        prioq_remove(c->expire, i, &i->expire_prioq_idx);

        dns_cache_item_free(i);
}

void dns_cache_flush(DnsCache *c) {
        DnsCacheItem *i;

        assert(c);

        while ((i = hashmap_first(c->rrsets)))
                dns_cache_item_remove_and_free(c, i);

        assert(hashmap_size(c->rrsets) == 0);
        assert(prioq_size(c->expire) == 0);

        hashmap_free(c->rrsets);
        c->rrsets = NULL;

        prioq_free(c->expire);
        c->expire = NULL;
}

void dns_cache_remove(DnsCache *c, DnsResourceKey *key) {
        DnsCacheItem *i;

        assert(c);
        assert(key);

        while ((i = hashmap_get(c->rrsets, key)))
                dns_cache_item_remove_and_free(c, i);
}

static void dns_cache_make_space(DnsCache *c, unsigned add) {
        assert(c);

        if (add <= 0)
                return;

        /* Makes space for n new entries. Note that we actually allow
         * the cache to grow beyond CACHE_MAX, but only when we shall
         * add more RRs to the cache than CACHE_MAX at once. In that
         * case the cache will be emptied completely otherwise. */

        for (;;) {
                _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
                DnsCacheItem *i;

                if (prioq_size(c->expire) <= 0)
                        break;

                if (prioq_size(c->expire) + add < CACHE_MAX)
                        break;

                i = prioq_peek(c->expire);
                assert(i);

                /* Take an extra reference to the key so that it
                 * doesn't go away in the middle of the remove call */
                key = dns_resource_key_ref(i->rr->key);
                dns_cache_remove(c, key);
        }
}

void dns_cache_prune(DnsCache *c) {
        usec_t t = 0;

        assert(c);

        /* Remove all entries that are past their TTL */

        for (;;) {
                _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
                DnsCacheItem *i;
                usec_t ttl;

                i = prioq_peek(c->expire);
                if (!i)
                        break;

                ttl = i->rr->ttl * USEC_PER_SEC;
                if (ttl > CACHE_TTL_MAX_USEC)
                        ttl = CACHE_TTL_MAX_USEC;

                if (t <= 0)
                        t = now(CLOCK_MONOTONIC);

                if (i->timestamp + ttl > t)
                        break;

                /* Take an extra reference to the key so that it
                 * doesn't go away in the middle of the remove call */
                key = dns_resource_key_ref(i->rr->key);
                dns_cache_remove(c, key);
        }
}

static int dns_cache_item_prioq_compare_func(const void *a, const void *b) {
        usec_t t, z;
        const DnsCacheItem *x = a, *y = b;

        t = x->timestamp + x->rr->ttl * USEC_PER_SEC;
        z = y->timestamp + y->rr->ttl * USEC_PER_SEC;

        if (t < z)
                return -1;
        if (t > z)
                return 1;
        return 0;
}

static void dns_cache_item_update(DnsCache *c, DnsCacheItem *i, DnsResourceRecord *rr, usec_t timestamp) {
        assert(c);
        assert(i);
        assert(rr);

        if (!i->rrsets_prev) {
                /* We are the first item in the list, we need to
                 * update the key used in the hashmap */

                assert_se(hashmap_replace(c->rrsets, rr->key, i) >= 0);
        }

        dns_resource_record_ref(rr);
        dns_resource_record_unref(i->rr);
        i->rr = rr;

        i->timestamp = timestamp;
        prioq_reshuffle(c->expire, i, &i->expire_prioq_idx);
}

static DnsCacheItem* dns_cache_get(DnsCache *c, DnsResourceRecord *rr) {
        DnsCacheItem *i;

        assert(c);
        assert(rr);

        LIST_FOREACH(rrsets, i, hashmap_get(c->rrsets, rr->key))
                if (dns_resource_record_equal(i->rr, rr))
                        return i;

        return NULL;
}

int dns_cache_put(DnsCache *c, DnsResourceRecord *rr, usec_t timestamp) {
        _cleanup_(dns_cache_item_freep) DnsCacheItem *i = NULL;
        DnsCacheItem *first = NULL, *existing;
        int r;

        assert(c);
        assert(rr);

        /* New TTL is 0? Delete the entry... */
        if (rr->ttl <= 0) {
                dns_cache_remove(c, rr->key);
                return 0;
        }

        /* Entry exists already? Update TTL and timestamp */
        existing = dns_cache_get(c, rr);
        if (existing) {
                dns_cache_item_update(c, existing, rr, timestamp);
                return 0;
        }

        /* Otherwise, add the new RR */
        r = prioq_ensure_allocated(&c->expire, dns_cache_item_prioq_compare_func);
        if (r < 0)
                return r;

        r = hashmap_ensure_allocated(&c->rrsets, dns_resource_key_hash_func, dns_resource_key_compare_func);
        if (r < 0)
                return r;

        dns_cache_make_space(c, 1);

        i = new0(DnsCacheItem, 1);
        if (!i)
                return -ENOMEM;

        i->rr = dns_resource_record_ref(rr);
        i->timestamp = timestamp;
        i->expire_prioq_idx = PRIOQ_IDX_NULL;

        r = prioq_put(c->expire, i, &i->expire_prioq_idx);
        if (r < 0)
                return r;

        first = hashmap_get(c->rrsets, i->rr->key);
        if (first) {
                LIST_PREPEND(rrsets, first, i);
                assert_se(hashmap_replace(c->rrsets, first->rr->key, first) >= 0);
        } else {
                r = hashmap_put(c->rrsets, i->rr->key, i);
                if (r < 0) {
                        prioq_remove(c->expire, i, &i->expire_prioq_idx);
                        return r;
                }
        }

        i = NULL;

        return 0;
}

int dns_cache_put_answer(DnsCache *c, DnsAnswer *answer, usec_t timestamp) {
        unsigned i, added = 0;
        int r;

        assert(c);
        assert(answer);

        /* First iteration, delete all matching old RRs, so that we
         * only keep complete rrsets in place. */
        for (i = 0; i < answer->n_rrs; i++)
                dns_cache_remove(c, answer->rrs[i]->key);

        dns_cache_make_space(c, answer->n_rrs);

        /* Second iteration, add in new RRs */
        for (added = 0; added < answer->n_rrs; added++) {
                if (timestamp <= 0)
                        timestamp = now(CLOCK_MONOTONIC);

                r = dns_cache_put(c, answer->rrs[added], timestamp);
                if (r < 0)
                        goto fail;
        }

        return 0;

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

        for (i = 0; i < added; i++)
                dns_cache_remove(c, answer->rrs[i]->key);

        return r;
}

int dns_cache_lookup(DnsCache *c, DnsQuestion *q, DnsAnswer **ret) {
        _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
        unsigned i, n = 0;
        int r;

        assert(c);
        assert(q);
        assert(ret);

        if (q->n_keys <= 0) {
                *ret = NULL;
                return 0;
        }

        for (i = 0; i < q->n_keys; i++) {
                DnsCacheItem *j;

                j = hashmap_get(c->rrsets, q->keys[i]);
                if (!j) {
                        /* If one question cannot be answered we need to refresh */
                        *ret = NULL;
                        return 0;
                }

                LIST_FOREACH(rrsets, j, j)
                        n++;
        }

        assert(n > 0);

        answer = dns_answer_new(n);
        if (!answer)
                return -ENOMEM;

        for (i = 0; i < q->n_keys; i++) {
                DnsCacheItem *j;

                j = hashmap_get(c->rrsets, q->keys[i]);
                LIST_FOREACH(rrsets, j, j) {
                        r = dns_answer_add(answer, j->rr);
                        if (r < 0)
                                return r;
                }
        }

        assert(n >= answer->n_rrs);

        *ret = answer;
        answer = NULL;

        return n;
}