summaryrefslogtreecommitdiffstats
path: root/src/knot/query/query.c
blob: d91c00866e2cf9512fee1c2cd09b23a027b419ec (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
/*  Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

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

    This program 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 General Public License for more details.

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

#include "knot/query/query.h"

#include <stdint.h>

#include "contrib/wire_ctx.h"
#include "libdnssec/random.h"
#include "knot/conf/conf.h"
#include "libknot/yparser/yptrafo.h"
#include "libknot/rrset.h"

void query_init_pkt(knot_pkt_t *pkt)
{
	if (pkt == NULL) {
		return;
	}

	knot_pkt_clear(pkt);
	knot_wire_set_id(pkt->wire, dnssec_random_uint16_t());
}

int query_edns_data_init(struct query_edns_data *edns_ptr, conf_t *conf,
                         const knot_dname_t *zone, int remote_family)
{
	if (!edns_ptr || !conf || !zone) {
		return KNOT_EINVAL;
	}

	struct query_edns_data edns = { 0 };

	// Determine max payload

	switch (remote_family) {
	case AF_INET:
		edns.max_payload = conf->cache.srv_udp_max_payload_ipv4;
		break;
	case AF_INET6:
		edns.max_payload = conf->cache.srv_udp_max_payload_ipv6;
		break;
	default:
		return KNOT_EINVAL;
	}

	*edns_ptr = edns;
	return KNOT_EOK;
}

int query_put_edns(knot_pkt_t *pkt, const struct query_edns_data *edns)
{
	if (!pkt || !edns) {
		return KNOT_EINVAL;
	}

	// Construct EDNS RR

	knot_rrset_t opt_rr = { 0 };
	int ret = knot_edns_init(&opt_rr, edns->max_payload, 0, KNOT_EDNS_VERSION, &pkt->mm);
	if (ret != KNOT_EOK) {
		return ret;
	}

	if (edns->do_flag) {
		knot_edns_set_do(&opt_rr);
	}

	// Add result into the packet

	knot_pkt_begin(pkt, KNOT_ADDITIONAL);

	ret = knot_pkt_put(pkt, KNOT_COMPR_HINT_NOCOMP, &opt_rr, KNOT_PF_FREE);
	if (ret != KNOT_EOK) {
		knot_rrset_clear(&opt_rr, &pkt->mm);
		return ret;
	}

	return KNOT_EOK;
}