]> git.sesse.net Git - cubemap/blob - config.cpp
Fix an issue where new UDP streams would be without HTTP headers.
[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 encoding.
211         map<string, string>::const_iterator encoding_parm_it = line.parameters.find("encoding");
212         if (encoding_parm_it == line.parameters.end() ||
213             encoding_parm_it->second == "raw") {
214                 stream.encoding = StreamConfig::STREAM_ENCODING_RAW;
215         } else if (encoding_parm_it->second == "metacube") {
216                 stream.encoding = StreamConfig::STREAM_ENCODING_METACUBE;
217         } else {
218                 log(ERROR, "Parameter 'encoding' must be either 'raw' (default) or 'metacube'");
219                 return false;
220         }
221
222         // Parse marks, if so desired.
223         map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
224         if (mark_parm_it == line.parameters.end()) {
225                 stream.mark_pool = -1;
226         } else {
227                 int from, to;
228                 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
229                         return false;
230                 }
231                 stream.mark_pool = allocate_mark_pool(from, to, config);
232         }
233
234         config->streams.push_back(stream);
235         return true;
236 }
237
238 bool parse_error_log(const ConfigLine &line, Config *config)
239 {
240         if (line.arguments.size() != 0) {
241                 log(ERROR, "'error_log' takes no arguments (only parameters type= and filename=)");
242                 return false;
243         }
244
245         LogConfig log_config;
246         map<string, string>::const_iterator type_it = line.parameters.find("type");
247         if (type_it == line.parameters.end()) {
248                 log(ERROR, "'error_log' has no type= parameter");
249                 return false; 
250         }
251
252         string type = type_it->second;
253         if (type == "file") {
254                 log_config.type = LogConfig::LOG_TYPE_FILE;
255         } else if (type == "syslog") {
256                 log_config.type = LogConfig::LOG_TYPE_SYSLOG;
257         } else if (type == "console") {
258                 log_config.type = LogConfig::LOG_TYPE_CONSOLE;
259         } else {
260                 log(ERROR, "Unknown log type '%s'", type.c_str());
261                 return false; 
262         }
263
264         if (log_config.type == LogConfig::LOG_TYPE_FILE) {
265                 map<string, string>::const_iterator filename_it = line.parameters.find("filename");
266                 if (filename_it == line.parameters.end()) {
267                         log(ERROR, "error_log type 'file' with no filename= parameter");
268                         return false; 
269                 }
270                 log_config.filename = filename_it->second;
271         }
272
273         config->log_destinations.push_back(log_config);
274         return true;
275 }
276
277 bool parse_config(const string &filename, Config *config)
278 {
279         vector<ConfigLine> lines;
280         if (!read_config(filename, &lines)) {
281                 return false;
282         }
283
284         config->daemonize = false;
285
286         if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
287                 log(ERROR, "Missing 'num_servers' statement in config file.");
288                 return false;
289         }
290         if (config->num_servers < 1 || config->num_servers >= 20000) {  // Insanely high max limit.
291                 log(ERROR, "'num_servers' is %d, needs to be in [1, 20000>.", config->num_servers);
292                 return false;
293         }
294
295         // See if the user wants stats.
296         config->stats_interval = 60;
297         bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
298         bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
299         if (has_stats_interval && !has_stats_file) {
300                 log(WARNING, "'stats_interval' given, but no 'stats_file'. No statistics will be written.");
301         }
302         
303         fetch_config_string(lines, "access_log", &config->access_log_file);
304
305         for (size_t i = 0; i < lines.size(); ++i) {
306                 const ConfigLine &line = lines[i];
307                 if (line.keyword == "num_servers" ||
308                     line.keyword == "stats_file" ||
309                     line.keyword == "stats_interval" ||
310                     line.keyword == "access_log") {
311                         // Already taken care of, above.
312                 } else if (line.keyword == "port") {
313                         if (!parse_port(line, config)) {
314                                 return false;
315                         }
316                 } else if (line.keyword == "stream") {
317                         if (!parse_stream(line, config)) {
318                                 return false;
319                         }
320                 } else if (line.keyword == "error_log") {
321                         if (!parse_error_log(line, config)) {
322                                 return false;
323                         }
324                 } else if (line.keyword == "daemonize") {
325                         config->daemonize = true;
326                 } else {
327                         log(ERROR, "Unknown configuration keyword '%s'.",
328                                 line.keyword.c_str());
329                         return false;
330                 }
331         }
332
333         return true;
334 }