1 /*
2  * Copyright (C) 2014 Federico Cabiddu (federico.cabiddu@gmail.com)
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * Kamailio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 
26 #include "../../core/sr_module.h"
27 #include "../../core/dprint.h"
28 #include "../../core/mod_fix.h"
29 #include "../../core/route.h"
30 #include "../../core/data_lump.h"
31 #include "../../core/dset.h"
32 #include "../../core/script_cb.h"
33 #include "../../core/parser/msg_parser.h"
34 #include "../../core/parser/contact/parse_contact.h"
35 #include "tsilo.h"
36 #include "ts_hash.h"
37 #include "ts_append.h"
38 
ts_append(struct sip_msg * msg,str * ruri,char * table)39 int ts_append(struct sip_msg* msg, str *ruri, char *table) {
40 	ts_urecord_t* _r;
41 	ts_transaction_t* ptr;
42 
43 	struct sip_uri p_uri;
44 	str *t_uri;
45 
46 	int res;
47 	int appended;
48 
49 	if (use_domain) {
50 		t_uri = ruri;
51 	} else {
52 		if(parse_uri(ruri->s, ruri->len, &p_uri)<0) {
53 			LM_ERR("failed to parse uri %.*s\n", ruri->len, ruri->s);
54 			return -1;
55 		}
56 		t_uri = &p_uri.user;
57 	}
58 
59 	lock_entry_by_ruri(t_uri);
60 
61 	res = get_ts_urecord(t_uri, &_r);
62 
63 	if (res != 0) {
64 		LM_ERR("failed to retrieve record for %.*s\n", t_uri->len, t_uri->s);
65 		unlock_entry_by_ruri(t_uri);
66 		return -1;
67 	}
68 
69 	ptr = _r->transactions;
70 
71 	while(ptr) {
72 		LM_DBG("transaction %u:%u found for %.*s, going to append branches\n",ptr->tindex, ptr->tlabel, t_uri->len, t_uri->s);
73 
74 		appended = ts_append_to(msg, ptr->tindex, ptr->tlabel, table, ruri);
75 		if (appended > 0)
76 			update_stat(added_branches, appended);
77 		ptr = ptr->next;
78 	}
79 
80 	unlock_entry_by_ruri(t_uri);
81 
82 	return 1;
83 }
84 
ts_append_to(struct sip_msg * msg,int tindex,int tlabel,char * table,str * uri)85 int ts_append_to(struct sip_msg* msg, int tindex, int tlabel, char *table, str *uri) {
86 	struct cell     *t=0;
87 	struct cell     *orig_t;
88 	struct sip_msg *orig_msg;
89 	int ret;
90 	str stable;
91 
92 	orig_t = _tmb.t_gett();
93 
94 	if(_tmb.t_lookup_ident(&t, tindex, tlabel) < 0)
95 	{
96 		LM_ERR("transaction [%u:%u] not found\n",
97 				tindex, tlabel);
98 		ret = -1;
99 		goto done;
100 	}
101 
102 	if (t->flags & T_CANCELED) {
103 		LM_DBG("trasaction [%u:%u] was cancelled\n",
104 				tindex, tlabel);
105 		ret = -2;
106 		goto done;
107 	}
108 
109 	if (t->uas.status >= 200) {
110 		LM_DBG("trasaction [%u:%u] sent out a final response already - %d\n",
111 				tindex, tlabel, t->uas.status);
112 		ret = -3;
113 		goto done;
114 	}
115 
116 	orig_msg = t->uas.request;
117 
118 	stable.s = table;
119 	stable.len = strlen(stable.s);
120 	if(uri==NULL || uri->s==NULL || uri->len<=0) {
121 		ret = _regapi.lookup_to_dset(orig_msg, &stable, NULL);
122 	} else {
123 		ret = _regapi.lookup_to_dset(orig_msg, &stable, uri);
124 	}
125 
126 	if(ret != 1) {
127 		LM_DBG("transaction %u:%u: error updating dset (%d)\n", tindex, tlabel, ret);
128 		ret = -4;
129 		goto done;
130 	}
131 
132 	ret = _tmb.t_append_branches();
133 
134 done:
135 	/* unref the transaction which had been referred by t_lookup_ident() call.
136 	 * Restore the original transaction (if any) */
137 	if(t) _tmb.unref_cell(t);
138 	_tmb.t_sett(orig_t, T_BR_UNDEFINED);
139 
140 	return ret;
141 }
142