/
acc_vars.c
508 lines (409 loc) · 10.4 KB
/
acc_vars.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/**
* Fraud Detection Module
*
* Copyright (C) 2016 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
*
* History
* -------
* 2016-06-23 initial version (Ionut Ionita)
*/
#include <stdio.h>
#include <string.h>
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../mem/mem.h"
#include "../../ut.h"
#include "acc_extra.h"
#include "acc_logic.h"
#define GET_EXTRA_VALUE_SAFE(val_ptr, extra) \
do { \
*val_ptr = extra->value; \
} while(0);
extern int extra_tgs_len;
extern tag_t* extra_tags;
extern int leg_tgs_len;
extern tag_t* leg_tags;
extern int acc_flags_ctx_idx;
/*
* acc_extra
* VARIABLE
* ***********************/
/*
* parse $acc_extra variable name
*/
int pv_parse_acc_extra_name(pv_spec_p sp, str *in)
{
int idx;
if (sp == NULL || in == NULL || in->s == NULL || in->len == 0) {
LM_ERR("bad name!\n");
return -1;
}
str_trim_spaces_lr(*in);
for (idx=0; idx<extra_tgs_len; idx++) {
if (in->len == extra_tags[idx].len &&
!memcmp(in->s, extra_tags[idx].s, extra_tags[idx].len)) {
sp->pvp.pvn.u.isname.name.n = idx;
return 0;
}
}
LM_ERR("tag <%.*s> not declared in modparam section!\n", in->len, in->s);
return -1;
}
/*
* getter function for $acc_extra
*/
int pv_get_acc_extra(struct sip_msg *msg, pv_param_t *param,
pv_value_t *val)
{
int tag_idx;
acc_ctx_t* ctx=try_fetch_ctx();
if (param == NULL || val == NULL) {
LM_ERR("bad input params!\n");
return -1;
}
if (ctx == NULL) {
/* if we don't have a context then create it */
if (init_acc_ctx(&ctx) < 0) {
LM_ERR("failed to create accounting context!\n");
return -1;
}
ACC_PUT_CTX(ctx);
}
tag_idx = param->pvn.u.isname.name.n;
/* sanity checks for the tag; it should be valid since
* we found it in the parse name function */
if (tag_idx < 0 || tag_idx >= extra_tgs_len) {
LM_BUG("invalid tag value! probably a memory corruption issue!\n");
return -1;
}
accX_lock(&ctx->lock);
if (ctx->extra_values[tag_idx].value.s == NULL) {
val->flags = PV_VAL_NULL;
} else {
val->rs = ctx->extra_values[tag_idx].value;
val->flags = PV_VAL_STR;
}
accX_unlock(&ctx->lock);
return 0;
}
/*
* set pv_value_t in pkg to pv_value_t from extra in shm
*
* * if it's an integer then convert it to string and set the string value
* to the shm pv_value_t
* * if it's a string then try converting it to it
*/
int set_value_shm(pv_value_t* pvt, extra_value_t* extra)
{
str s;
if (pvt == NULL || pvt->flags&PV_VAL_NULL) {
if (extra->shm_buf_len) {
shm_free(extra->value.s);
extra->shm_buf_len = 0;
}
extra->value.s = NULL;
extra->value.len = 0;
} else {
if (pvt->flags&PV_VAL_STR) {
s = pvt->rs;
} else if (pvt->flags&PV_VAL_INT) {
s.s = int2str( pvt->ri, &s.len);
} else {
LM_ERR("invalid pvt value!\n");
return -1;
}
if (extra->shm_buf_len == 0) {
extra->value.s = shm_malloc(s.len);
extra->shm_buf_len = extra->value.len = s.len;
} else if (extra->shm_buf_len < s.len) {
extra->value.s = shm_realloc(extra->value.s, s.len);
extra->shm_buf_len = extra->value.len = s.len;
} else {
extra->value.len = s.len;
}
if (extra->value.s == NULL)
goto memerr;
memcpy(extra->value.s, s.s, s.len);
}
return 0;
memerr:
LM_ERR("No more shm!\n");
return -1;
}
/*
* setter function for $acc_extra
*/
int pv_set_acc_extra(struct sip_msg *msg, pv_param_t *param, int op,
pv_value_t *val)
{
int tag_idx;
acc_ctx_t* ctx=try_fetch_ctx();
if (param == NULL) {
LM_ERR("bad params!\n");
return -1;
}
if (ctx == NULL) {
/* if we don't have a context then create it */
if (init_acc_ctx(&ctx) < 0) {
LM_ERR("failed to create accounting context!\n");
return -1;
}
ACC_PUT_CTX(ctx);
}
tag_idx = param->pvn.u.isname.name.n;
/* sanity checks for the tag; it should be valid since
* we found it in the parse name function */
if (tag_idx < 0 || tag_idx >= extra_tgs_len) {
LM_BUG("invalid tag value! probably a memory corruption issue!\n");
return -1;
}
/* go through all extras and fetch first value you find
* all the extras with the same tag will have the same
* value */
accX_lock(&ctx->lock);
if (set_value_shm(val, &ctx->extra_values[tag_idx]) < 0) {
LM_ERR("failed to set extra <%.*s> value!\n",
extra_tags[tag_idx].len, extra_tags[tag_idx].s);
accX_unlock(&ctx->lock);
return -1;
}
accX_unlock(&ctx->lock);
return 0;
}
/*
* acc_current_leg
* VARIABLE
* ***********************/
int pv_get_acc_current_leg(struct sip_msg *msg, pv_param_t *param,
pv_value_t *val)
{
acc_ctx_t* ctx=try_fetch_ctx();
if (ctx == NULL) {
/* if we don't have a context then create it */
if (init_acc_ctx(&ctx) < 0) {
LM_ERR("failed to create accounting context!\n");
return -1;
}
ACC_PUT_CTX(ctx);
}
if (ctx->leg_values == NULL) {
LM_ERR("no legs defined!\n");
return -1;
}
accX_lock(&ctx->lock);
val->ri = ctx->legs_no - 1;
val->rs.s = int2str(ctx->legs_no - 1, &val->rs.len);
accX_unlock(&ctx->lock);
val->flags = PV_VAL_INT | PV_VAL_STR | PV_TYPE_INT;
return 0;
}
/*
* acc_leg
* VARIABLE
* ***********************/
int pv_parse_acc_leg_index(pv_spec_p sp, str* in)
{
int idx;
pv_spec_p e;
if (in == NULL || in->s == NULL || in->len == 0) {
LM_ERR("bad index!\n");
return -1;
}
if (sp == NULL) {
LM_ERR("bad pv spec!\n");
return -1;
}
str_trim_spaces_lr(*in);
if (in->s[0] == PV_MARKER) {
e=pkg_malloc(sizeof(pv_spec_t));
if (e==NULL) {
LM_ERR("no more pkg mem!\n");
return -1;
}
memset(e, 0, sizeof(pv_spec_t));
if (pv_parse_spec(in, e) == NULL) {
LM_ERR("failed to parse index variable!\n");
pv_spec_free(e);
return -1;
}
sp->pvp.pvi.type = PV_IDX_PVAR;
sp->pvp.pvi.u.dval = (void *)e;
} else {
if (str2sint(in, &idx) < 0) {
LM_ERR("bad index! not a number! <%.*s>!\n", in->len, in->s);
return -1;
}
sp->pvp.pvi.type = PV_IDX_INT;
sp->pvp.pvi.u.ival = idx;
}
return 0;
}
/*
* parse $acc_leg variable name
*/
int pv_parse_acc_leg_name(pv_spec_p sp, str *in)
{
int idx;
if (sp == NULL || in == NULL || in->s == NULL || in->len == 0) {
LM_ERR("bad name!\n");
return -1;
}
str_trim_spaces_lr(*in);
for (idx=0; idx<leg_tgs_len; idx++) {
if (in->len == leg_tags[idx].len &&
!memcmp(in->s, leg_tags[idx].s, leg_tags[idx].len)) {
sp->pvp.pvn.u.isname.name.n = idx;
return 0;
}
}
LM_ERR("tag <%.*s> not declared in modparam section!\n", in->len, in->s);
return -1;
}
int pv_get_acc_leg(struct sip_msg *msg, pv_param_t *param,
pv_value_t *val)
{
int tag_idx, leg_idx;
acc_ctx_t* ctx = try_fetch_ctx();
pv_value_t idx_value;
if (ctx == NULL) {
/* if we don't have a context then create it */
if (init_acc_ctx(&ctx) < 0) {
LM_ERR("failed to create accounting context!\n");
return -1;
}
ACC_PUT_CTX(ctx);
}
if (ctx->leg_values == NULL) {
LM_ERR("no legs defined!\n");
return -1;
}
tag_idx = param->pvn.u.isname.name.n;
if (param->pvi.type == PV_IDX_PVAR) {
if (pv_get_spec_value(msg, param->pvi.u.dval, &idx_value) < 0) {
LM_ERR("failed to fetch index value!\n");
return -1;
}
if (idx_value.flags&PV_VAL_INT) {
leg_idx = idx_value.ri;
} else if (idx_value.flags&PV_VAL_STR) {
if (str2sint(&idx_value.rs, &leg_idx) < 0) {
goto invalid_leg;
}
} else {
goto invalid_leg;
}
} else if (param->pvi.type == PV_IDX_INT) {
leg_idx = param->pvi.u.ival;
} else {
/* if not provided consider the value of the last leg */
leg_idx = ctx->legs_no - 1;
}
if (leg_idx >= ctx->legs_no) {
LM_ERR("there aren't that many legs!\n");
return -1;
}
if (leg_idx < 0) {
if ((int)ctx->legs_no + leg_idx < 0) {
LM_ERR("invalid leg index %d!\n", leg_idx);
return -1;
}
/* -1 will be the last element and so on */
leg_idx += ctx->legs_no;
}
val->flags = PV_VAL_STR;
accX_lock(&ctx->lock);
if (ctx->leg_values[leg_idx][tag_idx].value.s == NULL) {
val->flags = PV_VAL_NULL;
} else {
val->rs = ctx->leg_values[leg_idx][tag_idx].value;
}
accX_unlock(&ctx->lock);
return 0;
invalid_leg:
LM_ERR("cannot fetch leg index value!\n");
return -1;
}
/*
*
*
*/
int pv_set_acc_leg(struct sip_msg *msg, pv_param_t *param, int flag,
pv_value_t *val)
{
int tag_idx, leg_idx;
acc_ctx_t* ctx = try_fetch_ctx();
pv_value_t idx_value;
if (ctx == NULL) {
/* if we don't have a context then create it */
if (init_acc_ctx(&ctx) < 0) {
LM_ERR("failed to create accounting context!\n");
return -1;
}
ACC_PUT_CTX(ctx);
}
if (ctx->leg_values == NULL) {
LM_ERR("no legs defined!\n");
return -1;
}
tag_idx = param->pvn.u.isname.name.n;
if (param->pvi.type == PV_IDX_PVAR) {
if (pv_get_spec_value(msg, param->pvi.u.dval, &idx_value) < 0) {
LM_ERR("failed to fetch index value!\n");
return -1;
}
if (idx_value.flags&PV_VAL_INT) {
leg_idx = idx_value.ri;
} else if (idx_value.flags&PV_VAL_STR) {
if (str2sint(&idx_value.rs, &leg_idx) < 0) {
goto invalid_leg;
}
} else {
goto invalid_leg;
}
} else if(param->pvi.type == PV_IDX_INT) {
leg_idx = param->pvi.u.ival;
} else {
/* if not provided consider the value of the last leg */
leg_idx = ctx->legs_no - 1;
}
if (leg_idx >= (int)ctx->legs_no) {
LM_ERR("there aren't that many legs!\n");
return -1;
}
if (leg_idx < 0) {
if ((int)ctx->legs_no + leg_idx < 0) {
LM_ERR("invalid leg index %d!\n", leg_idx);
return -1;
}
/* -1 will be the last element and so on */
leg_idx += ctx->legs_no;
}
accX_lock(&ctx->lock);
if (set_value_shm(val, &ctx->leg_values[leg_idx][tag_idx]) < 0) {
LM_ERR("failed to set leg <%.*s> value for leg number %d!\n",
extra_tags[tag_idx].len, leg_tags[tag_idx].s, leg_idx);
accX_unlock(&ctx->lock);
return -1;
}
accX_unlock(&ctx->lock);
return 0;
invalid_leg:
LM_ERR("cannot fetch leg index value!\n");
return -1;
}