chiark / gitweb /
resolved: rework logic so that we can share transactions between queries of different...
[elogind.git] / src / resolve / resolved-dns-answer.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "resolved-dns-answer.h"
23
24 DnsAnswer *dns_answer_new(unsigned n) {
25         DnsAnswer *a;
26
27         assert(n > 0);
28
29         a = malloc0(offsetof(DnsAnswer, rrs) + sizeof(DnsResourceRecord*) * n);
30         if (!a)
31                 return NULL;
32
33         a->n_ref = 1;
34         a->n_allocated = n;
35
36         return a;
37 }
38
39 DnsAnswer *dns_answer_ref(DnsAnswer *a) {
40         if (!a)
41                 return NULL;
42
43         assert(a->n_ref > 0);
44         a->n_ref++;
45         return a;
46 }
47
48 DnsAnswer *dns_answer_unref(DnsAnswer *a) {
49         if (!a)
50                 return NULL;
51
52         assert(a->n_ref > 0);
53
54         if (a->n_ref == 1) {
55                 unsigned i;
56
57                 for (i = 0; i < a->n_rrs; i++)
58                         dns_resource_record_unref(a->rrs[i]);
59
60                 free(a);
61         } else
62                 a->n_ref--;
63
64         return NULL;
65 }
66
67 int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr) {
68         assert(a);
69         assert(rr);
70
71         if (a->n_rrs >= a->n_allocated)
72                 return -ENOSPC;
73
74         a->rrs[a->n_rrs++] = dns_resource_record_ref(rr);
75         return 0;
76 }