]> git.sesse.net Git - cubemap/blob - config.cpp
Update the man page with our non-alphaness as well.
[cubemap] / config.cpp
1 #include <arpa/inet.h>
2 #include <assert.h>
3 #include <ctype.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/socket.h>
9 #include <map>
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 #include "config.h"
15 #include "log.h"
16 #include "parse.h"
17
18 using namespace std;
19
20 #define DEFAULT_BACKLOG_SIZE 1048576
21
22 struct ConfigLine {
23         string keyword;
24         vector<string> arguments;
25         map<string, string> parameters;
26 };
27
28 bool read_config(const string &filename, vector<ConfigLine> *lines)
29 {
30         FILE *fp = fopen(filename.c_str(), "r");
31         if (fp == NULL) {
32                 log_perror(filename.c_str());
33                 return false;
34         }
35
36         char buf[4096];
37         while (!feof(fp)) {
38                 if (fgets(buf, sizeof(buf), fp) == NULL) {
39                         break;
40                 }
41
42                 // Chop off the string at the first #, \r or \n.
43                 buf[strcspn(buf, "#\r\n")] = 0;
44
45                 // Remove all whitespace from the end of the string.
46                 size_t len = strlen(buf);
47                 while (len > 0 && isspace(buf[len - 1])) {
48                         buf[--len] = 0;
49                 }
50
51                 // If the line is now all blank, ignore it.
52                 if (len == 0) {
53                         continue;
54                 }
55
56                 vector<string> tokens = split_tokens(buf);
57                 assert(!tokens.empty());
58                 
59                 ConfigLine line;
60                 line.keyword = tokens[0];
61
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]);
67                         } else {
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));
71                         }
72                 }
73
74                 lines->push_back(line);
75         }
76
77         fclose(fp);
78         return true;
79 }
80
81 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
82 {
83         for (unsigned i = 0; i < config.size(); ++i) {
84                 if (config[i].keyword != keyword) {
85                         continue;
86                 }
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());
90                         return false;
91                 }
92                 *value = config[i].arguments[0];
93                 return true;
94         }
95         return false;
96 }
97
98 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
99 {
100         for (unsigned i = 0; i < config.size(); ++i) {
101                 if (config[i].keyword != keyword) {
102                         continue;
103                 }
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());
107                         return false;
108                 }
109                 *value = atoi(config[i].arguments[0].c_str());  // TODO: verify int validity.
110                 return true;
111         }
112         return false;
113 }
114
115 bool parse_port(const ConfigLine &line, Config *config)
116 {
117         if (line.arguments.size() != 1) {
118                 log(ERROR, "'port' takes exactly one argument");
119                 return false;
120         }
121
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);
126                 return false;
127         }
128
129         config->acceptors.push_back(acceptor);
130         return true;
131 }
132
133 int allocate_mark_pool(int from, int to, Config *config)
134 {
135         int pool_index = -1;    
136
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) {
142                         pool_index = i;
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);
148                 }
149         }
150
151         if (pool_index != -1) {
152                 return pool_index;
153         }
154
155         // No match to existing pools.
156         MarkPoolConfig pool;
157         pool.from = from;
158         pool.to = to;
159         config->mark_pools.push_back(pool);
160
161         return config->mark_pools.size() - 1;
162 }
163
164 bool parse_mark_pool(const string &mark_str, int *from, int *to)
165 {
166         size_t split = mark_str.find_first_of('-');
167         if (split == string::npos) {
168                 log(ERROR, "Invalid mark specification '%s' (expected 'X-Y').",
169                         mark_str.c_str());
170                 return false;
171         }
172
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());
177
178         if (*from <= 0 || *from >= 65536 || *to <= 0 || *to >= 65536) {
179                 log(ERROR, "Mark pool range %d-%d is outside legal range [1,65536>.",
180                         *from, *to);
181                 return false;
182         }
183
184         return true;
185 }
186
187 bool parse_stream(const ConfigLine &line, Config *config)
188 {
189         if (line.arguments.size() != 1) {
190                 log(ERROR, "'stream' takes exactly one argument");
191                 return false;
192         }
193
194         StreamConfig stream;
195         stream.url = line.arguments[0];
196
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.",
200                         stream.url.c_str());
201         } else {
202                 stream.src = src_it->second;
203                 // TODO: Verify that the URL is parseable?
204         }
205
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;
209         } else {
210                 stream.backlog_size = atoi(backlog_it->second.c_str());
211         }
212
213         // Parse encoding.
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;
220         } else {
221                 log(ERROR, "Parameter 'encoding' must be either 'raw' (default) or 'metacube'");
222                 return false;
223         }
224
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;
229         } else {
230                 int from, to;
231                 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
232                         return false;
233                 }
234                 stream.mark_pool = allocate_mark_pool(from, to, config);
235         }
236
237         config->streams.push_back(stream);
238         return true;
239 }
240
241 bool parse_udpstream(const ConfigLine &line, Config *config)
242 {
243         if (line.arguments.size() != 1) {
244                 log(ERROR, "'udpstream' takes exactly one argument");
245                 return false;
246         }
247
248         UDPStreamConfig udpstream;
249
250         string hostport = line.arguments[0];
251
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");
257                         return false;
258                 }
259
260                 string host(hostport.begin() + 1, hostport.begin() + split);
261                 string port = hostport.substr(split + 2);
262
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");
266                         return false;
267                 }
268
269                 udpstream.dst.sin6_port = htons(atoi(port.c_str()));  // TODO: Verify validity.
270         } else {
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");
275                         return false;
276                 }
277
278                 string host(hostport.begin(), hostport.begin() + split);
279                 string port = hostport.substr(split + 1);
280
281                 // Parse to an IPv4 address, then construct a mapped-v4 address from that.
282                 in_addr addr4;
283
284                 if (inet_pton(AF_INET, host.c_str(), &addr4) != 1) {
285                         log(ERROR, "udpstream destination host '%s' is not a valid IPv4 address");
286                         return false;
287                 }
288
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.
295         }
296
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.",
301                         hostport.c_str());
302         } else {
303                 udpstream.src = src_it->second;
304                 // TODO: Verify that the URL is parseable?
305         }
306
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;
311         } else {
312                 int from, to;
313                 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
314                         return false;
315                 }
316                 udpstream.mark_pool = allocate_mark_pool(from, to, config);
317         }
318
319         config->udpstreams.push_back(udpstream);
320         return true;
321 }
322 bool parse_error_log(const ConfigLine &line, Config *config)
323 {
324         if (line.arguments.size() != 0) {
325                 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
326                 return false;
327         }
328
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");
333                 return false; 
334         }
335
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;
343         } else {
344                 log(ERROR, "Unknown log type '%s'", type.c_str());
345                 return false; 
346         }
347
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");
352                         return false; 
353                 }
354                 log_config.filename = filename_it->second;
355         }
356
357         config->log_destinations.push_back(log_config);
358         return true;
359 }
360
361 bool parse_config(const string &filename, Config *config)
362 {
363         vector<ConfigLine> lines;
364         if (!read_config(filename, &lines)) {
365                 return false;
366         }
367
368         config->daemonize = false;
369
370         if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
371                 log(ERROR, "Missing 'num_servers' statement in config file.");
372                 return false;
373         }
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);
376                 return false;
377         }
378
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.");
385         }
386
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.");
392         }
393         
394         fetch_config_string(lines, "access_log", &config->access_log_file);
395
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)) {
407                                 return false;
408                         }
409                 } else if (line.keyword == "stream") {
410                         if (!parse_stream(line, config)) {
411                                 return false;
412                         }
413                 } else if (line.keyword == "udpstream") {
414                         if (!parse_udpstream(line, config)) {
415                                 return false;
416                         }
417                 } else if (line.keyword == "error_log") {
418                         if (!parse_error_log(line, config)) {
419                                 return false;
420                         }
421                 } else if (line.keyword == "daemonize") {
422                         config->daemonize = true;
423                 } else {
424                         log(ERROR, "Unknown configuration keyword '%s'.",
425                                 line.keyword.c_str());
426                         return false;
427                 }
428         }
429
430         return true;
431 }