9 #include <sys/socket.h>
22 #define DEFAULT_BACKLOG_SIZE 10485760
26 vector<string> arguments;
27 map<string, string> parameters;
32 bool parse_hostport(const string &hostport, sockaddr_in6 *addr)
34 memset(addr, 0, sizeof(*addr));
35 addr->sin6_family = AF_INET6;
39 // See if the argument if on the type [ipv6addr]:port.
40 if (!hostport.empty() && hostport[0] == '[') {
41 size_t split = hostport.find("]:");
42 if (split == string::npos) {
43 log(ERROR, "address '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
47 string host(hostport.begin() + 1, hostport.begin() + split);
48 port_string = hostport.substr(split + 2);
50 if (inet_pton(AF_INET6, host.c_str(), &addr->sin6_addr) != 1) {
51 log(ERROR, "'%s' is not a valid IPv6 address");
55 // OK, then it must be ipv4addr:port.
56 size_t split = hostport.find(":");
57 if (split == string::npos) {
58 log(ERROR, "address '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
62 string host(hostport.begin(), hostport.begin() + split);
63 port_string = hostport.substr(split + 1);
65 // Parse to an IPv4 address, then construct a mapped-v4 address from that.
68 if (inet_pton(AF_INET, host.c_str(), &addr4) != 1) {
69 log(ERROR, "'%s' is not a valid IPv4 address");
73 addr->sin6_addr.s6_addr32[2] = htonl(0xffff);
74 addr->sin6_addr.s6_addr32[3] = addr4.s_addr;
77 int port = atoi(port_string.c_str());
78 if (port < 1 || port >= 65536) {
79 log(ERROR, "port %d is out of range (must be [1,65536>).", port);
82 addr->sin6_port = ntohs(port);
87 bool read_config(const string &filename, vector<ConfigLine> *lines)
89 FILE *fp = fopen(filename.c_str(), "r");
91 log_perror(filename.c_str());
97 if (fgets(buf, sizeof(buf), fp) == NULL) {
101 // Chop off the string at the first #, \r or \n.
102 buf[strcspn(buf, "#\r\n")] = 0;
104 // Remove all whitespace from the end of the string.
105 size_t len = strlen(buf);
106 while (len > 0 && isspace(buf[len - 1])) {
110 // If the line is now all blank, ignore it.
115 vector<string> tokens = split_tokens(buf);
116 assert(!tokens.empty());
119 line.keyword = tokens[0];
121 for (size_t i = 1; i < tokens.size(); ++i) {
122 // foo=bar is a parameter; anything else is an argument.
123 size_t equals_pos = tokens[i].find_first_of('=');
124 if (equals_pos == string::npos) {
125 line.arguments.push_back(tokens[i]);
127 string key = tokens[i].substr(0, equals_pos);
128 string value = tokens[i].substr(equals_pos + 1, string::npos);
129 line.parameters.insert(make_pair(key, value));
133 lines->push_back(line);
136 if (fclose(fp) == EOF) {
137 log_perror(filename.c_str());
143 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
145 for (unsigned i = 0; i < config.size(); ++i) {
146 if (config[i].keyword != keyword) {
149 if (config[i].parameters.size() > 0 ||
150 config[i].arguments.size() != 1) {
151 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
154 *value = config[i].arguments[0];
160 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
162 for (unsigned i = 0; i < config.size(); ++i) {
163 if (config[i].keyword != keyword) {
166 if (config[i].parameters.size() > 0 ||
167 config[i].arguments.size() != 1) {
168 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
171 *value = atoi(config[i].arguments[0].c_str()); // TODO: verify int validity.
177 bool parse_port(const ConfigLine &line, Config *config)
179 if (line.arguments.size() != 1) {
180 log(ERROR, "'port' takes exactly one argument");
184 int port = atoi(line.arguments[0].c_str());
185 if (port < 1 || port >= 65536) {
186 log(ERROR, "port %d is out of range (must be [1,65536>).", port);
190 AcceptorConfig acceptor;
191 acceptor.addr = create_any_address(port);
193 config->acceptors.push_back(acceptor);
197 bool parse_listen(const ConfigLine &line, Config *config)
199 if (line.arguments.size() != 1) {
200 log(ERROR, "'listen' takes exactly one argument");
204 AcceptorConfig acceptor;
205 if (!parse_hostport(line.arguments[0], &acceptor.addr)) {
208 config->acceptors.push_back(acceptor);
212 bool parse_stream(const ConfigLine &line, Config *config)
214 if (line.arguments.size() != 1) {
215 log(ERROR, "'stream' takes exactly one argument");
220 stream.url = line.arguments[0];
222 map<string, string>::const_iterator src_it = line.parameters.find("src");
223 if (src_it == line.parameters.end()) {
224 log(WARNING, "stream '%s' has no src= attribute, clients will not get any data.",
227 stream.src = src_it->second;
228 // TODO: Verify that the URL is parseable?
231 map<string, string>::const_iterator backlog_it = line.parameters.find("backlog_size");
232 if (backlog_it == line.parameters.end()) {
233 stream.backlog_size = DEFAULT_BACKLOG_SIZE;
235 stream.backlog_size = atoi(backlog_it->second.c_str());
238 map<string, string>::const_iterator prebuffer_it = line.parameters.find("force_prebuffer");
239 if (prebuffer_it == line.parameters.end()) {
240 stream.prebuffering_bytes = 0;
242 stream.prebuffering_bytes = atoi(prebuffer_it->second.c_str());
246 map<string, string>::const_iterator encoding_parm_it = line.parameters.find("encoding");
247 if (encoding_parm_it == line.parameters.end() ||
248 encoding_parm_it->second == "raw") {
249 stream.encoding = StreamConfig::STREAM_ENCODING_RAW;
250 } else if (encoding_parm_it->second == "metacube") {
251 stream.encoding = StreamConfig::STREAM_ENCODING_METACUBE;
253 log(ERROR, "Parameter 'encoding' must be either 'raw' (default) or 'metacube'");
257 // Parse the pacing rate, converting from kilobits to bytes as needed.
258 map<string, string>::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit");
259 if (pacing_rate_it == line.parameters.end()) {
260 stream.pacing_rate = ~0U;
262 stream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
265 config->streams.push_back(stream);
269 bool parse_udpstream(const ConfigLine &line, Config *config)
271 if (line.arguments.size() != 1) {
272 log(ERROR, "'udpstream' takes exactly one argument");
276 UDPStreamConfig udpstream;
278 string hostport = line.arguments[0];
279 if (!parse_hostport(hostport, &udpstream.dst)) {
283 map<string, string>::const_iterator src_it = line.parameters.find("src");
284 if (src_it == line.parameters.end()) {
285 // This is pretty meaningless, but OK, consistency is good.
286 log(WARNING, "udpstream to %s has no src= attribute, clients will not get any data.",
289 udpstream.src = src_it->second;
290 // TODO: Verify that the URL is parseable?
293 // Parse the pacing rate, converting from kilobits to bytes as needed.
294 map<string, string>::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit");
295 if (pacing_rate_it == line.parameters.end()) {
296 udpstream.pacing_rate = ~0U;
298 udpstream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
301 // Parse the TTL. The same value is used for unicast and multicast.
302 map<string, string>::const_iterator ttl_it = line.parameters.find("ttl");
303 if (ttl_it == line.parameters.end()) {
306 udpstream.ttl = atoi(ttl_it->second.c_str());
309 // Parse the multicast interface index.
310 map<string, string>::const_iterator multicast_iface_it = line.parameters.find("multicast_output_interface");
311 if (multicast_iface_it == line.parameters.end()) {
312 udpstream.multicast_iface_index = -1;
314 udpstream.multicast_iface_index = if_nametoindex(multicast_iface_it->second.c_str());
315 if (udpstream.multicast_iface_index == 0) {
316 log(ERROR, "Interface '%s' does not exist", multicast_iface_it->second.c_str());
321 config->udpstreams.push_back(udpstream);
325 bool parse_error_log(const ConfigLine &line, Config *config)
327 if (line.arguments.size() != 0) {
328 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
332 LogConfig log_config;
333 map<string, string>::const_iterator type_it = line.parameters.find("type");
334 if (type_it == line.parameters.end()) {
335 log(ERROR, "'error_log' has no type= parameter");
339 string type = type_it->second;
340 if (type == "file") {
341 log_config.type = LogConfig::LOG_TYPE_FILE;
342 } else if (type == "syslog") {
343 log_config.type = LogConfig::LOG_TYPE_SYSLOG;
344 } else if (type == "console") {
345 log_config.type = LogConfig::LOG_TYPE_CONSOLE;
347 log(ERROR, "Unknown log type '%s'", type.c_str());
351 if (log_config.type == LogConfig::LOG_TYPE_FILE) {
352 map<string, string>::const_iterator filename_it = line.parameters.find("filename");
353 if (filename_it == line.parameters.end()) {
354 log(ERROR, "error_log type 'file' with no filename= parameter");
357 log_config.filename = filename_it->second;
360 config->log_destinations.push_back(log_config);
366 bool parse_config(const string &filename, Config *config)
368 vector<ConfigLine> lines;
369 if (!read_config(filename, &lines)) {
373 config->daemonize = false;
375 if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
376 log(ERROR, "Missing 'num_servers' statement in config file.");
379 if (config->num_servers < 1 || config->num_servers >= 20000) { // Insanely high max limit.
380 log(ERROR, "'num_servers' is %d, needs to be in [1, 20000>.", config->num_servers);
384 // See if the user wants stats.
385 config->stats_interval = 60;
386 bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
387 bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
388 if (has_stats_interval && !has_stats_file) {
389 log(WARNING, "'stats_interval' given, but no 'stats_file'. No client statistics will be written.");
392 config->input_stats_interval = 60;
393 bool has_input_stats_file = fetch_config_string(lines, "input_stats_file", &config->input_stats_file);
394 bool has_input_stats_interval = fetch_config_int(lines, "input_stats_interval", &config->input_stats_interval);
395 if (has_input_stats_interval && !has_input_stats_file) {
396 log(WARNING, "'input_stats_interval' given, but no 'input_stats_file'. No input statistics will be written.");
399 fetch_config_string(lines, "access_log", &config->access_log_file);
401 for (size_t i = 0; i < lines.size(); ++i) {
402 const ConfigLine &line = lines[i];
403 if (line.keyword == "num_servers" ||
404 line.keyword == "stats_file" ||
405 line.keyword == "stats_interval" ||
406 line.keyword == "input_stats_file" ||
407 line.keyword == "input_stats_interval" ||
408 line.keyword == "access_log") {
409 // Already taken care of, above.
410 } else if (line.keyword == "port") {
411 if (!parse_port(line, config)) {
414 } else if (line.keyword == "listen") {
415 if (!parse_listen(line, config)) {
418 } else if (line.keyword == "stream") {
419 if (!parse_stream(line, config)) {
422 } else if (line.keyword == "udpstream") {
423 if (!parse_udpstream(line, config)) {
426 } else if (line.keyword == "error_log") {
427 if (!parse_error_log(line, config)) {
430 } else if (line.keyword == "daemonize") {
431 config->daemonize = true;
433 log(ERROR, "Unknown configuration keyword '%s'.",
434 line.keyword.c_str());