1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 typedef struct DnsQuestion DnsQuestion;
5 typedef struct DnsQuestionItem DnsQuestionItem;
6
7 #include "macro.h"
8 #include "resolved-dns-rr.h"
9
10 /* A simple array of resource keys */
11
12 typedef enum DnsQuestionFlags {
13 DNS_QUESTION_WANTS_UNICAST_REPLY = 1 << 0, /* For mDNS: sender is willing to accept unicast replies */
14 } DnsQuestionFlags;
15
16 struct DnsQuestionItem {
17 DnsResourceKey *key;
18 DnsQuestionFlags flags;
19 };
20
21 struct DnsQuestion {
22 unsigned n_ref;
23 size_t n_keys, n_allocated;
24 DnsQuestionItem items[0];
25 };
26
27 DnsQuestion *dns_question_new(size_t n);
28 DnsQuestion *dns_question_ref(DnsQuestion *q);
29 DnsQuestion *dns_question_unref(DnsQuestion *q);
30
31 int dns_question_new_address(DnsQuestion **ret, int family, const char *name, bool convert_idna);
32 int dns_question_new_reverse(DnsQuestion **ret, int family, const union in_addr_union *a);
33 int dns_question_new_service(DnsQuestion **ret, const char *service, const char *type, const char *domain, bool with_txt, bool convert_idna);
34
35 int dns_question_add_raw(DnsQuestion *q, DnsResourceKey *key, DnsQuestionFlags flags);
36 int dns_question_add(DnsQuestion *q, DnsResourceKey *key, DnsQuestionFlags flags);
37
38 int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain);
39 int dns_question_matches_cname_or_dname(DnsQuestion *q, DnsResourceRecord *rr, const char* search_domain);
40 int dns_question_is_valid_for_query(DnsQuestion *q);
41 int dns_question_contains_key(DnsQuestion *q, const DnsResourceKey *k);
42 int dns_question_is_equal(DnsQuestion *a, DnsQuestion *b);
43
44 int dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname, DnsQuestion **ret);
45
46 void dns_question_dump(DnsQuestion *q, FILE *f);
47
48 const char *dns_question_first_name(DnsQuestion *q);
49
dns_question_first_key(DnsQuestion * q)50 static inline DnsResourceKey *dns_question_first_key(DnsQuestion *q) {
51 return (q && q->n_keys > 0) ? q->items[0].key : NULL;
52 }
53
dns_question_size(DnsQuestion * q)54 static inline size_t dns_question_size(DnsQuestion *q) {
55 return q ? q->n_keys : 0;
56 }
57
dns_question_isempty(DnsQuestion * q)58 static inline bool dns_question_isempty(DnsQuestion *q) {
59 return dns_question_size(q) <= 0;
60 }
61
62 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsQuestion*, dns_question_unref);
63
64 #define _DNS_QUESTION_FOREACH(u, k, q) \
65 for (size_t UNIQ_T(i, u) = ({ \
66 (k) = ((q) && (q)->n_keys > 0) ? (q)->items[0].key : NULL; \
67 0; \
68 }); \
69 (q) && (UNIQ_T(i, u) < (q)->n_keys); \
70 UNIQ_T(i, u)++, (k) = (UNIQ_T(i, u) < (q)->n_keys ? (q)->items[UNIQ_T(i, u)].key : NULL))
71
72 #define DNS_QUESTION_FOREACH(key, q) _DNS_QUESTION_FOREACH(UNIQ, key, q)
73
74 #define _DNS_QUESTION_FOREACH_ITEM(u, item, q) \
75 for (size_t UNIQ_T(i, u) = ({ \
76 (item) = dns_question_isempty(q) ? NULL : (q)->items; \
77 0; \
78 }); \
79 UNIQ_T(i, u) < dns_question_size(q); \
80 UNIQ_T(i, u)++, (item) = (UNIQ_T(i, u) < dns_question_size(q) ? (q)->items + UNIQ_T(i, u) : NULL))
81
82 #define DNS_QUESTION_FOREACH_ITEM(item, q) _DNS_QUESTION_FOREACH_ITEM(UNIQ, item, q)
83