]> git.sesse.net Git - cubemap/blob - config.cpp
Update the VLC Metacube patch to apply against current VLC master.
[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         // Parse the pacing rate, converting from kilobits to bytes as needed.
238         map<string, string>::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit");
239         if (pacing_rate_it == line.parameters.end()) {
240                 stream.pacing_rate = ~0U;
241         } else {
242                 stream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
243         }
244
245         config->streams.push_back(stream);
246         return true;
247 }
248
249 bool parse_udpstream(const ConfigLine &line, Config *config)
250 {
251         if (line.arguments.size() != 1) {
252                 log(ERROR, "'udpstream' takes exactly one argument");
253                 return false;
254         }
255
256         UDPStreamConfig udpstream;
257
258         string hostport = line.arguments[0];
259
260         // See if the argument if on the type [ipv6addr]:port.
261         if (!hostport.empty() && hostport[0] == '[') {
262                 size_t split = hostport.find("]:");
263                 if (split == string::npos) {
264                         log(ERROR, "udpstream destination '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
265                         return false;
266                 }
267
268                 string host(hostport.begin() + 1, hostport.begin() + split);
269                 string port = hostport.substr(split + 2);
270
271                 udpstream.dst.sin6_family = AF_INET6;
272                 if (inet_pton(AF_INET6, host.c_str(), &udpstream.dst.sin6_addr) != 1) {
273                         log(ERROR, "udpstream destination host '%s' is not a valid IPv6 address");
274                         return false;
275                 }
276
277                 udpstream.dst.sin6_port = htons(atoi(port.c_str()));  // TODO: Verify validity.
278         } else {
279                 // OK, then it must be ipv4addr:port.
280                 size_t split = hostport.find(":");
281                 if (split == string::npos) {
282                         log(ERROR, "udpstream destination '%s' is malformed; must be either [ipv6addr]:port or ipv4addr:port");
283                         return false;
284                 }
285
286                 string host(hostport.begin(), hostport.begin() + split);
287                 string port = hostport.substr(split + 1);
288
289                 // Parse to an IPv4 address, then construct a mapped-v4 address from that.
290                 in_addr addr4;
291
292                 if (inet_pton(AF_INET, host.c_str(), &addr4) != 1) {
293                         log(ERROR, "udpstream destination host '%s' is not a valid IPv4 address");
294                         return false;
295                 }
296
297                 udpstream.dst.sin6_family = AF_INET6;
298                 udpstream.dst.sin6_addr.s6_addr32[0] = 0;
299                 udpstream.dst.sin6_addr.s6_addr32[1] = 0;
300                 udpstream.dst.sin6_addr.s6_addr32[2] = htonl(0xffff);
301                 udpstream.dst.sin6_addr.s6_addr32[3] = addr4.s_addr;
302                 udpstream.dst.sin6_port = htons(atoi(port.c_str()));  // TODO: Verify validity.
303         }
304
305         map<string, string>::const_iterator src_it = line.parameters.find("src");
306         if (src_it == line.parameters.end()) {
307                 // This is pretty meaningless, but OK, consistency is good.
308                 log(WARNING, "udpstream to %s has no src= attribute, clients will not get any data.",
309                         hostport.c_str());
310         } else {
311                 udpstream.src = src_it->second;
312                 // TODO: Verify that the URL is parseable?
313         }
314
315         // Parse marks, if so desired.
316         map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
317         if (mark_parm_it == line.parameters.end()) {
318                 udpstream.mark_pool = -1;
319         } else {
320                 int from, to;
321                 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
322                         return false;
323                 }
324                 udpstream.mark_pool = allocate_mark_pool(from, to, config);
325         }
326
327         // Parse the pacing rate, converting from kilobits to bytes as needed.
328         map<string, string>::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit");
329         if (pacing_rate_it == line.parameters.end()) {
330                 udpstream.pacing_rate = ~0U;
331         } else {
332                 udpstream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8;
333         }
334
335         config->udpstreams.push_back(udpstream);
336         return true;
337 }
338
339 bool parse_error_log(const ConfigLine &line, Config *config)
340 {
341         if (line.arguments.size() != 0) {
342                 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
343                 return false;
344         }
345
346         LogConfig log_config;
347         map<string, string>::const_iterator type_it = line.parameters.find("type");
348         if (type_it == line.parameters.end()) {
349                 log(ERROR, "'error_log' has no type= parameter");
350                 return false; 
351         }
352
353         string type = type_it->second;
354         if (type == "file") {
355                 log_config.type = LogConfig::LOG_TYPE_FILE;
356         } else if (type == "syslog") {
357                 log_config.type = LogConfig::LOG_TYPE_SYSLOG;
358         } else if (type == "console") {
359                 log_config.type = LogConfig::LOG_TYPE_CONSOLE;
360         } else {
361                 log(ERROR, "Unknown log type '%s'", type.c_str());
362                 return false; 
363         }
364
365         if (log_config.type == LogConfig::LOG_TYPE_FILE) {
366                 map<string, string>::const_iterator filename_it = line.parameters.find("filename");
367                 if (filename_it == line.parameters.end()) {
368                         log(ERROR, "error_log type 'file' with no filename= parameter");
369                         return false; 
370                 }
371                 log_config.filename = filename_it->second;
372         }
373
374         config->log_destinations.push_back(log_config);
375         return true;
376 }
377
378 bool parse_config(const string &filename, Config *config)
379 {
380         vector<ConfigLine> lines;
381         if (!read_config(filename, &lines)) {
382                 return false;
383         }
384
385         config->daemonize = false;
386
387         if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
388                 log(ERROR, "Missing 'num_servers' statement in config file.");
389                 return false;
390         }
391         if (config->num_servers < 1 || config->num_servers >= 20000) {  // Insanely high max limit.
392                 log(ERROR, "'num_servers' is %d, needs to be in [1, 20000>.", config->num_servers);
393                 return false;
394         }
395
396         // See if the user wants stats.
397         config->stats_interval = 60;
398         bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
399         bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
400         if (has_stats_interval && !has_stats_file) {
401                 log(WARNING, "'stats_interval' given, but no 'stats_file'. No client statistics will be written.");
402         }
403
404         config->input_stats_interval = 60;
405         bool has_input_stats_file = fetch_config_string(lines, "input_stats_file", &config->input_stats_file);
406         bool has_input_stats_interval = fetch_config_int(lines, "input_stats_interval", &config->input_stats_interval);
407         if (has_input_stats_interval && !has_input_stats_file) {
408                 log(WARNING, "'input_stats_interval' given, but no 'input_stats_file'. No input statistics will be written.");
409         }
410         
411         fetch_config_string(lines, "access_log", &config->access_log_file);
412
413         for (size_t i = 0; i < lines.size(); ++i) {
414                 const ConfigLine &line = lines[i];
415                 if (line.keyword == "num_servers" ||
416                     line.keyword == "stats_file" ||
417                     line.keyword == "stats_interval" ||
418                     line.keyword == "input_stats_file" ||
419                     line.keyword == "input_stats_interval" ||
420                     line.keyword == "access_log") {
421                         // Already taken care of, above.
422                 } else if (line.keyword == "port") {
423                         if (!parse_port(line, config)) {
424                                 return false;
425                         }
426                 } else if (line.keyword == "stream") {
427                         if (!parse_stream(line, config)) {
428                                 return false;
429                         }
430                 } else if (line.keyword == "udpstream") {
431                         if (!parse_udpstream(line, config)) {
432                                 return false;
433                         }
434                 } else if (line.keyword == "error_log") {
435                         if (!parse_error_log(line, config)) {
436                                 return false;
437                         }
438                 } else if (line.keyword == "daemonize") {
439                         config->daemonize = true;
440                 } else {
441                         log(ERROR, "Unknown configuration keyword '%s'.",
442                                 line.keyword.c_str());
443                         return false;
444                 }
445         }
446
447         return true;
448 }