]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-answer.h
network: fix typo in log message
[thirdparty/systemd.git] / src / resolve / resolved-dns-answer.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
23 typedef struct DnsAnswer DnsAnswer;
24 typedef struct DnsAnswerItem DnsAnswerItem;
25
26 #include "macro.h"
27 #include "resolved-dns-rr.h"
28
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
32 * right ifindex.
33 *
34 * Note that we usually encode the empty DnsAnswer object as a simple NULL. */
35
36 typedef enum DnsAnswerFlags {
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 */
42 } DnsAnswerFlags;
43
44 struct DnsAnswerItem {
45 DnsResourceRecord *rr;
46 int ifindex;
47 DnsAnswerFlags flags;
48 };
49
50 struct DnsAnswer {
51 unsigned n_ref;
52 unsigned n_rrs, n_allocated;
53 DnsAnswerItem items[0];
54 };
55
56 DnsAnswer *dns_answer_new(unsigned n);
57 DnsAnswer *dns_answer_ref(DnsAnswer *a);
58 DnsAnswer *dns_answer_unref(DnsAnswer *a);
59
60 int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags);
61 int dns_answer_add_extend(DnsAnswer **a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags);
62 int dns_answer_add_soa(DnsAnswer *a, const char *name, uint32_t ttl, int ifindex);
63
64 int dns_answer_match_key(DnsAnswer *a, const DnsResourceKey *key, DnsAnswerFlags *combined_flags);
65 int dns_answer_contains_rr(DnsAnswer *a, DnsResourceRecord *rr, DnsAnswerFlags *combined_flags);
66 int dns_answer_contains_key(DnsAnswer *a, const DnsResourceKey *key, DnsAnswerFlags *combined_flags);
67 int dns_answer_contains_nsec_or_nsec3(DnsAnswer *a);
68 int dns_answer_contains_zone_nsec3(DnsAnswer *answer, const char *zone);
69
70 int dns_answer_find_soa(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord **ret, DnsAnswerFlags *flags);
71 int dns_answer_find_cname_or_dname(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord **ret, DnsAnswerFlags *flags);
72
73 int dns_answer_merge(DnsAnswer *a, DnsAnswer *b, DnsAnswer **ret);
74 int dns_answer_extend(DnsAnswer **a, DnsAnswer *b);
75
76 void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local);
77
78 int dns_answer_reserve(DnsAnswer **a, unsigned n_free);
79 int dns_answer_reserve_or_clone(DnsAnswer **a, unsigned n_free);
80
81 int dns_answer_remove_by_key(DnsAnswer **a, const DnsResourceKey *key);
82 int dns_answer_remove_by_rr(DnsAnswer **a, DnsResourceRecord *rr);
83
84 int dns_answer_copy_by_key(DnsAnswer **a, DnsAnswer *source, const DnsResourceKey *key, DnsAnswerFlags or_flags);
85 int dns_answer_move_by_key(DnsAnswer **to, DnsAnswer **from, const DnsResourceKey *key, DnsAnswerFlags or_flags);
86
87 bool dns_answer_has_dname_for_cname(DnsAnswer *a, DnsResourceRecord *cname);
88
89 static inline unsigned dns_answer_size(DnsAnswer *a) {
90 return a ? a->n_rrs : 0;
91 }
92
93 static inline bool dns_answer_isempty(DnsAnswer *a) {
94 return dns_answer_size(a) <= 0;
95 }
96
97 void dns_answer_dump(DnsAnswer *answer, FILE *f);
98
99 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsAnswer*, dns_answer_unref);
100
101 #define _DNS_ANSWER_FOREACH(q, kk, a) \
102 for (unsigned UNIQ_T(i, q) = ({ \
103 (kk) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].rr : NULL; \
104 0; \
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))
108
109 #define DNS_ANSWER_FOREACH(kk, a) _DNS_ANSWER_FOREACH(UNIQ, kk, a)
110
111 #define _DNS_ANSWER_FOREACH_IFINDEX(q, kk, ifi, a) \
112 for (unsigned UNIQ_T(i, q) = ({ \
113 (kk) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].rr : NULL; \
114 (ifi) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].ifindex : 0; \
115 0; \
116 }); \
117 (a) && (UNIQ_T(i, q) < (a)->n_rrs); \
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))
121
122 #define DNS_ANSWER_FOREACH_IFINDEX(kk, ifindex, a) _DNS_ANSWER_FOREACH_IFINDEX(UNIQ, kk, ifindex, a)
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)