summaryrefslogtreecommitdiffstats
path: root/src/utils/common/params.h
blob: bb071aad861dc62a879cccbcdd4a630d307e37b2 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*  Copyright (C) 2023 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/>.
 */

#pragma once

#include <limits.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

#include "libknot/libknot.h"
#include "contrib/string.h"
#include "contrib/ucw/lists.h"

#define DEFAULT_IPV4_NAME	"127.0.0.1"
#define DEFAULT_IPV6_NAME	"::1"
#define DEFAULT_DNS_PORT	"53"
#define DEFAULT_DNS_HTTPS_PORT	"443"
#define DEFAULT_DNS_QUIC_PORT	"853"
#define DEFAULT_DNS_TLS_PORT	"853"
#define DEFAULT_UDP_SIZE	512
#define DEFAULT_EDNS_SIZE	1232
#define MAX_PACKET_SIZE		65535

#define SEP_CHARS		"\n\t "

/*! \brief Variants of IP protocol. */
typedef enum {
	IP_ALL,
	IP_4,
	IP_6
} ip_t;

/*! \brief Variants of transport protocol. */
typedef enum {
	PROTO_ALL,
	PROTO_TCP,
	PROTO_UDP
} protocol_t;

/*! \brief Variants of output type. */
typedef enum {
	/*!< Verbose output (same for host and dig). */
	FORMAT_FULL,
	/*!< Short dig output. */
	FORMAT_DIG,
	/*!< Brief host output. */
	FORMAT_HOST,
	/*!< Brief nsupdate output. */
	FORMAT_NSUPDATE,
	/*!< Machine readable JSON format (RFC 8427). */
	FORMAT_JSON
} format_t;

/*! \brief Text output settings. */
typedef struct {
	/*!< Output format. */
	format_t	format;

	/*!< Style of rrset dump. */
	knot_dump_style_t	style;

	/*!< Show query packet. */
	bool	show_query;
	/*!< Show header info. */
	bool	show_header;
	/*!< Show section name. */
	bool	show_section;
	/*!< Show EDNS pseudosection. */
	bool	show_edns;
	/*!< Show unknown EDNS options in printable format. */
	bool	show_edns_opt_text;
	/*!< Show QUERY/ZONE section. */
	bool	show_question;
	/*!< Show ANSWER/PREREQ section. */
	bool	show_answer;
	/*!< Show UPDATE/AUTHORITY section. */
	bool	show_authority;
	/*!< Show ADDITIONAL section. */
	bool	show_additional;
	/*!< Show TSIG pseudosection. */
	bool	show_tsig;
	/*!< Show footer info. */
	bool	show_footer;
	/*!< Display EDNS in Presentation format. */
	bool	present_edns;

	/*!< KHOST - Hide CNAME record in answer (duplicity reduction). */
	bool	hide_cname;
} style_t;

/*! \brief Parameter handler. */
typedef int (*param_handle_f)(const char *arg, void *params);

/*! \brief Parameter argument type. */
typedef enum {
	ARG_NONE,
	ARG_REQUIRED,
	ARG_OPTIONAL
} arg_t;

/*! \brief Parameter specification. */
typedef struct {
	const char     *name;
	arg_t          arg;
	param_handle_f handler;
} param_t;

inline static void print_version(const char *prog_name, bool verbose)
{
	if (prog_name != NULL) {
		printf("%s, ", prog_name);
	}
	printf("Knot DNS %s\n", PACKAGE_VERSION);
	if (verbose) {
		printf("\n%s\n", configure_summary);
	}
}

/*!
 * \brief Transforms localized IDN string to ASCII punycode.
 *
 * \param idn_name	IDN name to transform.
 *
 * \retval NULL		if transformation fails.
 * \retval string	if ok.
 */
char *name_from_idn(const char *idn_name);

/*!
 * \brief Transforms ASCII punycode to localized IDN string.
 *
 * If an error occurs or IDN support is missing, this function does nothing.
 *
 * \param name	ASCII name to transform and replace with IDN name.
 */
void name_to_idn(char **name);

/*!
 * \brief Find the best parameter match in table based on prefix equality.
 *
 * \param str		Parameter name to look up.
 * \param str_len	Parameter name length.
 * \param tbl		Parameter table.
 * \param unique	Indication if output is unique result.
 *
 * \retval >=0		looked up parameter position in \a tbl.
 * \retval err		if error.
 */
int best_param(const char *str, const size_t str_len, const param_t *tbl,
               bool *unique);

char *get_reverse_name(const char *name);

char *get_fqd_name(const char *name);

int params_parse_class(const char *value, uint16_t *rclass);

int params_parse_type(const char *value, uint16_t *rtype, int64_t *serial,
                      bool *notify);

int params_parse_server(const char *value, list_t *servers, const char *def_port);

int params_parse_wait(const char *value, int32_t *dst);