8 #include <sys/socket.h>
20 #define DEFAULT_BACKLOG_SIZE 1048576
24 vector<string> arguments;
25 map<string, string> parameters;
28 bool read_config(const string &filename, vector<ConfigLine> *lines)
30 FILE *fp = fopen(filename.c_str(), "r");
32 log_perror(filename.c_str());
38 if (fgets(buf, sizeof(buf), fp) == NULL) {
42 // Chop off the string at the first #, \r or \n.
43 buf[strcspn(buf, "#\r\n")] = 0;
45 // Remove all whitespace from the end of the string.
46 size_t len = strlen(buf);
47 while (len > 0 && isspace(buf[len - 1])) {
51 // If the line is now all blank, ignore it.
56 vector<string> tokens = split_tokens(buf);
57 assert(!tokens.empty());
60 line.keyword = tokens[0];
62 for (size_t i = 1; i < tokens.size(); ++i) {
63 // foo=bar is a parameter; anything else is an argument.
64 size_t equals_pos = tokens[i].find_first_of('=');
65 if (equals_pos == string::npos) {
66 line.arguments.push_back(tokens[i]);
68 string key = tokens[i].substr(0, equals_pos);
69 string value = tokens[i].substr(equals_pos + 1, string::npos);
70 line.parameters.insert(make_pair(key, value));
74 lines->push_back(line);
81 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
83 for (unsigned i = 0; i < config.size(); ++i) {
84 if (config[i].keyword != keyword) {
87 if (config[i].parameters.size() > 0 ||
88 config[i].arguments.size() != 1) {
89 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
92 *value = config[i].arguments[0];
98 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
100 for (unsigned i = 0; i < config.size(); ++i) {
101 if (config[i].keyword != keyword) {
104 if (config[i].parameters.size() > 0 ||
105 config[i].arguments.size() != 1) {
106 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
109 *value = atoi(config[i].arguments[0].c_str()); // TODO: verify int validity.
115 bool parse_port(const ConfigLine &line, Config *config)
117 if (line.arguments.size() != 1) {
118 log(ERROR, "'port' takes exactly one argument");
122 AcceptorConfig acceptor;
123 acceptor.port = atoi(line.arguments[0].c_str());
124 if (acceptor.port < 1 || acceptor.port >= 65536) {
125 log(ERROR, "port %d is out of range (must be [1,65536>).", acceptor.port);
129 config->acceptors.push_back(acceptor);
133 int allocate_mark_pool(int from, int to, Config *config)
137 // Reuse mark pools if an identical one exists.
138 // Otherwise, check if we're overlapping some other mark pool.
139 for (size_t i = 0; i < config->mark_pools.size(); ++i) {
140 const MarkPoolConfig &pool = config->mark_pools[i];
141 if (from == pool.from && to == pool.to) {
143 } else if ((from >= pool.from && from < pool.to) ||
144 (to >= pool.from && to < pool.to)) {
145 log(WARNING, "Mark pool %d-%d partially overlaps with %d-%d, you may get duplicate marks."
146 "Mark pools must either be completely disjunct, or completely overlapping.",
147 from, to, pool.from, pool.to);
151 if (pool_index != -1) {
155 // No match to existing pools.
159 config->mark_pools.push_back(pool);
161 return config->mark_pools.size() - 1;
164 bool parse_mark_pool(const string &mark_str, int *from, int *to)
166 size_t split = mark_str.find_first_of('-');
167 if (split == string::npos) {
168 log(ERROR, "Invalid mark specification '%s' (expected 'X-Y').",
173 string from_str(mark_str.begin(), mark_str.begin() + split);
174 string to_str(mark_str.begin() + split + 1, mark_str.end());
175 *from = atoi(from_str.c_str());
176 *to = atoi(to_str.c_str());
178 if (*from <= 0 || *from >= 65536 || *to <= 0 || *to >= 65536) {
179 log(ERROR, "Mark pool range %d-%d is outside legal range [1,65536>.",
187 bool parse_stream(const ConfigLine &line, Config *config)
189 if (line.arguments.size() != 1) {
190 log(ERROR, "'stream' takes exactly one argument");
195 stream.url = line.arguments[0];
197 map<string, string>::const_iterator src_it = line.parameters.find("src");
198 if (src_it == line.parameters.end()) {
199 log(WARNING, "stream '%s' has no src= attribute, clients will not get any data.",
202 stream.src = src_it->second;
203 // TODO: Verify that the URL is parseable?
206 map<string, string>::const_iterator backlog_it = line.parameters.find("backlog_size");
207 if (backlog_it == line.parameters.end()) {
208 stream.backlog_size = DEFAULT_BACKLOG_SIZE;
210 stream.backlog_size = atoi(backlog_it->second.c_str());
214 map<string, string>::const_iterator encoding_parm_it = line.parameters.find("encoding");
215 if (encoding_parm_it == line.parameters.end() ||
216 encoding_parm_it->second == "raw") {
217 stream.encoding = StreamConfig::STREAM_ENCODING_RAW;
218 } else if (encoding_parm_it->second == "metacube") {
219 stream.encoding = StreamConfig::STREAM_ENCODING_METACUBE;
221 log(ERROR, "Parameter 'encoding' must be either 'raw' (default) or 'metacube'");
225 // Parse marks, if so desired.
226 map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
227 if (mark_parm_it == line.parameters.end()) {
228 stream.mark_pool = -1;
231 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
234 stream.mark_pool = allocate_mark_pool(from, to, config);
237 // Parse the pacing rate, converting from kilobits to bytes as needed.
238 map<string, string>::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit");
239 if (pacing_rate_it == line.parameters.end()) {
240 stream.pacing_rate = ~0U;
242 stream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
245 config->streams.push_back(stream);
249 bool parse_udpstream(const ConfigLine &line, Config *config)
251 if (line.arguments.size() != 1) {
252 log(ERROR, "'udpstream' takes exactly one argument");
256 UDPStreamConfig udpstream;
258 string hostport = line.arguments[0];
260 // See if the argument if on the type [ipv6addr]:port.
261 if (!hostport.empty() && hostport[0] == '[') {
262 size_t split = hostport.find("]:");
263 if (split == string::npos) {
264 log(ERROR, "udpstream destination '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
268 string host(hostport.begin() + 1, hostport.begin() + split);
269 string port = hostport.substr(split + 2);
271 udpstream.dst.sin6_family = AF_INET6;
272 if (inet_pton(AF_INET6, host.c_str(), &udpstream.dst.sin6_addr) != 1) {
273 log(ERROR, "udpstream destination host '%s' is not a valid IPv6 address");
277 udpstream.dst.sin6_port = htons(atoi(port.c_str())); // TODO: Verify validity.
279 // OK, then it must be ipv4addr:port.
280 size_t split = hostport.find(":");
281 if (split == string::npos) {
282 log(ERROR, "udpstream destination '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
286 string host(hostport.begin(), hostport.begin() + split);
287 string port = hostport.substr(split + 1);
289 // Parse to an IPv4 address, then construct a mapped-v4 address from that.
292 if (inet_pton(AF_INET, host.c_str(), &addr4) != 1) {
293 log(ERROR, "udpstream destination host '%s' is not a valid IPv4 address");
297 udpstream.dst.sin6_family = AF_INET6;
298 udpstream.dst.sin6_addr.s6_addr32[0] = 0;
299 udpstream.dst.sin6_addr.s6_addr32[1] = 0;
300 udpstream.dst.sin6_addr.s6_addr32[2] = htonl(0xffff);
301 udpstream.dst.sin6_addr.s6_addr32[3] = addr4.s_addr;
302 udpstream.dst.sin6_port = htons(atoi(port.c_str())); // TODO: Verify validity.
305 map<string, string>::const_iterator src_it = line.parameters.find("src");
306 if (src_it == line.parameters.end()) {
307 // This is pretty meaningless, but OK, consistency is good.
308 log(WARNING, "udpstream to %s has no src= attribute, clients will not get any data.",
311 udpstream.src = src_it->second;
312 // TODO: Verify that the URL is parseable?
315 // Parse marks, if so desired.
316 map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
317 if (mark_parm_it == line.parameters.end()) {
318 udpstream.mark_pool = -1;
321 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
324 udpstream.mark_pool = allocate_mark_pool(from, to, config);
327 // Parse the pacing rate, converting from kilobits to bytes as needed.
328 map<string, string>::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit");
329 if (pacing_rate_it == line.parameters.end()) {
330 udpstream.pacing_rate = ~0U;
332 udpstream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
335 config->udpstreams.push_back(udpstream);
339 bool parse_error_log(const ConfigLine &line, Config *config)
341 if (line.arguments.size() != 0) {
342 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
346 LogConfig log_config;
347 map<string, string>::const_iterator type_it = line.parameters.find("type");
348 if (type_it == line.parameters.end()) {
349 log(ERROR, "'error_log' has no type= parameter");
353 string type = type_it->second;
354 if (type == "file") {
355 log_config.type = LogConfig::LOG_TYPE_FILE;
356 } else if (type == "syslog") {
357 log_config.type = LogConfig::LOG_TYPE_SYSLOG;
358 } else if (type == "console") {
359 log_config.type = LogConfig::LOG_TYPE_CONSOLE;
361 log(ERROR, "Unknown log type '%s'", type.c_str());
365 if (log_config.type == LogConfig::LOG_TYPE_FILE) {
366 map<string, string>::const_iterator filename_it = line.parameters.find("filename");
367 if (filename_it == line.parameters.end()) {
368 log(ERROR, "error_log type 'file' with no filename= parameter");
371 log_config.filename = filename_it->second;
374 config->log_destinations.push_back(log_config);
378 bool parse_config(const string &filename, Config *config)
380 vector<ConfigLine> lines;
381 if (!read_config(filename, &lines)) {
385 config->daemonize = false;
387 if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
388 log(ERROR, "Missing 'num_servers' statement in config file.");
391 if (config->num_servers < 1 || config->num_servers >= 20000) { // Insanely high max limit.
392 log(ERROR, "'num_servers' is %d, needs to be in [1, 20000>.", config->num_servers);
396 // See if the user wants stats.
397 config->stats_interval = 60;
398 bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
399 bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
400 if (has_stats_interval && !has_stats_file) {
401 log(WARNING, "'stats_interval' given, but no 'stats_file'. No client statistics will be written.");
404 config->input_stats_interval = 60;
405 bool has_input_stats_file = fetch_config_string(lines, "input_stats_file", &config->input_stats_file);
406 bool has_input_stats_interval = fetch_config_int(lines, "input_stats_interval", &config->input_stats_interval);
407 if (has_input_stats_interval && !has_input_stats_file) {
408 log(WARNING, "'input_stats_interval' given, but no 'input_stats_file'. No input statistics will be written.");
411 fetch_config_string(lines, "access_log", &config->access_log_file);
413 for (size_t i = 0; i < lines.size(); ++i) {
414 const ConfigLine &line = lines[i];
415 if (line.keyword == "num_servers" ||
416 line.keyword == "stats_file" ||
417 line.keyword == "stats_interval" ||
418 line.keyword == "input_stats_file" ||
419 line.keyword == "input_stats_interval" ||
420 line.keyword == "access_log") {
421 // Already taken care of, above.
422 } else if (line.keyword == "port") {
423 if (!parse_port(line, config)) {
426 } else if (line.keyword == "stream") {
427 if (!parse_stream(line, config)) {
430 } else if (line.keyword == "udpstream") {
431 if (!parse_udpstream(line, config)) {
434 } else if (line.keyword == "error_log") {
435 if (!parse_error_log(line, config)) {
438 } else if (line.keyword == "daemonize") {
439 config->daemonize = true;
441 log(ERROR, "Unknown configuration keyword '%s'.",
442 line.keyword.c_str());