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 config->streams.push_back(stream);
241 bool parse_udpstream(const ConfigLine &line, Config *config)
243 if (line.arguments.size() != 1) {
244 log(ERROR, "'udpstream' takes exactly one argument");
248 UDPStreamConfig udpstream;
250 string hostport = line.arguments[0];
252 // See if the argument if on the type [ipv6addr]:port.
253 if (!hostport.empty() && hostport[0] == '[') {
254 size_t split = hostport.find("]:");
255 if (split == string::npos) {
256 log(ERROR, "udpstream destination '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
260 string host(hostport.begin() + 1, hostport.begin() + split);
261 string port = hostport.substr(split + 2);
263 udpstream.dst.sin6_family = AF_INET6;
264 if (inet_pton(AF_INET6, host.c_str(), &udpstream.dst.sin6_addr) != 1) {
265 log(ERROR, "udpstream destination host '%s' is not a valid IPv6 address");
269 udpstream.dst.sin6_port = htons(atoi(port.c_str())); // TODO: Verify validity.
271 // OK, then it must be ipv4addr:port.
272 size_t split = hostport.find(":");
273 if (split == string::npos) {
274 log(ERROR, "udpstream destination '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
278 string host(hostport.begin(), hostport.begin() + split);
279 string port = hostport.substr(split + 1);
281 // Parse to an IPv4 address, then construct a mapped-v4 address from that.
284 if (inet_pton(AF_INET, host.c_str(), &addr4) != 1) {
285 log(ERROR, "udpstream destination host '%s' is not a valid IPv4 address");
289 udpstream.dst.sin6_family = AF_INET6;
290 udpstream.dst.sin6_addr.s6_addr32[0] = 0;
291 udpstream.dst.sin6_addr.s6_addr32[1] = 0;
292 udpstream.dst.sin6_addr.s6_addr32[2] = htonl(0xffff);
293 udpstream.dst.sin6_addr.s6_addr32[3] = addr4.s_addr;
294 udpstream.dst.sin6_port = htons(atoi(port.c_str())); // TODO: Verify validity.
297 map<string, string>::const_iterator src_it = line.parameters.find("src");
298 if (src_it == line.parameters.end()) {
299 // This is pretty meaningless, but OK, consistency is good.
300 log(WARNING, "udpstream to %s has no src= attribute, clients will not get any data.",
303 udpstream.src = src_it->second;
304 // TODO: Verify that the URL is parseable?
307 // Parse marks, if so desired.
308 map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
309 if (mark_parm_it == line.parameters.end()) {
310 udpstream.mark_pool = -1;
313 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
316 udpstream.mark_pool = allocate_mark_pool(from, to, config);
319 config->udpstreams.push_back(udpstream);
322 bool parse_error_log(const ConfigLine &line, Config *config)
324 if (line.arguments.size() != 0) {
325 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
329 LogConfig log_config;
330 map<string, string>::const_iterator type_it = line.parameters.find("type");
331 if (type_it == line.parameters.end()) {
332 log(ERROR, "'error_log' has no type= parameter");
336 string type = type_it->second;
337 if (type == "file") {
338 log_config.type = LogConfig::LOG_TYPE_FILE;
339 } else if (type == "syslog") {
340 log_config.type = LogConfig::LOG_TYPE_SYSLOG;
341 } else if (type == "console") {
342 log_config.type = LogConfig::LOG_TYPE_CONSOLE;
344 log(ERROR, "Unknown log type '%s'", type.c_str());
348 if (log_config.type == LogConfig::LOG_TYPE_FILE) {
349 map<string, string>::const_iterator filename_it = line.parameters.find("filename");
350 if (filename_it == line.parameters.end()) {
351 log(ERROR, "error_log type 'file' with no filename= parameter");
354 log_config.filename = filename_it->second;
357 config->log_destinations.push_back(log_config);
361 bool parse_config(const string &filename, Config *config)
363 vector<ConfigLine> lines;
364 if (!read_config(filename, &lines)) {
368 config->daemonize = false;
370 if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
371 log(ERROR, "Missing 'num_servers' statement in config file.");
374 if (config->num_servers < 1 || config->num_servers >= 20000) { // Insanely high max limit.
375 log(ERROR, "'num_servers' is %d, needs to be in [1, 20000>.", config->num_servers);
379 // See if the user wants stats.
380 config->stats_interval = 60;
381 bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
382 bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
383 if (has_stats_interval && !has_stats_file) {
384 log(WARNING, "'stats_interval' given, but no 'stats_file'. No client statistics will be written.");
387 config->input_stats_interval = 60;
388 bool has_input_stats_file = fetch_config_string(lines, "input_stats_file", &config->input_stats_file);
389 bool has_input_stats_interval = fetch_config_int(lines, "input_stats_interval", &config->input_stats_interval);
390 if (has_input_stats_interval && !has_input_stats_file) {
391 log(WARNING, "'input_stats_interval' given, but no 'input_stats_file'. No input statistics will be written.");
394 fetch_config_string(lines, "access_log", &config->access_log_file);
396 for (size_t i = 0; i < lines.size(); ++i) {
397 const ConfigLine &line = lines[i];
398 if (line.keyword == "num_servers" ||
399 line.keyword == "stats_file" ||
400 line.keyword == "stats_interval" ||
401 line.keyword == "input_stats_file" ||
402 line.keyword == "input_stats_interval" ||
403 line.keyword == "access_log") {
404 // Already taken care of, above.
405 } else if (line.keyword == "port") {
406 if (!parse_port(line, config)) {
409 } else if (line.keyword == "stream") {
410 if (!parse_stream(line, config)) {
413 } else if (line.keyword == "udpstream") {
414 if (!parse_udpstream(line, config)) {
417 } else if (line.keyword == "error_log") {
418 if (!parse_error_log(line, config)) {
421 } else if (line.keyword == "daemonize") {
422 config->daemonize = true;
424 log(ERROR, "Unknown configuration keyword '%s'.",
425 line.keyword.c_str());