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