src/modules/path/path.c
46a0dab3
 /*
  * Path handling for intermediate proxies.
  *
  * Copyright (C) 2006 Inode GmbH (Andreas Granig <andreas.granig@inode.info>)
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
46a0dab3
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
46a0dab3
  * 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
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
46a0dab3
  * 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.
  *
0e86b73a
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
46a0dab3
  *
  */
 
e46039c5
 /*! \file
  * \brief Path :: Utilities
  *
  * \ingroup path
  * - Module: path
  */
46a0dab3
 
 #include <string.h>
 #include <stdio.h>
 
cf83221d
 #include "../../core/mem/mem.h"
 #include "../../core/data_lump.h"
 #include "../../core/parser/parse_param.h"
2c62b317
 #include "../../core/parser/parse_uri.h"
cf83221d
 #include "../../core/strutils.h"
 #include "../../core/dset.h"
46a0dab3
 
 #include "path.h"
 #include "path_mod.h"
 
b27311b5
 typedef enum {
3c04bda0
 	PATH_PARAM_NONE = 0, PATH_PARAM_RECEIVED = 1, PATH_PARAM_OB = 2
b27311b5
 } path_param_t;
 
46a0dab3
 #define PATH_PREFIX		"Path: <sip:"
 #define PATH_PREFIX_LEN		(sizeof(PATH_PREFIX)-1)
 
99415516
 const static char *proto_strings[] = {
 	[PROTO_TCP] = "%3Btransport%3Dtcp",
 	[PROTO_TLS] = "%3Btransport%3Dtls",
 	[PROTO_SCTP] = "%3Btransport%3Dsctp",
 	[PROTO_WS] = "%3Btransport%3Dws",
 	[PROTO_WSS] = "%3Btransport%3Dws",
 };
 
31d92814
 extern int path_sockname_mode;
6b607182
 extern str path_received_name;
31d92814
 
2c62b317
 static char *path_strzdup(char *src, int len)
 {
 	char *res;
 
 	if (!src)
 		return NULL;
 	if(len<0) {
 		len = strlen(src);
 	}
 	if (!(res = (char *) pkg_malloc(len + 1)))
 		return NULL;
 	strncpy(res, src, len);
 	res[len] = 0;
 
 	return res;
 }
 
3c04bda0
 static int handleOutbound(sip_msg_t* _m, str *user, path_param_t *param)
 {
 	if (path_obb.use_outbound != NULL && path_obb.use_outbound(_m)) {
 		struct via_body *via;
 
97c7081d
 		if (path_obb.encode_flow_token(user, &_m->rcv) != 0) {
3c04bda0
 			LM_ERR("encoding outbound flow-token\n");
 			return -1;
 		}
 
 		/* Only include ;ob parameter if this is the first-hop
 		 * (that means only one Via:) */
 		if (parse_via_header(_m, 2, &via) < 0) {
 			*param |= PATH_PARAM_OB;
 		}
 	}
 
 	return 1;
 }
 
2c62b317
 static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
 		str *add_params)
46a0dab3
 {
 	struct lump *l;
2c62b317
 	char *prefix, *suffix, *cp, *dp;
99415516
 	const char *proto_str;
46a0dab3
 	int prefix_len, suffix_len;
 	struct hdr_field *hf;
 
99415516
 	/* maximum possible length of suffix */
6b607182
 	suffix_len = sizeof(";lr;r2=on;=sip::12345%3Btransport%3Dsctp;ob;>\r\n")
 			+ IP_ADDR_MAX_STR_SIZE + 2 + (add_params ? add_params->len : 0)
 			+ path_received_name.len + 1;
99415516
 
 	cp = suffix = pkg_malloc(suffix_len);
 	if (!suffix) {
 		LM_ERR("no pkg memory left for suffix\n");
85fca706
 		goto out1;
46a0dab3
 	}
99415516
 
 	cp += sprintf(cp, ";lr");
46a0dab3
 
3c04bda0
 	if (param & PATH_PARAM_RECEIVED) {
2c62b317
 		if(path_received_format==0) {
 			if (_m->rcv.proto
 						< (sizeof(proto_strings) / sizeof(*proto_strings))) {
 				proto_str = proto_strings[(unsigned int) _m->rcv.proto];
 			} else {
 				proto_str = NULL;
 			}
 			if(_m->rcv.src_ip.af==AF_INET6) {
6b607182
 				cp += sprintf(cp, ";%s=sip:[%s]:%hu%s", path_received_name.s,
2c62b317
 						ip_addr2a(&_m->rcv.src_ip),
 						_m->rcv.src_port, proto_str ? : "");
 			} else {
6b607182
 				cp += sprintf(cp, ";%s=sip:%s:%hu%s", path_received_name.s,
 						ip_addr2a(&_m->rcv.src_ip),
2c62b317
 						_m->rcv.src_port, proto_str ? : "");
 			}
077b2638
 		} else {
2c62b317
 			if(_m->rcv.src_ip.af==AF_INET6) {
6b607182
 				cp += sprintf(cp, ";%s=[%s]~%hu~%d", path_received_name.s,
2c62b317
 						ip_addr2a(&_m->rcv.src_ip),
 						_m->rcv.src_port, (int)_m->rcv.proto);
 			} else {
6b607182
 				cp += sprintf(cp, ";%s=%s~%hu~%d", path_received_name.s,
2c62b317
 						ip_addr2a(&_m->rcv.src_ip),
 						_m->rcv.src_port, (int)_m->rcv.proto);
 			}
077b2638
 		}
3c04bda0
 	}
 
 	if (param & PATH_PARAM_OB) {
99415516
 		cp += sprintf(cp, ";ob");
46a0dab3
 	}
 
3c04bda0
 	if (add_params && add_params->len) {
99415516
 		cp += sprintf(cp, ";%.*s", add_params->len, add_params->s);
3c04bda0
 	}
99415516
 
2c62b317
 	if(path_enable_r2==0) {
 		cp += sprintf(cp, ">\r\n");
 	} else {
 		cp += sprintf(cp, ";r2=on>\r\n");
 	}
99415516
 
 	prefix_len = PATH_PREFIX_LEN + (user ? user->len : 0) + 2;
 	prefix = pkg_malloc(prefix_len);
 	if (!prefix) {
 		LM_ERR("no pkg memory left for prefix\n");
 		goto out2;
46a0dab3
 	}
99415516
 	if (user && user->len)
 		prefix_len = sprintf(prefix, PATH_PREFIX "%.*s@", user->len, user->s);
 	else
 		prefix_len = sprintf(prefix, PATH_PREFIX);
46a0dab3
 
 	if (parse_headers(_m, HDR_PATH_F, 0) < 0) {
387847d7
 		LM_ERR("failed to parse message for Path header\n");
99415516
 		goto out3;
46a0dab3
 	}
99415516
 	hf = get_hdr(_m, HDR_PATH_T);
46a0dab3
 	if (hf)
 		/* path found, add ours in front of that */
 		l = anchor_lump(_m, hf->name.s - _m->buf, 0, 0);
 	else
 		/* no path, append to message */
 		l = anchor_lump(_m, _m->unparsed - _m->buf, 0, 0);
 	if (!l) {
387847d7
 		LM_ERR("failed to get anchor\n");
99415516
 		goto out3;
46a0dab3
 	}
 
 	l = insert_new_lump_before(l, prefix, prefix_len, 0);
99415516
 	if (!l) goto out3;
31d92814
 	l = insert_subst_lump_before(l,
 			(path_sockname_mode)?SUBST_SND_ALL_EX:SUBST_SND_ALL, 0);
85fca706
 	if (!l) goto out2;
99415516
 	l = insert_new_lump_before(l, suffix, cp - suffix, 0);
85fca706
 	if (!l) goto out2;
99415516
 
2c62b317
 	if(path_enable_r2!=0) {
 		dp = path_strzdup(prefix, prefix_len);
 		if(dp==NULL) goto out1;
 		l = insert_new_lump_before(l, dp, prefix_len, 0);
 		if (!l) goto out1;
31d92814
 		l = insert_subst_lump_before(l,
 				(path_sockname_mode)?SUBST_RCV_ALL_EX:SUBST_RCV_ALL, 0);
2c62b317
 		if (!l) goto out1;
 		dp = path_strzdup(suffix, cp - suffix);
 		if(dp==NULL) goto out1;
a6d405b1
 		l = insert_new_lump_before(l, dp, cp - suffix, 0);
2c62b317
 		if (!l) goto out1;
 	}
 
46a0dab3
 	return 1;
85fca706
 
99415516
 out3:
 	pkg_free(prefix);
 out2:
 	pkg_free(suffix);
 out1:
 	LM_ERR("failed to insert Path header\n");
85fca706
 
46a0dab3
 	return -1;
 }
 
e46039c5
 /*! \brief
46a0dab3
  * Prepend own uri to Path header
  */
17be61ed
 int ki_add_path(struct sip_msg* _msg)
46a0dab3
 {
 	str user = {0,0};
b27311b5
 	int ret;
 	path_param_t param = PATH_PARAM_NONE;
 
3c04bda0
 	ret = handleOutbound(_msg, &user, &param);
8f0ab913
 
3c04bda0
 	if (ret > 0) {
 		ret = prepend_path(_msg, &user, param, NULL);
b27311b5
 	}
 
3c04bda0
 	if (user.s != NULL) {
b27311b5
 		pkg_free(user.s);
3c04bda0
 	}
b27311b5
 
 	return ret;
46a0dab3
 }
 
17be61ed
 int add_path(sip_msg_t* _msg, char* _a, char* _b)
 {
 	return ki_add_path(_msg);
 }
 
e46039c5
 /*! \brief
46a0dab3
  * Prepend own uri to Path header and take care of given
  * user.
  */
99415516
 int add_path_usr(struct sip_msg* _msg, char* _usr, char* _parms)
46a0dab3
 {
99415516
 	str user = {0,0};
 	str parms = {0,0};
 
2111a605
 	if (_usr) {
 		if(get_str_fparam(&user, _msg, (fparam_t *) _usr)<0) {
 			LM_ERR("failed to get user value\n");
 			return -1;
 		}
 	}
 	if (_parms) {
 		if(get_str_fparam(&parms, _msg, (fparam_t *) _parms)<0) {
 			LM_ERR("failed to get params value\n");
 			return -1;
 		}
 	}
99415516
 
 	return prepend_path(_msg, &user, PATH_PARAM_NONE, &parms);
46a0dab3
 }
 
17be61ed
 /*! \brief
  * Prepend own uri to Path header and take care of given
  * user.
  */
 int ki_add_path_user(sip_msg_t* _msg, str* _user)
 {
 	str parms = {0,0};
 	return prepend_path(_msg, _user, PATH_PARAM_NONE, &parms);
 }
 
 /*! \brief
  * Prepend own uri to Path header and take care of given
  * user.
  */
 int ki_add_path_user_params(sip_msg_t* _msg, str* _user, str* _params)
 {
 	return prepend_path(_msg, _user, PATH_PARAM_NONE, _params);
 }
 
e46039c5
 /*! \brief
46a0dab3
  * Prepend own uri to Path header and append received address as
  * "received"-param to that uri.
  */
3c04bda0
 int ki_add_path_received(sip_msg_t* _msg)
46a0dab3
 {
3c04bda0
 	str user = {0,0};
 	int ret;
 	path_param_t param = PATH_PARAM_RECEIVED;
 
 	ret = handleOutbound(_msg, &user, &param);
 
 	if (ret > 0) {
 		ret = prepend_path(_msg, &user, param, NULL);
 	}
 
 	if (user.s != NULL) {
 		pkg_free(user.s);
 	}
 
 	return ret;
46a0dab3
 }
 
17be61ed
 /*! \brief
  * Prepend own uri to Path header and append received address as
  * "received"-param to that uri.
  */
3c04bda0
 int add_path_received(struct sip_msg* _msg, char* _a, char* _b)
17be61ed
 {
3c04bda0
 	return ki_add_path_received(_msg);
17be61ed
 }
 
e46039c5
 /*! \brief
46a0dab3
  * Prepend own uri to Path header and append received address as
  * "received"-param to that uri and take care of given user.
  */
99415516
 int add_path_received_usr(struct sip_msg* _msg, char* _usr, char* _parms)
46a0dab3
 {
99415516
 	str user = {0,0};
 	str parms = {0,0};
 
2111a605
 	if (_usr) {
 		if(get_str_fparam(&user, _msg, (fparam_t *) _usr)<0) {
 			LM_ERR("failed to get user value\n");
 			return -1;
 		}
 	}
 	if (_parms) {
 		if(get_str_fparam(&parms, _msg, (fparam_t *) _parms)<0) {
 			LM_ERR("failed to get params value\n");
 			return -1;
 		}
 	}
99415516
 
 	return prepend_path(_msg, &user, PATH_PARAM_RECEIVED, &parms);
46a0dab3
 }
 
17be61ed
 /*! \brief
  * Prepend own uri to Path header and append received address as
  * "received"-param to that uri and take care of given user.
  */
 int ki_add_path_received_user(sip_msg_t* _msg, str* _user)
 {
 	str parms = {0,0};
 	return prepend_path(_msg, _user, PATH_PARAM_RECEIVED, &parms);
 }
 
 /*! \brief
  * Prepend own uri to Path header and append received address as
  * "received"-param to that uri and take care of given user.
  */
 int ki_add_path_received_user_params(sip_msg_t* _msg, str* _user, str* _params)
 {
 	return prepend_path(_msg, _user, PATH_PARAM_RECEIVED, _params);
 }
 
e46039c5
 /*! \brief
46a0dab3
  * rr callback
  */
 void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
 {
 	param_hooks_t hooks;
 	param_t *params;
6b607182
 	param_t *it;
1a22767e
 	static char dst_uri_buf[MAX_URI_SIZE];
 	static str dst_uri;
2c62b317
 	char *p;
 	int n;
 	int nproto;
 	str sproto;
6b607182
 	str rcvuri = STR_NULL;
2c62b317
 
6b607182
 	if((path_received_name.len==8)
 			&& strncmp(path_received_name.s, "received", 8)==0) {
 		if (parse_params(r_param, CLASS_CONTACT, &hooks, &params) != 0) {
 			LM_ERR("failed to parse route parameters\n");
 			return;
 		}
 		if (hooks.contact.received==NULL
 				|| hooks.contact.received->body.len<=0) {
 			LM_DBG("no received parameter in route header\n");
 			free_params(params);
 			return;
 		}
 		rcvuri = hooks.contact.received->body;
 	} else {
 		if (parse_params(r_param, CLASS_ANY, &hooks, &params) != 0) {
 			LM_ERR("failed to parse route parameters\n");
 			return;
 		}
 		for(it=params; it; it=it->next) {
 			if((it->name.len==path_received_name.len)
 					&& strncmp(path_received_name.s, it->name.s,
 						it->name.len)==0) {
 				break;
 			}
 		}
 		if (it==NULL || it->body.len<=0) {
 			LM_DBG("no %s parameter in route header\n", path_received_name.s);
 			free_params(params);
 			return;
 		}
 		rcvuri = it->body;
91ebd257
 	}
 
 	/* 24 => sip:...;transport=sctp */
6b607182
 	if(rcvuri.len + 24 >= MAX_URI_SIZE) {
 		LM_ERR("received uri is too long: %d\n", rcvuri.len);
91ebd257
 		goto done;
 	}
 	dst_uri.s = dst_uri_buf;
 	dst_uri.len = MAX_URI_SIZE;
 	if(path_received_format==0) {
 		/* received=sip:...;transport... */
6b607182
 		if (unescape_user(&rcvuri, &dst_uri) < 0) {
91ebd257
 			LM_ERR("unescaping received failed\n");
 			free_params(params);
 			return;
2c62b317
 		}
91ebd257
 	} else {
 		/* received=ip~port~proto */
daad89a5
 		memcpy(dst_uri_buf, "sip:", 4);
6b607182
 		memcpy(dst_uri_buf+4, rcvuri.s, rcvuri.len);
 		dst_uri_buf[4+rcvuri.len] = '\0';
91ebd257
 		p = dst_uri_buf + 4;
 		n = 0;
 		while(*p!='\0') {
 			if(*p=='~') {
 				n++;
 				if(n==1) {
 					/* port */
 					*p = ':';
 				} else if(n==2) {
 					/* proto */
 					*p = ';';
 					p++;
 					if(*p=='\0') {
 						LM_ERR("invalid received format\n");
 						goto done;
 					}
 					nproto = *p - '0';
 					if (nproto != PROTO_UDP) {
 						proto_type_to_str(nproto, &sproto);
 						if (sproto.len == 0) {
 							LM_ERR("unknown proto in received param\n");
2c62b317
 							goto done;
 						}
daad89a5
 						memcpy(p, "transport=", 10);
91ebd257
 						p += 10;
 						memcpy(p, sproto.s, sproto.len);
 						p += sproto.len;
 						*p = '\0';
 						dst_uri.len = p - dst_uri_buf;
 						break;
2c62b317
 					}
91ebd257
 				} else {
3541d0f2
 					LM_ERR("invalid number of separators (%d)\n", n);
91ebd257
 					goto done;
2c62b317
 				}
 			}
91ebd257
 			p++;
2c62b317
 		}
46a0dab3
 	}
91ebd257
 
 	LM_DBG("setting dst uri: %.*s\n", dst_uri.len, dst_uri.s);
 	if (set_dst_uri(_m, &dst_uri) != 0) {
 		LM_ERR("failed to set dst-uri\n");
 		free_params(params);
 		return;
 	}
 	/* dst_uri changed, so it makes sense to re-use the current uri
 	 * for forking */
 	ruri_mark_new(); /* re-use uri for serial forking */
 
2c62b317
 done:
1f3a6a4f
 	free_params(params);
46a0dab3
 }