forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvar.c
5659 lines (4910 loc) · 126 KB
/
pvar.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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (C) 2010-2020 OpenSIPS Solutions
* Copyright (C) 2005-2009 Voice Sistem SRL
* Copyright (C) 2001-2003 FhG Fokus
*
* 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:
* --------
* 2004-10-20 - added header name specifier (ramona)
* 2005-06-14 - added avp name specifier (ramona)
* 2005-06-18 - added color printing support via escape sequesnces
* contributed by Ingo Flaschberger (daniel)
* 2005-06-22 - created this file from modules/xlog/pv_lib.c (daniel)
* 2009-04-28 - $ct and $ct.fields() PVs added (bogdan)
* 2009-05-02 - $branch() added, $br, $bR, $bf, $bF removed (bogdan)
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include "dprint.h"
#include "mem/mem.h"
#include "mem/shm_mem.h"
#include "ut.h"
#include "trim.h"
#include "dset.h"
#include "action.h"
#include "socket_info.h"
#include "route_struct.h"
#include "usr_avp.h"
#include "errinfo.h"
#include "transformations.h"
#include "script_var.h"
#include "pvar.h"
#include "flags.h"
#include "xlog.h"
#include "parser/parse_from.h"
#include "parser/parse_uri.h"
#include "parser/parse_hname2.h"
#include "parser/parse_content.h"
#include "parser/parse_refer_to.h"
#include "parser/parse_rpid.h"
#include "parser/parse_diversion.h"
#include "parser/parse_ppi.h"
#include "parser/parse_pai.h"
#include "parser/digest/digest.h"
#include "parser/contact/parse_contact.h"
#define is_in_str(p, in) (p<in->s+in->len && *p)
extern int curr_action_line;
extern char *curr_action_file;
typedef struct _pv_extra
{
pv_export_t pve;
struct _pv_extra *next;
} pv_extra_t, *pv_extra_p;
pv_extra_p *_pv_extra_list=0;
static str str_marker = { PV_MARKER_STR, 1 };
/* IMPORTANT : the "const" strings returned by the var functions must
be read-write (as they may be changed by the script interpreter), so
we need to allocated as array and not as pointing to RO data segment
*/
static char _str_null_hlp[] = "<null>";
static str str_null = str_init(_str_null_hlp);
static char _str_empty_hlp[] = "";
static str str_empty = str_init(_str_empty_hlp);
int _pv_pid = 0;
#define PV_FIELD_DELIM ", "
#define PV_FIELD_DELIM_LEN (sizeof(PV_FIELD_DELIM) - 1)
#define PV_LOCAL_BUF_SIZE 511
static char pv_local_buf[PV_LOCAL_BUF_SIZE+1];
/* pv context list */
pv_context_t* pv_context_lst = NULL;
static pv_context_t* pv_get_context(const str* name);
static pv_context_t* add_pv_context(const str* name, pv_contextf_t get_context);
static int pv_parse_argv_name(pv_spec_p sp, const str *in);
static int pv_get_argv(struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
static int pv_contextlist_check(void);
/* always obtain a printable version of the given (pv_value_t *) */
static str pv_value_print(const pv_value_t *val);
static int pvc_before_check = 1;
int pv_print_buf_size = 20000;
#define PV_PRINT_BUF_NO 7
str *pv_print_buf;
#define is_pv_print_buf(p) (p >= (char *)pv_print_buf && \
p <= (char *)pv_print_buf + \
PV_PRINT_BUF_NO * (sizeof(*pv_print_buf) + pv_print_buf_size))
int init_pvar_support(void)
{
int i;
/* check pv context list */
if (pv_contextlist_check() != 0) {
LM_ERR("used pv context that was not defined\n");
return -1;
}
pv_print_buf = pkg_malloc(PV_PRINT_BUF_NO * (sizeof(str)
+ pv_print_buf_size));
if (!pv_print_buf) {
LM_ERR("oom\n");
return -1;
}
for (i = 0; i < PV_PRINT_BUF_NO; i++) {
pv_print_buf[i].s = (char *)(pv_print_buf + PV_PRINT_BUF_NO)
+ i * pv_print_buf_size;
pv_print_buf[i].len = pv_print_buf_size;
}
return 0;
}
/* route param variable */
static int pv_get_param(struct sip_msg *msg, pv_param_t *ip, pv_value_t *res);
static int pv_parse_param_name(pv_spec_p sp, const str *in);
static int pv_parse_return_value(pv_spec_p sp, const str *in);
/********** helper functions ********/
/**
* convert unsigned int to pv_value_t
*/
int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res, unsigned int uival)
{
int l = 0;
char *ch = NULL;
if(res==NULL)
return -1;
ch = int2str(uival, &l);
res->rs.s = ch;
res->rs.len = l;
res->ri = (int)uival;
res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
return 0;
}
/**
* convert signed int to pv_value_t
*/
int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res, int sival)
{
int l = 0;
char *ch = NULL;
if(res==NULL)
return -1;
ch = sint2str(sival, &l);
res->rs.s = ch;
res->rs.len = l;
res->ri = sival;
res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
return 0;
}
/**
* convert str to pv_value_t
*/
int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res, str *sval)
{
if(res==NULL)
return -1;
res->rs = *sval;
res->flags = PV_VAL_STR;
return 0;
}
/**
* convert str-int to pv_value_t (type is str)
*/
static int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res, str *sval, int ival)
{
if(res==NULL)
return -1;
res->rs = *sval;
res->ri = ival;
res->flags = PV_VAL_STR|PV_VAL_INT;
return 0;
}
/**
* convert int-str to pv_value_t (type is int)
*/
static int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res, int ival, str *sval)
{
if(res==NULL)
return -1;
res->rs = *sval;
res->ri = ival;
res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
return 0;
}
/************************************************************/
static int pv_get_marker(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
return pv_get_strintval(msg, param, res, &str_marker, (int)str_marker.s[0]);
}
int pv_get_null(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
{
if(res==NULL)
return -1;
res->rs = str_empty;
res->ri = 0;
res->flags = PV_VAL_NULL;
return 0;
}
/************************************************************/
static int pv_get_pid(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(_pv_pid == 0)
_pv_pid = (int)getpid();
return pv_get_sintval(msg, param, res, _pv_pid);
}
extern int return_code;
static int pv_get_return_code(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
return pv_get_sintval(msg, param, res, return_code);
}
static int pv_get_route(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
static str rn_buf;
str s;
int i, idx, idx_flags, len, rlen, has_name, route_stack_size_ctx;
if (pv_get_spec_index(msg, param, &idx, &idx_flags) != 0) {
LM_ERR("invalid index\n");
return -1;
}
if (idx_flags == PV_IDX_ALL) {
get_top_route_type(&s, &has_name);
if (!has_name)
goto unnamed_route;
len = strlen(route_stack[route_stack_start]);
if (pkg_str_extend(&rn_buf, s.len + 2 + len + 1) != 0) {
LM_ERR("oom\n");
return pv_get_null(msg, param, res);
}
len = sprintf(rn_buf.s, "%.*s[%s]", s.len, s.s, route_stack[route_stack_start]);
goto print_remaining;
unnamed_route:
if (pkg_str_extend(&rn_buf, s.len + 1) != 0) {
LM_ERR("oom\n");
return pv_get_null(msg, param, res);
}
len = sprintf(rn_buf.s, "%.*s", s.len, s.s);
print_remaining:
s = str_route;
for (i = route_stack_start+1; i < route_stack_size; i++) {
if (!route_stack[i]) {
if (pkg_str_extend(&rn_buf, len + 3 + s.len + 1) != 0) {
LM_ERR("oom\n");
return pv_get_null(msg, param, res);
}
len += sprintf(rn_buf.s + len, " > %.*s", s.len, s.s);
} else if (route_stack[i][0] != '!') {
rlen = strlen(route_stack[i]);
if (pkg_str_extend(&rn_buf, len + s.len + 5 + rlen + 1) != 0) {
LM_ERR("oom\n");
return pv_get_null(msg, param, res);
}
len += sprintf(rn_buf.s + len, " > %.*s[%s]",
s.len, s.s, route_stack[i]);
} else {
rlen = strlen(route_stack[i]);
/* the "!" marker tells us to print that route name as-is */
if (pkg_str_extend(&rn_buf, len + rlen + 3) != 0) {
LM_ERR("oom\n");
return pv_get_null(msg, param, res);
}
len += sprintf(rn_buf.s + len, " > %s", route_stack[i] + 1);
}
}
s.s = rn_buf.s;
s.len = len;
return pv_get_strval(msg, param, res, &s);
}
route_stack_size_ctx = route_stack_size - route_stack_start;
if (idx < 0)
idx += route_stack_size_ctx;
/* index out of bounds -- play nice and return NULL */
if (idx > route_stack_size_ctx - 1 || idx < 0)
return pv_get_null(msg, param, res);
/* reverse the index, since we index the route stack backwards */
idx = route_stack_size_ctx - idx - 1;
if (idx == 0) {
get_top_route_type(&s, &has_name);
if (!has_name)
goto out_ok;
} else {
s = str_route;
if (!route_stack[route_stack_start + idx])
goto out_ok;
}
len = strlen(route_stack[route_stack_start + idx]);
if (route_stack[route_stack_start + idx][0] != '!') {
if (pkg_str_extend(&rn_buf, s.len + 2 + len + 1) != 0) {
LM_ERR("oom\n");
return pv_get_null(msg, param, res);
}
s.len = sprintf(rn_buf.s, "%.*s[%s]", s.len, s.s, route_stack[route_stack_start + idx]);
s.s = rn_buf.s;
} else {
/* the "!" marker tells us to print that route name as-is */
if (pkg_str_extend(&rn_buf, len) != 0) {
LM_ERR("oom\n");
return pv_get_null(msg, param, res);
}
s.len = sprintf(rn_buf.s, "%s", route_stack[route_stack_start + idx] + 1);
s.s = rn_buf.s;
}
out_ok:
return pv_get_strval(msg, param, res, &s);
}
static int pv_get_route_name(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
str rn;
int idx, idx_flags, route_stack_size_ctx;
if (pv_get_spec_index(msg, param, &idx, &idx_flags) != 0) {
LM_ERR("invalid index\n");
return -1;
}
if (idx_flags == PV_IDX_ALL) {
LM_ERR("unsupported index: [*]\n");
return -1;
}
route_stack_size_ctx = route_stack_size - route_stack_start;
if (idx < 0)
idx += route_stack_size_ctx;
/* index out of bounds -- play nice and return NULL */
if (idx > route_stack_size_ctx - 1 || idx < 0)
return pv_get_null(msg, param, res);
/* reverse the index, since we index the route stack backwards */
idx = route_stack_size_ctx - idx - 1;
get_route_name(idx, &rn);
return pv_get_strval(msg, param, res, &rn);
}
static int pv_get_route_type(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
str rt;
int idx, idx_flags, route_stack_size_ctx;
if (pv_get_spec_index(msg, param, &idx, &idx_flags) != 0) {
LM_ERR("invalid index\n");
return -1;
}
if (idx_flags == PV_IDX_ALL) {
LM_ERR("unsupported index: [*]\n");
return -1;
}
route_stack_size_ctx = route_stack_size - route_stack_start;
if (idx < 0)
idx += route_stack_size_ctx;
/* index out of bounds -- play nice and return NULL */
if (idx > route_stack_size_ctx - 1 || idx < 0)
return pv_get_null(msg, param, res);
/* reverse the index, since we index the route stack backwards */
idx = route_stack_size_ctx - idx - 1;
get_route_type(idx, &rt);
return pv_get_strval(msg, param, res, &rt);
}
static int pv_get_times(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL)
return -1;
return pv_get_uintval(msg, param, res, (unsigned int)time(NULL));
}
static int pv_get_timem(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
struct timeval TP;
if(msg==NULL)
return -1;
gettimeofday(&TP, NULL);
return pv_get_uintval(msg, param, res, (unsigned int)TP.tv_usec);
}
static int pv_get_start_times(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL)
return -1;
return pv_get_uintval(msg, param, res, (unsigned int)startup_time);
}
static int pv_parse_time_name(pv_spec_p sp, const str *in)
{
sp->pvp.pvn.type = PV_NAME_INTSTR;
sp->pvp.pvn.u.isname.type = AVP_NAME_STR;
sp->pvp.pvn.u.isname.name.s.s = pkg_malloc(in->len + 1);
if (sp->pvp.pvn.u.isname.name.s.s==NULL) {
LM_ERR("failed to allocated private mem\n");
return -1;
}
memcpy(sp->pvp.pvn.u.isname.name.s.s, in->s, in->len);
sp->pvp.pvn.u.isname.name.s.s[in->len] = 0;
sp->pvp.pvn.u.isname.name.s.len = in->len;
return 0;
}
static int pv_get_formated_time(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
static char buf[128];
struct tm ltime;
time_t t;
if(msg==NULL)
return -1;
time( &t );
localtime_r(&t, <ime);
res->rs.len = strftime( buf, 127, param->pvn.u.isname.name.s.s, <ime);
if (res->rs.len<=0)
return pv_get_null(msg, param, res);
res->rs.s = buf;
res->flags = PV_VAL_STR;
return 0;
}
#define CTIME_BUFFER_NO 5
#define CTIME_BUFFER_SIZE 26
static int pv_get_timef(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
static char ctime_bufs[CTIME_BUFFER_NO][CTIME_BUFFER_SIZE];
static char *buf;
static int ctime_buf_no = 0;
time_t t;
str s;
if(msg==NULL)
return -1;
t = time(NULL);
buf = ctime_bufs[ctime_buf_no];
ctime_buf_no = (ctime_buf_no + 1) % CTIME_BUFFER_NO;
s.s = ctime_r(&t, buf);
s.len = strlen(s.s)-1;
return pv_get_strintval(msg, param, res, &s, (int)t);
}
static int pv_get_msgid(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL)
return -1;
return pv_get_uintval(msg, param, res, msg->id);
}
static int pv_get_method(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL)
return -1;
if(msg->first_line.type == SIP_REQUEST)
{
return pv_get_strintval(msg, param, res,
&msg->first_line.u.request.method,
(int)msg->first_line.u.request.method_value);
}
if(msg->cseq==NULL && ((parse_headers(msg, HDR_CSEQ_F, 0)==-1) ||
(msg->cseq==NULL)))
{
LM_ERR("no CSEQ header\n");
return pv_get_null(msg, param, res);
}
return pv_get_strintval(msg, param, res,
&get_cseq(msg)->method,
get_cseq(msg)->method_id);
}
static int pv_get_status(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL)
return -1;
if(msg->first_line.type != SIP_REPLY)
return pv_get_null(msg, param, res);
return pv_get_intstrval(msg, param, res,
(int)msg->first_line.u.reply.statuscode,
&msg->first_line.u.reply.status);
}
static int pv_get_reason(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL)
return -1;
if(msg->first_line.type != SIP_REPLY)
return pv_get_null(msg, param, res);
return pv_get_strval(msg, param, res, &msg->first_line.u.reply.reason);
}
static int pv_get_ruri(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL || res==NULL)
return -1;
if(msg->first_line.type == SIP_REPLY) /* REPLY doesn't have a ruri */
return pv_get_null(msg, param, res);
if(msg->parsed_uri_ok==0 /* R-URI not parsed*/ && parse_sip_msg_uri(msg)<0)
{
LM_ERR("failed to parse the R-URI\n");
return pv_get_null(msg, param, res);
}
if (msg->new_uri.s!=NULL)
return pv_get_strval(msg, param, res, &msg->new_uri);
return pv_get_strval(msg, param, res, &msg->first_line.u.request.uri);
}
static int pv_get_ru_q(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL || res==NULL)
return -1;
if(msg->first_line.type == SIP_REPLY)
return pv_get_null(msg, param, res);
return pv_get_sintval(msg, param, res, get_ruri_q(msg));
}
static int pv_get_ouri(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL || res==NULL)
return -1;
if(msg->first_line.type == SIP_REPLY) /* REPLY doesn't have a ruri */
return pv_get_null(msg, param, res);
if(msg->parsed_orig_ruri_ok==0
/* orig R-URI not parsed*/ && parse_orig_ruri(msg)<0)
{
LM_ERR("failed to parse the R-URI\n");
return pv_get_null(msg, param, res);
}
return pv_get_strval(msg, param, res, &msg->first_line.u.request.uri);
}
static int pv_get_xuri_attr(struct sip_msg *msg, struct sip_uri *parsed_uri,
pv_param_t *param, pv_value_t *res)
{
unsigned short proto;
str proto_s;
if(param->pvn.u.isname.name.n==1) /* username */
{
if(parsed_uri->user.s==NULL || parsed_uri->user.len<=0)
return pv_get_null(msg, param, res);
return pv_get_strval(msg, param, res, &parsed_uri->user);
} else if(param->pvn.u.isname.name.n==2) /* domain */ {
if(parsed_uri->host.s==NULL || parsed_uri->host.len<=0)
return pv_get_null(msg, param, res);
return pv_get_strval(msg, param, res, &parsed_uri->host);
} else if(param->pvn.u.isname.name.n==3) /* port */ {
if(parsed_uri->port.s==NULL)
return pv_get_uintval(msg, param, res,
get_uri_port( parsed_uri, &proto));
return pv_get_strintval(msg, param, res, &parsed_uri->port,
(int)parsed_uri->port_no);
} else if(param->pvn.u.isname.name.n==4) /* protocol */ {
if(parsed_uri->transport_val.s==NULL) {
get_uri_port(parsed_uri, &proto);
proto_s.s = protos[proto].name;
proto_s.len = proto_s.s ? strlen(proto_s.s) : 0;
return pv_get_strintval(msg, param, res, &proto_s, (int)proto);
}
return pv_get_strintval(msg, param, res, &parsed_uri->transport_val,
(int)parsed_uri->proto);
}
LM_ERR("unknown specifier\n");
return pv_get_null(msg, param, res);
}
static int pv_get_ruri_attr(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL)
return -1;
if(msg->first_line.type == SIP_REPLY) /* REPLY doesn't have a ruri */
return pv_get_null(msg, param, res);
if(msg->parsed_uri_ok==0 /* R-URI not parsed*/ && parse_sip_msg_uri(msg)<0)
{
LM_ERR("failed to parse the R-URI\n");
return pv_get_null(msg, param, res);
}
return pv_get_xuri_attr(msg, &(msg->parsed_uri), param, res);
}
static int pv_get_ouri_attr(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL)
return -1;
if(msg->first_line.type == SIP_REPLY) /* REPLY doesn't have a ruri */
return pv_get_null(msg, param, res);
if(msg->parsed_orig_ruri_ok==0
/* orig R-URI not parsed*/ && parse_orig_ruri(msg)<0)
{
LM_ERR("failed to parse the R-URI\n");
return pv_get_null(msg, param, res);
}
return pv_get_xuri_attr(msg, &(msg->parsed_orig_ruri), param, res);
}
static int pv_get_path(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
if(msg==NULL)
return -1;
if(!msg->path_vec.s)
{
return pv_get_null(msg, param, res);
}
return pv_get_strval(msg, param, res, &msg->path_vec);
}
#define CT_NAME_S "name"
#define CT_NAME_LEN (sizeof(CT_NAME_S)-1)
#define CT_NAME_ID 1
#define CT_URI_S "uri"
#define CT_URI_LEN (sizeof(CT_URI_S)-1)
#define CT_URI_ID 2
#define CT_Q_S "q"
#define CT_Q_LEN (sizeof(CT_Q_S)-1)
#define CT_Q_ID 3
#define CT_EXPIRES_S "expires"
#define CT_EXPIRES_LEN (sizeof(CT_EXPIRES_S)-1)
#define CT_EXPIRES_ID 4
#define CT_METHODS_S "methods"
#define CT_METHODS_LEN (sizeof(CT_METHODS_S)-1)
#define CT_METHODS_ID 5
#define CT_RECEIVED_S "received"
#define CT_RECEIVED_LEN (sizeof(CT_RECEIVED_S)-1)
#define CT_RECEIVED_ID 6
#define CT_PARAMS_S "params"
#define CT_PARAMS_LEN (sizeof(CT_PARAMS_S)-1)
#define CT_PARAMS_ID 7
static int pv_parse_ct_name(pv_spec_p sp, const str *in)
{
if (sp==NULL)
return -1;
sp->pvp.pvn.type = PV_NAME_INTSTR;
sp->pvp.pvn.u.isname.type = 0;
if (in==NULL || in->s==NULL || in->len==0) {
sp->pvp.pvn.u.isname.name.n = 0;
} else
if (in->len==CT_NAME_LEN &&
strncasecmp(in->s, CT_NAME_S, CT_NAME_LEN)==0 ) {
sp->pvp.pvn.u.isname.name.n = CT_NAME_ID;
} else
if (in->len==CT_URI_LEN &&
strncasecmp(in->s, CT_URI_S, CT_URI_LEN)==0 ) {
sp->pvp.pvn.u.isname.name.n = CT_URI_ID;
} else
if (in->len==CT_Q_LEN &&
strncasecmp(in->s, CT_Q_S, CT_Q_LEN)==0 ) {
sp->pvp.pvn.u.isname.name.n = CT_Q_ID;
} else
if (in->len==CT_EXPIRES_LEN &&
strncasecmp(in->s, CT_EXPIRES_S, CT_EXPIRES_LEN)==0 ) {
sp->pvp.pvn.u.isname.name.n = CT_EXPIRES_ID;
} else
if (in->len==CT_METHODS_LEN &&
strncasecmp(in->s, CT_METHODS_S, CT_METHODS_LEN)==0 ) {
sp->pvp.pvn.u.isname.name.n = CT_METHODS_ID;
} else
if (in->len==CT_RECEIVED_LEN &&
strncasecmp(in->s, CT_RECEIVED_S, CT_RECEIVED_LEN)==0 ) {
sp->pvp.pvn.u.isname.name.n = CT_RECEIVED_ID;
} else
if (in->len==CT_PARAMS_LEN &&
strncasecmp(in->s, CT_PARAMS_S, CT_PARAMS_LEN)==0 ) {
sp->pvp.pvn.u.isname.name.n = CT_PARAMS_ID;
} else {
LM_ERR("unsupported CT field <%.*s>\n",in->len,in->s);
return -1;
}
return 0;
}
static inline int get_contact_body_field(pv_value_t *res,struct hdr_field *cth,
contact_t *ct, pv_name_t *pvn)
{
param_t *p;
if (ct==NULL) {
/* star contact hdr */
if (pvn->u.isname.name.n==0) {
res->rs = cth->body;
res->flags = PV_VAL_STR;
return 0;
}
return pv_get_null(NULL, NULL, res);
}
switch (pvn->u.isname.name.n) {
case 0: /* all body */
res->rs.s = ct->name.s?ct->name.s:ct->uri.s;
res->rs.len = ct->len;
break;
case CT_NAME_ID: /* name only */
if (ct->name.s==NULL || ct->name.len==0)
return pv_get_null(NULL, NULL, res);
res->rs = ct->name;
break;
case CT_URI_ID: /* uri only */
res->rs = ct->uri;
break;
case CT_Q_ID: /* Q param only */
if ( !ct->q || !ct->q->body.s || !ct->q->body.len)
return pv_get_null(NULL, NULL, res);
res->rs = ct->q->body;
break;
case CT_EXPIRES_ID: /* EXPIRES param only */
if (!ct->expires||!ct->expires->body.s||!ct->expires->body.len)
return pv_get_null(NULL, NULL, res);
res->rs = ct->expires->body;
break;
case CT_METHODS_ID: /* METHODS param only */
if (!ct->methods||!ct->methods->body.s||!ct->methods->body.len)
return pv_get_null(NULL, NULL, res);
res->rs = ct->methods->body;
break;
case CT_RECEIVED_ID: /* RECEIVED param only */
if(!ct->received||!ct->received->body.s||!ct->received->body.len)
return pv_get_null(NULL, NULL, res);
res->rs = ct->received->body;
break;
case CT_PARAMS_ID: /* all param */
if (!ct->params)
return pv_get_null(NULL, NULL, res);
res->rs.s = ct->params->name.s;
for( p=ct->params ; p->next ; p=p->next);
res->rs.len = p->name.s + p->len - res->rs.s;
break;
default:
LM_CRIT("BUG - unsupported ID %d\n",pvn->u.isname.type);
return pv_get_null(NULL, NULL, res);
}
res->flags = PV_VAL_STR;
return 0;
}
static int pv_get_contact_body(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
struct hdr_field *ct_h;
contact_body_t *ct_b;
contact_t *ct;
int idx;
int idxf;
char *p;
if(msg==NULL)
return -1;
/* get all CONTACT headers */
if(parse_headers(msg, HDR_EOH_F, 0)==-1 || msg->contact==NULL ||
!msg->contact->body.s || msg->contact->body.len<=0)
{
LM_DBG("no contact header!\n");
return pv_get_null(msg, param, res);
}
ct_h = msg->contact;
if (parse_contact( ct_h )!=0) {
LM_ERR("failed to parse contact hdr\n");
return -1;
}
ct_b = (contact_body_t*)ct_h->parsed;
if (ct_b==NULL)
return pv_get_null(msg, param, res);
ct = ct_b->contacts;
/* get the index */
if(pv_get_spec_index(msg, param, &idx, &idxf)!=0) {
LM_ERR("invalid index\n");
return -1;
}
if( idxf!=PV_IDX_ALL && idx==0) {
/* no index specified -> return the first contact body */
return get_contact_body_field( res , ct_h, ct, ¶m->pvn);
}
if(idxf==PV_IDX_ALL) {
/* return all contact bodies */
p = pv_local_buf;
do {
if(p!=pv_local_buf) {
if (p-pv_local_buf+PV_FIELD_DELIM_LEN+1>PV_LOCAL_BUF_SIZE){
LM_ERR("local buffer length exceeded\n");
return pv_get_null(msg, param, res);
}
memcpy(p, PV_FIELD_DELIM, PV_FIELD_DELIM_LEN);
p += PV_FIELD_DELIM_LEN;
}
get_contact_body_field( res , ct_h, ct, ¶m->pvn);
if (p-pv_local_buf+res->rs.len+1>PV_LOCAL_BUF_SIZE) {
LM_ERR("local buffer length exceeded!\n");
return pv_get_null(msg, param, res);
}
memcpy(p, res->rs.s, res->rs.len);
p += res->rs.len;
ct = ct?ct->next:NULL;
while (ct==NULL && ct_h!=NULL) {
ct_h = ct_h->sibling;
if (ct_h) {
if (parse_contact( ct_h )!=0) {
LM_ERR("failed to parse contact hdr\n");
return -1;
}
ct_b = (contact_body_t*)ct_h->parsed;
ct = ct_b->contacts;
}
}
} while (ct_h);
res->rs.s = pv_local_buf;
res->rs.len = p - pv_local_buf;
res->flags = PV_VAL_STR;
return 0;
}
/* numerical index */
if (idx<0) {
/* index from the end */
idxf=0;
while(ct_h) {
idxf++;
ct = ct?ct->next:NULL;
while (ct==NULL && ct_h!=NULL) {
ct_h = ct_h->sibling;
if (ct_h) {
if (parse_contact( ct_h)!=0) {
LM_ERR("failed to parse contact hdr\n");
return -1;
}
ct_b = (contact_body_t*)ct_h->parsed;
ct = ct_b->contacts;
}
}
}
if (-idx>idxf)
return pv_get_null(msg, param, res);
idx = idxf +idx;
ct_h = msg->contact;
ct_b = (contact_body_t*)ct_h->parsed;
ct = ct_b->contacts;
}
while (idx!=0 && ct_h) {
/* get to the next contact body */
idx--;
ct = ct?ct->next:NULL;
while (ct==NULL && ct_h!=NULL) {
ct_h = ct_h->sibling;
if (ct_h) {
if (parse_contact( ct_h )!=0) {
LM_ERR("failed to parse contact hdr\n");