dset.c
caf80ae6
 /*
  * $Id$
  *
  * destination set
7dd0b342
  *
035f593c
  * Copyright (C) 2001-2004 FhG FOKUS
7dd0b342
  *
  * This file is part of ser, a free SIP server.
  *
  * ser is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
  * For a license to use the ser software under conditions
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
  * ser is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
caf80ae6
  */
 
b4ea4393
 /** destination set / branches support.
  * @file dset.c
  * @ingroup core
  * Module: @ref core
1d0661db
  */
 
caf80ae6
 #include <string.h>
 #include "dprint.h"
 #include "config.h"
 #include "parser/parser_f.h"
217f9fdd
 #include "parser/parse_uri.h"
caf80ae6
 #include "parser/msg_parser.h"
 #include "ut.h"
 #include "hash_func.h"
30449150
 #include "error.h"
035f593c
 #include "dset.h"
e3d05bb9
 #include "mem/mem.h"
dc4b13e8
 #include "ip_addr.h"
caf80ae6
 
035f593c
 #define CONTACT "Contact: "
 #define CONTACT_LEN (sizeof(CONTACT) - 1)
caf80ae6
 
035f593c
 #define CONTACT_DELIM ", "
 #define CONTACT_DELIM_LEN (sizeof(CONTACT_DELIM) - 1)
 
 #define Q_PARAM ">;q="
 #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
 
 
 /* 
  * Where we store URIs of additional transaction branches
  * (-1 because of the default branch, #0)
  */
 static struct branch branches[MAX_BRANCHES - 1];
caf80ae6
 
 /* how many of them we have */
87962c1a
 unsigned int nr_branches = 0;
035f593c
 
caf80ae6
 /* branch iterator */
035f593c
 static int branch_iterator = 0;
 
b4ea4393
 /* used to mark ruris "consumed" when branching (1 new, 0 consumed) */
 int ruri_is_new = 0;
 
035f593c
 /* The q parameter of the Request-URI */
0202608d
 static qvalue_t ruri_q = Q_UNSPECIFIED;
 
 /* Branch flags of the Request-URI */
 static flag_t ruri_bflags;
 
 
15627cd0
 /*! \brief
  * Return pointer to branch[idx] structure
  * @param idx - branch index
  *
  * @return  pointer to branch or NULL if invalid branch
  */
 branch_t *get_sip_branch(int idx)
 {
 	if(nr_branches==0)
 		return NULL;
 	if(idx<0)
 	{
90681624
 		if((int)nr_branches + idx >= 0)
15627cd0
 			return &branches[nr_branches+idx];
 		return NULL;
 	}
 	if(idx < nr_branches)
 		return &branches[idx];
 	return 0;
 }
 
 /*! \brief
  * Drop branch[idx]
  * @param idx - branch index
  *
  * @return  0 on success, -1 on error
  */
 int drop_sip_branch(int idx)
 {
 	if(nr_branches==0 || idx>=nr_branches)
 		return 0;
90681624
 	if(idx<0 && (int)nr_branches+idx<0)
15627cd0
 		return 0;
 	/* last branch */
 	if(idx==nr_branches-1)
 	{
 		nr_branches--;
 		return 0;
 	}
 	if(idx<0)
 		idx = nr_branches+idx;
 	/* shift back one position */
 	for(; idx<nr_branches-1; idx++)
 		memcpy(&branches[idx], &branches[idx+1], sizeof(branch_t));
 	nr_branches--;
 	return 0;
 }
 
0202608d
 static inline flag_t* get_bflags_ptr(unsigned int branch)
 {
 	if (branch == 0) return &ruri_bflags;
 	if (branch - 1 < nr_branches) return &branches[branch - 1].flags;
 	return NULL;
 }
 
 
 int setbflag(unsigned int branch, flag_t flag)
 {
 	flag_t* flags;
 
 	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
 	(*flags) |= 1 << flag;
 	return 1;
 }
 
 
 int isbflagset(unsigned int branch, flag_t flag)
 {
 	flag_t* flags;
 
 	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
 	return ((*flags) & (1 << flag)) ? 1 : -1;
 }
 
 
 int resetbflag(unsigned int branch, flag_t flag)
 {
 	flag_t* flags;
 
 	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
 	(*flags) &= ~ (1 << flag);
 	return 1;
 }
035f593c
 
caf80ae6
 
d02a166d
 int getbflagsval(unsigned int branch, flag_t* res)
5a68f95d
 {
 	flag_t* flags;
 	if (res == NULL) return -1;
 	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
 	*res = *flags;
 	return 1;
 }
 
 
 int setbflagsval(unsigned int branch, flag_t val)
 {
 	flag_t* flags;
 	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
 	*flags = val;
 	return 1;
 }
 
 
035f593c
 /*
53c7e0f1
  * Initialize the branch iterator, the next
035f593c
  * call to next_branch will return the first
  * contact from the dset array
  */
caf80ae6
 void init_branch_iterator(void)
 {
035f593c
 	branch_iterator = 0;
caf80ae6
 }
 
c5f101df
 /**
  * return the value of current branch iterator
  */
764bac17
 int get_branch_iterator(void)
 {
 	return branch_iterator;
 }
035f593c
 
c5f101df
 /**
  * set the value of current branch interator
  */
 void set_branch_iterator(int n)
 {
 	branch_iterator = n;
 }
caf80ae6
 
035f593c
 
52d5764e
 /** \brief Get a branch from the destination set
  * \return Return the 'i' branch from the dset
  * array, 0 is returned if there are no
  * more branches
  */
 char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
571e4e3f
 		 str* path, unsigned int *flags,
8e6c690e
 		 struct socket_info** force_socket,
8ca114df
 		 str *ruid, str *instance, str *location_ua)
52d5764e
 {
 	if (i < nr_branches) {
 		*len = branches[i].len;
 		*q = branches[i].q;
 		if (dst_uri) {
 			dst_uri->len = branches[i].dst_uri_len;
 			dst_uri->s = (dst_uri->len)?branches[i].dst_uri:0;
 		}
 		if (path) {
 			path->len = branches[i].path_len;
 			path->s = (path->len)?branches[i].path:0;
 		}
 		if (force_socket)
 			*force_socket = branches[i].force_send_socket;
 		if (flags)
 			*flags = branches[i].flags;
8e6c690e
 		if (ruid) {
 			ruid->len = branches[i].ruid_len;
 			ruid->s = (ruid->len)?branches[i].ruid:0;
 		}
42a1548a
 		if (instance) {
 			instance->len = branches[i].instance_len;
 			instance->s = (instance->len)?branches[i].instance:0;
 		}
8ca114df
 		if (location_ua) {
 			location_ua->len = branches[i].location_ua_len;
 			location_ua->s
 				= (location_ua->len)?branches[i].location_ua:0;
 		}
52d5764e
 		return branches[i].uri;
 	} else {
 		*len = 0;
 		*q = Q_UNSPECIFIED;
 		if (dst_uri) {
 			dst_uri->s = 0;
 			dst_uri->len = 0;
 		}
c43dc0cd
 		if (path) {
 			path->s = 0;
 			path->len = 0;
 		}
52d5764e
 		if (force_socket)
 			*force_socket = 0;
 		if (flags)
 			*flags = 0;
8e6c690e
 		if (ruid) {
 			ruid->s = 0;
 			ruid->len = 0;
 		}
42a1548a
 		if (instance) {
 			instance->s = 0;
 			instance->len = 0;
 		}
8ca114df
 		if (location_ua) {
 			location_ua->s = 0;
 			location_ua->len = 0;
 		}
52d5764e
 		return 0;
 	}
 }
 
 
c43dc0cd
 
 /** Return the next branch from the dset array.
  * 0 is returned if there are no more branches
  */
 char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
8e6c690e
 		  unsigned int* flags, struct socket_info** force_socket,
8ca114df
 		  str* ruid, str *instance, str *location_ua)
c43dc0cd
 {
 	char* ret;
 	
 	ret=get_branch(branch_iterator, len, q, dst_uri, path, flags,
8ca114df
 		       force_socket, ruid, instance, location_ua);
c43dc0cd
 	if (likely(ret))
 		branch_iterator++;
 	return ret;
 }
 
 
035f593c
 /*
  * Empty the dset array
  */
 void clear_branches(void)
caf80ae6
 {
035f593c
 	nr_branches = 0;
 	ruri_q = Q_UNSPECIFIED;
c6dd54e8
 	ruri_bflags = 0;
b4ea4393
 	ruri_mark_consumed();
caf80ae6
 }
 
035f593c
 
2a78db5c
 
c43dc0cd
 /**  Add a new branch to the current transaction.
  * @param msg - sip message, used for getting the uri if not specified (0).
  * @param uri - uri, can be 0 (in which case the uri is taken from msg)
  * @param dst_uri - destination uri, can be 0.
  * @param path - path vector (passed in a string), can be 0.
  * @param q  - q value.
  * @param flags - per branch flags.
  * @param force_socket - socket that should be used when sending.
  *
  * @return  <0 (-1) on failure, 1 on success (script convention).
52d5764e
  */
c43dc0cd
 int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
571e4e3f
 		  qvalue_t q, unsigned int flags,
 		  struct socket_info* force_socket,
e3ba8f38
 		  str* instance, unsigned int reg_id,
8ca114df
 		  str* ruid, str* location_ua)
52d5764e
 {
 	str luri;
 
 	/* if we have already set up the maximum number
 	 * of branches, don't try new ones 
 	 */
c43dc0cd
 	if (unlikely(nr_branches == MAX_BRANCHES - 1)) {
52d5764e
 		LOG(L_ERR, "max nr of branches exceeded\n");
 		ser_error = E_TOO_MANY_BRANCHES;
 		return -1;
 	}
 
 	/* if not parameterized, take current uri */
 	if (uri==0 || uri->len==0 || uri->s==0) {
 		if (msg->new_uri.s)
 			luri = msg->new_uri;
 		else
 			luri = msg->first_line.u.request.uri;
 	} else {
 		luri = *uri;
 	}
 
c43dc0cd
 	if (unlikely(luri.len > MAX_URI_SIZE - 1)) {
52d5764e
 		LOG(L_ERR, "too long uri: %.*s\n", luri.len, luri.s);
 		return -1;
 	}
 
 	/* copy the dst_uri */
 	if (dst_uri && dst_uri->len && dst_uri->s) {
c43dc0cd
 		if (unlikely(dst_uri->len > MAX_URI_SIZE - 1)) {
 			LOG(L_ERR, "too long dst_uri: %.*s\n", dst_uri->len, dst_uri->s);
52d5764e
 			return -1;
 		}
 		memcpy(branches[nr_branches].dst_uri, dst_uri->s, dst_uri->len);
 		branches[nr_branches].dst_uri[dst_uri->len] = 0;
 		branches[nr_branches].dst_uri_len = dst_uri->len;
 	} else {
 		branches[nr_branches].dst_uri[0] = '\0';
 		branches[nr_branches].dst_uri_len = 0;
 	}
 
 	/* copy the path string */
c43dc0cd
 	if (unlikely(path && path->len && path->s)) {
 		if (unlikely(path->len > MAX_PATH_SIZE - 1)) {
52d5764e
 			LOG(L_ERR, "too long path: %.*s\n", path->len, path->s);
 			return -1;
 		}
 		memcpy(branches[nr_branches].path, path->s, path->len);
 		branches[nr_branches].path[path->len] = 0;
 		branches[nr_branches].path_len = path->len;
 	} else {
 		branches[nr_branches].path[0] = '\0';
 		branches[nr_branches].path_len = 0;
 	}
 
 	/* copy the ruri */
 	memcpy(branches[nr_branches].uri, luri.s, luri.len);
 	branches[nr_branches].uri[luri.len] = 0;
 	branches[nr_branches].len = luri.len;
 	branches[nr_branches].q = q;
 
 	branches[nr_branches].force_send_socket = force_socket;
 	branches[nr_branches].flags = flags;
 
571e4e3f
 	/* copy instance string */
 	if (unlikely(instance && instance->len && instance->s)) {
 		if (unlikely(instance->len > MAX_INSTANCE_SIZE - 1)) {
 			LOG(L_ERR, "too long instance: %.*s\n",
 			    instance->len, instance->s);
 			return -1;
 		}
 		memcpy(branches[nr_branches].instance, instance->s,
 		       instance->len);
 		branches[nr_branches].instance[instance->len] = 0;
 		branches[nr_branches].instance_len = instance->len;
 	} else {
 		branches[nr_branches].instance[0] = '\0';
 		branches[nr_branches].instance_len = 0;
 	}
 
 	/* copy reg_id */
 	branches[nr_branches].reg_id = reg_id;
 
e3ba8f38
 	/* copy ruid string */
 	if (unlikely(ruid && ruid->len && ruid->s)) {
 		if (unlikely(ruid->len > MAX_RUID_SIZE - 1)) {
 			LOG(L_ERR, "too long ruid: %.*s\n",
 			    ruid->len, ruid->s);
 			return -1;
 		}
 		memcpy(branches[nr_branches].ruid, ruid->s,
 		       ruid->len);
 		branches[nr_branches].ruid[ruid->len] = 0;
 		branches[nr_branches].ruid_len = ruid->len;
 	} else {
 		branches[nr_branches].ruid[0] = '\0';
 		branches[nr_branches].ruid_len = 0;
 	}
 
8ca114df
 	if (unlikely(location_ua && location_ua->len && location_ua->s)) {
 		if (unlikely(location_ua->len > MAX_UA_SIZE)) {
 			LOG(L_ERR, "too long location_ua: %.*s\n",
 			    location_ua->len, location_ua->s);
 			return -1;
 		}
 		memcpy(branches[nr_branches].location_ua, location_ua->s,
 		       location_ua->len);
 		branches[nr_branches].location_ua[location_ua->len] = 0;
 		branches[nr_branches].location_ua_len = location_ua->len;
 	} else {
 		branches[nr_branches].location_ua[0] = '\0';
 		branches[nr_branches].location_ua_len = 0;
 	}
 	
52d5764e
 	nr_branches++;
 	return 1;
 }
 
 
035f593c
 /*
  * Create a Contact header field from the dset
  * array
  */
 char* print_dset(struct sip_msg* msg, int* len) 
2a78db5c
 {
d7a3fdea
 	int cnt, i;
 	unsigned int qlen;
035f593c
 	qvalue_t q;
2a78db5c
 	str uri;
b0edf8f9
 	char* p, *qbuf;
c5f101df
 	int crt_branch;
2a78db5c
 	static char dset[MAX_REDIRECTION_LEN];
 
 	if (msg->new_uri.s) {
035f593c
 		cnt = 1;
74bf08ef
 		*len = msg->new_uri.len + 1 /*'<'*/;
035f593c
 		if (ruri_q != Q_UNSPECIFIED) {
74bf08ef
 			*len += Q_PARAM_LEN + len_q(ruri_q);
 		} else {
 			*len += 1 /*'>'*/;
035f593c
 		}
2a78db5c
 	} else {
035f593c
 		cnt = 0;
 		*len = 0;
2a78db5c
 	}
 
c5f101df
 	/* backup current branch index to restore it later */
 	crt_branch = get_branch_iterator();
 
2a78db5c
 	init_branch_iterator();
8ca114df
 	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0, 0))) {
2a78db5c
 		cnt++;
74bf08ef
 		*len += uri.len + 1 /*'<'*/;
035f593c
 		if (q != Q_UNSPECIFIED) {
74bf08ef
 			*len += Q_PARAM_LEN + len_q(q);
 		} else {
 			*len += 1 /*'>'*/;
035f593c
 		}
2a78db5c
 	}
 
035f593c
 	if (cnt == 0) return 0;	
2a78db5c
 
035f593c
 	*len += CONTACT_LEN + CRLF_LEN + (cnt - 1) * CONTACT_DELIM_LEN;
2a78db5c
 
035f593c
 	if (*len + 1 > MAX_REDIRECTION_LEN) {
2a78db5c
 		LOG(L_ERR, "ERROR: redirection buffer length exceed\n");
c5f101df
 		goto error;
2a78db5c
 	}
 
035f593c
 	memcpy(dset, CONTACT, CONTACT_LEN);
 	p = dset + CONTACT_LEN;
2a78db5c
 	if (msg->new_uri.s) {
74bf08ef
 		*p++ = '<';
035f593c
 
2a78db5c
 		memcpy(p, msg->new_uri.s, msg->new_uri.len);
035f593c
 		p += msg->new_uri.len;
 
 		if (ruri_q != Q_UNSPECIFIED) {
 			memcpy(p, Q_PARAM, Q_PARAM_LEN);
 			p += Q_PARAM_LEN;
b0edf8f9
 
 			qbuf = q2str(ruri_q, &qlen);
 			memcpy(p, qbuf, qlen);
 			p += qlen;
74bf08ef
 		} else {
 			*p++ = '>';
035f593c
 		}
 		i = 1;
 	} else {
 		i = 0;
 	}
2a78db5c
 
 	init_branch_iterator();
8ca114df
 	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0, 0))) {
2a78db5c
 		if (i) {
035f593c
 			memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
 			p += CONTACT_DELIM_LEN;
 		}
 
74bf08ef
 		*p++ = '<';
035f593c
 
2a78db5c
 		memcpy(p, uri.s, uri.len);
035f593c
 		p += uri.len;
 		if (q != Q_UNSPECIFIED) {
 			memcpy(p, Q_PARAM, Q_PARAM_LEN);
 			p += Q_PARAM_LEN;
b0edf8f9
 
 			qbuf = q2str(q, &qlen);
 			memcpy(p, qbuf, qlen);
 			p += qlen;
74bf08ef
 		} else {
 			*p++ = '>';
035f593c
 		}
2a78db5c
 		i++;
 	}
035f593c
 
 	memcpy(p, CRLF " ", CRLF_LEN + 1);
c5f101df
 	set_branch_iterator(crt_branch);
2a78db5c
 	return dset;
c5f101df
 
 error:
 	set_branch_iterator(crt_branch);
 	return 0;
2a78db5c
 }
 
035f593c
 
 /*
  * Sets the q parameter of the Request-URI
  */
 void set_ruri_q(qvalue_t q)
 {
 	ruri_q = q;
 }
 
 
 /*
  * Return the q value of the Request-URI
  */
 qvalue_t get_ruri_q(void)
 {
 	return ruri_q;
 }
7c8890f0
 
 
 
 /*
  * Rewrite Request-URI
  */
 int rewrite_uri(struct sip_msg* _m, str* _s)
 {
07245289
 	char *buf = NULL;
7c8890f0
 
07245289
 	if(_m->new_uri.s==NULL || _m->new_uri.len<_s->len) {
 		buf = (char*)pkg_malloc(_s->len + 1);
 		if (!buf) {
 			LM_ERR("No memory left to rewrite r-uri\n");
 			return -1;
 		}
 	}
 	if(buf!=NULL) {
 		if(_m->new_uri.s)
 			pkg_free(_m->new_uri.s);
 	} else {
 		buf = _m->new_uri.s;
 	}
7c8890f0
 
07245289
 	memcpy(buf, _s->s, _s->len);
 	buf[_s->len] = '\0';
7c8890f0
 
07245289
 	_m->parsed_uri_ok = 0;
7c8890f0
 
07245289
 	_m->new_uri.s = buf;
 	_m->new_uri.len = _s->len;
 	/* mark ruri as new and available for forking */
 	ruri_mark_new();
7c8890f0
 
07245289
 	return 1;
7c8890f0
 }
 
3f3c865e
 /**
  * return src ip, port and proto as a SIP uri or proxy address
  * - value stored in a static buffer
  * - mode=0 return uri, mode=1 return proxy address
  */
 int msg_get_src_addr(sip_msg_t *msg, str *uri, int mode)
 {
 	static char buf[80];
 	char* p;
 	str ip, port;
 	int len;
 	str proto;
 
 	if (msg==NULL || uri==NULL) {
 		LM_ERR("invalid parameter value\n");
 		return -1;
 	}
 
 	ip.s = ip_addr2a(&msg->rcv.src_ip);
 	ip.len = strlen(ip.s);
 
 	port.s = int2str(msg->rcv.src_port, &port.len);
 
 	switch(msg->rcv.proto) {
 		case PROTO_NONE:
 		case PROTO_UDP:
 			if(mode==0) {
 				proto.s = 0; /* Do not add transport parameter, UDP is default */
 				proto.len = 0;
 			} else {
 				proto.s = "udp";
 				proto.len = 3;
 			}
 		break;
 
 		case PROTO_TCP:
 			proto.s = "tcp";
 			proto.len = 3;
 		break;
 
 		case PROTO_TLS:
 			proto.s = "tls";
 			proto.len = 3;
 		break;
 
 		case PROTO_SCTP:
 			proto.s = "sctp";
 			proto.len = 4;
 		break;
 
 		case PROTO_WS:
 		case PROTO_WSS:
 			proto.s = "ws";
 			proto.len = 2;
 		break;
 
 		default:
 			LM_ERR("unknown transport protocol\n");
 		return -1;
 	}
 
 	len = ip.len + 2*(msg->rcv.src_ip.af==AF_INET6)+ 1 + port.len;
 	if (mode==0) {
 		len += 4;
 		if(proto.s) {
 			len += TRANSPORT_PARAM_LEN;
 			len += proto.len;
 		}
 	} else {
 		len += proto.len + 1;
 	}
 
 	if (len > 79) {
 		LM_ERR("buffer too small\n");
 		return -1;
 	}
 
 	p = buf;
 	if(mode==0) {
 		memcpy(p, "sip:", 4);
 		p += 4;
 	} else {
 		memcpy(p, proto.s, proto.len);
 		p += proto.len;
 		*p++ = ':';
 	}
 
 	if (msg->rcv.src_ip.af==AF_INET6)
 		*p++ = '[';
 	memcpy(p, ip.s, ip.len);
 	p += ip.len;
 	if (msg->rcv.src_ip.af==AF_INET6)
 		*p++ = ']';
 
 	*p++ = ':';
 
 	memcpy(p, port.s, port.len);
 	p += port.len;
 
 	if (mode==0 && proto.s) {
 		memcpy(p, TRANSPORT_PARAM, TRANSPORT_PARAM_LEN);
 		p += TRANSPORT_PARAM_LEN;
 
 		memcpy(p, proto.s, proto.len);
 		p += proto.len;
 	}
 
 	uri->s = buf;
 	uri->len = len;
 	uri->s[uri->len] = '\0';
 
 	return 0;
 }
 
217f9fdd
 /**
  * add alias parameter with encoding of source address
  * - nuri->s must point to a buffer of nuri->len size
  */
 int uri_add_rcv_alias(sip_msg_t *msg, str *uri, str *nuri)
 {
 	char* p;
 	str ip, port;
 	int len;
 
 	if (msg==NULL || uri==NULL || nuri==NULL) {
 		LM_ERR("invalid parameter value\n");
 		return -1;
 	}
 
 	ip.s = ip_addr2a(&msg->rcv.src_ip);
 	ip.len = strlen(ip.s);
 
 	port.s = int2str(msg->rcv.src_port, &port.len);
 
 	/*uri;alias=[ip]~port~proto*/
 	len = uri->len+ip.len+port.len+12;
 	if(len>=nuri->len) {
 		LM_ERR("not enough space for new uri: %d\n", len);
 		return -1;
 	}
 	p = nuri->s;
 	memcpy(p, uri->s, uri->len);
 	p += uri->len;
 	memcpy(p, ";alias=", 7);
 	p += 7;
 	if (msg->rcv.src_ip.af == AF_INET6)
 		*p++ = '[';
 	memcpy(p, ip.s, ip.len);
 	p += ip.len;
 	if (msg->rcv.src_ip.af == AF_INET6)
 		*p++ = ']';
 	*p++ = '~';
 	memcpy(p, port.s, port.len);
 	p += port.len;
 	*p++ = '~';
 	*p++ = msg->rcv.proto + '0';
 	nuri->len = p - nuri->s;
 	nuri->s[nuri->len] = '\0';
 
 	LM_DBG("encoded <%.*s> => [%.*s]\n",
 			uri->len, uri->s, nuri->len, nuri->s);
 	return 0;
 }
 
 /**
  * restore from alias parameter with encoding of source address
  * - nuri->s must point to a buffer of nuri->len size
  * - suri->s must point to a buffer of suri->len size
  */
 int uri_restore_rcv_alias(str *uri, str *nuri, str *suri)
 {
 	char* p;
 	str skip;
 	str ip, port, sproto;
 	int proto;
 
 	if (uri==NULL || nuri==NULL || suri==NULL) {
 		LM_ERR("invalid parameter value\n");
 		return -1;
 	}
 
 	/* sip:x;alias=1.1.1.1~0~0 */
 	if(uri->len < 23) {
 		/* no alias possible */
 		return -2;
 	}
 	p = uri->s + uri->len-18;
 	skip.s = 0;
 	while(p>uri->s+5) {
 		if(strncmp(p, ";alias=", 7)==0) {
 			skip.s = p;
 			break;
 		}
 		p--;
 	}
 	if(skip.s==0) {
 		/* alias parameter not found */
 		return -2;
 	}
 	p += 7;
 	ip.s = p;
 	p = (char*)memchr(ip.s, '~', (size_t)(uri->s+uri->len-ip.s));
 	if(p==NULL) {
 		/* proper alias parameter not found */
 		return -2;
 	}
 	ip.len = p - ip.s;
 	p++;
 	if(p>=uri->s+uri->len) {
 		/* proper alias parameter not found */
 		return -2;
 	}
 	port.s = p;
 	p = (char*)memchr(port.s, '~', (size_t)(uri->s+uri->len-port.s));
 	if(p==NULL) {
 		/* proper alias parameter not found */
 		return -2;
 	}
 	port.len = p - port.s;
 	p++;
 	if(p>=uri->s+uri->len) {
 		/* proper alias parameter not found */
 		return -2;
 	}
 	proto = (int)(*p - '0');
 	p++;
 
 	if(p!=uri->s+uri->len && *p!=';') {
 		/* proper alias parameter not found */
 		return -2;
 	}
 	skip.len = (int)(p - skip.s);
 
 	if(suri->len<=4+ip.len+1+port.len+11/*;transport=*/+4) {
 		LM_ERR("address buffer too small\n");
 		return -1;
 	}
 	if(nuri->len<=uri->len - skip.len) {
 		LM_ERR("uri buffer too small\n");
 		return -1;
 	}
 
 	p = nuri->s;
 	memcpy(p, uri->s, (size_t)(skip.s-uri->s));
 	p += skip.s-uri->s;
 	memcpy(p, skip.s+skip.len, (size_t)(uri->s+uri->len - skip.s - skip.len));
 	p += uri->s+uri->len - skip.s - skip.len;
 	nuri->len = p - nuri->s;
 
 	p = suri->s;
 	strncpy(p, "sip:", 4);
 	p += 4;
 	strncpy(p, ip.s, ip.len);
 	p += ip.len;
 	*p++ = ':';
 	strncpy(p, port.s, port.len);
 	p += port.len;
 	proto_type_to_str((unsigned short)proto, &sproto);
 	if(sproto.len>0 && proto!=PROTO_UDP) {
 		strncpy(p, ";transport=", 11);
 		p += 11;
 		strncpy(p, sproto.s, sproto.len);
 		p += sproto.len;
 	}
 	suri->len = p - suri->s;
 
 	LM_DBG("decoded <%.*s> => [%.*s] [%.*s]\n",
 			uri->len, uri->s, nuri->len, nuri->s, suri->len, suri->s);
 
 	return 0;
 }