17 #define DEFAULT_BACKLOG_SIZE 1048576
21 vector<string> arguments;
22 map<string, string> parameters;
25 bool read_config(const string &filename, vector<ConfigLine> *lines)
27 FILE *fp = fopen(filename.c_str(), "r");
29 log_perror(filename.c_str());
35 if (fgets(buf, sizeof(buf), fp) == NULL) {
39 // Chop off the string at the first #, \r or \n.
40 buf[strcspn(buf, "#\r\n")] = 0;
42 // Remove all whitespace from the end of the string.
43 size_t len = strlen(buf);
44 while (len > 0 && isspace(buf[len - 1])) {
48 // If the line is now all blank, ignore it.
53 vector<string> tokens = split_tokens(buf);
54 assert(!tokens.empty());
57 line.keyword = tokens[0];
59 for (size_t i = 1; i < tokens.size(); ++i) {
60 // foo=bar is a parameter; anything else is an argument.
61 size_t equals_pos = tokens[i].find_first_of('=');
62 if (equals_pos == string::npos) {
63 line.arguments.push_back(tokens[i]);
65 string key = tokens[i].substr(0, equals_pos);
66 string value = tokens[i].substr(equals_pos + 1, string::npos);
67 line.parameters.insert(make_pair(key, value));
71 lines->push_back(line);
78 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
80 for (unsigned i = 0; i < config.size(); ++i) {
81 if (config[i].keyword != keyword) {
84 if (config[i].parameters.size() > 0 ||
85 config[i].arguments.size() != 1) {
86 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
89 *value = config[i].arguments[0];
95 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
97 for (unsigned i = 0; i < config.size(); ++i) {
98 if (config[i].keyword != keyword) {
101 if (config[i].parameters.size() > 0 ||
102 config[i].arguments.size() != 1) {
103 log(ERROR, "'%s' takes one argument and no parameters", keyword.c_str());
106 *value = atoi(config[i].arguments[0].c_str()); // TODO: verify int validity.
112 bool parse_port(const ConfigLine &line, Config *config)
114 if (line.arguments.size() != 1) {
115 log(ERROR, "'port' takes exactly one argument");
119 AcceptorConfig acceptor;
120 acceptor.port = atoi(line.arguments[0].c_str());
121 if (acceptor.port < 1 || acceptor.port >= 65536) {
122 log(ERROR, "port %d is out of range (must be [1,65536>).", acceptor.port);
126 config->acceptors.push_back(acceptor);
130 int allocate_mark_pool(int from, int to, Config *config)
134 // Reuse mark pools if an identical one exists.
135 // Otherwise, check if we're overlapping some other mark pool.
136 for (size_t i = 0; i < config->mark_pools.size(); ++i) {
137 const MarkPoolConfig &pool = config->mark_pools[i];
138 if (from == pool.from && to == pool.to) {
140 } else if ((from >= pool.from && from < pool.to) ||
141 (to >= pool.from && to < pool.to)) {
142 log(WARNING, "Mark pool %d-%d partially overlaps with %d-%d, you may get duplicate marks."
143 "Mark pools must either be completely disjunct, or completely overlapping.",
144 from, to, pool.from, pool.to);
148 if (pool_index != -1) {
152 // No match to existing pools.
156 config->mark_pools.push_back(pool);
158 return config->mark_pools.size() - 1;
161 bool parse_mark_pool(const string &mark_str, int *from, int *to)
163 size_t split = mark_str.find_first_of('-');
164 if (split == string::npos) {
165 log(ERROR, "Invalid mark specification '%s' (expected 'X-Y').",
170 string from_str(mark_str.begin(), mark_str.begin() + split);
171 string to_str(mark_str.begin() + split + 1, mark_str.end());
172 *from = atoi(from_str.c_str());
173 *to = atoi(to_str.c_str());
175 if (*from <= 0 || *from >= 65536 || *to <= 0 || *to >= 65536) {
176 log(ERROR, "Mark pool range %d-%d is outside legal range [1,65536>.",
184 bool parse_stream(const ConfigLine &line, Config *config)
186 if (line.arguments.size() != 1) {
187 log(ERROR, "'stream' takes exactly one argument");
192 stream.stream_id = line.arguments[0];
194 map<string, string>::const_iterator src_it = line.parameters.find("src");
195 if (src_it == line.parameters.end()) {
196 log(WARNING, "stream '%s' has no src= attribute, clients will not get any data.",
197 stream.stream_id.c_str());
199 stream.src = src_it->second;
200 // TODO: Verify that the URL is parseable?
203 map<string, string>::const_iterator backlog_it = line.parameters.find("backlog_size");
204 if (backlog_it == line.parameters.end()) {
205 stream.backlog_size = DEFAULT_BACKLOG_SIZE;
207 stream.backlog_size = atoi(backlog_it->second.c_str());
210 // Parse marks, if so desired.
211 map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
212 if (mark_parm_it == line.parameters.end()) {
213 stream.mark_pool = -1;
216 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
219 stream.mark_pool = allocate_mark_pool(from, to, config);
222 config->streams.push_back(stream);
226 bool parse_error_log(const ConfigLine &line, Config *config)
228 if (line.arguments.size() != 0) {
229 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
233 LogConfig log_config;
234 map<string, string>::const_iterator type_it = line.parameters.find("type");
235 if (type_it == line.parameters.end()) {
236 log(ERROR, "'error_log' has no type= parameter");
240 string type = type_it->second;
241 if (type == "file") {
242 log_config.type = LogConfig::LOG_TYPE_FILE;
243 } else if (type == "syslog") {
244 log_config.type = LogConfig::LOG_TYPE_SYSLOG;
245 } else if (type == "console") {
246 log_config.type = LogConfig::LOG_TYPE_CONSOLE;
248 log(ERROR, "Unknown log type '%s'", type.c_str());
252 if (log_config.type == LogConfig::LOG_TYPE_FILE) {
253 map<string, string>::const_iterator filename_it = line.parameters.find("filename");
254 if (filename_it == line.parameters.end()) {
255 log(ERROR, "error_log type 'file' with no filename= parameter");
258 log_config.filename = filename_it->second;
261 config->log_destinations.push_back(log_config);
265 bool parse_config(const string &filename, Config *config)
267 vector<ConfigLine> lines;
268 if (!read_config(filename, &lines)) {
272 if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
273 log(ERROR, "Missing 'num_servers' statement in config file.");
276 if (config->num_servers < 1 || config->num_servers >= 20000) { // Insanely high max limit.
277 log(ERROR, "'num_servers' is %d, needs to be in [1, 20000>.", config->num_servers);
281 // See if the user wants stats.
282 config->stats_interval = 60;
283 bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
284 bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
285 if (has_stats_interval && !has_stats_file) {
286 log(WARNING, "'stats_interval' given, but no 'stats_file'. No statistics will be written.");
289 for (size_t i = 0; i < lines.size(); ++i) {
290 const ConfigLine &line = lines[i];
291 if (line.keyword == "num_servers" ||
292 line.keyword == "stats_file" ||
293 line.keyword == "stats_interval") {
294 // Already taken care of, above.
295 } else if (line.keyword == "port") {
296 if (!parse_port(line, config)) {
299 } else if (line.keyword == "stream") {
300 if (!parse_stream(line, config)) {
303 } else if (line.keyword == "error_log") {
304 if (!parse_error_log(line, config)) {
308 log(ERROR, "Unknown configuration keyword '%s'.",
309 line.keyword.c_str());