8 #include <sys/socket.h>
21 #define DEFAULT_BACKLOG_SIZE 1048576
25 vector<string> arguments;
26 map<string, string> parameters;
31 bool parse_hostport(const string &hostport, sockaddr_in6 *addr)
33 memset(addr, 0, sizeof(*addr));
34 addr->sin6_family = AF_INET6;
38 // See if the argument if on the type [ipv6addr]:port.
39 if (!hostport.empty() && hostport[0] == '[') {
40 size_t split = hostport.find("]:");
41 if (split == string::npos) {
42 log(ERROR, "address '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
46 string host(hostport.begin() + 1, hostport.begin() + split);
47 port_string = hostport.substr(split + 2);
49 if (inet_pton(AF_INET6, host.c_str(), &addr->sin6_addr) != 1) {
50 log(ERROR, "'%s' is not a valid IPv6 address");
54 // OK, then it must be ipv4addr:port.
55 size_t split = hostport.find(":");
56 if (split == string::npos) {
57 log(ERROR, "address '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
61 string host(hostport.begin(), hostport.begin() + split);
62 port_string = hostport.substr(split + 1);
64 // Parse to an IPv4 address, then construct a mapped-v4 address from that.
67 if (inet_pton(AF_INET, host.c_str(), &addr4) != 1) {
68 log(ERROR, "'%s' is not a valid IPv4 address");
72 addr->sin6_addr.s6_addr32[2] = htonl(0xffff);
73 addr->sin6_addr.s6_addr32[3] = addr4.s_addr;
76 int port = atoi(port_string.c_str());
77 if (port < 1 || port >= 65536) {
78 log(ERROR, "port %d is out of range (must be [1,65536>).", port);
81 addr->sin6_port = ntohs(port);
86 bool read_config(const string &filename, vector<ConfigLine> *lines)
88 FILE *fp = fopen(filename.c_str(), "r");
90 log_perror(filename.c_str());
96 if (fgets(buf, sizeof(buf), fp) == NULL) {
100 // Chop off the string at the first #, \r or \n.
101 buf[strcspn(buf, "#\r\n")] = 0;
103 // Remove all whitespace from the end of the string.
104 size_t len = strlen(buf);
105 while (len > 0 && isspace(buf[len - 1])) {
109 // If the line is now all blank, ignore it.
114 vector<string> tokens = split_tokens(buf);
115 assert(!tokens.empty());
118 line.keyword = tokens[0];
120 for (size_t i = 1; i < tokens.size(); ++i) {
121 // foo=bar is a parameter; anything else is an argument.
122 size_t equals_pos = tokens[i].find_first_of('=');
123 if (equals_pos == string::npos) {
124 line.arguments.push_back(tokens[i]);
126 string key = tokens[i].substr(0, equals_pos);
127 string value = tokens[i].substr(equals_pos + 1, string::npos);
128 line.parameters.insert(make_pair(key, value));
132 lines->push_back(line);
139 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
141 for (unsigned i = 0; i < config.size(); ++i) {
142 if (config[i].keyword != keyword) {
145 if (config[i].parameters.size() > 0 ||
146 config[i].arguments.size() != 1) {
147 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
150 *value = config[i].arguments[0];
156 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
158 for (unsigned i = 0; i < config.size(); ++i) {
159 if (config[i].keyword != keyword) {
162 if (config[i].parameters.size() > 0 ||
163 config[i].arguments.size() != 1) {
164 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
167 *value = atoi(config[i].arguments[0].c_str()); // TODO: verify int validity.
173 bool parse_port(const ConfigLine &line, Config *config)
175 if (line.arguments.size() != 1) {
176 log(ERROR, "'port' takes exactly one argument");
180 int port = atoi(line.arguments[0].c_str());
181 if (port < 1 || port >= 65536) {
182 log(ERROR, "port %d is out of range (must be [1,65536>).", port);
186 AcceptorConfig acceptor;
187 acceptor.addr = CreateAnyAddress(port);
189 config->acceptors.push_back(acceptor);
193 bool parse_listen(const ConfigLine &line, Config *config)
195 if (line.arguments.size() != 1) {
196 log(ERROR, "'listen' takes exactly one argument");
200 AcceptorConfig acceptor;
201 if (!parse_hostport(line.arguments[0], &acceptor.addr)) {
204 config->acceptors.push_back(acceptor);
208 bool parse_stream(const ConfigLine &line, Config *config)
210 if (line.arguments.size() != 1) {
211 log(ERROR, "'stream' takes exactly one argument");
216 stream.url = line.arguments[0];
218 map<string, string>::const_iterator src_it = line.parameters.find("src");
219 if (src_it == line.parameters.end()) {
220 log(WARNING, "stream '%s' has no src= attribute, clients will not get any data.",
223 stream.src = src_it->second;
224 // TODO: Verify that the URL is parseable?
227 map<string, string>::const_iterator backlog_it = line.parameters.find("backlog_size");
228 if (backlog_it == line.parameters.end()) {
229 stream.backlog_size = DEFAULT_BACKLOG_SIZE;
231 stream.backlog_size = atoi(backlog_it->second.c_str());
235 map<string, string>::const_iterator encoding_parm_it = line.parameters.find("encoding");
236 if (encoding_parm_it == line.parameters.end() ||
237 encoding_parm_it->second == "raw") {
238 stream.encoding = StreamConfig::STREAM_ENCODING_RAW;
239 } else if (encoding_parm_it->second == "metacube") {
240 stream.encoding = StreamConfig::STREAM_ENCODING_METACUBE;
242 log(ERROR, "Parameter 'encoding' must be either 'raw' (default) or 'metacube'");
246 // Parse the pacing rate, converting from kilobits to bytes as needed.
247 map<string, string>::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit");
248 if (pacing_rate_it == line.parameters.end()) {
249 stream.pacing_rate = ~0U;
251 stream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
254 config->streams.push_back(stream);
258 bool parse_udpstream(const ConfigLine &line, Config *config)
260 if (line.arguments.size() != 1) {
261 log(ERROR, "'udpstream' takes exactly one argument");
265 UDPStreamConfig udpstream;
267 string hostport = line.arguments[0];
268 if (!parse_hostport(hostport, &udpstream.dst)) {
272 map<string, string>::const_iterator src_it = line.parameters.find("src");
273 if (src_it == line.parameters.end()) {
274 // This is pretty meaningless, but OK, consistency is good.
275 log(WARNING, "udpstream to %s has no src= attribute, clients will not get any data.",
278 udpstream.src = src_it->second;
279 // TODO: Verify that the URL is parseable?
282 // Parse the pacing rate, converting from kilobits to bytes as needed.
283 map<string, string>::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit");
284 if (pacing_rate_it == line.parameters.end()) {
285 udpstream.pacing_rate = ~0U;
287 udpstream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
290 config->udpstreams.push_back(udpstream);
294 bool parse_error_log(const ConfigLine &line, Config *config)
296 if (line.arguments.size() != 0) {
297 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
301 LogConfig log_config;
302 map<string, string>::const_iterator type_it = line.parameters.find("type");
303 if (type_it == line.parameters.end()) {
304 log(ERROR, "'error_log' has no type= parameter");
308 string type = type_it->second;
309 if (type == "file") {
310 log_config.type = LogConfig::LOG_TYPE_FILE;
311 } else if (type == "syslog") {
312 log_config.type = LogConfig::LOG_TYPE_SYSLOG;
313 } else if (type == "console") {
314 log_config.type = LogConfig::LOG_TYPE_CONSOLE;
316 log(ERROR, "Unknown log type '%s'", type.c_str());
320 if (log_config.type == LogConfig::LOG_TYPE_FILE) {
321 map<string, string>::const_iterator filename_it = line.parameters.find("filename");
322 if (filename_it == line.parameters.end()) {
323 log(ERROR, "error_log type 'file' with no filename= parameter");
326 log_config.filename = filename_it->second;
329 config->log_destinations.push_back(log_config);
335 bool parse_config(const string &filename, Config *config)
337 vector<ConfigLine> lines;
338 if (!read_config(filename, &lines)) {
342 config->daemonize = false;
344 if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
345 log(ERROR, "Missing 'num_servers' statement in config file.");
348 if (config->num_servers < 1 || config->num_servers >= 20000) { // Insanely high max limit.
349 log(ERROR, "'num_servers' is %d, needs to be in [1, 20000>.", config->num_servers);
353 // See if the user wants stats.
354 config->stats_interval = 60;
355 bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
356 bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
357 if (has_stats_interval && !has_stats_file) {
358 log(WARNING, "'stats_interval' given, but no 'stats_file'. No client statistics will be written.");
361 config->input_stats_interval = 60;
362 bool has_input_stats_file = fetch_config_string(lines, "input_stats_file", &config->input_stats_file);
363 bool has_input_stats_interval = fetch_config_int(lines, "input_stats_interval", &config->input_stats_interval);
364 if (has_input_stats_interval && !has_input_stats_file) {
365 log(WARNING, "'input_stats_interval' given, but no 'input_stats_file'. No input statistics will be written.");
368 fetch_config_string(lines, "access_log", &config->access_log_file);
370 for (size_t i = 0; i < lines.size(); ++i) {
371 const ConfigLine &line = lines[i];
372 if (line.keyword == "num_servers" ||
373 line.keyword == "stats_file" ||
374 line.keyword == "stats_interval" ||
375 line.keyword == "input_stats_file" ||
376 line.keyword == "input_stats_interval" ||
377 line.keyword == "access_log") {
378 // Already taken care of, above.
379 } else if (line.keyword == "port") {
380 if (!parse_port(line, config)) {
383 } else if (line.keyword == "listen") {
384 if (!parse_listen(line, config)) {
387 } else if (line.keyword == "stream") {
388 if (!parse_stream(line, config)) {
391 } else if (line.keyword == "udpstream") {
392 if (!parse_udpstream(line, config)) {
395 } else if (line.keyword == "error_log") {
396 if (!parse_error_log(line, config)) {
399 } else if (line.keyword == "daemonize") {
400 config->daemonize = true;
402 log(ERROR, "Unknown configuration keyword '%s'.",
403 line.keyword.c_str());