8 #include <sys/socket.h>
21 #define DEFAULT_BACKLOG_SIZE 1048576
25 vector<string> arguments;
26 map<string, string> parameters;
29 bool read_config(const string &filename, vector<ConfigLine> *lines)
31 FILE *fp = fopen(filename.c_str(), "r");
33 log_perror(filename.c_str());
39 if (fgets(buf, sizeof(buf), fp) == NULL) {
43 // Chop off the string at the first #, \r or \n.
44 buf[strcspn(buf, "#\r\n")] = 0;
46 // Remove all whitespace from the end of the string.
47 size_t len = strlen(buf);
48 while (len > 0 && isspace(buf[len - 1])) {
52 // If the line is now all blank, ignore it.
57 vector<string> tokens = split_tokens(buf);
58 assert(!tokens.empty());
61 line.keyword = tokens[0];
63 for (size_t i = 1; i < tokens.size(); ++i) {
64 // foo=bar is a parameter; anything else is an argument.
65 size_t equals_pos = tokens[i].find_first_of('=');
66 if (equals_pos == string::npos) {
67 line.arguments.push_back(tokens[i]);
69 string key = tokens[i].substr(0, equals_pos);
70 string value = tokens[i].substr(equals_pos + 1, string::npos);
71 line.parameters.insert(make_pair(key, value));
75 lines->push_back(line);
82 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
84 for (unsigned i = 0; i < config.size(); ++i) {
85 if (config[i].keyword != keyword) {
88 if (config[i].parameters.size() > 0 ||
89 config[i].arguments.size() != 1) {
90 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
93 *value = config[i].arguments[0];
99 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
101 for (unsigned i = 0; i < config.size(); ++i) {
102 if (config[i].keyword != keyword) {
105 if (config[i].parameters.size() > 0 ||
106 config[i].arguments.size() != 1) {
107 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
110 *value = atoi(config[i].arguments[0].c_str()); // TODO: verify int validity.
116 bool parse_port(const ConfigLine &line, Config *config)
118 if (line.arguments.size() != 1) {
119 log(ERROR, "'port' takes exactly one argument");
123 int port = atoi(line.arguments[0].c_str());
124 if (port < 1 || port >= 65536) {
125 log(ERROR, "port %d is out of range (must be [1,65536>).", port);
129 AcceptorConfig acceptor;
130 acceptor.addr = CreateAnyAddress(port);
132 config->acceptors.push_back(acceptor);
136 bool parse_listen(const ConfigLine &line, Config *config)
138 if (line.arguments.size() != 1) {
139 log(ERROR, "'listen' takes exactly one argument");
143 string addr_string = line.arguments[0];
144 if (addr_string.empty()) {
145 // Actually, this should never happen.
146 log(ERROR, "'listen' argument cannot be empty");
152 AcceptorConfig acceptor;
153 memset(&acceptor.addr, 0, sizeof(acceptor.addr));
154 acceptor.addr.sin6_family = AF_INET6;
155 if (addr_string[0] == '[') {
156 // IPv6 address: [addr]:port.
157 size_t addr_end = addr_string.find("]:");
158 if (addr_end == string::npos) {
159 log(ERROR, "IPv6 address '%s' should be on form [address]:port", addr_string.c_str());
163 string addr_only = addr_string.substr(1, addr_end - 1);
164 if (inet_pton(AF_INET6, addr_only.c_str(), &acceptor.addr.sin6_addr) != 1) {
165 log(ERROR, "Invalid IPv6 address '%s'", addr_only.c_str());
169 port_string = addr_string.substr(addr_end + 2);
171 // IPv4 address: addr:port.
172 size_t addr_end = addr_string.find(":");
173 if (addr_end == string::npos) {
174 log(ERROR, "IPv4 address '%s' should be on form address:port", addr_string.c_str());
179 string addr_only = addr_string.substr(0, addr_end);
180 if (inet_pton(AF_INET, addr_only.c_str(), &addr4) != 1) {
181 log(ERROR, "Invalid IPv4 address '%s'", addr_only.c_str());
185 // Convert to a v4-mapped address.
186 acceptor.addr.sin6_addr.s6_addr32[2] = htonl(0xffff);
187 acceptor.addr.sin6_addr.s6_addr32[3] = addr4.s_addr;
188 port_string = addr_string.substr(addr_end + 1);
191 int port = atoi(port_string.c_str());
192 if (port < 1 || port >= 65536) {
193 log(ERROR, "port %d is out of range (must be [1,65536>).", port);
196 acceptor.addr.sin6_port = ntohs(port);
198 config->acceptors.push_back(acceptor);
202 int allocate_mark_pool(int from, int to, Config *config)
206 // Reuse mark pools if an identical one exists.
207 // Otherwise, check if we're overlapping some other mark pool.
208 for (size_t i = 0; i < config->mark_pools.size(); ++i) {
209 const MarkPoolConfig &pool = config->mark_pools[i];
210 if (from == pool.from && to == pool.to) {
212 } else if ((from >= pool.from && from < pool.to) ||
213 (to >= pool.from && to < pool.to)) {
214 log(WARNING, "Mark pool %d-%d partially overlaps with %d-%d, you may get duplicate marks."
215 "Mark pools must either be completely disjunct, or completely overlapping.",
216 from, to, pool.from, pool.to);
220 if (pool_index != -1) {
224 // No match to existing pools.
228 config->mark_pools.push_back(pool);
230 return config->mark_pools.size() - 1;
233 bool parse_mark_pool(const string &mark_str, int *from, int *to)
235 size_t split = mark_str.find_first_of('-');
236 if (split == string::npos) {
237 log(ERROR, "Invalid mark specification '%s' (expected 'X-Y').",
242 string from_str(mark_str.begin(), mark_str.begin() + split);
243 string to_str(mark_str.begin() + split + 1, mark_str.end());
244 *from = atoi(from_str.c_str());
245 *to = atoi(to_str.c_str());
247 if (*from <= 0 || *from >= 65536 || *to <= 0 || *to >= 65536) {
248 log(ERROR, "Mark pool range %d-%d is outside legal range [1,65536>.",
256 bool parse_stream(const ConfigLine &line, Config *config)
258 if (line.arguments.size() != 1) {
259 log(ERROR, "'stream' takes exactly one argument");
264 stream.url = line.arguments[0];
266 map<string, string>::const_iterator src_it = line.parameters.find("src");
267 if (src_it == line.parameters.end()) {
268 log(WARNING, "stream '%s' has no src= attribute, clients will not get any data.",
271 stream.src = src_it->second;
272 // TODO: Verify that the URL is parseable?
275 map<string, string>::const_iterator backlog_it = line.parameters.find("backlog_size");
276 if (backlog_it == line.parameters.end()) {
277 stream.backlog_size = DEFAULT_BACKLOG_SIZE;
279 stream.backlog_size = atoi(backlog_it->second.c_str());
283 map<string, string>::const_iterator encoding_parm_it = line.parameters.find("encoding");
284 if (encoding_parm_it == line.parameters.end() ||
285 encoding_parm_it->second == "raw") {
286 stream.encoding = StreamConfig::STREAM_ENCODING_RAW;
287 } else if (encoding_parm_it->second == "metacube") {
288 stream.encoding = StreamConfig::STREAM_ENCODING_METACUBE;
290 log(ERROR, "Parameter 'encoding' must be either 'raw' (default) or 'metacube'");
294 // Parse marks, if so desired.
295 map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
296 if (mark_parm_it == line.parameters.end()) {
297 stream.mark_pool = -1;
300 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
303 stream.mark_pool = allocate_mark_pool(from, to, config);
306 // Parse the pacing rate, converting from kilobits to bytes as needed.
307 map<string, string>::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit");
308 if (pacing_rate_it == line.parameters.end()) {
309 stream.pacing_rate = ~0U;
311 stream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
314 config->streams.push_back(stream);
318 bool parse_udpstream(const ConfigLine &line, Config *config)
320 if (line.arguments.size() != 1) {
321 log(ERROR, "'udpstream' takes exactly one argument");
325 UDPStreamConfig udpstream;
327 string hostport = line.arguments[0];
329 // See if the argument if on the type [ipv6addr]:port.
330 if (!hostport.empty() && hostport[0] == '[') {
331 size_t split = hostport.find("]:");
332 if (split == string::npos) {
333 log(ERROR, "udpstream destination '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
337 string host(hostport.begin() + 1, hostport.begin() + split);
338 string port = hostport.substr(split + 2);
340 udpstream.dst.sin6_family = AF_INET6;
341 if (inet_pton(AF_INET6, host.c_str(), &udpstream.dst.sin6_addr) != 1) {
342 log(ERROR, "udpstream destination host '%s' is not a valid IPv6 address");
346 udpstream.dst.sin6_port = htons(atoi(port.c_str())); // TODO: Verify validity.
348 // OK, then it must be ipv4addr:port.
349 size_t split = hostport.find(":");
350 if (split == string::npos) {
351 log(ERROR, "udpstream destination '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
355 string host(hostport.begin(), hostport.begin() + split);
356 string port = hostport.substr(split + 1);
358 // Parse to an IPv4 address, then construct a mapped-v4 address from that.
361 if (inet_pton(AF_INET, host.c_str(), &addr4) != 1) {
362 log(ERROR, "udpstream destination host '%s' is not a valid IPv4 address");
366 udpstream.dst.sin6_family = AF_INET6;
367 udpstream.dst.sin6_addr.s6_addr32[0] = 0;
368 udpstream.dst.sin6_addr.s6_addr32[1] = 0;
369 udpstream.dst.sin6_addr.s6_addr32[2] = htonl(0xffff);
370 udpstream.dst.sin6_addr.s6_addr32[3] = addr4.s_addr;
371 udpstream.dst.sin6_port = htons(atoi(port.c_str())); // TODO: Verify validity.
374 map<string, string>::const_iterator src_it = line.parameters.find("src");
375 if (src_it == line.parameters.end()) {
376 // This is pretty meaningless, but OK, consistency is good.
377 log(WARNING, "udpstream to %s has no src= attribute, clients will not get any data.",
380 udpstream.src = src_it->second;
381 // TODO: Verify that the URL is parseable?
384 // Parse marks, if so desired.
385 map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
386 if (mark_parm_it == line.parameters.end()) {
387 udpstream.mark_pool = -1;
390 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
393 udpstream.mark_pool = allocate_mark_pool(from, to, config);
396 // Parse the pacing rate, converting from kilobits to bytes as needed.
397 map<string, string>::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit");
398 if (pacing_rate_it == line.parameters.end()) {
399 udpstream.pacing_rate = ~0U;
401 udpstream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
404 config->udpstreams.push_back(udpstream);
408 bool parse_error_log(const ConfigLine &line, Config *config)
410 if (line.arguments.size() != 0) {
411 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
415 LogConfig log_config;
416 map<string, string>::const_iterator type_it = line.parameters.find("type");
417 if (type_it == line.parameters.end()) {
418 log(ERROR, "'error_log' has no type= parameter");
422 string type = type_it->second;
423 if (type == "file") {
424 log_config.type = LogConfig::LOG_TYPE_FILE;
425 } else if (type == "syslog") {
426 log_config.type = LogConfig::LOG_TYPE_SYSLOG;
427 } else if (type == "console") {
428 log_config.type = LogConfig::LOG_TYPE_CONSOLE;
430 log(ERROR, "Unknown log type '%s'", type.c_str());
434 if (log_config.type == LogConfig::LOG_TYPE_FILE) {
435 map<string, string>::const_iterator filename_it = line.parameters.find("filename");
436 if (filename_it == line.parameters.end()) {
437 log(ERROR, "error_log type 'file' with no filename= parameter");
440 log_config.filename = filename_it->second;
443 config->log_destinations.push_back(log_config);
447 bool parse_config(const string &filename, Config *config)
449 vector<ConfigLine> lines;
450 if (!read_config(filename, &lines)) {
454 config->daemonize = false;
456 if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
457 log(ERROR, "Missing 'num_servers' statement in config file.");
460 if (config->num_servers < 1 || config->num_servers >= 20000) { // Insanely high max limit.
461 log(ERROR, "'num_servers' is %d, needs to be in [1, 20000>.", config->num_servers);
465 // See if the user wants stats.
466 config->stats_interval = 60;
467 bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
468 bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
469 if (has_stats_interval && !has_stats_file) {
470 log(WARNING, "'stats_interval' given, but no 'stats_file'. No client statistics will be written.");
473 config->input_stats_interval = 60;
474 bool has_input_stats_file = fetch_config_string(lines, "input_stats_file", &config->input_stats_file);
475 bool has_input_stats_interval = fetch_config_int(lines, "input_stats_interval", &config->input_stats_interval);
476 if (has_input_stats_interval && !has_input_stats_file) {
477 log(WARNING, "'input_stats_interval' given, but no 'input_stats_file'. No input statistics will be written.");
480 fetch_config_string(lines, "access_log", &config->access_log_file);
482 for (size_t i = 0; i < lines.size(); ++i) {
483 const ConfigLine &line = lines[i];
484 if (line.keyword == "num_servers" ||
485 line.keyword == "stats_file" ||
486 line.keyword == "stats_interval" ||
487 line.keyword == "input_stats_file" ||
488 line.keyword == "input_stats_interval" ||
489 line.keyword == "access_log") {
490 // Already taken care of, above.
491 } else if (line.keyword == "port") {
492 if (!parse_port(line, config)) {
495 } else if (line.keyword == "listen") {
496 if (!parse_listen(line, config)) {
499 } else if (line.keyword == "stream") {
500 if (!parse_stream(line, config)) {
503 } else if (line.keyword == "udpstream") {
504 if (!parse_udpstream(line, config)) {
507 } else if (line.keyword == "error_log") {
508 if (!parse_error_log(line, config)) {
511 } else if (line.keyword == "daemonize") {
512 config->daemonize = true;
514 log(ERROR, "Unknown configuration keyword '%s'.",
515 line.keyword.c_str());