/
pcontact.c
399 lines (351 loc) · 12 KB
/
pcontact.c
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
395
396
397
398
399
/*
* $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
*
*/
#include "pcontact.h"
#include <string.h>
#include "../../core/hashes.h"
#include "../../core/mem/shm_mem.h"
#include "../../core/dprint.h"
#include "../../core/ut.h"
#include "ims_usrloc_pcscf_mod.h"
#include "usrloc.h"
#include "utime.h"
#include "ul_callback.h"
#include "usrloc.h"
#include "../../lib/ims/useful_defs.h"
#include "usrloc_db.h"
#include "../../core/parser/parse_uri.h"
extern int db_mode;
extern int expires_grace;
/*! retransmission detection interval in seconds */
int cseq_delay = 20;
void insert_ppublic(struct pcontact* _c, ppublic_t* _p)
{
LM_DBG("linking IMPU <%.*s> to contact <%.*s>\n",
_p->public_identity.len,_p->public_identity.s,
_c->aor.len, _c->aor.s);
if (_c->head == 0) {//first entry
_c->head = _c->tail = _p;
} else {
_p->prev = _c->tail;
_c->tail->next = _p;
_c->tail = _p;
}
}
int new_ppublic(str* public_identity, int is_default, ppublic_t** _p)
{
*_p = (ppublic_t*)shm_malloc(sizeof(ppublic_t));
if (!*_p) {
LM_ERR("no more shm memory\n");
return -1;
}
(*_p)->next=(*_p)->prev=0;
(*_p)->public_identity.s = (char*)shm_malloc(public_identity->len);
if (!(*_p)->public_identity.s) {
LM_ERR("no more shm memory\n");
if (*_p) {
shm_free(*_p);
}
return -1;
}
(*_p)->is_default = is_default;
memcpy((*_p)->public_identity.s, public_identity->s, public_identity->len);
(*_p)->public_identity.len = public_identity->len;
return 0;
}
void free_ppublic(ppublic_t* _p)
{
if (!_p)
return;
if (_p->public_identity.s) {
shm_free(_p->public_identity.s);
}
shm_free(_p);
}
// The same piece of code also lives in modules/ims_registrar_pcscf/sec_agree.c
// Function - parse_sec_agree()
// goto label - cleanup
// Keep them in sync!
void free_security(security_t* _p)
{
if (!_p) {
return;
}
if(_p->sec_header.s) {
shm_free(_p->sec_header.s);
}
switch (_p->type)
{
case SECURITY_IPSEC:
if(_p->data.ipsec){
if(_p->data.ipsec->ealg.s) shm_free(_p->data.ipsec->ealg.s);
if(_p->data.ipsec->r_ealg.s) shm_free(_p->data.ipsec->r_ealg.s);
if(_p->data.ipsec->ck.s) shm_free(_p->data.ipsec->ck.s);
if(_p->data.ipsec->alg.s) shm_free(_p->data.ipsec->alg.s);
if(_p->data.ipsec->r_alg.s) shm_free(_p->data.ipsec->r_alg.s);
if(_p->data.ipsec->ik.s) shm_free(_p->data.ipsec->ik.s);
if(_p->data.ipsec->prot.s) shm_free(_p->data.ipsec->prot.s);
if(_p->data.ipsec->mod.s) shm_free(_p->data.ipsec->mod.s);
shm_free(_p->data.ipsec);
}
break;
case SECURITY_TLS:
if(_p->data.tls) {
shm_free(_p->data.tls);
}
break;
case SECURITY_NONE:
//Nothing to deallocate
break;
}
shm_free(_p);
}
int new_pcontact(struct udomain* _d, str* _contact, struct pcontact_info* _ci, struct pcontact** _c)
{
int i, has_rinstance=0;
ppublic_t* ppublic_ptr;
int is_default = 1, params_len;
struct sip_uri sip_uri;
char* p, *params, *sep;
str rinstance = {0,0};
*_c = (pcontact_t*)shm_malloc(sizeof(pcontact_t) + _contact->len + _ci->received_host.len + _ci->via_host.len);
if (*_c == 0) {
LM_ERR("no more shared memory\n");
return -1;
}
memset(*_c, 0, sizeof(pcontact_t));
LM_DBG("New contact [<%.*s>] with %d associated IMPUs in state: [%s]\n",
_contact->len, _contact->s,
_ci->num_public_ids,
reg_state_to_string(_ci->reg_state));
p = (char*)((struct pcontact*)(*_c) + 1);
(*_c)->aor.s = p;
memcpy(p, _contact->s, _contact->len);
p += _contact->len;
(*_c)->aor.len = _contact->len;
(*_c)->domain = (str*)_d;
if (parse_uri((*_c)->aor.s, (*_c)->aor.len, &sip_uri) != 0) {
LM_ERR("unable to determine contact host from uri [%.*s\n", (*_c)->aor.len, (*_c)->aor.s);
shm_free((*_c)->aor.s);
shm_free(*_c);
*_c = 0;
return -2;
}
/* is there an rinstance param */
LM_DBG("checking for rinstance");
/*check for alias - NAT */
params = sip_uri.sip_params.s;
params_len = sip_uri.sip_params.len;
while (params_len >= RINSTANCE_LEN) {
if (strncmp(params, RINSTANCE, RINSTANCE_LEN) == 0) {
has_rinstance = 1;
break;
}
sep = memchr(params, 59 /* ; */, params_len);
if (sep == NULL) {
LM_DBG("no rinstance param\n");
break;
} else {
params_len = params_len - (sep - params + 1);
params = sep + 1;
}
}
if (has_rinstance) {
rinstance.s = params + RINSTANCE_LEN;
rinstance.len = params_len - RINSTANCE_LEN;
sep = (char*)memchr(rinstance.s, 59 /* ; */, rinstance.len);
if (sep != NULL){
rinstance.len = (sep-rinstance.s);
}
}
(*_c)->rinstance.s = rinstance.s;
(*_c)->rinstance.len = rinstance.len;
(*_c)->contact_host.s = sip_uri.host.s;
(*_c)->contact_host.len = sip_uri.host.len;
(*_c)->contact_port = sip_uri.port_no?sip_uri.port_no:5060;
(*_c)->contact_user.s = sip_uri.user.s;
(*_c)->contact_user.len = sip_uri.user.len;
(*_c)->expires = _ci->expires;
(*_c)->reg_state = _ci->reg_state;
// Add received Info:
if (_ci->received_host.len > 0 && _ci->received_host.s) {
(*_c)->received_host.s = p;
memcpy(p, _ci->received_host.s, _ci->received_host.len);
p += _ci->received_host.len;
(*_c)->received_host.len = _ci->received_host.len;
(*_c)->received_port = _ci->received_port;
(*_c)->received_proto = _ci->received_proto;
}
if (_ci->via_host.len > 0 && _ci->via_host.s) {
(*_c)->via_host.s = p;
memcpy(p, _ci->via_host.s, _ci->via_host.len);
p += _ci->via_host.len;
(*_c)->via_host.len = _ci->via_host.len;
(*_c)->via_port = _ci->via_port;
(*_c)->via_proto = _ci->via_prot;
}
(*_c)->aorhash = get_aor_hash(_d, &_ci->via_host, _ci->via_port, _ci->via_prot);
//setup public ids
for (i=0; i<_ci->num_public_ids; i++) {
if (i>0) is_default = 0; //only the first one is default - P-Associated-uri (first one is default)
if (new_ppublic(&_ci->public_ids[i], is_default, &ppublic_ptr)!=0) {
LM_ERR("unable to create new ppublic\n");
} else {
insert_ppublic(*_c, ppublic_ptr);
}
}
//add the service routes
if (_ci->num_service_routes > 0) {
(*_c)->service_routes = shm_malloc(_ci->num_service_routes * sizeof(str));
if (!(*_c)->service_routes) {
LM_ERR("no more shm mem\n");
goto out_of_memory;
} else {
for (i = 0; i < _ci->num_service_routes; i++) {
STR_SHM_DUP((*_c)->service_routes[i], _ci->service_routes[i], "new_pcontact");
}
(*_c)->num_service_routes = _ci->num_service_routes;
}
}
LM_DBG("New contact host:port [%.*s:%d]\n", (*_c)->contact_host.len, (*_c)->contact_host.s, (*_c)->contact_port);
LM_DBG("New contact via host:port:proto: [%.*s:%d:%d]\n", (*_c)->via_host.len, (*_c)->via_host.s, (*_c)->via_port, (*_c)->via_proto);
LM_DBG("New contact received host:port:proto: [%.*s:%d:%d]\n", (*_c)->received_host.len, (*_c)->received_host.s, (*_c)->received_port, (*_c)->received_proto);
LM_DBG("New contact aorhash [%u]\n", (*_c)->aorhash);
return 0;
out_of_memory:
return -1;
}
void free_pcontact(pcontact_t* _c) {
ppublic_t* p, *tmp;
int i;
if (!_c)
return;
/*free callbacks*/
if (_c->cbs.first) {
destroy_ul_callbacks_list(_c->cbs.first);
}
LM_DBG("freeing pcontact: <%.*s>\n", _c->aor.len, _c->aor.s);
//free ppublics
p = _c->head;
while (p) {
LM_DBG("freeing linked IMPI: <%.*s>\n", p->public_identity.len, p->public_identity.s);
tmp = p->next;
free_ppublic(p);
p = tmp;
}
//free service_routes
if (_c->service_routes) { //remove old service routes
for (i = 0; i < _c->num_service_routes; i++) {
if (_c->service_routes[i].s)
shm_free(_c->service_routes[i].s);
}
shm_free(_c->service_routes);
_c->service_routes = 0;
_c->num_service_routes = 0;
}
// free_security() checks for NULL ptr
free_security(_c->security_temp);
free_security(_c->security);
if (_c->rx_session_id.len > 0 && _c->rx_session_id.s)
shm_free(_c->rx_session_id.s);
shm_free(_c);
}
static inline void nodb_timer(pcontact_t* _c)
{
LM_DBG("Running nodb timer on <%.*s>, "
"Reg state: %s, "
"Expires: %d, "
"Expires in: %d seconds, "
"Received: %.*s:%d, "
"Path: %.*s, "
"Proto: %d, "
"Hash: %u, "
"Slot: %u\n",
_c->aor.len, _c->aor.s,
reg_state_to_string(_c->reg_state),
(int)_c->expires,
(int)(_c->expires - time(NULL)),
_c->received_host.len, _c->received_host.s,
_c->received_port,
_c->path.len, _c->path.s,
_c->received_proto,
_c->aorhash,
_c->sl);
get_act_time();
if ((_c->expires - act_time) + expires_grace <= 0) {//we've allowed some grace time TODO: add as parameter
//if ((_c->expires - act_time) <= -10) {//we've allowed some grace time TODO: add as parameter
LM_DBG("pcscf contact <%.*s> has expired and will be removed\n", _c->aor.len, _c->aor.s);
if (exists_ulcb_type(PCSCF_CONTACT_EXPIRE)) {
run_ul_callbacks(PCSCF_CONTACT_EXPIRE, _c);
}
if (db_mode == WRITE_THROUGH && db_delete_pcontact(_c) != 0) {
LM_ERR("Error deleting ims_usrloc_pcscf record in DB");
}
update_stat(_c->slot->d->expired, 1);
mem_delete_pcontact(_c->slot->d, _c);
return;
}
//TODO: this is just for tmp debugging
// p = _c->head;
// while (p) {
// if (p->is_default)
// LM_DBG("public identity %i (default): <%.*s>\n", i, p->public_identity.len, p->public_identity.s);
// else
// LM_DBG("public identity %i: <%.*s>\n", i, p->public_identity.len, p->public_identity.s);
// i++;
// p=p->next;
// }
//
// LM_DBG("There are %i service routes as follows:\n", _c->num_service_routes);
// for (i=0; i<_c->num_service_routes; i++) {
// LM_DBG("service route %i: <%.*s>\n", i+1, _c->service_routes[i].len, _c->service_routes[i].s);
// }
}
void timer_pcontact(pcontact_t* _r)
{
nodb_timer(_r);
}
void print_pcontact(FILE* _f, pcontact_t* _r)
{
}