2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <depend>res_pjsip_session</depend>
23 <depend>res_pjsip_pubsub</depend>
24 <support_level>core</support_level>
32 #include "asterisk/res_pjsip.h"
33 #include "asterisk/res_pjsip_session.h"
34 #include "asterisk/module.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/taskprocessor.h"
37 #include "asterisk/bridge.h"
38 #include "asterisk/framehook.h"
39 #include "asterisk/stasis_bridges.h"
40 #include "asterisk/stasis_channels.h"
42 /*! \brief REFER Progress structure */
43 struct refer_progress {
44 /*! \brief Subscription to provide updates on */
46 /*! \brief Dialog for subscription */
48 /*! \brief Received packet, used to construct final response in case no subscription exists */
50 /*! \brief Frame hook for monitoring REFER progress */
52 /*! \brief Last received subclass in frame hook */
54 /*! \brief Serializer for notifications */
55 struct ast_taskprocessor *serializer;
56 /*! \brief Stasis subscription for bridge events */
57 struct stasis_subscription *bridge_sub;
58 /*! \brief Uniqueid of transferee channel */
62 /*! \brief REFER Progress notification structure */
63 struct refer_progress_notification {
64 /*! \brief Refer progress structure to send notification on */
65 struct refer_progress *progress;
66 /*! \brief SIP response code to send */
68 /*! \brief Subscription state */
69 pjsip_evsub_state state;
72 /*! \brief REFER Progress module, used to attach REFER progress structure to subscriptions */
73 static pjsip_module refer_progress_module = {
74 .name = { "REFER Progress", 14 },
78 /*! \brief Destructor for REFER Progress notification structure */
79 static void refer_progress_notification_destroy(void *obj)
81 struct refer_progress_notification *notification = obj;
83 ao2_cleanup(notification->progress);
86 /*! \brief Allocator for REFER Progress notification structure */
87 static struct refer_progress_notification *refer_progress_notification_alloc(struct refer_progress *progress, int response,
88 pjsip_evsub_state state)
90 struct refer_progress_notification *notification = ao2_alloc(sizeof(*notification), refer_progress_notification_destroy);
96 ao2_ref(progress, +1);
97 notification->progress = progress;
98 notification->response = response;
99 notification->state = state;
104 /*! \brief Serialized callback for subscription notification */
105 static int refer_progress_notify(void *data)
107 RAII_VAR(struct refer_progress_notification *, notification, data, ao2_cleanup);
109 pjsip_tx_data *tdata;
111 /* If the subscription has already been terminated we can't send a notification */
112 if (!(sub = notification->progress->sub)) {
113 ast_debug(3, "Not sending NOTIFY of response '%d' and state '%d' on progress monitor '%p' as subscription has been terminated\n",
114 notification->response, notification->state, notification->progress);
118 /* If the subscription is being terminated we want to actually remove the progress structure here to
119 * stop a deadlock from occurring - basically terminated changes the state which queues a synchronous task
120 * but we are already running a task... thus it would deadlock */
121 if (notification->state == PJSIP_EVSUB_STATE_TERMINATED) {
122 ast_debug(3, "Subscription '%p' is being terminated as a result of a NOTIFY, removing REFER progress structure early on progress monitor '%p'\n",
123 notification->progress->sub, notification->progress);
124 pjsip_dlg_inc_lock(notification->progress->dlg);
125 pjsip_evsub_set_mod_data(notification->progress->sub, refer_progress_module.id, NULL);
126 pjsip_dlg_dec_lock(notification->progress->dlg);
128 /* This is for dropping the reference on the subscription */
129 ao2_cleanup(notification->progress);
131 notification->progress->sub = NULL;
134 ast_debug(3, "Sending NOTIFY with response '%d' and state '%d' on subscription '%p' and progress monitor '%p'\n",
135 notification->response, notification->state, sub, notification->progress);
137 /* Actually send the notification */
138 if (pjsip_xfer_notify(sub, notification->state, notification->response, NULL, &tdata) == PJ_SUCCESS) {
139 pjsip_xfer_send_request(sub, tdata);
145 static void refer_progress_bridge(void *data, struct stasis_subscription *sub,
146 struct stasis_message *message)
148 struct refer_progress *progress = data;
149 struct ast_bridge_blob *enter_blob;
150 struct refer_progress_notification *notification;
152 if (stasis_subscription_final_message(sub, message)) {
153 ao2_ref(progress, -1);
157 if (ast_channel_entered_bridge_type() != stasis_message_type(message)) {
162 enter_blob = stasis_message_data(message);
163 if (strcmp(enter_blob->channel->uniqueid, progress->transferee)) {
168 /* OMG the transferee is joining a bridge. His call got answered! */
169 notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
171 if (ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
172 ao2_cleanup(notification);
174 progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
178 /*! \brief Progress monitoring frame hook - examines frames to determine state of transfer */
179 static struct ast_frame *refer_progress_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
181 struct refer_progress *progress = data;
182 struct refer_progress_notification *notification = NULL;
184 /* We only care about frames *to* the channel */
185 if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
189 /* Determine the state of the REFER based on the control frames (or voice frames) passing */
190 if (f->frametype == AST_FRAME_VOICE && !progress->subclass) {
191 /* Media is passing without progress, this means the call has been answered */
192 notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
193 } else if (f->frametype == AST_FRAME_CONTROL) {
195 /* Based on the control frame being written we can send a NOTIFY advising of the progress */
196 if ((f->subclass.integer == AST_CONTROL_RING) || (f->subclass.integer == AST_CONTROL_RINGING)) {
197 progress->subclass = f->subclass.integer;
198 notification = refer_progress_notification_alloc(progress, 180, PJSIP_EVSUB_STATE_ACTIVE);
199 } else if (f->subclass.integer == AST_CONTROL_BUSY) {
200 progress->subclass = f->subclass.integer;
201 notification = refer_progress_notification_alloc(progress, 486, PJSIP_EVSUB_STATE_TERMINATED);
202 } else if (f->subclass.integer == AST_CONTROL_CONGESTION) {
203 progress->subclass = f->subclass.integer;
204 notification = refer_progress_notification_alloc(progress, 503, PJSIP_EVSUB_STATE_TERMINATED);
205 } else if (f->subclass.integer == AST_CONTROL_PROGRESS) {
206 progress->subclass = f->subclass.integer;
207 notification = refer_progress_notification_alloc(progress, 183, PJSIP_EVSUB_STATE_ACTIVE);
208 } else if (f->subclass.integer == AST_CONTROL_PROCEEDING) {
209 progress->subclass = f->subclass.integer;
210 notification = refer_progress_notification_alloc(progress, 100, PJSIP_EVSUB_STATE_ACTIVE);
211 } else if (f->subclass.integer == AST_CONTROL_ANSWER) {
212 progress->subclass = f->subclass.integer;
213 notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
217 /* If a notification is due to be sent push it to the thread pool */
219 if (ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
220 ao2_cleanup(notification);
223 /* If the subscription is being terminated we don't need the frame hook any longer */
224 if (notification->state == PJSIP_EVSUB_STATE_TERMINATED) {
225 ast_debug(3, "Detaching REFER progress monitoring hook from '%s' as subscription is being terminated\n",
226 ast_channel_name(chan));
227 ast_framehook_detach(chan, progress->framehook);
234 /*! \brief Destroy callback for monitoring framehook */
235 static void refer_progress_framehook_destroy(void *data)
237 struct refer_progress *progress = data;
238 struct refer_progress_notification *notification = refer_progress_notification_alloc(progress, 503, PJSIP_EVSUB_STATE_TERMINATED);
240 if (notification && ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
241 ao2_cleanup(notification);
244 ao2_cleanup(progress);
247 /*! \brief Serialized callback for subscription termination */
248 static int refer_progress_terminate(void *data)
250 struct refer_progress *progress = data;
252 /* The subscription is no longer valid */
253 progress->sub = NULL;
258 /*! \brief Callback for REFER subscription state changes */
259 static void refer_progress_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)
261 struct refer_progress *progress = pjsip_evsub_get_mod_data(sub, refer_progress_module.id);
263 /* If being destroyed queue it up to the serializer */
264 if (progress && (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED)) {
265 /* To prevent a deadlock race condition we unlock the dialog so other serialized tasks can execute */
266 ast_debug(3, "Subscription '%p' has been remotely terminated, waiting for other tasks to complete on progress monitor '%p'\n",
269 /* It's possible that a task is waiting to remove us already, so bump the refcount of progress so it doesn't get destroyed */
270 ao2_ref(progress, +1);
271 pjsip_dlg_dec_lock(progress->dlg);
272 ast_sip_push_task_synchronous(progress->serializer, refer_progress_terminate, progress);
273 pjsip_dlg_inc_lock(progress->dlg);
274 ao2_ref(progress, -1);
276 ast_debug(3, "Subscription '%p' removed from progress monitor '%p'\n", sub, progress);
278 /* Since it was unlocked it is possible for this to have been removed already, so check again */
279 if (pjsip_evsub_get_mod_data(sub, refer_progress_module.id)) {
280 pjsip_evsub_set_mod_data(sub, refer_progress_module.id, NULL);
281 ao2_cleanup(progress);
286 /*! \brief Callback structure for subscription */
287 static pjsip_evsub_user refer_progress_evsub_cb = {
288 .on_evsub_state = refer_progress_on_evsub_state,
291 /*! \brief Destructor for REFER progress sutrcture */
292 static void refer_progress_destroy(void *obj)
294 struct refer_progress *progress = obj;
296 if (progress->bridge_sub) {
297 progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
300 ast_free(progress->transferee);
301 ast_taskprocessor_unreference(progress->serializer);
304 /*! \brief Internal helper function which sets up a refer progress structure if needed */
305 static int refer_progress_alloc(struct ast_sip_session *session, pjsip_rx_data *rdata, struct refer_progress **progress)
307 const pj_str_t str_refer_sub = { "Refer-Sub", 9 };
308 pjsip_generic_string_hdr *refer_sub = NULL;
309 const pj_str_t str_true = { "true", 4 };
310 pjsip_tx_data *tdata;
315 /* Grab the optional Refer-Sub header, it can be used to suppress the implicit subscription */
316 refer_sub = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_sub, NULL);
317 if ((refer_sub && pj_strnicmp(&refer_sub->hvalue, &str_true, 4))) {
321 if (!(*progress = ao2_alloc(sizeof(struct refer_progress), refer_progress_destroy))) {
325 ast_debug(3, "Created progress monitor '%p' for transfer occurring from channel '%s' and endpoint '%s'\n",
326 progress, ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
328 (*progress)->framehook = -1;
330 /* To prevent a potential deadlock we need the dialog so we can lock/unlock */
331 (*progress)->dlg = session->inv_session->dlg;
333 if (!((*progress)->serializer = ast_sip_create_serializer())) {
337 /* Create the implicit subscription for monitoring of this transfer */
338 if (pjsip_xfer_create_uas(session->inv_session->dlg, &refer_progress_evsub_cb, rdata, &(*progress)->sub) != PJ_SUCCESS) {
342 /* Associate the REFER progress structure with the subscription */
343 ao2_ref(*progress, +1);
344 pjsip_evsub_set_mod_data((*progress)->sub, refer_progress_module.id, *progress);
346 pj_list_init(&hdr_list);
348 pjsip_hdr *hdr = (pjsip_hdr*)pjsip_generic_string_hdr_create(session->inv_session->dlg->pool, &str_refer_sub, &str_true);
350 pj_list_push_back(&hdr_list, hdr);
353 /* Accept the REFER request */
354 ast_debug(3, "Accepting REFER request for progress monitor '%p'\n", *progress);
355 pjsip_xfer_accept((*progress)->sub, rdata, 202, &hdr_list);
357 /* Send initial NOTIFY Request */
358 ast_debug(3, "Sending initial 100 Trying NOTIFY for progress monitor '%p'\n", *progress);
359 if (pjsip_xfer_notify((*progress)->sub, PJSIP_EVSUB_STATE_ACTIVE, 100, NULL, &tdata) == PJ_SUCCESS) {
360 pjsip_xfer_send_request((*progress)->sub, tdata);
366 ao2_cleanup(*progress);
371 /*! \brief Structure for attended transfer task */
372 struct refer_attended {
373 /*! \brief Transferer session */
374 struct ast_sip_session *transferer;
375 /*! \brief Transferer channel */
376 struct ast_channel *transferer_chan;
377 /*! \brief Second transferer session */
378 struct ast_sip_session *transferer_second ;
379 /*! \brief Optional refer progress structure */
380 struct refer_progress *progress;
383 /*! \brief Destructor for attended transfer task */
384 static void refer_attended_destroy(void *obj)
386 struct refer_attended *attended = obj;
388 ao2_cleanup(attended->transferer);
389 ast_channel_unref(attended->transferer_chan);
390 ao2_cleanup(attended->transferer_second);
393 /*! \brief Allocator for attended transfer task */
394 static struct refer_attended *refer_attended_alloc(struct ast_sip_session *transferer, struct ast_sip_session *transferer_second,
395 struct refer_progress *progress)
397 struct refer_attended *attended = ao2_alloc(sizeof(*attended), refer_attended_destroy);
403 ao2_ref(transferer, +1);
404 attended->transferer = transferer;
405 ast_channel_ref(transferer->channel);
406 attended->transferer_chan = transferer->channel;
407 ao2_ref(transferer_second, +1);
408 attended->transferer_second = transferer_second;
411 ao2_ref(progress, +1);
412 attended->progress = progress;
418 /*! \brief Task for attended transfer */
419 static int refer_attended(void *data)
421 RAII_VAR(struct refer_attended *, attended, data, ao2_cleanup);
424 if (!attended->transferer_second->channel) {
428 ast_debug(3, "Performing a REFER attended transfer - Transferer #1: %s Transferer #2: %s\n",
429 ast_channel_name(attended->transferer_chan), ast_channel_name(attended->transferer_second->channel));
431 switch (ast_bridge_transfer_attended(attended->transferer_chan, attended->transferer_second->channel)) {
432 case AST_BRIDGE_TRANSFER_INVALID:
435 case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
438 case AST_BRIDGE_TRANSFER_FAIL:
441 case AST_BRIDGE_TRANSFER_SUCCESS:
443 ast_sip_session_defer_termination(attended->transferer);
447 ast_debug(3, "Final response for REFER attended transfer - Transferer #1: %s Transferer #2: %s is '%d'\n",
448 ast_channel_name(attended->transferer_chan), ast_channel_name(attended->transferer_second->channel), response);
450 if (attended->progress && response) {
451 struct refer_progress_notification *notification = refer_progress_notification_alloc(attended->progress, response, PJSIP_EVSUB_STATE_TERMINATED);
454 refer_progress_notify(notification);
461 /*! \brief Structure for blind transfer callback details */
463 /*! \brief Context being used for transfer */
465 /*! \brief Optional progress structure */
466 struct refer_progress *progress;
467 /*! \brief REFER message */
468 pjsip_rx_data *rdata;
469 /*! \brief Optional Replaces header */
470 pjsip_replaces_hdr *replaces;
471 /*! \brief Optional Refer-To header */
472 pjsip_sip_uri *refer_to;
475 /*! \brief Blind transfer callback function */
476 static void refer_blind_callback(struct ast_channel *chan, void *user_data, enum ast_transfer_type transfer_type)
478 struct refer_blind *refer = user_data;
479 const pj_str_t str_referred_by = { "Referred-By", 11 };
480 pjsip_generic_string_hdr *referred_by = pjsip_msg_find_hdr_by_name(refer->rdata->msg_info.msg, &str_referred_by, NULL);
482 pbx_builtin_setvar_helper(chan, "SIPTRANSFER", "yes");
484 /* If progress monitoring is being done attach a frame hook so we can monitor it */
485 if (refer->progress) {
486 struct ast_framehook_interface hook = {
487 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
488 .event_cb = refer_progress_framehook,
489 .destroy_cb = refer_progress_framehook_destroy,
490 .data = refer->progress,
493 refer->progress->transferee = ast_strdup(ast_channel_uniqueid(chan));
494 if (!refer->progress->transferee) {
495 struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
496 PJSIP_EVSUB_STATE_TERMINATED);
498 ast_log(LOG_WARNING, "Could not copy channel name '%s' during transfer - assuming success\n",
499 ast_channel_name(chan));
502 refer_progress_notify(notification);
506 /* We need to bump the reference count up on the progress structure since it is in the frame hook now */
507 ao2_ref(refer->progress, +1);
509 /* If we can't attach a frame hook for whatever reason send a notification of success immediately */
510 if ((refer->progress->framehook = ast_framehook_attach(chan, &hook)) < 0) {
511 struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
512 PJSIP_EVSUB_STATE_TERMINATED);
514 ast_log(LOG_WARNING, "Could not attach REFER transfer progress monitoring hook to channel '%s' - assuming success\n",
515 ast_channel_name(chan));
518 refer_progress_notify(notification);
521 ao2_cleanup(refer->progress);
524 /* We need to bump the reference count for the stasis subscription */
525 ao2_ref(refer->progress, +1);
526 /* We also will need to detect if the transferee enters a bridge. This is currently the only reliable way to
527 * detect if the transfer target has answered the call
529 refer->progress->bridge_sub = stasis_subscribe(ast_bridge_topic_all(), refer_progress_bridge, refer->progress);
530 if (!refer->progress->bridge_sub) {
531 struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
532 PJSIP_EVSUB_STATE_TERMINATED);
534 ast_log(LOG_WARNING, "Could not create bridge stasis subscription for monitoring progress on transfer of channel '%s' - assuming success\n",
535 ast_channel_name(chan));
538 refer_progress_notify(notification);
541 ast_framehook_detach(chan, refer->progress->framehook);
543 ao2_cleanup(refer->progress);
547 if (!ast_strlen_zero(refer->context)) {
548 pbx_builtin_setvar_helper(chan, "SIPREFERRINGCONTEXT", refer->context);
552 char *uri = referred_by->hvalue.ptr;
554 uri[referred_by->hvalue.slen] = '\0';
555 pbx_builtin_setvar_helper(chan, "SIPREFERREDBYHDR", uri);
558 if (refer->replaces) {
561 pjsip_hdr_print_on(refer->replaces, replaces, sizeof(replaces));
562 pbx_builtin_setvar_helper(chan, "SIPREPLACESHDR", replaces);
565 if (refer->refer_to) {
566 char refer_to[PJSIP_MAX_URL_SIZE];
568 pjsip_uri_print(PJSIP_URI_IN_REQ_URI, refer->refer_to, refer_to, sizeof(refer_to));
569 pbx_builtin_setvar_helper(chan, "SIPREFERTOHDR", refer_to);
573 static int refer_incoming_attended_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_sip_uri *target_uri,
574 pjsip_param *replaces_param, struct refer_progress *progress)
576 const pj_str_t str_replaces = { "Replaces", 8 };
577 pj_str_t replaces_content;
578 pjsip_replaces_hdr *replaces;
582 pj_strdup_with_null(rdata->tp_info.pool, &replaces_content, &replaces_param->value);
584 /* Parsing the parameter as a Replaces header easily grabs the needed information */
585 if (!(replaces = pjsip_parse_hdr(rdata->tp_info.pool, &str_replaces, replaces_content.ptr,
586 pj_strlen(&replaces_content), &parsed_len))) {
587 ast_log(LOG_ERROR, "Received REFER request on channel '%s' from endpoint '%s' with invalid Replaces header, rejecting\n",
588 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
592 /* See if the dialog is local, or remote */
593 if ((dlg = pjsip_ua_find_dialog(&replaces->call_id, &replaces->to_tag, &replaces->from_tag, PJ_TRUE))) {
594 RAII_VAR(struct ast_sip_session *, other_session, ast_sip_dialog_get_session(dlg), ao2_cleanup);
595 struct refer_attended *attended;
597 pjsip_dlg_dec_lock(dlg);
599 if (!other_session) {
600 ast_debug(3, "Received REFER request on channel '%s' from endpoint '%s' for local dialog but no session exists on it\n",
601 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
605 /* We defer actually doing the attended transfer to the other session so no deadlock can occur */
606 if (!(attended = refer_attended_alloc(session, other_session, progress))) {
607 ast_log(LOG_ERROR, "Received REFER request on channel '%s' from endpoint '%s' for local dialog but could not allocate structure to complete, rejecting\n",
608 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
612 /* Push it to the other session, which will have both channels with minimal locking */
613 if (ast_sip_push_task(other_session->serializer, refer_attended, attended)) {
614 ao2_cleanup(attended);
618 ast_debug(3, "Attended transfer from '%s' pushed to second channel serializer\n",
619 ast_channel_name(session->channel));
623 const char *context = (session->channel ? pbx_builtin_getvar_helper(session->channel, "TRANSFER_CONTEXT") : "");
624 struct refer_blind refer = { 0, };
626 if (ast_strlen_zero(context)) {
627 context = session->endpoint->context;
630 if (!ast_exists_extension(NULL, context, "external_replaces", 1, NULL)) {
631 ast_log(LOG_ERROR, "Received REFER for remote session on channel '%s' from endpoint '%s' but 'external_replaces' context does not exist for handling\n",
632 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
636 refer.context = context;
637 refer.progress = progress;
639 refer.replaces = replaces;
640 refer.refer_to = target_uri;
642 switch (ast_bridge_transfer_blind(1, session->channel, "external_replaces", context, refer_blind_callback, &refer)) {
643 case AST_BRIDGE_TRANSFER_INVALID:
645 case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
647 case AST_BRIDGE_TRANSFER_FAIL:
649 case AST_BRIDGE_TRANSFER_SUCCESS:
650 ast_sip_session_defer_termination(session);
660 static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_sip_uri *target,
661 struct refer_progress *progress)
663 const char *context = (session->channel ? pbx_builtin_getvar_helper(session->channel, "TRANSFER_CONTEXT") : "");
664 char exten[AST_MAX_EXTENSION];
665 struct refer_blind refer = { 0, };
667 /* If no explicit transfer context has been provided use their configured context */
668 if (ast_strlen_zero(context)) {
669 context = session->endpoint->context;
672 /* Using the user portion of the target URI see if it exists as a valid extension in their context */
673 ast_copy_pj_str(exten, &target->user, sizeof(exten));
674 if (!ast_exists_extension(NULL, context, exten, 1, NULL)) {
675 ast_log(LOG_ERROR, "Channel '%s' from endpoint '%s' attempted blind transfer to '%s@%s' but target does not exist\n",
676 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), exten, context);
680 refer.context = context;
681 refer.progress = progress;
684 switch (ast_bridge_transfer_blind(1, session->channel, exten, context, refer_blind_callback, &refer)) {
685 case AST_BRIDGE_TRANSFER_INVALID:
687 case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
689 case AST_BRIDGE_TRANSFER_FAIL:
691 case AST_BRIDGE_TRANSFER_SUCCESS:
692 ast_sip_session_defer_termination(session);
699 /*! \brief Structure used to retrieve channel from another session */
700 struct invite_replaces {
701 /*! \brief Session we want the channel from */
702 struct ast_sip_session *session;
703 /*! \brief Channel from the session (with reference) */
704 struct ast_channel *channel;
705 /*! \brief Bridge the channel is in */
706 struct ast_bridge *bridge;
709 /*! \brief Task for invite replaces */
710 static int invite_replaces(void *data)
712 struct invite_replaces *invite = data;
714 if (!invite->session->channel) {
718 ast_channel_ref(invite->session->channel);
719 invite->channel = invite->session->channel;
721 ast_channel_lock(invite->channel);
722 invite->bridge = ast_channel_get_bridge(invite->channel);
723 ast_channel_unlock(invite->channel);
728 static int refer_incoming_invite_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
730 pjsip_dialog *other_dlg = NULL;
731 pjsip_tx_data *packet;
733 RAII_VAR(struct ast_sip_session *, other_session, NULL, ao2_cleanup);
734 struct invite_replaces invite;
735 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
737 /* If a Replaces header is present make sure it is valid */
738 if (pjsip_replaces_verify_request(rdata, &other_dlg, PJ_TRUE, &packet) != PJ_SUCCESS) {
739 response = packet->msg->line.status.code;
740 pjsip_tx_data_dec_ref(packet);
744 /* If no other dialog exists then this INVITE request does not have a Replaces header */
749 other_session = ast_sip_dialog_get_session(other_dlg);
750 pjsip_dlg_dec_lock(other_dlg);
752 if (!other_session) {
754 ast_debug(3, "INVITE with Replaces received on channel '%s' from endpoint '%s', but requested session does not exist\n",
755 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
759 invite.session = other_session;
761 if (ast_sip_push_task_synchronous(other_session->serializer, invite_replaces, &invite)) {
766 ast_channel_lock(session->channel);
767 ast_setstate(session->channel, AST_STATE_RING);
768 ast_channel_unlock(session->channel);
769 ast_raw_answer(session->channel);
771 if (!invite.bridge) {
772 struct ast_channel *chan = session->channel;
774 /* This will use a synchronous task but we aren't operating in the serializer at this point in time, so it
776 if (!ast_channel_move(invite.channel, session->channel)) {
782 if (ast_bridge_impart(invite.bridge, session->channel, invite.channel, NULL,
783 AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
789 ast_debug(3, "INVITE with Replaces successfully completed on channels '%s' and '%s'\n",
790 ast_channel_name(session->channel), ast_channel_name(invite.channel));
793 ast_channel_unref(invite.channel);
794 ao2_cleanup(invite.bridge);
798 ast_debug(3, "INVITE with Replaces failed on channel '%s', sending response of '%d'\n",
799 ast_channel_name(session->channel), response);
800 session->defer_terminate = 1;
801 ast_hangup(session->channel);
802 session->channel = NULL;
804 if (pjsip_inv_end_session(session->inv_session, response, NULL, &packet) == PJ_SUCCESS) {
805 ast_sip_session_send_response(session, packet);
812 static int refer_incoming_refer_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
814 const pj_str_t str_refer_to = { "Refer-To", 8 };
815 pjsip_generic_string_hdr *refer_to;
817 const pj_str_t str_to = { "To", 2 };
818 pjsip_fromto_hdr *target;
819 pjsip_sip_uri *target_uri;
820 RAII_VAR(struct refer_progress *, progress, NULL, ao2_cleanup);
821 const pj_str_t str_replaces = { "Replaces", 8 };
822 pjsip_param *replaces;
825 if (!session->endpoint->allowtransfer) {
826 pjsip_dlg_respond(session->inv_session->dlg, rdata, 603, NULL, NULL, NULL);
827 ast_log(LOG_WARNING, "Endpoint %s transfer attempt blocked due to configuration\n",
828 ast_sorcery_object_get_id(session->endpoint));
832 /* A Refer-To header is required */
833 if (!(refer_to = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL))) {
834 pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
835 ast_debug(3, "Received a REFER without Refer-To on channel '%s' from endpoint '%s'\n",
836 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
839 uri = refer_to->hvalue.ptr;
840 uri[refer_to->hvalue.slen] = '\0';
842 /* Parse the provided URI string as a To header so we can get the target */
843 if (!(target = pjsip_parse_hdr(rdata->tp_info.pool, &str_to, refer_to->hvalue.ptr, refer_to->hvalue.slen, NULL)) ||
844 (!PJSIP_URI_SCHEME_IS_SIP(target->uri) && !PJSIP_URI_SCHEME_IS_SIPS(target->uri))) {
845 pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
846 ast_debug(3, "Received a REFER without a parseable Refer-To ('%s') on channel '%s' from endpoint '%s'\n",
847 uri, ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
850 target_uri = pjsip_uri_get_uri(target->uri);
852 /* Set up REFER progress subscription if requested/possible */
853 if (refer_progress_alloc(session, rdata, &progress)) {
854 pjsip_dlg_respond(session->inv_session->dlg, rdata, 500, NULL, NULL, NULL);
855 ast_debug(3, "Could not set up subscription for REFER on channel '%s' from endpoint '%s'\n",
856 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
860 /* Determine if this is an attended or blind transfer */
861 if ((replaces = pjsip_param_find(&target_uri->header_param, &str_replaces)) ||
862 (replaces = pjsip_param_find(&target_uri->other_param, &str_replaces))) {
863 response = refer_incoming_attended_request(session, rdata, target_uri, replaces, progress);
865 response = refer_incoming_blind_request(session, rdata, target_uri, progress);
869 /* The transferer has requested no subscription, so send a final response immediately */
870 pjsip_tx_data *tdata;
871 const pj_str_t str_refer_sub = { "Refer-Sub", 9 };
872 const pj_str_t str_false = { "false", 5 };
875 ast_debug(3, "Progress monitoring not requested for REFER on channel '%s' from endpoint '%s', sending immediate response of '%d'\n",
876 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), response);
878 if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, response, NULL, &tdata) != PJ_SUCCESS) {
879 pjsip_dlg_respond(session->inv_session->dlg, rdata, response, NULL, NULL, NULL);
883 hdr = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool, &str_refer_sub, &str_false);
884 pjsip_msg_add_hdr(tdata->msg, hdr);
886 pjsip_dlg_send_response(session->inv_session->dlg, pjsip_rdata_get_tsx(rdata), tdata);
887 } else if (response != 200) {
888 /* Since this failed we can send a final NOTIFY now and terminate the subscription */
889 struct refer_progress_notification *notification = refer_progress_notification_alloc(progress, response, PJSIP_EVSUB_STATE_TERMINATED);
892 /* The refer_progress_notify function will call ao2_cleanup on this for us */
893 refer_progress_notify(notification);
900 static int refer_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
902 if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_refer_method())) {
903 return refer_incoming_refer_request(session, rdata);
904 } else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_invite_method)) {
905 return refer_incoming_invite_request(session, rdata);
911 static void refer_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
913 const char *replaces;
915 if (pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_invite_method) ||
917 (session->inv_session->state != PJSIP_INV_STATE_CALLING) ||
918 !(replaces = pbx_builtin_getvar_helper(session->channel, "SIPREPLACESHDR"))) {
922 ast_sip_add_header(tdata, "Replaces", replaces);
925 static struct ast_sip_session_supplement refer_supplement = {
926 .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 1,
927 .incoming_request = refer_incoming_request,
928 .outgoing_request = refer_outgoing_request,
931 static int load_module(void)
933 const pj_str_t str_norefersub = { "norefersub", 10 };
935 pjsip_replaces_init_module(ast_sip_get_pjsip_endpoint());
936 pjsip_xfer_init_module(ast_sip_get_pjsip_endpoint());
937 pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), NULL, PJSIP_H_SUPPORTED, NULL, 1, &str_norefersub);
939 ast_sip_register_service(&refer_progress_module);
940 ast_sip_session_register_supplement(&refer_supplement);
942 return AST_MODULE_LOAD_SUCCESS;
945 static int unload_module(void)
947 ast_sip_session_unregister_supplement(&refer_supplement);
948 ast_sip_unregister_service(&refer_progress_module);
953 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Blind and Attended Transfer Support",
955 .unload = unload_module,
956 .load_pri = AST_MODPRI_APP_DEPEND,