1338566Sdes/*
2338566Sdes * util/edns.h - handle base EDNS options.
3338566Sdes *
4338566Sdes * Copyright (c) 2018, NLnet Labs. All rights reserved.
5338566Sdes *
6338566Sdes * This software is open source.
7338566Sdes *
8338566Sdes * Redistribution and use in source and binary forms, with or without
9338566Sdes * modification, are permitted provided that the following conditions
10338566Sdes * are met:
11338566Sdes *
12338566Sdes * Redistributions of source code must retain the above copyright notice,
13338566Sdes * this list of conditions and the following disclaimer.
14338566Sdes *
15338566Sdes * Redistributions in binary form must reproduce the above copyright notice,
16338566Sdes * this list of conditions and the following disclaimer in the documentation
17338566Sdes * and/or other materials provided with the distribution.
18338566Sdes *
19338566Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20338566Sdes * be used to endorse or promote products derived from this software without
21338566Sdes * specific prior written permission.
22338566Sdes *
23338566Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24338566Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25338566Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26338566Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27338566Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28338566Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29338566Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30338566Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31338566Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32338566Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33338566Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34338566Sdes */
35338566Sdes
36338566Sdes/**
37338566Sdes * \file
38338566Sdes *
39338566Sdes * This file contains functions for base EDNS options.
40338566Sdes */
41338566Sdes
42338566Sdes#ifndef UTIL_EDNS_H
43338566Sdes#define UTIL_EDNS_H
44338566Sdes
45368129Scy#include "util/storage/dnstree.h"
46368129Scy
47338566Sdesstruct edns_data;
48338566Sdesstruct config_file;
49338566Sdesstruct comm_point;
50338566Sdesstruct regional;
51338566Sdes
52338566Sdes/**
53368693Scy * Structure containing all EDNS strings.
54368129Scy */
55368693Scystruct edns_strings {
56368693Scy	/** Tree of EDNS client strings to use in upstream queries, per address
57368693Scy	 * prefix. Contains nodes of type edns_string_addr. */
58368693Scy	rbtree_type client_strings;
59368693Scy	/** EDNS opcode to use for client strings */
60368693Scy	uint16_t client_string_opcode;
61368129Scy	/** region to allocate tree nodes in */
62368129Scy	struct regional* region;
63368129Scy};
64368129Scy
65368129Scy/**
66368693Scy * EDNS string. Node of rbtree, containing string and prefix.
67368129Scy */
68368693Scystruct edns_string_addr {
69368129Scy	/** node in address tree, used for tree lookups. Need to be the first
70368129Scy	 * member of this struct. */
71368129Scy	struct addr_tree_node node;
72368693Scy	/** string, ascii format */
73368693Scy	uint8_t* string;
74368693Scy	/** length of string */
75368693Scy	size_t string_len;
76368129Scy};
77368129Scy
78368129Scy/**
79368693Scy * Create structure to hold EDNS strings
80368693Scy * @return: newly created edns_strings, NULL on alloc failure.
81368129Scy */
82368693Scystruct edns_strings* edns_strings_create(void);
83368129Scy
84368693Scy/** Delete EDNS strings structure
85368693Scy * @param edns_strings: struct to delete
86368129Scy */
87368693Scyvoid edns_strings_delete(struct edns_strings* edns_strings);
88368129Scy
89368129Scy/**
90368693Scy * Add configured EDNS strings
91368693Scy * @param edns_strings: edns strings to apply config to
92368693Scy * @param config: struct containing EDNS strings configuration
93368129Scy * @return 0 on error
94368129Scy */
95368693Scyint edns_strings_apply_cfg(struct edns_strings* edns_strings,
96368129Scy	struct config_file* config);
97368129Scy
98368129Scy/**
99368693Scy * Find string for address.
100368693Scy * @param tree: tree containing EDNS strings per address prefix.
101368129Scy * @param addr: address to use for tree lookup
102368129Scy * @param addrlen: length of address
103368129Scy * @return: matching tree node, NULL otherwise
104368129Scy */
105368693Scystruct edns_string_addr*
106368693Scyedns_string_addr_lookup(rbtree_type* tree, struct sockaddr_storage* addr,
107368129Scy	socklen_t addrlen);
108368129Scy
109368129Scy/**
110338566Sdes * Apply common EDNS options.
111338566Sdes *
112338566Sdes * @param edns_out: initialised edns information with outbound edns.
113338566Sdes * @param edns_in: initialised edns information with received edns.
114338566Sdes * @param cfg: configuration.
115338566Sdes * @param c: comm channel.
116338566Sdes * @param region: the region to allocate the edns options in.
117338566Sdes */
118338566Sdesint apply_edns_options(struct edns_data* edns_out, struct edns_data* edns_in,
119338566Sdes	struct config_file* cfg, struct comm_point* c, struct regional* region);
120338566Sdes
121338566Sdes#endif
122