/
res_pjsip_refer.c
1865 lines (1582 loc) · 62.2 KB
/
res_pjsip_refer.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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2013, Digium, Inc.
*
* Joshua Colp <jcolp@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*** MODULEINFO
<depend>pjproject</depend>
<depend>res_pjsip</depend>
<depend>res_pjsip_session</depend>
<depend>res_pjsip_pubsub</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <pjsip.h>
#include <pjsip_ua.h>
#include "asterisk/res_pjsip.h"
#include "asterisk/res_pjsip_session.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/bridge.h"
#include "asterisk/framehook.h"
#include "asterisk/stasis_bridges.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/causes.h"
#include "asterisk/refer.h"
static struct ast_taskprocessor *refer_serializer;
static pj_status_t refer_on_tx_request(pjsip_tx_data *tdata);
/*! \brief REFER Progress structure */
struct refer_progress {
/*! \brief Subscription to provide updates on */
pjsip_evsub *sub;
/*! \brief Dialog for subscription */
pjsip_dialog *dlg;
/*! \brief Received packet, used to construct final response in case no subscription exists */
pjsip_rx_data *rdata;
/*! \brief Frame hook for monitoring REFER progress */
int framehook;
/*! \brief Last received subclass in frame hook */
int subclass;
/*! \brief Serializer for notifications */
struct ast_taskprocessor *serializer;
/*! \brief Stasis subscription for bridge events */
struct stasis_subscription *bridge_sub;
/*! \brief Reference to transfer_channel_data related to the refer */
struct transfer_channel_data *transfer_data;
/*! \brief Uniqueid of transferee channel */
char *transferee;
/*! \brief Non-zero if the 100 notify has been sent */
int sent_100;
/*! \brief Whether to notifies all the progress details on blind transfer */
unsigned int refer_blind_progress;
};
/*! \brief REFER Progress notification structure */
struct refer_progress_notification {
/*! \brief Refer progress structure to send notification on */
struct refer_progress *progress;
/*! \brief SIP response code to send */
int response;
/*! \brief Subscription state */
pjsip_evsub_state state;
};
/*! \brief REFER Progress module, used to attach REFER progress structure to subscriptions */
static pjsip_module refer_progress_module = {
.name = { "REFER Progress", 14 },
.id = -1,
};
/*! \brief Destructor for REFER Progress notification structure */
static void refer_progress_notification_destroy(void *obj)
{
struct refer_progress_notification *notification = obj;
ao2_cleanup(notification->progress);
}
/*! \brief Allocator for REFER Progress notification structure */
static struct refer_progress_notification *refer_progress_notification_alloc(struct refer_progress *progress, int response,
pjsip_evsub_state state)
{
struct refer_progress_notification *notification = ao2_alloc(sizeof(*notification), refer_progress_notification_destroy);
if (!notification) {
return NULL;
}
ao2_ref(progress, +1);
notification->progress = progress;
notification->response = response;
notification->state = state;
return notification;
}
/*! \brief Serialized callback for subscription notification
*
* Locking and serialization:
*
* Although refer_progress_notify() always runs in the progress serializer,
* the pjproject evsub module itself can cause the subscription to be
* destroyed which then triggers refer_progress_on_evsub_state() to clean
* it up. In this case, it's possible that refer_progress_notify() could
* get the subscription pulled out from under it while it's trying to use it.
*
* At one point we tried to have refer_progress_on_evsub_state() push the
* cleanup to the serializer and wait for its return before returning to
* pjproject but since pjproject calls its state callbacks with the dialog
* locked, this required us to unlock the dialog while waiting for the
* serialized cleanup, then lock it again before returning to pjproject.
* There were also still some cases where other callers of
* refer_progress_notify() weren't using the serializer and crashes were
* resulting.
*
* Although all callers of refer_progress_notify() now use the progress
* serializer, we decided to simplify the locking so we didn't have to
* unlock and relock the dialog in refer_progress_on_evsub_state().
*
* Now, refer_progress_notify() holds the dialog lock for all its work
* rather than just when calling pjsip_evsub_set_mod_data() to clear the
* module data. Since pjproject also holds the dialog lock while calling
* refer_progress_on_evsub_state(), there should be no more chances for
* the subscription to be cleaned up while still being used to send NOTIFYs.
*/
static int refer_progress_notify(void *data)
{
RAII_VAR(struct refer_progress_notification *, notification, data, ao2_cleanup);
pjsip_evsub *sub;
pjsip_tx_data *tdata;
pjsip_dlg_inc_lock(notification->progress->dlg);
/* If the subscription has already been terminated we can't send a notification */
if (!(sub = notification->progress->sub)) {
ast_debug(3, "Not sending NOTIFY of response '%d' and state '%u' on progress monitor '%p' as subscription has been terminated\n",
notification->response, notification->state, notification->progress);
pjsip_dlg_dec_lock(notification->progress->dlg);
return 0;
}
/* Send a deferred initial 100 Trying SIP frag NOTIFY if we haven't already. */
if (!notification->progress->sent_100) {
notification->progress->sent_100 = 1;
if (notification->response != 100) {
ast_debug(3, "Sending initial 100 Trying NOTIFY for progress monitor '%p'\n",
notification->progress);
if (pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_ACTIVE, 100, NULL, &tdata) == PJ_SUCCESS) {
pjsip_xfer_send_request(sub, tdata);
}
}
}
ast_debug(3, "Sending NOTIFY with response '%d' and state '%u' on subscription '%p' and progress monitor '%p'\n",
notification->response, notification->state, sub, notification->progress);
/* Actually send the notification */
if (pjsip_xfer_notify(sub, notification->state, notification->response, NULL, &tdata) == PJ_SUCCESS) {
pjsip_xfer_send_request(sub, tdata);
}
pjsip_dlg_dec_lock(notification->progress->dlg);
return 0;
}
static void refer_progress_bridge(void *data, struct stasis_subscription *sub,
struct stasis_message *message)
{
struct refer_progress *progress = data;
struct ast_bridge_blob *enter_blob;
struct refer_progress_notification *notification;
struct ast_channel *chan;
if (stasis_subscription_final_message(sub, message)) {
ao2_ref(progress, -1);
return;
}
if (ast_channel_entered_bridge_type() != stasis_message_type(message)) {
/* Don't care */
return;
}
enter_blob = stasis_message_data(message);
if (strcmp(enter_blob->channel->base->uniqueid, progress->transferee)) {
/* Don't care */
return;
}
if (!progress->transfer_data->completed) {
/* We can't act on this message because the transfer_channel_data doesn't show that
* the transfer is ready to progress */
return;
}
/* OMG the transferee is joining a bridge. His call got answered! */
notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
if (notification) {
if (ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
ao2_cleanup(notification);
}
progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
}
chan = ast_channel_get_by_name(progress->transferee);
if (!chan) {
/* The channel is already gone */
return;
}
ast_channel_lock(chan);
ast_debug(3, "Detaching REFER progress monitoring hook from '%s' as it has joined a bridge\n",
ast_channel_name(chan));
ast_framehook_detach(chan, progress->framehook);
ast_channel_unlock(chan);
ast_channel_unref(chan);
}
/*! \brief Progress monitoring frame hook - examines frames to determine state of transfer */
static struct ast_frame *refer_progress_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
{
struct refer_progress *progress = data;
struct refer_progress_notification *notification = NULL;
/* We only care about frames *to* the channel */
if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
return f;
}
/* If the completed flag hasn't been raised, skip this pass. */
if (!progress->transfer_data->completed) {
return f;
}
/* Determine the state of the REFER based on the control frames (or voice frames) passing */
if (f->frametype == AST_FRAME_VOICE && !progress->subclass) {
/* Media is passing without progress, this means the call has been answered */
progress->subclass = AST_CONTROL_ANSWER;
notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
} else if (f->frametype == AST_FRAME_CONTROL) {
/* Based on the control frame being written we can send a NOTIFY advising of the progress */
if ((f->subclass.integer == AST_CONTROL_RING) || (f->subclass.integer == AST_CONTROL_RINGING)) {
/* Don't set progress->subclass; an ANSWER can still follow */
notification = refer_progress_notification_alloc(progress, 180, PJSIP_EVSUB_STATE_ACTIVE);
} else if (f->subclass.integer == AST_CONTROL_BUSY) {
progress->subclass = f->subclass.integer;
notification = refer_progress_notification_alloc(progress, 486, PJSIP_EVSUB_STATE_TERMINATED);
} else if (f->subclass.integer == AST_CONTROL_CONGESTION) {
progress->subclass = f->subclass.integer;
notification = refer_progress_notification_alloc(progress, 503, PJSIP_EVSUB_STATE_TERMINATED);
} else if (f->subclass.integer == AST_CONTROL_PROGRESS) {
/* Don't set progress->subclass; an ANSWER can still follow */
notification = refer_progress_notification_alloc(progress, 183, PJSIP_EVSUB_STATE_ACTIVE);
} else if (f->subclass.integer == AST_CONTROL_PROCEEDING) {
/* Don't set progress->subclass; an ANSWER can still follow */
notification = refer_progress_notification_alloc(progress, 100, PJSIP_EVSUB_STATE_ACTIVE);
} else if (f->subclass.integer == AST_CONTROL_ANSWER) {
progress->subclass = f->subclass.integer;
notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
}
}
/* If a notification is due to be sent push it to the thread pool */
if (notification) {
/* If the subscription is being terminated we don't need the frame hook any longer */
if (notification->state == PJSIP_EVSUB_STATE_TERMINATED) {
ast_debug(3, "Detaching REFER progress monitoring hook from '%s' as subscription is being terminated\n",
ast_channel_name(chan));
ast_framehook_detach(chan, progress->framehook);
}
if (ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
ao2_cleanup(notification);
}
}
return f;
}
/*! \brief Destroy callback for monitoring framehook */
static void refer_progress_framehook_destroy(void *data)
{
struct refer_progress *progress = data;
struct refer_progress_notification *notification = refer_progress_notification_alloc(progress, 503, PJSIP_EVSUB_STATE_TERMINATED);
if (notification && ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
ao2_cleanup(notification);
}
if (progress->bridge_sub) {
progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
}
ao2_cleanup(progress);
}
/*!
* \brief Callback for REFER subscription state changes
* \see refer_progress_notify
*
* The documentation attached to refer_progress_notify has more
* information about the locking issues with cleaning up
* the subscription.
*
* \note pjproject holds the dialog lock while calling this function.
*/
static void refer_progress_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)
{
struct refer_progress *progress = pjsip_evsub_get_mod_data(sub, refer_progress_module.id);
/*
* If being destroyed, remove the progress object from the subscription
* and release the reference it had.
*/
if (progress && (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED)) {
pjsip_evsub_set_mod_data(progress->sub, refer_progress_module.id, NULL);
progress->sub = NULL;
ao2_cleanup(progress);
}
}
/*! \brief Callback structure for subscription */
static pjsip_evsub_user refer_progress_evsub_cb = {
.on_evsub_state = refer_progress_on_evsub_state,
};
static int dlg_releaser_task(void *data) {
pjsip_dlg_dec_session((pjsip_dialog *)data, &refer_progress_module);
return 0;
}
/*! \brief Destructor for REFER progress sutrcture */
static void refer_progress_destroy(void *obj)
{
struct refer_progress *progress = obj;
if (progress->bridge_sub) {
progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
}
if (progress->dlg) {
/*
* Although the dlg session count was incremented in a pjsip servant
* thread, there's no guarantee that the last thread to unref this progress
* object was one. Before we decrement, we need to make sure that this
* is either a servant thread or that we push the decrement to a
* serializer that is one.
*
* Because pjsip_dlg_dec_session requires the dialog lock, we don't want
* to wait on the task to complete if we had to push it to a serializer.
*/
if (ast_sip_thread_is_servant()) {
pjsip_dlg_dec_session(progress->dlg, &refer_progress_module);
} else {
ast_sip_push_task(NULL, dlg_releaser_task, progress->dlg);
}
}
ao2_cleanup(progress->transfer_data);
ast_free(progress->transferee);
ast_taskprocessor_unreference(progress->serializer);
}
/*! \brief Internal helper function which sets up a refer progress structure if needed */
static int refer_progress_alloc(struct ast_sip_session *session, pjsip_rx_data *rdata, struct refer_progress **progress)
{
const pj_str_t str_refer_sub = { "Refer-Sub", 9 };
pjsip_generic_string_hdr *refer_sub = NULL;
const pj_str_t str_true = { "true", 4 };
pjsip_hdr hdr_list;
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
*progress = NULL;
/* Grab the optional Refer-Sub header, it can be used to suppress the implicit subscription */
refer_sub = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_sub, NULL);
if ((refer_sub && pj_strnicmp(&refer_sub->hvalue, &str_true, 4))) {
return 0;
}
if (!(*progress = ao2_alloc(sizeof(struct refer_progress), refer_progress_destroy))) {
return -1;
}
ast_debug(3, "Created progress monitor '%p' for transfer occurring from channel '%s' and endpoint '%s'\n",
progress, ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
(*progress)->refer_blind_progress = session->endpoint->refer_blind_progress;
(*progress)->framehook = -1;
/* Create name with seq number appended. */
ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/refer/%s",
ast_sorcery_object_get_id(session->endpoint));
if (!((*progress)->serializer = ast_sip_create_serializer(tps_name))) {
goto error;
}
/* Create the implicit subscription for monitoring of this transfer */
if (pjsip_xfer_create_uas(session->inv_session->dlg, &refer_progress_evsub_cb, rdata, &(*progress)->sub) != PJ_SUCCESS) {
goto error;
}
/* To prevent a potential deadlock we need the dialog so we can lock/unlock */
(*progress)->dlg = session->inv_session->dlg;
/* We also need to make sure it stays around until we're done with it */
pjsip_dlg_inc_session((*progress)->dlg, &refer_progress_module);
/* Associate the REFER progress structure with the subscription */
ao2_ref(*progress, +1);
pjsip_evsub_set_mod_data((*progress)->sub, refer_progress_module.id, *progress);
pj_list_init(&hdr_list);
if (refer_sub) {
pjsip_hdr *hdr = (pjsip_hdr*)pjsip_generic_string_hdr_create(session->inv_session->dlg->pool, &str_refer_sub, &str_true);
pj_list_push_back(&hdr_list, hdr);
}
/* Accept the REFER request */
ast_debug(3, "Accepting REFER request for progress monitor '%p'\n", *progress);
pjsip_xfer_accept((*progress)->sub, rdata, 202, &hdr_list);
return 0;
error:
ao2_cleanup(*progress);
*progress = NULL;
return -1;
}
/*! \brief Structure for attended transfer task */
struct refer_attended {
/*! \brief Transferer session */
struct ast_sip_session *transferer;
/*! \brief Transferer channel */
struct ast_channel *transferer_chan;
/*! \brief Second transferer session */
struct ast_sip_session *transferer_second;
/*! \brief Optional refer progress structure */
struct refer_progress *progress;
};
/*! \brief Destructor for attended transfer task */
static void refer_attended_destroy(void *obj)
{
struct refer_attended *attended = obj;
ao2_cleanup(attended->transferer);
ast_channel_cleanup(attended->transferer_chan);
ao2_cleanup(attended->transferer_second);
ao2_cleanup(attended->progress);
}
/*! \brief Allocator for attended transfer task */
static struct refer_attended *refer_attended_alloc(struct ast_sip_session *transferer,
struct ast_sip_session *transferer_second,
struct refer_progress *progress)
{
struct refer_attended *attended;
attended = ao2_alloc_options(sizeof(*attended), refer_attended_destroy,
AO2_ALLOC_OPT_LOCK_NOLOCK);
if (!attended) {
return NULL;
}
ao2_ref(transferer, +1);
attended->transferer = transferer;
ast_channel_ref(transferer->channel);
attended->transferer_chan = transferer->channel;
ao2_ref(transferer_second, +1);
attended->transferer_second = transferer_second;
if (progress) {
ao2_ref(progress, +1);
attended->progress = progress;
}
return attended;
}
static int session_end_if_deferred_task(void *data)
{
struct ast_sip_session *session = data;
ast_sip_session_end_if_deferred(session);
ao2_ref(session, -1);
return 0;
}
static int defer_termination_cancel_task(void *data)
{
struct ast_sip_session *session = data;
ast_sip_session_end_if_deferred(session);
ast_sip_session_defer_termination_cancel(session);
ao2_ref(session, -1);
return 0;
}
/*!
* \internal
* \brief Convert transfer enum to SIP response code.
* \since 13.3.0
*
* \param xfer_code Core transfer function enum result.
*
* \return SIP response code
*/
static int xfer_response_code2sip(enum ast_transfer_result xfer_code)
{
int response;
response = 503;
switch (xfer_code) {
case AST_BRIDGE_TRANSFER_INVALID:
response = 400;
break;
case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
response = 403;
break;
case AST_BRIDGE_TRANSFER_FAIL:
response = 500;
break;
case AST_BRIDGE_TRANSFER_SUCCESS:
response = 200;
break;
}
return response;
}
/*! \brief Task for attended transfer executed by attended->transferer_second serializer */
static int refer_attended_task(void *data)
{
struct refer_attended *attended = data;
int response;
int (*task_cb)(void *data);
if (attended->transferer_second->channel) {
ast_debug(3, "Performing a REFER attended transfer - Transferer #1: %s Transferer #2: %s\n",
ast_channel_name(attended->transferer_chan),
ast_channel_name(attended->transferer_second->channel));
response = xfer_response_code2sip(ast_bridge_transfer_attended(
attended->transferer_chan,
attended->transferer_second->channel));
ast_debug(3, "Final response for REFER attended transfer - Transferer #1: %s Transferer #2: %s is '%d'\n",
ast_channel_name(attended->transferer_chan),
ast_channel_name(attended->transferer_second->channel),
response);
} else {
ast_debug(3, "Received REFER request on channel '%s' but other channel has gone.\n",
ast_channel_name(attended->transferer_chan));
response = 603;
}
if (attended->progress) {
struct refer_progress_notification *notification;
notification = refer_progress_notification_alloc(attended->progress, response,
PJSIP_EVSUB_STATE_TERMINATED);
if (notification) {
if (ast_sip_push_task(attended->progress->serializer, refer_progress_notify, notification)) {
ao2_cleanup(notification);
}
}
}
if (response == 200) {
task_cb = session_end_if_deferred_task;
} else {
task_cb = defer_termination_cancel_task;
}
if (!ast_sip_push_task(attended->transferer->serializer,
task_cb, attended->transferer)) {
/* Gave the ref to the pushed task. */
attended->transferer = NULL;
} else {
/* Do this anyway even though it is the wrong serializer. */
ast_sip_session_end_if_deferred(attended->transferer);
}
ao2_ref(attended, -1);
return 0;
}
/*! \brief Structure for blind transfer callback details */
struct refer_blind {
/*! \brief Context being used for transfer */
const char *context;
/*! \brief Optional progress structure */
struct refer_progress *progress;
/*! \brief REFER message */
pjsip_rx_data *rdata;
/*! \brief Optional Replaces header */
pjsip_replaces_hdr *replaces;
/*! \brief Optional Refer-To header */
pjsip_sip_uri *refer_to;
/*! \brief Attended transfer flag */
unsigned int attended:1;
};
/*! \brief Blind transfer callback function */
static void refer_blind_callback(struct ast_channel *chan, struct transfer_channel_data *user_data_wrapper,
enum ast_transfer_type transfer_type)
{
struct refer_blind *refer = user_data_wrapper->data;
pjsip_generic_string_hdr *referred_by;
static const pj_str_t str_referred_by = { "Referred-By", 11 };
static const pj_str_t str_referred_by_s = { "b", 1 };
const char *get_xfrdata;
pbx_builtin_setvar_helper(chan, "SIPTRANSFER", "yes");
if (refer->progress && !refer->attended && !refer->progress->refer_blind_progress) {
/* If blind transfer and endpoint doesn't want to receive all the progress details */
struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
PJSIP_EVSUB_STATE_TERMINATED);
if (notification) {
if (ast_sip_push_task(refer->progress->serializer, refer_progress_notify, notification)) {
ao2_cleanup(notification);
}
}
} else if (refer->progress) {
/* If attended transfer and progress monitoring is being done attach a frame hook so we can monitor it */
struct ast_framehook_interface hook = {
.version = AST_FRAMEHOOK_INTERFACE_VERSION,
.event_cb = refer_progress_framehook,
.destroy_cb = refer_progress_framehook_destroy,
.data = refer->progress,
.disable_inheritance = 1,
};
refer->progress->transferee = ast_strdup(ast_channel_uniqueid(chan));
if (!refer->progress->transferee) {
struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
PJSIP_EVSUB_STATE_TERMINATED);
ast_log(LOG_WARNING, "Could not copy channel name '%s' during transfer - assuming success\n",
ast_channel_name(chan));
if (notification) {
if (ast_sip_push_task(refer->progress->serializer, refer_progress_notify, notification)) {
ao2_cleanup(notification);
}
}
}
/* Progress needs a reference to the transfer_channel_data so that it can track the completed status of the transfer */
ao2_ref(user_data_wrapper, +1);
refer->progress->transfer_data = user_data_wrapper;
/* We need to bump the reference count up on the progress structure since it is in the frame hook now */
ao2_ref(refer->progress, +1);
/* If we can't attach a frame hook for whatever reason send a notification of success immediately */
ast_channel_lock(chan);
refer->progress->framehook = ast_framehook_attach(chan, &hook);
ast_channel_unlock(chan);
if (refer->progress->framehook < 0) {
struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
PJSIP_EVSUB_STATE_TERMINATED);
ast_log(LOG_WARNING, "Could not attach REFER transfer progress monitoring hook to channel '%s' - assuming success\n",
ast_channel_name(chan));
if (notification) {
if (ast_sip_push_task(refer->progress->serializer, refer_progress_notify, notification)) {
ao2_cleanup(notification);
}
}
ao2_cleanup(refer->progress);
}
/* We need to bump the reference count for the stasis subscription */
ao2_ref(refer->progress, +1);
/* We also will need to detect if the transferee enters a bridge. This is currently the only reliable way to
* detect if the transfer target has answered the call
*/
refer->progress->bridge_sub = stasis_subscribe_pool(ast_bridge_topic_all(), refer_progress_bridge, refer->progress);
if (!refer->progress->bridge_sub) {
struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
PJSIP_EVSUB_STATE_TERMINATED);
ast_log(LOG_WARNING, "Could not create bridge stasis subscription for monitoring progress on transfer of channel '%s' - assuming success\n",
ast_channel_name(chan));
if (notification) {
if (ast_sip_push_task(refer->progress->serializer, refer_progress_notify, notification)) {
ao2_cleanup(notification);
}
}
ast_channel_lock(chan);
ast_framehook_detach(chan, refer->progress->framehook);
ast_channel_unlock(chan);
ao2_cleanup(refer->progress);
} else {
stasis_subscription_accept_message_type(refer->progress->bridge_sub, ast_channel_entered_bridge_type());
stasis_subscription_accept_message_type(refer->progress->bridge_sub, stasis_subscription_change_type());
stasis_subscription_set_filter(refer->progress->bridge_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
}
}
pbx_builtin_setvar_helper(chan, "SIPREFERRINGCONTEXT", S_OR(refer->context, NULL));
ast_channel_lock(chan);
if ((get_xfrdata = pbx_builtin_getvar_helper(chan, "GET_TRANSFERRER_DATA"))) {
get_xfrdata = ast_strdupa(get_xfrdata);
}
ast_channel_unlock(chan);
if (!ast_strlen_zero(get_xfrdata)) {
const pjsip_msg * msg = refer->rdata->msg_info.msg;
const struct pjsip_hdr *end = &msg->hdr;
struct pjsip_hdr *hdr = end->next;
struct ast_str *pbxvar = ast_str_create(64); /* initial buffer for variable name, extended on demand */
char buf[4096]; /* should be enough for one header value */
const char *prefix = get_xfrdata;
/* The '*' alone matches all headers. */
if (!strcmp(prefix, "*")) {
prefix = "";
}
if (pbxvar) {
for (; hdr != end; hdr = hdr->next) {
if (!pj_strnicmp2(&hdr->name, prefix, strlen(prefix))) {
const int hdr_name_strlen = pj_strlen(&hdr->name);
const int hdr_name_bytes = hdr_name_strlen + 1; /* +1 for string NULL terminator */
char hdr_name[hdr_name_bytes];
int len;
char *value_str;
ast_copy_pj_str(hdr_name, &hdr->name, hdr_name_bytes);
len = pjsip_hdr_print_on(hdr, buf, sizeof(buf) - 1);
if (len < 0) {
/* ignore too long headers */
ast_log(LOG_WARNING, "Could not store header '%s' from transfer on channel '%s' - too long text\n", hdr_name, ast_channel_name(chan));
continue;
}
buf[len] = '\0';
/* Get value - remove header name (before ':') from buf and trim blanks. */
value_str = strchr(buf, ':');
if (!value_str) {
/* Ignore header without value */
continue;
}
value_str = ast_strip(value_str + 1); /* +1 to get string right after the ':' delimiter (i.e. header value) */
ast_str_set(&pbxvar, -1, "~HASH~TRANSFER_DATA~%.*s~", hdr_name_strlen, hdr_name);
pbx_builtin_setvar_helper(chan, ast_str_buffer(pbxvar), value_str);
ast_debug(5, "On channel '%s' set TRANSFER_DATA variable '%s' to value '%s' \n", ast_channel_name(chan), hdr_name, value_str);
}
}
ast_free(pbxvar);
} else {
ast_log(LOG_ERROR, "Channel '%s' failed to allocate buffer for TRANSFER_DATA variable\n", ast_channel_name(chan));
}
}
referred_by = pjsip_msg_find_hdr_by_names(refer->rdata->msg_info.msg,
&str_referred_by, &str_referred_by_s, NULL);
if (referred_by) {
size_t uri_size = pj_strlen(&referred_by->hvalue) + 1;
char *uri = ast_alloca(uri_size);
ast_copy_pj_str(uri, &referred_by->hvalue, uri_size);
pbx_builtin_setvar_helper(chan, "__SIPREFERREDBYHDR", S_OR(uri, NULL));
} else {
pbx_builtin_setvar_helper(chan, "SIPREFERREDBYHDR", NULL);
}
if (refer->replaces) {
char replaces[512];
char *replaces_val = NULL;
int len;
len = pjsip_hdr_print_on(refer->replaces, replaces, sizeof(replaces) - 1);
if (len != -1) {
/* pjsip_hdr_print_on does not NULL terminate the buffer */
replaces[len] = '\0';
replaces_val = replaces + sizeof("Replaces:");
}
pbx_builtin_setvar_helper(chan, "__SIPREPLACESHDR", replaces_val);
} else {
pbx_builtin_setvar_helper(chan, "SIPREPLACESHDR", NULL);
}
if (refer->refer_to) {
char refer_to[PJSIP_MAX_URL_SIZE];
pjsip_uri_print(PJSIP_URI_IN_REQ_URI, refer->refer_to, refer_to, sizeof(refer_to));
pbx_builtin_setvar_helper(chan, "SIPREFERTOHDR", S_OR(refer_to, NULL));
} else {
pbx_builtin_setvar_helper(chan, "SIPREFERTOHDR", NULL);
}
}
/*!
* \internal
* \brief Set the passed in context variable to the determined transfer context.
* \since 13.3.0
*
* \param context Set to the determined transfer context.
* \param session INVITE dialog SIP session.
*/
#define DETERMINE_TRANSFER_CONTEXT(context, session) \
do { \
ast_channel_lock((session)->channel); \
context = pbx_builtin_getvar_helper((session)->channel, "TRANSFER_CONTEXT"); \
if (ast_strlen_zero(context)) { \
context = (session)->endpoint->context; \
} else { \
context = ast_strdupa(context); \
} \
ast_channel_unlock((session)->channel); \
} while (0) \
struct refer_data {
struct ast_refer *refer;
char *destination;
char *from;
char *refer_to;
int to_self;
};
static void refer_data_destroy(void *obj)
{
struct refer_data *rdata = obj;
ast_free(rdata->destination);
ast_free(rdata->from);
ast_free(rdata->refer_to);
ast_refer_destroy(rdata->refer);
}
static struct refer_data *refer_data_create(const struct ast_refer *refer)
{
char *uri_params;
const char *destination;
struct refer_data *rdata = ao2_alloc_options(sizeof(*rdata), refer_data_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
if (!rdata) {
return NULL;
}
/* typecast to suppress const warning */
rdata->refer = ast_refer_ref((struct ast_refer *) refer);
destination = ast_refer_get_to(refer);
/* To starts with 'pjsip:' which needs to be removed. */
if (!(destination = strchr(destination, ':'))) {
goto failure;
}
++destination;/* Now skip the ':' */
rdata->destination = ast_strdup(destination);
if (!rdata->destination) {
goto failure;
}
rdata->from = ast_strdup(ast_refer_get_from(refer));
if (!rdata->from) {
goto failure;
}
rdata->refer_to = ast_strdup(ast_refer_get_refer_to(refer));
if (!rdata->refer_to) {
goto failure;
}
rdata->to_self = ast_refer_get_to_self(refer);
/*
* Sometimes from URI can contain URI parameters, so remove them.
*
* sip:user;user-options@domain;uri-parameters
*/
uri_params = strchr(rdata->from, '@');
if (uri_params && (uri_params = strchr(uri_params, ';'))) {
*uri_params = '\0';
}
return rdata;
failure:
ao2_cleanup(rdata);
return NULL;
}
/*!
* \internal
* \brief Checks if the given refer var name should be blocked.
*
* \details Some headers are not allowed to be overridden by the user.
* Determine if the given var header name from the user is blocked for
* an outgoing REFER.
*
* \param name name of header to see if it is blocked.
*
* \retval TRUE if the given header is blocked.
*/
static int is_refer_var_blocked(const char *name)
{
int i;
/* Don't block the Max-Forwards header because the user can override it */
static const char *hdr[] = {
"To",
"From",
"Via",
"Route",
"Contact",
"Call-ID",
"CSeq",
"Allow",
"Content-Length",
"Content-Type",
"Request-URI",
};
for (i = 0; i < ARRAY_LEN(hdr); ++i) {
if (!strcasecmp(name, hdr[i])) {
/* Block addition of this header. */
return 1;
}
}
return 0;
}
/*!
* \internal
* \brief Copies any other refer vars over to the request headers.
*
* \param refer The refer structure to copy headers from
* \param tdata The SIP transmission data
*/
static enum pjsip_status_code vars_to_headers(const struct ast_refer *refer, pjsip_tx_data *tdata)
{
const char *name;
const char *value;
struct ast_refer_var_iterator *iter;
for (iter = ast_refer_var_iterator_init(refer);
ast_refer_var_iterator_next(iter, &name, &value);
ast_refer_var_unref_current(iter)) {
if (!is_refer_var_blocked(name)) {
ast_sip_add_header(tdata, name, value);
}
}
ast_refer_var_iterator_destroy(iter);
return PJSIP_SC_OK;
}
struct refer_out_of_dialog {
pjsip_dialog *dlg;
int authentication_challenge_count;
};
/*! \brief REFER Out-of-dialog module, used to attach session data structure to subscription */
static pjsip_module refer_out_of_dialog_module = {
.name = { "REFER Out-of-dialog Module", 26 },
.id = -1,
.on_tx_request = refer_on_tx_request,
/* Ensure that we are called after res_pjsp_nat module and before transport priority */
.priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 4,
};
/*! \brief Helper function which returns the name-addr of the Refer-To header or NULL */
static pjsip_uri *get_refer_to_uri(pjsip_tx_data *tdata)