]> git.sesse.net Git - cubemap/blob - config.cpp
Implement --test-config.
[cubemap] / config.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <limits.h>
5 #include <ctype.h>
6 #include <assert.h>
7 #include <map>
8 #include <vector>
9 #include <string>
10
11 #include "config.h"
12 #include "parse.h"
13
14 using namespace std;
15
16 struct ConfigLine {
17         string keyword;
18         vector<string> arguments;
19         map<string, string> parameters;
20 };
21
22 bool read_config(const string &filename, vector<ConfigLine> *lines)
23 {
24         FILE *fp = fopen(filename.c_str(), "r");
25         if (fp == NULL) {
26                 perror(filename.c_str());
27                 return false;
28         }
29
30         char buf[4096];
31         while (!feof(fp)) {
32                 if (fgets(buf, sizeof(buf), fp) == NULL) {
33                         break;
34                 }
35
36                 // Chop off the string at the first #, \r or \n.
37                 buf[strcspn(buf, "#\r\n")] = 0;
38
39                 // Remove all whitespace from the end of the string.
40                 size_t len = strlen(buf);
41                 while (len > 0 && isspace(buf[len - 1])) {
42                         buf[--len] = 0;
43                 }
44
45                 // If the line is now all blank, ignore it.
46                 if (len == 0) {
47                         continue;
48                 }
49
50                 vector<string> tokens = split_tokens(buf);
51                 assert(!tokens.empty());
52                 
53                 ConfigLine line;
54                 line.keyword = tokens[0];
55
56                 for (size_t i = 1; i < tokens.size(); ++i) {
57                         // foo=bar is a parameter; anything else is an argument.
58                         size_t equals_pos = tokens[i].find_first_of('=');
59                         if (equals_pos == string::npos) {
60                                 line.arguments.push_back(tokens[i]);
61                         } else {
62                                 string key = tokens[i].substr(0, equals_pos);
63                                 string value = tokens[i].substr(equals_pos + 1, string::npos);
64                                 line.parameters.insert(make_pair(key, value));
65                         }
66                 }
67
68                 lines->push_back(line);
69         }
70
71         fclose(fp);
72         return true;
73 }
74
75 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
76 {
77         for (unsigned i = 0; i < config.size(); ++i) {
78                 if (config[i].keyword != keyword) {
79                         continue;
80                 }
81                 if (config[i].parameters.size() > 0 ||
82                     config[i].arguments.size() != 1) {
83                         fprintf(stderr, "ERROR: '%s' takes one argument and no parameters\n", keyword.c_str());
84                         return false;
85                 }
86                 *value = config[i].arguments[0];
87                 return true;
88         }
89         return false;
90 }
91
92 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
93 {
94         for (unsigned i = 0; i < config.size(); ++i) {
95                 if (config[i].keyword != keyword) {
96                         continue;
97                 }
98                 if (config[i].parameters.size() > 0 ||
99                     config[i].arguments.size() != 1) {
100                         fprintf(stderr, "ERROR: '%s' takes one argument and no parameters\n", keyword.c_str());
101                         return false;
102                 }
103                 *value = atoi(config[i].arguments[0].c_str());  // TODO: verify int validity.
104                 return true;
105         }
106         return false;
107 }
108
109 bool parse_port(const ConfigLine &line, Config *config)
110 {
111         if (line.arguments.size() != 1) {
112                 fprintf(stderr, "ERROR: 'port' takes exactly one argument\n");
113                 return false;
114         }
115
116         AcceptorConfig acceptor;
117         acceptor.port = atoi(line.arguments[0].c_str());
118         if (acceptor.port < 1 || acceptor.port >= 65536) {
119                 fprintf(stderr, "ERROR: port %d is out of range (must be [1,65536>).\n", acceptor.port);
120                 return false;
121         }
122
123         config->acceptors.push_back(acceptor);
124         return true;
125 }
126
127 int allocate_mark_pool(int from, int to, Config *config)
128 {
129         int pool_index = -1;    
130
131         // Reuse mark pools if an identical one exists.
132         // Otherwise, check if we're overlapping some other mark pool.
133         for (size_t i = 0; i < config->mark_pools.size(); ++i) {
134                 const MarkPoolConfig &pool = config->mark_pools[i];
135                 if (from == pool.from && to == pool.to) {
136                         pool_index = i;
137                 } else if ((from >= pool.from && from < pool.to) ||
138                            (to >= pool.from && to < pool.to)) {
139                         fprintf(stderr, "WARNING: Mark pool %d-%d partially overlaps with %d-%d, you may get duplicate marks.\n",
140                                         from, to, pool.from, pool.to);
141                         fprintf(stderr, "         Mark pools must either be completely disjunct, or completely overlapping.\n");
142                 }
143         }
144
145         if (pool_index != -1) {
146                 return pool_index;
147         }
148
149         // No match to existing pools.
150         MarkPoolConfig pool;
151         pool.from = from;
152         pool.to = to;
153         config->mark_pools.push_back(pool);
154
155         return config->mark_pools.size() - 1;
156 }
157
158 bool parse_mark_pool(const string &mark_str, int *from, int *to)
159 {
160         size_t split = mark_str.find_first_of('-');
161         if (split == string::npos) {
162                 fprintf(stderr, "ERROR: Invalid mark specification '%s' (expected 'X-Y').\n",
163                         mark_str.c_str());
164                 return false;
165         }
166
167         string from_str(mark_str.begin(), mark_str.begin() + split);
168         string to_str(mark_str.begin() + split + 1, mark_str.end());
169         *from = atoi(from_str.c_str());
170         *to = atoi(to_str.c_str());
171
172         if (*from <= 0 || *from >= 65536 || *to <= 0 || *to >= 65536) {
173                 fprintf(stderr, "ERROR: Mark pool range %d-%d is outside legal range [1,65536>.\n",
174                         *from, *to);
175                 return false;
176         }
177
178         return true;
179 }
180
181 bool parse_stream(const ConfigLine &line, Config *config)
182 {
183         if (line.arguments.size() != 1) {
184                 fprintf(stderr, "ERROR: 'stream' takes exactly one argument\n");
185                 return false;
186         }
187
188         StreamConfig stream;
189         stream.stream_id = line.arguments[0];
190
191         map<string, string>::const_iterator src_it = line.parameters.find("src");
192         if (src_it == line.parameters.end()) {
193                 fprintf(stderr, "WARNING: stream '%s' has no src= attribute, clients will not get any data.\n",
194                         stream.stream_id.c_str());
195         } else {
196                 stream.src = src_it->second;
197                 // TODO: Verify that the URL is parseable?
198         }
199
200         // Parse marks, if so desired.
201         map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
202         if (mark_parm_it == line.parameters.end()) {
203                 stream.mark_pool = -1;
204         } else {
205                 int from, to;
206                 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
207                         return false;
208                 }
209                 stream.mark_pool = allocate_mark_pool(from, to, config);
210         }
211
212         config->streams.push_back(stream);
213         return true;
214 }
215
216 bool parse_config(const string &filename, Config *config)
217 {
218         vector<ConfigLine> lines;
219         if (!read_config(filename, &lines)) {
220                 return false;
221         }
222
223         if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
224                 fprintf(stderr, "ERROR: Missing 'num_servers' statement in config file.\n");
225                 return false;
226         }
227         if (config->num_servers < 1 || config->num_servers >= 20000) {  // Insanely high max limit.
228                 fprintf(stderr, "ERROR: 'num_servers' is %d, needs to be in [1, 20000>.\n", config->num_servers);
229                 return false;
230         }
231
232         // See if the user wants stats.
233         config->stats_interval = 60;
234         bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
235         bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
236         if (has_stats_interval && !has_stats_file) {
237                 fprintf(stderr, "WARNING: 'stats_interval' given, but no 'stats_file'. No statistics will be written.\n");
238         }
239
240         for (size_t i = 0; i < lines.size(); ++i) {
241                 const ConfigLine &line = lines[i];
242                 if (line.keyword == "num_servers" ||
243                     line.keyword == "stats_file" ||
244                     line.keyword == "stats_interval") {
245                         // Already taken care of, above.
246                 } else if (line.keyword == "port") {
247                         if (!parse_port(line, config)) {
248                                 return false;
249                         }
250                 } else if (line.keyword == "stream") {
251                         if (!parse_stream(line, config)) {
252                                 return false;
253                         }
254                 } else {
255                         fprintf(stderr, "ERROR: Unknown configuration keyword '%s'.\n",
256                                 line.keyword.c_str());
257                         return false;
258                 }
259         }
260
261         return true;
262 }