浏览代码

siptrace: wrap SCTP values within USE_SCTP

- make the module compile on no SCTP support OS
- fixed some compile warnings
Daniel-Constantin Mierla 14 年之前
父节点
当前提交
c5af235680
共有 1 个文件被更改,包括 264 次插入259 次删除
  1. 264 259
      modules_k/siptrace/siptrace.c

+ 264 - 259
modules_k/siptrace/siptrace.c

@@ -100,7 +100,7 @@ static void trace_sl_onreply_out(sl_cbp_t *slcb);
 static void trace_sl_ack_in(sl_cbp_t *slcb);
 
 static int trace_send_hep_duplicate(str *body, str *from, str *to);
-static int pipport2su (char *pipport, union sockaddr_union *tmp_su, unsigned int proto);
+static int pipport2su (char *pipport, union sockaddr_union *tmp_su, unsigned int *proto);
 
 
 static struct mi_root* sip_trace_mi(struct mi_root* cmd, void* param );
@@ -285,38 +285,38 @@ static int mod_init(void)
 		return -1;
 	}
 	trace_flag = 1<<trace_flag;
-	
+
 	trace_to_database_flag = (int*)shm_malloc(sizeof(int));
 	if(trace_to_database_flag==NULL) {
 		LM_ERR("no more shm memory left\n");
 		return -1;
 	}
-	
+
 	*trace_to_database_flag = trace_to_database;
 
 	/* Find a database module if needed */
 	if(trace_to_database_flag!=NULL && *trace_to_database_flag!=0) {
-    	        if (db_bind_mod(&db_url, &db_funcs))
-        	{
-	        	LM_ERR("unable to bind database module\n");
-        		return -1;
-                }
-        	if (trace_to_database_flag && !DB_CAPABILITY(db_funcs, DB_CAP_INSERT))
-                {
-		        LM_ERR("database modules does not provide all functions needed"
-    				" by module\n");
-        		return -1;
-        	}
-        }
+		if (db_bind_mod(&db_url, &db_funcs))
+		{
+			LM_ERR("unable to bind database module\n");
+			return -1;
+		}
+		if (trace_to_database_flag && !DB_CAPABILITY(db_funcs, DB_CAP_INSERT))
+		{
+			LM_ERR("database modules does not provide all functions needed"
+					" by module\n");
+			return -1;
+		}
+	}
 
 	trace_on_flag = (int*)shm_malloc(sizeof(int));
 	if(trace_on_flag==NULL) {
 		LM_ERR("no more shm memory left\n");
 		return -1;
 	}
-	
+
 	*trace_on_flag = trace_on;
-		
+
 	xheaders_write_flag = (int*)shm_malloc(sizeof(int));
 	xheaders_read_flag = (int*)shm_malloc(sizeof(int));
 	if (!(xheaders_write_flag && xheaders_read_flag)) {
@@ -350,10 +350,10 @@ static int mod_init(void)
 
 	slcb.type = SLCB_REPLY_READY;
 	slcb.cbf  = trace_sl_onreply_out;
-    if (slb.register_cb(&slcb) != 0) {
-        LM_ERR("can't register for SLCB_REPLY_READY\n");
-        return -1;
-    }
+	if (slb.register_cb(&slcb) != 0) {
+		LM_ERR("can't register for SLCB_REPLY_READY\n");
+		return -1;
+	}
 
 	if(trace_sl_acks)
 	{
@@ -434,14 +434,14 @@ static int child_init(int rank)
 	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
 		return 0; /* do nothing for the main process */
 
-        if(trace_to_database_flag!=NULL && *trace_to_database_flag!=0) {
-        	db_con = db_funcs.init(&db_url);
-        	if (!db_con)
-        	{
-	        	LM_ERR("unable to connect to database. Please check configuration.\n");
-        		return -1;
-                }
-        }
+	if(trace_to_database_flag!=NULL && *trace_to_database_flag!=0) {
+		db_con = db_funcs.init(&db_url);
+		if (!db_con)
+		{
+			LM_ERR("unable to connect to database. Please check configuration.\n");
+			return -1;
+		}
+	}
 
 	return 0;
 }
@@ -449,14 +449,14 @@ static int child_init(int rank)
 
 static void destroy(void)
 {
-        if(trace_to_database_flag!=NULL && *trace_to_database_flag!=0) {
-        	if (db_con!=NULL)
-	        	db_funcs.close(db_con);
-        }
-        
-        if (trace_on_flag)
-	     shm_free(trace_on_flag);
-        
+	if(trace_to_database_flag!=NULL && *trace_to_database_flag!=0) {
+		if (db_con!=NULL)
+			db_funcs.close(db_con);
+	}
+
+	if (trace_on_flag)
+		shm_free(trace_on_flag);
+
 }
 
 static inline int siptrace_copy_proto(int proto, char *buf)
@@ -500,7 +500,7 @@ static int sip_trace_prepare(sip_msg_t *msg)
 		LM_ERR("cannot parse FROM header\n");
 		goto error;
 	}
-	
+
 	if(parse_headers(msg, HDR_CALLID_F, 0)!=0 || msg->callid==NULL
 			|| msg->callid->body.s==NULL) {
 		LM_ERR("cannot parse call-id\n");
@@ -547,16 +547,16 @@ static int sip_trace_xheaders_write(struct _siptrace_data *sto)
 	// the \r\n terminating the old headers and the beginning of the message
 	// body. Both will be recovered later.
 	bytes_written = snprintf(eoh, XHEADERS_BUFSIZE,
-		"X-Siptrace-Fromip: %.*s\r\n"
-		"X-Siptrace-Toip: %.*s\r\n"
-		"X-Siptrace-Time: %llu %llu\r\n"
-		"X-Siptrace-Method: %.*s\r\n"
-		"X-Siptrace-Dir: %s\r\n",
-		sto->fromip.len, sto->fromip.s,
-		sto->toip.len, sto->toip.s,
-		(unsigned long long)sto->tv.tv_sec, (unsigned long long)sto->tv.tv_usec,
-		sto->method.len, sto->method.s,
-		sto->dir);
+			"X-Siptrace-Fromip: %.*s\r\n"
+			"X-Siptrace-Toip: %.*s\r\n"
+			"X-Siptrace-Time: %llu %llu\r\n"
+			"X-Siptrace-Method: %.*s\r\n"
+			"X-Siptrace-Dir: %s\r\n",
+			sto->fromip.len, sto->fromip.s,
+			sto->toip.len, sto->toip.s,
+			(unsigned long long)sto->tv.tv_sec, (unsigned long long)sto->tv.tv_usec,
+			sto->method.len, sto->method.s,
+			sto->dir);
 	if (bytes_written >= XHEADERS_BUFSIZE) {
 		LM_ERR("sip_trace_xheaders_write: string too long\n");
 		goto error;
@@ -611,8 +611,8 @@ static int sip_trace_xheaders_read(struct _siptrace_data *sto)
 	xheaders = strstr(sto->body.s, "\r\nX-Siptrace-Fromip: ");
 	if (xheaders == NULL) {
 		LM_ERR("sip_trace_xheaders_read: message without x-headers "
-			"from %.*s, callid %.*s\n",
-			sto->fromip.len, sto->fromip.s, sto->callid.len, sto->callid.s);
+				"from %.*s, callid %.*s\n",
+				sto->fromip.len, sto->fromip.s, sto->callid.len, sto->callid.s);
 		return -1;
 	}
 
@@ -629,15 +629,15 @@ static int sip_trace_xheaders_read(struct _siptrace_data *sto)
 
 	// Parse the x-headers: scanf()
 	if (sscanf(xheaders, "\r\n"
-			"X-Siptrace-Fromip: %50s\r\n"
-			"X-Siptrace-Toip: %50s\r\n"
-			"X-Siptrace-Time: %llu %llu\r\n"
-			"X-Siptrace-Method: %50s\r\n"
-			"X-Siptrace-Dir: %3s",
-			sto->fromip.s, sto->toip.s,
-			&tv_sec, &tv_usec,
-			sto->method.s,
-			sto->dir) == EOF) {
+				"X-Siptrace-Fromip: %50s\r\n"
+				"X-Siptrace-Toip: %50s\r\n"
+				"X-Siptrace-Time: %llu %llu\r\n"
+				"X-Siptrace-Method: %50s\r\n"
+				"X-Siptrace-Dir: %3s",
+				sto->fromip.s, sto->toip.s,
+				&tv_sec, &tv_usec,
+				sto->method.s,
+				sto->dir) == EOF) {
 		LM_ERR("sip_trace_xheaders_read: malformed x-headers\n");
 		goto erroraftermalloc;
 	}
@@ -695,16 +695,16 @@ static int sip_trace_store(struct _siptrace_data *sto)
 		LM_DBG("invalid parameter\n");
 		return -1;
 	}
-	
+
 	gettimeofday(&sto->tv, NULL);
-	
+
 	if (sip_trace_xheaders_read(sto) != 0)
 		return -1;
 	int ret = sip_trace_store_db(sto);
 
 	if (sip_trace_xheaders_write(sto) != 0)
 		return -1;
-	
+
 	if(hep_mode_on) trace_send_hep_duplicate(&sto->body, &sto->fromip, &sto->toip);
 	else trace_send_duplicate(sto->body.s, sto->body.len);
 
@@ -718,7 +718,7 @@ static int sip_trace_store_db(struct _siptrace_data *sto)
 {
 	if(trace_to_database_flag==NULL || *trace_to_database_flag==0)
 		goto done;
-	
+
 	db_key_t db_keys[NR_KEYS];
 	db_val_t db_vals[NR_KEYS];
 
@@ -726,12 +726,12 @@ static int sip_trace_store_db(struct _siptrace_data *sto)
 	db_vals[0].type = DB1_BLOB;
 	db_vals[0].nul = 0;
 	db_vals[0].val.blob_val = sto->body;
-	
+
 	db_keys[1] = &callid_column;
 	db_vals[1].type = DB1_STR;
 	db_vals[1].nul = 0;
 	db_vals[1].val.str_val = sto->callid;
-	
+
 	db_keys[2] = &method_column;
 	db_vals[2].type = DB1_STR;
 	db_vals[2].nul = 0;
@@ -741,47 +741,47 @@ static int sip_trace_store_db(struct _siptrace_data *sto)
 	db_vals[3].type = DB1_STR;
 	db_vals[3].nul = 0;
 	db_vals[3].val.str_val = sto->status;
-		
+
 	db_keys[4] = &fromip_column;
 	db_vals[4].type = DB1_STR;
 	db_vals[4].nul = 0;
 	db_vals[4].val.str_val = sto->fromip;
-	
+
 	db_keys[5] = &toip_column;
 	db_vals[5].type = DB1_STR;
 	db_vals[5].nul = 0;
 	db_vals[5].val.str_val = sto->toip;
-	
+
 	db_keys[6] = &date_column;
 	db_vals[6].type = DB1_DATETIME;
 	db_vals[6].nul = 0;
 	db_vals[6].val.time_val = sto->tv.tv_sec;
-	
+
 	db_keys[7] = &direction_column;
 	db_vals[7].type = DB1_STRING;
 	db_vals[7].nul = 0;
 	db_vals[7].val.string_val = sto->dir;
-	
+
 	db_keys[8] = &fromtag_column;
 	db_vals[8].type = DB1_STR;
 	db_vals[8].nul = 0;
 	db_vals[8].val.str_val = sto->fromtag;
-	
+
 	db_keys[9] = &traced_user_column;
 	db_vals[9].type = DB1_STR;
 	db_vals[9].nul = 0;
-	
+
 	db_keys[10] = &time_us_column;
 	db_vals[10].type = DB1_INT;
 	db_vals[10].nul = 0;
 	db_vals[10].val.int_val = sto->tv.tv_usec;
-	
+
 	db_funcs.use_table(db_con, siptrace_get_table());
 
 	if(trace_on_flag!=NULL && *trace_on_flag!=0) {
 		db_vals[9].val.str_val.s   = "";
 		db_vals[9].val.str_val.len = 0;
-	
+
 		LM_DBG("storing info...\n");
 		if(db_funcs.insert(db_con, db_keys, db_vals, NR_KEYS) < 0) {
 			LM_ERR("error storing trace\n");
@@ -791,10 +791,10 @@ static int sip_trace_store_db(struct _siptrace_data *sto)
 		update_stat(sto->stat, 1);
 #endif
 	}
-	
+
 	if(sto->avp==NULL)
 		goto done;
-	
+
 	db_vals[9].val.str_val = sto->avp_value.s;
 
 	LM_DBG("storing info...\n");
@@ -912,7 +912,7 @@ static int sip_trace(struct sip_msg *msg, char *dir, char *s2)
 
 #define trace_is_off(_msg) \
 	(trace_on_flag==NULL || *trace_on_flag==0 || \
-		((_msg)->flags&trace_flag)==0)
+	 ((_msg)->flags&trace_flag)==0)
 
 static void trace_onreq_in(struct cell* t, int type, struct tmcb_params *ps)
 {
@@ -925,14 +925,14 @@ static void trace_onreq_in(struct cell* t, int type, struct tmcb_params *ps)
 		LM_DBG("no uas request, local transaction\n");
 		return;
 	}
-	
+
 	msg = ps->req;
 	if(msg==NULL)
 	{
 		LM_DBG("no uas request, local transaction\n");
 		return;
 	}
-	
+
 	avp = NULL;
 	if(traced_user_avp.n!=0)
 		avp=search_first_avp(traced_user_avp_type, traced_user_avp, &avp_value,
@@ -943,7 +943,7 @@ static void trace_onreq_in(struct cell* t, int type, struct tmcb_params *ps)
 		LM_DBG("trace off...\n");
 		return;
 	}
-	
+
 	if(parse_from_header(msg)==-1 || msg->from==NULL || get_from(msg)==NULL)
 	{
 		LM_ERR("cannot parse FROM header\n");
@@ -981,7 +981,7 @@ static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
 	struct ip_addr to_ip;
 	int len;
 	struct dest_info *dst;
-	
+
 	if(t==NULL || ps==NULL) {
 		LM_DBG("very weird\n");
 		return;
@@ -1016,7 +1016,7 @@ static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
 		sto.body.s   = "No request buffer";
 		sto.body.len = sizeof("No request buffer")-1;
 	}
-	
+
 	sto.callid = msg->callid->body;
 
 	if(ps->send_buf.len>10) {
@@ -1035,7 +1035,7 @@ static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
 
 	sto.status.s = "";
 	sto.status.len = 0;
-		
+
 	memset(&to_ip, 0, sizeof(struct ip_addr));
 	dst = ps->dst;
 
@@ -1053,7 +1053,7 @@ static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
 			sto.fromip = dst->send_sock->sock_str;
 		}
 	}
-	
+
 	if(dst==0) {
 		sto.toip.s = "any:255.255.255.255";
 		sto.toip.len = 19;
@@ -1067,15 +1067,15 @@ static void trace_onreq_out(struct cell* t, int type, struct tmcb_params *ps)
 		sto.toip.s = sto.toip_buff;
 		sto.toip.len = strlen(sto.toip_buff);
 	}
-	
+
 	sto.dir = "out";
-	
+
 	sto.fromtag = get_from(msg)->tag_value;
-	
+
 #ifdef STATISTICS
 	sto.stat = siptrace_req;
 #endif
-	
+
 	sip_trace_store(&sto);
 	return;
 }
@@ -1099,7 +1099,7 @@ static void trace_onreply_in(struct cell* t, int type, struct tmcb_params *ps)
 		return;
 	}
 	memset(&sto, 0, sizeof(struct _siptrace_data));
-	
+
 	if(traced_user_avp.n!=0)
 		sto.avp=search_first_avp(traced_user_avp_type, traced_user_avp,
 				&sto.avp_value, &sto.state);
@@ -1116,7 +1116,7 @@ static void trace_onreply_in(struct cell* t, int type, struct tmcb_params *ps)
 	sto.body.len = msg->len;
 
 	sto.callid = msg->callid->body;
-	
+
 	sto.method = t->method;
 
 	strcpy(statusbuf, int2str(ps->code, &sto.status.len));
@@ -1128,7 +1128,7 @@ static void trace_onreply_in(struct cell* t, int type, struct tmcb_params *ps)
 	strcat(sto.fromip_buff, int2str(msg->rcv.src_port, NULL));
 	sto.fromip.s = sto.fromip_buff;
 	sto.fromip.len = strlen(sto.fromip_buff);
-	
+
 	if(trace_local_ip.s && trace_local_ip.len > 0) {
 		sto.toip = trace_local_ip;
 	} else {
@@ -1139,14 +1139,14 @@ static void trace_onreply_in(struct cell* t, int type, struct tmcb_params *ps)
 		sto.toip.s = sto.toip_buff;
 		sto.toip.len = strlen(sto.toip_buff);
 	}
-	
+
 	sto.dir = "in";
-	
+
 	sto.fromtag = get_from(msg)->tag_value;
 #ifdef STATISTICS
 	sto.stat = siptrace_rpl;
 #endif
-	
+
 	sip_trace_store(&sto);
 	return;
 }
@@ -1166,7 +1166,7 @@ static void trace_onreply_out(struct cell* t, int type, struct tmcb_params *ps)
 		LM_DBG("no uas request, local transaction\n");
 		return;
 	}
-	
+
 	if(ps->flags&TMCB_RETR_F) {
 		LM_DBG("retransmission\n");
 		return;
@@ -1180,7 +1180,7 @@ static void trace_onreply_out(struct cell* t, int type, struct tmcb_params *ps)
 		LM_DBG("trace off...\n");
 		return;
 	}
-	
+
 	req = ps->req;
 	msg = ps->rpl;
 	if(msg==NULL || msg==FAKED_REPLY) {
@@ -1215,7 +1215,7 @@ static void trace_onreply_out(struct cell* t, int type, struct tmcb_params *ps)
 			sto.body.len = sizeof("No reply buffer")-1;
 		}
 	}
-	
+
 	sto.callid = msg->callid->body;
 	sto.method = t->method;
 
@@ -1229,10 +1229,10 @@ static void trace_onreply_out(struct cell* t, int type, struct tmcb_params *ps)
 		sto.fromip.s = sto.fromip_buff;
 		sto.fromip.len = strlen(sto.fromip_buff);
 	}
-	
+
 	strcpy(statusbuf, int2str(ps->code, &sto.status.len));
 	sto.status.s = statusbuf;
-		
+
 	memset(&to_ip, 0, sizeof(struct ip_addr));
 	dst = ps->dst;
 	if(dst==0) {
@@ -1248,10 +1248,10 @@ static void trace_onreply_out(struct cell* t, int type, struct tmcb_params *ps)
 		sto.toip.s = sto.toip_buff;
 		sto.toip.len = strlen(sto.toip_buff);
 	}
-	
+
 	sto.dir = "out";
 	sto.fromtag = get_from(msg)->tag_value;
-	
+
 #ifdef STATISTICS
 	sto.stat = siptrace_rpl;
 #endif
@@ -1284,7 +1284,7 @@ static void trace_sl_onreply_out(sl_cbp_t *slcbp)
 		return;
 	}
 	req = slcbp->req;
-	
+
 	memset(&sto, 0, sizeof(struct _siptrace_data));
 	if(traced_user_avp.n!=0)
 		sto.avp=search_first_avp(traced_user_avp_type, traced_user_avp,
@@ -1294,19 +1294,19 @@ static void trace_sl_onreply_out(sl_cbp_t *slcbp)
 		LM_DBG("trace off...\n");
 		return;
 	}
-	
+
 	msg = req;
 	faked = 1;
 
 	if(sip_trace_prepare(msg)<0)
 		return;
-	
+
 	sto.body.s = (slcbp->reply)?slcbp->reply->s:"";
 	sto.body.len = (slcbp->reply)?slcbp->reply->len:0;
-	
+
 	sto.callid = msg->callid->body;
 	sto.method = msg->first_line.u.request.method;
-		
+
 	if(trace_local_ip.len > 0) {
 		sto.fromip = trace_local_ip;
 	} else {
@@ -1320,7 +1320,7 @@ static void trace_sl_onreply_out(sl_cbp_t *slcbp)
 
 	strcpy(statusbuf, int2str(slcbp->code, &sto.status.len));
 	sto.status.s = statusbuf;
-		
+
 	memset(&to_ip, 0, sizeof(struct ip_addr));
 	if(slcbp->dst==0)
 	{
@@ -1336,14 +1336,14 @@ static void trace_sl_onreply_out(sl_cbp_t *slcbp)
 		sto.toip.s = sto.toip_buff;
 		sto.toip.len = strlen(sto.toip_buff);
 	}
-	
+
 	sto.dir = "out";
 	sto.fromtag = get_from(msg)->tag_value;
-	
+
 #ifdef STATISTICS
 	sto.stat = siptrace_rpl;
 #endif
-	
+
 	sip_trace_store(&sto);
 	return;
 }
@@ -1359,7 +1359,7 @@ static void trace_sl_onreply_out(sl_cbp_t *slcbp)
 static struct mi_root* sip_trace_mi(struct mi_root* cmd_tree, void* param )
 {
 	struct mi_node* node;
-	
+
 	struct mi_node *rpl; 
 	struct mi_root *rpl_tree ; 
 
@@ -1400,13 +1400,13 @@ static int trace_send_duplicate(char *buf, int len)
 {
 	struct dest_info dst;
 	struct proxy_l * p;
-	
+
 	if(buf==NULL || len <= 0)
 		return -1;
-	
+
 	if(dup_uri_str.s==0 || dup_uri==NULL)
 		return 0;
-	
+
 	init_dest_info(&dst);
 	/* create a temporary proxy*/
 	dst.proto = PROTO_UDP;
@@ -1417,9 +1417,9 @@ static int trace_send_duplicate(char *buf, int len)
 		LM_ERR("bad host name in uri\n");
 		return -1;
 	}
-	
+
 	hostent2su(&dst.to, &p->host, p->addr_idx, (p->port)?p->port:SIP_PORT);
-	
+
 	dst.send_sock=get_send_socket(0, &dst.to, dst.proto);
 	if (dst.send_sock==0)
 	{
@@ -1433,7 +1433,7 @@ static int trace_send_duplicate(char *buf, int len)
 		LM_ERR("cannot send duplicate message\n");
 		goto error;
 	}
-	
+
 	free_proxy(p); /* frees only p content, not p itself */
 	pkg_free(p);
 	return 0;
@@ -1445,70 +1445,70 @@ error:
 
 static int trace_send_hep_duplicate(str *body, str *from, str *to)
 {
-        struct dest_info dst;
-        struct proxy_l * p=NULL /* make gcc happy */;
-        void* buffer;
-        union sockaddr_union from_su;
-        union sockaddr_union to_su;
-        unsigned int len, buflen, proto;
-        struct hep_hdr hdr;
+	struct dest_info dst;
+	struct proxy_l * p=NULL /* make gcc happy */;
+	void* buffer = NULL;
+	union sockaddr_union from_su;
+	union sockaddr_union to_su;
+	unsigned int len, buflen, proto;
+	struct hep_hdr hdr;
 	struct hep_iphdr hep_ipheader;
 #if USE_IPV6
-        struct hep_ip6hdr hep_ip6header;
+	struct hep_ip6hdr hep_ip6header;
 #endif
 
-        if(body->s==NULL || body->len <= 0)
-                return -1;
+	if(body->s==NULL || body->len <= 0)
+		return -1;
 
-        if(dup_uri_str.s==0 || dup_uri==NULL)
-                return 0;
+	if(dup_uri_str.s==0 || dup_uri==NULL)
+		return 0;
 
 
-        /* message length */
-        len = body->len 
+	/* message length */
+	len = body->len 
 #if USE_IPV6
-                + sizeof(struct hep_ip6hdr)
+		+ sizeof(struct hep_ip6hdr)
 #else
-                + sizeof(struct hep_iphdr)          
+		+ sizeof(struct hep_iphdr)          
 #endif
-                + sizeof(struct hep_hdr);
-           
+		+ sizeof(struct hep_hdr);
 
-        /* The packet is too big for us */
-        if (unlikely(len>BUF_SIZE)){
-                goto error;
-        }
 
-        /* Convert proto:ip:port to sockaddress union SRC IP */
-        if (pipport2su(from->s, &from_su, proto)==-1 || (pipport2su(to->s, &to_su, proto)==-1))
-                goto error;
+	/* The packet is too big for us */
+	if (unlikely(len>BUF_SIZE)){
+		goto error;
+	}
+
+	/* Convert proto:ip:port to sockaddress union SRC IP */
+	if (pipport2su(from->s, &from_su, &proto)==-1 || (pipport2su(to->s, &to_su, &proto)==-1))
+		goto error;
 
 	/* check if from and to are in the same family*/
 	if(from_su.s.sa_family != to_su.s.sa_family) {
-                LOG(L_ERR, "ERROR: trace_send_hep_duplicate: interworking detected ?\n");
+		LOG(L_ERR, "ERROR: trace_send_hep_duplicate: interworking detected ?\n");
 		goto error;
 	}
 
-        init_dest_info(&dst);
-        /* create a temporary proxy*/
-        dst.proto = PROTO_UDP;
-        p=mk_proxy(&dup_uri->host, (dup_uri->port_no)?dup_uri->port_no:SIP_PORT,
-                        dst.proto);
-        if (p==0)
-        {
-                LM_ERR("bad host name in uri\n");
-                goto error;
-        }
+	init_dest_info(&dst);
+	/* create a temporary proxy*/
+	dst.proto = PROTO_UDP;
+	p=mk_proxy(&dup_uri->host, (dup_uri->port_no)?dup_uri->port_no:SIP_PORT,
+			dst.proto);
+	if (p==0)
+	{
+		LM_ERR("bad host name in uri\n");
+		goto error;
+	}
 
-        hostent2su(&dst.to, &p->host, p->addr_idx, (p->port)?p->port:SIP_PORT);
+	hostent2su(&dst.to, &p->host, p->addr_idx, (p->port)?p->port:SIP_PORT);
 
-        dst.send_sock=get_send_socket(0, &dst.to, dst.proto);
-        if (dst.send_sock==0)
-        {
-                LM_ERR("can't forward to af %d, proto %d no corresponding"
-                                " listening socket\n", dst.to.s.sa_family, dst.proto);
-                goto error;
-        }
+	dst.send_sock=get_send_socket(0, &dst.to, dst.proto);
+	if (dst.send_sock==0)
+	{
+		LM_ERR("can't forward to af %d, proto %d no corresponding"
+				" listening socket\n", dst.to.s.sa_family, dst.proto);
+		goto error;
+	}
 
 	/* Version && proto && length */
 	hdr.hp_l = sizeof(struct hep_hdr);
@@ -1517,7 +1517,7 @@ static int trace_send_hep_duplicate(str *body, str *from, str *to)
 
 	/* AND the last */
 	if (from_su.s.sa_family==AF_INET){
-        	/* prepare the hep headers */
+		/* prepare the hep headers */
 
 		hdr.hp_f = AF_INET;
 		hdr.hp_sport = htons(from_su.sin.sin_port);
@@ -1529,13 +1529,13 @@ static int trace_send_hep_duplicate(str *body, str *from, str *to)
 		len = sizeof(struct hep_iphdr);
 	}
 #ifdef USE_IPV6
-        else if (from_su.s.sa_family==AF_INET6){
-        	/* prepare the hep6 headers */
+	else if (from_su.s.sa_family==AF_INET6){
+		/* prepare the hep6 headers */
 
 		hdr.hp_f = AF_INET6;
 
 		hdr.hp_sport = htons(from_su.sin6.sin6_port);
-                hdr.hp_dport = htons(to_su.sin6.sin6_port);
+		hdr.hp_dport = htons(to_su.sin6.sin6_port);
 
 		hep_ip6header.hp6_src = from_su.sin6.sin6_addr;
 		hep_ip6header.hp6_dst = to_su.sin6.sin6_addr;
@@ -1543,25 +1543,25 @@ static int trace_send_hep_duplicate(str *body, str *from, str *to)
 		len = sizeof(struct hep_ip6hdr);
 	}
 #endif /* USE_IPV6 */
-        else {
-                LOG(L_ERR, "ERROR: trace_send_hep_duplicate: Unsupported protocol family\n");
-                goto error;;
-        }
-
-        hdr.hp_l +=len;
-        len += sizeof(struct hep_hdr) + body->len;
-        buffer = (void *)pkg_malloc(len+1);
-        if (buffer==0){
-                LOG(L_ERR, "ERROR: trace_send_hep_duplicate: out of memory\n");
-                goto error;
-        }
+	else {
+		LOG(L_ERR, "ERROR: trace_send_hep_duplicate: Unsupported protocol family\n");
+		goto error;;
+	}
+
+	hdr.hp_l +=len;
+	len += sizeof(struct hep_hdr) + body->len;
+	buffer = (void *)pkg_malloc(len+1);
+	if (buffer==0){
+		LOG(L_ERR, "ERROR: trace_send_hep_duplicate: out of memory\n");
+		goto error;
+	}
 
 	/* Copy job */
-        memset(buffer, '\0', len+1);
+	memset(buffer, '\0', len+1);
 
-        /* copy hep_hdr */
-        memcpy((void*)buffer, &hdr, sizeof(struct hep_hdr));
-        buflen = sizeof(struct hep_hdr);
+	/* copy hep_hdr */
+	memcpy((void*)buffer, &hdr, sizeof(struct hep_hdr));
+	buflen = sizeof(struct hep_hdr);
 
 	/* hep_ip_hdr */
 	if(from_su.s.sa_family==AF_INET) {
@@ -1575,25 +1575,28 @@ static int trace_send_hep_duplicate(str *body, str *from, str *to)
 	}
 #endif /* USE_IPV6 */
 
-        /* PAYLOAD */
-        memcpy((void*)(buffer + buflen) , (void*)body->s, body->len);
-        buflen +=body->len;
+	/* PAYLOAD */
+	memcpy((void*)(buffer + buflen) , (void*)body->s, body->len);
+	buflen +=body->len;
 
-        if (msg_send(&dst, buffer, buflen)<0)
-        {
-                LM_ERR("cannot send hep duplicate message\n");
-                goto error;
-        }
+	if (msg_send(&dst, buffer, buflen)<0)
+	{
+		LM_ERR("cannot send hep duplicate message\n");
+		goto error;
+	}
 
-        free_proxy(p); /* frees only p content, not p itself */
-        pkg_free(p);
-        pkg_free(buffer);
-        return 0;
+	free_proxy(p); /* frees only p content, not p itself */
+	pkg_free(p);
+	pkg_free(buffer);
+	return 0;
 error:
-        free_proxy(p); /* frees only p content, not p itself */
-        pkg_free(p);
-        pkg_free(buffer);
-        return -1;
+	if(p)
+	{
+		free_proxy(p); /* frees only p content, not p itself */
+		pkg_free(p);
+	}
+	if(buffer) pkg_free(buffer);
+	return -1;
 }
 
 /*!
@@ -1604,79 +1607,81 @@ error:
  * \param proto uint protocol type
  * \return success / unsuccess
  */
-static int pipport2su (char *pipport, union sockaddr_union *tmp_su, unsigned int proto)
+static int pipport2su (char *pipport, union sockaddr_union *tmp_su, unsigned int *proto)
 {
-        unsigned int port_no, cutlen = 4;
-        struct ip_addr *ip;
-        char *p, *host_s;
-        str port_str, host_uri;
-        unsigned len = 0;
-
-        /*parse protocol */
-        if(strncmp(pipport, "udp:",4) == 0) proto = IPPROTO_UDP;
-        else if(strncmp(pipport, "tcp:",4) == 0) proto = IPPROTO_TCP;
-        else if(strncmp(pipport, "tls:",4) == 0) proto = IPPROTO_IDP; /* fake proto type */
-        else if(strncmp(pipport, "sctp:",5) == 0) cutlen = 5, proto = IPPROTO_SCTP;
-        else if(strncmp(pipport, "any:",4) == 0) proto = IPPROTO_UDP;
-        else {
-                LM_ERR("bad protocol %s\n", pipport);
-                return -1;
-        }
-
-        /*separate proto and host */
-        p = pipport+cutlen;
-        if( (*(p)) == '\0') {
-                LM_ERR("malformed ip address\n");
-                return -1;
-        }
-        host_s=p;
-
-        if( (p = strrchr(p+1, ':')) == 0 ) {
-                LM_ERR("no port specified\n");
-                return -1;
-        }
-       /*the address contains a port number*/
-        *p = '\0';
-        p++;
-        port_str.s = p;
-        port_str.len = strlen(p);
-        LM_DBG("the port string is %s\n", p);
-        if(str2int(&port_str, &port_no) != 0 ) {
-                LM_ERR("there is not a valid number port\n");
-                return -1;
-        }
-        *p = '\0';
-        if (port_no<1024  || port_no>65536)
-        {
-                LM_ERR("invalid port number; must be in [1024,65536]\n");
-                return -1;
-        }
-	
+	unsigned int port_no, cutlen = 4;
+	struct ip_addr *ip;
+	char *p, *host_s;
+	str port_str, host_uri;
+	unsigned len = 0;
+
+	/*parse protocol */
+	if(strncmp(pipport, "udp:",4) == 0) *proto = IPPROTO_UDP;
+	else if(strncmp(pipport, "tcp:",4) == 0) *proto = IPPROTO_TCP;
+	else if(strncmp(pipport, "tls:",4) == 0) *proto = IPPROTO_IDP; /* fake proto type */
+#ifdef USE_SCTP
+	else if(strncmp(pipport, "sctp:",5) == 0) cutlen = 5, *proto = IPPROTO_SCTP;
+#endif
+	else if(strncmp(pipport, "any:",4) == 0) *proto = IPPROTO_UDP;
+	else {
+		LM_ERR("bad protocol %s\n", pipport);
+		return -1;
+	}
+
+	/*separate proto and host */
+	p = pipport+cutlen;
+	if( (*(p)) == '\0') {
+		LM_ERR("malformed ip address\n");
+		return -1;
+	}
+	host_s=p;
+
+	if( (p = strrchr(p+1, ':')) == 0 ) {
+		LM_ERR("no port specified\n");
+		return -1;
+	}
+	/*the address contains a port number*/
+	*p = '\0';
+	p++;
+	port_str.s = p;
+	port_str.len = strlen(p);
+	LM_DBG("the port string is %s\n", p);
+	if(str2int(&port_str, &port_no) != 0 ) {
+		LM_ERR("there is not a valid number port\n");
+		return -1;
+	}
+	*p = '\0';
+	if (port_no<1024  || port_no>65536)
+	{
+		LM_ERR("invalid port number; must be in [1024,65536]\n");
+		return -1;
+	}
+
 	/* now IPv6 address has no brakets. It should be fixed! */
 	if (host_s[0] == '[') {
-              len = strlen(host_s + 1) - 1;
-              if(host_s[len+1] != ']') {
-                LM_ERR("bracket not closed\n");
-                return -1;
-            }
-            memmove(host_s, host_s + 1, len);
-            host_s[len] = '\0';
-        }
+		len = strlen(host_s + 1) - 1;
+		if(host_s[len+1] != ']') {
+			LM_ERR("bracket not closed\n");
+			return -1;
+		}
+		memmove(host_s, host_s + 1, len);
+		host_s[len] = '\0';
+	}
 
-        host_uri.s = host_s;
-        host_uri.len = strlen(host_s);
+	host_uri.s = host_s;
+	host_uri.len = strlen(host_s);
 
 
 	/* check if it's an ip address */
-        if (((ip=str2ip(&host_uri))!=0)
+	if (((ip=str2ip(&host_uri))!=0)
 #ifdef  USE_IPV6
-               || ((ip=str2ip6(&host_uri))!=0)
+			|| ((ip=str2ip6(&host_uri))!=0)
 #endif
-         ) {
+	   ) {
 		ip_addr2su(tmp_su, ip, ntohs(port_no));
 		return 0;	
 
-          }
+	}
 
-        return -1;
+	return -1;
 }