/
sipcapture.c
2616 lines (2126 loc) · 66.3 KB
/
sipcapture.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
/*
* sipcapture module - helper module to capture sip messages
*
* Copyright (C) 2011-2015 Alexandr Dubovikov (alexandr.dubovikov@gmail.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
* sipcapture module - helper module to capture sip messages
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <arpa/inet.h>
/* BPF structure */
#ifdef __OS_linux
#include <linux/filter.h>
#endif
#ifndef __USE_BSD
#define __USE_BSD /* on linux use bsd version of iphdr (more portable) */
#endif /* __USE_BSD */
#include <netinet/ip.h>
#define __FAVOR_BSD /* on linux use bsd version of udphdr (more portable) */
#include <netinet/udp.h>
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../events.h"
#include "../../ut.h"
#include "../../ip_addr.h"
#include "../../mem/mem.h"
#include "../../mem/shm_mem.h"
#include "../../lib/kmi/mi.h"
#include "../../rpc.h"
#include "../../rpc_lookup.h"
#include "../../lib/srdb1/db.h"
#include "../../parser/contact/parse_contact.h"
#include "../../parser/parse_content.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_uri.h"
#include "../../parser/digest/digest.h"
#include "../../parser/parse_ppi_pai.h"
#include "../../pvar.h"
#include "../../str.h"
#include "../../onsend.h"
#include "../../resolve.h"
#include "../../receive.h"
#include "../../mod_fix.h"
#include "../../rand/kam_rand.h"
#include "sipcapture.h"
#include "hash_mode.h"
#include "hep.h"
#ifdef STATISTICS
#include "../../lib/kcore/statistics.h"
#endif
#include "../topoh/api.h"
MODULE_VERSION
#define ETHHDR 14 /* sizeof of ethhdr structure */
#define EMPTY_STR(val) val.s=""; val.len=0;
#define TABLE_LEN 256
#define NR_KEYS 41
#define RTCP_NR_KEYS 12
#define MAX_HEADERS 16
/*multiple table mode*/
enum e_mt_mode{
mode_random = 1,
mode_hash,
mode_round_robin,
mode_error
};
typedef struct _capture_mode_data {
unsigned int id;
str name;
str db_url;
db1_con_t *db_con;
db_func_t db_funcs;
str * table_names;
unsigned int no_tables;
enum e_mt_mode mtmode;
enum hash_source hash_source;
unsigned int rr_idx;
stat_var* sipcapture_req;
stat_var* sipcapture_rpl;
struct _capture_mode_data * next;
}_capture_mode_data_t;
_capture_mode_data_t * capture_modes_root = NULL;
_capture_mode_data_t * capture_def = NULL;
/* module function prototypes */
static int mod_init(void);
static int sipcapture_init_rpc(void);
static int child_init(int rank);
static void destroy(void);
static int sipcapture_fixup(void** param, int param_no);
static int reportcapture_fixup(void** param, int param_no);
static int float2int_fixup(void** param, int param_no);
static int sip_capture(struct sip_msg *msg, str *dtable, _capture_mode_data_t *cm_data);
static int report_capture(struct sip_msg *msg, str *_table, str* _corr, str *_data);
static int w_sip_capture(struct sip_msg* _m, char* _table, _capture_mode_data_t * _cm_data, char* s2);
static int w_report_capture(struct sip_msg* _m, char* _table, char* _corr, char* _data);
static int w_float2int(struct sip_msg* _m, char* _val, char* _coof, char* s2);
static int sipcapture_parse_aleg_callid_headers();
int parse_aleg_callid_headers(str *headers_str, str *headers);
int init_rawsock_children(void);
int extract_host_port(void);
int raw_capture_socket(struct ip_addr* ip, str* iface, int port_start, int port_end, int proto);
int raw_capture_rcv_loop(int rsock, int port1, int port2, int ipip);
static struct mi_root* sip_capture_mi(struct mi_root* cmd, void* param );
static str db_url = str_init(DEFAULT_DB_URL);
static str table_name = str_init("sip_capture");
static str hash_source = str_init("call_id");
static str table_time_sufix = str_init("%Y%m%D");
static str mt_mode = str_init("rand");
static str date_column = str_init("date");
static str micro_ts_column = str_init("micro_ts");
static str method_column = str_init("method");
static str reply_reason_column = str_init("reply_reason");
static str correlation_column = str_init("correlation_id");
static str ruri_column = str_init("ruri");
static str ruri_user_column = str_init("ruri_user");
static str ruri_domain_column = str_init("ruri_domain");
static str from_user_column = str_init("from_user");
static str from_domain_column = str_init("from_domain");
static str from_tag_column = str_init("from_tag");
static str to_user_column = str_init("to_user");
static str to_domain_column = str_init("to_domain");
static str to_tag_column = str_init("to_tag");
static str pid_user_column = str_init("pid_user");
static str contact_user_column = str_init("contact_user");
static str auth_user_column = str_init("auth_user");
static str callid_column = str_init("callid");
static str callid_aleg_column = str_init("callid_aleg");
static str via_1_column = str_init("via_1");
static str via_1_branch_column = str_init("via_1_branch");
static str cseq_column = str_init("cseq");
static str diversion_column = str_init("diversion");
static str reason_column = str_init("reason");
static str content_type_column = str_init("content_type");
static str authorization_column = str_init("auth");
static str user_agent_column = str_init("user_agent");
static str source_ip_column = str_init("source_ip");
static str source_port_column = str_init("source_port");
static str dest_ip_column = str_init("destination_ip");
static str dest_port_column = str_init("destination_port");
static str contact_ip_column = str_init("contact_ip");
static str contact_port_column = str_init("contact_port");
static str orig_ip_column = str_init("originator_ip");
static str orig_port_column = str_init("originator_port");
static str rtp_stat_column = str_init("rtp_stat");
static str proto_column = str_init("proto");
static str family_column = str_init("family");
static str type_column = str_init("type");
static str node_column = str_init("node");
static str msg_column = str_init("msg");
static str capture_node = str_init("homer01");
static str star_contact = str_init("*");
static str callid_aleg_header = str_init("X-CID");
int raw_sock_desc = -1; /* raw socket used for ip packets */
unsigned int raw_sock_children = 1;
int capture_on = 0;
int ipip_capture_on = 0;
int moni_capture_on = 0;
int moni_port_start = 0;
int moni_port_end = 0;
int *capture_on_flag = NULL;
int db_insert_mode = 0;
int promisc_on = 0;
int bpf_on = 0;
int hep_capture_on = 0;
int insert_retries = 0;
int insert_retry_timeout = 60;
int hep_offset = 0;
str raw_socket_listen = { 0, 0 };
str raw_interface = { 0, 0 };
char *authkey = NULL, *correlation_id = NULL;
str callid_aleg_headers[MAX_HEADERS];
int n_callid_aleg_headers = 0;
struct ifreq ifr; /* interface structure */
static int sc_topoh_unmask = 0;
static topoh_api_t thb = {0};
#ifdef __OS_linux
/* Linux socket filter */
/* tcpdump -s 0 udp and portrange 5060-5090 -dd */
static struct sock_filter BPF_code[] = { { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 7, 0x000086dd },
{ 0x30, 0, 0, 0x00000014 }, { 0x15, 0, 18, 0x00000011 }, { 0x28, 0, 0, 0x00000036 },
{ 0x35, 0, 1, 0x000013c4 }, { 0x25, 0, 14, 0x000013e2 }, { 0x28, 0, 0, 0x00000038 },
{ 0x35, 11, 13, 0x000013c4 }, { 0x15, 0, 12, 0x00000800 }, { 0x30, 0, 0, 0x00000017 },
{ 0x15, 0, 10, 0x00000011 }, { 0x28, 0, 0, 0x00000014 }, { 0x45, 8, 0, 0x00001fff },
{ 0xb1, 0, 0, 0x0000000e }, { 0x48, 0, 0, 0x0000000e }, { 0x35, 0, 1, 0x000013c4 },
{ 0x25, 0, 3, 0x000013e2 }, { 0x48, 0, 0, 0x00000010 }, { 0x35, 0, 2, 0x000013c4 },
{ 0x25, 1, 0, 0x000013e2 }, { 0x6, 0, 0, 0x0000ffff }, { 0x6, 0, 0, 0x00000000 },
};
#endif
//db1_con_t *db_con = NULL; /*!< database connection */
//db_func_t db_funcs; /*!< Database functions */
//str* table_names = NULL;
unsigned int no_tables = 0;
enum e_mt_mode mtmode = mode_random ;
enum hash_source source = hs_error;
//unsigned int rr_idx = 0;
struct hep_timehdr* heptime;
/*! \brief
* Exported functions
*/
static cmd_export_t cmds[] = {
{"sip_capture", (cmd_function)w_sip_capture, 0, 0, 0, ANY_ROUTE},
{"sip_capture", (cmd_function)w_sip_capture, 1, sipcapture_fixup, 0, ANY_ROUTE },
{"sip_capture", (cmd_function)w_sip_capture, 2, sipcapture_fixup, 0, ANY_ROUTE },
{"report_capture", (cmd_function)w_report_capture, 1, reportcapture_fixup, 0, ANY_ROUTE },
{"report_capture", (cmd_function)w_report_capture, 2, reportcapture_fixup, 0, ANY_ROUTE },
{"report_capture", (cmd_function)w_report_capture, 3, reportcapture_fixup, 0, ANY_ROUTE },
{"float2int", (cmd_function)w_float2int, 2, float2int_fixup, 0, ANY_ROUTE },
{0, 0, 0, 0, 0, 0}
};
int capture_mode_param(modparam_t type, void *val);
/*! \brief
* Exported parameters
*/
static param_export_t params[] = {
{"db_url", PARAM_STR, &db_url },
{"table_name", PARAM_STR, &table_name },
{"hash_source", PARAM_STR, &hash_source },
{"mt_mode", PARAM_STR, &mt_mode },
{"date_column", PARAM_STR, &date_column },
{"micro_ts_column", PARAM_STR, µ_ts_column },
{"method_column", PARAM_STR, &method_column },
{"correlation_column", PARAM_STR, &correlation_column.s },
{"reply_reason_column", PARAM_STR, &reply_reason_column },
{"ruri_column", PARAM_STR, &ruri_column },
{"ruri_user_column", PARAM_STR, &ruri_user_column },
{"ruri_domain_column", PARAM_STR, &ruri_domain_column },
{"from_user_column", PARAM_STR, &from_user_column },
{"from_domain_column", PARAM_STR, &from_domain_column },
{"from_tag_column", PARAM_STR, &from_tag_column },
{"to_user_column", PARAM_STR, &to_user_column },
{"to_domain_column", PARAM_STR, &to_domain_column },
{"to_tag_column", PARAM_STR, &to_tag_column },
{"pid_user_column", PARAM_STR, &pid_user_column },
{"contact_user_column", PARAM_STR, &contact_user_column },
{"auth_user_column", PARAM_STR, &auth_user_column },
{"callid_column", PARAM_STR, &callid_column},
{"callid_aleg_column", PARAM_STR, &callid_aleg_column},
{"via_1_column", PARAM_STR, &via_1_column },
{"via_1_branch_column", PARAM_STR, &via_1_branch_column },
{"cseq_column", PARAM_STR, &cseq_column },
{"diversion_column", PARAM_STR, &diversion_column },
{"reason_column", PARAM_STR, &reason_column },
{"content_type_column", PARAM_STR, &content_type_column },
{"authorization_column", PARAM_STR, &authorization_column },
{"user_agent_column", PARAM_STR, &user_agent_column },
{"source_ip_column", PARAM_STR, &source_ip_column },
{"source_port_column", PARAM_STR, &source_port_column},
{"destination_ip_column", PARAM_STR, &dest_ip_column },
{"destination_port_column", PARAM_STR, &dest_port_column },
{"contact_ip_column", PARAM_STR, &contact_ip_column },
{"contact_port_column", PARAM_STR, &contact_port_column},
{"originator_ip_column", PARAM_STR, &orig_ip_column },
{"originator_port_column", PARAM_STR, &orig_port_column },
{"proto_column", PARAM_STR, &proto_column },
{"family_column", PARAM_STR, &family_column },
{"rtp_stat_column", PARAM_STR, &rtp_stat_column },
{"type_column", PARAM_STR, &type_column },
{"node_column", PARAM_STR, &node_column },
{"msg_column", PARAM_STR, &msg_column },
{"capture_on", INT_PARAM, &capture_on },
{"capture_node", PARAM_STR, &capture_node },
{"raw_sock_children", INT_PARAM, &raw_sock_children },
{"hep_capture_on", INT_PARAM, &hep_capture_on },
{"raw_socket_listen", PARAM_STR, &raw_socket_listen },
{"raw_ipip_capture_on", INT_PARAM, &ipip_capture_on },
{"raw_moni_capture_on", INT_PARAM, &moni_capture_on },
{"db_insert_mode", INT_PARAM, &db_insert_mode },
{"raw_interface", PARAM_STR, &raw_interface },
{"promiscious_on", INT_PARAM, &promisc_on },
{"raw_moni_bpf_on", INT_PARAM, &bpf_on },
{"callid_aleg_header", PARAM_STR, &callid_aleg_header},
{"capture_mode", PARAM_STRING|USE_FUNC_PARAM, (void *)capture_mode_param},
{"insert_retries", INT_PARAM, &insert_retries },
{"insert_retry_timeout", INT_PARAM, &insert_retry_timeout },
{"table_time_sufix", PARAM_STR, &table_time_sufix },
{"topoh_unamsk", PARAM_INT, &sc_topoh_unmask },
{0, 0, 0}
};
/*! \brief
* MI commands
*/
static mi_export_t mi_cmds[] = {
{ "sip_capture", sip_capture_mi, 0, 0, 0 },
{ 0, 0, 0, 0, 0}
};
#ifdef STATISTICS
/*stat_var* sipcapture_req;
stat_var* sipcapture_rpl;
stat_export_t sipcapture_stats[] = {
{"captured_requests" , 0, &sipcapture_req },
{"captured_replies" , 0, &sipcapture_rpl },
{0,0,0}
};
*/
stat_export_t *sipcapture_stats = NULL;
#endif
/*! \brief module exports */
struct module_exports exports = {
"sipcapture",
DEFAULT_DLFLAGS, /*!< dlopen flags */
cmds, /*!< Exported functions */
params, /*!< Exported parameters */
#ifdef STATISTICS
// sipcapture_stats, /*!< exported statistics */
0,
#else
0, /*!< exported statistics */
#endif
mi_cmds, /*!< exported MI functions */
0, /*!< exported pseudo-variables */
0, /*!< extra processes */
mod_init, /*!< module initialization function */
0, /*!< response function */
destroy, /*!< destroy function */
child_init /*!< child initialization function */
};
/* returns number of tables if successful
* <0 if failed
*/
int parse_table_names (str table_name, str ** table_names){
char *p = NULL;
unsigned int no_tables;
char * table_name_cpy;
unsigned int i;
/*parse and save table names*/
no_tables = 1;
i = 0;
str * names;
table_name_cpy = (char *) pkg_malloc(sizeof(char) * table_name.len + 1 );
if (table_name_cpy == NULL){
LM_ERR("no more pkg memory left\n");
return -1;
}
memcpy (table_name_cpy, table_name.s, table_name.len);
table_name_cpy[table_name.len] = '\0';
p = table_name_cpy;
while (*p)
{
if (*p== '|')
{
no_tables++;
}
p++;
}
names = (str*)pkg_malloc(sizeof(str) * no_tables);
if(names == NULL) {
LM_ERR("no more pkg memory left\n");
return -1;
}
p = strtok (table_name_cpy,"| \t");
while (p != NULL)
{
LM_INFO ("INFO: table name:%s\n",p);
names[i].len = strlen (p);
names[i].s = (char *)pkg_malloc(sizeof(char) *names[i].len);
memcpy(names[i].s, p, names[i].len);
i++;
p = strtok (NULL, "| \t");
}
pkg_free(table_name_cpy);
*table_names = names;
return no_tables;
}
/* checks for some missing fields*/
int check_capture_mode ( _capture_mode_data_t * n) {
if (!n->db_url.s || !n->db_url.len){
LM_ERR("db_url not set\n");
goto error;
}
if (!n->mtmode ){
LM_ERR("mt_mode not set\n");
goto error;
}
else if (!n->no_tables || !n->table_names){
LM_ERR("table names not set\n");
goto error;
}
return 0;
error:
LM_ERR("parsing capture_mode: not all needed parameters are set. Please check again\n");
return -1;
}
int capture_mode_set_params (_capture_mode_data_t * n, str * params){
param_t * params_list = NULL;
param_hooks_t phooks;
param_t *pit=NULL;
db_func_t db_funcs;
str s;
LM_DBG("to tokenize: [%.*s]\n", params->len, params->s);
if ( n == NULL || params == NULL)
return -1;
s = *params;
if (parse_params(&s, CLASS_ANY, &phooks, ¶ms_list)<0)
return -1;
for (pit = params_list; pit; pit=pit->next)
{
LM_DBG("parameter is [%.*s]\n",pit->name.len, pit->name.s );
LM_DBG("parameter value is [%.*s]\n", pit->body.len, pit->body.s);
if (pit->name.len == 6 && strncmp (pit->name.s, "db_url", pit->name.len)==0){
n->db_url.len =pit->body.len;
n->db_url.s = (char*)pkg_malloc(sizeof(char) * n->db_url.len);
if (!n->db_url.s){
LM_ERR("no more pkg memory\n");
goto error;
}
memcpy(n->db_url.s, pit->body.s,n->db_url.len );
if (db_bind_mod(&n->db_url, &db_funcs)){
LM_ERR("parsing capture_mode: could not bind db funcs for url:[%.*s]\n", n->db_url.len, n->db_url.s);
goto error;
}
n->db_funcs = db_funcs;
if (!DB_CAPABILITY(n->db_funcs, DB_CAP_INSERT))
{
LM_ERR("parsing capture_mode: database modules does not provide all functions needed"
" by module\n");
goto error;
}
}
else if (pit->name.len == 10 && strncmp (pit->name.s, "table_name", pit->name.len)==0){
if ((int)(n->no_tables = parse_table_names(pit->body, &n->table_names))<0){
LM_ERR("parsing capture_mode: table name parsing failed\n");
goto error;
}
}
else if (pit->name.len == 7 && strncmp (pit->name.s, "mt_mode", pit->name.len)==0){
if (pit->body.len == 4 && strncmp(pit->body.s, "rand",pit->body.len ) ==0)
{
n->mtmode = mode_random;
}
else if (pit->body.len == 11 && strncmp(pit->body.s, "round_robin",pit->body.len ) ==0)
{
n->mtmode = mode_round_robin;
}
else if (pit->body.len == 4 && strncmp(pit->body.s, "hash", pit->body.len) ==0)
{
n->mtmode = mode_hash;
}
else {
LM_ERR("parsing capture_mode: capture mode not recognized: [%.*s]\n", pit->body.len, pit->body.s);
goto error;
}
}
else if (pit->name.len == 11 && strncmp (pit->name.s, "hash_source", pit->name.len)==0){
if ( (n->hash_source = get_hash_source (pit->body.s)) == hs_error)
{
LM_ERR("parsing capture_mode: hash source unrecognized: [%.*s]\n", pit->body.len, pit->body.s);
goto error;
}
}
}
if (n->mtmode == mode_hash && ( n->hash_source == 0 || n->hash_source == hs_error )){
LM_WARN("Hash mode set, but no hash source provided for [%.*s]. Will consider hashing by call id.\n", n->name.len, n->name.s);
n->hash_source = hs_call_id;
}
if ( check_capture_mode(n)){
goto error;
}
return 0;
error:
if (n->db_url.s){
pkg_free(n->db_url.s);
}
return -1;
}
void * capture_mode_init(str *name, str * params) {
_capture_mode_data_t * n = NULL;
unsigned int id;
if (!name || name->len == 0){
LM_ERR("capture_mode name is empty\n");
goto error;
}
if (!params || params->len == 0){
LM_ERR("capture_mode params are empty\n");
goto error;
}
id = core_case_hash(name, 0, 0);
n = (_capture_mode_data_t *) pkg_malloc(sizeof(_capture_mode_data_t));
if (!n){
LM_ERR("no more pkg memory\n");
goto error;
}
memset (n, 0,sizeof(_capture_mode_data_t) );
n->id = id;
n->name.len = name->len;
n->name.s = (char *)pkg_malloc(sizeof(char) * n->name.len);
if (!n->name.s){
LM_ERR("no more pkg memory\n");
goto error;
}
memcpy(n->name.s, name->s, n->name.len);
n->table_names = (str *)pkg_malloc(sizeof(str));
if (!n->table_names){
LM_ERR("no more pkg memory\n");
goto error;
}
if (capture_mode_set_params (n, params)<0){
LM_ERR("capture mode parsing failed\n");
goto error;
}
n->next = capture_modes_root;
capture_modes_root = n;
return n;
error:
if (n){
if (n->name.s){
pkg_free(n->name.s);
}
if (n->table_names){
pkg_free(n->table_names);
}
pkg_free(n);
}
return 0;
}
/*parse name=>param1=>val1;param2=>val2;..*/
int capture_mode_param(modparam_t type, void *val){
str name;
str in;
str tok;
char * p;
in.s = val;
in.len = strlen(in.s);
p = in.s;
while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
p++;
if(p>in.s+in.len || *p=='\0')
goto error;
name.s = p;
while(p < in.s + in.len)
{
if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
break;
p++;
}
if(p>in.s+in.len || *p=='\0')
goto error;
name.len = p - name.s;
if(*p!='=')
{
while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
p++;
if(p>in.s+in.len || *p=='\0' || *p!='=')
goto error;
}
p++;
if(*p!='>')
goto error;
p++;
while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
p++;
tok.s = p;
tok.len = in.len + (int)(in.s - p);
LM_DBG("capture_mode name: [%.*s] data: [%.*s]\n", name.len, name.s, tok.len, tok.s);
if (!capture_mode_init(&name, &tok)){
return -1;
}
return 0;
error:
LM_ERR("invalid parameter [%.*s] at [%d]\n", in.len, in.s,
(int)(p-in.s));
return -1;
}
/*! \brief Initialize sipcapture module */
static int mod_init(void)
{
struct ip_addr *ip = NULL;
char * def_params = NULL;
#ifdef STATISTICS
int cnt = 0;
int i = 0;
char * stat_name = NULL;
_capture_mode_data_t * c = NULL;
int def;
#endif
if(register_mi_mod(exports.name, mi_cmds)!=0)
{
LM_ERR("failed to register MI commands\n");
return -1;
}
if(sipcapture_init_rpc()!=0)
{
LM_ERR("failed to register RPC commands\n");
return -1;
}
if(sc_topoh_unmask==1) {
/* bind the topoh API */
if (topoh_load_api(&thb)!=0) {
LM_ERR("cannot bind to topoh API\n");
return -1;
}
}
/*Check the table name - if table_name is empty and no capture modes are defined, then error*/
if(!table_name.len && capture_modes_root == NULL) {
LM_ERR("ERROR: sipcapture: mod_init: table_name is not defined or empty\n");
return -1;
}
/*create a default capture mode using the default parameters*/
def_params = (char *) pkg_malloc(snprintf(NULL, 0, "db_url=%s;table_name=%s;mt_mode=%s;hash_source=%s",db_url.s, table_name.s, mt_mode.s,hash_source.s) + 1);
sprintf(def_params, "db_url=%s;table_name=%s;mt_mode=%s;hash_source=%s",db_url.s, table_name.s, mt_mode.s,hash_source.s);
str def_name, def_par;
def_name.s= strdup("default");
def_name.len = 7;
def_par.s = def_params;
def_par.len = strlen (def_params);
LM_DBG("def_params is: %s\n", def_params);
if ((capture_def =capture_mode_init(&def_name, &def_par)) == NULL){
LM_WARN("Default capture mode configuration failed. Suppose sip_capture calls will use other defined capture modes.\n");
}
pkg_free(def_params);
if (sipcapture_parse_aleg_callid_headers() < 0) {
return -1;
}
#ifdef STATISTICS
c = capture_modes_root;
while (c){
cnt++;
c=c->next;
}
/*requests and replies for each mode + 1 zero-filled stat_export */
stat_export_t *stats = (stat_export_t *) shm_malloc(sizeof(stat_export_t) * cnt * 2 + 1 );
c = capture_modes_root;
while (c){
/*for the default capture_mode, don't add it's name to the stat name*/
def = (capture_def && c == capture_def)?1:0;
stat_name = (char *)shm_malloc(sizeof (char) * (snprintf(NULL, 0 , (def)?"captured_requests%.*s":"captured_requests[%.*s]", (def)?0:c->name.len, (def)?"":c->name.s) + 1));
sprintf(stat_name, (def)?"captured_requests%.*s":"captured_requests[%.*s]", (def)?0:c->name.len, (def)?"":c->name.s);
stats[i].name = stat_name;
stats[i].flags = 0;
stats[i].stat_pointer = &c->sipcapture_req;
i++;
stat_name = (char *)shm_malloc(sizeof (char) * (snprintf(NULL, 0 , (def)?"captured_replies%.*s":"captured_replies[%.*s]", (def)?0:c->name.len, (def)?"":c->name.s) + 1));
sprintf(stat_name, (def)?"captured_replies%.*s":"captured_replies[%.*s]", (def)?0:c->name.len, (def)?"":c->name.s);
stats[i].name = stat_name;
stats[i].flags = 0;
stats[i].stat_pointer = &c->sipcapture_rpl;
i++;
c=c->next;
}
stats[i].name = 0;
stats[i].flags = 0;
stats[i].stat_pointer = 0;
sipcapture_stats = stats;
/* register statistics */
if (register_module_stats(exports.name, sipcapture_stats)!=0)
{
LM_ERR("failed to register core statistics\n");
return -1;
}
#endif
kam_srand(time(NULL));
if(db_insert_mode) {
LM_INFO("INFO: sipcapture: mod_init: you have enabled INSERT DELAYED \
Make sure your DB can support it\n");
}
capture_on_flag = (int*)shm_malloc(sizeof(int));
if(capture_on_flag==NULL) {
LM_ERR("no more shm memory left\n");
return -1;
}
*capture_on_flag = capture_on;
/* register DGRAM event */
if(sr_event_register_cb(SREV_NET_DGRAM_IN, hep_msg_received) < 0) {
LM_ERR("failed to register SREV_NET_DGRAM_IN event\n");
return -1;
}
if(ipip_capture_on && moni_capture_on) {
LM_ERR("only one RAW mode is supported. Please disable ipip_capture_on or moni_capture_on\n");
return -1;
}
if ((insert_retries <0) || ( insert_retries > 500)) {
LM_ERR("insert_retries should be a value between 0 and 500\n");
return -1;
}
if (( 0 == insert_retries) && (insert_retry_timeout != 0)){
LM_ERR("insert_retry_timeout has no meaning when insert_retries is not set\n");
}
if ((insert_retry_timeout <0) || ( insert_retry_timeout > 300)) {
LM_ERR("insert_retry_timeout should be a value between 0 and 300\n");
return -1;
}
/* raw processes for IPIP encapsulation */
if (ipip_capture_on || moni_capture_on) {
register_procs(raw_sock_children);
if(extract_host_port() && (((ip=str2ip(&raw_socket_listen)) == NULL)
&& ((ip=str2ip6(&raw_socket_listen)) == NULL)
))
{
LM_ERR("sipcapture mod_init: bad RAW IP: %.*s\n", raw_socket_listen.len, raw_socket_listen.s);
return -1;
}
if(moni_capture_on && !moni_port_start) {
LM_ERR("ERROR:sipcapture:mod_init: Please define port/portrange in 'raw_socket_listen', before \
activate monitoring capture\n");
return -1;
}
raw_sock_desc = raw_capture_socket(raw_socket_listen.len ? ip : 0, raw_interface.len ? &raw_interface : 0,
moni_port_start, moni_port_end , ipip_capture_on ? IPPROTO_IPIP : htons(0x0800));
if(raw_sock_desc < 0) {
LM_ERR("could not initialize raw udp socket:"
" %s (%d)\n", strerror(errno), errno);
if (errno == EPERM)
LM_ERR("could not initialize raw socket on startup"
" due to inadequate permissions, please"
" restart as root or with CAP_NET_RAW\n");
return -1;
}
if(promisc_on && raw_interface.len) {
memset(&ifr, 0, sizeof(ifr));
memcpy(ifr.ifr_name, raw_interface.s, raw_interface.len);
#ifdef __OS_linux
if(ioctl(raw_sock_desc, SIOCGIFFLAGS, &ifr) < 0) {
LM_ERR("could not get flags from interface [%.*s]:"
" %s (%d)\n", raw_interface.len, raw_interface.s, strerror(errno), errno);
goto error;
}
ifr.ifr_flags |= IFF_PROMISC;
if (ioctl(raw_sock_desc, SIOCSIFFLAGS, &ifr) < 0) {
LM_ERR("could not set PROMISC flag to interface [%.*s]:"
" %s (%d)\n", raw_interface.len, raw_interface.s, strerror(errno), errno);
goto error;
}
#endif
}
}
return 0;
#ifdef __OS_linux
error:
if(raw_sock_desc) close(raw_sock_desc);
return -1;
#endif
}
static int sipcapture_fixup(void** param, int param_no)
{
_capture_mode_data_t *con;
str val;
unsigned int id;
if (param_no == 1 ) {
return fixup_var_pve_str_12(param, 1);
}
if (param_no == 2 ){
val.s = (char *)*param;
val.len = strlen((char *)*param);
con = capture_modes_root;
id = core_case_hash (&val, 0 , 0);
while (con){
if (id == con->id && con->name.len == val.len
&& strncmp(con->name.s, val.s, val.len) == 0){
*param = (void *)con;
LM_DBG("found capture mode :[%.*s]\n",con->name.len, con->name.s);
return 0;
}
con = con->next;
}
LM_ERR("no capture mode found\n");
return -1;
}
return 0;
}
static int reportcapture_fixup(void** param, int param_no)
{
if (param_no == 1 ) {
return fixup_var_pve_str_12(param, 1);
}
if (param_no == 2 ){
return fixup_var_pve_str_12(param, 1);
}
if (param_no == 3 ){
return fixup_var_pve_str_12(param, 1);
}
return 0;
}
static int float2int_fixup(void** param, int param_no)
{
if (param_no == 1 ) {
return fixup_var_pve_str_12(param, 1);
}
if (param_no == 2 ){
return fixup_var_pve_str_12(param, 1);
}
return 0;
}
static int w_float2int(struct sip_msg* _m, char* _val, char* _coof, char* s2)
{
str value = {0};
str coof = {0};
int ret = 0;
if(_val!=NULL && (get_str_fparam(&value, _m, (fparam_t*)_val) < 0))
{
LM_ERR("invalid table parameter [%s] [%s]\n", _val, value.s);
return -1;
}
if(_coof!=NULL && (get_str_fparam(&coof, _m, (fparam_t*)_coof) < 0))
{
LM_ERR("invalid data parameter [%s] [%s]\n", _coof, coof.s);
return -1;