-
Notifications
You must be signed in to change notification settings - Fork 564
/
tracer.c
3398 lines (2827 loc) · 85.2 KB
/
tracer.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
/*
* tracer module - helper module to trace sip messages
*
* Copyright (C) 2006-2009 Voice Sistem S.R.L.
*
* 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
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../../net/net_tcp.h"
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../ut.h"
#include "../../ip_addr.h"
#include "../../mem/mem.h"
#include "../../mem/shm_mem.h"
#include "../../mi/mi.h"
#include "../../db/db.h"
#include "../../db/db_insertq.h"
#include "../../parser/parse_content.h"
#include "../../parser/parse_from.h"
#include "../../pvar.h"
#include "../../sl_cb.h"
#include "../../str.h"
#include "../../script_cb.h"
#include "../tm/tm_load.h"
#include "../dialog/dlg_load.h"
#include "../../mod_fix.h"
#include "tracer.h"
/* trace info context position */
int sl_ctx_idx=-1;
/* DB structures used for all queries */
db_key_t db_keys[NR_KEYS];
db_val_t db_vals[NR_KEYS];
static db_ps_t siptrace_ps = NULL;
//static query_list_t *ins_list = NULL;
#define trace_id(_tid) ((tlist_elem_p)(_tid))
#define trace_id_dyn(_tid) ((tlist_dyn_elem_p)(_tid))
#define trace_id_ref(_tid) (trace_id_dyn(_tid)->ref++)
#define trace_id_unref(_tid) \
do { \
trace_id_dyn(_tid)->ref--; \
if (trace_id_dyn(_tid)->ref == 0) { \
if (trace_id(_tid)->type == TYPE_HEP) \
tprot.release_trace_dest(trace_id(_tid)->el.hep.hep_id); \
if (trace_id_dyn(_tid)->filters) \
free_trace_filters(trace_id_dyn(_tid)->filters); \
shm_free(_tid); \
} \
} while (0)
struct tm_binds tmb;
struct dlg_binds dlgb;
static trace_proto_t tprot;
/* "sip" tracing identifier */
int sip_trace_id;
/* module function prototypes */
static int mod_init(void);
static int child_init(int rank);
static void destroy(void);
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 trace_attrs_column = str_init("trace_attrs"); /* 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 fromproto_column = str_init("from_proto"); /* 06 */
static str fromip_column = str_init("from_ip"); /* 07 */
static str fromport_column = str_init("from_port"); /* 08 */
static str toproto_column = str_init("to_proto"); /* 09 */
static str toip_column = str_init("to_ip"); /* 10 */
static str toport_column = str_init("to_port"); /* 11 */
static str fromtag_column = str_init("fromtag"); /* 12 */
static str direction_column = str_init("direction"); /* 13 */
int trace_on = 1;
int *trace_on_flag = NULL;
static str trace_local_proto = {NULL, 0};
static str trace_local_ip = {NULL, 0};
static unsigned short trace_local_port = 0;
static tlist_elem_p trace_list=NULL;
static tlist_elem_p *dyn_trace_list=NULL;
static gen_lock_t *dyn_trace_lock;
static const char* corr_id_s="correlation_id";
static int corr_vendor = -1;
static int corr_id=-1;
/**
* TRACER FUNCTIONS
*
* **/
static int fixup_tid(void **param);
static int fixup_sflags(void **param);
static int trace_w(struct sip_msg *msg, tlist_elem_p list,
void *scope_p, str *trace_types_s, str *trace_attrs);
static int sip_trace(struct sip_msg*, trace_info_p);
static int sip_trace_instance(struct sip_msg*, trace_instance_p, int);
static int trace_dialog(struct sip_msg*, trace_info_p);
static int trace_transaction(struct sip_msg* msg, trace_info_p info,
char dlg_tran);
static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps);
static void trace_tm_in(struct cell* t, int type, struct tmcb_params *ps);
static void trace_tm_out(struct cell* t, int type, struct tmcb_params *ps);
static void trace_msg_out(struct sip_msg* req, str *buffer,
struct socket_info* send_sock, int proto, union sockaddr_union *to,
trace_info_p info);
static void siptrace_dlg_cancel(struct cell* t, int type, struct tmcb_params *param);
/*
* callback used for statelessly forwarded requests; also catches the ACK in
* stateful transaction
*/
static void trace_slreq_out(struct sip_msg* req, str *buffer,int rpl_code,
union sockaddr_union *to, struct socket_info *sock, int proto);
static void trace_slreply_out(struct sip_msg* req, str *buffer,int rpl_code,
union sockaddr_union *dst, struct socket_info *sock, int proto);
#if 0
static void trace_slack_in(struct sip_msg* req, str *buffer,int rpl_code,
union sockaddr_union *dst, struct socket_info *sock, int proto);
#endif
static mi_response_t *sip_trace_mi(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *sip_trace_mi_tid(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *sip_trace_mi_mode(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *sip_trace_mi_2(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *sip_trace_mi_dyn(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *sip_trace_mi_stop(const mi_params_t *params,
struct mi_handler *async_hdl);
static int trace_send_duplicate(char *buf, int len, struct sip_uri *uri);
static int send_trace_proto_duplicate(trace_dest dest,
str* correlation, trace_instance_p info, int conn_id);
static int api_is_id_traced(int id);
static int is_id_traced(int id, trace_instance_p info);
static int pipport2su (str *sproto, str *ip, unsigned short port,
union sockaddr_union *tmp_su, unsigned int *proto);
static int parse_trace_id(unsigned int type, void *val);
void free_trace_info_pkg(void *param);
void free_trace_info_shm(void *param);
static void free_trace_filters(struct trace_filter *list);
static int init_dyn_tracing(void);
static void destroy_dyn_tracing(void);
static int process_dyn_tracing(struct sip_msg *msg, void *param);
/*
* Exported functions
*/
static cmd_export_t cmds[] = {
{"trace", (cmd_function)trace_w, {
{CMD_PARAM_STR, fixup_tid, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, fixup_sflags, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{0,0,{{0,0,0}},0}
};
/*
* Exported parameters
*/
static param_export_t params[] = {
{"trace_id", STR_PARAM|USE_FUNC_PARAM, parse_trace_id},
{"date_column", STR_PARAM, &date_column.s },
{"callid_column", STR_PARAM, &callid_column.s },
{"trace_attrs_column", STR_PARAM,&trace_attrs_column.s},
{"msg_column", STR_PARAM, &msg_column.s },
{"method_column", STR_PARAM, &method_column.s },
{"status_column", STR_PARAM, &status_column.s },
{"fromproto_column", STR_PARAM, &fromproto_column.s },
{"fromip_column", STR_PARAM, &fromip_column.s },
{"fromport_column", STR_PARAM, &fromport_column.s },
{"toproto_column", STR_PARAM, &toproto_column.s },
{"toip_column", STR_PARAM, &toip_column.s },
{"toport_column", STR_PARAM, &toport_column.s },
{"fromtag_column", STR_PARAM, &fromtag_column.s },
{"direction_column", STR_PARAM, &direction_column.s },
{"trace_on", INT_PARAM, &trace_on },
{"trace_local_ip", STR_PARAM, &trace_local_ip.s },
{0, 0, 0}
};
static mi_export_t mi_cmds[] = {
{ "trace", 0, MI_NAMED_PARAMS_ONLY, 0, {
{sip_trace_mi, {0}},
{sip_trace_mi_tid, {"id", 0}},
{sip_trace_mi_mode, {"mode", 0}},
{sip_trace_mi_2,{"id", "mode", 0}},
{EMPTY_MI_RECIPE}
}
},
{ "trace_start", 0, 0, 0, {
{sip_trace_mi_dyn,{"id", "uri", 0}},
{sip_trace_mi_dyn,{"id", "uri", "filter", 0}},
{sip_trace_mi_dyn,{"id", "uri", "filter", "scope", "type", 0}},
{EMPTY_MI_RECIPE}
}
},
{ "trace_stop", 0, 0, 0, {
{sip_trace_mi_stop,{"id", 0}},
{EMPTY_MI_RECIPE}
}
},
{EMPTY_MI_EXPORT}
};
#ifdef STATISTICS
#include "../../statistics.h"
stat_var* siptrace_req;
stat_var* siptrace_rpl;
static stat_export_t siptrace_stats[] = {
{"traced_requests" , 0, &siptrace_req },
{"traced_replies" , 0, &siptrace_rpl },
{0,0,0}
};
#endif
static module_dependency_t *get_deps_hep(param_export_t *param)
{
tlist_elem_p it;
for (it=trace_list;it;it=it->next)
if (it->type==TYPE_HEP)
return alloc_module_dep(MOD_TYPE_DEFAULT, "proto_hep", DEP_ABORT);
else if (it->type==TYPE_DB)
return alloc_module_dep(MOD_TYPE_SQLDB, NULL, DEP_ABORT);
return NULL;
}
static dep_export_t deps = {
{ /* OpenSIPS module dependencies */
{ MOD_TYPE_NULL, NULL, 0 },
},
{ /* modparam dependencies */
{"trace_id", get_deps_hep},
{ NULL, NULL },
},
};
/* module exports */
struct module_exports exports = {
"tracer",
MOD_TYPE_DEFAULT,/* class of this module */
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
&deps, /* OpenSIPS module dependencies */
cmds, /* Exported functions */
0, /* Exported async functions */
params, /* Exported parameters */
#ifdef STATISTICS
siptrace_stats,
#else
0, /* exported statistics */
#endif
mi_cmds, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* exported transformations */
0, /* extra processes */
mod_init, /* module initialization function */
0, /* response function */
destroy, /* destroy function */
child_init, /* child initialization function */
0 /* reload confirm function */
};
static int
get_db_struct(str *url, str *tb_name, st_db_struct_t **st_db)
{
st_db_struct_t *dbs;
dbs = pkg_malloc(sizeof(st_db_struct_t));
if (st_db == NULL) {
LM_ERR("invalid output parameter!\n");
return -1;
}
if (url == NULL || url->s == NULL || url->len == 0) {
LM_ERR("invalid URL!\n");
return -1;
}
/* if not set populated with 'tracer_table' in parse_siptrace_id() */
dbs->table = *tb_name;
if (db_bind_mod(url, &dbs->funcs)) {
LM_ERR("unable to bind database module\n");
return -1;
}
if (!DB_CAPABILITY(dbs->funcs, DB_CAP_INSERT)) {
LM_ERR("database modules does not provide all functions needed by module\n");
return -1;
}
if ((dbs->con=dbs->funcs.init(url)) == 0) {
LM_CRIT("Cannot connect to DB\n");
return -1;
}
if (db_check_table_version(&dbs->funcs, dbs->con,
&dbs->table, TRACER_TABLE_VERSION) < 0) {
LM_ERR("error during table version check.\n");
return -1;
}
dbs->url = *url;
dbs->funcs.close(dbs->con);
dbs->con = 0;
*st_db = dbs;
return 0;
}
static inline void get_value_and_update(str* token, char delim, str* value)
{
char* end;
value->s = token->s;
if ( !(end=q_memchr(token->s, delim, token->len)) ) {
value->len = token->len;
token->len = 0;
} else {
value->len = end - token->s;
token->len = token->len - ((end + 1) - token->s);
/* might have a negative value if ';' is the last character */
if ( token->len > 0 )
token->s = end + 1;
}
str_trim_spaces_lr(*value);
}
static int
parse_siptrace_uri(str* token, str* uri, str* param1)
{
static const char key_value_delim = '=';
static const char param_delim = ';';
static const char db_delim = '\\';
static str uri_str={"uri", sizeof("uri")-1};
static str tb_name_str={"table", sizeof("table")-1};
char *end;
str key, value, aux;
/* we must be careful because there might be a mysql password containing
* our delimiters; so we first parse the key then carefully go through
* the value if it's an uri */
str_trim_spaces_lr(*token);
while ( token->len > 0 ) {
if ( !(end=q_memchr(token->s, key_value_delim, token->len)) ) {
LM_ERR("key delimiter '=' not found in <%.*s>\n", token->len, token->s);
return -1;
}
key.s = token->s;
key.len = end - token->s;
str_trim_spaces_lr(key);
token->len -= ((end + 1) - token->s);
token->s = end + 1;
if ( !strncmp(key.s, uri_str.s, uri_str.len) ) {
if ( !(end=q_memrchr( token->s, db_delim, token->len)) ) {
/* no '/' so it's not a db url; just do a normal parse */
get_value_and_update( token, param_delim, &value);
} else {
aux.s = end;
aux.len = token->len - (end - token->s);
value.s = token->s;
/* now search the actual delimiter */
if ( !(end=q_memchr( aux.s, param_delim, aux.len)) ) {
value.len = token->len;
token->len = 0;
} else {
value.len = end - token->s;
token->len = token->len - ((end+1) - token->s);
if ( token->len > 0 )
token->s = end + 1;
}
}
*uri = value;
} else if ( !strncmp(key.s, tb_name_str.s, tb_name_str.len) ) {
get_value_and_update( token, param_delim, &value);
*param1 = value;
} else {
LM_ERR("Invalid key <%.*s> in trace id!\n", key.len, key.s);
return -1;
}
}
return 0;
}
static void get_siptrace_type(str *name, str *trace_uri, str *param1,
unsigned int *hash, enum types *type)
{
#define IS_HEP_URI(__url__) ((__url__->len > 3/*O_o*/ \
&& (__url__->s[0]|0x20) == 'h' && (__url__->s[1]|0x20) == 'e' \
&& (__url__->s[2]|0x20) == 'p'))
#define IS_SIP_URI(__url__) ((__url__->len > 3/*O_o*/ \
&& (__url__->s[0]|0x20) == 's' && (__url__->s[1]|0x20) == 'i' \
&& (__url__->s[2]|0x20) == 'p'))
#define IS_UDP(__url__) ((__url__.len == 3/*O_o*/ \
&& (__url__.s[0]|0x20) == 'u' && (__url__.s[1]|0x20) == 'd' \
&& (__url__.s[2]|0x20) == 'p'))
#define IS_TCP(__url__) ((__url__.len == 3/*O_o*/ \
&& (__url__.s[0]|0x20) == 't' && (__url__.s[1]|0x20) == 'c' \
&& (__url__.s[2]|0x20) == 'p'))
unsigned int param_hash;
*hash = core_hash(name, trace_uri, 0);
if (IS_HEP_URI(trace_uri)) {
/* jump over 'hep:' prefix and keep the name; will be loaded in mod init */
*type = TYPE_HEP;
trace_uri->len -= HEP_PREFIX_LEN;
trace_uri->s += HEP_PREFIX_LEN;
} else if (IS_SIP_URI(trace_uri)) {
*type = TYPE_SIP;
} else {
/* need to take the table into account */
if (param1 && (param1->s == NULL || param1->len == 0))
param1 = &siptrace_table;
param_hash = core_hash(trace_uri, param1, 0);
*hash ^= (param_hash>>3);
*type = TYPE_DB;
}
#undef IS_HEP_URI
#undef IS_SIP_URI
#undef IS_TCP
#undef IS_UDP
}
static inline tlist_elem_p get_siptrace_id(str *name, unsigned int hash, enum types type)
{
tlist_elem_p el;
for (el = trace_list; el; el = el->next)
if (el->type == type && el->uri_hash == hash)
return el;
return NULL;
}
static inline tlist_elem_p get_dyn_siptrace_id(str *name, unsigned int hash, enum types type,
int leave_locked)
{
tlist_elem_p el;
lock_get(dyn_trace_lock);
for (el = *dyn_trace_list; el; el = el->next)
if (el->type == type && el->uri_hash == hash)
break;
if (!leave_locked)
lock_release(dyn_trace_lock);
return el;
}
static int parse_siptrace_id(str *suri)
{
#define PARSE_NAME(__uri, __name) \
do { \
while (__uri->s[0]==' ') \
(__uri->s++, __uri->len--); \
__name.s = __uri->s; \
while (__uri->len \
&& (__uri->s[0] != ']' && __uri->s[0] != ' ')) \
(__uri->s++, __uri->len--, __name.len++); \
\
if (*(__uri->s-1) != ']') \
while (__uri->len && __uri->s[0] != ']') \
(__uri->s++, __uri->len--); \
\
if (!__uri->len || __uri->s[0] != ']') { \
LM_ERR("bad name [%.*s]!\n", __uri->len, __uri->s); \
return -1; \
} \
(__uri->s++, __uri->len--); \
} while(0);
unsigned int hash;
str name={NULL, 0};
str trace_uri;
str param1={NULL, 0};
tlist_elem_p elem;
enum types uri_type;
if (suri == NULL) {
LM_ERR("bad input parameters!\n");
return -1;
}
/*format: [<proto>]uri; it should never have
* less than 5 */
if (suri->len < 5) {
LM_ERR("suri too short!\n");
return -1;
}
/* we consider the str trimmed before the function */
if (suri->s[0] != '[') {
LM_ERR("bad format for uri {%.*s}\n", suri->len, suri->s);
return -1;
} else {
suri->s++;
suri->len--;
}
PARSE_NAME(suri, name); /*parse '[<name>]'*/
if (parse_siptrace_uri(suri, &trace_uri, ¶m1) < 0) {
LM_ERR("invalid uri <%.*s>\n", suri->len, suri->s);
return -1;
}
get_siptrace_type(&name, &trace_uri, ¶m1, &hash, &uri_type);
if (get_siptrace_id(&name, hash, uri_type))
return 0;
LM_DBG("allocating %d [%.*s]\n", hash, name.len, name.s);
elem = pkg_malloc(sizeof(tlist_elem_t));
if (!elem) {
LM_ERR("could not allocate elem for %.*s!\n", name.len, name.s);
return -1;
}
memset(elem, 0, sizeof(tlist_elem_t));
elem->type = uri_type;
elem->hash = core_hash(&name, NULL, 0);
elem->uri_hash = hash;
elem->name = name;
if (uri_type == TYPE_DB) {
if (get_db_struct(&trace_uri, ¶m1, &elem->el.db) < 0) {
LM_ERR("Invalid parameters extracted!url <%.*s>! table name <%.*s>!\n",
trace_uri.len, trace_uri.s, param1.len, param1.s);
return -1;
}
} else if (uri_type == TYPE_SIP) {
if (parse_uri(trace_uri.s, trace_uri.len, &elem->el.uri) < 0) {
LM_ERR("failed to parse the URI!\n");
return -1;
}
} else {
elem->el.hep.name = trace_uri;
}
add_last(elem, trace_list);
return 0;
#undef PARSE_NAME
}
int parse_trace_id(unsigned int type, void *val)
{
str suri;
suri.s = (char*)val;
suri.len = strlen(suri.s);
str_trim_spaces_lr(suri);
if (parse_siptrace_id(&suri) < 0) {
LM_ERR("failed to parse tracer uri [%.*s]\n", suri.len, suri.s);
return -1;
}
return 0;
}
static int parse_trace_local_ip(void){
/* We tokenize the trace_local_ip from proto:ip:port to three fields */
trace_local_ip.len = strlen(trace_local_ip.s);
unsigned int port_no;
char *c = strchr(trace_local_ip.s, ':');
if (c == NULL) {
/* Only ip is specified */
trace_local_port = SIP_PORT;
trace_local_proto.s = "udp";
trace_local_proto.len = sizeof("udp") -1;
}
else {
str first_token = {c + 1,
trace_local_ip.len - (c - trace_local_ip.s) - 1};
if (str2int(&first_token, &port_no) == 0){
/* The first token is the port, so no proto */
if (port_no > 65535 || port_no == 0){
LM_WARN("trace local_ip: port is out of range (%d). "
"Will consider it to be %d\n", port_no, SIP_PORT);
trace_local_port = SIP_PORT;
}
else
trace_local_port = (unsigned short) port_no;
trace_local_proto.s = "udp";
trace_local_proto.len = sizeof("udp") - 1;
trace_local_ip.len = c - trace_local_ip.s;
}
else {
/* The first token is the protocol */
trace_local_proto.s = trace_local_ip.s;
trace_local_proto.len = c - trace_local_ip.s;
if (trace_local_proto.len > 4){
/* Too many letters for the protocol. Avoiding overflow */
LM_ERR("trace_local_ip : wrong protocol\n");
return -1;
} else if (trace_local_proto.len == 0){
trace_local_proto.s = "udp";
trace_local_proto.len = sizeof("udp") - 1;
}
char *c2 = strchr(c + 1, ':');
if (c2 != NULL){
/* We have a second token */
str second_token;
second_token.s = c2 + 1;
second_token.len = trace_local_ip.len -
(c2 - trace_local_ip.s) - 1;
if (str2int(&second_token, &port_no) != 0) {
trace_local_port = SIP_PORT;
LM_WARN("trace_local_ip: port is wrongly defined. "
"Will consider it as %hd\n", trace_local_port);
}
else if (port_no > 65535 || port_no == 0){
LM_WARN("trace local_ip: port is out of range (%d). "
"Will consider it to be %d\n",
port_no, SIP_PORT);
trace_local_port = SIP_PORT;
}
else
trace_local_port = (unsigned short) port_no;
trace_local_ip.s = c + 1;
trace_local_ip.len = c2 - c - 1;
}
else {
trace_local_port = SIP_PORT;
trace_local_ip.len -= c - trace_local_ip.s + 1;
trace_local_ip.s = c + 1;
}
}
}
return 0;
}
/*
* no fancy stuff just bubble sort; the list will be quite small
*/
static void do_sort(tlist_elem_p *list_p)
{
int done=1;
tlist_elem_p it, prev, tmp;
/* 0 or 1 elems already sorted */
if (*list_p==NULL || (*list_p)->next==NULL)
return;
do {
done=1;
prev=NULL;
it=*list_p;
do {
if (it->hash > it->next->hash) {
/* need to modify start of the list */
if (!prev) {
tmp=it->next;
it->next=tmp->next;
tmp->next=it;
*list_p=tmp;
} else {
tmp=it->next;
prev->next=tmp;
it->next=tmp->next;
tmp->next=it;
}
done=0;
}
prev=it;
it=it->next;
} while (it && it->next);
} while (!done);
}
static void init_db_cols(void)
{
#define COL_INIT(_col, _index, _type) \
do { \
db_keys[_index] = &_col##_column; \
db_vals[_index].type = DB_##_type; \
db_vals[_index].nul = 0; \
} while(0);
COL_INIT(msg, 0, STR);
COL_INIT(callid, 1, STR);
COL_INIT(method, 2, STR);
COL_INIT(status, 3, STR);
COL_INIT(fromproto, 4, STR);
COL_INIT(fromip, 5, STR);
COL_INIT(fromport, 6, INT);
COL_INIT(toproto, 7, STR);
COL_INIT(toip, 8, STR);
COL_INIT(toport, 9, INT);
COL_INIT(date, 10, DATETIME);
COL_INIT(direction, 11, STRING);
COL_INIT(fromtag, 12, STR);
COL_INIT(trace_attrs, 13, STR);
}
static int mod_init(void)
{
tlist_elem_p it;
date_column.len = strlen(date_column.s);
callid_column.len = strlen(callid_column.s);
trace_attrs_column.len = strlen(trace_attrs_column.s);
msg_column.len = strlen(msg_column.s);
method_column.len = strlen(method_column.s);
status_column.len = strlen(status_column.s);
fromproto_column.len = strlen(fromproto_column.s);
fromip_column.len = strlen(fromip_column.s);
fromport_column.len = strlen(fromport_column.s);
toproto_column.len = strlen(toproto_column.s);
toip_column.len = strlen(toip_column.s);
toport_column.len = strlen(toport_column.s);
fromtag_column.len = strlen(fromtag_column.s);
direction_column.len = strlen(direction_column.s);
if (trace_local_ip.s)
parse_trace_local_ip();
LM_INFO("initializing...\n");
trace_on_flag = (int*)shm_malloc(sizeof(int));
if(trace_on_flag==NULL)
{
LM_ERR("no more shm memory left\n");
return -1;
}
if (init_dyn_tracing() < 0) {
LM_ERR("could not initiate dynamic tracing!\n");
return -1;
}
*trace_on_flag = trace_on;
if (trace_prot_bind(TRACE_PROTO, &tprot)) {
LM_ERR("Failed to bind tracing protocol!\n");
return -1;
}
/* initialize hep api */
for (it=trace_list;it;it=it->next) {
if (it->type!=TYPE_HEP)
continue;
it->el.hep.hep_id = tprot.get_trace_dest_by_name(&it->el.hep.name);
if (it->el.hep.hep_id == NULL) {
LM_ERR("hep id not found!\n");
return -1;
}
LM_DBG("hep id {%.*s} loaded successfully!\n",
it->el.hep.name.len, it->el.hep.name.s);
}
/* set db_keys/vals info */
init_db_cols();
/* this will allow using HEP, SIP and DB that
* are declared under the same name in the same
* sip_trace() call */
for (it=trace_list; it; it=it->next) {
it->traceable=shm_malloc(sizeof(unsigned char));
if (it->traceable==NULL) {
LM_ERR("no more shmem!\n");
return -1;
}
*it->traceable = trace_on;
}
/* sort the list */
do_sort(&trace_list);
if (trace_list==NULL) {
LM_WARN("No trace id defined! The module is useless!\n");
}
/* this makes sense only if trace protocol is loaded */
if ( tprot.send_message ) {
sip_trace_id=register_traced_type(SIP_TRACE_TYPE_STR);
if (sip_trace_id == -1) {
LM_ERR("failed to get an id for \"%s\" tracing!\n", SIP_TRACE_TYPE_STR);
return -1;
}
if (register_trace_type == NULL)
register_trace_type = ®ister_traced_type;
if (check_is_traced == NULL)
check_is_traced = &api_is_id_traced;
if (get_next_destination == NULL)
get_next_destination = get_next_trace_dest;
if (sip_context_trace == NULL)
sip_context_trace = sip_context_trace_impl;
/* set the global trace api for other modules/functions if loaded */
if (tprot.get_trace_dest_by_name && !global_trace_api)
global_trace_api = &tprot;
}
/* load the module dependencies, best effort for now, as the strict
* dependency will be checked later in fixup function, according to
* the sip_trace() flags */
load_dlg_api(&dlgb);
load_tm_api(&tmb);
/* statelessly forwarded request callbacks */
if (register_slcb(SLCB_REQUEST_OUT, FL_USE_SIPTRACE,
trace_slreq_out) != 0) {
LM_ERR("can't register callback for statelessly "
"forwarded request\n");
return -1;
}
if (register_slcb(SLCB_REPLY_OUT, FL_USE_SIPTRACE,
trace_slreply_out) != 0) {
LM_ERR("can't register callback for statelessly "
"forwarded request\n");
return -1;
}
/* FIXME find a way to pass the flags and the trace_info_p
* parameter if there's any*/
#if 0
if (register_slcb(SLCB_ACK_IN, 0, trace_slack_in) != 0) {
LM_ERR("can't register callback for statelessly "
"forwarded request\n");
return -1;
}
#endif
/* register the context index for the tracing context
* set a free function only if no TM, otherwise
* tm/dialog will free the structure */
sl_ctx_idx=context_register_ptr(CONTEXT_GLOBAL,
(tmb.t_gett==NULL)?free_trace_info_pkg:0);
if (register_script_cb(process_dyn_tracing, PRE_SCRIPT_CB|REQ_TYPE_CB, 0)!=0) {
LM_ERR("could not register request dynamic tracing callback\n");
return -1;
}
return 0;
}
static int child_init(int rank)
{
tlist_elem_p it;
for (it=trace_list; it; it=it->next) {
if (it->type == TYPE_DB) {
LM_DBG("Initializing trace id [%.*s]\n",
it->name.len, it->name.s);
it->el.db->con = it->el.db->funcs.init(&it->el.db->url);
if (!it->el.db->con) {
LM_ERR("Unable to connect to database with url [%.*s]\n",
it->el.db->url.len, it->el.db->url.s);
return -1;
}
}
}
return 0;
}
static void destroy(void)
{
tlist_elem_p el, last=NULL;
el=trace_list;
while (el) {
if (last) {
shm_free(last->traceable);
pkg_free(last);
}
if (el->type == TYPE_DB) {
if (el->el.db->con) {
el->el.db->funcs.close(el->el.db->con);
}
}
last=el;
el=el->next;
}
if (last)
if (trace_on_flag)
shm_free(trace_on_flag);
destroy_dyn_tracing();
}
static inline int insert_siptrace(st_db_struct_t *st_db,
db_key_t *keys,db_val_t *vals, str *trace_attrs)