/
pua_dialoginfo.c
753 lines (655 loc) · 22.9 KB
/
pua_dialoginfo.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
* pua_dialoginfo module - publish dialog-info from dialo module
*
* Copyright (C) 2006 Voice Sistem S.R.L.
* Copyright (C) 2007-2008 Dan Pascu
* Copyright (C) 2008 Klaus Darilion IPCom
*
* 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:
* --------
* 2008-08-25 initial version (kd)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#include <time.h>
#include "../../script_cb.h"
#include "../../sr_module.h"
#include "../../parser/parse_expires.h"
#include "../../dprint.h"
#include "../../mem/shm_mem.h"
#include "../../parser/msg_parser.h"
#include "../../parser/parse_to.h"
#include "../../parser/contact/parse_contact.h"
#include "../../str.h"
#include "../../str_list.h"
#include "../../mem/mem.h"
#include "../../pt.h"
#include "../dialog/dlg_load.h"
#include "../dialog/dlg_hash.h"
#include "../pua/pua_bind.h"
#include "pua_dialoginfo.h"
MODULE_VERSION
/* Default module parameter values */
#define DEF_INCLUDE_CALLID 1
#define DEF_INCLUDE_LOCALREMOTE 1
#define DEF_INCLUDE_TAGS 1
#define DEF_OVERRIDE_LIFETIME 0
#define DEF_CALLER_ALWAYS_CONFIRMED 0
#define DEF_INCLUDE_REQ_URI 0
#define DEF_OVERRIDE_LIFETIME 0
#define DEF_SEND_PUBLISH_FLAG -1
#define DEF_USE_PUBRURI_AVPS 0
#define DEF_PUBRURI_CALLER_AVP 0
#define DEF_PUBRURI_CALLEE_AVP 0
#define DEF_CALLEE_TRYING 0
/* define PUA_DIALOGINFO_DEBUG to activate more verbose
* logging and dialog info callback debugging
*/
/* #define PUA_DIALOGINFO_DEBUG 1 */
pua_api_t pua;
struct dlg_binds dlg_api;
unsigned short pubruri_caller_avp_type;
int_str pubruri_caller_avp_name;
unsigned short pubruri_callee_avp_type;
int_str pubruri_callee_avp_name;
static str caller_dlg_var = {0, 0}; /* pubruri_caller */
static str callee_dlg_var = {0, 0}; /* pubruri_callee */
/* Module parameter variables */
int include_callid = DEF_INCLUDE_CALLID;
int include_localremote = DEF_INCLUDE_LOCALREMOTE;
int include_tags = DEF_INCLUDE_TAGS;
int override_lifetime = DEF_OVERRIDE_LIFETIME;
int caller_confirmed = DEF_CALLER_ALWAYS_CONFIRMED;
int include_req_uri = DEF_INCLUDE_REQ_URI;
int send_publish_flag = DEF_SEND_PUBLISH_FLAG;
int use_pubruri_avps = DEF_USE_PUBRURI_AVPS;
int callee_trying = DEF_CALLEE_TRYING;
char * pubruri_caller_avp = DEF_PUBRURI_CALLER_AVP;
char * pubruri_callee_avp = DEF_PUBRURI_CALLEE_AVP;
send_publish_t pua_send_publish;
/** module functions */
static int mod_init(void);
static cmd_export_t cmds[]=
{
{0, 0, 0, 0, 0, 0}
};
static param_export_t params[]={
{"include_callid", INT_PARAM, &include_callid },
{"include_localremote", INT_PARAM, &include_localremote },
{"include_tags", INT_PARAM, &include_tags },
{"override_lifetime", INT_PARAM, &override_lifetime },
{"caller_confirmed", INT_PARAM, &caller_confirmed },
{"include_req_uri", INT_PARAM, &include_req_uri },
{"send_publish_flag", INT_PARAM, &send_publish_flag },
{"use_pubruri_avps", INT_PARAM, &use_pubruri_avps },
{"pubruri_caller_avp", PARAM_STRING, &pubruri_caller_avp },
{"pubruri_callee_avp", PARAM_STRING, &pubruri_callee_avp },
{"pubruri_caller_dlg_var", PARAM_STR, &caller_dlg_var },
{"pubruri_callee_dlg_var", PARAM_STR, &callee_dlg_var },
{"callee_trying", INT_PARAM, &callee_trying },
{0, 0, 0 }
};
struct module_exports exports= {
"pua_dialoginfo", /* module name */
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* exported functions */
params, /* exported parameters */
0, /* exported statistics */
0, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* extra processes */
mod_init, /* module initialization function */
0, /* response handling function */
0, /* destroy function */
NULL /* per-child init function */
};
#ifdef PUA_DIALOGINFO_DEBUG
static void
__dialog_cbtest(struct dlg_cell *dlg, int type, struct dlg_cb_params *_params)
{
str tag;
struct sip_msg *msg;
LM_ERR("dialog callback received, from=%.*s, to=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s, dlg->to_uri.len,
dlg->to_uri.s);
if (dlg->tag[0].len && dlg->tag[0].s ) {
LM_ERR("dialog callback: tag[0] = %.*s",
dlg->tag[0].len, dlg->tag[0].s);
}
if (dlg->tag[0].len && dlg->tag[1].s ) {
LM_ERR("dialog callback: tag[1] = %.*s",
dlg->tag[1].len, dlg->tag[1].s);
}
if (type != DLGCB_DESTROY) {
msg = dlg_get_valid_msg(_params);
if (!msg) {
LM_ERR("no SIP message available in callback parameters\n");
return;
}
/* get to tag*/
if ( !msg->to) {
// to header not defined, parse to header
LM_ERR("to header not defined, parse to header\n");
if (parse_headers(msg, HDR_TO_F,0)<0) {
//parser error
LM_ERR("parsing of to-header failed\n");
tag.s = 0;
tag.len = 0;
} else if (!msg->to) {
// to header still not defined
LM_ERR("bad reply or missing TO header\n");
tag.s = 0;
tag.len = 0;
} else
tag = get_to(msg)->tag_value;
} else {
tag = get_to(msg)->tag_value;
if (tag.s==0 || tag.len==0) {
LM_ERR("missing TAG param in TO hdr :-/\n");
tag.s = 0;
tag.len = 0;
}
}
if (tag.s) {
LM_ERR("dialog callback: msg->to->parsed->tag_value = %.*s",
tag.len, tag.s);
}
}
switch (type) {
case DLGCB_FAILED:
LM_ERR("dialog callback type 'DLGCB_FAILED' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
case DLGCB_CONFIRMED_NA:
LM_ERR("dialog callback type 'DLGCB_CONFIRMED_NA' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
case DLGCB_CONFIRMED:
LM_ERR("dialog callback type 'DLGCB_CONFIRMED' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
case DLGCB_REQ_WITHIN:
LM_ERR("dialog callback type 'DLGCB_REQ_WITHIN' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
case DLGCB_TERMINATED:
LM_ERR("dialog callback type 'DLGCB_TERMINATED' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
case DLGCB_EXPIRED:
LM_ERR("dialog callback type 'DLGCB_EXPIRED' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
case DLGCB_EARLY:
LM_ERR("dialog callback type 'DLGCB_EARLY' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
case DLGCB_RESPONSE_FWDED:
LM_ERR("dialog callback type 'DLGCB_RESPONSE_FWDED' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
case DLGCB_RESPONSE_WITHIN:
LM_ERR("dialog callback type 'DLGCB_RESPONSE_WITHIN' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
case DLGCB_MI_CONTEXT:
LM_ERR("dialog callback type 'DLGCB_MI_CONTEXT' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
case DLGCB_DESTROY:
LM_ERR("dialog callback type 'DLGCB_DESTROY' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
break;
default:
LM_ERR("dialog callback type 'unknown' received, from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
}
}
#endif
static void
__dialog_sendpublish(struct dlg_cell *dlg, int type, struct dlg_cb_params *_params)
{
str tag = {0,0};
str uri = {0,0};
str target = {0,0};
struct dlginfo_cell *dlginfo = NULL;
dlginfo = (struct dlginfo_cell*)*_params->param;
if(dlg==NULL || dlginfo==NULL) {
LM_WARN("execution with null parameters - type %d, dlg %p, info %p\n",
type, dlg, dlginfo);
return;
}
if(include_req_uri) {
uri = dlginfo->req_uri;
} else {
uri = dlginfo->to_uri;
}
switch (type) {
case DLGCB_FAILED:
case DLGCB_TERMINATED:
case DLGCB_EXPIRED:
LM_DBG("dialog over, from=%.*s\n", dlginfo->from_uri.len,
dlginfo->from_uri.s);
dialog_publish_multi("terminated", dlginfo->pubruris_caller,
&(dlginfo->from_uri), &uri, &(dlginfo->callid), 1,
10, 0, 0, &(dlginfo->from_contact),
&target, send_publish_flag==-1?1:0);
dialog_publish_multi("terminated", dlginfo->pubruris_callee,
&uri, &(dlginfo->from_uri), &(dlginfo->callid), 0,
10, 0, 0, &target, &(dlginfo->from_contact),
send_publish_flag==-1?1:0);
break;
case DLGCB_CONFIRMED:
case DLGCB_REQ_WITHIN:
case DLGCB_CONFIRMED_NA:
LM_DBG("dialog confirmed, from=%.*s\n", dlginfo->from_uri.len,
dlginfo->from_uri.s);
dialog_publish_multi("confirmed", dlginfo->pubruris_caller,
&(dlginfo->from_uri), &uri, &(dlginfo->callid), 1,
dlginfo->lifetime, 0, 0, &(dlginfo->from_contact), &target,
send_publish_flag==-1?1:0);
dialog_publish_multi("confirmed", dlginfo->pubruris_callee, &uri,
&(dlginfo->from_uri), &(dlginfo->callid), 0,
dlginfo->lifetime, 0, 0, &target, &(dlginfo->from_contact),
send_publish_flag==-1?1:0);
break;
case DLGCB_EARLY:
LM_DBG("dialog is early, from=%.*s\n", dlginfo->from_uri.len,
dlginfo->from_uri.s);
if (include_tags) {
/* get remotetarget */
if ( !_params->rpl->contact && ((parse_headers(_params->rpl,
HDR_CONTACT_F,0)<0) || !_params->rpl->contact) ) {
LM_ERR("bad reply or missing CONTACT hdr\n");
} else {
if ( parse_contact(_params->rpl->contact)<0 ||
((contact_body_t *)_params->rpl->contact->parsed)->contacts==NULL ||
((contact_body_t *)_params->rpl->contact->parsed)->contacts->next!=NULL ) {
LM_ERR("Malformed CONTACT hdr\n");
} else {
target = ((contact_body_t *)_params->rpl->contact->parsed)->contacts->uri;
}
}
/* get to tag*/
if ( !_params->rpl->to && ((parse_headers(_params->rpl, HDR_TO_F,0)<0)
|| !_params->rpl->to) ) {
LM_ERR("bad reply or missing TO hdr :-/\n");
tag.s = 0;
tag.len = 0;
} else {
tag = get_to(_params->rpl)->tag_value;
if (tag.s==0 || tag.len==0) {
LM_ERR("missing TAG param in TO hdr :-/\n");
tag.s = 0;
tag.len = 0;
}
}
if (caller_confirmed) {
dialog_publish_multi("confirmed", dlginfo->pubruris_caller,
&(dlginfo->from_uri), &uri, &(dlginfo->callid), 1,
dlginfo->lifetime, &(dlginfo->from_tag), &tag,
&(dlginfo->from_contact), &target,
send_publish_flag==-1?1:0);
} else {
dialog_publish_multi("early", dlginfo->pubruris_caller,
&(dlginfo->from_uri), &uri, &(dlginfo->callid), 1,
dlginfo->lifetime, &(dlginfo->from_tag), &tag,
&(dlginfo->from_contact), &target,
send_publish_flag==-1?1:0);
}
dialog_publish_multi("early", dlginfo->pubruris_callee, &uri,
&(dlginfo->from_uri), &(dlginfo->callid), 0,
dlginfo->lifetime, &tag, &(dlginfo->from_tag), &target,
&(dlginfo->from_contact), send_publish_flag==-1?1:0);
} else {
if (caller_confirmed) {
dialog_publish_multi("confirmed", dlginfo->pubruris_caller,
&(dlginfo->from_uri), &uri, &(dlginfo->callid), 1,
dlginfo->lifetime, 0, 0, &(dlginfo->from_contact),
&target, send_publish_flag==-1?1:0);
} else {
dialog_publish_multi("early", dlginfo->pubruris_caller,
&(dlginfo->from_uri), &uri, &(dlginfo->callid), 1,
dlginfo->lifetime, 0, 0, &(dlginfo->from_contact),
&target, send_publish_flag==-1?1:0);
}
dialog_publish_multi("early", dlginfo->pubruris_callee, &uri,
&(dlginfo->from_uri), &(dlginfo->callid), 0,
dlginfo->lifetime, 0, 0, &target,
&(dlginfo->from_contact), send_publish_flag==-1?1:0);
}
break;
default:
LM_ERR("unhandled dialog callback type %d received, from=%.*s\n",
type, dlginfo->from_uri.len, dlginfo->from_uri.s);
dialog_publish_multi("terminated", dlginfo->pubruris_caller,
&(dlginfo->from_uri), &uri, &(dlginfo->callid), 1,
10, 0, 0, &(dlginfo->from_contact), &target,
send_publish_flag==-1?1:0);
dialog_publish_multi("terminated", dlginfo->pubruris_callee, &uri,
&(dlginfo->from_uri), &(dlginfo->callid), 0,
10, 0, 0, &target, &(dlginfo->from_contact),
send_publish_flag==-1?1:0);
}
}
/*
* Writes all avps with name avp_name to new str_list (shm mem)
* Be careful: returns NULL pointer if no avp present!
*
*/
struct str_list* get_str_list(unsigned short avp_flags, int_str avp_name) {
int_str avp_value;
unsigned int len;
struct str_list* list_first = 0;
struct str_list* list_current = 0 ;
struct search_state st;
if(!search_first_avp(avp_flags, avp_name, &avp_value, &st)) {
return NULL;
}
do {
LM_DBG("AVP found '%.*s'\n", avp_value.s.len, avp_value.s.s);
len = sizeof(struct str_list) + avp_value.s.len;
if(list_current) {
list_current->next = (struct str_list*) shm_malloc( len);
list_current=list_current->next;
} else {
list_current=list_first= (struct str_list*) shm_malloc( len);
}
if (list_current==0) {
LM_ERR("no more shm mem (%d)\n",len);
return 0;
}
memset( list_current, 0, len);
list_current->s.s = (char*)list_current + sizeof(struct str_list);
list_current->s.len = avp_value.s.len;
memcpy(list_current->s.s,avp_value.s.s,avp_value.s.len);
} while(search_next_avp(&st, &avp_value));
return list_first;
}
struct dlginfo_cell* get_dialog_data(struct dlg_cell *dlg, int type)
{
struct dlginfo_cell *dlginfo;
int len;
str* s=NULL;
/* create dlginfo structure to store important data inside the module*/
len = sizeof(struct dlginfo_cell)
+ dlg->from_uri.len
+ dlg->to_uri.len
+ dlg->callid.len
+ dlg->tag[0].len
+ dlg->req_uri.len
+ dlg->contact[0].len;
dlginfo = (struct dlginfo_cell*)shm_malloc( len );
if (dlginfo==0) {
LM_ERR("no more shm mem (%d)\n",len);
return NULL;
}
memset( dlginfo, 0, len);
/* copy from dlg structure to dlginfo structure */
dlginfo->lifetime = override_lifetime ? override_lifetime : dlg->lifetime;
dlginfo->from_uri.s = (char*)dlginfo + sizeof(struct dlginfo_cell);
dlginfo->from_uri.len = dlg->from_uri.len;
dlginfo->to_uri.s = dlginfo->from_uri.s + dlg->from_uri.len;
dlginfo->to_uri.len = dlg->to_uri.len;
dlginfo->callid.s = dlginfo->to_uri.s + dlg->to_uri.len;
dlginfo->callid.len = dlg->callid.len;
dlginfo->from_tag.s = dlginfo->callid.s + dlg->callid.len;
dlginfo->from_tag.len = dlg->tag[0].len;
dlginfo->req_uri.s = dlginfo->from_tag.s + dlginfo->from_tag.len;
dlginfo->req_uri.len = dlg->req_uri.len;
dlginfo->from_contact.s = dlginfo->req_uri.s + dlginfo->req_uri.len;
dlginfo->from_contact.len = dlg->contact[0].len;
memcpy(dlginfo->from_uri.s, dlg->from_uri.s, dlg->from_uri.len);
memcpy(dlginfo->to_uri.s, dlg->to_uri.s, dlg->to_uri.len);
memcpy(dlginfo->callid.s, dlg->callid.s, dlg->callid.len);
memcpy(dlginfo->from_tag.s, dlg->tag[0].s, dlg->tag[0].len);
memcpy(dlginfo->req_uri.s, dlg->req_uri.s, dlg->req_uri.len);
memcpy(dlginfo->from_contact.s, dlg->contact[0].s, dlg->contact[0].len);
if (use_pubruri_avps) {
if(type==DLGCB_CREATED) {
dlginfo->pubruris_caller = get_str_list(pubruri_caller_avp_type,
pubruri_caller_avp_name);
dlginfo->pubruris_callee = get_str_list(pubruri_callee_avp_type,
pubruri_callee_avp_name);
if(dlginfo->pubruris_callee!=NULL && callee_dlg_var.len>0)
dlg_api.set_dlg_var(dlg, &callee_dlg_var,
&dlginfo->pubruris_callee->s);
if(dlginfo->pubruris_caller!=NULL && caller_dlg_var.len>0)
dlg_api.set_dlg_var(dlg, &caller_dlg_var,
&dlginfo->pubruris_caller->s);
} else {
if(caller_dlg_var.len>0
&& (s = dlg_api.get_dlg_var(dlg, &caller_dlg_var))!=0) {
dlginfo->pubruris_caller =
(struct str_list*)shm_malloc( sizeof(struct str_list) );
if (dlginfo->pubruris_caller==0) {
LM_ERR("no more shm mem (%d)\n", (int) sizeof(struct str_list));
free_dlginfo_cell(dlginfo);
return NULL;
}
memset( dlginfo->pubruris_caller, 0, sizeof(struct str_list));
dlginfo->pubruris_caller->s=*s;
LM_DBG("Found pubruris_caller in dialog '%.*s'\n",
dlginfo->pubruris_caller->s.len, dlginfo->pubruris_caller->s.s);
}
if(callee_dlg_var.len>0
&& (s = dlg_api.get_dlg_var(dlg, &callee_dlg_var))!=0) {
dlginfo->pubruris_callee =
(struct str_list*)shm_malloc( sizeof(struct str_list) );
if (dlginfo->pubruris_callee==0) {
LM_ERR("no more shm mem (%d)\n", (int) sizeof(struct str_list));
free_dlginfo_cell(dlginfo);
return NULL;
}
memset( dlginfo->pubruris_callee, 0, sizeof(struct str_list));
dlginfo->pubruris_callee->s=*s;
LM_DBG("Found pubruris_callee in dialog '%.*s'\n",
dlginfo->pubruris_callee->s.len, dlginfo->pubruris_callee->s.s);
}
}
if(dlginfo->pubruris_caller == 0 && dlginfo->pubruris_callee == 0 ) {
/* No reason to save dlginfo, we have nobody to publish to */
LM_DBG("Neither pubruris_caller nor pubruris_callee found.\n");
free_dlginfo_cell(dlginfo);
return NULL;
}
} else {
dlginfo->pubruris_caller =
(struct str_list*)shm_malloc( sizeof(struct str_list) );
if (dlginfo->pubruris_caller==0) {
LM_ERR("no more shm mem (%d)\n", (int) sizeof(struct str_list));
free_dlginfo_cell(dlginfo);
return NULL;
}
memset( dlginfo->pubruris_caller, 0, sizeof(struct str_list));
dlginfo->pubruris_caller->s=dlginfo->from_uri;
dlginfo->pubruris_callee =
(struct str_list*)shm_malloc( sizeof(struct str_list) );
if (dlginfo->pubruris_callee==0) {
LM_ERR("no more shm mem (%d)\n", (int) sizeof(struct str_list));
free_dlginfo_cell(dlginfo);
return NULL;
}
memset( dlginfo->pubruris_callee, 0, sizeof(struct str_list));
if(include_req_uri) {
dlginfo->pubruris_callee->s = dlginfo->req_uri;
} else {
dlginfo->pubruris_callee->s = dlginfo->to_uri;
}
}
/* register dialog callbacks which triggers sending PUBLISH */
if (dlg_api.register_dlgcb(dlg,
DLGCB_FAILED| DLGCB_CONFIRMED_NA | DLGCB_TERMINATED
| DLGCB_EXPIRED | DLGCB_REQ_WITHIN | DLGCB_EARLY,
__dialog_sendpublish, dlginfo, free_dlginfo_cell) != 0) {
LM_ERR("cannot register callback for interesting dialog types\n");
free_dlginfo_cell(dlginfo);
return NULL;
}
#ifdef PUA_DIALOGINFO_DEBUG
/* dialog callback testing (registered last to be executed frist) */
if (dlg_api.register_dlgcb(dlg,
DLGCB_FAILED| DLGCB_CONFIRMED_NA | DLGCB_CONFIRMED
| DLGCB_REQ_WITHIN | DLGCB_TERMINATED | DLGCB_EXPIRED
| DLGCB_EARLY | DLGCB_RESPONSE_FWDED | DLGCB_RESPONSE_WITHIN
| DLGCB_MI_CONTEXT | DLGCB_DESTROY,
__dialog_cbtest, NULL, NULL) != 0) {
LM_ERR("cannot register callback for all dialog types\n");
free_dlginfo_cell(dlginfo);
return NULL;
}
#endif
return(dlginfo);
}
static void
__dialog_created(struct dlg_cell *dlg, int type, struct dlg_cb_params *_params)
{
struct sip_msg *request = _params->req;
struct dlginfo_cell *dlginfo;
if (request->REQ_METHOD != METHOD_INVITE)
return;
if(send_publish_flag > -1 && !(request->flags & (1<<send_publish_flag)))
return;
LM_DBG("new INVITE dialog created: from=%.*s\n",
dlg->from_uri.len, dlg->from_uri.s);
dlginfo=get_dialog_data(dlg, type);
if(dlginfo==NULL)
return;
dialog_publish_multi("Trying", dlginfo->pubruris_caller,
&(dlg->from_uri),
(include_req_uri)?&(dlg->req_uri):&(dlg->to_uri),
&(dlg->callid), 1, dlginfo->lifetime,
0, 0, 0, 0, (send_publish_flag==-1)?1:0);
if (callee_trying)
{
dialog_publish_multi("Trying", dlginfo->pubruris_callee,
(include_req_uri)?&(dlg->req_uri):&(dlg->to_uri),
&(dlg->from_uri),
&(dlg->callid), 0, dlginfo->lifetime,
0, 0, 0, 0, (send_publish_flag==-1)?1:0);
}
}
static void
__dialog_loaded(struct dlg_cell *dlg, int type, struct dlg_cb_params *_params)
{
struct dlginfo_cell *dlginfo;
LM_DBG("INVITE dialog loaded: from=%.*s\n", dlg->from_uri.len, dlg->from_uri.s);
dlginfo=get_dialog_data(dlg, type);
if(dlginfo!=NULL) {
LM_DBG("dialog info initialized (from=%.*s)\n",
dlg->from_uri.len, dlg->from_uri.s);
/* free_dlginfo_cell(dlginfo); */
}
}
/**
* init module function
*/
static int mod_init(void)
{
bind_pua_t bind_pua;
str s;
pv_spec_t avp_spec;
if(caller_dlg_var.len<=0)
LM_WARN("pubruri_caller_dlg_var is not set - restore on restart disabled\n");
if(callee_dlg_var.len<=0)
LM_WARN("pubruri_callee_dlg_var is not set - restore on restart disabled\n");
bind_pua= (bind_pua_t)find_export("bind_pua", 1,0);
if (!bind_pua)
{
LM_ERR("Can't bind pua\n");
return -1;
}
if (bind_pua(&pua) < 0)
{
LM_ERR("Can't bind pua\n");
return -1;
}
if(pua.send_publish == NULL)
{
LM_ERR("Could not import send_publish\n");
return -1;
}
pua_send_publish= pua.send_publish;
/* bind to the dialog API */
if (load_dlg_api(&dlg_api)!=0) {
LM_ERR("failed to find dialog API - is dialog module loaded?\n");
return -1;
}
/* register dialog creation callback */
if (dlg_api.register_dlgcb(NULL, DLGCB_CREATED, __dialog_created, NULL, NULL) != 0) {
LM_ERR("cannot register callback for dialog creation\n");
return -1;
}
/* register dialog loaded callback */
if (dlg_api.register_dlgcb(NULL, DLGCB_LOADED, __dialog_loaded, NULL, NULL) != 0) {
LM_ERR("cannot register callback for dialog loaded\n");
return -1;
}
if(use_pubruri_avps) {
if(!(pubruri_caller_avp && *pubruri_caller_avp)
&& (pubruri_callee_avp && *pubruri_callee_avp)) {
LM_ERR("pubruri_caller_avp and pubruri_callee_avp must be set,"
" if use_pubruri_avps is enabled\n");
return -1;
}
s.s = pubruri_caller_avp; s.len = strlen(s.s);
if (pv_parse_spec(&s, &avp_spec)==0 || avp_spec.type!=PVT_AVP) {
LM_ERR("malformed or non AVP %s AVP definition\n", pubruri_caller_avp);
return -1;
}
if(pv_get_avp_name(0, &avp_spec.pvp, &pubruri_caller_avp_name,
&pubruri_caller_avp_type)!=0) {
LM_ERR("[%s]- invalid AVP definition\n", pubruri_caller_avp);
return -1;
}
s.s = pubruri_callee_avp; s.len = strlen(s.s);
if (pv_parse_spec(&s, &avp_spec)==0 || avp_spec.type!=PVT_AVP) {
LM_ERR("malformed or non AVP %s AVP definition\n", pubruri_callee_avp);
return -1;
}
if(pv_get_avp_name(0, &avp_spec.pvp, &pubruri_callee_avp_name,
&pubruri_callee_avp_type)!=0) {
LM_ERR("[%s]- invalid AVP definition\n", pubruri_callee_avp);
return -1;
}
}
return 0;
}
void free_dlginfo_cell(void *param) {
struct dlginfo_cell *cell = NULL;
if(param==NULL)
return;
cell = param;
free_str_list_all(cell->pubruris_caller);
free_str_list_all(cell->pubruris_callee);
/*if (cell->to_tag) {
shm_free(cell->to_tag);
}*/
shm_free(param);
}
void free_str_list_all(struct str_list * del_current) {
struct str_list* del_next;
while(del_current) {
del_next = del_current->next;
shm_free(del_current);
del_current=del_next;
}
}