1 /*
2 * $Id$
3 *
4 * Copyright (C) 2012 Smile Communications, jason.penton@smilecoms.com
5 * Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
6 *
7 * The initial version of this code was written by Dragos Vingarzan
8 * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
9 * Fruanhofer Institute. It was and still is maintained in a separate
10 * branch of the original SER. We are therefore migrating it to
11 * Kamailio/SR and look forward to maintaining it from here on out.
12 * 2011/2012 Smile Communications, Pty. Ltd.
13 * ported/maintained/improved by
14 * Jason Penton (jason(dot)penton(at)smilecoms.com and
15 * Richard Good (richard(dot)good(at)smilecoms.com) as part of an
16 * effort to add full IMS support to Kamailio/SR using a new and
17 * improved architecture
18 *
19 * NB: Alot of this code was originally part of OpenIMSCore,
20 * FhG Fokus.
21 * Copyright (C) 2004-2006 FhG Fokus
22 * Thanks for great work! This is an effort to
23 * break apart the various CSCF functions into logically separate
24 * components. We hope this will drive wider use. We also feel
25 * that in this way the architecture is more complete and thereby easier
26 * to manage in the Kamailio/SR environment
27 *
28 * This file is part of Kamailio, a free SIP server.
29 *
30 * Kamailio is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version
34 *
35 * Kamailio is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
43 *
44 *
45 *
46 * History:
47 * --------
48 * 2011-02-02 initial version (jason.penton)
49 */
50
51 #include "../../core/sr_module.h"
52 #include "../../core/ut.h"
53 #include "../../core/mem/shm_mem.h"
54 #include "../../core/parser/contact/parse_contact.h"
55 #include "../../core/locking.h"
56 #include "../tm/tm_load.h"
57 #include "../../modules/ims_dialog/dlg_load.h"
58 #include "../../modules/ims_dialog/dlg_hash.h"
59 #include "../ims_usrloc_pcscf/usrloc.h"
60
61 #include "../../core/mem/shm_mem.h"
62 #include "../../core/parser/sdp/sdp_helpr_funcs.h"
63 #include "../../core/parser/sdp/sdp.h"
64 #include "../../core/parser/parse_rr.h"
65 #include "../cdp/cdp_load.h"
66 #include "rx_authdata.h"
67 #include "rx_avp.h"
68 #include "../../lib/ims/ims_getters.h"
69 #include "ims_qos_mod.h"
70
create_new_regsessiondata(str * domain,str * aor,str * ip,int ip_version,int recv_port,unsigned short recv_proto,str * via_host,unsigned short via_port,unsigned short via_proto,rx_authsessiondata_t ** session_data)71 int create_new_regsessiondata(str* domain, str* aor, str *ip, int ip_version, int recv_port, unsigned short recv_proto, str *via_host, unsigned short via_port, unsigned short via_proto, rx_authsessiondata_t** session_data) {
72
73 int len = (domain->len + 1) + ip->len + aor->len + via_host->len + sizeof(rx_authsessiondata_t);
74 rx_authsessiondata_t* p_session_data = shm_malloc(len);
75 if (!p_session_data) {
76 LM_ERR("no more shm memory\n");
77 return -1;
78 }
79 memset(p_session_data, 0, len);
80
81 p_session_data->subscribed_to_signaling_path_status = 1;
82 p_session_data->must_terminate_dialog = 0; /*irrelevent for reg session data this will always be 0 */
83
84 p_session_data->session_has_been_opened = 0; /*0 has not been opened 1 has been opened*/
85 p_session_data->ip_version = ip_version;
86 p_session_data->via_port = via_port;
87 p_session_data->via_proto = via_proto;
88
89 p_session_data->recv_port = recv_port;
90 p_session_data->recv_proto = recv_proto;
91
92 char* p = (char*)(p_session_data + 1);
93 p_session_data->domain.s = p;
94 memcpy(p, domain->s, domain->len);
95 p_session_data->domain.len = domain->len;
96 p += domain->len;
97 *p++ = '\0';
98
99 p_session_data->registration_aor.s = p;
100 memcpy(p, aor->s, aor->len);
101 p_session_data->registration_aor.len = aor->len;
102 p += aor->len;
103
104 p_session_data->ip.s = p;
105 memcpy(p, ip->s, ip->len);
106 p_session_data->ip.len = ip->len;
107 p += ip->len;
108
109 p_session_data->via_host.s = p;
110 memcpy(p, via_host->s, via_host->len);
111 p_session_data->via_host.len = via_host->len;
112 p += via_host->len;
113
114 if (p != (((char*)p_session_data) + len)) {
115 LM_ERR("buffer over/underflow\n");
116 shm_free(p_session_data);
117 p_session_data = 0;
118 return -1;
119 }
120 *session_data = p_session_data;
121
122 return 1;
123 }
124
create_new_callsessiondata(str * callid,str * ftag,str * ttag,str * identifier,int identifier_type,str * ip,int ip_version,rx_authsessiondata_t ** session_data)125 int create_new_callsessiondata(str* callid, str* ftag, str* ttag, str* identifier, int identifier_type, str* ip, int ip_version, rx_authsessiondata_t** session_data) {
126
127 int len = callid->len + ftag->len + ttag->len + identifier->len + ip->len + sizeof(rx_authsessiondata_t);
128 rx_authsessiondata_t* call_session_data = shm_malloc(len);
129 if (!call_session_data){
130 LM_ERR("no more shm mem trying to create call_session_data of size %d\n", len);
131 return -1;
132 }
133 memset(call_session_data, 0, len);
134 call_session_data->subscribed_to_signaling_path_status = 0; //this is for a media session not regitration
135 call_session_data->must_terminate_dialog = 0; //this is used to determine if the dialog must be torn down when the CDP session terminates
136
137 call_session_data->first_current_flow_description=0;
138 call_session_data->first_new_flow_description=0;
139 call_session_data->ip_version = ip_version;
140 call_session_data->identifier_type = identifier_type;
141
142 call_session_data->session_has_been_opened = 0; /*0 has not been opened 1 has been opened*/
143
144 char *p = (char*)(call_session_data + 1);
145
146 if (callid && callid->len>0 && callid->s) {
147 LM_DBG("Copying callid [%.*s] into call session data\n", callid->len, callid->s);
148 call_session_data->callid.s = p;
149 memcpy(call_session_data->callid.s, callid->s, callid->len);
150 call_session_data->callid.len = callid->len;
151 p+=callid->len;
152 }
153 if (ftag && ftag->len > 0 && ftag->s) {
154 LM_DBG("Copying ftag [%.*s] into call session data\n", ftag->len, ftag->s);
155 call_session_data->ftag.s = p;
156 memcpy(call_session_data->ftag.s, ftag->s, ftag->len);
157 call_session_data->ftag.len = ftag->len;
158 p += ftag->len;
159 }
160 if (ttag && ttag->len > 0 && ttag->s) {
161 LM_DBG("Copying ttag [%.*s] into call session data\n", ttag->len, ttag->s);
162 call_session_data->ttag.s = p;
163 memcpy(call_session_data->ttag.s, ttag->s, ttag->len);
164 call_session_data->ttag.len = ttag->len;
165 p += ttag->len;
166 }
167 if (identifier && identifier->len > 0 && identifier->s) {
168 LM_DBG("Copying identifier [%.*s] into call session data\n", identifier->len, identifier->s);
169 call_session_data->identifier.s = p;
170 memcpy(call_session_data->identifier.s, identifier->s, identifier->len);
171 call_session_data->identifier.len = identifier->len;
172 p += identifier->len;
173 }
174 if (ip && ip->len > 0 && ip->s) {
175 LM_DBG("Copying ip [%.*s] into call session data\n", ip->len, ip->s);
176 call_session_data->ip.s = p;
177 memcpy(call_session_data->ip.s, ip->s, ip->len);
178 call_session_data->ip.len = ip->len;
179 p += ip->len;
180 }
181 if (p != ((char*)(call_session_data) + len)) {
182 LM_ERR("buffer under/overflow\n");
183 shm_free(call_session_data);
184 return -1;
185 }
186
187 *session_data = call_session_data;
188 return 1;
189 }
190
191 /* Param current tells us if this a current fd or a new fd to add*/
add_flow_description(rx_authsessiondata_t * session_data,int stream_num,str * media,str * req_sdp_ip_addr,str * req_sdp_port,str * rpl_sdp_ip_addr,str * rpl_sdp_port,str * rpl_sdp_transport,str * req_sdp_raw_stream,str * rpl_sdp_raw_stream,int direction,int current)192 int add_flow_description(rx_authsessiondata_t* session_data, int stream_num, str *media, str *req_sdp_ip_addr, str *req_sdp_port,
193 str *rpl_sdp_ip_addr, str *rpl_sdp_port, str *rpl_sdp_transport, str *req_sdp_raw_stream, str *rpl_sdp_raw_stream, int direction, int current) {
194
195 flow_description_t *fd = 0;
196 flow_description_t *tmp, *tmp1 = 0;
197
198 int len = media->len + req_sdp_ip_addr->len + req_sdp_port->len + rpl_sdp_ip_addr->len + rpl_sdp_port->len + rpl_sdp_transport->len + req_sdp_raw_stream->len + rpl_sdp_raw_stream->len + sizeof(flow_description_t);
199 fd = shm_malloc(len);
200 if (!fd){
201 LM_ERR("no more shm mem trying to create new flow description of size %d\n", len);
202 return -1;
203 }
204 memset(fd, 0, len);
205
206 fd->direction = direction;
207 fd->stream_num = stream_num;
208
209 char *p = (char*)(fd + 1);
210
211 if (media && media->len>0 && media->s) {
212 LM_DBG("Copying media [%.*s] into flow description\n", media->len, media->s);
213 fd->media.s = p;
214 memcpy(fd->media.s, media->s, media->len);
215 fd->media.len = media->len;
216 p+=media->len;
217 }
218 if (req_sdp_ip_addr && req_sdp_ip_addr->len>0 && req_sdp_ip_addr->s) {
219 LM_DBG("Copying req_sdp_ip_addr [%.*s] into flow description\n", req_sdp_ip_addr->len, req_sdp_ip_addr->s);
220 fd->req_sdp_ip_addr.s = p;
221 memcpy(fd->req_sdp_ip_addr.s, req_sdp_ip_addr->s, req_sdp_ip_addr->len);
222 fd->req_sdp_ip_addr.len = req_sdp_ip_addr->len;
223 p+=req_sdp_ip_addr->len;
224 }
225 if (req_sdp_port && req_sdp_port->len>0 && req_sdp_port->s) {
226 LM_DBG("Copying req_sdp_port [%.*s] into flow description\n", req_sdp_port->len, req_sdp_port->s);
227 fd->req_sdp_port.s = p;
228 memcpy(fd->req_sdp_port.s, req_sdp_port->s, req_sdp_port->len);
229 fd->req_sdp_port.len = req_sdp_port->len;
230 p+=req_sdp_port->len;
231 }
232 if (rpl_sdp_ip_addr && rpl_sdp_ip_addr->len>0 && rpl_sdp_ip_addr->s) {
233 LM_DBG("Copying rpl_sdp_ip_addr [%.*s] into flow description\n", rpl_sdp_ip_addr->len, rpl_sdp_ip_addr->s);
234 fd->rpl_sdp_ip_addr.s = p;
235 memcpy(fd->rpl_sdp_ip_addr.s, rpl_sdp_ip_addr->s, rpl_sdp_ip_addr->len);
236 fd->rpl_sdp_ip_addr.len = rpl_sdp_ip_addr->len;
237 p+=rpl_sdp_ip_addr->len;
238 }
239 if (rpl_sdp_port && rpl_sdp_port->len>0 && rpl_sdp_port->s) {
240 LM_DBG("Copying rpl_sdp_port [%.*s] into flow description\n", rpl_sdp_port->len, rpl_sdp_port->s);
241 fd->rpl_sdp_port.s = p;
242 memcpy(fd->rpl_sdp_port.s, rpl_sdp_port->s, rpl_sdp_port->len);
243 fd->rpl_sdp_port.len = rpl_sdp_port->len;
244 p+=rpl_sdp_port->len;
245 }
246 if (rpl_sdp_transport && rpl_sdp_transport->len>0 && rpl_sdp_transport->s) {
247 LM_DBG("Copying rpl_sdp_transport [%.*s] into flow description\n", rpl_sdp_transport->len, rpl_sdp_transport->s);
248 fd->rpl_sdp_transport.s = p;
249 memcpy(fd->rpl_sdp_transport.s, rpl_sdp_transport->s, rpl_sdp_transport->len);
250 fd->rpl_sdp_transport.len = rpl_sdp_transport->len;
251 p+=rpl_sdp_transport->len;
252 }
253 if (req_sdp_raw_stream && req_sdp_raw_stream->len>0 && req_sdp_raw_stream->s) {
254 LM_DBG("Copying req_sdp_raw_stream [%.*s] into flow description\n", req_sdp_raw_stream->len, req_sdp_raw_stream->s);
255 fd->req_sdp_raw_stream.s = p;
256 memcpy(fd->req_sdp_raw_stream.s, req_sdp_raw_stream->s, req_sdp_raw_stream->len);
257 fd->req_sdp_raw_stream.len = req_sdp_raw_stream->len;
258 p+=req_sdp_raw_stream->len;
259 }
260 if (rpl_sdp_raw_stream && rpl_sdp_raw_stream->len>0 && rpl_sdp_raw_stream->s) {
261 LM_DBG("Copying rpl_sdp_raw_stream [%.*s] into flow description\n", rpl_sdp_raw_stream->len, rpl_sdp_raw_stream->s);
262 fd->rpl_sdp_raw_stream.s = p;
263 memcpy(fd->rpl_sdp_raw_stream.s, rpl_sdp_raw_stream->s, rpl_sdp_raw_stream->len);
264 fd->rpl_sdp_raw_stream.len = rpl_sdp_raw_stream->len;
265 p+=rpl_sdp_raw_stream->len;
266 }
267
268 if (p != ((char*)(fd) + len)) {
269 LM_ERR("buffer under/overflow\n");
270 shm_free(fd);
271 return -1;
272 }
273
274 fd->next=0;
275
276 if(current){
277 LM_DBG("Adding current flow description\n");
278 if(session_data->first_current_flow_description == 0) {
279 LM_DBG("This is the first\n");
280 session_data->first_current_flow_description = fd;
281 } else{
282 LM_DBG("This is NOT the first - adding to the list\n");
283 tmp = session_data->first_current_flow_description;
284 while (tmp) {
285 tmp1 = tmp->next;
286 if(!tmp1) {
287 break;
288 }
289 tmp = tmp1;
290 }
291 tmp->next = fd;
292 }
293 } else {
294 LM_DBG("Adding new flow description\n");
295 if(session_data->first_new_flow_description == 0) {
296 LM_DBG("This is the first\n");
297 session_data->first_new_flow_description = fd;
298 } else{
299 LM_DBG("This is NOT the first - adding to the list\n");
300 tmp = session_data->first_new_flow_description;
301 //scrolls to last valid entry
302 while (tmp) {
303 tmp1 = tmp->next;
304 if(!tmp1) {
305 break;
306 }
307 tmp = tmp1;
308 }
309 tmp->next = fd;
310 }
311 }
312
313 return 1;
314 }
315
316 /* Param current tells us if this a current fd or a new fd to add*/
free_flow_description(rx_authsessiondata_t * session_data,int current)317 void free_flow_description(rx_authsessiondata_t* session_data, int current) {
318
319 flow_description_t *flow_description;
320 flow_description_t *flow_description_tmp;
321 if(!session_data){
322 return;
323 }
324
325 if(current) {
326 LM_DBG("Destroy current flow description\n");
327 flow_description = session_data->first_current_flow_description;
328 if(!flow_description) {
329 return;
330 }
331 } else {
332 LM_DBG("Destroy new flow description\n");
333 flow_description = session_data->first_new_flow_description;
334 if(!flow_description) {
335 return;
336 }
337 }
338
339 while (flow_description) {
340 flow_description_tmp = flow_description->next;
341 shm_free(flow_description);
342 flow_description = 0;
343 flow_description = flow_description_tmp;
344 }
345 }
346
free_callsessiondata(rx_authsessiondata_t * session_data)347 void free_callsessiondata(rx_authsessiondata_t* session_data) {
348
349 if(!session_data){
350 return;
351 }
352 LM_DBG("Freeing session data for [%.*s]\n", session_data->via_host.len, session_data->via_host.s);
353 LM_DBG("Destroy current flow description\n");
354 free_flow_description(session_data, 1);
355
356 LM_DBG("Destroy new flow description\n");
357 free_flow_description(session_data, 0);
358
359 LM_DBG("Destroy session data\n");
360 shm_free(session_data);
361 session_data = 0;
362 }
363
show_callsessiondata(rx_authsessiondata_t * session_data)364 void show_callsessiondata(rx_authsessiondata_t* session_data) {
365
366 flow_description_t *flow_description;
367
368 if(!session_data){
369 return;
370 }
371
372 LM_DBG("Session data:\n");
373 LM_DBG("=====================\n");
374 LM_DBG("Call id [%.*s]\n", session_data->callid.len, session_data->callid.s);
375 LM_DBG("Domain [%.*s]\n", session_data->domain.len, session_data->domain.s);
376 LM_DBG("Ftag [%.*s]\n", session_data->ftag.len, session_data->ftag.s);
377 LM_DBG("Ttag [%.*s]\n", session_data->ttag.len, session_data->ttag.s);
378 LM_DBG("Identifier [%.*s]\n", session_data->identifier.len, session_data->identifier.s);
379 LM_DBG("Registration AOR [%.*s]\n", session_data->registration_aor.len, session_data->registration_aor.s);
380 LM_DBG("IP [%.*s]\n", session_data->ip.len, session_data->ip.s);
381 LM_DBG("IP version [%d]\n", session_data->ip_version);
382 LM_DBG("Must terminate dialog [%d]\n", session_data->must_terminate_dialog);
383 LM_DBG("Subscribed to signalling path status [%d]\n", session_data->subscribed_to_signaling_path_status);
384
385 flow_description = session_data->first_current_flow_description;
386 while(flow_description) {
387 LM_DBG("Current Flow description [%d]\n", flow_description->stream_num);
388 LM_DBG("\tMedia [%.*s]\n", flow_description->media.len, flow_description->media.s);
389 LM_DBG("\tReq_sdp_ip_addr [%.*s]\n", flow_description->req_sdp_ip_addr.len, flow_description->req_sdp_ip_addr.s);
390 LM_DBG("\tReq_sdp_port [%.*s]\n", flow_description->req_sdp_port.len, flow_description->req_sdp_port.s);
391 LM_DBG("\tReq_sdp_raw_stream [%.*s]\n", flow_description->req_sdp_raw_stream.len, flow_description->req_sdp_raw_stream.s);
392 LM_DBG("\tRpl_sdp_ip_addr [%.*s]\n", flow_description->rpl_sdp_ip_addr.len, flow_description->rpl_sdp_ip_addr.s);
393 LM_DBG("\tRpl_sdp_port [%.*s]\n", flow_description->rpl_sdp_port.len, flow_description->rpl_sdp_port.s);
394 LM_DBG("\tRpl_sdp_raw_stream [%.*s]\n", flow_description->rpl_sdp_raw_stream.len, flow_description->rpl_sdp_raw_stream.s);
395 LM_DBG("\tRpl_sdp_transport [%.*s]\n", flow_description->rpl_sdp_transport.len, flow_description->rpl_sdp_transport.s);
396 LM_DBG("\tDirection [%d]\n", flow_description->direction);
397 flow_description = flow_description->next;
398 }
399 flow_description = session_data->first_new_flow_description;
400 while(flow_description) {
401 LM_DBG("New Flow description [%d]\n", flow_description->stream_num);
402 LM_DBG("\tMedia [%.*s]\n", flow_description->media.len, flow_description->media.s);
403 LM_DBG("\tReq_sdp_ip_addr [%.*s]\n", flow_description->req_sdp_ip_addr.len, flow_description->req_sdp_ip_addr.s);
404 LM_DBG("\tReq_sdp_port [%.*s]\n", flow_description->req_sdp_port.len, flow_description->req_sdp_port.s);
405 LM_DBG("\tReq_sdp_raw_stream [%.*s]\n", flow_description->req_sdp_raw_stream.len, flow_description->req_sdp_raw_stream.s);
406 LM_DBG("\tRpl_sdp_ip_addr [%.*s]\n", flow_description->rpl_sdp_ip_addr.len, flow_description->rpl_sdp_ip_addr.s);
407 LM_DBG("\tRpl_sdp_port [%.*s]\n", flow_description->rpl_sdp_port.len, flow_description->rpl_sdp_port.s);
408 LM_DBG("\tRpl_sdp_raw_stream [%.*s]\n", flow_description->rpl_sdp_raw_stream.len, flow_description->rpl_sdp_raw_stream.s);
409 LM_DBG("\tRpl_sdp_transport [%.*s]\n", flow_description->rpl_sdp_transport.len, flow_description->rpl_sdp_transport.s);
410 LM_DBG("\tDirection [%d]\n", flow_description->direction);
411 flow_description = flow_description->next;
412 }
413
414 LM_DBG("=====================\n");
415 }
416
417