File: mod_fix.c

package info (click to toggle)
kamailio 4.2.0-2+deb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (601 lines) | stat: -rw-r--r-- 14,647 bytes parent folder | download | duplicates (2)
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/* 
 * Copyright (C) 2008 iptelorg GmbH
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* 
 * History:
 * --------
 *  2008-11-25  initial version (andrei)
 */

/*!
 * \file
 * \brief SIP-router core :: kamailio compatible fixups
 * \ingroup core
 * Module: \ref core
 */

#include "mod_fix.h"
#include "mem/mem.h"
#include "trim.h"



#if 0
/* TODO: */
int fixup_regexpNL_null(void** param, int param_no); /* not used */
int fixup_regexpNL_none(void** param, int param_no); /* textops */
#endif



#define FREE_FIXUP_FP(suffix, minp, maxp) \
	int fixup_free_##suffix(void** param, int param_no) \
	{ \
		if ((param_no > (maxp)) || (param_no < (minp))) \
			return E_UNSPEC; \
		if (*param) \
			fparam_free_restore(param); \
		return 0; \
	}


/** macro for declaring a fixup and the corresponding free_fixup
  * for a function which fixes to fparam_t and expects 2 different types.
  *
  * The result (in *param) will be a fparam_t.
  *
  * @param suffix - function suffix (fixup_ will be pre-pended to it 
  * @param minp - minimum parameter number acceptable
  * @param maxp - maximum parameter number
  * @param no1 -  number of parameters of type1
  * @param type1 - fix_param type for the 1st param
  * @param type2 - fix_param type for all the other params
  */
#define FIXUP_F2FP(suffix, minp, maxp, no1, type1, type2) \
	int fixup_##suffix (void** param, int param_no) \
	{ \
		if ((param_no > (maxp)) || (param_no <(minp))) \
			return E_UNSPEC; \
		if (param_no <= (no1)){ \
			if (fix_param_types((type1), param)!=0) {\
				ERR("Cannot convert function parameter %d to" #type1 "\n", \
						param_no);\
				return E_UNSPEC; \
			} \
		}else{ \
			if (fix_param_types((type2), param)!=0) {\
				ERR("Cannot convert function parameter %d to" #type2 "\n", \
						param_no); \
				return E_UNSPEC; \
			} \
		}\
		return 0; \
	} \
	FREE_FIXUP_FP(suffix, minp, maxp)


/** macro for declaring a fixup and the corresponding free_fixup
  * for a function which fixes directly to the requested type.
  *
  * @see FIXUP_F2FP for the parameters
  * Side effect: declares also some _fp_helper functions
  */
#define FIXUP_F2T(suffix, minp, maxp, no1, type1, type2) \
	FIXUP_F2FP(fp_##suffix, minp, maxp, no1, type1, type2) \
	int fixup_##suffix (void** param, int param_no) \
	{ \
		int ret; \
		if ((ret=fixup_fp_##suffix (param, param_no))!=0) \
			return ret; \
		*param=((fparam_t*)*param)->fixed; \
		return 0; \
	} \
	int fixup_free_##suffix (void** param, int param_no) \
	{ \
		void* p; \
		int ret; \
		if (param && *param){ \
			p=*param - (long)&((fparam_t*)0)->v; \
			if ((ret=fixup_free_fp_##suffix(&p, param_no))==0) *param=p; \
			return ret; \
		} \
		return 0; \
	}


/** macro for declaring a fixup and the corresponding free_fixup
  * for a function expecting first no1 params as fparamt_t and the
  * rest as direct type.
  *
  * @see FIXUP_F2FP for the parameters with the exception
  * that only the first no1 parameters are converted to 
  * fparamt_t and the rest directly to the correponding type
  *
  * Side effect: declares also some _fpt_helper functions
  */
#define FIXUP_F2FP_T(suffix, minp, maxp, no1, type1, type2) \
	FIXUP_F2FP(fpt_##suffix, minp, maxp, no1, type1, type2) \
	int fixup_##suffix (void** param, int param_no) \
	{ \
		int ret; \
		if ((ret=fixup_fpt_##suffix(param, param_no))!=0) \
			return ret; \
		if (param_no>(no1)) *param=&((fparam_t*)*param)->v; \
		return 0; \
	} \
	int fixup_free_##suffix (void** param, int param_no) \
	{ \
		void* p; \
		int ret; \
		if (param && *param){ \
			p=(param_no>(no1))? *param - (long)&((fparam_t*)0)->v : *param;\
			if ((ret=fixup_free_fpt_##suffix(&p, param_no))==0) *param=p; \
			return ret; \
		} \
		return 0; \
	}


/** macro for declaring a fixup which fixes all the paremeters to the same
  * type.
  *
  * @see FIXUP_F2T.
  */
#define FIXUP_F1T(suffix, minp, maxp, type) \
	FIXUP_F2T(suffix, minp, maxp, maxp, type, 0)



FIXUP_F1T(str_null, 1, 1, FPARAM_STR)
FIXUP_F1T(str_str, 1, 2,  FPARAM_STR)
FIXUP_F1T(str_all, 1, 100,  FPARAM_STR)

/*
  no free fixups possible for unit_*
  (they overwrite the pointer with the converted number => the original
   value cannot be recovered)
FIXUP_F1T(uint_null, 1, 1, FPARAM_INT)
FIXUP_F1T(uint_uint, 1, 2, FPARAM_INT)
*/



int fixup_uint_uint(void** param, int param_no)
{
	str s;
	unsigned int num;
	
	s.s = *param;
	s.len = strlen(s.s);
	if (likely(str2int(&s, &num) == 0)) {
		*param = (void*)(long)num;
	} else
		/* not a number */
		return E_UNSPEC;
	return 0;
}



int fixup_uint_null(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_uint_uint(param, param_no);
	return E_UNSPEC;
}


/* fixup_regexp_null() has to be written "by hand", since
   it needs to save the original pointer (the fixup users expects
   a pointer to the regex in *param and hence the original value
   needed on free cannot be recovered directly).
FIXUP_F1T(regexp_null, 1, 1, FPARAM_REGEX)
*/

struct regex_fixup {
	regex_t regex; /* compiled regex */
	void* orig;    /* original pointer */
};

int fixup_regexp_null(void** param, int param_no)
{
	struct regex_fixup* re;
	
	if (param_no != 1)
		return E_UNSPEC;
	if ((re=pkg_malloc(sizeof(*re))) ==0) {
		ERR("No memory left\n");
		goto error;
	}
	if (regcomp(&re->regex, *param,
				REG_EXTENDED|REG_ICASE|REG_NEWLINE))
		goto error;
	re->orig = *param;
	*param = re;
	return 0;
error:
	if (re)
		pkg_free(re);
	return E_UNSPEC;
}


int fixup_free_regexp_null(void** param, int param_no)
{
	struct regex_fixup* re;
	
	if (param_no != 1)
		return E_UNSPEC;
	if (*param) {
		re = *param;
		*param = re->orig;
		regfree(&re->regex);
		pkg_free(re);
	}
	return 0;
}

/* fixup_pvar_*() has to be written "by hand", since
   it needs to save the original pointer (the fixup users expects
   a pointer to the pv_spec_t in *param and hence the original value
   needed on free cannot be recovered directly).
FIXUP_F1T(pvar_null, 1, 1, FPARAM_PVS)
FIXUP_F1T(pvar_pvar, 1, 2, FPARAM_PVS)
*/

struct pvs_fixup {
	pv_spec_t pvs; /* parsed pv spec */
	void* orig;    /* original pointer */
};

int fixup_pvar_all(void** param, int param_no)
{
	struct pvs_fixup* pvs_f;
	str name;
	
	pvs_f = 0;
	name.s = *param;
	name.len = strlen(name.s);
	trim(&name);
	if (name.len == 0 || name.s[0] != '$')
		/* not a pvs id */
		goto error;
	if ((pvs_f=pkg_malloc(sizeof(*pvs_f))) == 0) {
		ERR("No memory left\n");
		goto error;
	}
	if (pv_parse_spec2(&name, &pvs_f->pvs, 1) == 0)
		/* not a valid pvs identifier */
		goto error;
	pvs_f->orig = *param;
	*param = pvs_f;
	return 0;
error:
	if (pvs_f)
		pkg_free(pvs_f);
	return E_UNSPEC;
}



int fixup_free_pvar_all(void** param, int param_no)
{
	struct pvs_fixup* pvs_f;
	
	if (*param) {
		pvs_f = *param;
		*param = pvs_f->orig;
		/* free only the contents (don't attempt to free &pvs_f->pvs)*/
		pv_spec_destroy(&pvs_f->pvs);
		/* free the whole pvs_fixup */
		pkg_free(pvs_f);
	}
	return 0;
}



int fixup_pvar_pvar(void** param, int param_no)
{
	if (param_no > 2)
		return E_UNSPEC;
	return fixup_pvar_all(param, param_no);
}



int fixup_free_pvar_pvar(void** param, int param_no)
{
	if (param_no > 2)
		return E_UNSPEC;
	return fixup_free_pvar_all(param, param_no);
}



int fixup_pvar_null(void** param, int param_no)
{
	if (param_no != 1)
		return E_UNSPEC;
	return fixup_pvar_all(param, param_no);
}



int fixup_free_pvar_null(void** param, int param_no)
{
	if (param_no != 1)
		return E_UNSPEC;
	return fixup_free_pvar_all(param, param_no);
}

int fixup_pvar_none(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_pvar_all(param, param_no);
	return 0;
}



int fixup_free_pvar_none(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_free_pvar_all(param, param_no);
	return 0;
}


/* must be written "by hand", see above (fixup_pvar_pvar).
FIXUP_F2T(pvar_str, 1, 2, 1, FPARAM_PVS, FPARAM_STR)
FIXUP_F2T(pvar_str_str, 1, 3, 1, FPARAM_PVS, FPARAM_STR)
*/

int fixup_pvar_str(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_pvar_all(param, param_no);
	else if (param_no == 2)
		return fixup_str_str(param, param_no);
	return E_UNSPEC;
}



int fixup_free_pvar_str(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_free_pvar_all(param, param_no);
	else if (param_no == 2)
		return fixup_free_str_str(param, param_no);
	return E_UNSPEC;
}



int fixup_pvar_str_str(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_pvar_all(param, param_no);
	else if (param_no == 2 || param_no == 3)
		return fixup_str_all(param, param_no);
	return E_UNSPEC;
}



int fixup_free_pvar_str_str(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_free_pvar_all(param, param_no);
	else if (param_no == 2 || param_no == 3)
		return fixup_free_str_all(param, param_no);
	return E_UNSPEC;
}


int fixup_pvar_uint(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_pvar_all(param, param_no);
	else if (param_no == 2)
		return fixup_uint_uint(param, param_no);
	return E_UNSPEC;
}


int fixup_free_pvar_uint(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_free_pvar_all(param, param_no);
	return E_UNSPEC;
}


FIXUP_F2FP(igp_null, 1, 1, 1, FPARAM_INT|FPARAM_PVS, 0)
FIXUP_F2FP(igp_igp, 1, 2, 2,  FPARAM_INT|FPARAM_PVS, 0)

/* must be declared by hand, because of the pvar special handling
   (see above)
FIXUP_F2FP(igp_pvar, 1, 2, 1,  FPARAM_INT|FPARAM_PVS, FPARAM_PVS)
FIXUP_F2FP_T(igp_pvar_pvar, 1, 3, 1, FPARAM_INT|FPARAM_PVS, FPARAM_PVS)
*/

int fixup_igp_pvar(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_igp_null(param, param_no);
	else if (param_no == 2)
		return fixup_pvar_all(param, param_no);
	return E_UNSPEC;
}



int fixup_free_igp_pvar(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_free_igp_null(param, param_no);
	else if (param_no == 2)
		return fixup_free_pvar_all(param, param_no);
	return E_UNSPEC;
}



int fixup_igp_pvar_pvar(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_igp_null(param, param_no);
	else if (param_no == 2 || param_no == 3)
		return fixup_pvar_all(param, param_no);
	return E_UNSPEC;
}



int fixup_free_igp_pvar_pvar(void** param, int param_no)
{
	if (param_no == 1)
		return fixup_free_igp_null(param, param_no);
	else if (param_no == 2 || param_no == 3)
		return fixup_free_pvar_all(param, param_no);
	return E_UNSPEC;
}



/** macro for declaring a spve fixup and the corresponding free_fixup
  * for a function expecting first no1 params as fparam converted spve 
  * and the * rest as direct type.
  *
  * @see FIXUP_F2FP for the parameters with the exception
  * that the first no1 parameters are converted to fparam_t from spve
  * and the rest directly to the corresponding type
  *
  * Side effect: declares also some _spvet_helper functions
  */
#define FIXUP_F_SPVE_T(suffix, minp, maxp, no1, type2) \
	FIXUP_F1T(spvet_##suffix, minp, maxp, type2) \
	int fixup_##suffix (void** param, int param_no) \
	{ \
		int ret; \
		fparam_t* fp; \
		if (param_no<=(no1)){ \
			if ((ret=fix_param_types(FPARAM_PVE, param))<0){ \
				ERR("Cannot convert function parameter %d to spve \n", \
						param_no);\
				return E_UNSPEC; \
			} else{ \
				fp=(fparam_t*)*param; \
				if ((ret==0) && (fp->v.pve->spec==0 \
							|| fp->v.pve->spec->getf==0)){ \
					fparam_free_restore(param); \
					return fix_param_types(FPARAM_STR, param); \
				} else if (ret==1) \
					return fix_param_types(FPARAM_STR, param); \
				return ret; \
			} \
		} else return fixup_spvet_##suffix(param, param_no); \
		return 0; \
	} \
	int fixup_free_##suffix (void** param, int param_no) \
	{ \
		if (param && *param){ \
			if (param_no<=(no1)) \
				fparam_free_restore(param); \
			else \
				return fixup_free_spvet_##suffix(param, param_no); \
		} \
		return 0; \
	}


/* format: name, minp, maxp, no_of_spve_params, type_for_rest_params */
FIXUP_F_SPVE_T(spve_spve, 1, 2, 2, 0)
FIXUP_F_SPVE_T(spve_uint, 1, 2, 1, FPARAM_INT)
FIXUP_F_SPVE_T(spve_str, 1, 2, 1, FPARAM_STR)
FIXUP_F_SPVE_T(spve_null, 1, 1, 1, 0)

/** get the corresp. fixup_free* function.
 * @param f -fixup function pointer.
 * @return  - pointer to free_fixup function if known, 0 otherwise.
 */
free_fixup_function mod_fix_get_fixup_free(fixup_function f)
{
	if (f == fixup_str_null) return fixup_free_str_null;
	if (f == fixup_str_str) return fixup_free_str_str;
	/* no free fixup for fixup_uint_* (they overwrite the pointer
	   value with a number and the original value cannot be recovered) */
	if (f == fixup_uint_null) return 0;
	if (f == fixup_uint_uint) return 0;
	if (f == fixup_regexp_null) return fixup_free_regexp_null;
	if (f == fixup_pvar_null) return fixup_free_pvar_null;
	if (f == fixup_pvar_pvar) return fixup_free_pvar_pvar;
	if (f == fixup_pvar_str) return fixup_free_pvar_str;
	if (f == fixup_pvar_str_str) return fixup_free_pvar_str_str;
	if (f == fixup_igp_igp) return fixup_free_igp_igp;
	if (f == fixup_igp_null) return fixup_free_igp_null;
	if (f == fixup_igp_pvar) return fixup_free_igp_pvar;
	if (f == fixup_igp_pvar_pvar) return fixup_free_igp_pvar_pvar;
	if (f == fixup_spve_spve) return fixup_free_spve_spve;
	if (f == fixup_spve_null) return fixup_free_spve_null;
	/* no free fixup, because of the uint part (the uint cannot be freed,
	   see above fixup_uint_null) */
	if (f == fixup_spve_uint) return 0;
	if (f == fixup_spve_str) return fixup_free_spve_str;
	return 0;
}

/**
 *
 */
int fixup_spve_all(void** param, int param_no)
{
	return fixup_spve_null(param, 1);
}

/**
 *
 */
int fixup_igp_all(void** param, int param_no)
{
	return fixup_igp_null(param, 1);
}

/**
 *
 */
int fixup_spve_igp(void** param, int param_no)
{
	if(param_no==1)
		return fixup_spve_null(param, 1);
	if(param_no==2)
		return fixup_igp_null(param, 1);
	return E_UNSPEC;
}

/**
 *
 */
int fixup_free_spve_igp(void** param, int param_no)
{
	if(param_no==1)
		return fixup_free_spve_null(param, 1);
	if(param_no==2)
		return fixup_free_igp_null(param, 1);
	return E_UNSPEC;
}