/
route_config.c
409 lines (376 loc) · 11.6 KB
/
route_config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (C) 2007-2008 1&1 Internet AG
*
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/**
* @file route_config.c
* @brief Functions for load and save routing data from a config file.
*/
#include <confuse.h>
#include "../../mem/shm_mem.h"
#include "../../mem/mem.h"
#include "../../ut.h"
#include "route_config.h"
#include "route.h"
#include "carrierroute.h"
#include "carrier_tree.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
static int save_route_data_recursor(struct route_tree_item * rt, FILE * outfile);
static cfg_t * parse_config();
static int backup_config();
/**
* reports errors during config file parsing using LOG macro
*
* @param cfg points to the current config data structure
* @param fmt a format string
* @param ap format arguments
*/
void conf_error(cfg_t *cfg, const char * fmt, va_list ap) {
/* FIXME this don't seems to work reliable, produces strange error messages */
static char buf[2048];
snprintf(buf, 2048, fmt, ap);
LM_ERR("%s\n", buf);
}
/**
* Loads the routing data from the config file given in global
* variable config_data and stores it in routing tree rd.
*
* @param rd Pointer to the route data tree where the routing data
* shall be loaded into
*
* @return 0 means ok, -1 means an error occurred
*
*/
int load_config(struct rewrite_data * rd) {
cfg_t * cfg = NULL;
int n, m, o, i, j, k,l;
cfg_t * d, * p, * t;
str domain;
str prefix;
double prob;
str rewrite_prefix;
str rewrite_suffix;
str rewrite_host;
str comment;
int backed_up_size = 0;
int * backed_up = NULL;
int backup = 0;
int status, hash_index, max_targets, strip;
if ((cfg = parse_config()) == NULL) {
return -1;
}
if ((rd->carriers = shm_malloc(sizeof(struct carrier_tree *))) == NULL) {
LM_ERR("out of shared memory\n");
return -1;
}
memset(rd->carriers, 0, sizeof(struct carrier_tree *));
rd->tree_num = 1;
n = cfg_size(cfg, "domain");
if (add_carrier_tree(&default_tree, 1, rd, n) == NULL) {
LM_ERR("couldn't add carrier tree\n");
return -1;
}
memset(rd->carriers[0]->trees, 0, sizeof(struct route_tree *) * n);
for (i = 0; i < n; i++) {
d = cfg_getnsec(cfg, "domain", i);
domain.s = (char *)cfg_title(d);
if (domain.s==NULL) domain.s="";
domain.len = strlen(domain.s);
m = cfg_size(d, "prefix");
LM_INFO("loading domain %.*s\n", domain.len, domain.s);
for (j = 0; j < m; j++) {
p = cfg_getnsec(d, "prefix", j);
prefix.s = (char *)cfg_title(p);
if (prefix.s==NULL) prefix.s="";
prefix.len = strlen(prefix.s);
if (str_strcasecmp(&prefix, &SP_EMPTY_PREFIX) == 0) {
prefix.s = "";
prefix.len = 0;
}
LM_INFO("loading prefix %.*s\n", prefix.len, prefix.s);
max_targets = cfg_getint(p, "max_targets");
o = cfg_size(p, "target");
for (k = 0; k < o; k++) {
t = cfg_getnsec(p, "target", k);
rewrite_host.s = (char *)cfg_title(t);
if (rewrite_host.s==NULL) rewrite_host.s="";
rewrite_host.len = strlen(rewrite_host.s);
if (str_strcasecmp(&rewrite_host, &SP_EMPTY_PREFIX) == 0) {
rewrite_host.s = "";
rewrite_host.len = 0;
}
LM_INFO("loading target %.*s\n", rewrite_host.len, rewrite_host.s);
prob = cfg_getfloat(t, "prob");
strip = cfg_getint(t, "strip");
rewrite_prefix.s = (char *)cfg_getstr(t, "rewrite_prefix");
if (rewrite_prefix.s==NULL) rewrite_prefix.s="";
rewrite_prefix.len = strlen(rewrite_prefix.s);
rewrite_suffix.s = (char *)cfg_getstr(t, "rewrite_suffix");
if (rewrite_suffix.s==NULL) rewrite_suffix.s="";
rewrite_suffix.len = strlen(rewrite_suffix.s);
hash_index = cfg_getint(t, "hash_index");
comment.s = (char *)cfg_getstr(t, "comment");
if (comment.s==NULL) comment.s="";
comment.len = strlen(comment.s);
status = cfg_getint(t, "status");
if ((backed_up_size = cfg_size(t, "backed_up")) > 0) {
if ((backed_up = pkg_malloc(sizeof(int) * (backed_up_size + 1))) == NULL) {
LM_ERR("out of private memory\n");
return -1;
}
for (l = 0; l < backed_up_size; l++) {
backed_up[l] = cfg_getnint(t, "backed_up", l);
}
backed_up[backed_up_size] = -1;
}
backup = cfg_getint(t, "backup");
LM_INFO("adding route for prefix %.*s, to host %.*s, prob %f, backed up: %i, backup: %i\n",
prefix.len, prefix.s, rewrite_host.len, rewrite_host.s, prob, backed_up_size, backup);
if (add_route(rd, 1, &domain, &prefix, 0, 0, max_targets, prob, &rewrite_host,
strip, &rewrite_prefix, &rewrite_suffix, status,
hash_index, backup, backed_up, &comment) < 0) {
LM_INFO("Error while adding route\n");
if (backed_up) {
pkg_free(backed_up);
}
return -1;
}
if (backed_up) {
pkg_free(backed_up);
}
backed_up = NULL;
}
}
}
cfg_free(cfg);
return 0;
}
/**
* Parses the config file
*
* @return a pointer to the configuration data structure, NULL on failure
*/
static cfg_t * parse_config(void) {
cfg_t * cfg = NULL;
cfg_opt_t target_opts[] = {
CFG_STR("comment", 0, CFGF_NONE),
CFG_INT("strip", 0, CFGF_NONE),
CFG_STR("rewrite_prefix", 0, CFGF_NONE),
CFG_FLOAT("prob", 0, CFGF_NONE),
CFG_INT("hash_index", 0, CFGF_NONE),
CFG_STR("rewrite_suffix", 0, CFGF_NONE),
CFG_INT("status", 1, CFGF_NONE),
CFG_INT_LIST("backed_up", NULL, CFGF_NONE),
CFG_INT("backup", -1, CFGF_NONE),
CFG_END()
};
cfg_opt_t prefix_opts[] = {
CFG_SEC("target", target_opts, CFGF_MULTI | CFGF_TITLE),
CFG_INT("max_targets", -1, CFGF_NONE),
CFG_END()
};
cfg_opt_t domain_opts[] = {
CFG_SEC("prefix", prefix_opts, CFGF_MULTI | CFGF_TITLE),
CFG_END()
};
cfg_opt_t opts[] = {
CFG_SEC("domain", domain_opts, CFGF_MULTI | CFGF_TITLE),
CFG_END()
};
cfg = cfg_init(opts, CFGF_NONE);
cfg_set_error_function(cfg, conf_error);
switch (cfg_parse(cfg, config_file)) {
case CFG_FILE_ERROR: LM_ERR("file not found: %s\n", config_file);
return NULL;
case CFG_PARSE_ERROR: LM_ERR("error while parsing %s in line %i, section %s\n",
cfg->filename, cfg->line, cfg->name);
return NULL;
case CFG_SUCCESS: break;
}
return cfg;
}
/**
* Stores the routing data rd in config_file
*
* @param rd Pointer to the routing tree which shall be saved to file
*
* @return 0 means ok, -1 means an error occurred
*/
int save_config(struct rewrite_data * rd) {
FILE * outfile;
int i,j;
if(backup_config() < 0){
return -1;
}
if ((outfile = fopen(config_file, "w")) == NULL) {
LM_ERR("Could not open config file %s\n", config_file);
return -1;
}
i = 0;
if (rd->tree_num>=1) {
for (j=0; j< rd->carriers[i]->tree_num; j++) {
fprintf(outfile, "domain %.*s {\n", rd->carriers[i]->trees[j]->name.len, rd->carriers[i]->trees[j]->name.s);
if (save_route_data_recursor(rd->carriers[i]->trees[j]->tree, outfile) < 0) {
goto errout;
}
fprintf(outfile, "}\n\n");
}
}
fclose(outfile);
return 0;
errout:
fclose(outfile);
LM_ERR("Cannot save config file %s\n", config_file);
return -1;
}
/**
* Does the work for save_config, traverses the routing data tree
* and writes each rule to file.
*
* @param rt the current route tree node
* @param outfile the filehandle to which the config data is written
*
* @return 0 on success, -1 on failure
*/
static int save_route_data_recursor(struct route_tree_item * rt, FILE * outfile) {
int i;
struct route_rule * rr;
struct route_rule_p_list * rl;
str *tmp_str;
str null_str = str_init("NULL");
/* no support for flag lists in route config */
if (rt->flag_list && rt->flag_list->rule_list) {
rr = rt->flag_list->rule_list;
tmp_str = (rr->prefix.len ? &rr->prefix : &null_str);
fprintf(outfile, "\tprefix %.*s {\n", tmp_str->len, tmp_str->s);
fprintf(outfile, "\t\tmax_targets = %i\n\n", rt->flag_list->max_targets);
while (rr) {
tmp_str = (rr->host.len ? &rr->host : &null_str);
fprintf(outfile, "\t\ttarget %.*s {\n", tmp_str->len, tmp_str->s);
fprintf(outfile, "\t\t\tprob = %f\n", rr->orig_prob);
fprintf(outfile, "\t\t\thash_index = %i\n", rr->hash_index);
fprintf(outfile, "\t\t\tstatus = %i\n", rr->status);
if (rr->strip > 0) {
fprintf(outfile, "\t\t\tstrip = \"%i\"\n", rr->strip);
}
if (rr->local_prefix.len) {
fprintf(outfile, "\t\t\trewrite_prefix = \"%.*s\"\n", rr->local_prefix.len, rr->local_prefix.s);
}
if (rr->local_suffix.len) {
fprintf(outfile, "\t\t\trewrite_suffix: \"%.*s\"\n", rr->local_suffix.len, rr->local_suffix.s);
}
if (rr->backup) {
fprintf(outfile, "\t\t\tbackup = %i\n", rr->backup->hash_index);
}
if (rr->backed_up) {
rl = rr->backed_up;
fprintf(outfile, "\t\t\tbacked_up = {");
i=0;
while (rl) {
if (i>0) {
fprintf(outfile, ", ");
}
fprintf(outfile, "%i", rl->hash_index);
rl = rl->next;
i++;
}
fprintf(outfile, "}\n");
}
if (rr->comment.len) {
fprintf(outfile, "\t\t\tcomment = \"%.*s\"\n", rr->comment.len, rr->comment.s);
}
fprintf(outfile, "\t\t}\n");
rr = rr->next;
}
fprintf(outfile, "\t}\n");
}
for (i = 0; i < 10; i++) {
if (rt->nodes[i]) {
if (save_route_data_recursor(rt->nodes[i], outfile) < 0) {
return -1;
}
}
}
return 0;
}
static int backup_config(void) {
FILE * from = NULL, * to = NULL;
char * backup_file;
int ch;
int ret = -1;
LM_INFO("start configuration backup\n");
if((backup_file = pkg_malloc(strlen(config_file) + strlen (".bak") + 1)) == NULL){
LM_ERR("out of private memory\n");
return -1;
}
if(!strcpy(backup_file, config_file)){
LM_ERR("can't copy filename\n");
goto errout;
}
if(!strcat(backup_file, ".bak")){
LM_ERR("can't attach suffix\n");
goto errout;
}
/* open source file */
if ((from = fopen(config_file, "rb"))==NULL) {
LM_ERR("Cannot open source file.\n");
goto errout;
}
/* open destination file */
if ((to = fopen(backup_file, "wb"))==NULL) {
LM_ERR("Cannot open destination file.\n");
goto errout;
}
/* copy the file */
while (!feof(from)) {
ch = fgetc(from);
if (ferror(from)) {
LM_ERR("Error reading source file.\n");
goto errout;
}
if (!feof(from)) fputc(ch, to);
if (ferror(to)) {
LM_ERR("Error writing destination file.\n");
goto errout;
}
}
if (fclose(from)==EOF) {
LM_ERR("Error closing source file.\n");
goto close_to;
}
if (fclose(to)==EOF) {
LM_ERR("Error closing destination file.\n");
goto exit;
}
LM_NOTICE("backup written to %s\n", backup_file);
ret = 0;
goto exit;
errout:
if (from)
fclose(from);
close_to:
if (to)
fclose(to);
exit:
pkg_free(backup_file);
return ret;
}