File: rx_authdata.c

package info (click to toggle)
kamailio 4.2.0-2%2Bdeb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (394 lines) | stat: -rw-r--r-- 15,962 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * $Id$
 *
 * Copyright (C) 2012 Smile Communications, jason.penton@smilecoms.com
 * Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
 * 
 * The initial version of this code was written by Dragos Vingarzan
 * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
 * Fruanhofer Institute. It was and still is maintained in a separate
 * branch of the original SER. We are therefore migrating it to
 * Kamailio/SR and look forward to maintaining it from here on out.
 * 2011/2012 Smile Communications, Pty. Ltd.
 * ported/maintained/improved by 
 * Jason Penton (jason(dot)penton(at)smilecoms.com and
 * Richard Good (richard(dot)good(at)smilecoms.com) as part of an 
 * effort to add full IMS support to Kamailio/SR using a new and
 * improved architecture
 * 
 * NB: Alot of this code was originally part of OpenIMSCore,
 * FhG Fokus. 
 * Copyright (C) 2004-2006 FhG Fokus
 * Thanks for great work! This is an effort to 
 * break apart the various CSCF functions into logically separate
 * components. We hope this will drive wider use. We also feel
 * that in this way the architecture is more complete and thereby easier
 * to manage in the Kamailio/SR environment
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio 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
 *
 * Kamailio 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 *
 *
 * History:
 * --------
 *  2011-02-02  initial version (jason.penton)
 */

#include "../../sr_module.h"
#include "../../ut.h"
#include "../../mem/shm_mem.h"
#include "../../parser/contact/parse_contact.h"
#include "../../locking.h"
#include "../tm/tm_load.h"
#include "../../modules/dialog_ng/dlg_load.h"
#include "../../modules/dialog_ng/dlg_hash.h"
#include "../ims_usrloc_pcscf/usrloc.h"

#include "../../mem/shm_mem.h"
#include "../../parser/sdp/sdp_helpr_funcs.h"
#include "../../parser/sdp/sdp.h"
#include "../../parser/parse_rr.h"
#include "../cdp/cdp_load.h"
#include "rx_authdata.h"
#include "rx_avp.h"
#include "../../lib/ims/ims_getters.h"
#include "mod.h"

int create_new_regsessiondata(str* domain, str* aor, rx_authsessiondata_t** session_data) {

	int len = (domain->len + 1) + aor->len + sizeof(rx_authsessiondata_t);
	rx_authsessiondata_t* p_session_data = shm_malloc(len);
	if (!p_session_data) {
		LM_ERR("no more shm memory\n");
		return -1;
	}
	memset(p_session_data, 0, len);

	p_session_data->subscribed_to_signaling_path_status = 1;
        p_session_data->must_terminate_dialog = 0; /*irrelevent for reg session data this will always be 0 */

	char* p = (char*)(p_session_data + 1);
	p_session_data->domain.s = p;
	memcpy(p, domain->s, domain->len);
	p_session_data->domain.len = domain->len;
	p += domain->len;
	*p++ = '\0';

	p_session_data->registration_aor.s = p;
	memcpy(p, aor->s, aor->len);
	p_session_data->registration_aor.len = aor->len;
	p += aor->len;
	if (p != (((char*)p_session_data) + len)) {
		LM_ERR("buffer over/underflow\n");
		shm_free(p_session_data);
		p_session_data = 0;
		return -1;
	}
	*session_data = p_session_data;

	return 1;
}

int create_new_callsessiondata(str* callid, str* ftag, str* ttag, str* identifier, str* ip, int ip_version, rx_authsessiondata_t** session_data) {

	int len = callid->len + ftag->len + ttag->len + identifier->len + ip->len + sizeof(rx_authsessiondata_t);
	rx_authsessiondata_t* call_session_data = shm_malloc(len);
	if (!call_session_data){
		LM_ERR("no more shm mem trying to create call_session_data of size %d\n", len);
		return -1;
	}
	memset(call_session_data, 0, len);
	call_session_data->subscribed_to_signaling_path_status = 0; //this is for a media session not regitration
        call_session_data->must_terminate_dialog = 0; //this is used to determine if the dialog must be torn down when the CDP session terminates
	
	call_session_data->first_current_flow_description=0;
	call_session_data->first_new_flow_description=0;
	call_session_data->ip_version = ip_version;
	
	char *p = (char*)(call_session_data + 1);

	if (callid && callid->len>0 && callid->s) {
		LM_DBG("Copying callid [%.*s] into call session data\n", callid->len, callid->s);
		call_session_data->callid.s = p;
		memcpy(call_session_data->callid.s, callid->s, callid->len);
                call_session_data->callid.len = callid->len;
		p+=callid->len;
	}
	if (ftag && ftag->len > 0 && ftag->s) {
		LM_DBG("Copying ftag [%.*s] into call session data\n", ftag->len, ftag->s);
		call_session_data->ftag.s = p;
		memcpy(call_session_data->ftag.s, ftag->s, ftag->len);
                call_session_data->ftag.len = ftag->len;
		p += ftag->len;
	}
	if (ttag && ttag->len > 0 && ttag->s) {
		LM_DBG("Copying ttag [%.*s] into call session data\n", ttag->len, ttag->s);
		call_session_data->ttag.s = p;
		memcpy(call_session_data->ttag.s, ttag->s, ttag->len);
                call_session_data->ttag.len = ttag->len;
		p += ttag->len;
	}
	if (identifier && identifier->len > 0 && identifier->s) {
		LM_DBG("Copying identifier [%.*s] into call session data\n", identifier->len, identifier->s);
		call_session_data->identifier.s = p;
		memcpy(call_session_data->identifier.s, identifier->s, identifier->len);
                call_session_data->identifier.len = identifier->len;
		p += identifier->len;
	}
	if (ip && ip->len > 0 && ip->s) {
		LM_DBG("Copying ip [%.*s] into call session data\n", ip->len, ip->s);
		call_session_data->ip.s = p;
		memcpy(call_session_data->ip.s, ip->s, ip->len);
                call_session_data->ip.len = ip->len;
		p += ip->len;
	}
	if (p != ((char*)(call_session_data) + len)) {
		LM_ERR("buffer under/overflow\n");
		shm_free(call_session_data);
		return -1;
	}

	*session_data = call_session_data;
	return 1;
}

/* Param current tells us if this a current fd or a new fd to add*/
int 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) {
    
    flow_description_t *fd = 0;
    flow_description_t *tmp, *tmp1 = 0;
    
    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);
    fd = shm_malloc(len);
    if (!fd){
	    LM_ERR("no more shm mem trying to create new flow description of size %d\n", len);
	    return -1;
    }
    memset(fd, 0, len);
    
    fd->direction = direction;
    fd->stream_num = stream_num;
    
    char *p = (char*)(fd + 1);

    if (media && media->len>0 && media->s) {
	    LM_DBG("Copying media [%.*s] into flow description\n", media->len, media->s);
	    fd->media.s = p;
	    memcpy(fd->media.s, media->s, media->len);
	    fd->media.len = media->len;
	    p+=media->len;
    }
    if (req_sdp_ip_addr && req_sdp_ip_addr->len>0 && req_sdp_ip_addr->s) {
	    LM_DBG("Copying req_sdp_ip_addr [%.*s] into flow description\n", req_sdp_ip_addr->len, req_sdp_ip_addr->s);
	    fd->req_sdp_ip_addr.s = p;
	    memcpy(fd->req_sdp_ip_addr.s, req_sdp_ip_addr->s, req_sdp_ip_addr->len);
	    fd->req_sdp_ip_addr.len = req_sdp_ip_addr->len;
	    p+=req_sdp_ip_addr->len;
    }
    if (req_sdp_port && req_sdp_port->len>0 && req_sdp_port->s) {
	    LM_DBG("Copying req_sdp_port [%.*s] into flow description\n", req_sdp_port->len, req_sdp_port->s);
	    fd->req_sdp_port.s = p;
	    memcpy(fd->req_sdp_port.s, req_sdp_port->s, req_sdp_port->len);
	    fd->req_sdp_port.len = req_sdp_port->len;
	    p+=req_sdp_port->len;
    }
    if (rpl_sdp_ip_addr && rpl_sdp_ip_addr->len>0 && rpl_sdp_ip_addr->s) {
	    LM_DBG("Copying rpl_sdp_ip_addr [%.*s] into flow description\n", rpl_sdp_ip_addr->len, rpl_sdp_ip_addr->s);
	    fd->rpl_sdp_ip_addr.s = p;
	    memcpy(fd->rpl_sdp_ip_addr.s, rpl_sdp_ip_addr->s, rpl_sdp_ip_addr->len);
	    fd->rpl_sdp_ip_addr.len = rpl_sdp_ip_addr->len;
	    p+=rpl_sdp_ip_addr->len;
    }
    if (rpl_sdp_port && rpl_sdp_port->len>0 && rpl_sdp_port->s) {
	    LM_DBG("Copying rpl_sdp_port [%.*s] into flow description\n", rpl_sdp_port->len, rpl_sdp_port->s);
	    fd->rpl_sdp_port.s = p;
	    memcpy(fd->rpl_sdp_port.s, rpl_sdp_port->s, rpl_sdp_port->len);
	    fd->rpl_sdp_port.len = rpl_sdp_port->len;
	    p+=rpl_sdp_port->len;
    }
    if (rpl_sdp_transport && rpl_sdp_transport->len>0 && rpl_sdp_transport->s) {
	    LM_DBG("Copying rpl_sdp_transport [%.*s] into flow description\n", rpl_sdp_transport->len, rpl_sdp_transport->s);
	    fd->rpl_sdp_transport.s = p;
	    memcpy(fd->rpl_sdp_transport.s, rpl_sdp_transport->s, rpl_sdp_transport->len);
	    fd->rpl_sdp_transport.len = rpl_sdp_transport->len;
	    p+=rpl_sdp_transport->len;
    }
    if (req_sdp_raw_stream && req_sdp_raw_stream->len>0 && req_sdp_raw_stream->s) {
	    LM_DBG("Copying req_sdp_raw_stream [%.*s] into flow description\n", req_sdp_raw_stream->len, req_sdp_raw_stream->s);
	    fd->req_sdp_raw_stream.s = p;
	    memcpy(fd->req_sdp_raw_stream.s, req_sdp_raw_stream->s, req_sdp_raw_stream->len);
	    fd->req_sdp_raw_stream.len = req_sdp_raw_stream->len;
	    p+=req_sdp_raw_stream->len;
    }
    if (rpl_sdp_raw_stream && rpl_sdp_raw_stream->len>0 && rpl_sdp_raw_stream->s) {
	    LM_DBG("Copying rpl_sdp_raw_stream [%.*s] into flow description\n", rpl_sdp_raw_stream->len, rpl_sdp_raw_stream->s);
	    fd->rpl_sdp_raw_stream.s = p;
	    memcpy(fd->rpl_sdp_raw_stream.s, rpl_sdp_raw_stream->s, rpl_sdp_raw_stream->len);
	    fd->rpl_sdp_raw_stream.len = rpl_sdp_raw_stream->len;
	    p+=rpl_sdp_raw_stream->len;
    }
	
    if (p != ((char*)(fd) + len)) {
	    LM_ERR("buffer under/overflow\n");
	    shm_free(fd);
	    return -1;
    }

    fd->next=0;

    if(current){
	LM_DBG("Adding current flow description\n");
	    if(session_data->first_current_flow_description == 0) {
		LM_DBG("This is the first\n");
		session_data->first_current_flow_description = fd;
	} else{
	    LM_DBG("This is NOT the first - adding to the list\n");
	    tmp = session_data->first_current_flow_description;
	     while (tmp) {
		tmp1 = tmp->next;
		if(!tmp1) {
		    break;
		}
		tmp = tmp1;
	    }
	    tmp->next = fd;
	}
    } else {
	LM_DBG("Adding new flow description\n");
	    if(session_data->first_new_flow_description == 0) {
		LM_DBG("This is the first\n");
		session_data->first_new_flow_description = fd;
	} else{
	    LM_DBG("This is NOT the first - adding to the list\n");
	    tmp = session_data->first_new_flow_description;
	    //scrolls to last valid entry
	    while (tmp) {
		tmp1 = tmp->next;
		if(!tmp1) {
		    break;
		}
		tmp = tmp1;
	    }
	    tmp->next = fd;
	}
    }
    
    return 1;
}

/* Param current tells us if this a current fd or a new fd to add*/
void free_flow_description(rx_authsessiondata_t* session_data, int current) {
    
    flow_description_t *flow_description;
    flow_description_t *flow_description_tmp;
    if(!session_data){
	return;
    }
    
    if(current) {
	LM_DBG("Destroy current flow description\n");
	flow_description = session_data->first_current_flow_description;
	if(!flow_description) {
	    return;
	}
    } else {
	LM_DBG("Destroy new flow description\n");
	flow_description = session_data->first_new_flow_description;
	if(!flow_description) {
	    return;
	}
    }
    
    while (flow_description) {
        flow_description_tmp = flow_description->next;
        shm_free(flow_description);
	flow_description = 0;
	flow_description = flow_description_tmp;
    }
}

void free_callsessiondata(rx_authsessiondata_t* session_data) {

    if(!session_data){
	return;
    }
    
    LM_DBG("Destroy current flow description\n");
    free_flow_description(session_data, 1);
    
    LM_DBG("Destroy new flow description\n");
    free_flow_description(session_data, 0);
    
    LM_DBG("Destroy session data\n");
    shm_free(session_data);
    session_data = 0;
}

void show_callsessiondata(rx_authsessiondata_t* session_data) {
    
    flow_description_t *flow_description;
    
    if(!session_data){
	return;
    }
    
    LM_DBG("Session data:\n");
    LM_DBG("=====================\n");
    LM_DBG("Call id [%.*s]\n", session_data->callid.len, session_data->callid.s);
    LM_DBG("Domain [%.*s]\n", session_data->domain.len, session_data->domain.s);
    LM_DBG("Ftag [%.*s]\n", session_data->ftag.len, session_data->ftag.s);
    LM_DBG("Ttag [%.*s]\n", session_data->ttag.len, session_data->ttag.s);
    LM_DBG("Identifier [%.*s]\n", session_data->identifier.len, session_data->identifier.s);
    LM_DBG("Registration AOR [%.*s]\n", session_data->registration_aor.len, session_data->registration_aor.s);
    LM_DBG("IP [%.*s]\n", session_data->ip.len, session_data->ip.s);
    LM_DBG("IP version [%d]\n", session_data->ip_version);
    LM_DBG("Must terminate dialog [%d]\n", session_data->must_terminate_dialog);
    LM_DBG("Subscribed to signalling path status [%d]\n", session_data->subscribed_to_signaling_path_status);
    
    flow_description = session_data->first_current_flow_description;
    while(flow_description) {
	LM_DBG("Current Flow description [%d]\n", flow_description->stream_num);
	LM_DBG("\tMedia [%.*s]\n", flow_description->media.len, flow_description->media.s);
	LM_DBG("\tReq_sdp_ip_addr [%.*s]\n", flow_description->req_sdp_ip_addr.len, flow_description->req_sdp_ip_addr.s);
	LM_DBG("\tReq_sdp_port [%.*s]\n", flow_description->req_sdp_port.len, flow_description->req_sdp_port.s);
	LM_DBG("\tReq_sdp_raw_stream [%.*s]\n", flow_description->req_sdp_raw_stream.len, flow_description->req_sdp_raw_stream.s);
	LM_DBG("\tRpl_sdp_ip_addr [%.*s]\n", flow_description->rpl_sdp_ip_addr.len, flow_description->rpl_sdp_ip_addr.s);
	LM_DBG("\tRpl_sdp_port [%.*s]\n", flow_description->rpl_sdp_port.len, flow_description->rpl_sdp_port.s);
	LM_DBG("\tRpl_sdp_raw_stream [%.*s]\n", flow_description->rpl_sdp_raw_stream.len, flow_description->rpl_sdp_raw_stream.s);
	LM_DBG("\tRpl_sdp_transport [%.*s]\n", flow_description->rpl_sdp_transport.len, flow_description->rpl_sdp_transport.s);
	LM_DBG("\tDirection [%d]\n", flow_description->direction);
	flow_description = flow_description->next;
    }
    flow_description = session_data->first_new_flow_description;
    while(flow_description) {
	LM_DBG("New Flow description [%d]\n", flow_description->stream_num);
	LM_DBG("\tMedia [%.*s]\n", flow_description->media.len, flow_description->media.s);
	LM_DBG("\tReq_sdp_ip_addr [%.*s]\n", flow_description->req_sdp_ip_addr.len, flow_description->req_sdp_ip_addr.s);
	LM_DBG("\tReq_sdp_port [%.*s]\n", flow_description->req_sdp_port.len, flow_description->req_sdp_port.s);
	LM_DBG("\tReq_sdp_raw_stream [%.*s]\n", flow_description->req_sdp_raw_stream.len, flow_description->req_sdp_raw_stream.s);
	LM_DBG("\tRpl_sdp_ip_addr [%.*s]\n", flow_description->rpl_sdp_ip_addr.len, flow_description->rpl_sdp_ip_addr.s);
	LM_DBG("\tRpl_sdp_port [%.*s]\n", flow_description->rpl_sdp_port.len, flow_description->rpl_sdp_port.s);
	LM_DBG("\tRpl_sdp_raw_stream [%.*s]\n", flow_description->rpl_sdp_raw_stream.len, flow_description->rpl_sdp_raw_stream.s);
	LM_DBG("\tRpl_sdp_transport [%.*s]\n", flow_description->rpl_sdp_transport.len, flow_description->rpl_sdp_transport.s);
	LM_DBG("\tDirection [%d]\n", flow_description->direction);
	flow_description = flow_description->next;
    }
    
    LM_DBG("=====================\n");
}