Browse code

siptrace: split code from one single big file

Daniel-Constantin Mierla authored on 31/10/2017 17:11:43
Showing 6 changed files
... ...
@@ -52,34 +52,12 @@
52 52
 #include "../../core/onsend.h"
53 53
 #include "../../core/events.h"
54 54
 
55
-#include "../../modules/sipcapture/hep.h"
56
-
57
-#ifdef STATISTICS
58
-#include "../../core/counters.h"
59
-#endif
55
+#include "siptrace_data.h"
56
+#include "siptrace_hep.h"
57
+#include "siptrace_send.h"
60 58
 
61 59
 MODULE_VERSION
62 60
 
63
-struct _siptrace_data {
64
-	struct usr_avp *avp;
65
-	int_str avp_value;
66
-	struct search_state state;
67
-	str body;
68
-	str callid;
69
-	str method;
70
-	str status;
71
-	char *dir;
72
-	str fromtag;
73
-	str fromip;
74
-	str totag;
75
-	str toip;
76
-	char toip_buff[IP_ADDR_MAX_STR_SIZE+12];
77
-	char fromip_buff[IP_ADDR_MAX_STR_SIZE+12];
78
-	struct timeval tv;
79
-#ifdef STATISTICS
80
-	stat_var *stat;
81
-#endif
82
-};
83 61
 
84 62
 struct tm_binds tmb;
85 63
 
... ...
@@ -91,57 +69,48 @@ static int mod_init(void);
91 69
 static int siptrace_init_rpc(void);
92 70
 static int child_init(int rank);
93 71
 static void destroy(void);
94
-static int sip_trace(struct sip_msg*, struct dest_info*, str *correlation_id_str, char*);
72
+static int sip_trace(
73
+		struct sip_msg *, struct dest_info *, str *correlation_id_str, char *);
95 74
 static int sip_trace2(struct sip_msg *, char *dest, char *correlation_id);
96
-static int fixup_siptrace(void ** param, int param_no);
97
-
98
-static int w_hlog1(struct sip_msg*, char* message, char*);
99
-static int w_hlog2(struct sip_msg*, char *correlationid, char* message);
100
-static int hlog(struct sip_msg*, str*, str*);
75
+static int fixup_siptrace(void **param, int param_no);
101 76
 
77
+static int w_hlog1(struct sip_msg *, char *message, char *);
78
+static int w_hlog2(struct sip_msg *, char *correlationid, char *message);
102 79
 
103
-static int sip_trace_store_db(struct _siptrace_data* sto);
104
-static int trace_send_duplicate(char *buf, int len, struct dest_info*);
80
+static int sip_trace_store_db(struct _siptrace_data *sto);
105 81
 
106
-static void trace_onreq_in(struct cell* t, int type, struct tmcb_params *ps);
107
-static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps);
108
-static void trace_onreply_in(struct cell* t, int type, struct tmcb_params *ps);
109
-static void trace_onreply_out(struct cell* t, int type, struct tmcb_params *ps);
82
+static void trace_onreq_in(struct cell *t, int type, struct tmcb_params *ps);
83
+static void trace_onreq_out(struct cell *t, int type, struct tmcb_params *ps);
84
+static void trace_onreply_in(struct cell *t, int type, struct tmcb_params *ps);
85
+static void trace_onreply_out(struct cell *t, int type, struct tmcb_params *ps);
110 86
 static void trace_sl_onreply_out(sl_cbp_t *slcb);
111 87
 static void trace_sl_ack_in(sl_cbp_t *slcb);
112 88
 
113
-static int trace_send_hep_duplicate(str *body, str *from, str *to, struct dest_info*, str *correlation_id);
114
-static int trace_send_hep2_duplicate(str *body, str *from, str *to, struct dest_info*);
115
-static int trace_send_hep3_duplicate(str *body, str *from, str *to, struct dest_info*, str *correlation_id);
116
-static int pipport2su (char *pipport, union sockaddr_union *tmp_su, unsigned int *proto);
117
-
118 89
 int siptrace_net_data_recv(sr_event_param_t *evp);
119 90
 int siptrace_net_data_send(sr_event_param_t *evp);
120 91
 static int _siptrace_mode = 0;
121 92
 
122 93
 
123
-static str db_url             = str_init(DEFAULT_DB_URL);
124
-static str siptrace_table     = str_init("sip_trace");
125
-static str date_column        = str_init("time_stamp");  /* 00 */
126
-static str callid_column      = str_init("callid");      /* 01 */
94
+static str db_url = str_init(DEFAULT_DB_URL);
95
+static str siptrace_table = str_init("sip_trace");
96
+static str date_column = str_init("time_stamp");		 /* 00 */
97
+static str callid_column = str_init("callid");			 /* 01 */
127 98
 static str traced_user_column = str_init("traced_user"); /* 02 */
128
-static str msg_column         = str_init("msg");         /* 03 */
129
-static str method_column      = str_init("method");      /* 04 */
130
-static str status_column      = str_init("status");      /* 05 */
131
-static str fromip_column      = str_init("fromip");      /* 06 */
132
-static str toip_column        = str_init("toip");        /* 07 */
133
-static str fromtag_column     = str_init("fromtag");     /* 08 */
134
-static str direction_column   = str_init("direction");   /* 09 */
135
-static str time_us_column     = str_init("time_us");     /* 10 */
136
-static str totag_column       = str_init("totag");       /* 11 */
99
+static str msg_column = str_init("msg");				 /* 03 */
100
+static str method_column = str_init("method");			 /* 04 */
101
+static str status_column = str_init("status");			 /* 05 */
102
+static str fromip_column = str_init("fromip");			 /* 06 */
103
+static str toip_column = str_init("toip");				 /* 07 */
104
+static str fromtag_column = str_init("fromtag");		 /* 08 */
105
+static str direction_column = str_init("direction");	 /* 09 */
106
+static str time_us_column = str_init("time_us");		 /* 10 */
107
+static str totag_column = str_init("totag");			 /* 11 */
137 108
 
138 109
 #define NR_KEYS 12
139 110
 #define SIP_TRACE_TABLE_VERSION 4
140 111
 
141
-#define XHEADERS_BUFSIZE 512
142
-
143 112
 int trace_flag = 0;
144
-int trace_on   = 0;
113
+int trace_on = 0;
145 114
 int trace_sl_acks = 1;
146 115
 
147 116
 int trace_to_database = 1;
... ...
@@ -156,9 +125,9 @@ int xheaders_write = 0;
156 125
 int xheaders_read = 0;
157 126
 
158 127
 str force_send_sock_str = {0, 0};
159
-struct sip_uri * force_send_sock_uri = 0;
128
+struct sip_uri *force_send_sock_uri = 0;
160 129
 
161
-str    dup_uri_str      = {0, 0};
130
+str dup_uri_str = {0, 0};
162 131
 struct sip_uri *dup_uri = 0;
163 132
 
164 133
 int *trace_on_flag = NULL;
... ...
@@ -179,18 +148,23 @@ static str trace_local_ip = {NULL, 0};
179 148
 
180 149
 int hep_mode_on = 0;
181 150
 
182
-db1_con_t *db_con = NULL; 		/*!< database connection */
183
-db_func_t db_funcs;      		/*!< Database functions */
151
+db1_con_t *db_con = NULL; /*!< database connection */
152
+db_func_t db_funcs;		  /*!< Database functions */
184 153
 
185 154
 /*! \brief
186 155
  * Exported functions
187 156
  */
188 157
 static cmd_export_t cmds[] = {
189
-	{"sip_trace", (cmd_function)sip_trace, 0, 0, 0, ANY_ROUTE},
190
-	{"sip_trace", (cmd_function)sip_trace, 1, fixup_siptrace, 0, ANY_ROUTE},
191
-	{"sip_trace", (cmd_function)sip_trace2, 2, fixup_spve_spve, 0, ANY_ROUTE},
192
-	{"hlog", (cmd_function)w_hlog1, 1, fixup_spve_null, 0, ANY_ROUTE},
193
-	{"hlog", (cmd_function)w_hlog2, 2, fixup_spve_spve, 0, ANY_ROUTE},
158
+	{"sip_trace", (cmd_function)sip_trace, 0, 0, 0,
159
+		ANY_ROUTE},
160
+	{"sip_trace", (cmd_function)sip_trace, 1, fixup_siptrace, 0,
161
+		ANY_ROUTE},
162
+	{"sip_trace", (cmd_function)sip_trace2, 2, fixup_spve_spve, 0,
163
+		ANY_ROUTE},
164
+	{"hlog", (cmd_function)w_hlog1, 1, fixup_spve_null, 0,
165
+		ANY_ROUTE},
166
+	{"hlog", (cmd_function)w_hlog2, 2, fixup_spve_spve, 0,
167
+		ANY_ROUTE},
194 168
 	{0, 0, 0, 0, 0, 0}
195 169
 };
196 170
 
... ...
@@ -199,47 +173,44 @@ static cmd_export_t cmds[] = {
199 173
  * Exported parameters
200 174
  */
201 175
 static param_export_t params[] = {
202
-	{"auth_key",           PARAM_STR, &auth_key_str       },
203
-	{"db_url",             PARAM_STR, &db_url            },
204
-	{"table",              PARAM_STR, &siptrace_table     },
205
-	{"date_column",        PARAM_STR, &date_column        },
206
-	{"callid_column",      PARAM_STR, &callid_column      },
207
-	{"traced_user_column", PARAM_STR, &traced_user_column },
208
-	{"msg_column",         PARAM_STR, &msg_column         },
209
-	{"method_column",      PARAM_STR, &method_column      },
210
-	{"status_column",      PARAM_STR, &status_column      },
211
-	{"fromip_column",      PARAM_STR, &fromip_column      },
212
-	{"toip_column",        PARAM_STR, &toip_column        },
213
-	{"fromtag_column",     PARAM_STR, &fromtag_column     },
214
-	{"totag_column",       PARAM_STR, &totag_column       },
215
-	{"direction_column",   PARAM_STR, &direction_column   },
216
-	{"trace_flag",         INT_PARAM, &trace_flag           },
217
-	{"trace_on",           INT_PARAM, &trace_on             },
218
-	{"traced_user_avp",    PARAM_STR, &traced_user_avp_str},
219
-	{"trace_table_avp",    PARAM_STR, &trace_table_avp_str},
220
-	{"duplicate_uri",      PARAM_STR, &dup_uri_str        },
221
-	{"trace_to_database",  INT_PARAM, &trace_to_database    },
222
-	{"trace_local_ip",     PARAM_STR, &trace_local_ip     },
223
-	{"trace_sl_acks",      INT_PARAM, &trace_sl_acks        },
224
-	{"xheaders_write",     INT_PARAM, &xheaders_write       },
225
-	{"xheaders_read",      INT_PARAM, &xheaders_read        },
226
-	{"hep_mode_on",        INT_PARAM, &hep_mode_on          },
227
-	{"force_send_sock",    PARAM_STR, &force_send_sock_str	},
228
-	{"hep_version",        INT_PARAM, &hep_version          },
229
-	{"hep_capture_id",     INT_PARAM, &hep_capture_id       },
230
-	{"trace_delayed",      INT_PARAM, &trace_delayed        },
231
-	{"trace_mode",         PARAM_INT, &_siptrace_mode       },
232
-	{0, 0, 0}
176
+	{"auth_key", PARAM_STR, &auth_key_str},
177
+	{"db_url", PARAM_STR, &db_url}, {"table", PARAM_STR, &siptrace_table},
178
+	{"date_column", PARAM_STR, &date_column},
179
+	{"callid_column", PARAM_STR, &callid_column},
180
+	{"traced_user_column", PARAM_STR, &traced_user_column},
181
+	{"msg_column", PARAM_STR, &msg_column},
182
+	{"method_column", PARAM_STR, &method_column},
183
+	{"status_column", PARAM_STR, &status_column},
184
+	{"fromip_column", PARAM_STR, &fromip_column},
185
+	{"toip_column", PARAM_STR, &toip_column},
186
+	{"fromtag_column", PARAM_STR, &fromtag_column},
187
+	{"totag_column", PARAM_STR, &totag_column},
188
+	{"direction_column", PARAM_STR, &direction_column},
189
+	{"trace_flag", INT_PARAM, &trace_flag},
190
+	{"trace_on", INT_PARAM, &trace_on},
191
+	{"traced_user_avp", PARAM_STR, &traced_user_avp_str},
192
+	{"trace_table_avp", PARAM_STR, &trace_table_avp_str},
193
+	{"duplicate_uri", PARAM_STR, &dup_uri_str},
194
+	{"trace_to_database", INT_PARAM, &trace_to_database},
195
+	{"trace_local_ip", PARAM_STR, &trace_local_ip},
196
+	{"trace_sl_acks", INT_PARAM, &trace_sl_acks},
197
+	{"xheaders_write", INT_PARAM, &xheaders_write},
198
+	{"xheaders_read", INT_PARAM, &xheaders_read},
199
+	{"hep_mode_on", INT_PARAM, &hep_mode_on},
200
+	{"force_send_sock", PARAM_STR, &force_send_sock_str},
201
+	{"hep_version", INT_PARAM, &hep_version},
202
+	{"hep_capture_id", INT_PARAM, &hep_capture_id},
203
+	{"trace_delayed", INT_PARAM, &trace_delayed},
204
+	{"trace_mode", PARAM_INT, &_siptrace_mode}, {0, 0, 0}
233 205
 };
234 206
 
235 207
 #ifdef STATISTICS
236
-stat_var* siptrace_req;
237
-stat_var* siptrace_rpl;
208
+stat_var *siptrace_req;
209
+stat_var *siptrace_rpl;
238 210
 
239 211
 stat_export_t siptrace_stats[] = {
240
-	{"traced_requests" ,  0,  &siptrace_req  },
241
-	{"traced_replies"  ,  0,  &siptrace_rpl  },
242
-	{0,0,0}
212
+	{"traced_requests", 0, &siptrace_req},
213
+	{"traced_replies", 0, &siptrace_rpl}, {0, 0, 0}
243 214
 };
244 215
 #endif
245 216
 
... ...
@@ -247,20 +218,20 @@ stat_export_t siptrace_stats[] = {
247 218
 struct module_exports exports = {
248 219
 	"siptrace",
249 220
 	DEFAULT_DLFLAGS, /*!< dlopen flags */
250
-	cmds,       /*!< Exported functions */
251
-	params,     /*!< Exported parameters */
221
+	cmds,						 /*!< Exported functions */
222
+	params,						 /*!< Exported parameters */
252 223
 #ifdef STATISTICS
253
-	siptrace_stats,  /*!< exported statistics */
224
+	siptrace_stats, /*!< exported statistics */
254 225
 #else
255
-	0,          /*!< exported statistics */
226
+	0, /*!< exported statistics */
256 227
 #endif
257
-	0,    /*!< exported MI functions */
258
-	0,          /*!< exported pseudo-variables */
259
-	0,          /*!< extra processes */
260
-	mod_init,   /*!< module initialization function */
261
-	0,          /*!< response function */
262
-	destroy,    /*!< destroy function */
263
-	child_init  /*!< child initialization function */
228
+	0,		   /*!< exported MI functions */
229
+	0,		   /*!< exported pseudo-variables */
230
+	0,		   /*!< extra processes */
231
+	mod_init,  /*!< module initialization function */
232
+	0,		   /*!< response function */
233
+	destroy,   /*!< destroy function */
234
+	child_init /*!< child initialization function */
264 235
 };
265 236
 
266 237
 
... ...
@@ -272,28 +243,25 @@ static int mod_init(void)
272 243
 
273 244
 #ifdef STATISTICS
274 245
 	/* register statistics */
275
-	if (register_module_stats(exports.name, siptrace_stats)!=0)
276
-	{
246
+	if(register_module_stats(exports.name, siptrace_stats) != 0) {
277 247
 		LM_ERR("failed to register core statistics\n");
278 248
 		return -1;
279 249
 	}
280 250
 #endif
281 251
 
282
-	if(siptrace_init_rpc() != 0)
283
-	{
252
+	if(siptrace_init_rpc() != 0) {
284 253
 		LM_ERR("failed to register RPC commands\n");
285 254
 		return -1;
286 255
 	}
287 256
 
288
-	if (trace_flag<0 || trace_flag>(int)MAX_FLAG)
289
-	{
257
+	if(trace_flag < 0 || trace_flag > (int)MAX_FLAG) {
290 258
 		LM_ERR("invalid trace flag %d\n", trace_flag);
291 259
 		return -1;
292 260
 	}
293
-	trace_flag = 1<<trace_flag;
261
+	trace_flag = 1 << trace_flag;
294 262
 
295
-	trace_to_database_flag = (int*)shm_malloc(sizeof(int));
296
-	if(trace_to_database_flag==NULL) {
263
+	trace_to_database_flag = (int *)shm_malloc(sizeof(int));
264
+	if(trace_to_database_flag == NULL) {
297 265
 		LM_ERR("no more shm memory left\n");
298 266
 		return -1;
299 267
 	}
... ...
@@ -306,31 +274,29 @@ static int mod_init(void)
306 274
 	}
307 275
 
308 276
 	/* Find a database module if needed */
309
-	if(trace_to_database_flag!=NULL && *trace_to_database_flag!=0) {
310
-		if (db_bind_mod(&db_url, &db_funcs))
311
-		{
277
+	if(trace_to_database_flag != NULL && *trace_to_database_flag != 0) {
278
+		if(db_bind_mod(&db_url, &db_funcs)) {
312 279
 			LM_ERR("unable to bind database module\n");
313 280
 			return -1;
314 281
 		}
315
-		if (trace_to_database_flag && !DB_CAPABILITY(db_funcs, DB_CAP_INSERT))
316
-		{
282
+		if(trace_to_database_flag && !DB_CAPABILITY(db_funcs, DB_CAP_INSERT)) {
317 283
 			LM_ERR("database modules does not provide all functions needed"
318
-					" by module\n");
284
+				   " by module\n");
319 285
 			return -1;
320 286
 		}
321 287
 	}
322 288
 
323
-	trace_on_flag = (int*)shm_malloc(sizeof(int));
324
-	if(trace_on_flag==NULL) {
289
+	trace_on_flag = (int *)shm_malloc(sizeof(int));
290
+	if(trace_on_flag == NULL) {
325 291
 		LM_ERR("no more shm memory left\n");
326 292
 		return -1;
327 293
 	}
328 294
 
329 295
 	*trace_on_flag = trace_on;
330 296
 
331
-	xheaders_write_flag = (int*)shm_malloc(sizeof(int));
332
-	xheaders_read_flag = (int*)shm_malloc(sizeof(int));
333
-	if (!(xheaders_write_flag && xheaders_read_flag)) {
297
+	xheaders_write_flag = (int *)shm_malloc(sizeof(int));
298
+	xheaders_read_flag = (int *)shm_malloc(sizeof(int));
299
+	if(!(xheaders_write_flag && xheaders_read_flag)) {
334 300
 		LM_ERR("no more shm memory left\n");
335 301
 		return -1;
336 302
 	}
... ...
@@ -338,107 +304,100 @@ static int mod_init(void)
338 304
 	*xheaders_read_flag = xheaders_read;
339 305
 
340 306
 	/* register callbacks to TM */
341
-	if (load_tm_api(&tmb)!=0) {
307
+	if(load_tm_api(&tmb) != 0) {
342 308
 		LM_WARN("can't load tm api. Will not install tm callbacks.\n");
343
-	} else if(tmb.register_tmcb( 0, 0, TMCB_REQUEST_IN, trace_onreq_in, 0, 0) <=0) {
309
+	} else if(tmb.register_tmcb(0, 0, TMCB_REQUEST_IN, trace_onreq_in, 0, 0)
310
+			  <= 0) {
344 311
 		LM_ERR("can't register trace_onreq_in\n");
345 312
 		return -1;
346 313
 	}
347 314
 
348 315
 	/* bind the SL API */
349
-	if (sl_load_api(&slb)!=0) {
316
+	if(sl_load_api(&slb) != 0) {
350 317
 		LM_WARN("cannot bind to SL API. Will not install sl callbacks.\n");
351 318
 	} else {
352 319
 		/* register sl callbacks */
353 320
 		memset(&slcb, 0, sizeof(sl_cbelem_t));
354 321
 
355 322
 		slcb.type = SLCB_REPLY_READY;
356
-		slcb.cbf  = trace_sl_onreply_out;
357
-		if (slb.register_cb(&slcb) != 0) {
323
+		slcb.cbf = trace_sl_onreply_out;
324
+		if(slb.register_cb(&slcb) != 0) {
358 325
 			LM_ERR("can't register for SLCB_REPLY_READY\n");
359 326
 			return -1;
360 327
 		}
361 328
 
362
-		if(trace_sl_acks)
363
-		{
329
+		if(trace_sl_acks) {
364 330
 			slcb.type = SLCB_ACK_FILTERED;
365
-			slcb.cbf  = trace_sl_ack_in;
366
-			if (slb.register_cb(&slcb) != 0) {
331
+			slcb.cbf = trace_sl_ack_in;
332
+			if(slb.register_cb(&slcb) != 0) {
367 333
 				LM_ERR("can't register for SLCB_ACK_FILTERED\n");
368 334
 				return -1;
369 335
 			}
370 336
 		}
371 337
 	}
372 338
 
373
-	if(dup_uri_str.s!=0)
374
-	{
339
+	if(dup_uri_str.s != 0) {
375 340
 		dup_uri = (struct sip_uri *)pkg_malloc(sizeof(struct sip_uri));
376
-		if(dup_uri==0)
377
-		{
341
+		if(dup_uri == 0) {
378 342
 			LM_ERR("no more pkg memory left\n");
379 343
 			return -1;
380 344
 		}
381 345
 		memset(dup_uri, 0, sizeof(struct sip_uri));
382
-		if(parse_uri(dup_uri_str.s, dup_uri_str.len, dup_uri)<0)
383
-		{
346
+		if(parse_uri(dup_uri_str.s, dup_uri_str.len, dup_uri) < 0) {
384 347
 			LM_ERR("bad dup uri\n");
385 348
 			return -1;
386 349
 		}
387 350
 	}
388 351
 
389
-	if(force_send_sock_str.s!=0)
390
-	{
352
+	if(force_send_sock_str.s != 0) {
391 353
 		force_send_sock_str.len = strlen(force_send_sock_str.s);
392
-		force_send_sock_uri = (struct sip_uri *)pkg_malloc(sizeof(struct sip_uri));
393
-		if(force_send_sock_uri==0)
394
-		{
354
+		force_send_sock_uri =
355
+				(struct sip_uri *)pkg_malloc(sizeof(struct sip_uri));
356
+		if(force_send_sock_uri == 0) {
395 357
 			LM_ERR("no more pkg memory left\n");
396 358
 			return -1;
397 359
 		}
398 360
 		memset(force_send_sock_uri, 0, sizeof(struct sip_uri));
399
-		if(parse_uri(force_send_sock_str.s, force_send_sock_str.len, force_send_sock_uri)<0)
400
-		{
361
+		if(parse_uri(force_send_sock_str.s, force_send_sock_str.len,
362
+				   force_send_sock_uri)
363
+				< 0) {
401 364
 			LM_ERR("bad dup uri\n");
402 365
 			return -1;
403 366
 		}
404 367
 	}
405 368
 
406
-	if(traced_user_avp_str.s && traced_user_avp_str.len > 0)
407
-	{
408
-		if (pv_parse_spec(&traced_user_avp_str, &avp_spec)==0
409
-				|| avp_spec.type!=PVT_AVP)
410
-		{
369
+	if(traced_user_avp_str.s && traced_user_avp_str.len > 0) {
370
+		if(pv_parse_spec(&traced_user_avp_str, &avp_spec) == 0
371
+				|| avp_spec.type != PVT_AVP) {
411 372
 			LM_ERR("malformed or non AVP %.*s AVP definition\n",
412 373
 					traced_user_avp_str.len, traced_user_avp_str.s);
413 374
 			return -1;
414 375
 		}
415 376
 
416
-		if(pv_get_avp_name(0, &avp_spec.pvp, &traced_user_avp,
417
-					&traced_user_avp_type)!=0)
418
-		{
419
-			LM_ERR("[%.*s] - invalid AVP definition\n",
420
-					traced_user_avp_str.len, traced_user_avp_str.s);
377
+		if(pv_get_avp_name(
378
+				   0, &avp_spec.pvp, &traced_user_avp, &traced_user_avp_type)
379
+				!= 0) {
380
+			LM_ERR("[%.*s] - invalid AVP definition\n", traced_user_avp_str.len,
381
+					traced_user_avp_str.s);
421 382
 			return -1;
422 383
 		}
423 384
 	} else {
424 385
 		traced_user_avp.n = 0;
425 386
 		traced_user_avp_type = 0;
426 387
 	}
427
-	if(trace_table_avp_str.s && trace_table_avp_str.len > 0)
428
-	{
429
-		if (pv_parse_spec(&trace_table_avp_str, &avp_spec)==0
430
-				|| avp_spec.type!=PVT_AVP)
431
-		{
388
+	if(trace_table_avp_str.s && trace_table_avp_str.len > 0) {
389
+		if(pv_parse_spec(&trace_table_avp_str, &avp_spec) == 0
390
+				|| avp_spec.type != PVT_AVP) {
432 391
 			LM_ERR("malformed or non AVP %.*s AVP definition\n",
433 392
 					trace_table_avp_str.len, trace_table_avp_str.s);
434 393
 			return -1;
435 394
 		}
436 395
 
437
-		if(pv_get_avp_name(0, &avp_spec.pvp, &trace_table_avp,
438
-					&trace_table_avp_type)!=0)
439
-		{
440
-			LM_ERR("[%.*s] - invalid AVP definition\n",
441
-					trace_table_avp_str.len, trace_table_avp_str.s);
396
+		if(pv_get_avp_name(
397
+				   0, &avp_spec.pvp, &trace_table_avp, &trace_table_avp_type)
398
+				!= 0) {
399
+			LM_ERR("[%.*s] - invalid AVP definition\n", trace_table_avp_str.len,
400
+					trace_table_avp_str.s);
442 401
 			return -1;
443 402
 		}
444 403
 	} else {
... ...
@@ -446,7 +405,7 @@ static int mod_init(void)
446 405
 		trace_table_avp_type = 0;
447 406
 	}
448 407
 
449
-	if(_siptrace_mode==1) {
408
+	if(_siptrace_mode == 1) {
450 409
 		sr_event_register_cb(SREV_NET_DATA_RECV, siptrace_net_data_recv);
451 410
 		sr_event_register_cb(SREV_NET_DATA_SEND, siptrace_net_data_send);
452 411
 	}
... ...
@@ -456,20 +415,21 @@ static int mod_init(void)
456 415
 
457 416
 static int child_init(int rank)
458 417
 {
459
-	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
418
+	if(rank == PROC_INIT || rank == PROC_MAIN || rank == PROC_TCP_MAIN)
460 419
 		return 0; /* do nothing for the main process */
461 420
 
462
-	if(trace_to_database_flag!=NULL && *trace_to_database_flag!=0) {
421
+	if(trace_to_database_flag != NULL && *trace_to_database_flag != 0) {
463 422
 		db_con = db_funcs.init(&db_url);
464
-		if (!db_con)
465
-		{
466
-			LM_ERR("unable to connect to database. Please check configuration.\n");
423
+		if(!db_con) {
424
+			LM_ERR("unable to connect to database. Please check "
425
+				   "configuration.\n");
467 426
 			return -1;
468 427
 		}
469
-		if (db_check_table_version(&db_funcs, db_con, &siptrace_table,
470
-					SIP_TRACE_TABLE_VERSION) < 0) {
428
+		if(db_check_table_version(
429
+				   &db_funcs, db_con, &siptrace_table, SIP_TRACE_TABLE_VERSION)
430
+				< 0) {
471 431
 			LM_ERR("error during table version check\n");
472
-			db_funcs.close(db_con);		
432
+			db_funcs.close(db_con);
473 433
 			return -1;
474 434
 		}
475 435
 	}
... ...
@@ -480,282 +440,58 @@ static int child_init(int rank)
480 440
 
481 441
 static void destroy(void)
482 442
 {
483
-	if(trace_to_database_flag!=NULL && *trace_to_database_flag!=0) {
484
-		if (db_con!=NULL)
443
+	if(trace_to_database_flag != NULL && *trace_to_database_flag != 0) {
444
+		if(db_con != NULL)
485 445
 			db_funcs.close(db_con);
486 446
 	}
487 447
 
488
-	if (trace_on_flag)
448
+	if(trace_on_flag)
489 449
 		shm_free(trace_on_flag);
490
-
491
-}
492
-
493
-static inline int siptrace_copy_proto(int proto, char *buf)
494
-{
495
-	if(buf==0)
496
-		return -1;
497
-	if(proto==PROTO_TCP) {
498
-		strcpy(buf, "tcp:");
499
-	} else if(proto==PROTO_TLS) {
500
-		strcpy(buf, "tls:");
501
-	} else if(proto==PROTO_SCTP) {
502
-		strcpy(buf, "sctp:");
503
-	} else if(proto==PROTO_WS) {
504
-		strcpy(buf, "ws:");
505
-	} else if(proto==PROTO_WSS) {
506
-		strcpy(buf, "wss:");
507
-	} else {
508
-		strcpy(buf, "udp:");
509
-	}
510
-	return 0;
511 450
 }
512 451
 
513
-static inline str* siptrace_get_table(void)
452
+static inline str *siptrace_get_table(void)
514 453
 {
515
-	static int_str         avp_value;
454
+	static int_str avp_value;
516 455
 	struct usr_avp *avp;
517 456
 
518
-	if(trace_table_avp.n==0)
457
+	if(trace_table_avp.n == 0)
519 458
 		return &siptrace_table;
520 459
 
521 460
 	avp = NULL;
522
-	if(trace_table_avp.n!=0)
523
-		avp=search_first_avp(trace_table_avp_type, trace_table_avp, &avp_value,
524
-				0);
461
+	if(trace_table_avp.n != 0)
462
+		avp = search_first_avp(
463
+				trace_table_avp_type, trace_table_avp, &avp_value, 0);
525 464
 
526
-	if(avp==NULL || !is_avp_str_val(avp) || avp_value.s.len<=0)
465
+	if(avp == NULL || !is_avp_str_val(avp) || avp_value.s.len <= 0)
527 466
 		return &siptrace_table;
528 467
 
529 468
 	return &avp_value.s;
530 469
 }
531 470
 
532
-static int sip_trace_prepare(sip_msg_t *msg)
471
+static int sip_trace_store(struct _siptrace_data *sto, struct dest_info *dst,
472
+		str *correlation_id_str)
533 473
 {
534
-	if(parse_from_header(msg)==-1 || msg->from==NULL || get_from(msg)==NULL) {
535
-		LM_ERR("cannot parse FROM header\n");
536
-		goto error;
537
-	}
538
-
539
-	if(parse_to_header(msg)==-1 || msg->to==NULL || get_to(msg)==NULL) {
540
-		LM_ERR("cannot parse To header\n");
541
-		goto error;
542
-	}
543
-
544
-	if(parse_headers(msg, HDR_CALLID_F, 0)!=0 || msg->callid==NULL
545
-			|| msg->callid->body.s==NULL) {
546
-		LM_ERR("cannot parse call-id\n");
547
-		goto error;
548
-	}
549
-
550
-	if(msg->cseq==NULL && ((parse_headers(msg, HDR_CSEQ_F, 0)==-1)
551
-				|| (msg->cseq==NULL)))
552
-	{
553
-		LM_ERR("cannot parse cseq\n");
554
-		goto error;
555
-	}
556
-
557
-	return 0;
558
-error:
559
-	return -1;
560
-}
561
-
562
-// Appends x-headers to the message in sto->body containing data from sto
563
-static int sip_trace_xheaders_write(struct _siptrace_data *sto)
564
-{
565
-	char* buf = NULL;
566
-	int bytes_written = 0;
567
-	char* eoh = NULL;
568
-	int eoh_offset = 0;
569
-	char* new_eoh = NULL;
570
-
571
-	if(xheaders_write_flag==NULL || *xheaders_write_flag==0)
572
-		return 0;
573
-
574
-	// Memory for the message with some additional headers.
575
-	// It gets free()ed in sip_trace_xheaders_free().
576
-	buf = pkg_malloc(sto->body.len + XHEADERS_BUFSIZE);
577
-	if (buf == NULL) {
578
-		LM_ERR("sip_trace_xheaders_write: out of memory\n");
579
-		return -1;
580
-	}
581
-
582
-	// Copy the whole message to buf first; it must be \0-terminated for
583
-	// strstr() to work. Then search for the end-of-header sequence.
584
-	memcpy(buf, sto->body.s, sto->body.len);
585
-	buf[sto->body.len] = '\0';
586
-	eoh = strstr(buf, "\r\n\r\n");
587
-	if (eoh == NULL) {
588
-		LM_ERR("sip_trace_xheaders_write: malformed message\n");
589
-		goto error;
590
-	}
591
-	eoh += 2; // the first \r\n belongs to the last header => skip it
592
-
593
-	// Write the new headers a the end-of-header position. This overwrites
594
-	// the \r\n terminating the old headers and the beginning of the message
595
-	// body. Both will be recovered later.
596
-	bytes_written = snprintf(eoh, XHEADERS_BUFSIZE,
597
-			"X-Siptrace-Fromip: %.*s\r\n"
598
-			"X-Siptrace-Toip: %.*s\r\n"
599
-			"X-Siptrace-Time: %llu %llu\r\n"
600
-			"X-Siptrace-Method: %.*s\r\n"
601
-			"X-Siptrace-Dir: %s\r\n",
602
-			sto->fromip.len, sto->fromip.s,
603
-			sto->toip.len, sto->toip.s,
604
-			(unsigned long long)sto->tv.tv_sec, (unsigned long long)sto->tv.tv_usec,
605
-			sto->method.len, sto->method.s,
606
-			sto->dir);
607
-	if (bytes_written >= XHEADERS_BUFSIZE) {
608
-		LM_ERR("sip_trace_xheaders_write: string too long\n");
609
-		goto error;
610
-	}
611
-
612
-	// Copy the \r\n terminating the old headers and the message body from the
613
-	// old buffer in sto->body.s to the new end-of-header in buf.
614
-	eoh_offset = eoh - buf;
615
-	new_eoh = eoh + bytes_written;
616
-	memcpy(new_eoh, sto->body.s + eoh_offset, sto->body.len - eoh_offset);
617
-
618
-	// Change sto to point to the new buffer.
619
-	sto->body.s = buf;
620
-	sto->body.len += bytes_written;
621
-	return 0;
622
-error:
623
-	if(buf != NULL)
624
-		pkg_free(buf);
625
-	return -1;
626
-}
627
-
628
-// Parses x-headers, saves the data back to sto, and removes the x-headers
629
-// from the message in sto->buf
630
-static int sip_trace_xheaders_read(struct _siptrace_data *sto)
631
-{
632
-	char* searchend = NULL;
633
-	char* eoh = NULL;
634
-	char* xheaders = NULL;
635
-	long long unsigned int tv_sec, tv_usec;
636
-
637
-	if(xheaders_read_flag==NULL || *xheaders_read_flag==0)
638
-		return 0;
639
-
640
-	// Find the end-of-header marker \r\n\r\n
641
-	searchend = sto->body.s + sto->body.len - 3;
642
-	eoh = memchr(sto->body.s, '\r', searchend - eoh);
643
-	while (eoh != NULL && eoh < searchend) {
644
-		if (memcmp(eoh, "\r\n\r\n", 4) == 0)
645
-			break;
646
-		eoh = memchr(eoh + 1, '\r', searchend - eoh);
647
-	}
648
-	if (eoh == NULL) {
649
-		LM_ERR("sip_trace_xheaders_read: malformed message\n");
650
-		return -1;
651
-	}
652
-
653
-	// Find x-headers: eoh will be overwritten by \0 to allow the use of
654
-	// strstr(). The byte at eoh will later be recovered, when the
655
-	// message body is shifted towards the beginning of the message
656
-	// to remove the x-headers.
657
-	*eoh = '\0';
658
-	xheaders = strstr(sto->body.s, "\r\nX-Siptrace-Fromip: ");
659
-	if (xheaders == NULL) {
660
-		LM_ERR("sip_trace_xheaders_read: message without x-headers "
661
-				"from %.*s, callid %.*s\n",
662
-				sto->fromip.len, sto->fromip.s, sto->callid.len, sto->callid.s);
663
-		return -1;
664
-	}
665
-
666
-	// Allocate memory for new strings in sto
667
-	// (gets free()ed in sip_trace_xheaders_free() )
668
-	sto->fromip.s = pkg_malloc(51);
669
-	sto->toip.s = pkg_malloc(51);
670
-	sto->method.s = pkg_malloc(51);
671
-	sto->dir = pkg_malloc(4);
672
-	if (!(sto->fromip.s && sto->toip.s && sto->method.s && sto->dir)) {
673
-		LM_ERR("sip_trace_xheaders_read: out of memory\n");
674
-		goto erroraftermalloc;
675
-	}
676
-
677
-	// Parse the x-headers: scanf()
678
-	if (sscanf(xheaders, "\r\n"
679
-				"X-Siptrace-Fromip: %50s\r\n"
680
-				"X-Siptrace-Toip: %50s\r\n"
681
-				"X-Siptrace-Time: %llu %llu\r\n"
682
-				"X-Siptrace-Method: %50s\r\n"
683
-				"X-Siptrace-Dir: %3s",
684
-				sto->fromip.s, sto->toip.s,
685
-				&tv_sec, &tv_usec,
686
-				sto->method.s,
687
-				sto->dir) == EOF) {
688
-		LM_ERR("sip_trace_xheaders_read: malformed x-headers\n");
689
-		goto erroraftermalloc;
690
-	}
691
-	sto->fromip.len = strlen(sto->fromip.s);
692
-	sto->toip.len = strlen(sto->toip.s);
693
-	sto->tv.tv_sec = (time_t)tv_sec;
694
-	sto->tv.tv_usec = (suseconds_t)tv_usec;
695
-	sto->method.len = strlen(sto->method.s);
696
-
697
-	// Remove the x-headers: the message body is shifted towards the beginning
698
-	// of the message, overwriting the x-headers. Before that, the byte at eoh
699
-	// is recovered.
700
-	*eoh = '\r';
701
-	memmove(xheaders, eoh, sto->body.len - (eoh - sto->body.s));
702
-	sto->body.len -= eoh - xheaders;
703
-
704
-	return 0;
705
-
706
-erroraftermalloc:
707
-	if (sto->fromip.s)
708
-		pkg_free(sto->fromip.s);
709
-	if (sto->toip.s)
710
-		pkg_free(sto->toip.s);
711
-	if (sto->method.s)
712
-		pkg_free(sto->method.s);
713
-	if (sto->dir)
714
-		pkg_free(sto->dir);
715
-	return -1;
716
-}
717
-
718
-// Frees the memory allocated by sip_trace_xheaders_{write,read}
719
-static int sip_trace_xheaders_free(struct _siptrace_data *sto)
720
-{
721
-	if (xheaders_write_flag != NULL && *xheaders_write_flag != 0) {
722
-		if(sto->body.s)
723
-			pkg_free(sto->body.s);
724
-	}
725
-
726
-	if (xheaders_read_flag != NULL && *xheaders_read_flag != 0) {
727
-		if(sto->fromip.s)
728
-			pkg_free(sto->fromip.s);
729
-		if(sto->toip.s)
730
-			pkg_free(sto->toip.s);
731
-		if(sto->dir)
732
-			pkg_free(sto->dir);
733
-	}
734
-
735
-	return 0;
736
-}
737
-
738
-static int sip_trace_store(struct _siptrace_data *sto, struct dest_info *dst, str *correlation_id_str)
739
-{
740
-	if(sto==NULL)
741
-	{
474
+	if(sto == NULL) {
742 475
 		LM_DBG("invalid parameter\n");
743 476
 		return -1;
744 477
 	}
745 478
 
746 479
 	gettimeofday(&sto->tv, NULL);
747 480
 
748
-	if (sip_trace_xheaders_read(sto) != 0)
481
+	if(sip_trace_xheaders_read(sto) != 0)
749 482
 		return -1;
750 483
 	int ret = sip_trace_store_db(sto);
751 484
 
752
-	if (sip_trace_xheaders_write(sto) != 0)
485
+	if(sip_trace_xheaders_write(sto) != 0)
753 486
 		return -1;
754 487
 
755
-	if(hep_mode_on) trace_send_hep_duplicate(&sto->body, &sto->fromip, &sto->toip, dst, correlation_id_str);
756
-	else trace_send_duplicate(sto->body.s, sto->body.len, dst);
488
+	if(hep_mode_on)
489
+		trace_send_hep_duplicate(
490
+				&sto->body, &sto->fromip, &sto->toip, dst, correlation_id_str);
491
+	else
492
+		trace_send_duplicate(sto->body.s, sto->body.len, dst);
757 493
 
758
-	if (sip_trace_xheaders_free(sto) != 0)
494
+	if(sip_trace_xheaders_free(sto) != 0)
759 495
 		return -1;
760 496
 
761 497
 	return ret;
... ...
@@ -763,12 +499,12 @@ static int sip_trace_store(struct _siptrace_data *sto, struct dest_info *dst, st
763 499
 
764 500
 static int sip_trace_store_db(struct _siptrace_data *sto)
765 501
 {
766
-	if(db_con==NULL) {
502
+	if(db_con == NULL) {
767 503
 		LM_DBG("database connection not initialized\n");
768 504
 		return -1;
769 505
 	}
770 506
 
771
-	if(trace_to_database_flag==NULL || *trace_to_database_flag==0)
507
+	if(trace_to_database_flag == NULL || *trace_to_database_flag == 0)
772 508
 		goto done;
773 509
 
774 510
 	db_key_t db_keys[NR_KEYS];
... ...
@@ -835,14 +571,13 @@ static int sip_trace_store_db(struct _siptrace_data *sto)
835 571
 
836 572
 	db_funcs.use_table(db_con, siptrace_get_table());
837 573
 
838
-	if(trace_on_flag!=NULL && *trace_on_flag!=0) {
839
-		db_vals[9].val.str_val.s   = "";
574
+	if(trace_on_flag != NULL && *trace_on_flag != 0) {
575
+		db_vals[9].val.str_val.s = "";
840 576
 		db_vals[9].val.str_val.len = 0;
841 577
 
842 578
 		LM_DBG("storing info...\n");
843
-		if(trace_delayed!=0 && db_funcs.insert_delayed!=NULL)
844
-		{
845
-			if(db_funcs.insert_delayed(db_con, db_keys, db_vals, NR_KEYS)<0) {
579
+		if(trace_delayed != 0 && db_funcs.insert_delayed != NULL) {
580
+			if(db_funcs.insert_delayed(db_con, db_keys, db_vals, NR_KEYS) < 0) {
846 581
 				LM_ERR("error storing trace\n");
847 582
 				goto error;
848 583
 			}
... ...
@@ -857,14 +592,13 @@ static int sip_trace_store_db(struct _siptrace_data *sto)
857 592
 #endif
858 593
 	}
859 594
 
860
-	if(sto->avp==NULL)
595
+	if(sto->avp == NULL)
861 596
 		goto done;
862 597
 
863 598
 	db_vals[9].val.str_val = sto->avp_value.s;
864 599
 
865 600
 	LM_DBG("storing info...\n");
866
-	if(trace_delayed!=0 && db_funcs.insert_delayed!=NULL)
867
-	{
601
+	if(trace_delayed != 0 && db_funcs.insert_delayed != NULL) {
868 602
 		if(db_funcs.insert_delayed(db_con, db_keys, db_vals, NR_KEYS) < 0) {
869 603
 			LM_ERR("error storing trace\n");
870 604
 			goto error;
... ...
@@ -877,12 +611,11 @@ static int sip_trace_store_db(struct _siptrace_data *sto)
877 611
 	}
878 612
 
879 613
 	sto->avp = search_next_avp(&sto->state, &sto->avp_value);
880
-	while(sto->avp!=NULL) {
614
+	while(sto->avp != NULL) {
881 615
 		db_vals[9].val.str_val = sto->avp_value.s;
882 616
 
883 617
 		LM_DBG("storing info...\n");
884
-		if(trace_delayed!=0 && db_funcs.insert_delayed!=NULL)
885
-		{
618
+		if(trace_delayed != 0 && db_funcs.insert_delayed != NULL) {
886 619
 			if(db_funcs.insert_delayed(db_con, db_keys, db_vals, NR_KEYS) < 0) {
887 620
 				LM_ERR("error storing trace\n");
888 621
 				goto error;
... ...
@@ -902,32 +635,31 @@ error:
902 635
 	return -1;
903 636
 }
904 637
 
905
-static int fixup_siptrace(void** param, int param_no) {
638
+static int fixup_siptrace(void **param, int param_no)
639
+{
906 640
 	char *duri;
907 641
 	struct sip_uri uri;
908 642
 	struct dest_info *dst = NULL;
909
-	struct proxy_l * p = NULL;
910
-	str dup_uri_str = { 0, 0 };
643
+	struct proxy_l *p = NULL;
644
+	str dup_uri_str = {0, 0};
911 645
 
912
-	if (param_no != 1) {
913
-		LM_DBG("params:%s\n", (char*)*param);
646
+	if(param_no != 1) {
647
+		LM_DBG("params:%s\n", (char *)*param);
914 648
 		return 0;
915 649
 	}
916 650
 
917
-	if (((char*)(*param))[0] == '\0') {
651
+	if(((char *)(*param))[0] == '\0') {
918 652
 		// Empty URI, use the URI set at module level (dup_uri)
919
-		if (dup_uri) {
653
+		if(dup_uri) {
920 654
 			uri = *dup_uri;
921
-		}
922
-		else {
655
+		} else {
923 656
 			LM_ERR("Missing duplicate URI\n");
924 657
 			return -1;
925 658
 		}
926
-	}
927
-	else {
928
-		duri = (char*) *param;
659
+	} else {
660
+		duri = (char *)*param;
929 661
 
930
-		if (!(*duri)) {
662
+		if(!(*duri)) {
931 663
 			LM_ERR("invalid dup URI\n");
932 664
 			return -1;
933 665
 		}
... ...
@@ -938,23 +670,22 @@ static int fixup_siptrace(void** param, int param_no) {
938 670
 		dup_uri_str.len = strlen(dup_uri_str.s);
939 671
 		memset(&uri, 0, sizeof(struct sip_uri));
940 672
 
941
-		if (parse_uri(dup_uri_str.s, dup_uri_str.len, &uri) < 0) {
673
+		if(parse_uri(dup_uri_str.s, dup_uri_str.len, &uri) < 0) {
942 674
 			LM_ERR("bad dup uri\n");
943 675
 			return -1;
944 676
 		}
945 677
 	}
946 678
 
947
-	dst = (struct dest_info *) pkg_malloc(sizeof(struct dest_info));
948
-	if (dst == 0) {
679
+	dst = (struct dest_info *)pkg_malloc(sizeof(struct dest_info));
680
+	if(dst == 0) {
949 681
 		LM_ERR("no more pkg memory left\n");
950 682
 		return -1;
951 683
 	}
952 684
 	init_dest_info(dst);
953 685
 	/* create a temporary proxy*/
954 686
 	dst->proto = PROTO_UDP;
955
-	p = mk_proxy(&uri.host, (uri.port_no) ? uri.port_no : SIP_PORT,
956
-			dst->proto);
957
-	if (p == 0) {
687
+	p = mk_proxy(&uri.host, (uri.port_no) ? uri.port_no : SIP_PORT, dst->proto);
688
+	if(p == 0) {
958 689
 		LM_ERR("bad host name in uri\n");
959 690
 		pkg_free(dst);
960 691
 		return -1;
... ...
@@ -963,12 +694,12 @@ static int fixup_siptrace(void** param, int param_no) {
963 694
 
964 695
 	pkg_free(*param);
965 696
 	/* free temporary proxy*/
966
-	if (p) {
697
+	if(p) {
967 698
 		free_proxy(p); /* frees only p content, not p itself */
968 699
 		pkg_free(p);
969 700
 	}
970 701
 
971
-	*param = (void*) dst;
702
+	*param = (void *)dst;
972 703
 	return 0;
973 704
 }
974 705
 
... ...
@@ -976,35 +707,34 @@ static int sip_trace2(struct sip_msg *msg, char *dest, char *correlation_id)
976 707
 {
977 708
 	struct dest_info *dst = NULL;
978 709
 	struct sip_uri uri;
979
-	struct proxy_l * p = NULL;
980
-	str dup_uri_str = { 0, 0 };
981
-	str correlation_id_str = {0, 0};;
710
+	struct proxy_l *p = NULL;
711
+	str dup_uri_str = {0, 0};
712
+	str correlation_id_str = {0, 0};
713
+	;
982 714
 
983
-	if(fixup_get_svalue(msg, (gparam_t*)dest, &dup_uri_str)!=0) {
715
+	if(fixup_get_svalue(msg, (gparam_t *)dest, &dup_uri_str) != 0) {
984 716
 		LM_ERR("unable to parse the dest URI string\n");
985 717
 		return -1;
986 718
 	}
987 719
 
988 720
 	// If the dest is empty, use the module parameter, if set
989
-	if (dup_uri_str.len == 0) {
990
-		if (dup_uri) {
721
+	if(dup_uri_str.len == 0) {
722
+		if(dup_uri) {
991 723
 			uri = *dup_uri;
992
-		}
993
-		else {
724
+		} else {
994 725
 			LM_ERR("Missing duplicate URI\n");
995 726
 			return -1;
996 727
 		}
997
-	}
998
-	else {
728
+	} else {
999 729
 		memset(&uri, 0, sizeof(struct sip_uri));
1000
-		if (parse_uri(dup_uri_str.s, dup_uri_str.len, &uri) < 0) {
730
+		if(parse_uri(dup_uri_str.s, dup_uri_str.len, &uri) < 0) {
1001 731
 			LM_ERR("bad dup uri\n");
1002 732
 			return -1;
1003 733
 		}
1004 734
 	}
1005 735
 
1006 736
 	dst = (struct dest_info *)pkg_malloc(sizeof(struct dest_info));
1007
-	if (dst == 0) {
737
+	if(dst == 0) {
1008 738
 		LM_ERR("no more pkg memory left\n");
1009 739
 		return -1;
1010 740
 	}
... ...
@@ -1012,9 +742,8 @@ static int sip_trace2(struct sip_msg *msg, char *dest, char *correlation_id)
1012 742
 
1013 743
 	/* create a temporary proxy*/
1014 744
 	dst->proto = PROTO_UDP;
1015
-	p = mk_proxy(&uri.host, (uri.port_no) ? uri.port_no : SIP_PORT,
1016
-			dst->proto);
1017
-	if (p == 0) {
745
+	p = mk_proxy(&uri.host, (uri.port_no) ? uri.port_no : SIP_PORT, dst->proto);
746
+	if(p == 0) {
1018 747
 		LM_ERR("bad host name in uri\n");
1019 748
 		pkg_free(dst);
1020 749
 		return -1;
... ...
@@ -1023,12 +752,13 @@ static int sip_trace2(struct sip_msg *msg, char *dest, char *correlation_id)
1023 752
 	hostent2su(&dst->to, &p->host, p->addr_idx, (p->port) ? p->port : SIP_PORT);
1024 753
 
1025 754
 	/* free temporary proxy*/
1026
-	if (p) {
755
+	if(p) {
1027 756
 		free_proxy(p); /* frees only p content, not p itself */
1028 757
 		pkg_free(p);
1029 758
 	}
1030 759
 
1031
-	if(fixup_get_svalue(msg, (gparam_t*)correlation_id, &correlation_id_str)!=0) {
760
+	if(fixup_get_svalue(msg, (gparam_t *)correlation_id, &correlation_id_str)
761
+			!= 0) {
1032 762
 		LM_ERR("unable to parse the correlation id\n");
1033 763
 		return -1;
1034 764
 	}
... ...
@@ -1036,92 +766,96 @@ static int sip_trace2(struct sip_msg *msg, char *dest, char *correlation_id)
1036 766
 	return sip_trace(msg, dst, &correlation_id_str, NULL);
1037 767
 }
1038 768
 
1039
-static int sip_trace(struct sip_msg *msg, struct dest_info * dst, str *correlation_id_str, char *dir)
769
+static int sip_trace(struct sip_msg *msg, struct dest_info *dst,
770
+		str *correlation_id_str, char *dir)
1040 771
 {
1041 772
 	struct _siptrace_data sto;
1042 773
 	struct onsend_info *snd_inf = NULL;
1043 774
 
1044
-	if (dst){
1045
-		if (dst->send_sock == 0){
1046
-			dst->send_sock=get_send_socket(0, &dst->to, dst->proto);
1047
-			if (dst->send_sock==0){
775
+	if(dst) {
776
+		if(dst->send_sock == 0) {
777
+			dst->send_sock = get_send_socket(0, &dst->to, dst->proto);
778
+			if(dst->send_sock == 0) {
1048 779
 				LM_ERR("can't forward to af %d, proto %d no corresponding"
1049
-						" listening socket\n", dst->to.s.sa_family, dst->proto);
780
+					   " listening socket\n",
781
+						dst->to.s.sa_family, dst->proto);
1050 782
 				return -1;
1051 783
 			}
1052 784
 		}
1053 785
 	}
1054 786
 
1055
-	if(msg==NULL) {
787
+	if(msg == NULL) {
1056 788
 		LM_DBG("nothing to trace\n");
1057 789
 		return -1;
1058 790
 	}
1059 791
 	memset(&sto, 0, sizeof(struct _siptrace_data));
1060 792
 
1061
-	if(traced_user_avp.n!=0)
1062
-		sto.avp=search_first_avp(traced_user_avp_type, traced_user_avp,
793
+	if(traced_user_avp.n != 0)
794
+		sto.avp = search_first_avp(traced_user_avp_type, traced_user_avp,
1063 795
 				&sto.avp_value, &sto.state);
1064 796
 
1065
-	if((sto.avp==NULL) && (trace_on_flag==NULL || *trace_on_flag==0)) {
797
+	if((sto.avp == NULL) && (trace_on_flag == NULL || *trace_on_flag == 0)) {
1066 798
 		LM_DBG("trace off...\n");
1067 799
 		return -1;
1068 800
 	}
1069
-	if(sip_trace_prepare(msg)<0)
801
+	if(sip_trace_prepare(msg) < 0)
1070 802
 		return -1;
1071 803
 
1072 804
 	sto.callid = msg->callid->body;
1073 805
 
1074
-	if(msg->first_line.type==SIP_REQUEST) {
806
+	if(msg->first_line.type == SIP_REQUEST) {
1075 807
 		sto.method = msg->first_line.u.request.method;
1076 808
 	} else {
1077
-		if(parse_headers(msg, HDR_CSEQ_F, 0) != 0 || msg->cseq==NULL
1078
-				|| msg->cseq->parsed==NULL) {
809
+		if(parse_headers(msg, HDR_CSEQ_F, 0) != 0 || msg->cseq == NULL
810
+				|| msg->cseq->parsed == NULL) {
1079 811
 			LM_ERR("cannot parse cseq header\n");
1080 812
 			return -1;
1081 813
 		}
1082 814
 		sto.method = get_cseq(msg)->method;
1083 815
 	}
1084 816
 
1085
-	if(msg->first_line.type==SIP_REPLY) {
817
+	if(msg->first_line.type == SIP_REPLY) {
1086 818
 		sto.status = msg->first_line.u.reply.status;
1087 819
 	} else {
1088 820
 		sto.status.s = "";
1089 821
 		sto.status.len = 0;
1090 822
 	}
1091 823
 
1092
-	snd_inf=get_onsend_info();
1093
-	if(snd_inf==NULL) {
824
+	snd_inf = get_onsend_info();
825
+	if(snd_inf == NULL) {
1094 826
 		sto.body.s = msg->buf;
1095 827
 		sto.body.len = msg->len;
1096 828
 
1097
-		sto.dir = (dir)?dir:"in";
829
+		sto.dir = (dir) ? dir : "in";
1098 830
 
1099
-		if (trace_local_ip.s && trace_local_ip.len > 0 && strncmp(sto.dir, "out", 3) == 0) {
831
+		if(trace_local_ip.s && trace_local_ip.len > 0
832
+				&& strncmp(sto.dir, "out", 3) == 0) {
1100 833
 			sto.fromip = trace_local_ip;
1101 834
 		} else {
1102 835
 			siptrace_copy_proto(msg->rcv.proto, sto.fromip_buff);
1103 836
 			strcat(sto.fromip_buff, ip_addr2a(&msg->rcv.src_ip));
1104
-			strcat(sto.fromip_buff,":");
837
+			strcat(sto.fromip_buff, ":");
1105 838
 			strcat(sto.fromip_buff, int2str(msg->rcv.src_port, NULL));
1106 839
 			sto.fromip.s = sto.fromip_buff;
1107 840
 			sto.fromip.len = strlen(sto.fromip_buff);
1108 841
 		}
1109 842
 
1110
-		if (trace_local_ip.s && trace_local_ip.len > 0 && strncmp(sto.dir, "in", 2) == 0) {
843
+		if(trace_local_ip.s && trace_local_ip.len > 0
844
+				&& strncmp(sto.dir, "in", 2) == 0) {
1111 845
 			sto.toip = trace_local_ip;
1112 846
 		} else {
1113 847
 			siptrace_copy_proto(msg->rcv.proto, sto.toip_buff);
1114 848
 			strcat(sto.toip_buff, ip_addr2a(&msg->rcv.dst_ip));
1115
-			strcat(sto.toip_buff,":");
849
+			strcat(sto.toip_buff, ":");
1116 850
 			strcat(sto.toip_buff, int2str(msg->rcv.dst_port, NULL));
1117 851
 			sto.toip.s = sto.toip_buff;
1118 852
 			sto.toip.len = strlen(sto.toip_buff);
1119 853
 		}
1120 854
 	} else {
1121
-		sto.body.s   = snd_inf->buf;
855
+		sto.body.s = snd_inf->buf;
1122 856
 		sto.body.len = snd_inf->len;
1123 857
 
1124
-		if (trace_local_ip.s && trace_local_ip.len > 0) {
858
+		if(trace_local_ip.s && trace_local_ip.len > 0) {
1125 859
 			sto.fromip = trace_local_ip;
1126 860
 		} else {
1127 861
 			strncpy(sto.fromip_buff, snd_inf->send_sock->sock_str.s,
... ...
@@ -1132,7 +866,7 @@ static int sip_trace(struct sip_msg *msg, struct dest_info * dst, str *correlati
1132 866
 
1133 867
 		siptrace_copy_proto(snd_inf->send_sock->proto, sto.toip_buff);
1134 868
 		strcat(sto.toip_buff, suip2a(snd_inf->to, sizeof(*snd_inf->to)));
1135
-		strcat(sto.toip_buff,":");
869
+		strcat(sto.toip_buff, ":");
1136 870
 		strcat(sto.toip_buff, int2str((int)su_getport(snd_inf->to), NULL));
1137 871
 		sto.toip.s = sto.toip_buff;
1138 872
 		sto.toip.len = strlen(sto.toip_buff);
... ...
@@ -1144,7 +878,7 @@ static int sip_trace(struct sip_msg *msg, struct dest_info * dst, str *correlati
1144 878
 	sto.totag = get_to(msg)->tag_value;
1145 879
 
1146 880
 #ifdef STATISTICS
1147
-	if(msg->first_line.type==SIP_REPLY) {
881
+	if(msg->first_line.type == SIP_REPLY) {
1148 882
 		sto.stat = siptrace_rpl;
1149 883
 	} else {
1150 884
 		sto.stat = siptrace_req;
... ...
@@ -1153,71 +887,65 @@ static int sip_trace(struct sip_msg *msg, struct dest_info * dst, str *correlati
1153 887
 	return sip_trace_store(&sto, dst, correlation_id_str);
1154 888
 }
1155 889
 
1156
-#define trace_is_off(_msg) \
1157
-	(trace_on_flag==NULL || *trace_on_flag==0 || \
1158
-	 ((_msg)->flags&trace_flag)==0)
890
+#define trace_is_off(_msg)                        \
891
+	(trace_on_flag == NULL || *trace_on_flag == 0 \
892
+			|| ((_msg)->flags & trace_flag) == 0)
1159 893
 
1160
-static void trace_onreq_in(struct cell* t, int type, struct tmcb_params *ps)
894
+static void trace_onreq_in(struct cell *t, int type, struct tmcb_params *ps)
1161 895
 {
1162
-	struct sip_msg* msg;
1163
-	int_str         avp_value;
1164
-	struct usr_avp* avp;
896
+	struct sip_msg *msg;
897
+	int_str avp_value;
898
+	struct usr_avp *avp;
1165 899
 
1166
-	if(t==NULL || ps==NULL)
1167
-	{
900
+	if(t == NULL || ps == NULL) {
1168 901
 		LM_DBG("no uas request, local transaction\n");
1169 902
 		return;
1170 903
 	}
1171 904
 
1172 905
 	msg = ps->req;
1173
-	if(msg==NULL)
1174
-	{
906
+	if(msg == NULL) {
1175 907
 		LM_DBG("no uas request, local transaction\n");
1176 908
 		return;
1177 909
 	}
1178 910
 
1179 911
 	avp = NULL;
1180
-	if(traced_user_avp.n!=0)
1181
-		avp=search_first_avp(traced_user_avp_type, traced_user_avp, &avp_value,
1182
-				0);
912
+	if(traced_user_avp.n != 0)
913
+		avp = search_first_avp(
914
+				traced_user_avp_type, traced_user_avp, &avp_value, 0);
1183 915
 
1184
-	if((avp==NULL) && trace_is_off(msg))
1185
-	{
916
+	if((avp == NULL) && trace_is_off(msg)) {
1186 917
 		LM_DBG("trace off...\n");
1187 918
 		return;
1188 919
 	}
1189 920
 
1190
-	if(parse_from_header(msg)==-1 || msg->from==NULL || get_from(msg)==NULL)
1191
-	{
921
+	if(parse_from_header(msg) == -1 || msg->from == NULL
922
+			|| get_from(msg) == NULL) {
1192 923
 		LM_ERR("cannot parse FROM header\n");
1193 924
 		return;
1194 925
 	}
1195 926
 
1196
-	if(parse_headers(msg, HDR_CALLID_F, 0)!=0)
1197
-	{
927
+	if(parse_headers(msg, HDR_CALLID_F, 0) != 0) {
1198 928
 		LM_ERR("cannot parse call-id\n");
1199 929
 		return;
1200 930
 	}
1201 931
 
1202
-	if(tmb.register_tmcb(0, t, TMCB_REQUEST_SENT, trace_onreq_out, 0, 0) <=0)
1203
-	{
932
+	if(tmb.register_tmcb(0, t, TMCB_REQUEST_SENT, trace_onreq_out, 0, 0) <= 0) {
1204 933
 		LM_ERR("can't register trace_onreq_out\n");
1205 934
 		return;
1206 935
 	}
1207
-	if(tmb.register_tmcb(0, t, TMCB_RESPONSE_IN, trace_onreply_in, 0, 0) <=0)
1208
-	{
936
+	if(tmb.register_tmcb(0, t, TMCB_RESPONSE_IN, trace_onreply_in, 0, 0) <= 0) {
1209 937
 		LM_ERR("can't register trace_onreply_in\n");
1210 938
 		return;
1211 939
 	}
1212 940
 
1213
-	if(tmb.register_tmcb(0, t, TMCB_RESPONSE_SENT, trace_onreply_out, 0, 0)<=0)
1214
-	{
941
+	if(tmb.register_tmcb(0, t, TMCB_RESPONSE_SENT, trace_onreply_out, 0, 0)
942
+			<= 0) {
1215 943
 		LM_ERR("can't register trace_onreply_out\n");
1216 944
 		return;
1217 945
 	}
1218 946
 }
1219 947
 
1220
-static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
948
+static void trace_onreq_out(struct cell *t, int type, struct tmcb_params *ps)
1221 949
 {
1222 950
 	struct _siptrace_data sto;
1223 951
 	sip_msg_t *msg;
... ...
@@ -1225,23 +953,23 @@ static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
1225 953
 	int len;
1226 954
 	struct dest_info *dst;
1227 955
 
1228
-	if(t==NULL || ps==NULL) {
956
+	if(t == NULL || ps == NULL) {
1229 957
 		LM_DBG("very weird\n");
1230 958
 		return;
1231 959
 	}
1232 960
 
1233
-	if(ps->flags&TMCB_RETR_F) {
961
+	if(ps->flags & TMCB_RETR_F) {
1234 962
 		LM_DBG("retransmission\n");
1235 963
 		return;
1236 964
 	}
1237
-	msg=ps->req;
1238
-	if(msg==NULL) {
965
+	msg = ps->req;
966
+	if(msg == NULL) {
1239 967
 		/* check if it is outgoing cancel, t is INVITE
1240 968
 		 * and send_buf starts with CANCEL */
1241
-		if(is_invite(t) && ps->send_buf.len>7
1242
-				&& strncmp(ps->send_buf.s, "CANCEL ", 7)==0) {
969
+		if(is_invite(t) && ps->send_buf.len > 7
970
+				&& strncmp(ps->send_buf.s, "CANCEL ", 7) == 0) {
1243 971
 			msg = t->uas.request;
1244
-			if(msg==NULL) {
972
+			if(msg == NULL) {
1245 973
 				LM_DBG("no uas msg for INVITE transaction\n");
1246 974
 				return;
1247 975
 			} else {
... ...
@@ -1254,36 +982,36 @@ static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
1254 982
 	}
1255 983
 	memset(&sto, 0, sizeof(struct _siptrace_data));
1256 984
 
1257
-	if(traced_user_avp.n!=0)
1258
-		sto.avp=search_first_avp(traced_user_avp_type, traced_user_avp,
985
+	if(traced_user_avp.n != 0)
986
+		sto.avp = search_first_avp(traced_user_avp_type, traced_user_avp,
1259 987
 				&sto.avp_value, &sto.state);
1260 988
 
1261
-	if((sto.avp==NULL) && trace_is_off(msg) ) {
989
+	if((sto.avp == NULL) && trace_is_off(msg)) {
1262 990
 		LM_DBG("trace off...\n");
1263 991
 		return;
1264 992
 	}
1265 993
 
1266
-	if(sip_trace_prepare(msg)<0)
994
+	if(sip_trace_prepare(msg) < 0)
1267 995
 		return;
1268 996
 
1269
-	if(ps->send_buf.len>0) {
997
+	if(ps->send_buf.len > 0) {
1270 998
 		sto.body = ps->send_buf;
1271 999
 	} else {
1272
-		sto.body.s   = "No request buffer";
1273
-		sto.body.len = sizeof("No request buffer")-1;
1000
+		sto.body.s = "No request buffer";
1001
+		sto.body.len = sizeof("No request buffer") - 1;
1274 1002
 	}
1275 1003
 
1276 1004
 	sto.callid = msg->callid->body;
1277 1005
 
1278
-	if(ps->send_buf.len>10) {
1006
+	if(ps->send_buf.len > 10) {
1279 1007
 		sto.method.s = ps->send_buf.s;
1280 1008
 		sto.method.len = 0;
1281
-		while(sto.method.len<ps->send_buf.len) {
1282
-			if(ps->send_buf.s[sto.method.len]==' ')
1009
+		while(sto.method.len < ps->send_buf.len) {
1010
+			if(ps->send_buf.s[sto.method.len] == ' ')
1283 1011
 				break;
1284 1012
 			sto.method.len++;
1285 1013
 		}
1286
-		if(sto.method.len==ps->send_buf.len)
1014
+		if(sto.method.len == ps->send_buf.len)
1287 1015
 			sto.method.len = 10;
1288 1016
 	} else {
1289 1017
 		sto.method = t->method;
... ...
@@ -1295,13 +1023,13 @@ static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
1295 1023
 	memset(&to_ip, 0, sizeof(struct ip_addr));
1296 1024
 	dst = ps->dst;
1297 1025
 
1298
-	if (trace_local_ip.s && trace_local_ip.len > 0) {
1026
+	if(trace_local_ip.s && trace_local_ip.len > 0) {
1299 1027
 		sto.fromip = trace_local_ip;
1300 1028
 	} else {
1301
-		if(dst==0 || dst->send_sock==0 || dst->send_sock->sock_str.s==0) {
1029
+		if(dst == 0 || dst->send_sock == 0 || dst->send_sock->sock_str.s == 0) {
1302 1030
 			siptrace_copy_proto(msg->rcv.proto, sto.fromip_buff);
1303 1031
 			strcat(sto.fromip_buff, ip_addr2a(&msg->rcv.dst_ip));
1304
-			strcat(sto.fromip_buff,":");
1032
+			strcat(sto.fromip_buff, ":");
1305 1033
 			strcat(sto.fromip_buff, int2str(msg->rcv.dst_port, NULL));
1306 1034
 			sto.fromip.s = sto.fromip_buff;
1307 1035
 			sto.fromip.len = strlen(sto.fromip_buff);
... ...
@@ -1310,7 +1038,7 @@ static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
1310 1038
 		}
1311 1039
 	}
1312 1040
 
1313
-	if(dst==0) {
1041
+	if(dst == 0) {
1314 1042
 		sto.toip.s = "any:255.255.255.255";
1315 1043
 		sto.toip.len = 19;
1316 1044
 	} else {
... ...
@@ -1337,36 +1065,36 @@ static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
1337 1065
 	return;
1338 1066
 }
1339 1067
 
1340
-static void trace_onreply_in(struct cell* t, int type, struct tmcb_params *ps)
1068
+static void trace_onreply_in(struct cell *t, int type, struct tmcb_params *ps)
1341 1069
 {
1342 1070
 	struct _siptrace_data sto;
1343 1071
 	sip_msg_t *msg;
1344 1072
 	sip_msg_t *req;
1345 1073
 	char statusbuf[8];
1346 1074