]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-dns-answer.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / resolve / resolved-dns-answer.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
faa133f3
LP
2#pragma once
3
4/***
5 This file is part of systemd.
6
7 Copyright 2014 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
22
23typedef struct DnsAnswer DnsAnswer;
78c6a153 24typedef struct DnsAnswerItem DnsAnswerItem;
faa133f3 25
92aea95e 26#include "macro.h"
faa133f3
LP
27#include "resolved-dns-rr.h"
28
78c6a153
LP
29/* A simple array of resource records. We keep track of the
30 * originating ifindex for each RR where that makes sense, so that we
31 * can qualify A and AAAA RRs referring to a local link with the
547973de
LP
32 * right ifindex.
33 *
c629ff58 34 * Note that we usually encode the empty DnsAnswer object as a simple NULL. */
105e1512
LP
35
36typedef enum DnsAnswerFlags {
c3ae4188
DR
37 DNS_ANSWER_AUTHENTICATED = 1, /* Item has been authenticated */
38 DNS_ANSWER_CACHEABLE = 2, /* Item is subject to caching */
39 DNS_ANSWER_SHARED_OWNER = 4, /* For mDNS: RRset may be owner by multiple peers */
40 DNS_ANSWER_CACHE_FLUSH = 8, /* For mDNS: sets cache-flush bit in the rrclass of response records */
41 DNS_ANSWER_GOODBYE = 16, /* For mDNS: item is subject to disappear */
105e1512 42} DnsAnswerFlags;
78c6a153
LP
43
44struct DnsAnswerItem {
45 DnsResourceRecord *rr;
46 int ifindex;
105e1512 47 DnsAnswerFlags flags;
78c6a153 48};
faa133f3
LP
49
50struct DnsAnswer {
51 unsigned n_ref;
52 unsigned n_rrs, n_allocated;
78c6a153 53 DnsAnswerItem items[0];
faa133f3
LP
54};
55
56DnsAnswer *dns_answer_new(unsigned n);
57DnsAnswer *dns_answer_ref(DnsAnswer *a);
58DnsAnswer *dns_answer_unref(DnsAnswer *a);
59
105e1512
LP
60int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags);
61int dns_answer_add_extend(DnsAnswer **a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags);
97ebebbc 62int dns_answer_add_soa(DnsAnswer *a, const char *name, uint32_t ttl, int ifindex);
faa133f3 63
105e1512
LP
64int dns_answer_match_key(DnsAnswer *a, const DnsResourceKey *key, DnsAnswerFlags *combined_flags);
65int dns_answer_contains_rr(DnsAnswer *a, DnsResourceRecord *rr, DnsAnswerFlags *combined_flags);
66int dns_answer_contains_key(DnsAnswer *a, const DnsResourceKey *key, DnsAnswerFlags *combined_flags);
67int dns_answer_contains_nsec_or_nsec3(DnsAnswer *a);
e926785a 68int dns_answer_contains_zone_nsec3(DnsAnswer *answer, const char *zone);
547973de 69
fd009cd8 70int dns_answer_find_soa(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord **ret, DnsAnswerFlags *flags);
105e1512 71int dns_answer_find_cname_or_dname(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord **ret, DnsAnswerFlags *flags);
547973de
LP
72
73int dns_answer_merge(DnsAnswer *a, DnsAnswer *b, DnsAnswer **ret);
74int dns_answer_extend(DnsAnswer **a, DnsAnswer *b);
75
af93291c 76void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local);
934e9b10 77
78c6a153 78int dns_answer_reserve(DnsAnswer **a, unsigned n_free);
547973de
LP
79int dns_answer_reserve_or_clone(DnsAnswer **a, unsigned n_free);
80
81int dns_answer_remove_by_key(DnsAnswer **a, const DnsResourceKey *key);
0c857028
LP
82int dns_answer_remove_by_rr(DnsAnswer **a, DnsResourceRecord *rr);
83
105e1512
LP
84int dns_answer_copy_by_key(DnsAnswer **a, DnsAnswer *source, const DnsResourceKey *key, DnsAnswerFlags or_flags);
85int dns_answer_move_by_key(DnsAnswer **to, DnsAnswer **from, const DnsResourceKey *key, DnsAnswerFlags or_flags);
547973de 86
43e6779a
LP
87bool dns_answer_has_dname_for_cname(DnsAnswer *a, DnsResourceRecord *cname);
88
547973de
LP
89static inline unsigned dns_answer_size(DnsAnswer *a) {
90 return a ? a->n_rrs : 0;
91}
78c6a153 92
501e8eb0
LP
93static inline bool dns_answer_isempty(DnsAnswer *a) {
94 return dns_answer_size(a) <= 0;
95}
96
26156910
LP
97void dns_answer_dump(DnsAnswer *answer, FILE *f);
98
faa133f3 99DEFINE_TRIVIAL_CLEANUP_FUNC(DnsAnswer*, dns_answer_unref);
45ec7efb 100
92aea95e
LP
101#define _DNS_ANSWER_FOREACH(q, kk, a) \
102 for (unsigned UNIQ_T(i, q) = ({ \
801ad6a6 103 (kk) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].rr : NULL; \
45ec7efb 104 0; \
92aea95e
LP
105 }); \
106 (a) && (UNIQ_T(i, q) < (a)->n_rrs); \
107 UNIQ_T(i, q)++, (kk) = (UNIQ_T(i, q) < (a)->n_rrs ? (a)->items[UNIQ_T(i, q)].rr : NULL))
45ec7efb 108
92aea95e
LP
109#define DNS_ANSWER_FOREACH(kk, a) _DNS_ANSWER_FOREACH(UNIQ, kk, a)
110
aa44ee27 111#define _DNS_ANSWER_FOREACH_IFINDEX(q, kk, ifi, a) \
92aea95e 112 for (unsigned UNIQ_T(i, q) = ({ \
801ad6a6 113 (kk) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].rr : NULL; \
aa44ee27 114 (ifi) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].ifindex : 0; \
45ec7efb 115 0; \
92aea95e
LP
116 }); \
117 (a) && (UNIQ_T(i, q) < (a)->n_rrs); \
105e1512
LP
118 UNIQ_T(i, q)++, \
119 (kk) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].rr : NULL), \
120 (ifi) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].ifindex : 0))
92aea95e
LP
121
122#define DNS_ANSWER_FOREACH_IFINDEX(kk, ifindex, a) _DNS_ANSWER_FOREACH_IFINDEX(UNIQ, kk, ifindex, a)
105e1512
LP
123
124#define _DNS_ANSWER_FOREACH_FLAGS(q, kk, fl, a) \
125 for (unsigned UNIQ_T(i, q) = ({ \
126 (kk) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].rr : NULL; \
127 (fl) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].flags : 0; \
128 0; \
129 }); \
130 (a) && (UNIQ_T(i, q) < (a)->n_rrs); \
131 UNIQ_T(i, q)++, \
132 (kk) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].rr : NULL), \
133 (fl) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].flags : 0))
134
135#define DNS_ANSWER_FOREACH_FLAGS(kk, flags, a) _DNS_ANSWER_FOREACH_FLAGS(UNIQ, kk, flags, a)
136
137#define _DNS_ANSWER_FOREACH_FULL(q, kk, ifi, fl, a) \
138 for (unsigned UNIQ_T(i, q) = ({ \
139 (kk) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].rr : NULL; \
140 (ifi) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].ifindex : 0; \
141 (fl) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].flags : 0; \
142 0; \
143 }); \
144 (a) && (UNIQ_T(i, q) < (a)->n_rrs); \
145 UNIQ_T(i, q)++, \
146 (kk) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].rr : NULL), \
147 (ifi) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].ifindex : 0), \
148 (fl) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].flags : 0))
149
150#define DNS_ANSWER_FOREACH_FULL(kk, ifindex, flags, a) _DNS_ANSWER_FOREACH_FULL(UNIQ, kk, ifindex, flags, a)