]> git.sesse.net Git - cubemap/blob - config.cpp
Log IP address instead of file descriptor.
[cubemap] / config.cpp
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <map>
7 #include <string>
8 #include <utility>
9 #include <vector>
10
11 #include "config.h"
12 #include "log.h"
13 #include "parse.h"
14
15 using namespace std;
16
17 #define DEFAULT_BACKLOG_SIZE 1048576
18
19 struct ConfigLine {
20         string keyword;
21         vector<string> arguments;
22         map<string, string> parameters;
23 };
24
25 bool read_config(const string &filename, vector<ConfigLine> *lines)
26 {
27         FILE *fp = fopen(filename.c_str(), "r");
28         if (fp == NULL) {
29                 log_perror(filename.c_str());
30                 return false;
31         }
32
33         char buf[4096];
34         while (!feof(fp)) {
35                 if (fgets(buf, sizeof(buf), fp) == NULL) {
36                         break;
37                 }
38
39                 // Chop off the string at the first #, \r or \n.
40                 buf[strcspn(buf, "#\r\n")] = 0;
41
42                 // Remove all whitespace from the end of the string.
43                 size_t len = strlen(buf);
44                 while (len > 0 && isspace(buf[len - 1])) {
45                         buf[--len] = 0;
46                 }
47
48                 // If the line is now all blank, ignore it.
49                 if (len == 0) {
50                         continue;
51                 }
52
53                 vector<string> tokens = split_tokens(buf);
54                 assert(!tokens.empty());
55                 
56                 ConfigLine line;
57                 line.keyword = tokens[0];
58
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]);
64                         } else {
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));
68                         }
69                 }
70
71                 lines->push_back(line);
72         }
73
74         fclose(fp);
75         return true;
76 }
77
78 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
79 {
80         for (unsigned i = 0; i < config.size(); ++i) {
81                 if (config[i].keyword != keyword) {
82                         continue;
83                 }
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());
87                         return false;
88                 }
89                 *value = config[i].arguments[0];
90                 return true;
91         }
92         return false;
93 }
94
95 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
96 {
97         for (unsigned i = 0; i < config.size(); ++i) {
98                 if (config[i].keyword != keyword) {
99                         continue;
100                 }
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());
104                         return false;
105                 }
106                 *value = atoi(config[i].arguments[0].c_str());  // TODO: verify int validity.
107                 return true;
108         }
109         return false;
110 }
111
112 bool parse_port(const ConfigLine &line, Config *config)
113 {
114         if (line.arguments.size() != 1) {
115                 log(ERROR, "'port' takes exactly one argument");
116                 return false;
117         }
118
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);
123                 return false;
124         }
125
126         config->acceptors.push_back(acceptor);
127         return true;
128 }
129
130 int allocate_mark_pool(int from, int to, Config *config)
131 {
132         int pool_index = -1;    
133
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) {
139                         pool_index = i;
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);
145                 }
146         }
147
148         if (pool_index != -1) {
149                 return pool_index;
150         }
151
152         // No match to existing pools.
153         MarkPoolConfig pool;
154         pool.from = from;
155         pool.to = to;
156         config->mark_pools.push_back(pool);
157
158         return config->mark_pools.size() - 1;
159 }
160
161 bool parse_mark_pool(const string &mark_str, int *from, int *to)
162 {
163         size_t split = mark_str.find_first_of('-');
164         if (split == string::npos) {
165                 log(ERROR, "Invalid mark specification '%s' (expected 'X-Y').",
166                         mark_str.c_str());
167                 return false;
168         }
169
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());
174
175         if (*from <= 0 || *from >= 65536 || *to <= 0 || *to >= 65536) {
176                 log(ERROR, "Mark pool range %d-%d is outside legal range [1,65536>.",
177                         *from, *to);
178                 return false;
179         }
180
181         return true;
182 }
183
184 bool parse_stream(const ConfigLine &line, Config *config)
185 {
186         if (line.arguments.size() != 1) {
187                 log(ERROR, "'stream' takes exactly one argument");
188                 return false;
189         }
190
191         StreamConfig stream;
192         stream.stream_id = line.arguments[0];
193
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());
198         } else {
199                 stream.src = src_it->second;
200                 // TODO: Verify that the URL is parseable?
201         }
202
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;
206         } else {
207                 stream.backlog_size = atoi(backlog_it->second.c_str());
208         }
209
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;
214         } else {
215                 int from, to;
216                 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
217                         return false;
218                 }
219                 stream.mark_pool = allocate_mark_pool(from, to, config);
220         }
221
222         config->streams.push_back(stream);
223         return true;
224 }
225
226 bool parse_error_log(const ConfigLine &line, Config *config)
227 {
228         if (line.arguments.size() != 0) {
229                 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
230                 return false;
231         }
232
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");
237                 return false; 
238         }
239
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;
247         } else {
248                 log(ERROR, "Unknown log type '%s'", type.c_str());
249                 return false; 
250         }
251
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");
256                         return false; 
257                 }
258                 log_config.filename = filename_it->second;
259         }
260
261         config->log_destinations.push_back(log_config);
262         return true;
263 }
264
265 bool parse_config(const string &filename, Config *config)
266 {
267         vector<ConfigLine> lines;
268         if (!read_config(filename, &lines)) {
269                 return false;
270         }
271
272         if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
273                 log(ERROR, "Missing 'num_servers' statement in config file.");
274                 return false;
275         }
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);
278                 return false;
279         }
280
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.");
287         }
288
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)) {
297                                 return false;
298                         }
299                 } else if (line.keyword == "stream") {
300                         if (!parse_stream(line, config)) {
301                                 return false;
302                         }
303                 } else if (line.keyword == "error_log") {
304                         if (!parse_error_log(line, config)) {
305                                 return false;
306                         }
307                 } else {
308                         log(ERROR, "Unknown configuration keyword '%s'.",
309                                 line.keyword.c_str());
310                         return false;
311                 }
312         }
313
314         return true;
315 }