/
ds_fixups.c
477 lines (399 loc) · 9.29 KB
/
ds_fixups.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/**
* dispatcher module fixup functions
*
* Copyright (C) 2004-2005 FhG Fokus
* Copyright (C) 2006-2010 Voice Sistem SRL
* Copyright (C) 2014 OpenSIPS Foundation
*
* 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
*
*/
#include "ds_fixups.h"
#include "../../trim.h"
#define LIST_DELIM ','
#define FLAGS_DELIM 'M'
#define DS_TYPE_INT 0
#define DS_TYPE_PVS 1
extern ds_partition_t *default_partition;
extern ds_partition_t *partitions;
int fixup_ds_part(void **param)
{
ds_partition_t *it;
str *part = (str *)*param;
if (!part) {
*param = default_partition;
return 0;
}
for (it = partitions; it; it = it->next)
if (!str_strcmp(&it->name, part)) {
*param = it;
return 0;
}
LM_ERR("could not locate partition %.*s\n", part->len, part->s);
*param = NULL;
return -1;
}
/*
* Expand a pvar into a list of ints
*/
int_list_t *set_list_from_pvs(struct sip_msg *msg, pv_spec_t *pvs, int_list_t *end)
{
int_list_t *result = end, *new_el;
pv_value_t value;
if (pv_get_spec_value(msg, pvs, &value) != 0 || value.flags&PV_VAL_NULL
|| (!(value.flags&PV_VAL_INT) && !(value.flags&PV_VAL_STR))) {
LM_ERR("no valid PV value found (error in scripts)\n");
return NULL;
}
if (value.flags & PV_VAL_INT) {
/* Just one element */
new_el = pkg_malloc(sizeof(int_list_t));
if (new_el == NULL) {
LM_ERR("no more shared memory\n");
return NULL;
}
new_el->v.ival = value.ri;
new_el->type = DS_TYPE_INT;
new_el->next = end;
return new_el;
}
str sval = value.rs;
if (sval.s == NULL)
goto wrong_value;
char * delim;
do{
delim = q_memchr(sval.s, LIST_DELIM, sval.len);
str s_num = {sval.s, delim ? delim - sval.s : sval.len};
sval.len -= s_num.len + 1;
sval.s = delim + 1;
trim(&s_num);
int u_num;
if (s_num.len == 0 || str2sint(&s_num, &u_num) != 0)
goto wrong_value;
new_el = pkg_malloc(sizeof(int_list_t));
if (new_el == NULL) {
goto no_memory;
}
new_el->v.ival = u_num;
new_el->type = DS_TYPE_INT;
new_el->next = result;
result = new_el;
} while (delim);
if (sval.len > 0)
goto wrong_value;
return result;
no_memory:
while(result != end) {
if (result->type == DS_TYPE_PVS)
pkg_free(result->v.pvs);
int_list_t *aux = result;
result = result->next;
pkg_free(aux);
}
LM_ERR("no more private memory\n");
return NULL;
wrong_value:
while(result != end) {
if (result->type == DS_TYPE_PVS)
pkg_free(result->v.pvs);
int_list_t *aux = result;
result = result->next;
pkg_free(aux);
}
LM_ERR("wrong var value <%.*s>\n", value.rs.len, value.rs.s);
return NULL;
}
/*
* Create an int list from a string. Eg ("1, 2, 4")
*/
int set_list_from_string(str input, int_list_t **result)
{
str original_input = input;
int_list_t *new_el=NULL;
int flags=0;
int uset;
*result = NULL;
if (input.s == NULL || input.len == 0)
return 0;
if (str2sint(&input, &uset) == 0) {
/* Just one set in the list */
*result = pkg_malloc(sizeof(int_list_t));
if (*result == NULL)
goto no_memory;
(*result)->v.ival = uset;
(*result)->type = DS_TYPE_INT;
(*result)->next = NULL;
return 0;
}
char * delim, *pvdelim, *flagsdelim=NULL;
str flg_tok;
unsigned int u_num=0;
int def_val = -1;
do{
delim = q_memchr(input.s, LIST_DELIM, input.len);
str s_tok = {input.s, delim ? delim - input.s : input.len};
int full_tok_len = s_tok.len;
trim(&s_tok);
/* search if only max results */
if (s_tok.s[0] >= '0' && s_tok.s[0] <= '9') {
flags = 0;
goto only_max_res;
}
/*search for flags flags/maxlist delimiter*/
flagsdelim=q_memchr(s_tok.s, FLAGS_DELIM, s_tok.len);
if (flagsdelim == NULL) {
/* search for only flags */
if ((s_tok.s[0] >= 'a' && s_tok.s[0] <= 'z') ||
(s_tok.s[0] >= 'A' && s_tok.s[0] <= 'Z')) {
flg_tok.s = s_tok.s;
flg_tok.len=0;
if ((flg_tok.s[flg_tok.len] >= 'a' && flg_tok.s[flg_tok.len] <= 'z') ||
(flg_tok.s[flg_tok.len] >= 'A' && flg_tok.s[flg_tok.len] <= 'Z'))
flg_tok.len=s_tok.len;
goto only_flags00;
}
}
/* if found parse the flags */
if (flagsdelim != NULL) {
flg_tok.s = s_tok.s;
flg_tok.len = flagsdelim - s_tok.s;
only_flags00:
/* update list token */
s_tok.s += flg_tok.len +1;
s_tok.len -= (flg_tok.len +1);
new_el = pkg_malloc(sizeof(int_list_t));
if (new_el == NULL)
goto no_memory;
memset(new_el, 0, sizeof(int_list_t));
trim(&flg_tok);
/* must fixup flags string value */
if ((flags = fixup_flags(&flg_tok)) < 0) {
LM_ERR("cannot fixup flags\n");
return -1;
}
trim(&s_tok);
/* default value for max results */
def_val = 1000;
}
only_max_res:
if (s_tok.len == 0) {
if (flags > 0) {
goto only_flags01;
} else
goto wrong_value;
}
else if (s_tok.s[0] == PV_MARKER) {
if (new_el == NULL) {
new_el = pkg_malloc(sizeof(int_list_t));
if (new_el == NULL)
goto no_memory;
}
new_el->type = DS_TYPE_PVS;
new_el->v.pvs = pkg_malloc(sizeof(pv_spec_t));
if (new_el->v.pvs == NULL) {
pkg_free(new_el);
goto no_memory;
}
if ((pvdelim = pv_parse_spec(&s_tok, new_el->v.pvs)) == NULL) {
pkg_free(new_el->v.pvs);
pkg_free(new_el);
goto wrong_value;
}
new_el->next = *result;
*result = new_el;
new_el = NULL;
if (delim)
if (delim != pvdelim)
goto wrong_value;
else {
input.len -= delim - input.s + 1;
input.s = delim + 1;
}
else {
input.len -= pvdelim - input.s + 1;
input.s = pvdelim;
}
}
else if (str2int(&s_tok, &u_num) == 0) {
/*
* don't alloc twice
* if both flags and max_results defined
* it is already allocated
*
*/
if (new_el == NULL) {
new_el = pkg_malloc(sizeof(int_list_t));
if (new_el == NULL)
goto no_memory;
}
only_flags01:
new_el->v.ival = def_val > 0 ? def_val : u_num;
new_el->type = DS_TYPE_INT;
if (flags>0)
new_el->flags = flags;
new_el->next = *result;
*result = new_el;
new_el = NULL;
input.len -= full_tok_len + 1;
input.s = delim + 1;
}
else goto wrong_value;
} while (delim);
if (input.len > 0)
goto wrong_value;
return 0;
no_memory:
while(*result) {
if ((*result)->type == DS_TYPE_PVS)
pkg_free((*result)->v.pvs);
int_list_t *aux = *result;
*result = (*result)->next;
pkg_free(aux);
}
LM_ERR("no more shared memory\n");
return -1;
wrong_value:
while(*result) {
if ((*result)->type == DS_TYPE_PVS)
pkg_free((*result)->v.pvs);
int_list_t *aux = *result;
*result = (*result)->next;
pkg_free(aux);
}
LM_ERR("wrong format for set/set list. Token <%.*s>\n", original_input.len, original_input.s);
return -1;
}
/*
* Fixup for flags
*/
int fixup_flags(str* param)
{
int index, ret=0;
for (index=0; index < param->len; index++) {
switch (param->s[index]) {
case ' ':
break;
case 'f':
case 'F':
ret |= DS_FAILOVER_ON;
break;
case 'u':
case 'U':
ret |= DS_HASH_USER_ONLY;
break;
case 'd':
case 'D':
ret |= DS_USE_DEFAULT;
break;
case 'a':
case 'A':
ret |= DS_APPEND_MODE;
break;
default:
LM_ERR("Invalid flag: '%c'\n", param->s[index]);
return -1;
}
}
return ret;
}
int fixup_ds_flags(void** param)
{
int index, ret=0;
str *flags = (str *)*param;
for (index=0; index < flags->len; index++) {
switch (flags->s[index]) {
case ' ':
break;
case 'f':
case 'F':
ret |= DS_FAILOVER_ON;
break;
case 'u':
case 'U':
ret |= DS_HASH_USER_ONLY;
break;
case 'd':
case 'D':
ret |= DS_USE_DEFAULT;
break;
case 'a':
case 'A':
ret |= DS_APPEND_MODE;
break;
default:
LM_ERR("Invalid definition\n");
return -1;
}
}
*param = (void *)(long)ret;
return 0;
}
/*
* Free an expanded list (obtained with set_list_from_pvs)
* Delete everything in the range [start, end).
* Do not use this function to erase any other lists
*/
void free_int_list(int_list_t *start, int_list_t *end)
{
int_list_t *aux;
while (start != end) {
aux = start;
start = start->next;
pkg_free(aux);
}
}
/*
* Search for value in int_list_t
*/
int in_int_list(int_list_t *list, int val)
{
int_list_t *tmp;
for (tmp=list;tmp!=NULL;tmp=tmp->next) {
if (tmp->v.ival == val) // TODO: test!
return 0;
}
return -1;
}
int fixup_ds_count_filter(void **param)
{
str *s = (str *)*param;
int i, code = 0;
for (i = 0; i < s->len; i++) {
switch (s->s[i]) {
/* active */
case 'a':
case 'A':
case '1':
code |= DS_COUNT_ACTIVE;
break;
/* inactive */
case 'i':
case 'I':
case '0':
code |= DS_COUNT_INACTIVE;
break;
/* probing */
case 'p':
case 'P':
case '2':
code |= DS_COUNT_PROBING;
break;
}
}
*param = (void *)(long)code;
return 0;
}