Browse code

Merge pull request #309 from kamailio/vseva/scriptvar2xavp

pv: create command to copy script vars to xavp

Victor Seva authored on 01/09/2015 08:02:10
Showing 7 changed files
... ...
@@ -11,11 +11,11 @@ Daniel-Constantin Mierla
11 11
 
12 12
    <daniel@asipto.com>
13 13
 
14
-   Copyright � 2008-2011 Daniel-Constantin Mierla (asipto.com)
14
+   Copyright (c) 2008-2011 Daniel-Constantin Mierla (asipto.com)
15 15
 
16
-   Copyright � 2011 Juha Heinanen
16
+   Copyright (c) 2011 Juha Heinanen
17 17
 
18
-   Copyright � 2013 Olle E. Johansson, Edvina AB
18
+   Copyright (c) 2013 Olle E. Johansson, Edvina AB
19 19
      __________________________________________________________________
20 20
 
21 21
    Table of Contents
... ...
@@ -46,6 +46,7 @@ Daniel-Constantin Mierla
46 46
               4.8. sbranch_append()
47 47
               4.9. sbranch_reset()
48 48
               4.10. pv_xavp_print()
49
+              4.11. pv_var_to_xavp(varname, xname)
49 50
 
50 51
         5. MI Commands
51 52
 
... ...
@@ -72,8 +73,9 @@ Daniel-Constantin Mierla
72 73
    1.11. sbranch_append() usage
73 74
    1.12. sbranch_append() usage
74 75
    1.13. pv_xavp_print() usage
75
-   1.14. shv_set usage
76
-   1.15. shv_get usage
76
+   1.14. pv_var_to_xavp() usage
77
+   1.15. shv_set usage
78
+   1.16. shv_get usage
77 79
 
78 80
 Chapter 1. Admin Guide
79 81
 
... ...
@@ -103,6 +105,7 @@ Chapter 1. Admin Guide
103 105
         4.8. sbranch_append()
104 106
         4.9. sbranch_reset()
105 107
         4.10. pv_xavp_print()
108
+        4.11. pv_var_to_xavp(varname, xname)
106 109
 
107 110
    5. MI Commands
108 111
 
... ...
@@ -207,6 +210,7 @@ modparam("pv","avp_aliases","email=s:email_addr;tmp=i:100")
207 210
    4.8. sbranch_append()
208 211
    4.9. sbranch_reset()
209 212
    4.10. pv_xavp_print()
213
+   4.11. pv_var_to_xavp(varname, xname)
210 214
 
211 215
 4.1. pv_isset(pvar)
212 216
 
... ...
@@ -239,7 +243,7 @@ if(pv_isset("$avp(s:x)"))
239 243
 pv_unset("$avp(s:x)");
240 244
 ...
241 245
 
242
-4.3. is_int(pvar)
246
+4.3.  is_int(pvar)
243 247
 
244 248
    Function checks if pvar argument contains integer value and returns 1
245 249
    if it does and -1 otherwise.
... ...
@@ -253,7 +257,7 @@ if (is_int("$var(foo)")) {
253 257
 }
254 258
 ...
255 259
 
256
-4.4. typeof(pvar, vtype)
260
+4.4.  typeof(pvar, vtype)
257 261
 
258 262
    Returns true if the type of pseudo-variable matches the second
259 263
    parameter. The second parameter can be: 'int' - type is integer; 'str'
... ...
@@ -268,7 +272,7 @@ if (typeof("$var(foo)", "str")) {
268 272
 }
269 273
 ...
270 274
 
271
-4.5. not_empty(pvar)
275
+4.5.  not_empty(pvar)
272 276
 
273 277
    Returns true if the pseudo-variables has the type string and is not
274 278
    empty value.
... ...
@@ -282,7 +286,7 @@ if (not_empty("$var(foo)")) {
282 286
 }
283 287
 ...
284 288
 
285
-4.6. xavp_params_explode(sparams, xname)
289
+4.6.  xavp_params_explode(sparams, xname)
286 290
 
287 291
    Convert a parameters string in xavp atributes.
288 292
 
... ...
@@ -303,7 +307,7 @@ xavp_params_explode("a=b;c=d;e=d", "x");
303 307
 #    $xavp(x=>e) = "f";
304 308
 ...
305 309
 
306
-4.7. sbranch_set_ruri()
310
+4.7.  sbranch_set_ruri()
307 311
 
308 312
    Use the attributes from static branch ($sbranch(key) variable) to set
309 313
    request URI and the other fields of the branch associated with request
... ...
@@ -324,7 +328,7 @@ $sbranch(send_socket) =  "udp:127.0.0.1:5060";
324 328
 sbranch_set_ruri();
325 329
 ...
326 330
 
327
-4.8. sbranch_append()
331
+4.8.  sbranch_append()
328 332
 
329 333
    Use the attributes from static branch ($sbranch(key) variable) to
330 334
    append a new branch to destination set. It is an alternative to
... ...
@@ -345,7 +349,7 @@ $sbranch(send_socket) =  "udp:127.0.0.1:5060";
345 349
 sbranch_append();
346 350
 ...
347 351
 
348
-4.9. sbranch_reset()
352
+4.9.  sbranch_reset()
349 353
 
350 354
    Reset the content of static branch ($sbranch(key) variable.
351 355
 
... ...
@@ -356,7 +360,7 @@ sbranch_append();
356 360
 sbranch_reset();
357 361
 ...
358 362
 
359
-4.10. pv_xavp_print()
363
+4.10.  pv_xavp_print()
360 364
 
361 365
    Print all XAVPs to the syslog using INFO log level.
362 366
 
... ...
@@ -367,6 +371,30 @@ sbranch_reset();
367 371
 pv_xavp_print();
368 372
 ...
369 373
 
374
+4.11.  pv_var_to_xavp(varname, xname)
375
+
376
+   Copy script variables values to a xavp.
377
+
378
+   First parameter can be '*' in order to copy all script variables.
379
+   Second parameter is the name of the destination xavp. If xavp already
380
+   exists it will be reset first.
381
+
382
+   Function can be used from ANY_ROUTE.
383
+
384
+   Example 1.14. pv_var_to_xavp() usage
385
+...
386
+$var("temp") = 3;
387
+$var("foo") = "foo indeed";
388
+pv_var_to_xavp("temp", "ok");
389
+...
390
+$xavp("ok[0]=>temp") now is 3
391
+...
392
+pv_var_to_xavp("*", "ok");
393
+...
394
+$xavp("ok[0]=>temp") now is 3
395
+$xavp("ok[0]=>foo") now is "foo indeed"
396
+...
397
+
370 398
 5. MI Commands
371 399
 
372 400
    5.1. shv_set
... ...
@@ -390,7 +418,7 @@ pv_xavp_print();
390 418
                 _value_
391 419
                 _empty_line_
392 420
 
393
-   Example 1.14. shv_set usage
421
+   Example 1.15. shv_set usage
394 422
 ...
395 423
 $ kamctl fifo shv_set debug int 0
396 424
 ...
... ...
@@ -408,7 +436,7 @@ $ kamctl fifo shv_set debug int 0
408 436
                 _name_
409 437
                 _empty_line_
410 438
 
411
-   Example 1.15. shv_get usage
439
+   Example 1.16. shv_get usage
412 440
 ...
413 441
 $ kamctl fifo shv_get debug
414 442
 $ kamctl fifo shv_get
... ...
@@ -391,6 +391,37 @@ sbranch_reset();
391 391
 				<programlisting format="linespecific">
392 392
 ...
393 393
 pv_xavp_print();
394
+...
395
+				</programlisting>
396
+			</example>
397
+		</section>
398
+		<section id="pv.f.pv_var_to_xavp">
399
+			<title>
400
+				<function moreinfo="none">pv_var_to_xavp(varname, xname)</function>
401
+			</title>
402
+			<para>
403
+				Copy script variables values to a xavp.
404
+			</para>
405
+			<para>
406
+				First parameter can be '*' in order to copy all script variables. Second parameter is the name of the destination xavp. If xavp already exists it will be reset first.
407
+			</para>
408
+			<para>
409
+			Function can be used from ANY_ROUTE.
410
+			</para>
411
+			<example>
412
+				<title><function>pv_var_to_xavp()</function> usage</title>
413
+				<programlisting format="linespecific">
414
+...
415
+$var("temp") = 3;
416
+$var("foo") = "foo indeed";
417
+pv_var_to_xavp("temp", "ok");
418
+...
419
+$xavp("ok[0]=>temp") now is 3
420
+...
421
+pv_var_to_xavp("*", "ok");
422
+...
423
+$xavp("ok[0]=>temp") now is 3
424
+$xavp("ok[0]=>foo") now is "foo indeed"
394 425
 ...
395 426
 				</programlisting>
396 427
 			</example>
... ...
@@ -494,6 +494,7 @@ static int w_xavp_params_explode(sip_msg_t *msg, char *pparams, char *pxname);
494 494
 static int w_sbranch_set_ruri(sip_msg_t *msg, char p1, char *p2);
495 495
 static int w_sbranch_append(sip_msg_t *msg, char p1, char *p2);
496 496
 static int w_sbranch_reset(sip_msg_t *msg, char p1, char *p2);
497
+static int w_var_to_xavp(sip_msg_t *msg, char *p1, char *p2);
497 498
 
498 499
 static int pv_init_rpc(void);
499 500
 
... ...
@@ -505,6 +506,8 @@ static cmd_export_t cmds[]={
505 506
 #ifdef WITH_XAVP
506 507
 	{"pv_xavp_print",  (cmd_function)pv_xavp_print,  0, 0, 0, 
507 508
 		ANY_ROUTE },
509
+	{"pv_var_to_xavp",  (cmd_function)w_var_to_xavp, 2, 0, 0,
510
+		ANY_ROUTE },
508 511
 #endif
509 512
 	{"is_int", (cmd_function)is_int, 1, fixup_pvar_null, fixup_free_pvar_null,
510 513
 		ANY_ROUTE},
... ...
@@ -693,6 +696,20 @@ static int is_int(struct sip_msg* msg, char* pvar, char* s2)
693 696
 	return -1;
694 697
 }
695 698
 
699
+static int w_var_to_xavp(sip_msg_t *msg, char *s1, char *s2)
700
+{
701
+	str xname, varname;
702
+
703
+	if(s1 == NULL || s2 == NULL) {
704
+		LM_ERR("wrong parameters\n");
705
+		return -1;
706
+	}
707
+
708
+	varname.len = strlen(s1); varname.s = s1;
709
+	xname.s = s2; xname.len = strlen(s2);
710
+	return pv_var_to_xavp(&varname, &xname);
711
+}
712
+
696 713
 /**
697 714
  *
698 715
  */
... ...
@@ -186,6 +186,10 @@ script_var_t* get_varnull_by_name(str *name)
186 186
 	return 0;
187 187
 }
188 188
 
189
+script_var_t* get_var_all(void) {
190
+	return script_vars;
191
+}
192
+
189 193
 void reset_vars(void)
190 194
 {
191 195
 	script_var_t *it;
... ...
@@ -51,6 +51,7 @@ script_var_t* add_var(str *name, int vtype);
51 51
 script_var_t* set_var_value(script_var_t *var, int_str *value, int flags);
52 52
 script_var_t* get_var_by_name(str *name);
53 53
 script_var_t* get_varnull_by_name(str *name);
54
+script_var_t* get_var_all(void);
54 55
 
55 56
 void reset_vars(void);
56 57
 void destroy_vars(void);
... ...
@@ -613,4 +613,65 @@ int xavp_params_explode(str *params, str *xname)
613 613
 
614 614
 	return 0;
615 615
 }
616
+
617
+int pv_var_to_xavp(str *varname, str *xname)
618
+{
619
+	script_var_t *it;
620
+	sr_xavp_t *xavp = NULL;
621
+	sr_xval_t xval;
622
+
623
+	LM_DBG("xname:%.*s varname:%.*s\n", xname->len, xname->s,
624
+		varname->len, varname->s);
625
+
626
+	// clean xavp
627
+	xavp_rm_by_name(xname, 1, NULL);
628
+
629
+	if(varname->len==1 && varname->s[0] == '*') {
630
+		for(it=get_var_all(); it; it=it->next) {
631
+			memset(&xval, 0, sizeof(sr_xval_t));
632
+			if(it->v.flags&VAR_VAL_INT)
633
+			{
634
+				xval.type = SR_XTYPE_INT;
635
+				xval.v.i = it->v.value.n;
636
+			} else {
637
+				if(it->v.value.s.len==0) continue;
638
+				xval.type = SR_XTYPE_STR;
639
+				xval.v.s.s = it->v.value.s.s;
640
+				xval.v.s.len = it->v.value.s.len;
641
+			}
642
+			xavp = xavp_add_xavp_value(xname, &it->name, &xval, NULL);
643
+			if(xavp==NULL) {
644
+				LM_ERR("can't copy [%.*s]\n", it->name.len, it->name.s);
645
+				goto error;
646
+			}
647
+		}
648
+	}
649
+	else {
650
+		it = get_var_by_name(varname);
651
+		if(it==NULL) {
652
+			LM_ERR("script var [%.*s] not found\n", varname->len, varname->s);
653
+			return -1;
654
+		}
655
+		memset(&xval, 0, sizeof(sr_xval_t));
656
+		if(it->v.flags&VAR_VAL_INT)
657
+		{
658
+			xval.type = SR_XTYPE_INT;
659
+			xval.v.i = it->v.value.n;
660
+		} else {
661
+			xval.type = SR_XTYPE_STR;
662
+			xval.v.s.s = it->v.value.s.s;
663
+			xval.v.s.len = it->v.value.s.len;
664
+		}
665
+		xavp = xavp_add_xavp_value(xname, &it->name, &xval, NULL);
666
+		if(xavp==NULL) {
667
+			LM_ERR("can't copy [%.*s]\n", it->name.len, it->name.s);
668
+			goto error;
669
+		}
670
+	}
671
+	return 1;
672
+
673
+error:
674
+	xavp_rm_by_name(xname, 1, NULL);
675
+	return -1;
676
+}
616 677
 #endif
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com) 
2
+ * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com)
3 3
  *
4 4
  * Permission to use, copy, modify, and distribute this software for any
5 5
  * purpose with or without fee is hereby granted, provided that the above
... ...
@@ -20,6 +20,7 @@
20 20
 #ifdef WITH_XAVP
21 21
 
22 22
 #include "../../pvar.h"
23
+#include "pv_svar.h"
23 24
 
24 25
 int pv_get_xavp(struct sip_msg *msg, pv_param_t *param,
25 26
 		pv_value_t *res);
... ...
@@ -31,5 +32,7 @@ int pv_xavp_print(struct sip_msg* msg, char* s1, char *s2);
31 32
 
32 33
 int xavp_params_explode(str *params, str *xname);
33 34
 
35
+int pv_var_to_xavp(str *varname, str *xname);
36
+
34 37
 #endif
35 38
 #endif