/
siptrace.c
2247 lines (1910 loc) · 57.9 KB
/
siptrace.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
/*
* siptrace module - helper module to trace sip messages
*
* Copyright (C) 2006 Voice Sistem S.R.L.
* Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com)
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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
*
* Kamailio 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
*
*/
/*! \file
* siptrace module - helper module to trace sip messages
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../../core/sr_module.h"
#include "../../core/mod_fix.h"
#include "../../core/dprint.h"
#include "../../core/ut.h"
#include "../../core/ip_addr.h"
#include "../../core/mem/mem.h"
#include "../../core/mem/shm_mem.h"
#include "../../core/rpc.h"
#include "../../core/rpc_lookup.h"
#include "../../lib/srdb1/db.h"
#include "../../core/parser/parse_content.h"
#include "../../core/parser/parse_from.h"
#include "../../core/parser/parse_cseq.h"
#include "../../core/pvar.h"
#include "../../modules/tm/tm_load.h"
#include "../../modules/sl/sl.h"
#include "../../modules/dialog/dlg_load.h"
#include "../../core/str.h"
#include "../../core/onsend.h"
#include "../../core/events.h"
#include "../../core/kemi.h"
#include "siptrace_data.h"
#include "siptrace_hep.h"
#include "siptrace_send.h"
MODULE_VERSION
#define SIPTRACE_ANYADDR "any:255.255.255.255:5060"
#define SIPTRACE_ANYADDR_LEN (sizeof(SIPTRACE_ANYADDR) - 1)
#define trace_is_off(_msg) \
(((_msg)->msg_flags & FL_SIPTRACE) == 0)
#define is_null_pv(_str) \
(!str_strcmp(&_str, pv_get_null_str()))
struct tm_binds tmb;
struct dlg_binds dlgb;
/** SL API structure */
sl_api_t slb;
/* module function prototypes */
static int mod_init(void);
static int siptrace_init_rpc(void);
static int child_init(int rank);
static void destroy(void);
static int sip_trace(sip_msg_t *msg, dest_info_t *, str *, char *);
static int w_sip_trace0(struct sip_msg *, char *p1, char *p2);
static int w_sip_trace1(struct sip_msg *, char *dest, char *p2);
static int w_sip_trace2(struct sip_msg *, char *dest, char *correlation_id);
static int w_sip_trace3(struct sip_msg *, char *dest, char *correlation_id, char *trace_type);
static int fixup_siptrace(void **param, int param_no);
static int w_sip_trace_mode(sip_msg_t *msg, char *pmode, char *p2);
static int parse_siptrace_uri(str* duri, dest_info_t* dst);
static enum siptrace_type_t parse_siptrace_flag(str* sflags);
static int w_hlog1(struct sip_msg *, char *message, char *);
static int w_hlog2(struct sip_msg *, char *correlationid, char *message);
static int sip_trace_store_db(siptrace_data_t *sto);
static void trace_onreq_out(struct cell *t, int type, struct tmcb_params *ps);
static void trace_cancel_in(struct cell *t, int type, struct tmcb_params *ps);
static void trace_onreply_in(struct cell *t, int type, struct tmcb_params *ps);
static void trace_onreply_out(struct cell *t, int type, struct tmcb_params *ps);
static void trace_tm_neg_ack_in(struct cell *t, int type, struct tmcb_params *ps);
static void trace_sl_onreply_out(sl_cbp_t *slcb);
static void trace_sl_ack_in(sl_cbp_t *slcb);
static void trace_transaction(sip_msg_t* msg, siptrace_info_t* info, int dlg_tran);
static void trace_dialog(struct dlg_cell* dlg, int type, struct dlg_cb_params *params);
static void trace_dialog_transaction(struct dlg_cell* dlg, int type, struct dlg_cb_params *params);
static void free_trace_info(void* trace_info);
static int add_info_xavp(siptrace_info_t* info);
static inline int parse_raw_uri(siptrace_info_t* info);
int siptrace_net_data_recv(sr_event_param_t *evp);
int siptrace_net_data_send(sr_event_param_t *evp);
static int _siptrace_mode = 0;
static str db_url = str_init(DEFAULT_DB_URL);
static str siptrace_table = str_init("sip_trace");
static str date_column = str_init("time_stamp"); /* 00 */
static str callid_column = str_init("callid"); /* 01 */
static str traced_user_column = str_init("traced_user"); /* 02 */
static str msg_column = str_init("msg"); /* 03 */
static str method_column = str_init("method"); /* 04 */
static str status_column = str_init("status"); /* 05 */
static str fromip_column = str_init("fromip"); /* 06 */
static str toip_column = str_init("toip"); /* 07 */
static str fromtag_column = str_init("fromtag"); /* 08 */
static str direction_column = str_init("direction"); /* 09 */
static str time_us_column = str_init("time_us"); /* 10 */
static str totag_column = str_init("totag"); /* 11 */
#define XAVP_TRACE_INFO_NAME "trace_info"
static str xavp_trace_info_name_s = str_init(XAVP_TRACE_INFO_NAME);
#define NR_KEYS 12
#define SIP_TRACE_TABLE_VERSION 4
int trace_flag_param = -1;
int trace_flag = 0;
int trace_on = 0;
int trace_sl_acks = 1;
int trace_to_database = 1;
int trace_delayed = 0;
int hep_version = 1;
int hep_capture_id = 1;
int hep_vendor_id = 0;
str auth_key_str = {0, 0};
int xheaders_write = 0;
int xheaders_read = 0;
str force_send_sock_str = {0, 0};
struct sip_uri *force_send_sock_uri = 0;
str dup_uri_str = {0, 0};
struct sip_uri *dup_uri = 0;
int *trace_on_flag = NULL;
int *trace_to_database_flag = NULL;
int *xheaders_write_flag = NULL;
int *xheaders_read_flag = NULL;
static unsigned short traced_user_avp_type = 0;
static int_str traced_user_avp;
static str traced_user_avp_str = {NULL, 0};
static unsigned short trace_table_avp_type = 0;
static int_str trace_table_avp;
static str trace_table_avp_str = {NULL, 0};
static str trace_local_ip = {NULL, 0};
int hep_mode_on = 0;
db1_con_t *db_con = NULL; /*!< database connection */
db_func_t db_funcs; /*!< Database functions */
/*! \brief
* Exported functions
*/
static cmd_export_t cmds[] = {
{"sip_trace", (cmd_function)w_sip_trace0, 0, 0, 0,
ANY_ROUTE},
{"sip_trace", (cmd_function)w_sip_trace1, 1, fixup_siptrace, 0,
ANY_ROUTE},
{"sip_trace", (cmd_function)w_sip_trace2, 2, fixup_siptrace, 0,
ANY_ROUTE},
{"sip_trace", (cmd_function)w_sip_trace3, 3, fixup_siptrace, 0,
ANY_ROUTE},
{"hlog", (cmd_function)w_hlog1, 1, fixup_spve_null, 0,
ANY_ROUTE},
{"hlog", (cmd_function)w_hlog2, 2, fixup_spve_spve, 0,
ANY_ROUTE},
{"sip_trace_mode", (cmd_function)w_sip_trace_mode, 1, fixup_spve_null,
fixup_free_spve_null, ANY_ROUTE},
{0, 0, 0, 0, 0, 0}
};
/*! \brief
* Exported parameters
*/
static param_export_t params[] = {
{"auth_key", PARAM_STR, &auth_key_str},
{"db_url", PARAM_STR, &db_url},
{"table", PARAM_STR, &siptrace_table},
{"date_column", PARAM_STR, &date_column},
{"callid_column", PARAM_STR, &callid_column},
{"traced_user_column", PARAM_STR, &traced_user_column},
{"msg_column", PARAM_STR, &msg_column},
{"method_column", PARAM_STR, &method_column},
{"status_column", PARAM_STR, &status_column},
{"fromip_column", PARAM_STR, &fromip_column},
{"toip_column", PARAM_STR, &toip_column},
{"fromtag_column", PARAM_STR, &fromtag_column},
{"totag_column", PARAM_STR, &totag_column},
{"direction_column", PARAM_STR, &direction_column},
{"trace_flag", INT_PARAM, &trace_flag_param},
{"trace_on", INT_PARAM, &trace_on},
{"traced_user_avp", PARAM_STR, &traced_user_avp_str},
{"trace_table_avp", PARAM_STR, &trace_table_avp_str},
{"duplicate_uri", PARAM_STR, &dup_uri_str},
{"trace_to_database", INT_PARAM, &trace_to_database},
{"trace_local_ip", PARAM_STR, &trace_local_ip},
{"trace_sl_acks", INT_PARAM, &trace_sl_acks},
{"xheaders_write", INT_PARAM, &xheaders_write},
{"xheaders_read", INT_PARAM, &xheaders_read},
{"hep_mode_on", INT_PARAM, &hep_mode_on},
{"force_send_sock", PARAM_STR, &force_send_sock_str},
{"hep_version", INT_PARAM, &hep_version},
{"hep_capture_id", INT_PARAM, &hep_capture_id},
{"trace_delayed", INT_PARAM, &trace_delayed},
{"trace_mode", PARAM_INT, &_siptrace_mode}, {0, 0, 0}
};
#ifdef STATISTICS
stat_var *siptrace_req;
stat_var *siptrace_rpl;
stat_export_t siptrace_stats[] = {
{"traced_requests", 0, &siptrace_req},
{"traced_replies", 0, &siptrace_rpl}, {0, 0, 0}
};
#endif
/*! \brief module exports */
struct module_exports exports = {
"siptrace", /*!< module name */
DEFAULT_DLFLAGS, /*!< dlopen flags */
cmds, /*!< exported functions */
params, /*!< exported parameters */
0, /*!< exported rpc functions */
0, /*!< exported pseudo-variables */
0, /*!< response function */
mod_init, /*!< module initialization function */
child_init, /*!< child initialization function */
destroy /*!< destroy function */
};
/*! \brief Initialize siptrace module */
static int mod_init(void)
{
pv_spec_t avp_spec;
sl_cbelem_t slcb;
#ifdef STATISTICS
/* register statistics */
if(register_module_stats(exports.name, siptrace_stats) != 0) {
LM_ERR("failed to register core statistics\n");
return -1;
}
#endif
if(siptrace_init_rpc() != 0) {
LM_ERR("failed to register RPC commands\n");
return -1;
}
if(trace_flag_param!=-1) {
if(trace_flag_param < -1 || trace_flag_param > (int)MAX_FLAG) {
LM_ERR("invalid trace flag %d\n", trace_flag_param);
return -1;
}
trace_flag = 1 << trace_flag_param;
}
trace_to_database_flag = (int *)shm_malloc(sizeof(int));
if(trace_to_database_flag == NULL) {
LM_ERR("no more shm memory left\n");
return -1;
}
*trace_to_database_flag = trace_to_database;
if(hep_version != 1 && hep_version != 2 && hep_version != 3) {
LM_ERR("unsupported version of HEP");
return -1;
}
/* Find a database module if needed */
if(trace_to_database_flag != NULL && *trace_to_database_flag != 0) {
if(db_bind_mod(&db_url, &db_funcs)) {
LM_ERR("unable to bind database module\n");
return -1;
}
if(trace_to_database_flag && !DB_CAPABILITY(db_funcs, DB_CAP_INSERT)) {
LM_ERR("database modules does not provide all functions needed"
" by module\n");
return -1;
}
}
trace_on_flag = (int *)shm_malloc(sizeof(int));
if(trace_on_flag == NULL) {
LM_ERR("no more shm memory left\n");
return -1;
}
*trace_on_flag = trace_on;
xheaders_write_flag = (int *)shm_malloc(sizeof(int));
xheaders_read_flag = (int *)shm_malloc(sizeof(int));
if(!(xheaders_write_flag && xheaders_read_flag)) {
LM_ERR("no more shm memory left\n");
return -1;
}
*xheaders_write_flag = xheaders_write;
*xheaders_read_flag = xheaders_read;
/* register callbacks to TM */
if(load_tm_api(&tmb) != 0) {
LM_WARN("can't load tm api. Will not install tm callbacks.\n");
}
if (load_dlg_api(&dlgb) < 0) {
LM_INFO("can't load dlg api. Will not install dialog callbacks.\n");
} else {
if (dlgb.register_dlgcb(NULL, DLGCB_CREATED, trace_dialog, NULL, NULL) != 0) {
LM_ERR("failed to register dialog callbacks! Tracing dialogs won't be available\n");
}
}
/* bind the SL API */
if(sl_load_api(&slb) != 0) {
LM_WARN("cannot bind to SL API. Will not install sl callbacks.\n");
} else {
/* register sl callbacks */
memset(&slcb, 0, sizeof(sl_cbelem_t));
slcb.type = SLCB_REPLY_READY;
slcb.cbf = trace_sl_onreply_out;
if(slb.register_cb(&slcb) != 0) {
LM_ERR("can't register for SLCB_REPLY_READY\n");
return -1;
}
if(trace_sl_acks) {
slcb.type = SLCB_ACK_FILTERED;
slcb.cbf = trace_sl_ack_in;
if(slb.register_cb(&slcb) != 0) {
LM_ERR("can't register for SLCB_ACK_FILTERED\n");
return -1;
}
}
}
if(dup_uri_str.s != 0) {
dup_uri = (struct sip_uri *)pkg_malloc(sizeof(struct sip_uri));
if(dup_uri == 0) {
LM_ERR("no more pkg memory left\n");
return -1;
}
memset(dup_uri, 0, sizeof(struct sip_uri));
if(parse_uri(dup_uri_str.s, dup_uri_str.len, dup_uri) < 0) {
LM_ERR("bad duplicate_uri\n");
return -1;
}
if(!dup_uri->proto) {
dup_uri->proto = PROTO_UDP;
}
if(!dup_uri->port_no) {
dup_uri->port_no = SIP_PORT;
}
}
if(force_send_sock_str.s != 0) {
force_send_sock_str.len = strlen(force_send_sock_str.s);
force_send_sock_uri =
(struct sip_uri *)pkg_malloc(sizeof(struct sip_uri));
if(force_send_sock_uri == 0) {
LM_ERR("no more pkg memory left\n");
return -1;
}
memset(force_send_sock_uri, 0, sizeof(struct sip_uri));
if(parse_uri(force_send_sock_str.s, force_send_sock_str.len,
force_send_sock_uri)
< 0) {
LM_ERR("bad force_send_sock\n");
return -1;
}
if(!force_send_sock_uri->proto) {
force_send_sock_uri->proto = PROTO_UDP;
}
if(!force_send_sock_uri->port_no) {
force_send_sock_uri->port_no = SIP_PORT;
}
}
if(traced_user_avp_str.s && traced_user_avp_str.len > 0) {
if(pv_parse_spec(&traced_user_avp_str, &avp_spec) == 0
|| avp_spec.type != PVT_AVP) {
LM_ERR("malformed or non AVP %.*s AVP definition\n",
traced_user_avp_str.len, traced_user_avp_str.s);
return -1;
}
if(pv_get_avp_name(
0, &avp_spec.pvp, &traced_user_avp, &traced_user_avp_type)
!= 0) {
LM_ERR("[%.*s] - invalid AVP definition\n", traced_user_avp_str.len,
traced_user_avp_str.s);
return -1;
}
} else {
traced_user_avp.n = 0;
traced_user_avp_type = 0;
}
if(trace_table_avp_str.s && trace_table_avp_str.len > 0) {
if(pv_parse_spec(&trace_table_avp_str, &avp_spec) == 0
|| avp_spec.type != PVT_AVP) {
LM_ERR("malformed or non AVP %.*s AVP definition\n",
trace_table_avp_str.len, trace_table_avp_str.s);
return -1;
}
if(pv_get_avp_name(
0, &avp_spec.pvp, &trace_table_avp, &trace_table_avp_type)
!= 0) {
LM_ERR("[%.*s] - invalid AVP definition\n", trace_table_avp_str.len,
trace_table_avp_str.s);
return -1;
}
} else {
trace_table_avp.n = 0;
trace_table_avp_type = 0;
}
if(_siptrace_mode == 1) {
sr_event_register_cb(SREV_NET_DATA_RECV, siptrace_net_data_recv);
sr_event_register_cb(SREV_NET_DATA_SEND, siptrace_net_data_send);
}
return 0;
}
static int child_init(int rank)
{
if(rank == PROC_INIT || rank == PROC_MAIN || rank == PROC_TCP_MAIN)
return 0; /* do nothing for the main process */
if(trace_to_database_flag != NULL && *trace_to_database_flag != 0) {
db_con = db_funcs.init(&db_url);
if(!db_con) {
LM_ERR("unable to connect to database. Please check "
"configuration.\n");
return -1;
}
if(DB_CAPABILITY(db_funcs, DB_CAP_QUERY)) {
if(db_check_table_version(
&db_funcs, db_con, &siptrace_table, SIP_TRACE_TABLE_VERSION)
< 0) {
DB_TABLE_VERSION_ERROR(siptrace_table);
db_funcs.close(db_con);
db_con = 0;
return -1;
}
}
}
return 0;
}
static void destroy(void)
{
if(trace_to_database_flag != NULL) {
if(db_con != NULL) {
db_funcs.close(db_con);
}
shm_free(trace_to_database_flag);
}
if(trace_on_flag) {
shm_free(trace_on_flag);
}
}
static inline str *siptrace_get_table(void)
{
static int_str avp_value;
struct usr_avp *avp;
if(trace_table_avp.n == 0)
return &siptrace_table;
avp = NULL;
if(trace_table_avp.n != 0)
avp = search_first_avp(
trace_table_avp_type, trace_table_avp, &avp_value, 0);
if(avp == NULL || !is_avp_str_val(avp) || avp_value.s.len <= 0)
return &siptrace_table;
return &avp_value.s;
}
static int sip_trace_store(siptrace_data_t *sto, dest_info_t *dst,
str *correlation_id_str)
{
int ret = 1;
if(sto == NULL) {
LM_DBG("invalid parameter\n");
return -1;
}
gettimeofday(&sto->tv, NULL);
if(sip_trace_xheaders_read(sto) != 0)
return -1;
ret = sip_trace_store_db(sto);
if(sip_trace_xheaders_write(sto) != 0)
return -1;
if(hep_mode_on) {
trace_send_hep_duplicate(
&sto->body, &sto->fromip, &sto->toip, dst, correlation_id_str);
} else {
if(dst) {
trace_send_duplicate(sto->body.s, sto->body.len, dst);
}
}
if(sip_trace_xheaders_free(sto) != 0)
return -1;
return ret;
}
static int sip_trace_store_db(siptrace_data_t *sto)
{
if(trace_to_database_flag == NULL || *trace_to_database_flag == 0)
goto done;
if(db_con == NULL) {
LM_DBG("database connection not initialized\n");
return -1;
}
db_key_t db_keys[NR_KEYS];
db_val_t db_vals[NR_KEYS];
db_keys[0] = &msg_column;
db_vals[0].type = DB1_BLOB;
db_vals[0].nul = 0;
db_vals[0].val.blob_val = sto->body;
db_keys[1] = &callid_column;
db_vals[1].type = DB1_STR;
db_vals[1].nul = 0;
db_vals[1].val.str_val = sto->callid;
db_keys[2] = &method_column;
db_vals[2].type = DB1_STR;
db_vals[2].nul = 0;
db_vals[2].val.str_val = sto->method;
db_keys[3] = &status_column;
db_vals[3].type = DB1_STR;
db_vals[3].nul = 0;
db_vals[3].val.str_val = sto->status;
db_keys[4] = &fromip_column;
db_vals[4].type = DB1_STR;
db_vals[4].nul = 0;
db_vals[4].val.str_val = sto->fromip;
db_keys[5] = &toip_column;
db_vals[5].type = DB1_STR;
db_vals[5].nul = 0;
db_vals[5].val.str_val = sto->toip;
db_keys[6] = &date_column;
db_vals[6].type = DB1_DATETIME;
db_vals[6].nul = 0;
db_vals[6].val.time_val = sto->tv.tv_sec;
db_keys[7] = &direction_column;
db_vals[7].type = DB1_STRING;
db_vals[7].nul = 0;
db_vals[7].val.string_val = sto->dir;
db_keys[8] = &fromtag_column;
db_vals[8].type = DB1_STR;
db_vals[8].nul = 0;
db_vals[8].val.str_val = sto->fromtag;
db_keys[9] = &traced_user_column;
db_vals[9].type = DB1_STR;
db_vals[9].nul = 0;
db_keys[10] = &time_us_column;
db_vals[10].type = DB1_INT;
db_vals[10].nul = 0;
db_vals[10].val.int_val = sto->tv.tv_usec;
db_keys[11] = &totag_column;
db_vals[11].type = DB1_STR;
db_vals[11].nul = 0;
db_vals[11].val.str_val = sto->totag;
db_funcs.use_table(db_con, siptrace_get_table());
if(trace_on_flag != NULL && *trace_on_flag != 0) {
db_vals[9].val.str_val.s = "";
db_vals[9].val.str_val.len = 0;
LM_DBG("storing info...\n");
if(trace_delayed != 0 && db_funcs.insert_delayed != NULL) {
if(db_funcs.insert_delayed(db_con, db_keys, db_vals, NR_KEYS) < 0) {
LM_ERR("error storing trace\n");
goto error;
}
} else {
if(db_funcs.insert(db_con, db_keys, db_vals, NR_KEYS) < 0) {
LM_ERR("error storing trace\n");
goto error;
}
}
#ifdef STATISTICS
update_stat(sto->stat, 1);
#endif
}
if(sto->avp == NULL)
goto done;
db_vals[9].val.str_val = sto->avp_value.s;
LM_DBG("storing info...\n");
if(trace_delayed != 0 && db_funcs.insert_delayed != NULL) {
if(db_funcs.insert_delayed(db_con, db_keys, db_vals, NR_KEYS) < 0) {
LM_ERR("error storing trace\n");
goto error;
}
} else {
if(db_funcs.insert(db_con, db_keys, db_vals, NR_KEYS) < 0) {
LM_ERR("error storing trace\n");
goto error;
}
}
sto->avp = search_next_avp(&sto->state, &sto->avp_value);
while(sto->avp != NULL) {
db_vals[9].val.str_val = sto->avp_value.s;
LM_DBG("storing info...\n");
if(trace_delayed != 0 && db_funcs.insert_delayed != NULL) {
if(db_funcs.insert_delayed(db_con, db_keys, db_vals, NR_KEYS) < 0) {
LM_ERR("error storing trace\n");
goto error;
}
} else {
if(db_funcs.insert(db_con, db_keys, db_vals, NR_KEYS) < 0) {
LM_ERR("error storing trace\n");
goto error;
}
}
sto->avp = search_next_avp(&sto->state, &sto->avp_value);
}
done:
return 1;
error:
return -1;
}
enum siptrace_type_t parse_siptrace_flag(str* sflags)
{
int idx;
enum siptrace_type_t trace_type = SIPTRACE_NONE;
if (sflags == NULL || sflags->s == NULL || sflags->len == 0) {
return SIPTRACE_NONE;
}
for (idx = 0; idx < sflags->len; idx++) {
switch(sflags->s[idx]|0x20) { /*|0x20 - to lowercase */
case SIPTRACE_MESSAGE:
case SIPTRACE_TRANSACTION:
case SIPTRACE_DIALOG:
if (trace_type != SIPTRACE_NONE) {
LM_ERR("only one tracing flag can be used <%.*s>!\n",
sflags->len, sflags->s);
return SIPTRACE_NONE;
}
trace_type = (sflags->s[idx]|0x20);
break;
case ' ':
case '\t':
break;
default:
LM_ERR("Invalid character <%c> in <%.*s> at position <%d>!\n", sflags->s[idx],
sflags->len, sflags->s, idx);
return SIPTRACE_NONE;
}
}
return trace_type;
}
static int fixup_siptrace(void **param, int param_no)
{
str sflags;
enum siptrace_type_t trace_type;
if(param_no < 1 || param_no > 3) {
LM_DBG("params:%s\n", (char *)*param);
return 0;
}
if (param_no == 1 || param_no == 2) {
/* correlation id */
return fixup_spve_all(param, param_no);
} else if (param_no == 3) {
/* tracing type; string only */
sflags.s = (char *)*param;
sflags.len = strlen(sflags.s);
trace_type = parse_siptrace_flag(&sflags);
if (trace_type == SIPTRACE_NONE) {
LM_ERR("Failed to parse trace type!\n");
return -1;
}
*param = pkg_malloc(sizeof(trace_type));
if (*param == NULL) {
LM_ERR("no more pkg memory!\n");
return -1;
}
memcpy(*param, &trace_type, sizeof(trace_type));
}
return 0;
}
/**
*
* parse_siptrace_uri (to replace siptrace_fixup and ki_sip_trace_dst_cid beginning)
* parse_siptrace_type
*
*/
static int parse_siptrace_uri(str* duri, dest_info_t* dst)
{
sip_uri_t uri;
proxy_l_t *p = NULL;
if (dst == NULL) {
LM_ERR("bad destination!\n");
return -1;
}
if (duri == NULL || duri->len <= 0) {
if(dup_uri) {
uri = *dup_uri;
} else {
LM_ERR("Missing duplicate URI\n");
return -1;
}
} else {
memset(&uri, 0, sizeof(struct sip_uri));
if(parse_uri(duri->s, duri->len, &uri) < 0) {
LM_ERR("bad dup uri\n");
return -1;
}
if(!uri.proto) {
uri.proto = PROTO_UDP;
}
if(!uri.port_no) {
uri.port_no = SIP_PORT;
}
}
init_dest_info(dst);
/* create a temporary proxy*/
dst->proto = uri.proto;
p = mk_proxy(&uri.host, uri.port_no, dst->proto);
if(p == 0) {
LM_ERR("bad host name in uri\n");
return -1;
}
hostent2su(&dst->to, &p->host, p->addr_idx, (p->port) ? p->port : SIP_PORT);
/* free temporary proxy*/
if(p) {
free_proxy(p); /* frees only p content, not p itself */
pkg_free(p);
}
return 0;
}
/**
*
*/
static int sip_trace_helper(sip_msg_t *msg, dest_info_t *dst, str *duri,
str *corid, char *dir, enum siptrace_type_t trace_type)
{
siptrace_info_t* info = NULL;
struct cell *t_invite;
char *p = NULL;
if (trace_type == SIPTRACE_TRANSACTION || trace_type == SIPTRACE_DIALOG) {
int alloc_size = sizeof(siptrace_info_t);
/*
* for each type check that conditions are created
* transaction: it's a request starting a transaction; tm module loaded
* dialog: it's an INVITE; dialog module is loaded
*
* */
if (tmb.t_gett == NULL) {
LM_WARN("TM module not loaded! Tracing only current message!\n");
goto trace_current;
}
/* if sip_trace is called over an incoming CANCEL, skip
* capturing it if the cancelled transaction is already being traced
*/
if (msg->REQ_METHOD==METHOD_CANCEL) {
t_invite=tmb.t_lookup_original(msg);
if (t_invite!=T_NULL_CELL) {
if (t_invite->uas.request->msg_flags & FL_SIPTRACE) {
LM_DBG("Transaction is already been traced, skipping.\n");
tmb.t_unref(msg);
return 1;
}
tmb.t_unref(msg);
}
}
if (trace_type == SIPTRACE_DIALOG && dlgb.get_dlg == NULL) {
LM_WARN("DIALOG module not loaded! Tracing only current message!\n");
goto trace_current;
}
if (msg->first_line.type != SIP_REQUEST ||
(trace_type == SIPTRACE_DIALOG && msg->first_line.u.request.method_value != METHOD_INVITE)) {
LM_WARN("When tracing a %s sip_trace() has to be initiated on the %s\n",
trace_type == SIPTRACE_TRANSACTION ? "transaction" : "dialog",
trace_type == SIPTRACE_TRANSACTION ? "request message" : "initial invite");
return -1;
}
if (corid) {
alloc_size += corid->len;
}
if (duri) {
alloc_size += duri->len;
}
info = shm_malloc(alloc_size);
if (info == NULL) {
LM_ERR("No more shm!\n");
return -1;
}
memset(info, 0, alloc_size);
p = (char *)(info + 1);
/* could use the dest_info we've already parsed but there's no way to pass
* it to DLGCB_CREATED callback so the only thing to do is keep
* it as uri, serialize in a dlg_var and parse again in DLGCB_CREATED */
if(corid) {
info->correlation_id.s = p;
info->correlation_id.len = corid->len;
memcpy(info->correlation_id.s, corid->s, corid->len);
}
if (duri) {
info->uriState = STRACE_RAW_URI;
info->u.dup_uri.s = p + ((info->correlation_id.s)?info->correlation_id.len:0);
memcpy(info->u.dup_uri.s, duri->s, duri->len);
info->u.dup_uri.len = duri->len;
} else {
info->uriState = STRACE_UNUSED_URI;
}
if (trace_type == SIPTRACE_TRANSACTION) {
trace_transaction(msg, info, 0);
} else if (trace_type == SIPTRACE_DIALOG) {
if (unlikely(dlgb.set_dlg_var == NULL)) {
/* FIXME should we abort tracing here? */
LM_WARN("Dialog api not loaded! will trace only current transaction!\n");
} else {
/* serialize what's in info */
/* save correlation id in siptrace_info avp
* we want to have traced user avp value at the moment of sip_trace function call*/
if (add_info_xavp(info) < 0) {
LM_ERR("failed to serialize siptrace info! Won't trace dialog!\n");
return -1;
} else {
msg->msg_flags |= FL_SIPTRACE;
}
}
/**
* WARNING: don't move trace_transaction before add_info_xavp()
* add_info_xavp() expects the URI in RAW format, unparsed
* trace_transaction() parses the URI if it finds it in raw format;
* a BUG will be thrown if this happens
*/
trace_transaction(msg, info, 1);
}
}
if(trace_type != SIPTRACE_MESSAGE && trace_is_off(msg)) {
LM_DBG("trace off...\n");
return 1;
}
trace_current:
return sip_trace(msg, dst, corid, dir);
}
/**
* Send sip trace with destination and correlation id and specify what messages to be traced
*/
static int ki_sip_trace_dst_cid_flag(sip_msg_t *msg, str *duri, str *cid, str* sflag)
{
dest_info_t dst;
enum siptrace_type_t trace_type;
if(duri) {
if (parse_siptrace_uri(duri, &dst) < 0) {
LM_ERR("failed to parse siptrace uri!\n");
return -1;
}
}
if (sflag) {
trace_type = parse_siptrace_flag(sflag);
if (trace_type == SIPTRACE_NONE) {
LM_ERR("Invalid flags <%.*s>\n", sflag->len, sflag->s);
}
} else {
trace_type = SIPTRACE_MESSAGE;
}
return sip_trace_helper(msg, (duri)?&dst:NULL, duri, cid, NULL, trace_type);
}
/**
* Send sip trace with destination and correlation id
*/
static int ki_sip_trace_dst_cid(sip_msg_t *msg, str *duri, str *cid)
{
return ki_sip_trace_dst_cid_flag(msg, duri, cid, NULL);
}
/**
* Send sip trace with destination
*/
static int ki_sip_trace_dst(sip_msg_t *msg, str *duri)
{
return ki_sip_trace_dst_cid_flag(msg, duri, NULL, NULL);
}
/**
*
*/
static int ki_sip_trace(sip_msg_t *msg)
{
return ki_sip_trace_dst_cid_flag(msg, NULL, NULL, NULL);
}