/
dns_options.cc
320 lines (284 loc) · 13.6 KB
/
dns_options.cc
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <boost/asio/ip/host_name.hpp>
#include <fstream>
#include <iostream>
#include "base/contrail_ports.h"
#include "base/logging.h"
#include "base/misc_utils.h"
#include "base/util.h"
#include "cmn/buildinfo.h"
#include "cmn/dns_options.h"
#include "net/address_util.h"
using namespace std;
using namespace boost::asio::ip;
namespace opt = boost::program_options;
// Process command line options for dns.
Options::Options() {
}
bool Options::Parse(EventManager &evm, int argc, char *argv[]) {
opt::options_description cmdline_options("Allowed options");
Initialize(evm, cmdline_options);
Process(argc, argv, cmdline_options);
return true;
}
// Initialize dns's command line option tags with appropriate default
// values. Options can from a config file as well. By default, we read
// options from /etc/contrail/contrail-dns.conf
void Options::Initialize(EventManager &evm,
opt::options_description &cmdline_options) {
boost::system::error_code error;
string hostname = host_name(error);
string host_ip = GetHostIp(evm.io_service(), hostname);
if (host_ip.empty()) {
cout << "Error! Cannot resolve host " << hostname <<
" to a valid IP address";
exit(-1);
}
opt::options_description generic("Generic options");
// Command line only options.
generic.add_options()
("conf_file", opt::value<string>()->default_value(
"/etc/contrail/contrail-dns.conf"),
"Configuration file")
("help", "help message")
("version", "Display version information")
;
uint16_t default_dns_server_port = ContrailPorts::DnsServerPort();
uint16_t default_http_server_port = ContrailPorts::HttpPortDns();
uint16_t default_discovery_port = ContrailPorts::DiscoveryServerPort();
default_collector_server_list_.push_back("127.0.0.1:8086");
// Command line and config file options.
opt::options_description config("Configuration options");
config.add_options()
("DEFAULT.collectors",
opt::value<vector<string> >()->default_value(
default_collector_server_list_, "127.0.0.1:8086"),
"Collector server list")
("DEFAULT.dns_config_file",
opt::value<string>()->default_value("dns_config.xml"),
"DNS Configuration file")
("DEFAULT.named_config_file",
opt::value<string>()->default_value("contrail-named.conf"),
"Named Configuration file")
("DEFAULT.named_config_directory",
opt::value<string>()->default_value("/etc/contrail/dns"),
"Named Configuration directory")
("DEFAULT.named_log_file",
opt::value<string>()->default_value("/var/log/contrail/contrail-named.log"),
"Named log file")
("DEFAULT.rndc_config_file",
opt::value<string>()->default_value("contrail-rndc.conf"),
"Rndc Configuration file")
("DEFAULT.rndc_secret",
opt::value<string>()->default_value("xvysmOR8lnUQRBcunkC6vg=="),
"RNDC secret")
("DEFAULT.named_max_cache_size",
opt::value<string>()->default_value("32M"),
"Maximum cache size, in bytes, used by contrail-named (per view)")
("DEFAULT.named_max_retransmissions",
opt::value<uint16_t>()->default_value(12),
"Maximum number of retries to named")
("DEFAULT.named_retransmission_interval",
opt::value<uint16_t>()->default_value(1000),
"Retranmission interval in msec")
("DEFAULT.hostip", opt::value<string>()->default_value(host_ip),
"IP address of DNS Server")
("DEFAULT.hostname", opt::value<string>()->default_value(hostname),
"Hostname of DNS Server")
("DEFAULT.http_server_port",
opt::value<uint16_t>()->default_value(default_http_server_port),
"Sandesh HTTP listener port")
("DEFAULT.dns_server_port",
opt::value<uint16_t>()->default_value(default_dns_server_port),
"DNS server port")
("DEFAULT.log_category",
opt::value<string>()->default_value(log_category_),
"Category filter for local logging of sandesh messages")
("DEFAULT.log_disable", opt::bool_switch(&log_disable_),
"Disable sandesh logging")
("DEFAULT.log_property_file", opt::value<string>()->default_value(""),
"log4cplus property file name")
("DEFAULT.log_file", opt::value<string>()->default_value("<stdout>"),
"Filename for the logs to be written to")
("DEFAULT.log_files_count",
opt::value<int>()->default_value(10),
"Maximum log file roll over index")
("DEFAULT.log_file_size",
opt::value<long>()->default_value(1024*1024),
"Maximum size of the log file")
("DEFAULT.log_level", opt::value<string>()->default_value("SYS_NOTICE"),
"Severity level for local logging of sandesh messages")
("DEFAULT.log_local", opt::bool_switch(&log_local_),
"Enable local logging of sandesh messages")
("DEFAULT.use_syslog", opt::bool_switch(&use_syslog_),
"Enable logging to syslog")
("DEFAULT.syslog_facility", opt::value<string>()->default_value("LOG_LOCAL0"),
"Syslog facility to receive log lines")
("DEFAULT.test_mode", opt::bool_switch(&test_mode_),
"Enable dns to run in test-mode")
("DEFAULT.sandesh_send_rate_limit",
opt::value<uint32_t>()->default_value(
Sandesh::get_send_rate_limit()),
"Sandesh send rate limit in messages/sec")
("DISCOVERY.port", opt::value<uint16_t>()->default_value(
default_discovery_port),
"Port of Discovery Server")
("DISCOVERY.server", opt::value<string>(),
"IP address of Discovery Server")
("IFMAP.certs_store", opt::value<string>(),
"Certificates store to use for communication with IFMAP server")
("IFMAP.password",
opt::value<string>()->default_value("dns_user_passwd"),
"IFMAP server password")
("IFMAP.server_url", opt::value<string>()->default_value(
ifmap_config_options_.server_url), "IFMAP server URL")
("IFMAP.user", opt::value<string>()->default_value("dns_user"),
"IFMAP server username")
("IFMAP.stale_entries_cleanup_timeout",
opt::value<int>()->default_value(300),
"IFMAP stale entries cleanup timeout")
("IFMAP.end_of_rib_timeout", opt::value<int>()->default_value(10),
"IFMAP end of rib timeout")
("IFMAP.peer_response_wait_time", opt::value<int>()->default_value(60),
"IFMAP peer response wait time")
("DEFAULT.xmpp_dns_auth_enable", opt::bool_switch(&xmpp_auth_enable_),
"Enable authentication over Xmpp")
("DEFAULT.xmpp_server_cert",
opt::value<string>()->default_value(
"/etc/contrail/ssl/certs/server.pem"),
"XMPP Server ssl certificate")
("DEFAULT.xmpp_server_key",
opt::value<string>()->default_value(
"/etc/contrail/ssl/private/server-privkey.pem"),
"XMPP Server ssl private key")
("DEFAULT.xmpp_ca_cert",
opt::value<string>()->default_value(
"/etc/contrail/ssl/certs/ca-cert.pem"),
"XMPP CA ssl certificate")
;
config_file_options_.add(config);
cmdline_options.add(generic).add(config);
}
template <typename ValueType>
void Options::GetOptValue(const boost::program_options::variables_map &var_map,
ValueType &var, std::string val) {
GetOptValueImpl(var_map, var, val, static_cast<ValueType *>(0));
}
template <typename ValueType>
void Options::GetOptValueImpl(
const boost::program_options::variables_map &var_map,
ValueType &var, std::string val, ValueType*) {
// Check if the value is present.
if (var_map.count(val)) {
var = var_map[val].as<ValueType>();
}
}
template <typename ElementType>
void Options::GetOptValueImpl(
const boost::program_options::variables_map &var_map,
std::vector<ElementType> &var, std::string val, std::vector<ElementType>*) {
// Check if the value is present.
if (var_map.count(val)) {
std::vector<ElementType> tmp(
var_map[val].as<std::vector<ElementType> >());
// Now split the individual elements
for (typename std::vector<ElementType>::const_iterator it =
tmp.begin();
it != tmp.end(); it++) {
std::stringstream ss(*it);
std::copy(istream_iterator<ElementType>(ss),
istream_iterator<ElementType>(),
std::back_inserter(var));
}
}
}
// Process command line options. They can come from a conf file as well. Options
// from command line always overrides those that come from the config file.
void Options::Process(int argc, char *argv[],
opt::options_description &cmdline_options) {
// Process options off command line first.
opt::variables_map var_map;
opt::store(opt::parse_command_line(argc, argv, cmdline_options), var_map);
// Process options off configuration file.
GetOptValue<string>(var_map, config_file_, "conf_file");
ifstream config_file_in;
config_file_in.open(config_file_.c_str());
if (config_file_in.good()) {
opt::store(opt::parse_config_file(config_file_in, config_file_options_),
var_map);
}
config_file_in.close();
opt::notify(var_map);
if (var_map.count("help")) {
cout << cmdline_options << endl;
exit(0);
}
if (var_map.count("version")) {
string build_info;
cout << MiscUtils::GetBuildInfo(MiscUtils::Dns, BuildInfo,
build_info) << endl;
exit(0);
}
// Retrieve the options.
GetOptValue<string>(var_map, dns_config_file_, "DEFAULT.dns_config_file");
GetOptValue< vector<string> >(var_map, collector_server_list_,
"DEFAULT.collectors");
collectors_configured_ = true;
if (collector_server_list_.size() == 1 &&
!collector_server_list_[0].compare(default_collector_server_list_[0])) {
collectors_configured_ = false;
}
GetOptValue<string>(var_map, named_config_file_,
"DEFAULT.named_config_file");
GetOptValue<string>(var_map, named_config_dir_,
"DEFAULT.named_config_directory");
GetOptValue<string>(var_map, named_log_file_, "DEFAULT.named_log_file");
GetOptValue<string>(var_map, rndc_config_file_, "DEFAULT.rndc_config_file");
GetOptValue<string>(var_map, rndc_secret_, "DEFAULT.rndc_secret");
GetOptValue<string>(var_map, named_max_cache_size_,
"DEFAULT.named_max_cache_size");
GetOptValue<uint16_t>(var_map, named_max_retransmissions_,
"DEFAULT.named_max_retransmissions");
GetOptValue<uint16_t>(var_map, named_retransmission_interval_,
"DEFAULT.named_retransmission_interval");
GetOptValue<string>(var_map, host_ip_, "DEFAULT.hostip");
GetOptValue<string>(var_map, hostname_, "DEFAULT.hostname");
GetOptValue<uint16_t>(var_map, http_server_port_,
"DEFAULT.http_server_port");
GetOptValue<uint16_t>(var_map, dns_server_port_, "DEFAULT.dns_server_port");
GetOptValue<string>(var_map, log_category_, "DEFAULT.log_category");
GetOptValue<string>(var_map, log_file_, "DEFAULT.log_file");
GetOptValue<string>(var_map, log_property_file_,
"DEFAULT.log_property_file");
GetOptValue<int>(var_map, log_files_count_, "DEFAULT.log_files_count");
GetOptValue<long>(var_map, log_file_size_, "DEFAULT.log_file_size");
GetOptValue<string>(var_map, log_level_, "DEFAULT.log_level");
GetOptValue<bool>(var_map, use_syslog_, "DEFAULT.use_syslog");
GetOptValue<string>(var_map, syslog_facility_, "DEFAULT.syslog_facility");
GetOptValue<uint32_t>(var_map, send_ratelimit_,
"DEFAULT.sandesh_send_rate_limit");
GetOptValue<uint16_t>(var_map, discovery_port_, "DISCOVERY.port");
GetOptValue<string>(var_map, discovery_server_, "DISCOVERY.server");
GetOptValue<string>(var_map, ifmap_config_options_.password,
"IFMAP.password");
GetOptValue<string>(var_map, ifmap_config_options_.server_url,
"IFMAP.server_url");
GetOptValue<string>(var_map, ifmap_config_options_.user,
"IFMAP.user");
GetOptValue<string>(var_map, ifmap_config_options_.certs_store,
"IFMAP.certs_store");
GetOptValue<int>(var_map,
ifmap_config_options_.stale_entries_cleanup_timeout,
"IFMAP.stale_entries_cleanup_timeout");
GetOptValue<int>(var_map, ifmap_config_options_.end_of_rib_timeout,
"IFMAP.end_of_rib_timeout");
GetOptValue<int>(var_map,
ifmap_config_options_.peer_response_wait_time,
"IFMAP.peer_response_wait_time");
GetOptValue<bool>(var_map, xmpp_auth_enable_, "DEFAULT.xmpp_dns_auth_enable");
GetOptValue<string>(var_map, xmpp_server_cert_, "DEFAULT.xmpp_server_cert");
GetOptValue<string>(var_map, xmpp_server_key_, "DEFAULT.xmpp_server_key");
GetOptValue<string>(var_map, xmpp_ca_cert_, "DEFAULT.xmpp_ca_cert");
}