7485d5a4187b574836bc530c32f92b66ca8fe8ff
[sip-router] / src / modules / sipcapture / sipcapture.c
1 /*
2  * sipcapture module - helper module to capture sip messages
3  *
4  * Copyright (C) 2011-2015 Alexandr Dubovikov (alexandr.dubovikov@gmail.com)
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * Kamailio is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 /*! \file
25  * sipcapture module - helper module to capture sip messages
26  *
27  */
28
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <net/if.h>
39 #include <netdb.h>
40 #include <arpa/inet.h>
41
42 /* BPF structure */
43 #ifdef __OS_linux
44 #include <linux/filter.h>
45 #endif
46
47 #ifndef __USE_BSD
48 #define __USE_BSD /* on linux use bsd version of iphdr (more portable) */
49 #endif                    /* __USE_BSD */
50 #include <netinet/ip.h>
51 #define __FAVOR_BSD /* on linux use bsd version of udphdr (more portable) */
52 #include <netinet/udp.h>
53
54 #include "../../core/sr_module.h"
55 #include "../../core/dprint.h"
56 #include "../../core/events.h"
57 #include "../../core/ut.h"
58 #include "../../core/ip_addr.h"
59 #include "../../core/mem/mem.h"
60 #include "../../core/mem/shm_mem.h"
61 #include "../../core/rpc.h"
62 #include "../../core/rpc_lookup.h"
63 #include "../../lib/srdb1/db.h"
64 #include "../../core/parser/contact/parse_contact.h"
65 #include "../../core/parser/parse_content.h"
66 #include "../../core/parser/parse_from.h"
67 #include "../../core/parser/parse_uri.h"
68 #include "../../core/parser/digest/digest.h"
69 #include "../../core/parser/parse_ppi_pai.h"
70 #include "../../core/parser/parse_rpid.h"
71 #include "../../core/forward.h"
72 #include "../../core/pvar.h"
73 #include "../../core/str.h"
74 #include "../../core/onsend.h"
75 #include "../../core/resolve.h"
76 #include "../../core/receive.h"
77 #include "../../core/mod_fix.h"
78 #include "../../core/rand/kam_rand.h"
79 #include "../../core/kemi.h"
80 #include "sipcapture.h"
81 #include "hash_mode.h"
82 #include "hep.h"
83
84 #ifdef STATISTICS
85 #include "../../core/counters.h"
86 #endif
87
88
89 #include "../topoh/api.h"
90
91 MODULE_VERSION
92
93
94 #define ETHHDR 14 /* sizeof of ethhdr structure */
95
96 #define EMPTY_STR(val) \
97         val.s = "";        \
98         val.len = 0;
99
100 #define TABLE_LEN 256
101
102 #define NR_KEYS 44
103 #define RTCP_NR_KEYS 12
104
105 #define MAX_HEADERS 16
106
107 /*multiple table mode*/
108 enum e_mt_mode
109 {
110         mode_random = 1,
111         mode_hash,
112         mode_round_robin,
113         mode_error
114 };
115
116
117 typedef struct _capture_mode_data
118 {
119         unsigned int id;
120         str name;
121         str db_url;
122         db1_con_t *db_con;
123         db_func_t db_funcs;
124         str *table_names;
125         unsigned int no_tables;
126         enum e_mt_mode mtmode;
127         enum hash_source hash_source;
128         unsigned int rr_idx;
129         stat_var *sipcapture_req;
130         stat_var *sipcapture_rpl;
131         struct _capture_mode_data *next;
132 } _capture_mode_data_t;
133
134 _capture_mode_data_t *capture_modes_root = NULL;
135 _capture_mode_data_t *capture_def = NULL;
136
137 /* module function prototypes */
138 static int mod_init(void);
139 static int sipcapture_init_rpc(void);
140 static int child_init(int rank);
141 static void destroy(void);
142 static int sipcapture_fixup(void **param, int param_no);
143 static int reportcapture_fixup(void **param, int param_no);
144 static int float2int_fixup(void **param, int param_no);
145
146 static int pv_get_hep(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
147 static int pv_parse_hep_name(pv_spec_p sp, str *in);
148
149 static int sip_capture(sip_msg_t *msg, str *dtable,
150                 _capture_mode_data_t *cm_data);
151 static int report_capture(sip_msg_t *msg, str *_table, str *_corr, str *_data);
152 static int w_sip_capture0(sip_msg_t *_m, char *_p1, char *_p2);
153 static int w_sip_capture1(sip_msg_t *_m, char *_table, char *_p2);
154 static int w_sip_capture2(sip_msg_t *_m, char *_table, char *_cmdata);
155 static int w_sip_capture(struct sip_msg *_m, char *_table,
156                 _capture_mode_data_t *_cm_data);
157 static int w_report_capture1(sip_msg_t *_m, char *_table, char *_p2);
158 static int w_report_capture2(sip_msg_t *_m, char *_table, char *_corr);
159 static int w_report_capture3(sip_msg_t *_m, char *_table, char *_corr,
160                 char *_data);
161 static int w_float2int(sip_msg_t *_m, char *_val, char *_coof);
162
163 static int w_sip_capture_forward(sip_msg_t *_m, char *_dst, char *_p2);
164
165 static int sipcapture_parse_aleg_callid_headers();
166 int parse_aleg_callid_headers(str *headers_str, str *headers);
167
168 int init_rawsock_children(void);
169 int extract_host_port(void);
170 int raw_capture_socket(struct ip_addr *ip, str *iface, int port_start,
171                 int port_end, int proto);
172 int raw_capture_rcv_loop(int rsock, int port1, int port2, int ipip);
173 static int nosip_hep_msg(sr_event_param_t *evp);
174
175 static int hep_version(struct sip_msg *msg);
176
177
178 static str db_url = str_init(DEFAULT_DB_URL);
179 static str table_name = str_init("sip_capture");
180 static str hash_source = str_init("call_id");
181 static str table_time_sufix = str_init("%Y%m%D");
182 static str mt_mode = str_init("rand");
183 static str date_column = str_init("date");
184 static str micro_ts_column = str_init("micro_ts");
185 static str method_column = str_init("method");
186 static str reply_reason_column = str_init("reply_reason");
187 static str correlation_column = str_init("correlation_id");
188 static str ruri_column = str_init("ruri");
189 static str ruri_user_column = str_init("ruri_user");
190 static str ruri_domain_column = str_init("ruri_domain");
191 static str from_user_column = str_init("from_user");
192 static str from_domain_column = str_init("from_domain");
193 static str from_tag_column = str_init("from_tag");
194 static str to_user_column = str_init("to_user");
195 static str to_domain_column = str_init("to_domain");
196 static str to_tag_column = str_init("to_tag");
197 static str pid_user_column = str_init("pid_user");
198 static str contact_user_column = str_init("contact_user");
199 static str auth_user_column = str_init("auth_user");
200 static str callid_column = str_init("callid");
201 static str callid_aleg_column = str_init("callid_aleg");
202 static str via_1_column = str_init("via_1");
203 static str via_1_branch_column = str_init("via_1_branch");
204 static str cseq_column = str_init("cseq");
205 static str diversion_column = str_init("diversion");
206 static str reason_column = str_init("reason");
207 static str content_type_column = str_init("content_type");
208 static str authorization_column = str_init("auth");
209 static str user_agent_column = str_init("user_agent");
210 static str source_ip_column = str_init("source_ip");
211 static str source_port_column = str_init("source_port");
212 static str dest_ip_column = str_init("destination_ip");
213 static str dest_port_column = str_init("destination_port");
214 static str contact_ip_column = str_init("contact_ip");
215 static str contact_port_column = str_init("contact_port");
216 static str orig_ip_column = str_init("originator_ip");
217 static str orig_port_column = str_init("originator_port");
218 static str rtp_stat_column = str_init("rtp_stat");
219 static str proto_column = str_init("proto");
220 static str family_column = str_init("family");
221 static str type_column = str_init("type");
222 static str node_column = str_init("node");
223 static str msg_column = str_init("msg");
224 static str custom_field1_column = str_init("custom_field1");
225 static str custom_field2_column = str_init("custom_field2");
226 static str custom_field3_column = str_init("custom_field3");
227 static str capture_node = str_init("homer01");
228 static str star_contact = str_init("*");
229 static str callid_aleg_header = str_init("X-CID");
230 static str custom_field1_header = str_init("Mac");
231 static str custom_field2_header = str_init("IP");
232 static str custom_field3_header = str_init("Port");
233
234
235 int raw_sock_desc = -1; /* raw socket used for ip packets */
236 unsigned int raw_sock_children = 1;
237 int capture_on = 0;
238 int ipip_capture_on = 0;
239 int moni_capture_on = 0;
240 int moni_port_start = 0;
241 int moni_port_end = 0;
242 int *capture_on_flag = NULL;
243 int db_insert_mode = 0;
244 int promisc_on = 0;
245 int bpf_on = 0;
246 int hep_capture_on = 0;
247 int insert_retries = 0;
248 int insert_retry_timeout = 60;
249 int hep_offset = 0;
250 str raw_socket_listen = {0, 0};
251 str raw_interface = {0, 0};
252 char *authkey = NULL, *correlation_id = NULL;
253 str callid_aleg_headers[MAX_HEADERS];
254 int n_callid_aleg_headers = 0;
255 str sc_event_callback = STR_NULL;
256
257 struct ifreq ifr; /* interface structure */
258
259 /* by default nonsip_hook is inactive */
260 static int nonsip_hook = 0;
261 static int hep_route_no = -1;
262
263 static int sc_topoh_unmask = 0;
264 static topoh_api_t thb = {0};
265
266 #ifdef __OS_linux
267 /* Linux socket filter */
268 /* tcpdump -s 0 udp and portrange 5060-5090 -dd */
269 static struct sock_filter BPF_code[] = {
270         {0x28, 0, 0, 0x0000000c}, {0x15, 0, 7, 0x000086dd},
271         {0x30, 0, 0, 0x00000014}, {0x15, 0, 18, 0x00000011},
272         {0x28, 0, 0, 0x00000036}, {0x35, 0, 1, 0x000013c4},
273         {0x25, 0, 14, 0x000013e2}, {0x28, 0, 0, 0x00000038},
274         {0x35, 11, 13, 0x000013c4}, {0x15, 0, 12, 0x00000800},
275         {0x30, 0, 0, 0x00000017}, {0x15, 0, 10, 0x00000011},
276         {0x28, 0, 0, 0x00000014}, {0x45, 8, 0, 0x00001fff},
277         {0xb1, 0, 0, 0x0000000e}, {0x48, 0, 0, 0x0000000e},
278         {0x35, 0, 1, 0x000013c4}, {0x25, 0, 3, 0x000013e2},
279         {0x48, 0, 0, 0x00000010}, {0x35, 0, 2, 0x000013c4},
280         {0x25, 1, 0, 0x000013e2}, {0x6, 0, 0, 0x0000ffff},
281         {0x6, 0, 0, 0x00000000},
282 };
283 #endif
284
285 //db1_con_t *db_con = NULL;             /*!< database connection */
286 //db_func_t db_funcs;                   /*!< Database functions */
287
288 //str* table_names = NULL;
289
290 unsigned int no_tables = 0;
291
292 enum e_mt_mode mtmode = mode_random;
293 enum hash_source source = hs_error;
294
295 //unsigned int rr_idx = 0;
296
297 struct hep_timeinfo *heptime;
298
299 /*! \brief
300  * Exported functions
301  */
302 static cmd_export_t cmds[] = {
303         {"sip_capture", (cmd_function)w_sip_capture0, 0, 0, 0,
304                 ANY_ROUTE},
305         {"sip_capture", (cmd_function)w_sip_capture1, 1, sipcapture_fixup, 0,
306                 ANY_ROUTE},
307         {"sip_capture", (cmd_function)w_sip_capture2, 2, sipcapture_fixup, 0,
308                 ANY_ROUTE},
309         {"report_capture", (cmd_function)w_report_capture1, 1,
310                 reportcapture_fixup, 0, ANY_ROUTE},
311         {"report_capture", (cmd_function)w_report_capture2, 2,
312                 reportcapture_fixup, 0, ANY_ROUTE},
313         {"report_capture", (cmd_function)w_report_capture3, 3,
314                 reportcapture_fixup, 0, ANY_ROUTE},
315         {"float2int", (cmd_function)w_float2int, 2, float2int_fixup, 0,
316                 ANY_ROUTE},
317         {"sip_capture_forward", (cmd_function)w_sip_capture_forward, 1,
318                 fixup_spve_null, 0, ANY_ROUTE},
319         {0, 0, 0, 0, 0, 0}};
320
321
322 static pv_export_t mod_pvs[] = {
323         {{"hep", sizeof("hep") - 1}, PVT_OTHER, pv_get_hep, 0,
324                 pv_parse_hep_name, 0, 0, 0},
325         {{0, 0}, 0, 0, 0, 0, 0, 0, 0}
326 };
327
328 int capture_mode_param(modparam_t type, void *val);
329
330 /*! \brief
331  * Exported parameters
332  */
333 static param_export_t params[] = {
334         {"db_url", PARAM_STR, &db_url},
335         {"table_name", PARAM_STR, &table_name},
336         {"hash_source", PARAM_STR, &hash_source},
337         {"mt_mode", PARAM_STR, &mt_mode},
338         {"date_column", PARAM_STR, &date_column},
339         {"micro_ts_column", PARAM_STR, &micro_ts_column},
340         {"method_column", PARAM_STR, &method_column},
341         {"correlation_column", PARAM_STR, &correlation_column.s},
342         {"reply_reason_column", PARAM_STR, &reply_reason_column},
343         {"ruri_column", PARAM_STR, &ruri_column},
344         {"ruri_user_column", PARAM_STR, &ruri_user_column},
345         {"ruri_domain_column", PARAM_STR, &ruri_domain_column},
346         {"from_user_column", PARAM_STR, &from_user_column},
347         {"from_domain_column", PARAM_STR, &from_domain_column},
348         {"from_tag_column", PARAM_STR, &from_tag_column},
349         {"to_user_column", PARAM_STR, &to_user_column},
350         {"to_domain_column", PARAM_STR, &to_domain_column},
351         {"to_tag_column", PARAM_STR, &to_tag_column},
352         {"pid_user_column", PARAM_STR, &pid_user_column},
353         {"contact_user_column", PARAM_STR, &contact_user_column},
354         {"auth_user_column", PARAM_STR, &auth_user_column},
355         {"callid_column", PARAM_STR, &callid_column},
356         {"callid_aleg_column", PARAM_STR, &callid_aleg_column},
357         {"via_1_column", PARAM_STR, &via_1_column},
358         {"via_1_branch_column", PARAM_STR, &via_1_branch_column},
359         {"cseq_column", PARAM_STR, &cseq_column},
360         {"diversion_column", PARAM_STR, &diversion_column},
361         {"reason_column", PARAM_STR, &reason_column},
362         {"content_type_column", PARAM_STR, &content_type_column},
363         {"authorization_column", PARAM_STR, &authorization_column},
364         {"user_agent_column", PARAM_STR, &user_agent_column},
365         {"source_ip_column", PARAM_STR, &source_ip_column},
366         {"source_port_column", PARAM_STR, &source_port_column},
367         {"destination_ip_column", PARAM_STR, &dest_ip_column},
368         {"destination_port_column", PARAM_STR, &dest_port_column},
369         {"contact_ip_column", PARAM_STR, &contact_ip_column},
370         {"contact_port_column", PARAM_STR, &contact_port_column},
371         {"originator_ip_column", PARAM_STR, &orig_ip_column},
372         {"originator_port_column", PARAM_STR, &orig_port_column},
373         {"proto_column", PARAM_STR, &proto_column},
374         {"family_column", PARAM_STR, &family_column},
375         {"rtp_stat_column", PARAM_STR, &rtp_stat_column},
376         {"type_column", PARAM_STR, &type_column},
377         {"node_column", PARAM_STR, &node_column},
378         {"msg_column", PARAM_STR, &msg_column},
379         {"custom_field1_column", PARAM_STR, &custom_field1_column},
380         {"custom_field2_column", PARAM_STR, &custom_field2_column},
381         {"custom_field3_column", PARAM_STR, &custom_field3_column},
382         {"capture_on", INT_PARAM, &capture_on},
383         {"capture_node", PARAM_STR, &capture_node},
384         {"raw_sock_children", INT_PARAM, &raw_sock_children},
385         {"hep_capture_on", INT_PARAM, &hep_capture_on},
386         {"raw_socket_listen", PARAM_STR, &raw_socket_listen},
387         {"raw_ipip_capture_on", INT_PARAM, &ipip_capture_on},
388         {"raw_moni_capture_on", INT_PARAM, &moni_capture_on},
389         {"db_insert_mode", INT_PARAM, &db_insert_mode},
390         {"raw_interface", PARAM_STR, &raw_interface},
391         {"promiscious_on", INT_PARAM, &promisc_on},
392         {"raw_moni_bpf_on", INT_PARAM, &bpf_on},
393         {"callid_aleg_header", PARAM_STR, &callid_aleg_header},
394         {"custom_field1_header", PARAM_STR, &custom_field1_header},
395         {"custom_field2_header", PARAM_STR, &custom_field2_header},
396         {"custom_field3_header", PARAM_STR, &custom_field3_header},
397         {"capture_mode", PARAM_STRING | USE_FUNC_PARAM,
398                 (void *)capture_mode_param},
399         {"insert_retries", INT_PARAM, &insert_retries},
400         {"insert_retry_timeout", INT_PARAM, &insert_retry_timeout},
401         {"table_time_sufix", PARAM_STR, &table_time_sufix},
402         {"topoh_unmask", PARAM_INT, &sc_topoh_unmask},
403         {"nonsip_hook", PARAM_INT, &nonsip_hook},
404         {"event_callback", PARAM_STR, &sc_event_callback},
405         {0, 0, 0}
406 };
407
408
409 #ifdef STATISTICS
410 /*
411 stat_var* sipcapture_req;
412 stat_var* sipcapture_rpl;
413
414 stat_export_t sipcapture_stats[] = {
415         {"captured_requests" ,  0,  &sipcapture_req  },
416         {"captured_replies"  ,  0,  &sipcapture_rpl  },
417         {0,0,0}
418 };
419 */
420 stat_export_t *sipcapture_stats = NULL;
421 #endif
422
423 /*! \brief module exports */
424 struct module_exports exports = {
425         "sipcapture", DEFAULT_DLFLAGS, /*!< dlopen flags */
426         cmds,                                              /*!< Exported functions */
427         params,                                            /*!< Exported parameters */
428 #ifdef STATISTICS
429         //      sipcapture_stats,  /*!< exported statistics */
430         0,
431 #else
432         0, /*!< exported statistics */
433 #endif
434         0,                 /*!< exported MI functions */
435         mod_pvs,   /*!< exported pseudo-variables */
436         0,                 /*!< extra processes */
437         mod_init,  /*!< module initialization function */
438         0,                 /*!< response function */
439         destroy,   /*!< destroy function */
440         child_init /*!< child initialization function */
441 };
442
443
444 /* returns number of tables if successful
445  * <0 if failed
446  */
447 int parse_table_names(str table_name, str **table_names)
448 {
449
450         char *p = NULL;
451         unsigned int no_tables;
452         char *table_name_cpy;
453         unsigned int i;
454
455         /*parse and save table names*/
456         no_tables = 1;
457         i = 0;
458
459         str *names;
460
461         table_name_cpy = (char *)pkg_malloc(sizeof(char) * table_name.len + 1);
462         if(table_name_cpy == NULL) {
463                 LM_ERR("no more pkg memory left\n");
464                 return -1;
465         }
466         memcpy(table_name_cpy, table_name.s, table_name.len);
467         table_name_cpy[table_name.len] = '\0';
468
469         p = table_name_cpy;
470
471         while(*p) {
472                 if(*p == '|') {
473                         no_tables++;
474                 }
475                 p++;
476         }
477
478         names = (str *)pkg_malloc(sizeof(str) * no_tables);
479         if(names == NULL) {
480                 LM_ERR("no more pkg memory left\n");
481                 pkg_free(table_name_cpy);
482                 return -1;
483         }
484         p = strtok(table_name_cpy, "| \t");
485         while(p != NULL) {
486                 LM_INFO("INFO: table name:%s\n", p);
487                 names[i].len = strlen(p);
488                 names[i].s = (char *)pkg_malloc(sizeof(char) * names[i].len);
489                 memcpy(names[i].s, p, names[i].len);
490                 i++;
491                 p = strtok(NULL, "| \t");
492         }
493
494         pkg_free(table_name_cpy);
495
496         *table_names = names;
497
498         return no_tables;
499 }
500
501 /* checks for some missing fields*/
502 int check_capture_mode(_capture_mode_data_t *n)
503 {
504
505
506         if(!n->db_url.s || !n->db_url.len) {
507                 LM_ERR("db_url not set\n");
508                 goto error;
509         }
510
511         if(!n->mtmode) {
512                 LM_ERR("mt_mode not set\n");
513                 goto error;
514         } else if(!n->no_tables || !n->table_names) {
515                 LM_ERR("table names not set\n");
516                 goto error;
517         }
518         return 0;
519
520 error:
521         LM_ERR("parsing capture_mode: not all needed parameters are set. Please "
522                         "check again\n");
523         return -1;
524 }
525
526 int capture_mode_set_params(_capture_mode_data_t *n, str *params)
527 {
528
529
530         param_t *params_list = NULL;
531         param_hooks_t phooks;
532         param_t *pit = NULL;
533         db_func_t db_funcs;
534
535         str s;
536         LM_DBG("to tokenize: [%.*s]\n", params->len, params->s);
537         if(n == NULL || params == NULL)
538                 return -1;
539         s = *params;
540
541         if(parse_params(&s, CLASS_ANY, &phooks, &params_list) < 0)
542                 return -1;
543         for(pit = params_list; pit; pit = pit->next) {
544                 LM_DBG("parameter is [%.*s]\n", pit->name.len, pit->name.s);
545                 LM_DBG("parameter value is [%.*s]\n", pit->body.len, pit->body.s);
546                 if(pit->name.len == 6
547                                 && strncmp(pit->name.s, "db_url", pit->name.len) == 0) {
548
549                         n->db_url.len = pit->body.len;
550                         n->db_url.s = (char *)pkg_malloc(sizeof(char) * n->db_url.len);
551                         if(!n->db_url.s) {
552                                 LM_ERR("no more pkg memory\n");
553                                 goto error;
554                         }
555                         memcpy(n->db_url.s, pit->body.s, n->db_url.len);
556
557                         if(db_bind_mod(&n->db_url, &db_funcs)) {
558
559                                 LM_ERR("parsing capture_mode: could not bind db funcs for "
560                                                 "url:[%.*s]\n",
561                                                 n->db_url.len, n->db_url.s);
562                                 goto error;
563                         }
564                         n->db_funcs = db_funcs;
565
566                         if(!DB_CAPABILITY(n->db_funcs, DB_CAP_INSERT)) {
567                                 LM_ERR("parsing capture_mode: database modules does not "
568                                                 "provide all functions needed"
569                                                 " by module\n");
570                                 goto error;
571                         }
572
573                 }
574
575                 else if(pit->name.len == 10
576                                 && strncmp(pit->name.s, "table_name", pit->name.len) == 0) {
577                         if((int)(n->no_tables =
578                                                 parse_table_names(pit->body, &n->table_names))
579                                         < 0) {
580                                 LM_ERR("parsing capture_mode: table name parsing failed\n");
581                                 goto error;
582                         }
583
584                 } else if(pit->name.len == 7
585                                 && strncmp(pit->name.s, "mt_mode", pit->name.len) == 0) {
586
587                         if(pit->body.len == 4
588                                         && strncmp(pit->body.s, "rand", pit->body.len) == 0) {
589                                 n->mtmode = mode_random;
590                         } else if(pit->body.len == 11
591                                         && strncmp(pit->body.s, "round_robin", pit->body.len)
592                                         == 0) {
593                                 n->mtmode = mode_round_robin;
594                         } else if(pit->body.len == 4
595                                         && strncmp(pit->body.s, "hash", pit->body.len) == 0) {
596                                 n->mtmode = mode_hash;
597                         } else {
598                                 LM_ERR("parsing capture_mode: capture mode not recognized: "
599                                                 "[%.*s]\n",
600                                                 pit->body.len, pit->body.s);
601                                 goto error;
602                         }
603                 } else if(pit->name.len == 11
604                                 && strncmp(pit->name.s, "hash_source", pit->name.len) == 0) {
605                         if((n->hash_source = get_hash_source(pit->body.s)) == hs_error) {
606                                 LM_ERR("parsing capture_mode: hash source unrecognized: "
607                                                 "[%.*s]\n",
608                                                 pit->body.len, pit->body.s);
609                                 goto error;
610                         }
611                 }
612         }
613         if(n->mtmode == mode_hash
614                         && (n->hash_source == 0 || n->hash_source == hs_error)) {
615                 LM_WARN("Hash mode set, but no hash source provided for [%.*s]. Will "
616                                 "consider hashing by call id.\n",
617                                 n->name.len, n->name.s);
618                 n->hash_source = hs_call_id;
619         }
620
621         if(check_capture_mode(n)) {
622                 goto error;
623         }
624
625         return 0;
626
627 error:
628         if(n->db_url.s) {
629                 pkg_free(n->db_url.s);
630         }
631         return -1;
632 }
633
634 void *capture_mode_init(str *name, str *params)
635 {
636
637         _capture_mode_data_t *n = NULL;
638         unsigned int id;
639
640         if(!name || name->len == 0) {
641                 LM_ERR("capture_mode name is empty\n");
642                 goto error;
643         }
644         if(!params || params->len == 0) {
645                 LM_ERR("capture_mode params are empty\n");
646                 goto error;
647         }
648         id = core_case_hash(name, 0, 0);
649         n = (_capture_mode_data_t *)pkg_malloc(sizeof(_capture_mode_data_t));
650         if(!n) {
651                 LM_ERR("no more pkg memory\n");
652                 goto error;
653         }
654         memset(n, 0, sizeof(_capture_mode_data_t));
655         n->id = id;
656         n->name.len = name->len;
657         n->name.s = (char *)pkg_malloc(sizeof(char) * n->name.len);
658         if(!n->name.s) {
659                 LM_ERR("no more pkg memory\n");
660                 goto error;
661         }
662         memcpy(n->name.s, name->s, n->name.len);
663         n->table_names = (str *)pkg_malloc(sizeof(str));
664         if(!n->table_names) {
665                 LM_ERR("no more pkg memory\n");
666                 goto error;
667         }
668
669
670         if(capture_mode_set_params(n, params) < 0) {
671                 LM_ERR("capture mode parsing failed\n");
672                 goto error;
673         }
674
675         n->next = capture_modes_root;
676         capture_modes_root = n;
677         return n;
678
679 error:
680         if(n) {
681                 if(n->name.s) {
682                         pkg_free(n->name.s);
683                 }
684                 if(n->table_names) {
685                         pkg_free(n->table_names);
686                 }
687                 pkg_free(n);
688         }
689         return 0;
690 }
691
692 /*parse name=>param1=>val1;param2=>val2;..*/
693 int capture_mode_param(modparam_t type, void *val)
694 {
695
696
697         str name;
698         str in;
699         str tok;
700         char *p;
701
702         in.s = val;
703         in.len = strlen(in.s);
704         p = in.s;
705
706         while(p < in.s + in.len
707                         && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'))
708                 p++;
709         if(p > in.s + in.len || *p == '\0')
710                 goto error;
711         name.s = p;
712         while(p < in.s + in.len) {
713                 if(*p == '=' || *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
714                         break;
715                 p++;
716         }
717
718         if(p > in.s + in.len || *p == '\0')
719                 goto error;
720         name.len = p - name.s;
721         if(*p != '=') {
722                 while(p < in.s + in.len
723                                 && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'))
724                         p++;
725                 if(p > in.s + in.len || *p == '\0' || *p != '=')
726                         goto error;
727         }
728         p++;
729         if(*p != '>')
730                 goto error;
731         p++;
732         while(p < in.s + in.len
733                         && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'))
734                 p++;
735         tok.s = p;
736         tok.len = in.len + (int)(in.s - p);
737
738         LM_DBG("capture_mode name: [%.*s] data: [%.*s]\n", name.len, name.s,
739                         tok.len, tok.s);
740         if(!capture_mode_init(&name, &tok)) {
741                 return -1;
742         }
743         return 0;
744
745 error:
746         LM_ERR("invalid parameter [%.*s] at [%d]\n", in.len, in.s, (int)(p - in.s));
747         return -1;
748 }
749
750
751 /*! \brief Initialize sipcapture module */
752 static int mod_init(void)
753 {
754         struct ip_addr *ip = NULL;
755         char *def_params = NULL;
756         sr_kemi_eng_t *keng = NULL;
757
758 #ifdef STATISTICS
759         int cnt = 0;
760         int i = 0;
761         char *stat_name = NULL;
762         _capture_mode_data_t *c = NULL;
763         int def;
764 #endif
765
766         if(sipcapture_init_rpc() != 0) {
767                 LM_ERR("failed to register RPC commands\n");
768                 return -1;
769         }
770
771         if(sc_topoh_unmask == 1) {
772                 /* bind the topoh API */
773                 if(topoh_load_api(&thb) != 0) {
774                         LM_ERR("cannot bind to topoh API\n");
775                         return -1;
776                 }
777         }
778
779         /* Check the table name - if table_name is empty and no capture modes
780          * are defined, then error*/
781         if(!table_name.len && capture_modes_root == NULL) {
782                 LM_ERR("ERROR: sipcapture: mod_init: table_name is not defined or "
783                                 "empty\n");
784                 return -1;
785         }
786
787
788         /*create a default capture mode using the default parameters*/
789         def_params = (char *)pkg_malloc(
790                         snprintf(NULL, 0,
791                                 "db_url=%s;table_name=%s;mt_mode=%s;hash_source=%s",
792                                 db_url.s, table_name.s, mt_mode.s, hash_source.s)
793                         + 1);
794         sprintf(def_params, "db_url=%s;table_name=%s;mt_mode=%s;hash_source=%s",
795                         db_url.s, table_name.s, mt_mode.s, hash_source.s);
796
797         str def_name, def_par;
798         def_name.s = strdup("default");
799         def_name.len = 7;
800         def_par.s = def_params;
801         def_par.len = strlen(def_params);
802
803         LM_DBG("def_params is: %s\n", def_params);
804
805
806         if((capture_def = capture_mode_init(&def_name, &def_par)) == NULL) {
807                 LM_WARN("Default capture mode configuration failed. Suppose "
808                                 "sip_capture calls will use other defined capture modes.\n");
809         }
810
811         pkg_free(def_params);
812
813         if(sipcapture_parse_aleg_callid_headers() < 0) {
814                 return -1;
815         }
816
817
818 #ifdef STATISTICS
819
820         int route_no;
821         c = capture_modes_root;
822         while(c) {
823                 cnt++;
824                 c = c->next;
825         }
826         /*requests and replies for each mode + 1 zero-filled stat_export */
827         stat_export_t *stats =
828                 (stat_export_t *)shm_malloc(sizeof(stat_export_t) * cnt * 2 + 1);
829
830         c = capture_modes_root;
831
832         while(c) {
833                 /*for the default capture_mode, don't add it's name to the stat name*/
834                 def = (capture_def && c == capture_def) ? 1 : 0;
835                 stat_name = (char *)shm_malloc(
836                                 sizeof(char)
837                                 * (snprintf(NULL, 0,
838                                                 (def) ? "captured_requests%.*s"
839                                                 : "captured_requests[%.*s]",
840                                                 (def) ? 0 : c->name.len, (def) ? "" : c->name.s)
841                                         + 1));
842                 sprintf(stat_name,
843                                 (def) ? "captured_requests%.*s" : "captured_requests[%.*s]",
844                                 (def) ? 0 : c->name.len, (def) ? "" : c->name.s);
845                 stats[i].name = stat_name;
846                 stats[i].flags = 0;
847                 stats[i].stat_pointer = &c->sipcapture_req;
848                 i++;
849                 stat_name = (char *)shm_malloc(
850                                 sizeof(char)
851                                 * (snprintf(NULL, 0,
852                                                 (def) ? "captured_replies%.*s"
853                                                 : "captured_replies[%.*s]",
854                                                 (def) ? 0 : c->name.len, (def) ? "" : c->name.s)
855                                         + 1));
856                 sprintf(stat_name,
857                                 (def) ? "captured_replies%.*s" : "captured_replies[%.*s]",
858                                 (def) ? 0 : c->name.len, (def) ? "" : c->name.s);
859                 stats[i].name = stat_name;
860                 stats[i].flags = 0;
861                 stats[i].stat_pointer = &c->sipcapture_rpl;
862                 i++;
863                 c = c->next;
864         }
865         stats[i].name = 0;
866         stats[i].flags = 0;
867         stats[i].stat_pointer = 0;
868
869         sipcapture_stats = stats;
870
871         /* register statistics */
872         if(register_module_stats(exports.name, sipcapture_stats) != 0) {
873                 LM_ERR("failed to register core statistics\n");
874                 return -1;
875         }
876 #endif
877
878         kam_srand(time(NULL));
879
880
881         if(db_insert_mode) {
882                 LM_INFO("INFO: sipcapture: mod_init: you have enabled INSERT DELAYED \
883                                 Make sure your DB can support it\n");
884         }
885
886         capture_on_flag = (int *)shm_malloc(sizeof(int));
887         if(capture_on_flag == NULL) {
888                 LM_ERR("no more shm memory left\n");
889                 return -1;
890         }
891
892         *capture_on_flag = capture_on;
893
894         if(nonsip_hook) {
895                 if(sc_event_callback.s == NULL || sc_event_callback.len <= 0) {
896                         route_no = route_get(&event_rt, "sipcapture:request");
897                         if(route_no == -1) {
898                                 LM_ERR("failed to find event_route[sipcapture:request]\n");
899                                 return -1;
900                         }
901
902                         if(event_rt.rlist[route_no] == 0) {
903                                 LM_ERR("event_route[sipcapture:request] is empty\n");
904                                 return -1;
905                         }
906
907                         hep_route_no = route_no;
908                 } else {
909                         hep_route_no = -1;
910                         keng = sr_kemi_eng_get();
911                         if(keng == NULL) {
912                                 LM_DBG("event callback (%s) set, but no cfg engine\n",
913                                                 sc_event_callback.s);
914                                 return -1;
915                         }
916                 }
917
918                 if(sr_event_register_cb(SREV_RCV_NOSIP, nosip_hep_msg) < 0) {
919                         LM_ERR("failed to register SREV_RCV_NOSIP event\n");
920                         return -1;
921                 }
922         } else {
923                 /* register DGRAM event */
924                 if(sr_event_register_cb(SREV_NET_DGRAM_IN, hep_msg_received) < 0) {
925                         LM_ERR("failed to register SREV_NET_DGRAM_IN event\n");
926                         return -1;
927                 }
928         }
929
930         if(ipip_capture_on && moni_capture_on) {
931                 LM_ERR("only one RAW mode is supported. Please disable ipip_capture_on "
932                                 "or moni_capture_on\n");
933                 return -1;
934         }
935
936         if((insert_retries < 0) || (insert_retries > 500)) {
937                 LM_ERR("insert_retries should be a value between 0 and 500\n");
938                 return -1;
939         }
940
941         if((0 == insert_retries) && (insert_retry_timeout != 0)) {
942                 LM_ERR("insert_retry_timeout has no meaning when insert_retries is not "
943                                 "set\n");
944         }
945
946         if((insert_retry_timeout < 0) || (insert_retry_timeout > 300)) {
947                 LM_ERR("insert_retry_timeout should be a value between 0 and 300\n");
948                 return -1;
949         }
950
951         /* raw processes for IPIP encapsulation */
952         if(ipip_capture_on || moni_capture_on) {
953                 register_procs(raw_sock_children);
954
955                 if(extract_host_port()
956                                 && (((ip = str2ip(&raw_socket_listen)) == NULL)
957                                         && ((ip = str2ip6(&raw_socket_listen)) == NULL))) {
958                         LM_ERR("sipcapture mod_init: bad RAW IP: %.*s\n",
959                                         raw_socket_listen.len, raw_socket_listen.s);
960                         return -1;
961                 }
962
963                 if(moni_capture_on && !moni_port_start) {
964                         LM_ERR("ERROR:sipcapture:mod_init: Please define port/portrange"
965                                         " in 'raw_socket_listen', before activate"
966                                         " monitoring capture\n");
967                         return -1;
968                 }
969
970                 raw_sock_desc = raw_capture_socket(raw_socket_listen.len ? ip : 0,
971                                 raw_interface.len ? &raw_interface : 0, moni_port_start,
972                                 moni_port_end, ipip_capture_on ? IPPROTO_IPIP : htons(0x0800));
973
974                 if(raw_sock_desc < 0) {
975                         LM_ERR("could not initialize raw udp socket:"
976                                         " %s (%d)\n",
977                                         strerror(errno), errno);
978                         if(errno == EPERM)
979                                 LM_ERR("could not initialize raw socket on startup"
980                                                 " due to inadequate permissions, please"
981                                                 " restart as root or with CAP_NET_RAW\n");
982
983                         return -1;
984                 }
985
986                 if(promisc_on && raw_interface.len) {
987
988                         memset(&ifr, 0, sizeof(ifr));
989                         memcpy(ifr.ifr_name, raw_interface.s, raw_interface.len);
990
991
992 #ifdef __OS_linux
993                         if(ioctl(raw_sock_desc, SIOCGIFFLAGS, &ifr) < 0) {
994                                 LM_ERR("could not get flags from interface [%.*s]:"
995                                                 " %s (%d)\n",
996                                                 raw_interface.len, raw_interface.s, strerror(errno),
997                                                 errno);
998                                 goto error;
999                         }
1000
1001                         ifr.ifr_flags |= IFF_PROMISC;
1002
1003                         if(ioctl(raw_sock_desc, SIOCSIFFLAGS, &ifr) < 0) {
1004                                 LM_ERR("could not set PROMISC flag to interface [%.*s]:"
1005                                                 " %s (%d)\n",
1006                                                 raw_interface.len, raw_interface.s, strerror(errno),
1007                                                 errno);
1008                                 goto error;
1009                         }
1010 #endif
1011                 }
1012         }
1013
1014         return 0;
1015 #ifdef __OS_linux
1016 error:
1017         if(raw_sock_desc)
1018                 close(raw_sock_desc);
1019         return -1;
1020 #endif
1021 }
1022
1023 static int sipcapture_fixup(void **param, int param_no)
1024 {
1025
1026         _capture_mode_data_t *con;
1027
1028         str val;
1029         unsigned int id;
1030
1031         if(param_no == 1) {
1032                 return fixup_var_pve_str_12(param, 1);
1033         }
1034         if(param_no == 2) {
1035
1036                 val.s = (char *)*param;
1037                 val.len = strlen((char *)*param);
1038
1039
1040                 con = capture_modes_root;
1041                 id = core_case_hash(&val, 0, 0);
1042                 while(con) {
1043                         if(id == con->id && con->name.len == val.len
1044                                         && strncmp(con->name.s, val.s, val.len) == 0) {
1045                                 *param = (void *)con;
1046                                 LM_DBG("found capture mode :[%.*s]\n", con->name.len,
1047                                                 con->name.s);
1048                                 return 0;
1049                         }
1050                         con = con->next;
1051                 }
1052
1053                 LM_ERR("no capture mode found\n");
1054                 return -1;
1055         }
1056
1057         return 0;
1058 }
1059
1060 static int reportcapture_fixup(void **param, int param_no)
1061 {
1062
1063         if(param_no == 1) {
1064                 return fixup_var_pve_str_12(param, 1);
1065         }
1066         if(param_no == 2) {
1067                 return fixup_var_pve_str_12(param, 1);
1068         }
1069         if(param_no == 3) {
1070                 return fixup_var_pve_str_12(param, 1);
1071         }
1072
1073         return 0;
1074 }
1075
1076 static int float2int_fixup(void **param, int param_no)
1077 {
1078
1079         if(param_no == 1) {
1080                 return fixup_var_pve_str_12(param, 1);
1081         }
1082         if(param_no == 2) {
1083                 return fixup_var_pve_str_12(param, 1);
1084         }
1085
1086         return 0;
1087 }
1088
1089
1090 static int w_float2int(sip_msg_t *_m, char *_val, char *_coof)
1091 {
1092         str value = {0};
1093         str coof = {0};
1094         int ret = 0;
1095
1096         if(_val != NULL && (get_str_fparam(&value, _m, (fparam_t *)_val) < 0)) {
1097                 LM_ERR("invalid table parameter [%s] [%s]\n", _val, value.s);
1098                 return -1;
1099         }
1100
1101         if(_coof != NULL && (get_str_fparam(&coof, _m, (fparam_t *)_coof) < 0)) {
1102                 LM_ERR("invalid data parameter [%s] [%s]\n", _coof, coof.s);
1103                 return -1;
1104         }
1105
1106         if(value.s == NULL || coof.s == NULL)
1107                 return -1;
1108
1109         ret = (int)(atof(value.s) * atoi(coof.s));
1110
1111         return ret ? ret : -1;
1112 }
1113
1114 static int ki_float2int(sip_msg_t *_m, str *_val, str *_coof)
1115 {
1116         int ret = 0;
1117
1118         if(_val == NULL || _val->s == NULL || _val->len <= 0
1119                         || _coof == NULL || _coof->s == NULL || _coof->len <= 0)
1120                 return -1;
1121
1122         ret = (int)(atof(_val->s) * atoi(_coof->s));
1123
1124         return ret ? ret : -1;
1125 }
1126
1127 static int w_sip_capture(sip_msg_t *_m, char *_table,
1128                 _capture_mode_data_t *cm_data)
1129 {
1130         str table = {0};
1131
1132         if(_table != NULL && (get_str_fparam(&table, _m, (fparam_t *)_table) < 0)) {
1133                 LM_ERR("invalid table parameter [%s] [%s]\n", _table, table.s);
1134                 return -1;
1135         }
1136
1137         return sip_capture(_m, (table.len > 0) ? &table : NULL, cm_data);
1138 }
1139
1140 static int w_sip_capture0(sip_msg_t *_m, char *_p1, char *_p2)
1141 {
1142         return w_sip_capture(_m, NULL, NULL);
1143 }
1144
1145 static int w_sip_capture1(sip_msg_t *_m, char *_table, char *_p2)
1146 {
1147         return w_sip_capture(_m, _table, NULL);
1148 }
1149
1150 static int w_sip_capture2(sip_msg_t *_m, char *_table, char *_cmdata)
1151 {
1152         return w_sip_capture(_m, _table, (_capture_mode_data_t*)_cmdata);
1153 }
1154
1155 static int ki_sip_capture_mode(sip_msg_t *_m, str *_table, str *_cmdata)
1156 {
1157         _capture_mode_data_t *pcmdata = NULL;
1158         unsigned int id;
1159
1160         if(_cmdata != NULL && _cmdata->len > 0) {
1161                 pcmdata = capture_modes_root;
1162                 id = core_case_hash(_cmdata, 0, 0);
1163                 while(pcmdata) {
1164                         if(id == pcmdata->id && pcmdata->name.len == _cmdata->len
1165                                         && strncmp(pcmdata->name.s, _cmdata->s, _cmdata->len) == 0) {
1166                                 break;
1167                         }
1168                         pcmdata = pcmdata->next;
1169                 }
1170                 if(pcmdata) {
1171                         LM_DBG("found capture mode: [%.*s]\n", _cmdata->len, _cmdata->s);
1172                 } else {
1173                         LM_ERR("not found capture mode: [%.*s]\n", _cmdata->len, _cmdata->s);
1174                         return -1;
1175                 }
1176         }
1177
1178         return sip_capture(_m, (_table != NULL && _table->len > 0) ? _table : NULL,
1179                         pcmdata);
1180 }
1181
1182 static int ki_sip_capture(sip_msg_t *_m)
1183 {
1184         return sip_capture(_m, NULL, NULL);
1185 }
1186
1187 static int ki_sip_capture_table(sip_msg_t *_m, str *_table)
1188 {
1189         return sip_capture(_m, _table, NULL);
1190 }
1191
1192 static int w_report_capture1(sip_msg_t *_m, char *_table, char *_p2)
1193 {
1194         return w_report_capture3(_m, _table, NULL, NULL);
1195 }
1196
1197 static int w_report_capture2(sip_msg_t *_m, char *_table, char *_corr)
1198 {
1199         return w_report_capture3(_m, _table, _corr, NULL);
1200 }
1201
1202 static int w_report_capture3(sip_msg_t *_m, char *_table, char *_corr,
1203                 char *_data)
1204 {
1205         str table = {0};
1206         str corr = {0};
1207         str data = {0};
1208
1209         if(_table != NULL && (get_str_fparam(&table, _m, (fparam_t *)_table) < 0)) {
1210                 LM_ERR("invalid table parameter [%s] [%s]\n", _table, table.s);
1211                 return -1;
1212         }
1213
1214         if(_corr != NULL && (get_str_fparam(&corr, _m, (fparam_t *)_corr) < 0)) {
1215                 LM_ERR("invalid corr parameter [%s] [%s]\n", _corr, corr.s);
1216                 return -1;
1217         }
1218
1219         if(_data != NULL && (get_str_fparam(&data, _m, (fparam_t *)_data) < 0)) {
1220
1221
1222                 LM_ERR("invalid data parameter [%s] [%s]\n", _data, data.s);
1223                 return -1;
1224         }
1225
1226         /* workaround for data function */
1227         if(data.len > 0 && !strncmp(data.s, "report_capture", data.len))
1228                 data.len = 0;
1229
1230         return report_capture(_m, (table.len > 0) ? &table : NULL,
1231                         (corr.len > 0) ? &corr : NULL, (data.len > 0) ? &data : NULL);
1232 }
1233
1234
1235 static int ki_report_capture_data(sip_msg_t *_m, str *_table, str *_cid,
1236                 str *_data)
1237 {
1238         /* workaround for data function */
1239         if(_data!=NULL && _data->len > 0 && _data->len == 14
1240                         && !strncmp(_data->s, "report_capture", 14)) {
1241                 return report_capture(_m,
1242                                 (_table!=NULL && _table->len > 0) ? _table : NULL,
1243                                 (_cid != NULL && _cid->len > 0) ? _cid : NULL, NULL);
1244
1245         } else {
1246                 return report_capture(_m,
1247                                 (_table!=NULL && _table->len > 0) ? _table : NULL,
1248                                 (_cid != NULL && _cid->len > 0) ? _cid : NULL,
1249                                 (_data != NULL && _data->len > 0) ? _data : NULL);
1250
1251         }
1252 }
1253
1254 static int ki_report_capture(sip_msg_t *_m, str *_table)
1255 {
1256         return ki_report_capture_data(_m, _table, NULL, NULL);
1257 }
1258
1259 static int ki_report_capture_cid(sip_msg_t *_m, str *_table, str *_cid)
1260 {
1261         return ki_report_capture_data(_m, _table, _cid, NULL);
1262 }
1263
1264 int extract_host_port(void)
1265 {
1266         if(raw_socket_listen.len) {
1267                 char *p1, *p2;
1268                 p1 = raw_socket_listen.s;
1269
1270                 if((p1 = strrchr(p1, ':')) != 0) {
1271                         *p1 = '\0';
1272                         p1++;
1273                         p2 = p1;
1274                         if((p2 = strrchr(p2, '-')) != 0) {
1275                                 p2++;
1276                                 moni_port_end = atoi(p2);
1277                                 p1[strlen(p1) - strlen(p2) - 1] = '\0';
1278                         }
1279                         moni_port_start = atoi(p1);
1280                         raw_socket_listen.len = strlen(raw_socket_listen.s);
1281                 }
1282                 return 1;
1283         }
1284         return 0;
1285 }
1286
1287
1288 static int child_init(int rank)
1289 {
1290
1291         _capture_mode_data_t *c;
1292
1293         if(rank == PROC_MAIN && (ipip_capture_on || moni_capture_on)) {
1294                 if(init_rawsock_children() < 0)
1295                         return -1;
1296         }
1297
1298         if(rank == PROC_INIT || rank == PROC_MAIN || rank == PROC_TCP_MAIN)
1299                 return 0; /* do nothing for the main process */
1300
1301
1302         c = capture_modes_root;
1303
1304         while(c) {
1305                 if(!c->db_url.s || !c->db_url.len) {
1306                         LM_ERR("DB URL not set for capture mode:[%.*s]\n", c->name.len,
1307                                         c->name.s);
1308                         return -1;
1309                 }
1310                 c->db_con = c->db_funcs.init(&c->db_url);
1311                 if(!c->db_con) {
1312                         LM_ERR("unable to connect to database [%.*s] from capture_mode "
1313                                         "param.\n",
1314                                         c->db_url.len, c->db_url.s);
1315                         return -1;
1316                 }
1317                 if(c->mtmode == mode_round_robin && rank > 0) {
1318                         c->rr_idx = rank % c->no_tables;
1319                 }
1320                 c = c->next;
1321         }
1322
1323
1324         heptime = (struct hep_timeinfo *)pkg_malloc(sizeof(struct hep_timeinfo));
1325         if(heptime == NULL) {
1326                 LM_ERR("no more pkg memory left\n");
1327                 return -1;
1328         }
1329
1330         heptime->tv_sec = 0;
1331
1332         return 0;
1333 }
1334
1335 /*
1336  * RAW IPIP || Monitoring listeners
1337  */
1338 int init_rawsock_children(void)
1339 {
1340         int i;
1341         pid_t pid;
1342
1343         for(i = 0; i < raw_sock_children; i++) {
1344                 pid = fork_process(PROC_UNIXSOCK, "homer raw socket", 1);
1345                 if(pid < 0) {
1346                         ERR("Unable to fork: %s\n", strerror(errno));
1347                         return -1;
1348                 } else if(pid == 0) { /* child */
1349                         raw_capture_rcv_loop(raw_sock_desc, moni_port_start, moni_port_end,
1350                                         moni_capture_on ? 0 : 1);
1351                 }
1352                 /* Parent */
1353         }
1354
1355         DBG("Raw IPIP socket server successfully initialized\n");
1356         return 1;
1357 }
1358
1359
1360 static void destroy(void)
1361 {
1362         //if (capture_def->db_con!=NULL)
1363         //      capture_def->db_funcs.close(capture_def->db_con);
1364
1365         /*free content from the linked list*/
1366         _capture_mode_data_t *c;
1367         _capture_mode_data_t *c0;
1368
1369         c = capture_modes_root;
1370
1371         while(c) {
1372                 c0 = c->next;
1373                 if(c->name.s) {
1374                         pkg_free(c->name.s);
1375                 }
1376                 if(c->db_url.s) {
1377                         pkg_free(c->db_url.s);
1378                 }
1379                 if(c->db_con) {
1380                         c->db_funcs.close(c->db_con);
1381                 }
1382                 if(c->table_names) {
1383                         pkg_free(c->table_names);
1384                 }
1385
1386                 pkg_free(c);
1387                 c = c0;
1388         }
1389
1390         if(capture_on_flag)
1391                 shm_free(capture_on_flag);
1392
1393         if(heptime)
1394                 pkg_free(heptime);
1395
1396         if(raw_sock_desc > 0) {
1397                 if(promisc_on && raw_interface.len) {
1398 #ifdef __OS_linux
1399                         ifr.ifr_flags &= ~(IFF_PROMISC);
1400
1401                         if(ioctl(raw_sock_desc, SIOCSIFFLAGS, &ifr) < 0) {
1402                                 LM_ERR("destroy: could not remove PROMISC flag from interface "
1403                                                 "[%.*s]:"
1404                                                 " %s (%d)\n",
1405                                                 raw_interface.len, raw_interface.s, strerror(errno),
1406                                                 errno);
1407                         }
1408 #endif
1409                 }
1410                 close(raw_sock_desc);
1411         }
1412
1413
1414         //      if (table_names){
1415         //              pkg_free(table_names);
1416         //      }
1417 }
1418
1419 static int sip_capture_prepare(sip_msg_t *msg)
1420 {
1421         /* We need parse all headers */
1422         if(parse_headers(msg, HDR_CALLID_F | HDR_EOH_F, 0) != 0) {
1423                 LM_ERR("cannot parse headers\n");
1424                 return 0;
1425         }
1426
1427         return 0;
1428 }
1429
1430 static int sip_capture_store(struct _sipcapture_object *sco, str *dtable,
1431                 _capture_mode_data_t *cm_data)
1432 {
1433         db_key_t db_keys[NR_KEYS];
1434         db_val_t db_vals[NR_KEYS];
1435
1436         str tmp, corrtmp, ntab;
1437         int ii = 0;
1438         int ret = 0;
1439         int counter = 0;
1440         db_insert_f insert;
1441         time_t retry_failed_time = 0;
1442         struct tm capt_ts;
1443         str ocallid;
1444
1445         /* new */
1446         str *table = NULL;
1447         _capture_mode_data_t *c = NULL;
1448         char strftime_buf[128];
1449         time_t tvsec_;
1450
1451         c = (cm_data) ? cm_data : capture_def;
1452         if(!c) {
1453                 LM_ERR("no connection mode available to store data\n");
1454                 return -1;
1455         }
1456
1457         if(sco == NULL) {
1458                 LM_DBG("invalid parameter\n");
1459                 return -1;
1460         }
1461
1462         if(sc_topoh_unmask == 1) {
1463                 if(thb.unmask_callid(&sco->callid, &ocallid) == 0) {
1464                         sco->callid = ocallid;
1465                 }
1466         }
1467
1468         if(correlation_id) {
1469                 corrtmp.s = correlation_id;
1470                 corrtmp.len = strlen(correlation_id);
1471         }
1472
1473         db_keys[0] = &date_column;
1474         db_vals[0].type = DB1_DATETIME;
1475         db_vals[0].nul = 0;
1476         db_vals[0].val.time_val = (sco->tmstamp / 1000000);
1477
1478         db_keys[1] = &micro_ts_column;
1479         db_vals[1].type = DB1_BIGINT;
1480         db_vals[1].nul = 0;
1481         db_vals[1].val.ll_val = sco->tmstamp;
1482
1483         db_keys[2] = &method_column;
1484         db_vals[2].type = DB1_STR;
1485         db_vals[2].nul = 0;
1486         db_vals[2].val.str_val = sco->method;
1487
1488         db_keys[3] = &reply_reason_column;
1489         db_vals[3].type = DB1_STR;
1490         db_vals[3].nul = 0;
1491         db_vals[3].val.str_val = sco->reply_reason;
1492
1493         db_keys[4] = &ruri_column;
1494         db_vals[4].type = DB1_STR;
1495         db_vals[4].nul = 0;
1496         db_vals[4].val.str_val = sco->ruri;
1497
1498         db_keys[5] = &ruri_user_column;
1499         db_vals[5].type = DB1_STR;
1500         db_vals[5].nul = 0;
1501         db_vals[5].val.str_val = sco->ruri_user;
1502
1503         db_keys[6] = &from_user_column;
1504         db_vals[6].type = DB1_STR;
1505         db_vals[6].nul = 0;
1506         db_vals[6].val.str_val = sco->from_user;
1507
1508         db_keys[7] = &from_tag_column;
1509         db_vals[7].type = DB1_STR;
1510         db_vals[7].nul = 0;
1511         db_vals[7].val.str_val = sco->from_tag;
1512
1513         db_keys[8] = &to_user_column;
1514         db_vals[8].type = DB1_STR;
1515         db_vals[8].nul = 0;
1516         db_vals[8].val.str_val = sco->to_user;
1517
1518         db_keys[9] = &to_tag_column;
1519         db_vals[9].type = DB1_STR;
1520         db_vals[9].nul = 0;
1521         db_vals[9].val.str_val = sco->to_tag;
1522
1523         db_keys[10] = &pid_user_column;
1524         db_vals[10].type = DB1_STR;
1525         db_vals[10].nul = 0;
1526         db_vals[10].val.str_val = sco->pid_user;
1527
1528         db_keys[11] = &contact_user_column;
1529         db_vals[11].type = DB1_STR;
1530         db_vals[11].nul = 0;
1531         db_vals[11].val.str_val = sco->contact_user;
1532
1533         db_keys[12] = &auth_user_column;
1534         db_vals[12].type = DB1_STR;
1535         db_vals[12].nul = 0;
1536         db_vals[12].val.str_val = sco->auth_user;
1537
1538         db_keys[13] = &callid_column;
1539         db_vals[13].type = DB1_STR;
1540         db_vals[13].nul = 0;
1541         db_vals[13].val.str_val = sco->callid;
1542
1543         db_keys[14] = &callid_aleg_column;
1544         db_vals[14].type = DB1_STR;
1545         db_vals[14].nul = 0;
1546         db_vals[14].val.str_val = sco->callid_aleg;
1547
1548         db_keys[15] = &via_1_column;
1549         db_vals[15].type = DB1_STR;
1550         db_vals[15].nul = 0;
1551         db_vals[15].val.str_val = sco->via_1;
1552
1553         db_keys[16] = &via_1_branch_column;
1554         db_vals[16].type = DB1_STR;
1555         db_vals[16].nul = 0;
1556         db_vals[16].val.str_val = sco->via_1_branch;
1557
1558         db_keys[17] = &cseq_column;
1559         db_vals[17].type = DB1_STR;
1560         db_vals[17].nul = 0;
1561         db_vals[17].val.str_val = sco->cseq;
1562
1563         db_keys[18] = &reason_column;
1564         db_vals[18].type = DB1_STR;
1565         db_vals[18].nul = 0;
1566         db_vals[18].val.str_val = sco->reason;
1567
1568         db_keys[19] = &content_type_column;
1569         db_vals[19].type = DB1_STR;
1570         db_vals[19].nul = 0;
1571         db_vals[19].val.str_val = sco->content_type;
1572
1573         db_keys[20] = &authorization_column;
1574         db_vals[20].type = DB1_STR;
1575         db_vals[20].nul = 0;
1576         db_vals[20].val.str_val = sco->authorization;
1577
1578         db_keys[21] = &user_agent_column;
1579         db_vals[21].type = DB1_STR;
1580         db_vals[21].nul = 0;
1581         db_vals[21].val.str_val = sco->user_agent;
1582
1583         db_keys[22] = &source_ip_column;
1584         db_vals[22].type = DB1_STR;
1585         db_vals[22].nul = 0;
1586         db_vals[22].val.str_val = sco->source_ip;
1587
1588         db_keys[23] = &source_port_column;
1589         db_vals[23].type = DB1_INT;
1590         db_vals[23].nul = 0;
1591         db_vals[23].val.int_val = sco->source_port;
1592
1593         db_keys[24] = &dest_ip_column;
1594         db_vals[24].type = DB1_STR;
1595         db_vals[24].nul = 0;
1596         db_vals[24].val.str_val = sco->destination_ip;
1597
1598         db_keys[25] = &dest_port_column;
1599         db_vals[25].type = DB1_INT;
1600         db_vals[25].nul = 0;
1601         db_vals[25].val.int_val = sco->destination_port;
1602
1603         db_keys[26] = &contact_ip_column;
1604         db_vals[26].type = DB1_STR;
1605         db_vals[26].nul = 0;
1606         db_vals[26].val.str_val = sco->contact_ip;
1607
1608         db_keys[27] = &contact_port_column;
1609         db_vals[27].type = DB1_INT;
1610         db_vals[27].nul = 0;
1611         db_vals[27].val.int_val = sco->contact_port;
1612
1613         db_keys[28] = &orig_ip_column;
1614         db_vals[28].type = DB1_STR;
1615         db_vals[28].nul = 0;
1616         db_vals[28].val.str_val = sco->originator_ip;
1617
1618         db_keys[29] = &orig_port_column;
1619         db_vals[29].type = DB1_INT;
1620         db_vals[29].nul = 0;
1621         db_vals[29].val.int_val = sco->originator_port;
1622
1623         db_keys[30] = &proto_column;
1624         db_vals[30].type = DB1_INT;
1625         db_vals[30].nul = 0;
1626         db_vals[30].val.int_val = sco->proto;
1627
1628         db_keys[31] = &family_column;
1629         db_vals[31].type = DB1_INT;
1630         db_vals[31].nul = 0;
1631         db_vals[31].val.int_val = sco->family;
1632
1633         db_keys[32] = &rtp_stat_column;
1634         db_vals[32].type = DB1_STR;
1635         db_vals[32].nul = 0;
1636         db_vals[32].val.str_val = sco->rtp_stat;
1637
1638         db_keys[33] = &type_column;
1639         db_vals[33].type = DB1_INT;
1640         db_vals[33].nul = 0;
1641         db_vals[33].val.int_val = sco->type;
1642
1643         db_keys[34] = &node_column;
1644         db_vals[34].type = DB1_STR;
1645         db_vals[34].nul = 0;
1646         db_vals[34].val.str_val = sco->node;
1647
1648         db_keys[35] = &correlation_column;
1649         db_vals[35].type = DB1_STR;
1650         db_vals[35].nul = 0;
1651         db_vals[35].val.str_val = (correlation_id) ? corrtmp : sco->callid;
1652
1653         db_keys[36] = &from_domain_column;
1654         db_vals[36].type = DB1_STR;
1655         db_vals[36].nul = 0;
1656         db_vals[36].val.str_val = sco->from_domain;
1657
1658         db_keys[37] = &to_domain_column;
1659         db_vals[37].type = DB1_STR;
1660         db_vals[37].nul = 0;
1661         db_vals[37].val.str_val = sco->to_domain;
1662
1663         db_keys[38] = &ruri_domain_column;
1664         db_vals[38].type = DB1_STR;
1665         db_vals[38].nul = 0;
1666         db_vals[38].val.str_val = sco->ruri_domain;
1667
1668         db_keys[39] = &diversion_column;
1669         db_vals[39].type = DB1_STR;
1670         db_vals[39].nul = 0;
1671         db_vals[39].val.str_val = sco->diversion;
1672
1673         db_keys[40] = &msg_column;
1674         db_vals[40].type = DB1_BLOB;
1675         db_vals[40].nul = 0;
1676
1677         /*we don't have empty spaces now */
1678         tmp.s = sco->msg.s;
1679         tmp.len = sco->msg.len;
1680
1681         db_vals[40].val.blob_val = tmp;
1682
1683         db_keys[41] = &custom_field1_column;
1684         db_vals[41].type = DB1_STR;
1685         db_vals[41].nul = 0;
1686         db_vals[41].val.str_val = sco->custom1;
1687
1688         db_keys[42] = &custom_field2_column;
1689         db_vals[42].type = DB1_STR;
1690         db_vals[42].nul = 0;
1691         db_vals[42].val.str_val = sco->custom2;
1692
1693         db_keys[43] = &custom_field3_column;
1694         db_vals[43].type = DB1_STR;
1695         db_vals[43].nul = 0;
1696         db_vals[43].val.str_val = sco->custom3;
1697
1698         if(dtable) {
1699                 table = dtable;
1700         } else if(c->no_tables > 0) {
1701
1702                 if(c->mtmode == mode_hash) {
1703                         ii = hash_func(sco, c->hash_source, c->no_tables);
1704                         if(ii < 0) {
1705                                 LM_ERR("hashing failed\n");
1706                                 return -1;
1707                         }
1708                         LM_DBG("hash idx is:%d\n", ii);
1709                 } else if(c->mtmode == mode_random) {
1710                         ii = kam_rand() % c->no_tables;
1711                         LM_DBG("rand idx is:%d\n", ii);
1712                 } else if(c->mtmode == mode_round_robin) {
1713                         ii = c->rr_idx;
1714                         c->rr_idx = (c->rr_idx + 1) % c->no_tables;
1715                         LM_DBG("round robin idx is:%d\n", ii);
1716                 }
1717                 table = &c->table_names[ii];
1718         } else {
1719                 table = &table_name;
1720         }
1721
1722         tvsec_ = (time_t)(sco->tmstamp / 1000000);
1723         if(gmtime_r(&tvsec_, &capt_ts) == NULL) {
1724                 LM_ERR("unable to set time to attributes\n");
1725                 return -1;
1726         }
1727
1728         ntab.len = strftime(strftime_buf, sizeof(strftime_buf), table->s, &capt_ts);
1729         ntab.s = strftime_buf;
1730
1731         table = &ntab;
1732
1733         /* check dynamic table */
1734         LM_DBG("insert into homer table [1]: [%.*s]\n", table->len, table->s);
1735         c->db_funcs.use_table(c->db_con, table);
1736
1737         LM_DBG("storing info...\n");
1738
1739         if(db_insert_mode == 1 && c->db_funcs.insert_delayed != NULL)
1740                 insert = c->db_funcs.insert_delayed;
1741         else if(db_insert_mode == 2 && c->db_funcs.insert_async != NULL)
1742                 insert = c->db_funcs.insert_async;
1743         else
1744                 insert = c->db_funcs.insert;
1745         ret = insert(c->db_con, db_keys, db_vals, NR_KEYS);
1746
1747         if(ret < 0) {
1748                 LM_DBG("failed to insert into database(first attempt)\n");
1749                 if(insert_retries != 0) {
1750                         counter = 0;
1751                         while((ret = insert(c->db_con, db_keys, db_vals, NR_KEYS)) < 0) {
1752                                 counter++;
1753                                 if(1 == counter) //first failed retry
1754                                         retry_failed_time = time(NULL);
1755
1756                                 if((counter > insert_retries)
1757                                                 || (time(NULL) - retry_failed_time
1758                                                         > insert_retry_timeout)) {
1759                                         LM_ERR("failed to insert into database(second attempt)\n");
1760                                         break;
1761                                 }
1762                         }
1763                 }
1764         }
1765         if(ret < 0)
1766                 goto error;
1767 #ifdef STATISTICS
1768         update_stat(sco->stat, 1);
1769 #endif
1770
1771         return 1;
1772 error:
1773         return -1;
1774 }
1775
1776 static int sip_capture(sip_msg_t *msg, str *_table,
1777                 _capture_mode_data_t *cm_data)
1778 {
1779         struct _sipcapture_object sco;
1780         struct sip_uri from, to, contact;
1781         struct hdr_field *hook1 = NULL;
1782         hdr_field_t *tmphdr[7];
1783         contact_body_t *cb = 0;
1784         char buf_ip[IP_ADDR_MAX_STR_SIZE + 12];
1785         char *port_str = NULL, *tmp = NULL;
1786         struct timeval tvb;
1787         struct timezone tz;
1788         char tmp_node[100];
1789
1790         LM_DBG("CAPTURE DEBUG...\n");
1791
1792         gettimeofday(&tvb, &tz);
1793
1794         if(msg == NULL) {
1795                 LM_DBG("nothing to capture\n");
1796                 return -1;
1797         }
1798         memset(&sco, 0, sizeof(struct _sipcapture_object));
1799
1800
1801         if(capture_on_flag == NULL || *capture_on_flag == 0) {
1802                 LM_DBG("capture off...\n");
1803                 return -1;
1804         }
1805
1806         if(sip_capture_prepare(msg) < 0)
1807                 return -1;
1808
1809         if(msg->first_line.type == SIP_REQUEST) {
1810
1811                 if(parse_sip_msg_uri(msg) < 0)
1812                         return -1;
1813
1814                 sco.method = msg->first_line.u.request.method;
1815                 EMPTY_STR(sco.reply_reason);
1816
1817                 sco.ruri = msg->first_line.u.request.uri;
1818                 sco.ruri_user = msg->parsed_uri.user;
1819                 sco.ruri_domain = msg->parsed_uri.host;
1820         } else if(msg->first_line.type == SIP_REPLY) {
1821                 sco.method = msg->first_line.u.reply.status;
1822                 sco.reply_reason = msg->first_line.u.reply.reason;
1823
1824                 EMPTY_STR(sco.ruri);
1825                 EMPTY_STR(sco.ruri_user);
1826                 EMPTY_STR(sco.ruri_domain);
1827         } else {
1828                 LM_ERR("unknown type [%i]\n", msg->first_line.type);
1829                 EMPTY_STR(sco.method);
1830                 EMPTY_STR(sco.reply_reason);
1831                 EMPTY_STR(sco.ruri);
1832                 EMPTY_STR(sco.ruri_user);
1833         }
1834
1835         if(heptime && heptime->tv_sec != 0) {
1836                 sco.tmstamp = (unsigned long long)heptime->tv_sec * 1000000
1837                         + heptime->tv_usec; /* micro ts */
1838                 snprintf(tmp_node, 100, "%.*s:%lu", capture_node.len, capture_node.s,
1839                                 (unsigned long)heptime->captid);
1840                 sco.node.s = tmp_node;
1841                 sco.node.len = strlen(tmp_node);
1842         } else {
1843                 sco.tmstamp = (unsigned long long)tvb.tv_sec * 1000000
1844                         + tvb.tv_usec; /* micro ts */
1845                 sco.node = capture_node;
1846         }
1847
1848         /* Parse FROM */
1849         if(msg->from) {
1850
1851                 if(parse_from_header(msg) != 0) {
1852                         LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1853                                         " From: header\n");
1854                         return -1;
1855                 }
1856
1857                 if(parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len, &from) < 0) {
1858                         LOG(L_ERR, "ERROR: do_action: bad from dropping"
1859                                         " packet\n");
1860                         return -1;
1861                 }
1862
1863                 sco.from_user = from.user;
1864                 sco.from_domain = from.host;
1865                 sco.from_tag = get_from(msg)->tag_value;
1866         } else {
1867                 EMPTY_STR(sco.from_user);
1868                 EMPTY_STR(sco.from_domain);
1869                 EMPTY_STR(sco.from_tag);
1870         }
1871
1872         /* Parse TO */
1873         if(msg->to) {
1874
1875                 if(parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len, &to) < 0) {
1876                         LOG(L_ERR, "ERROR: do_action: bad to dropping"
1877                                         " packet\n");
1878                         return -1;
1879                 }
1880
1881                 sco.to_user = to.user;
1882                 sco.to_domain = to.host;
1883                 if(get_to(msg)->tag_value.len)
1884                         sco.to_tag = get_to(msg)->tag_value;
1885                 else {
1886                         EMPTY_STR(sco.to_tag);
1887                 }
1888         } else {
1889                 EMPTY_STR(sco.to_user);
1890                 EMPTY_STR(sco.to_domain);
1891                 EMPTY_STR(sco.to_tag);
1892         }
1893
1894         /* Call-id */
1895         if(msg->callid)
1896                 sco.callid = msg->callid->body;
1897         else {
1898                 EMPTY_STR(sco.callid);
1899         }
1900
1901         /* P-Asserted-Id */
1902         if((parse_pai_header(msg) == 0) && (msg->pai) && (msg->pai->parsed)) {
1903                 to_body_t *pai = get_pai(msg)->id; /* This returns the first entry */
1904                 if((pai->parsed_uri.user.s == NULL)
1905                                 && (parse_uri(pai->uri.s, pai->uri.len, &pai->parsed_uri)
1906                                         < 0)) {
1907                         LM_DBG("DEBUG: do_action: bad pai: method:[%.*s] CID: [%.*s]\n",
1908                                         sco.method.len, sco.method.s, sco.callid.len, sco.callid.s);
1909                 } else {
1910                         LM_DBG("PARSE PAI: (%.*s)\n", pai->uri.len, pai->uri.s);
1911                         sco.pid_user = pai->parsed_uri.user;
1912                 }
1913         } else if((parse_ppi_header(msg) == 0) && (msg->ppi)
1914                         && (msg->ppi->parsed)) {
1915                 to_body_t *ppi = get_ppi(msg)->id; /* This returns the first entry */
1916                 if((ppi->parsed_uri.user.s == NULL)
1917                                 && (parse_uri(ppi->uri.s, ppi->uri.len, &ppi->parsed_uri)
1918                                         < 0)) {
1919                         LM_DBG("DEBUG: do_action: bad ppi: method:[%.*s] CID: [%.*s]\n",
1920                                         sco.method.len, sco.method.s, sco.callid.len, sco.callid.s);
1921                 } else {
1922                         LM_DBG("PARSE PPI: (%.*s)\n", ppi->uri.len, ppi->uri.s);
1923                         sco.pid_user = ppi->parsed_uri.user;
1924                 }
1925         } else if((parse_rpid_header(msg) == 0) && (msg->rpid) && (msg->rpid->parsed)) {
1926                 to_body_t *rpid = get_rpid(msg);
1927                 if((rpid->parsed_uri.user.s == NULL)
1928                                 && (parse_uri(rpid->uri.s, rpid->uri.len, &rpid->parsed_uri) < 0)) {
1929                         LM_DBG("DEBUG: do_action: bad rpid: method:[%.*s] CID: [%.*s]\n",
1930                                         sco.method.len, sco.method.s, sco.callid.len, sco.callid.s);
1931                 } else {
1932                         LM_DBG("PARSE RPID: (%.*s)\n",rpid->uri.len, rpid->uri.s);
1933                         sco.pid_user = rpid->parsed_uri.user;
1934                 }
1935         } else {
1936                 EMPTY_STR(sco.pid_user);
1937         }
1938
1939         /* Auth headers */
1940         if(msg->proxy_auth != NULL)
1941                 hook1 = msg->proxy_auth;
1942         else if(msg->authorization != NULL)
1943                 hook1 = msg->authorization;
1944
1945         if(hook1) {
1946                 if(parse_credentials(hook1) == 0)
1947                         sco.auth_user =
1948                                 ((auth_body_t *)(hook1->parsed))->digest.username.user;
1949                 else {
1950                         EMPTY_STR(sco.auth_user);
1951                 }
1952         } else {
1953                 EMPTY_STR(sco.auth_user);
1954         }
1955
1956         if(msg->contact) {
1957
1958                 if(msg->contact->parsed == 0 && parse_contact(msg->contact) == -1) {
1959                         LOG(L_ERR, "assemble_msg: error while parsing <Contact:> header\n");
1960                         return -1;
1961                 }
1962
1963                 cb = (contact_body_t *)msg->contact->parsed;
1964
1965                 if(cb) {
1966                         if(cb->contacts) {
1967                                 if(parse_uri(
1968                                                         cb->contacts->uri.s, cb->contacts->uri.len, &contact)
1969                                                 < 0) {
1970                                         LOG(L_ERR, "ERROR: do_action: bad contact dropping"
1971                                                         " packet\n");
1972                                         return -1;
1973                                 }
1974                         } else {
1975                                 if(cb->star) { /* in the case Contact is "*" */
1976                                         memset(&contact, 0, sizeof(contact));
1977                                         contact.user.s = star_contact.s;
1978                                         contact.user.len = star_contact.len;
1979                                 } else {
1980                                         LOG(L_NOTICE, "Invalid contact\n");
1981                                         memset(&contact, 0, sizeof(contact));
1982                                 }
1983                         }
1984                 }
1985         }
1986
1987         /* callid_aleg - default is X-CID but configurable via modul params */
1988         EMPTY_STR(sco.callid_aleg);
1989         int index;
1990         for(index = 0; index < n_callid_aleg_headers; index++) {
1991                 if((tmphdr[0] = get_hdr_by_name(msg, callid_aleg_headers[index].s,
1992                                                 callid_aleg_headers[index].len))
1993                                 != NULL) {
1994                         LM_DBG("MATCHED header %.*s\n", callid_aleg_headers[index].len,
1995                                         callid_aleg_headers[index].s);
1996                         sco.callid_aleg = tmphdr[0]->body;
1997                         break;
1998                 }
1999         }
2000
2001         /* VIA 1 */
2002         sco.via_1 = msg->h_via1->body;
2003
2004         /* Via branch */
2005         if(msg->via1->branch)
2006                 sco.via_1_branch = msg->via1->branch->value;
2007         else {
2008                 EMPTY_STR(sco.via_1_branch);
2009         }
2010
2011         /* CSEQ */
2012         if(msg->cseq)
2013                 sco.cseq = msg->cseq->body;
2014         else {
2015                 EMPTY_STR(sco.cseq);
2016         }
2017
2018         /* Reason */
2019         if((tmphdr[1] = get_hdr_by_name(msg, "Reason", 6)) != NULL) {
2020                 sco.reason = tmphdr[1]->body;
2021         } else {
2022                 EMPTY_STR(sco.reason);
2023         }
2024
2025         /* Diversion */
2026         if(msg->diversion)
2027                 sco.diversion = msg->diversion->body;
2028         else {
2029                 EMPTY_STR(sco.diversion);
2030         }
2031
2032         /* Content-type */
2033         if(msg->content_type)
2034                 sco.content_type = msg->content_type->body;
2035         else {
2036                 EMPTY_STR(sco.content_type);
2037         }
2038
2039         /* User-Agent */
2040         if(msg->user_agent)
2041                 sco.user_agent = msg->user_agent->body;
2042         else {
2043                 EMPTY_STR(sco.user_agent);
2044         }
2045
2046         /* Contact */
2047         if(msg->contact && cb) {
2048                 sco.contact_ip = contact.host;
2049                 str2int(&contact.port, (unsigned int *)&sco.contact_port);
2050                 sco.contact_user = contact.user;
2051         } else {
2052                 EMPTY_STR(sco.contact_ip);
2053                 sco.contact_port = 0;
2054                 EMPTY_STR(sco.contact_user);
2055         }
2056
2057         /* X-OIP */
2058         if((tmphdr[2] = get_hdr_by_name(msg, "X-OIP", 5)) != NULL) {
2059                 sco.originator_ip = tmphdr[2]->body;
2060                 /* Originator port. Should be parsed from XOIP header as ":" param */
2061                 tmp = strchr(tmphdr[2]->body.s, ':');
2062                 if(tmp) {
2063                         *tmp = '\0';
2064                         port_str = tmp + 1;
2065                         sco.originator_port = strtol(port_str, NULL, 10);
2066                 } else
2067                         sco.originator_port = 0;
2068         } else {
2069                 EMPTY_STR(sco.originator_ip);
2070                 sco.originator_port = 0;
2071         }
2072
2073         /* X-RTP-Stat-Add */
2074         if((tmphdr[3] = get_hdr_by_name(msg, "X-RTP-Stat-Add", 14)) != NULL) {
2075                 sco.rtp_stat = tmphdr[3]->body;
2076         }
2077         /* X-RTP-Stat-T38 */
2078         else if((tmphdr[3] = get_hdr_by_name(msg, "X-RTP-Stat-T38", 14)) != NULL) {
2079                 sco.rtp_stat = tmphdr[3]->body;
2080         }
2081         /* X-RTP-Stat */
2082         else if((tmphdr[3] = get_hdr_by_name(msg, "X-RTP-Stat", 10)) != NULL) {
2083                 sco.rtp_stat = tmphdr[3]->body;
2084         }
2085         /* P-RTP-Stat */
2086         else if((tmphdr[3] = get_hdr_by_name(msg, "P-RTP-Stat", 10)) != NULL) {
2087                 sco.rtp_stat = tmphdr[3]->body;
2088         }
2089         /* X-Siemens-RTP-stats */
2090         else if((tmphdr[3] = get_hdr_by_name(msg, "X-Siemens-RTP-stats", 19))
2091                         != NULL) {
2092                 sco.rtp_stat = tmphdr[3]->body;
2093         }
2094         /* X-NG-RTP-STATS */
2095         else if((tmphdr[3] = get_hdr_by_name(msg, "X-NG-RTP-STATS", 14)) != NULL) {
2096                 sco.rtp_stat = tmphdr[3]->body;
2097         }
2098         /* RTP-RxStat */
2099         else if((tmphdr[3] = get_hdr_by_name(msg, "RTP-RxStat", 10)) != NULL) {
2100                 sco.rtp_stat = tmphdr[3]->body;
2101         } else {
2102                 EMPTY_STR(sco.rtp_stat);
2103         }
2104
2105         /* Custom - field1 */
2106         if(custom_field1_header.len > 0
2107                         && (tmphdr[4] = get_hdr_by_name(
2108                                         msg, custom_field1_header.s, custom_field1_header.len))
2109                         != NULL) {
2110                 sco.custom1 = tmphdr[4]->body;
2111         } else {
2112                 EMPTY_STR(sco.custom1);
2113         }
2114
2115         /* Custom - field2 */
2116         if(custom_field2_header.len > 0
2117                         && (tmphdr[5] = get_hdr_by_name(
2118                                         msg, custom_field2_header.s, custom_field2_header.len))
2119                         != NULL) {
2120                 sco.custom2 = tmphdr[5]->body;
2121         } else {
2122                 EMPTY_STR(sco.custom2);
2123         }
2124
2125         /* Custom - field3 */
2126         if(custom_field3_header.len > 0
2127                         && (tmphdr[6] = get_hdr_by_name(
2128                                         msg, custom_field3_header.s, custom_field3_header.len))
2129                         != NULL) {
2130                 sco.custom3 = tmphdr[6]->body;
2131         } else {
2132                 EMPTY_STR(sco.custom3);
2133         }
2134
2135         /* PROTO TYPE */
2136         sco.proto = msg->rcv.proto;
2137
2138         /* FAMILY TYPE */
2139         sco.family = msg->rcv.src_ip.af;
2140
2141         /* MESSAGE TYPE */
2142         sco.type = msg->first_line.type;
2143
2144         /* MSG */
2145         sco.msg.s = msg->buf;
2146         sco.msg.len = msg->len;
2147         //EMPTY_STR(sco.msg);
2148
2149         /* IP source and destination */
2150
2151         strcpy(buf_ip, ip_addr2a(&msg->rcv.src_ip));
2152         sco.source_ip.s = buf_ip;
2153         sco.source_ip.len = strlen(buf_ip);
2154         sco.source_port = msg->rcv.src_port;
2155
2156         /*source ip*/
2157         sco.destination_ip.s = ip_addr2a(&msg->rcv.dst_ip);
2158         sco.destination_ip.len = strlen(sco.destination_ip.s);
2159         sco.destination_port = msg->rcv.dst_port;
2160
2161
2162         LM_DBG("src_ip: [%.*s]\n", sco.source_ip.len, sco.source_ip.s);
2163         LM_DBG("dst_ip: [%.*s]\n", sco.destination_ip.len, sco.destination_ip.s);
2164
2165         LM_DBG("dst_port: [%d]\n", sco.destination_port);
2166         LM_DBG("src_port: [%d]\n", sco.source_port);
2167
2168 #ifdef STATISTICS
2169         if(msg->first_line.type == SIP_REPLY) {
2170                 sco.stat = (cm_data) ? cm_data->sipcapture_rpl
2171                         : capture_def->sipcapture_rpl;
2172         } else {
2173                 sco.stat = (cm_data) ? cm_data->sipcapture_req
2174                         : capture_def->sipcapture_req;
2175         }
2176 #endif
2177         //LM_DBG("DONE");
2178         return sip_capture_store(&sco, _table, cm_data);
2179 }
2180
2181 #define capture_is_off(_msg) (capture_on_flag == NULL || *capture_on_flag == 0)
2182
2183
2184 /* Local raw socket */
2185 int raw_capture_socket(
2186                 struct ip_addr *ip, str *iface, int port_start, int port_end, int proto)
2187 {
2188
2189         int sock = -1;
2190         union sockaddr_union su;
2191
2192 #ifdef __OS_linux
2193         struct sock_fprog pf;
2194         char short_ifname[sizeof(int)];
2195         int ifname_len;
2196         char *ifname;
2197 #endif
2198         //0x0003 - all packets
2199         if(proto == IPPROTO_IPIP) {
2200                 sock = socket(PF_INET, SOCK_RAW, proto);
2201         }
2202 #ifdef __OS_linux
2203         else if(proto == htons(0x800)) {
2204                 sock = socket(PF_PACKET, SOCK_RAW, proto);
2205         }
2206 #endif
2207         else {
2208                 ERR("raw_capture_socket: LSF currently supported only on linux\n");
2209                 goto error;
2210         }
2211
2212         if(sock == -1)
2213                 goto error;
2214
2215 #ifdef __OS_linux
2216
2217         /* set socket options */
2218         if(iface && iface->s) {
2219
2220                 /* workaround for linux bug: arg to setsockopt must have at least
2221                  * sizeof(int) size or EINVAL would be returned */
2222                 if(iface->len < sizeof(int)) {
2223                         memcpy(short_ifname, iface->s, iface->len);
2224                         short_ifname[iface->len] = 0; /* make sure it's zero term */
2225                         ifname_len = sizeof(short_ifname);
2226                         ifname = short_ifname;
2227                 } else {
2228                         ifname_len = iface->len;
2229                         ifname = iface->s;
2230                 }
2231                 if(setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, ifname_len)
2232                                 < 0) {
2233                         ERR("raw_socket: could not bind to %.*s: %s [%d]\n", iface->len,
2234                                         ZSW(iface->s), strerror(errno), errno);
2235                         goto error;
2236                 }
2237         }
2238
2239         if(bpf_on) {
2240
2241                 memset(&pf, 0, sizeof(pf));
2242                 pf.len = sizeof(BPF_code) / sizeof(BPF_code[0]);
2243                 pf.filter = (struct sock_filter *)BPF_code;
2244
2245                 if(!port_end)
2246                         port_end = port_start;
2247
2248                 /* Start PORT */
2249                 BPF_code[5] = (struct sock_filter)BPF_JUMP(0x35, port_start, 0, 1);
2250                 BPF_code[8] = (struct sock_filter)BPF_JUMP(0x35, port_start, 11, 13);
2251                 BPF_code[16] = (struct sock_filter)BPF_JUMP(0x35, port_start, 0, 1);
2252                 BPF_code[19] = (struct sock_filter)BPF_JUMP(0x35, port_start, 0, 2);
2253                 /* Stop PORT */
2254                 BPF_code[6] = (struct sock_filter)BPF_JUMP(0x25, port_end, 0, 14);
2255                 BPF_code[17] = (struct sock_filter)BPF_JUMP(0x25, port_end, 0, 3);
2256                 BPF_code[20] = (struct sock_filter)BPF_JUMP(0x25, port_end, 1, 0);
2257
2258                 /* Attach the filter to the socket */
2259                 if(setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &pf, sizeof(pf))
2260                                 < 0) {
2261                         ERR(" setsockopt filter: [%s] [%d]\n", strerror(errno), errno);
2262                 }
2263         }
2264 #endif
2265
2266         if(ip && proto == IPPROTO_IPIP) {
2267                 init_su(&su, ip, 0);
2268                 if(bind(sock, &su.s, sockaddru_len(su)) == -1) {
2269                         ERR("raw_capture_socket: bind(%s) failed: %s [%d]\n", ip_addr2a(ip),
2270                                         strerror(errno), errno);
2271                         goto error;
2272                 }
2273         }
2274
2275         return sock;
2276
2277 error:
2278         if(sock != -1)
2279                 close(sock);
2280         return -1;
2281 }
2282
2283 /* Local raw receive loop */
2284 int raw_capture_rcv_loop(int rsock, int port1, int port2, int ipip)
2285 {
2286
2287
2288         static char buf[BUF_SIZE + 1];
2289         union sockaddr_union from;
2290         union sockaddr_union to;
2291         struct receive_info ri;
2292         int len;
2293         struct ip *iph;
2294         struct udphdr *udph;
2295         char *udph_start;
2296         unsigned short udp_len;
2297         int offset = 0;
2298         char *end;
2299         unsigned short dst_port;
2300         unsigned short src_port;
2301         struct ip_addr dst_ip, src_ip;
2302         struct socket_info *si = 0;
2303         int tmp_len;
2304
2305
2306         for(;;) {
2307
2308                 len = recvfrom(rsock, buf, BUF_SIZE, 0x20, 0, 0);
2309
2310                 if(len < 0) {
2311                         if(len == -1) {
2312                                 LOG(L_ERR, "ERROR: raw_moni_rcv_loop:recvfrom: %s [%d]\n",
2313                                                 strerror(errno), errno);
2314                                 if((errno == EINTR) || (errno == EWOULDBLOCK))
2315                                         continue;
2316                         } else {
2317                                 DBG("raw_moni_rcv_loop: recvfrom error: %d\n", len);
2318                                 continue;
2319                         }
2320                 }
2321
2322                 end = buf + len;
2323
2324                 offset = ipip ? sizeof(struct ip) : ETHHDR;
2325
2326                 if(unlikely(len
2327                                         < (sizeof(struct ip) + sizeof(struct udphdr) + offset))) {
2328                         DBG("received small packet: %d. Ignore it\n", len);
2329                         continue;
2330                 }
2331
2332                 iph = (struct ip *)(buf + offset);
2333
2334                 offset += iph->ip_hl * 4;
2335
2336                 udph_start = buf + offset;
2337
2338                 udph = (struct udphdr *)udph_start;
2339                 offset += sizeof(struct udphdr);
2340
2341                 if(unlikely((buf + offset) > end)) {
2342                         continue;
2343                 }
2344
2345                 udp_len = ntohs(udph->uh_ulen);
2346                 if(unlikely((udph_start + udp_len) != end)) {
2347                         if((udph_start + udp_len) > end) {
2348                                 continue;
2349                         } else {
2350                                 DBG("udp length too small: %d/%d\n", (int)udp_len,
2351                                                 (int)(end - udph_start));
2352                                 continue;
2353                         }
2354                 }
2355
2356                 /* cut off the offset */
2357                 len -= offset;
2358
2359                 if(len < MIN_UDP_PACKET) {
2360                         DBG("raw_udp4_rcv_loop: probing packet received from\n");
2361                         continue;
2362                 }
2363
2364                 /* fill dst_port && src_port */
2365                 dst_port = ntohs(udph->uh_dport);
2366                 src_port = ntohs(udph->uh_sport);
2367
2368                 /* if the message has not alpha */
2369                 if(!isalnum((buf + offset)[0])) {
2370                         DBG("not alpha and not digit... skipping...\n");
2371                         continue;
2372                 }
2373
2374
2375                 DBG("PORT: [%d] and [%d]\n", port1, port2);
2376
2377                 if((!port1 && !port2) || (src_port >= port1 && src_port <= port2)
2378                                 || (dst_port >= port1 && dst_port <= port2)
2379                                 || (!port2 && (src_port == port1 || dst_port == port1))) {
2380
2381                         /*FIL IPs*/
2382                         dst_ip.af = AF_INET;
2383                         dst_ip.len = 4;
2384                         dst_ip.u.addr32[0] = iph->ip_dst.s_addr;
2385
2386                         /* fill dst_port */
2387                         ip_addr2su(&to, &dst_ip, dst_port);
2388
2389                         /* fill src_port */
2390                         src_ip.af = AF_INET;
2391                         src_ip.len = 4;
2392                         src_ip.u.addr32[0] = iph->ip_src.s_addr;
2393                         ip_addr2su(&from, &src_ip, src_port);
2394                         su_setport(&from, src_port);
2395
2396                         ri.src_su = from;
2397                         su2ip_addr(&ri.src_ip, &from);
2398                         ri.src_port = src_port;
2399                         su2ip_addr(&ri.dst_ip, &to);
2400                         ri.dst_port = dst_port;
2401                         ri.proto = PROTO_UDP;
2402
2403                         /* a little bit memory */
2404                         si = (struct socket_info *)pkg_malloc(sizeof(struct socket_info));
2405                         if(si == 0) {
2406                                 LOG(L_ERR, "ERROR: new_sock_info: memory allocation error\n");
2407                                 return 0;
2408                         }
2409
2410                         memset(si, 0, sizeof(struct socket_info));
2411                         si->address = ri.dst_ip;
2412                         si->socket = -1;
2413
2414                         /* set port & proto */
2415                         si->port_no = dst_port;
2416                         si->proto = PROTO_UDP;
2417                         si->flags = 0;
2418                         si->addr_info_lst = 0;
2419
2420                         si->port_no_str.s = int2str(si->port_no, &tmp_len);
2421                         si->port_no_str.len = tmp_len;
2422
2423                         si->address_str.s = ip_addr2a(&si->address);
2424                         ;
2425                         si->address_str.len = strlen(si->address_str.s);
2426
2427                         si->name.len = si->address_str.len;
2428                         si->name.s = si->address_str.s;
2429
2430                         ri.bind_address = si;
2431
2432
2433                         /* and now recieve message */
2434                         receive_msg(buf + offset, len, &ri);
2435                         if(si)
2436                                 pkg_free(si);
2437                 }
2438         }
2439
2440         return 0;
2441 }
2442
2443 static void sipcapture_rpc_status(rpc_t *rpc, void *c)
2444 {
2445         str status = {0, 0};
2446
2447         if(rpc->scan(c, "S", &status) < 1) {
2448                 rpc->fault(c, 500, "Not enough parameters (on, off or check)");
2449                 return;
2450         }
2451
2452         if(capture_on_flag == NULL) {
2453                 rpc->fault(c, 500, "Internal error");
2454                 return;
2455         }
2456
2457         if(strncasecmp(status.s, "on", strlen("on")) == 0) {
2458                 *capture_on_flag = 1;
2459                 rpc->rpl_printf(c, "Enabled");
2460                 return;
2461         }
2462         if(strncasecmp(status.s, "off", strlen("off")) == 0) {
2463                 *capture_on_flag = 0;
2464                 rpc->rpl_printf(c, "Disabled");
2465                 return;
2466         }
2467         if(strncasecmp(status.s, "check", strlen("check")) == 0) {
2468                 rpc->rpl_printf(c, *capture_on_flag ? "Enabled" : "Disabled");
2469                 return;
2470         }
2471         rpc->fault(c, 500, "Bad parameter (on, off or check)");
2472         return;
2473 }
2474
2475 static const char *sipcapture_status_doc[2] = {
2476         "Get status or turn on/off sipcapture.", 0};
2477
2478 rpc_export_t sipcapture_rpc[] = {
2479         {"sipcapture.status", sipcapture_rpc_status, sipcapture_status_doc, 0},
2480         {0, 0, 0, 0}
2481 };
2482
2483 static int sipcapture_init_rpc(void)
2484 {
2485         if(rpc_register_array(sipcapture_rpc) != 0) {
2486                 LM_ERR("failed to register RPC commands\n");
2487                 return -1;
2488         }
2489         return 0;
2490 }
2491
2492
2493 /* for rtcp and logging */
2494 int receive_logging_json_msg(char *buf, unsigned int len,
2495                 struct hep_generic_recv *hg, char *log_table)
2496 {
2497
2498
2499         db_key_t db_keys[RTCP_NR_KEYS];
2500         db_val_t db_vals[RTCP_NR_KEYS];
2501         struct _sipcapture_object sco;
2502         char ipstr_dst[INET6_ADDRSTRLEN], ipstr_src[INET6_ADDRSTRLEN];
2503         char tmp_node[100];
2504         struct timeval tvb;
2505         struct timezone tz;
2506         time_t epoch_time_as_time_t;
2507
2508         str tmp, corrtmp, table;
2509         _capture_mode_data_t *c = NULL;
2510
2511         c = capture_def;
2512         if(!c) {
2513                 LM_ERR("no connection mode available to store data\n");
2514                 return -1;
2515         }
2516
2517         if(!correlation_id || strlen(correlation_id) == 0) {
2518                 LM_ERR("no correlation id defined\n");
2519                 return -1;
2520         }
2521
2522         memset(&sco, 0, sizeof(struct _sipcapture_object));
2523         gettimeofday(&tvb, &tz);
2524
2525         /* PROTO TYPE */
2526         if(hg->ip_proto->data == IPPROTO_TCP)
2527                 sco.proto = PROTO_TCP;
2528         else if(hg->ip_proto->data == IPPROTO_UDP)
2529                 sco.proto = PROTO_UDP;
2530         /* FAMILY TYPE */
2531         sco.family = hg->ip_family->data;
2532
2533         /* IP source and destination */
2534
2535         if(hg->ip_family->data == AF_INET6) {
2536                 inet_ntop(AF_INET6, &(hg->hep_dst_ip6->data), ipstr_dst,
2537                                 INET6_ADDRSTRLEN);
2538                 inet_ntop(AF_INET6, &(hg->hep_src_ip6->data), ipstr_src,
2539                                 INET6_ADDRSTRLEN);
2540         } else if(hg->ip_family->data == AF_INET) {
2541                 inet_ntop(
2542                                 AF_INET, &(hg->hep_src_ip4->data), ipstr_src, INET_ADDRSTRLEN);
2543                 inet_ntop(
2544                                 AF_INET, &(hg->hep_dst_ip4->data), ipstr_dst, INET_ADDRSTRLEN);
2545         }
2546
2547
2548         /* type of proto */
2549         if(hg->proto_t->data == 5)
2550                 sco.type = 3;
2551         else if(hg->proto_t->data == 32)
2552                 sco.type = 2;
2553         else if(hg->proto_t->data == 99)
2554                 sco.type = 4;
2555         else if(hg->proto_t->data == 100)
2556                 sco.type = 5;
2557
2558
2559         /*source ip*/
2560         sco.source_ip.s = ipstr_src;
2561         sco.source_ip.len = strlen(ipstr_src);
2562         sco.source_port = ntohs(hg->src_port->data);
2563
2564         sco.destination_ip.s = ipstr_dst;
2565         sco.destination_ip.len = strlen(ipstr_dst);
2566         sco.destination_port = ntohs(hg->dst_port->data);
2567
2568         if(heptime && heptime->tv_sec != 0) {
2569                 sco.tmstamp = (unsigned long long)heptime->tv_sec * 1000000
2570                         + heptime->tv_usec; /* micro ts */
2571                 snprintf(tmp_node, 100, "%.*s:%lu", capture_node.len, capture_node.s,
2572                                 (unsigned long)heptime->captid);
2573                 sco.node.s = tmp_node;
2574                 sco.node.len = strlen(tmp_node);
2575                 epoch_time_as_time_t = heptime->tv_sec;
2576                 ;
2577         } else {
2578                 sco.tmstamp = (unsigned long long)tvb.tv_sec * 1000000
2579                         + tvb.tv_usec; /* micro ts */
2580                 sco.node = capture_node;
2581                 epoch_time_as_time_t = tvb.tv_sec;
2582         }
2583
2584         if(correlation_id) {
2585                 corrtmp.s = correlation_id;
2586                 corrtmp.len = strlen(correlation_id);
2587         }
2588
2589         db_keys[0] = &date_column;
2590         db_vals[0].type = DB1_DATETIME;
2591         db_vals[0].nul = 0;
2592         db_vals[0].val.time_val = epoch_time_as_time_t;
2593
2594         db_keys[1] = &micro_ts_column;
2595         db_vals[1].type = DB1_BIGINT;
2596         db_vals[1].nul = 0;
2597         db_vals[1].val.ll_val = sco.tmstamp;
2598
2599         db_keys[2] = &correlation_column;
2600         db_vals[2].type = DB1_STR;
2601         db_vals[2].nul = 0;
2602         db_vals[2].val.str_val = corrtmp;
2603
2604         db_keys[3] = &source_ip_column;
2605         db_vals[3].type = DB1_STR;
2606         db_vals[3].nul = 0;
2607         db_vals[3].val.str_val = sco.source_ip;
2608
2609         db_keys[4] = &source_port_column;
2610         db_vals[4].type = DB1_INT;
2611         db_vals[4].nul = 0;
2612         db_vals[4].val.int_val = sco.source_port;
2613
2614         db_keys[5] = &dest_ip_column;
2615         db_vals[5].type = DB1_STR;
2616         db_vals[5].nul = 0;
2617         db_vals[5].val.str_val = sco.destination_ip;
2618
2619         db_keys[6] = &dest_port_column;
2620         db_vals[6].type = DB1_INT;
2621         db_vals[6].nul = 0;
2622         db_vals[6].val.int_val = sco.destination_port;
2623
2624         db_keys[7] = &proto_column;
2625         db_vals[7].type = DB1_INT;
2626         db_vals[7].nul = 0;
2627         db_vals[7].val.int_val = sco.proto;
2628
2629         db_keys[8] = &family_column;
2630         db_vals[8].type = DB1_INT;
2631         db_vals[8].nul = 0;
2632         db_vals[8].val.int_val = sco.family;
2633
2634         db_keys[9] = &type_column;
2635         db_vals[9].type = DB1_INT;
2636         db_vals[9].nul = 0;
2637         db_vals[9].val.int_val = sco.type;
2638
2639         db_keys[10] = &node_column;
2640         db_vals[10].type = DB1_STR;
2641         db_vals[10].nul = 0;
2642         db_vals[10].val.str_val = sco.node;
2643
2644         db_keys[11] = &msg_column;
2645         db_vals[11].type = DB1_BLOB;
2646         db_vals[11].nul = 0;
2647
2648         tmp.s = buf;
2649         tmp.len = len;
2650
2651         db_vals[11].val.blob_val = tmp;
2652
2653         table.s = log_table;
2654         table.len = strlen(log_table);
2655
2656         c->db_funcs.use_table(c->db_con, &table);
2657
2658         if(db_insert_mode == 1 && c->db_funcs.insert_delayed != NULL) {
2659                 if(c->db_funcs.insert_delayed(c->db_con, db_keys, db_vals, RTCP_NR_KEYS)
2660                                 < 0) {
2661                         LM_ERR("failed to insert delayed into database\n");
2662                         goto error;
2663                 }
2664         } else if(db_insert_mode == 2 && c->db_funcs.insert_async != NULL) {
2665                 if(c->db_funcs.insert_async(c->db_con, db_keys, db_vals, RTCP_NR_KEYS)
2666                                 < 0) {
2667                         LM_ERR("failed to insert async into database\n");
2668                         goto error;
2669                 }
2670         } else if(c->db_funcs.insert(c->db_con, db_keys, db_vals, RTCP_NR_KEYS)
2671                         < 0) {
2672                 LM_ERR("failed to insert into database\n");
2673                 goto error;
2674         }
2675
2676
2677         return 1;
2678 error:
2679         return -1;
2680 }
2681
2682 static int report_capture(sip_msg_t *msg, str *_table, str *_corr, str *_data)
2683 {
2684         struct _sipcapture_object sco;
2685         db_key_t db_keys[RTCP_NR_KEYS];
2686         db_val_t db_vals[RTCP_NR_KEYS];
2687         char buf_ip[IP_ADDR_MAX_STR_SIZE + 12];
2688         struct timeval tvb;
2689         struct timezone tz;
2690         char tmp_node[100];
2691         time_t epoch_time_as_time_t;
2692         str corrtmp = STR_NULL, tmp;
2693
2694
2695         _capture_mode_data_t *c = NULL;
2696         c = capture_def;
2697         if(!c) {
2698                 LM_ERR("no connection mode available to store data\n");
2699                 return -1;
2700         }
2701
2702
2703         LM_DBG("CAPTURE DEBUG...\n");
2704
2705         gettimeofday(&tvb, &tz);
2706
2707         if(msg == NULL) {
2708                 LM_DBG("nothing to capture\n");
2709                 return -1;
2710         }
2711         memset(&sco, 0, sizeof(struct _sipcapture_object));
2712
2713         if(capture_on_flag == NULL || *capture_on_flag == 0) {
2714                 LM_DBG("capture off...\n");
2715                 return -1;
2716         }
2717
2718         sco.proto = msg->rcv.proto;
2719
2720         /* FAMILY TYPE */
2721         sco.family = msg->rcv.src_ip.af;
2722
2723         /* MESSAGE TYPE */
2724         sco.type = msg->first_line.type;
2725
2726         /* MSG */
2727         sco.msg.s = msg->buf;
2728         sco.msg.len = msg->len;
2729         //EMPTY_STR(sco.msg);
2730
2731         /* IP source and destination */
2732
2733         strcpy(buf_ip, ip_addr2a(&msg->rcv.src_ip));
2734         sco.source_ip.s = buf_ip;
2735         sco.source_ip.len = strlen(buf_ip);
2736         sco.source_port = msg->rcv.src_port;
2737
2738         /*source ip*/
2739         sco.destination_ip.s = ip_addr2a(&msg->rcv.dst_ip);
2740         sco.destination_ip.len = strlen(sco.destination_ip.s);
2741         sco.destination_port = msg->rcv.dst_port;
2742
2743
2744         if(heptime && heptime->tv_sec != 0) {
2745                 sco.tmstamp = (unsigned long long)heptime->tv_sec * 1000000
2746                         + heptime->tv_usec; /* micro ts */
2747                 snprintf(tmp_node, 100, "%.*s:%lu", capture_node.len, capture_node.s,
2748                                 (unsigned long)heptime->captid);
2749                 sco.node.s = tmp_node;
2750                 sco.node.len = strlen(tmp_node);
2751                 epoch_time_as_time_t = heptime->tv_sec;
2752         } else {
2753                 sco.tmstamp = (unsigned long long)tvb.tv_sec * 1000000
2754                         + tvb.tv_usec; /* micro ts */
2755                 sco.node = capture_node;
2756                 epoch_time_as_time_t = tvb.tv_sec;
2757         }
2758
2759         if(_corr && _corr->len > 0) {
2760                 corrtmp.s = _corr->s;
2761                 corrtmp.len = _corr->len;
2762         } else if(correlation_id) {
2763                 corrtmp.s = correlation_id;
2764                 corrtmp.len = strlen(correlation_id);
2765         }
2766
2767         db_keys[0] = &date_column;
2768         db_vals[0].type = DB1_DATETIME;
2769         db_vals[0].nul = 0;
2770         db_vals[0].val.time_val = epoch_time_as_time_t;
2771
2772         db_keys[1] = &micro_ts_column;
2773         db_vals[1].type = DB1_BIGINT;
2774         db_vals[1].nul = 0;
2775         db_vals[1].val.ll_val = sco.tmstamp;
2776
2777         db_keys[2] = &correlation_column;
2778         db_vals[2].type = DB1_STR;
2779         db_vals[2].nul = 0;
2780         db_vals[2].val.str_val = corrtmp;
2781
2782         db_keys[3] = &source_ip_column;
2783         db_vals[3].type = DB1_STR;
2784         db_vals[3].nul = 0;
2785         db_vals[3].val.str_val = sco.source_ip;
2786
2787         db_keys[4] = &source_port_column;
2788         db_vals[4].type = DB1_INT;
2789         db_vals[4].nul = 0;
2790         db_vals[4].val.int_val = sco.source_port;
2791
2792         db_keys[5] = &dest_ip_column;
2793         db_vals[5].type = DB1_STR;
2794         db_vals[5].nul = 0;
2795         db_vals[5].val.str_val = sco.destination_ip;
2796
2797         db_keys[6] = &dest_port_column;
2798         db_vals[6].type = DB1_INT;
2799         db_vals[6].nul = 0;
2800         db_vals[6].val.int_val = sco.destination_port;
2801
2802         db_keys[7] = &proto_column;
2803         db_vals[7].type = DB1_INT;
2804         db_vals[7].nul = 0;
2805         db_vals[7].val.int_val = sco.proto;
2806
2807         db_keys[8] = &family_column;
2808         db_vals[8].type = DB1_INT;
2809         db_vals[8].nul = 0;
2810         db_vals[8].val.int_val = sco.family;
2811
2812         db_keys[9] = &type_column;
2813         db_vals[9].type = DB1_INT;
2814         db_vals[9].nul = 0;
2815         db_vals[9].val.int_val = sco.type;
2816
2817         db_keys[10] = &node_column;
2818         db_vals[10].type = DB1_STR;
2819         db_vals[10].nul = 0;
2820         db_vals[10].val.str_val = sco.node;
2821
2822         db_keys[11] = &msg_column;
2823         db_vals[11].type = DB1_BLOB;
2824         db_vals[11].nul = 0;
2825
2826         if(_data && _data->len > 0) {
2827                 tmp.s = _data->s;
2828                 tmp.len = _data->len;
2829         } else {
2830                 /* MSG */
2831                 tmp.s = msg->buf;
2832                 tmp.len = msg->len;
2833         }
2834
2835         db_vals[11].val.blob_val = tmp;
2836
2837         c->db_funcs.use_table(c->db_con, _table);
2838
2839         if(c->db_funcs.insert(c->db_con, db_keys, db_vals, RTCP_NR_KEYS) < 0) {
2840                 LM_ERR("failed to insert into database\n");
2841                 goto error;
2842         }
2843
2844         return 1;
2845
2846 error:
2847         return -1;
2848 }
2849
2850 // Algorithm optimized by coudot
2851 int parse_aleg_callid_headers(str *headers_str, str *headers)
2852 {
2853         if(headers_str->len == 0) {
2854                 return 0;
2855         }
2856
2857         int index = 0;
2858         int begin = 0;
2859         int current = 0;
2860
2861         while((index < headers_str->len) && (current < MAX_HEADERS)) {
2862                 // End of headers string
2863                 if((index == headers_str->len - 1) && (headers_str->s[index] != ';')) {
2864                         headers[current].s = headers_str->s + begin;
2865                         headers[current].len = index + 1 - begin;
2866                         current++;
2867                         break;
2868                 } else if(headers_str->s[index] == ';') {
2869                         // Skip empty header
2870                         if(begin == index) {
2871                                 begin++;
2872                         } else {
2873                                 // Another header identified
2874                                 headers[current].s = headers_str->s + begin;
2875                                 headers[current].len = index - begin;
2876                                 current++;
2877                                 begin = index + 1;
2878                         }
2879                 }
2880                 // Move to next char
2881                 index++;
2882         }
2883
2884         // current now holds the number of headers
2885         return current;
2886 }
2887
2888 static int sipcapture_parse_aleg_callid_headers()
2889 {
2890         int i;
2891         n_callid_aleg_headers =
2892                 parse_aleg_callid_headers(&callid_aleg_header, callid_aleg_headers);
2893         LM_DBG("Number of headers:%d\n", n_callid_aleg_headers);
2894
2895         for(i = 0; i < n_callid_aleg_headers; i++) {
2896                 LM_DBG("Header: %.*s\n", callid_aleg_headers[i].len,
2897                                 callid_aleg_headers[i].s);
2898         }
2899
2900         return n_callid_aleg_headers;
2901 }
2902
2903
2904 static int nosip_hep_msg(sr_event_param_t *evp)
2905 {
2906         sip_msg_t *msg;
2907         char *buf;
2908         int len = 0;
2909         struct run_act_ctx ra_ctx;
2910         int ret = 0;
2911         int rtb;
2912         sr_kemi_eng_t *keng = NULL;
2913         str evname = str_init("sipcapture:request");
2914         struct hep_hdr *heph;
2915
2916         msg = (sip_msg_t *)evp->data;
2917
2918         buf = msg->buf;
2919         len = msg->len;
2920
2921         rtb = get_route_type();
2922         set_route_type(EVENT_ROUTE);
2923         if(hep_route_no>=-1) {
2924                 /* first send to route */
2925                 init_run_actions_ctx(&ra_ctx);
2926                 ret = run_actions(&ra_ctx, event_rt.rlist[hep_route_no], msg);
2927
2928                 if(ret != 1) {
2929                         LM_DBG("return code from event route: %d - skipping\n", ret);
2930                         set_route_type(rtb);
2931                         return ret;
2932                 }
2933         } else {
2934                 if(sc_event_callback.s == NULL || sc_event_callback.len <= 0) {
2935                         LM_ERR("no kemi callback set\n");
2936                         set_route_type(rtb);
2937                         return -1;
2938                 }
2939                 keng = sr_kemi_eng_get();
2940                 if(keng==NULL) {
2941                         LM_ERR("kemi engine not available\n");
2942                         set_route_type(rtb);
2943                         return -1;
2944                 }
2945                 if(keng->froute(msg, EVENT_ROUTE, &sc_event_callback, &evname) < 0) {
2946                         LM_ERR("error running event route kemi callback\n");
2947                         set_route_type(rtb);
2948                         return -1;
2949                 }
2950         }
2951         set_route_type(rtb);
2952
2953         /* hep_hdr */
2954         heph = (struct hep_hdr *)msg->buf;
2955
2956         if(heph->hp_v == 1 || heph->hp_v == 2) {
2957
2958                 LOG(L_ERR, "ERROR: HEP v 1/2: v:[%d] l:[%d]\n", heph->hp_v, heph->hp_l);
2959                 if((len = hepv2_message_parse(buf, len, msg)) < 0) {
2960                         LOG(L_ERR, "ERROR: during hepv2 parsing :[%d]\n", len);
2961                         return 0;
2962                 }
2963
2964                 buf = msg->buf + len;
2965                 len = msg->len - len;
2966
2967                 msg->buf = buf;
2968                 msg->len = len;
2969         } else if(!memcmp(msg->buf, "\x48\x45\x50\x33", 4)
2970                         || !memcmp(msg->buf, "\x45\x45\x50\x31", 4)) {
2971
2972                 if((len = hepv3_message_parse(buf, len, msg)) < 0) {
2973                         LOG(L_ERR, "ERROR: during hepv3 parsing :[%d]\n", len);
2974                         return 0;
2975                 }
2976
2977                 buf = msg->buf + len;
2978                 len = msg->len - len;
2979
2980                 msg->buf = buf;
2981                 msg->len = len;
2982         } else {
2983                 LOG(L_ERR, "ERROR: sipcapture:hep_msg_received: not supported version "
2984                                 "or bad length: v:[%d] l:[%d]\n",
2985                                 heph->hp_v, heph->hp_l);
2986                 return -1;
2987         }
2988
2989         if(parse_msg(buf, len, msg) != 0) {
2990                 LOG(L_ERR, "couldn't parse sip message\n");
2991                 return -1;
2992         }
2993
2994         return ret;
2995 }
2996
2997
2998 static int hep_version(struct sip_msg *msg)
2999 {
3000         struct hep_hdr *heph;
3001         /* hep_hdr */
3002         heph = (struct hep_hdr *)msg->buf;
3003
3004         if(heph->hp_v == 1 || heph->hp_v == 2)
3005                 return heph->hp_v;
3006         else if(!memcmp(msg->buf, "\x48\x45\x50\x33", 4)
3007                         || !memcmp(msg->buf, "\x45\x45\x50\x31", 4))
3008                 return 3;
3009
3010         return -1;
3011 }
3012
3013 static int fix_hex_int(str *s)
3014 {
3015
3016         unsigned int retval = 0;
3017
3018         if(!s->len || !s->s)
3019                 goto error;
3020
3021         if(s->len > 2)
3022                 if((s->s[0] == '0') && ((s->s[1] | 0x20) == 'x')) {
3023                         if(hexstr2int(s->s + 2, s->len - 2, &retval) != 0)
3024                                 goto error;
3025                         else
3026                                 return retval;
3027                 }
3028
3029         if(str2int(s, (unsigned int *)&retval) < 0)
3030                 goto error;
3031
3032
3033         return retval;
3034
3035 error:
3036         LM_ERR("Invalid value for hex: <%*s>!\n", s->len, s->s);
3037         return -1;
3038 }
3039
3040
3041 static int pv_parse_hep_name(pv_spec_p sp, str *in)
3042 {
3043         int valchunk = 0;
3044
3045         if(sp == NULL || in == NULL || in->len <= 0)
3046                 return -1;
3047
3048         LM_DBG("REQUEST, PRE, %.*s\n", in->len, in->s);
3049
3050         switch(in->len) {
3051                 case 5: {
3052                                         if((valchunk = fix_hex_int(in)) > 0)
3053                                                 sp->pvp.pvn.u.isname.name.n = valchunk;
3054                                         else
3055                                                 goto error;
3056                                 } break;
3057                 case 6: {
3058                                         if(!strncmp(in->s, "src_ip", 6))
3059                                                 sp->pvp.pvn.u.isname.name.n = 2;
3060                                         else if(!strncmp(in->s, "dst_ip", 6))
3061                                                 sp->pvp.pvn.u.isname.name.n = 3;
3062                                         else
3063                                                 goto error;
3064                                 } break;
3065                 case 7: {
3066                                         if(!strncmp(in->s, "version", 7))
3067                                                 sp->pvp.pvn.u.isname.name.n = 0;
3068                                         else
3069                                                 goto error;
3070                                 } break;
3071                 default:
3072                                 goto error;
3073         }
3074         sp->pvp.pvn.type = PV_NAME_INTSTR;
3075         sp->pvp.pvn.u.isname.type = 0;
3076
3077         return 0;
3078
3079 error:
3080         LM_ERR("unknown hep name %.*s\n", in->len, in->s);
3081         return -1;
3082 }
3083
3084
3085 static int pv_get_hep(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
3086 {
3087         static char sc_buf_ip[IP_ADDR_MAX_STR_SIZE + 12];
3088         int sc_buf_ip_len;
3089
3090         if(param == NULL)
3091                 return -1;
3092
3093         switch(param->pvn.u.isname.name.n) {
3094                 case 0:
3095                         return pv_get_uintval(msg, param, res, hep_version(msg));
3096                 case 1:
3097                         return pv_get_uintval(msg, param, res, hep_version(msg));
3098                 case 2:
3099                         sc_buf_ip_len = ip_addr2sbuf(
3100                                         &msg->rcv.src_ip, sc_buf_ip, sizeof(sc_buf_ip) - 1);
3101                         sc_buf_ip[sc_buf_ip_len] = 0;
3102                         return pv_get_strlval(msg, param, res, sc_buf_ip, sc_buf_ip_len);
3103                 case 3:
3104                         sc_buf_ip_len = ip_addr2sbuf(
3105                                         &msg->rcv.dst_ip, sc_buf_ip, sizeof(sc_buf_ip) - 1);
3106                         sc_buf_ip[sc_buf_ip_len] = 0;
3107                         return pv_get_strlval(msg, param, res, sc_buf_ip, sc_buf_ip_len);
3108                 default:
3109                         return hepv3_get_chunk(msg, msg->buf, msg->len,
3110                                         param->pvn.u.isname.name.n, param, res);
3111         }
3112         return 0;
3113 }
3114
3115 /**
3116  *
3117  */
3118 static int ki_sip_capture_forward(sip_msg_t *msg, str *puri)
3119 {
3120         dest_info_t dst;
3121         sip_uri_t next_hop;
3122         str pdata;
3123         int ret = 0;
3124         char proto;
3125
3126         if(parse_uri(puri->s, puri->len, &next_hop)<0) {
3127                 LM_ERR("bad dst sip uri <%.*s>\n", puri->len, puri->s);
3128                 return -1;
3129         }
3130
3131         init_dest_info(&dst);
3132         LM_DBG("sending data to sip uri <%.*s>\n", puri->len, puri->s);
3133         proto = next_hop.proto;
3134         if(sip_hostport2su(&dst.to, &next_hop.host, next_hop.port_no,
3135                                 &proto)!=0) {
3136                 LM_ERR("failed to resolve [%.*s]\n", next_hop.host.len,
3137                         ZSW(next_hop.host.s));
3138                 return -1;
3139         }
3140         dst.proto = proto;
3141         if(dst.proto==PROTO_NONE) dst.proto = PROTO_UDP;
3142
3143         pdata.s = msg->buf;
3144         pdata.len = msg->len;
3145
3146         if (dst.proto == PROTO_UDP) {
3147                 dst.send_sock=get_send_socket(0, &dst.to, PROTO_UDP);
3148                 if (dst.send_sock!=0) {
3149                         ret=udp_send(&dst, pdata.s, pdata.len);
3150                 } else {
3151                         LM_ERR("no socket for dst sip uri <%.*s>\n", puri->len, puri->s);
3152                         ret=-1;
3153                 }
3154         }
3155 #ifdef USE_TCP
3156         else if(dst.proto == PROTO_TCP) {
3157                 /*tcp*/
3158                 dst.id=0;
3159                 ret=tcp_send(&dst, 0, pdata.s, pdata.len);
3160         }
3161 #endif
3162 #ifdef USE_TLS
3163         else if(dst.proto == PROTO_TLS) {
3164                 /*tls*/
3165                 dst.id=0;
3166                 ret=tcp_send(&dst, 0, pdata.s, pdata.len);
3167         }
3168 #endif
3169 #ifdef USE_SCTP
3170         else if(dst.proto == PROTO_SCTP) {
3171                 /*sctp*/
3172                 dst.send_sock=get_send_socket(0, &dst.to, PROTO_SCTP);
3173                 if (dst.send_sock!=0) {
3174                         ret=sctp_core_msg_send(&dst, pdata.s, pdata.len);
3175                 } else {
3176                         LM_ERR("no socket for dst sip uri <%.*s>\n", puri->len, puri->s);
3177                         ret=-1;
3178                 }
3179         }
3180 #endif
3181         else {
3182                 LM_ERR("unknown proto [%d] for dst sip uri <%.*s>\n",
3183                                 dst.proto, puri->len, puri->s);
3184                 ret=-1;
3185         }
3186
3187         if (ret>=0) ret=1;
3188
3189         return ret;
3190 }
3191
3192 /**
3193  *
3194  */
3195 static int w_sip_capture_forward(sip_msg_t *_m, char *_dst, char *_p2)
3196 {
3197         str sdst;
3198
3199         if(fixup_get_svalue(_m, (gparam_t*)_dst, &sdst)<0) {
3200                 LM_ERR("failed to get the destination address\n");
3201                 return -1;
3202         }
3203
3204         return ki_sip_capture_forward(_m, &sdst);
3205 }
3206
3207 /**
3208  *
3209  */
3210 /* clang-format off */
3211 static sr_kemi_t sr_kemi_sipcapture_exports[] = {
3212         { str_init("sipcapture"), str_init("sip_capture"),
3213                 SR_KEMIP_INT, ki_sip_capture,
3214                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
3215                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3216         },
3217         { str_init("sipcapture"), str_init("sip_capture_table"),
3218                 SR_KEMIP_INT, ki_sip_capture_table,
3219                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3220                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3221         },
3222         { str_init("sipcapture"), str_init("sip_capture_mode"),
3223                 SR_KEMIP_INT, ki_sip_capture_mode,
3224                 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
3225                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3226         },
3227         { str_init("sipcapture"), str_init("report_capture"),
3228                 SR_KEMIP_INT, ki_report_capture,
3229                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3230                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3231         },
3232         { str_init("sipcapture"), str_init("report_capture_cid"),
3233                 SR_KEMIP_INT, ki_report_capture_cid,
3234                 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
3235                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3236         },
3237         { str_init("sipcapture"), str_init("report_capture_data"),
3238                 SR_KEMIP_INT, ki_report_capture_data,
3239                 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
3240                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3241         },
3242         { str_init("sipcapture"), str_init("float2int"),
3243                 SR_KEMIP_INT, ki_float2int,
3244                 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
3245                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3246         },
3247         { str_init("sipcapture"), str_init("sip_capture_forward"),
3248                 SR_KEMIP_INT, ki_sip_capture_forward,
3249                 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3250                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3251         },
3252
3253         { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
3254 };
3255 /* clang-format on */
3256
3257 /**
3258  *
3259  */
3260 int mod_register(char *path, int *dlflags, void *p1, void *p2)
3261 {
3262         sr_kemi_modules_add(sr_kemi_sipcapture_exports);
3263         return 0;
3264 }