-
Notifications
You must be signed in to change notification settings - Fork 3
/
settings.cpp
196 lines (160 loc) · 7.67 KB
/
settings.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Module that is concerned with settings.
* All setting symbols should be defined
* in here with sensible defaults.
*/
#include "simple_config.h"
#include <stdio.h>
#include <string.h>
#include <vector>
// global data
const char* transdns_version = VERSION;
int serve_version_records = 1;
const char* db_host = "localhost";
const char* db_database = "transdns";
const char* db_username = "transdns";
const char* db_password = "";
// current defaults for these settings are all compatible
// with the old, non settings aware version of transdns
int update_interval = 60;
int support_notifies = 0;
int support_axfr = 1;
int support_compression = 0;
int compress_all_packages = 0;
int support_edns = 0;
int support_dnssec = 0;
int dnssec_nsec3 = 0;
int max_udp_payload_size = 1200;
// verbosity
int verbosity_print_updates = 0;
int send_default_replies_when_no_match = 1;
int use_translog = 0;
int translog_max_queries = 1000000;
const char* translog_filename = "/usr/translog";
int notify_handle_interval = 20;
int dnssec_nsec3_bind9_wildcard_compatibility = 1;
int dnssec_nsec3_noerror_for_empty_non_terminals = 1;
int dnssec_nsec3_hash_cache_size = 20000;
int udp_thread_count = 4;
int tcp_thread_count = 1;
const char* port_to_bind = "53";
// ip settings
std::vector<char*> ips_to_bind_to;
// flags
int settings_are_from_config_file = 0;
static void print_support_flag(const char* flag_name, int value)
{
printf("%s: %s\n", flag_name, value ? "enabled" : "disabled");
}
/**
* Handles the commandline arguments passed to TransDNS
* by either reading a config file or interpreting the
* arguments as ips to bind to.
*
* @param int argc the argument count as passed to main()
* @param char** argv a list of arguments as passed to main()
* @return int the number of ips to bind to
*/
int handle_commandline(int argc, char** argv)
{
if (argc >= 2) {
config_file_t* config = config_file_open(argv[1]);
if (NULL == config) {
settings_are_from_config_file = 0;
printf("%s does not seem to be a config file, skipping...\n", argv[1]);
for (int i = 1; i < argc; ++i) {
ips_to_bind_to.push_back(strdup(argv[i]));
}
} else {
settings_are_from_config_file = 1;
printf("successfully read config file %s\n", argv[1]);
serve_version_records = config_file_get_bool(config, "serve-version-records", serve_version_records, 0);
db_host = strdup(config_file_get_string(config, "db-host", db_host, 0));
db_username = strdup(config_file_get_string(config, "db-user", db_username, 0));
db_password = strdup(config_file_get_string(config, "db-password", db_password, 0));
db_database = strdup(config_file_get_string(config, "db-database", db_database, 0));
int temp = config_file_get_int(config, "update-interval", update_interval, 0);
if (temp > 0)
update_interval = temp;
int temp_notify_interval = config_file_get_int(config, "notify-handle-interval", notify_handle_interval, 0);
if (temp_notify_interval >= 0)
notify_handle_interval = temp_notify_interval;
support_notifies = config_file_get_bool(config, "support-notifies", support_notifies, 0);
support_axfr = config_file_get_bool(config, "support-axfr", support_axfr, 0);
support_compression = config_file_get_bool(config, "support-compression", support_compression, 0);
compress_all_packages = config_file_get_bool(config, "compress-all-packages", compress_all_packages, 0);
support_edns = config_file_get_bool(config, "support-edns", support_edns, 0);
support_dnssec = config_file_get_bool(config, "support-dnssec", support_dnssec, 0);
dnssec_nsec3 = config_file_get_bool(config, "dnssec-nsec3", dnssec_nsec3, 0);
dnssec_nsec3_bind9_wildcard_compatibility = config_file_get_bool(config, "dnssec-nsec3-bind9-wildcard-compatibility",
dnssec_nsec3_bind9_wildcard_compatibility, 0);
dnssec_nsec3_noerror_for_empty_non_terminals = config_file_get_bool(config, "dnssec-nsec3-empty-non-terminals-return-no-data",
dnssec_nsec3_noerror_for_empty_non_terminals, 0);
dnssec_nsec3_hash_cache_size = config_file_get_int(config, "dnssec-nsec3-hash-cache-size", dnssec_nsec3_hash_cache_size, 0);
verbosity_print_updates = config_file_get_bool(config, "verbosity-print-updates", verbosity_print_updates, 0);
use_translog = config_file_get_bool(config, "log-queries", use_translog, 0);
translog_max_queries = config_file_get_int(config, "log-queries-max", translog_max_queries, 0);
translog_filename = strdup(config_file_get_string(config, "log-queries-filename", translog_filename, 0));
max_udp_payload_size = config_file_get_int(config, "max-udp-payload-size", max_udp_payload_size, 0);
if (max_udp_payload_size <= 0)
max_udp_payload_size = 512;
udp_thread_count = config_file_get_int(config, "udp-thread-count", udp_thread_count, 0);
if (udp_thread_count <= 0)
udp_thread_count = 4;
tcp_thread_count = config_file_get_int(config, "tcp-thread-count", tcp_thread_count, 0);
if (tcp_thread_count <= 0)
tcp_thread_count = 1;
port_to_bind = strdup(config_file_get_string(config, "port-to-bind", port_to_bind, 0));
if (atoi(port_to_bind) <= 0)
port_to_bind = "53";
int ips_count = config_file_key_count(config, "ip");
for (int i = 0; i < ips_count; ++i) {
const char* ip = config_file_get_string(config, "ip", NULL, i);
if (ip != NULL) {
ips_to_bind_to.push_back(strdup(ip));
}
}
config_file_close(config);
}
}
print_support_flag("notifies", support_notifies);
print_support_flag("axfr", support_axfr);
print_support_flag("edns", support_edns);
print_support_flag("dnssec", support_dnssec);
print_support_flag("nnsec3", dnssec_nsec3);
print_support_flag("nsec3-bind9-wildcard-compatibility", dnssec_nsec3_bind9_wildcard_compatibility);
print_support_flag("nsec3-empty-non-terminals-return-no-data", dnssec_nsec3_noerror_for_empty_non_terminals);
print_support_flag("compression", support_compression);
print_support_flag("include-version-records", compress_all_packages);
print_support_flag("include-version-records", serve_version_records);
print_support_flag("query logging", use_translog);
print_support_flag("verbosity-print-updates:", verbosity_print_updates);
printf("udp-thread-count: %d\n", udp_thread_count);
printf("tcp-thread-count: %d\n", tcp_thread_count);
printf("port-to-bind: %s\n", port_to_bind);
return ips_to_bind_to.size();
}
/**
* Gets an IP to bind to
*
* @param unsigned int the index of the ip to retrieve
* @return char* the requested ip to bind to from the list,if available.
* if index is out of bounds, NULL is returned.
* the caller does not get ownership of the returned pointer.
*/
char* get_ip_to_bind_to(unsigned int index)
{
return index < ips_to_bind_to.size() ? ips_to_bind_to[index] : NULL;
}
/**
* Returns whether the settings has been read from a config file,
* instead of interpreting the commandline as ips to bind to.
*
* @return int 0 if the commandline was interpreted as ips t bind to,
* a non-zero value if the settings have been read from a
* config file.
*/
int settings_have_been_read_from_config_file()
{
return settings_are_from_config_file;
}