9 #include <sys/socket.h>
12 #include <unordered_map>
24 #define DEFAULT_BACKLOG_SIZE 10485760
28 vector<string> arguments;
29 unordered_map<string, string> parameters;
34 bool parse_hostport(const string &hostport, sockaddr_in6 *addr)
36 memset(addr, 0, sizeof(*addr));
37 addr->sin6_family = AF_INET6;
41 // See if the argument if on the type [ipv6addr]:port.
42 if (!hostport.empty() && hostport[0] == '[') {
43 size_t split = hostport.find("]:");
44 if (split == string::npos) {
45 log(ERROR, "address '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
49 string host(hostport.begin() + 1, hostport.begin() + split);
50 port_string = hostport.substr(split + 2);
52 if (inet_pton(AF_INET6, host.c_str(), &addr->sin6_addr) != 1) {
53 log(ERROR, "'%s' is not a valid IPv6 address");
57 // OK, then it must be ipv4addr:port.
58 size_t split = hostport.find(":");
59 if (split == string::npos) {
60 log(ERROR, "address '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
64 string host(hostport.begin(), hostport.begin() + split);
65 port_string = hostport.substr(split + 1);
67 // Parse to an IPv4 address, then construct a mapped-v4 address from that.
70 if (inet_pton(AF_INET, host.c_str(), &addr4) != 1) {
71 log(ERROR, "'%s' is not a valid IPv4 address");
75 addr->sin6_addr.s6_addr32[2] = htonl(0xffff);
76 addr->sin6_addr.s6_addr32[3] = addr4.s_addr;
79 int port = atoi(port_string.c_str());
80 if (port < 1 || port >= 65536) {
81 log(ERROR, "port %d is out of range (must be [1,65536>).", port);
84 addr->sin6_port = ntohs(port);
89 bool read_config(const string &filename, vector<ConfigLine> *lines)
91 FILE *fp = fopen(filename.c_str(), "r");
93 log_perror(filename.c_str());
99 if (fgets(buf, sizeof(buf), fp) == nullptr) {
103 // Chop off the string at the first #, \r or \n.
104 buf[strcspn(buf, "#\r\n")] = 0;
106 // Remove all whitespace from the end of the string.
107 size_t len = strlen(buf);
108 while (len > 0 && isspace(buf[len - 1])) {
112 // If the line is now all blank, ignore it.
117 vector<string> tokens = split_tokens(buf);
118 assert(!tokens.empty());
121 line.keyword = tokens[0];
123 for (size_t i = 1; i < tokens.size(); ++i) {
124 // foo=bar is a parameter; anything else is an argument.
125 size_t equals_pos = tokens[i].find_first_of('=');
126 if (equals_pos == string::npos) {
127 line.arguments.push_back(tokens[i]);
129 string key = tokens[i].substr(0, equals_pos);
130 string value = tokens[i].substr(equals_pos + 1, string::npos);
131 line.parameters.insert(make_pair(key, value));
135 lines->push_back(line);
138 if (fclose(fp) == EOF) {
139 log_perror(filename.c_str());
145 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
147 for (const ConfigLine &line : config) {
148 if (line.keyword != keyword) {
151 if (line.parameters.size() > 0 ||
152 line.arguments.size() != 1) {
153 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
156 *value = line.arguments[0];
162 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
164 for (const ConfigLine &line : config) {
165 if (line.keyword != keyword) {
168 if (line.parameters.size() > 0 ||
169 line.arguments.size() != 1) {
170 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
173 *value = atoi(line.arguments[0].c_str()); // TODO: verify int validity.
179 bool load_file_to_string(const string &filename, size_t max_size, string *contents)
183 FILE *fp = fopen(filename.c_str(), "r");
185 log_perror(filename.c_str());
191 size_t ret = fread(buf, 1, sizeof(buf), fp);
193 contents->append(buf, buf + ret);
196 log_perror(filename.c_str());
204 if (contents->size() > max_size) {
205 log(ERROR, "%s was longer than the maximum allowed %zu bytes", filename.c_str(), max_size);
214 bool parse_tls_parameters(const unordered_map<string, string> ¶meters, AcceptorConfig *acceptor)
216 bool has_cert = false, has_key = false;
218 auto tls_cert_it = parameters.find("tls_cert");
219 if (tls_cert_it != parameters.end()) {
220 if (!load_file_to_string(tls_cert_it->second, 1048576, &acceptor->certificate_chain)) {
224 // Verify that the certificate is valid.
225 bool is_server = true;
226 TLSContext *server_context = tls_create_context(is_server, TLS_V12);
227 int num_cert = tls_load_certificates(
229 reinterpret_cast<const unsigned char *>(acceptor->certificate_chain.data()),
230 acceptor->certificate_chain.size());
232 log_tls_error(tls_cert_it->second.c_str(), num_cert);
233 tls_destroy_context(server_context);
235 } else if (num_cert == 0) {
236 log(ERROR, "%s did not contain any certificates", tls_cert_it->second.c_str());
239 tls_destroy_context(server_context);
243 auto tls_key_it = parameters.find("tls_key");
244 if (tls_key_it != parameters.end()) {
245 if (!load_file_to_string(tls_key_it->second, 1048576, &acceptor->private_key)) {
249 // Verify that the key is valid.
250 bool is_server = true;
251 TLSContext *server_context = tls_create_context(is_server, TLS_V12);
252 int num_keys = tls_load_private_key(
254 reinterpret_cast<const unsigned char *>(acceptor->private_key.data()),
255 acceptor->private_key.size());
257 log_tls_error(tls_key_it->second.c_str(), num_keys);
258 tls_destroy_context(server_context);
260 } else if (num_keys == 0) {
261 log(ERROR, "%s did not contain any private keys", tls_key_it->second.c_str());
264 tls_destroy_context(server_context);
268 if (has_cert != has_key) {
269 log(ERROR, "Only one of tls_cert= and tls_key= was given, needs zero or both");
277 bool parse_port(const ConfigLine &line, Config *config)
279 if (line.arguments.size() != 1) {
280 log(ERROR, "'port' takes exactly one argument");
284 int port = atoi(line.arguments[0].c_str());
285 if (port < 1 || port >= 65536) {
286 log(ERROR, "port %d is out of range (must be [1,65536>).", port);
290 AcceptorConfig acceptor;
291 acceptor.addr = create_any_address(port);
293 if (!parse_tls_parameters(line.parameters, &acceptor)) {
296 config->acceptors.push_back(acceptor);
300 bool parse_listen(const ConfigLine &line, Config *config)
302 if (line.arguments.size() != 1) {
303 log(ERROR, "'listen' takes exactly one argument");
307 AcceptorConfig acceptor;
308 if (!parse_hostport(line.arguments[0], &acceptor.addr)) {
311 if (!parse_tls_parameters(line.parameters, &acceptor)) {
314 config->acceptors.push_back(acceptor);
318 bool parse_stream(const ConfigLine &line, Config *config)
320 if (line.arguments.size() != 1) {
321 log(ERROR, "'stream' takes exactly one argument");
326 stream.url = line.arguments[0];
328 const auto src_it = line.parameters.find("src");
329 if (src_it == line.parameters.end()) {
330 log(WARNING, "stream '%s' has no src= attribute, clients will not get any data.",
333 stream.src = src_it->second;
334 // TODO: Verify that the URL is parseable?
337 const auto backlog_it = line.parameters.find("backlog_size");
338 if (backlog_it == line.parameters.end()) {
339 stream.backlog_size = DEFAULT_BACKLOG_SIZE;
341 stream.backlog_size = atoi(backlog_it->second.c_str());
344 const auto prebuffer_it = line.parameters.find("force_prebuffer");
345 if (prebuffer_it == line.parameters.end()) {
346 stream.prebuffering_bytes = 0;
348 stream.prebuffering_bytes = atoi(prebuffer_it->second.c_str());
351 // Parse output encoding.
352 const auto encoding_parm_it = line.parameters.find("encoding");
353 if (encoding_parm_it == line.parameters.end() ||
354 encoding_parm_it->second == "raw") {
355 stream.encoding = StreamConfig::STREAM_ENCODING_RAW;
356 } else if (encoding_parm_it->second == "metacube") {
357 stream.encoding = StreamConfig::STREAM_ENCODING_METACUBE;
359 log(ERROR, "Parameter 'encoding' must be either 'raw' (default) or 'metacube'");
363 // Parse input encoding.
364 const auto src_encoding_parm_it = line.parameters.find("src_encoding");
365 if (src_encoding_parm_it == line.parameters.end() ||
366 src_encoding_parm_it->second == "metacube") {
367 stream.src_encoding = StreamConfig::STREAM_ENCODING_METACUBE;
368 } else if (src_encoding_parm_it->second == "raw") {
369 stream.src_encoding = StreamConfig::STREAM_ENCODING_RAW;
371 log(ERROR, "Parameter 'src_encoding' must be either 'raw' or 'metacube' (default)");
375 // Parse the pacing rate, converting from kilobits to bytes as needed.
376 const auto pacing_rate_it = line.parameters.find("pacing_rate_kbit");
377 if (pacing_rate_it == line.parameters.end()) {
378 stream.pacing_rate = ~0U;
380 stream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
383 // Parse the HLS URL, if any.
384 const auto hls_url_it = line.parameters.find("hls_playlist");
385 if (hls_url_it != line.parameters.end()) {
386 stream.hls_url = hls_url_it->second;
387 if (stream.hls_url.empty()) {
388 log(ERROR, "Parameter 'hls_playlist' was given but empty");
391 if (stream.encoding == StreamConfig::STREAM_ENCODING_METACUBE) {
392 log(ERROR, "HLS cannot be used with Metacube output");
397 // Parse the HLS fragment duration, if any.
398 const auto hls_frag_duration_it = line.parameters.find("hls_frag_duration");
399 if (hls_frag_duration_it != line.parameters.end()) {
400 if (stream.hls_url.empty()) {
401 log(ERROR, "Parameter 'hls_frag_duration' given, but no 'hls_playlist' given");
404 stream.hls_frag_duration = stoi(hls_frag_duration_it->second);
405 if (stream.hls_frag_duration <= 0) {
406 log(ERROR, "'hls_frag_duration' must be a strictly positive integer");
411 // Parse the HLS backlog margin, if any.
412 const auto hls_backlog_margin_it = line.parameters.find("hls_backlog_margin");
413 if (hls_backlog_margin_it != line.parameters.end()) {
414 if (stream.hls_url.empty()) {
415 log(ERROR, "Parameter 'hls_backlog_margin' given, but no 'hls_playlist' given");
418 stream.hls_backlog_margin = stoi(hls_backlog_margin_it->second);
419 if (stream.hls_backlog_margin < 0 || stream.hls_backlog_margin >= stream.backlog_size) {
420 log(ERROR, "'hls_backlog_margin' must be nonnegative, but less than the backlog size");
425 // Parse the CORS origin, if it exists.
426 const auto allow_origin_it = line.parameters.find("allow_origin");
427 if (allow_origin_it != line.parameters.end()) {
428 stream.allow_origin = allow_origin_it->second;
431 config->streams.push_back(stream);
435 bool parse_udpstream(const ConfigLine &line, Config *config)
437 if (line.arguments.size() != 1) {
438 log(ERROR, "'udpstream' takes exactly one argument");
442 UDPStreamConfig udpstream;
444 string hostport = line.arguments[0];
445 if (!parse_hostport(hostport, &udpstream.dst)) {
449 const auto src_it = line.parameters.find("src");
450 if (src_it == line.parameters.end()) {
451 // This is pretty meaningless, but OK, consistency is good.
452 log(WARNING, "udpstream to %s has no src= attribute, clients will not get any data.",
455 udpstream.src = src_it->second;
456 // TODO: Verify that the URL is parseable?
459 // Parse the pacing rate, converting from kilobits to bytes as needed.
460 const auto pacing_rate_it = line.parameters.find("pacing_rate_kbit");
461 if (pacing_rate_it == line.parameters.end()) {
462 udpstream.pacing_rate = ~0U;
464 udpstream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
467 // Parse the TTL. The same value is used for unicast and multicast.
468 const auto ttl_it = line.parameters.find("ttl");
469 if (ttl_it == line.parameters.end()) {
472 udpstream.ttl = atoi(ttl_it->second.c_str());
475 // Parse the multicast interface index.
476 const auto multicast_iface_it = line.parameters.find("multicast_output_interface");
477 if (multicast_iface_it == line.parameters.end()) {
478 udpstream.multicast_iface_index = -1;
480 udpstream.multicast_iface_index = if_nametoindex(multicast_iface_it->second.c_str());
481 if (udpstream.multicast_iface_index == 0) {
482 log(ERROR, "Interface '%s' does not exist", multicast_iface_it->second.c_str());
487 config->udpstreams.push_back(udpstream);
491 bool parse_gen204(const ConfigLine &line, Config *config)
493 if (line.arguments.size() != 1) {
494 log(ERROR, "'gen204' takes exactly one argument");
499 gen204.url = line.arguments[0];
501 // Parse the CORS origin, if it exists.
502 const auto allow_origin_it = line.parameters.find("allow_origin");
503 if (allow_origin_it != line.parameters.end()) {
504 gen204.allow_origin = allow_origin_it->second;
507 config->pings.push_back(gen204);
511 bool parse_error_log(const ConfigLine &line, Config *config)
513 if (line.arguments.size() != 0) {
514 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
518 LogConfig log_config;
519 const auto type_it = line.parameters.find("type");
520 if (type_it == line.parameters.end()) {
521 log(ERROR, "'error_log' has no type= parameter");
525 string type = type_it->second;
526 if (type == "file") {
527 log_config.type = LogConfig::LOG_TYPE_FILE;
528 } else if (type == "syslog") {
529 log_config.type = LogConfig::LOG_TYPE_SYSLOG;
530 } else if (type == "console") {
531 log_config.type = LogConfig::LOG_TYPE_CONSOLE;
533 log(ERROR, "Unknown log type '%s'", type.c_str());
537 if (log_config.type == LogConfig::LOG_TYPE_FILE) {
538 const auto filename_it = line.parameters.find("filename");
539 if (filename_it == line.parameters.end()) {
540 log(ERROR, "error_log type 'file' with no filename= parameter");
543 log_config.filename = filename_it->second;
546 config->log_destinations.push_back(log_config);
552 bool parse_config(const string &filename, Config *config)
554 vector<ConfigLine> lines;
555 if (!read_config(filename, &lines)) {
559 config->daemonize = false;
561 if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
562 log(ERROR, "Missing 'num_servers' statement in config file.");
565 if (config->num_servers < 1 || config->num_servers >= 20000) { // Insanely high max limit.
566 log(ERROR, "'num_servers' is %d, needs to be in [1, 20000>.", config->num_servers);
570 // See if the user wants stats.
571 config->stats_interval = 60;
572 bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
573 bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
574 if (has_stats_interval && !has_stats_file) {
575 log(WARNING, "'stats_interval' given, but no 'stats_file'. No client statistics will be written.");
578 config->input_stats_interval = 60;
579 bool has_input_stats_file = fetch_config_string(lines, "input_stats_file", &config->input_stats_file);
580 bool has_input_stats_interval = fetch_config_int(lines, "input_stats_interval", &config->input_stats_interval);
581 if (has_input_stats_interval && !has_input_stats_file) {
582 log(WARNING, "'input_stats_interval' given, but no 'input_stats_file'. No input statistics will be written.");
585 fetch_config_string(lines, "access_log", &config->access_log_file);
587 for (const ConfigLine &line : lines) {
588 if (line.keyword == "num_servers" ||
589 line.keyword == "stats_file" ||
590 line.keyword == "stats_interval" ||
591 line.keyword == "input_stats_file" ||
592 line.keyword == "input_stats_interval" ||
593 line.keyword == "access_log") {
594 // Already taken care of, above.
595 } else if (line.keyword == "port") {
596 if (!parse_port(line, config)) {
599 } else if (line.keyword == "listen") {
600 if (!parse_listen(line, config)) {
603 } else if (line.keyword == "stream") {
604 if (!parse_stream(line, config)) {
607 } else if (line.keyword == "udpstream") {
608 if (!parse_udpstream(line, config)) {
611 } else if (line.keyword == "gen204") {
612 if (!parse_gen204(line, config)) {
615 } else if (line.keyword == "error_log") {
616 if (!parse_error_log(line, config)) {
619 } else if (line.keyword == "daemonize") {
620 config->daemonize = true;
622 log(ERROR, "Unknown configuration keyword '%s'.",
623 line.keyword.c_str());