res_pjsip_refer.c: Fix seg fault in process of Refer-to header.
[asterisk/asterisk.git] / res / res_pjsip_refer.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <depend>res_pjsip_session</depend>
23         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29 #include <pjsip_ua.h>
30
31 #include "asterisk/res_pjsip.h"
32 #include "asterisk/res_pjsip_session.h"
33 #include "asterisk/module.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/taskprocessor.h"
36 #include "asterisk/bridge.h"
37 #include "asterisk/framehook.h"
38 #include "asterisk/stasis_bridges.h"
39 #include "asterisk/stasis_channels.h"
40 #include "asterisk/causes.h"
41
42 /*! \brief REFER Progress structure */
43 struct refer_progress {
44         /*! \brief Subscription to provide updates on */
45         pjsip_evsub *sub;
46         /*! \brief Dialog for subscription */
47         pjsip_dialog *dlg;
48         /*! \brief Received packet, used to construct final response in case no subscription exists */
49         pjsip_rx_data *rdata;
50         /*! \brief Frame hook for monitoring REFER progress */
51         int framehook;
52         /*! \brief Last received subclass in frame hook */
53         int subclass;
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 Reference to transfer_channel_data related to the refer */
59         struct transfer_channel_data *transfer_data;
60         /*! \brief Uniqueid of transferee channel */
61         char *transferee;
62         /*! \brief Non-zero if the 100 notify has been sent */
63         int sent_100;
64 };
65
66 /*! \brief REFER Progress notification structure */
67 struct refer_progress_notification {
68         /*! \brief Refer progress structure to send notification on */
69         struct refer_progress *progress;
70         /*! \brief SIP response code to send */
71         int response;
72         /*! \brief Subscription state */
73         pjsip_evsub_state state;
74 };
75
76 /*! \brief REFER Progress module, used to attach REFER progress structure to subscriptions */
77 static pjsip_module refer_progress_module = {
78         .name = { "REFER Progress", 14 },
79         .id = -1,
80 };
81
82 /*! \brief Destructor for REFER Progress notification structure */
83 static void refer_progress_notification_destroy(void *obj)
84 {
85         struct refer_progress_notification *notification = obj;
86
87         ao2_cleanup(notification->progress);
88 }
89
90 /*! \brief Allocator for REFER Progress notification structure */
91 static struct refer_progress_notification *refer_progress_notification_alloc(struct refer_progress *progress, int response,
92         pjsip_evsub_state state)
93 {
94         struct refer_progress_notification *notification = ao2_alloc(sizeof(*notification), refer_progress_notification_destroy);
95
96         if (!notification) {
97                 return NULL;
98         }
99
100         ao2_ref(progress, +1);
101         notification->progress = progress;
102         notification->response = response;
103         notification->state = state;
104
105         return notification;
106 }
107
108 /*! \brief Serialized callback for subscription notification */
109 static int refer_progress_notify(void *data)
110 {
111         RAII_VAR(struct refer_progress_notification *, notification, data, ao2_cleanup);
112         pjsip_evsub *sub;
113         pjsip_tx_data *tdata;
114
115         /* If the subscription has already been terminated we can't send a notification */
116         if (!(sub = notification->progress->sub)) {
117                 ast_debug(3, "Not sending NOTIFY of response '%d' and state '%u' on progress monitor '%p' as subscription has been terminated\n",
118                         notification->response, notification->state, notification->progress);
119                 return 0;
120         }
121
122         /* If the subscription is being terminated we want to actually remove the progress structure here to
123          * stop a deadlock from occurring - basically terminated changes the state which queues a synchronous task
124          * but we are already running a task... thus it would deadlock */
125         if (notification->state == PJSIP_EVSUB_STATE_TERMINATED) {
126                 ast_debug(3, "Subscription '%p' is being terminated as a result of a NOTIFY, removing REFER progress structure early on progress monitor '%p'\n",
127                         notification->progress->sub, notification->progress);
128                 pjsip_dlg_inc_lock(notification->progress->dlg);
129                 pjsip_evsub_set_mod_data(notification->progress->sub, refer_progress_module.id, NULL);
130                 pjsip_dlg_dec_lock(notification->progress->dlg);
131
132                 /* This is for dropping the reference on the subscription */
133                 ao2_cleanup(notification->progress);
134
135                 notification->progress->sub = NULL;
136         }
137
138         /* Send a deferred initial 100 Trying SIP frag NOTIFY if we haven't already. */
139         if (!notification->progress->sent_100) {
140                 notification->progress->sent_100 = 1;
141                 if (notification->response != 100) {
142                         ast_debug(3, "Sending initial 100 Trying NOTIFY for progress monitor '%p'\n",
143                                 notification->progress);
144                         if (pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_ACTIVE, 100, NULL, &tdata) == PJ_SUCCESS) {
145                                 pjsip_xfer_send_request(sub, tdata);
146                         }
147                 }
148         }
149
150         ast_debug(3, "Sending NOTIFY with response '%d' and state '%u' on subscription '%p' and progress monitor '%p'\n",
151                 notification->response, notification->state, sub, notification->progress);
152
153         /* Actually send the notification */
154         if (pjsip_xfer_notify(sub, notification->state, notification->response, NULL, &tdata) == PJ_SUCCESS) {
155                 pjsip_xfer_send_request(sub, tdata);
156         }
157
158         return 0;
159 }
160
161 static void refer_progress_bridge(void *data, struct stasis_subscription *sub,
162                 struct stasis_message *message)
163 {
164         struct refer_progress *progress = data;
165         struct ast_bridge_blob *enter_blob;
166         struct refer_progress_notification *notification;
167         struct ast_channel *chan;
168
169         if (stasis_subscription_final_message(sub, message)) {
170                 ao2_ref(progress, -1);
171                 return;
172         }
173
174         if (ast_channel_entered_bridge_type() != stasis_message_type(message)) {
175                 /* Don't care */
176                 return;
177         }
178
179         enter_blob = stasis_message_data(message);
180         if (strcmp(enter_blob->channel->uniqueid, progress->transferee)) {
181                 /* Don't care */
182                 return;
183         }
184
185         if (!progress->transfer_data->completed) {
186                 /* We can't act on this message because the transfer_channel_data doesn't show that
187                  * the transfer is ready to progress */
188                 return;
189         }
190
191         /* OMG the transferee is joining a bridge. His call got answered! */
192         notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
193         if (notification) {
194                 if (ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
195                         ao2_cleanup(notification);
196                 }
197                 progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
198         }
199
200         chan = ast_channel_get_by_name(progress->transferee);
201         if (!chan) {
202                 /* The channel is already gone */
203                 return;
204         }
205
206         ast_channel_lock(chan);
207         ast_debug(3, "Detaching REFER progress monitoring hook from '%s' as it has joined a bridge\n",
208                 ast_channel_name(chan));
209         ast_framehook_detach(chan, progress->framehook);
210         ast_channel_unlock(chan);
211
212         ast_channel_unref(chan);
213 }
214
215 /*! \brief Progress monitoring frame hook - examines frames to determine state of transfer */
216 static struct ast_frame *refer_progress_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
217 {
218         struct refer_progress *progress = data;
219         struct refer_progress_notification *notification = NULL;
220
221         /* We only care about frames *to* the channel */
222         if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
223                 return f;
224         }
225
226         /* If the completed flag hasn't been raised, skip this pass. */
227         if (!progress->transfer_data->completed) {
228                 return f;
229         }
230
231         /* Determine the state of the REFER based on the control frames (or voice frames) passing */
232         if (f->frametype == AST_FRAME_VOICE && !progress->subclass) {
233                 /* Media is passing without progress, this means the call has been answered */
234                 notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
235         } else if (f->frametype == AST_FRAME_CONTROL) {
236                 /* Based on the control frame being written we can send a NOTIFY advising of the progress */
237                 if ((f->subclass.integer == AST_CONTROL_RING) || (f->subclass.integer == AST_CONTROL_RINGING)) {
238                         progress->subclass = f->subclass.integer;
239                         notification = refer_progress_notification_alloc(progress, 180, PJSIP_EVSUB_STATE_ACTIVE);
240                 } else if (f->subclass.integer == AST_CONTROL_BUSY) {
241                         progress->subclass = f->subclass.integer;
242                         notification = refer_progress_notification_alloc(progress, 486, PJSIP_EVSUB_STATE_TERMINATED);
243                 } else if (f->subclass.integer == AST_CONTROL_CONGESTION) {
244                         progress->subclass = f->subclass.integer;
245                         notification = refer_progress_notification_alloc(progress, 503, PJSIP_EVSUB_STATE_TERMINATED);
246                 } else if (f->subclass.integer == AST_CONTROL_PROGRESS) {
247                         progress->subclass = f->subclass.integer;
248                         notification = refer_progress_notification_alloc(progress, 183, PJSIP_EVSUB_STATE_ACTIVE);
249                 } else if (f->subclass.integer == AST_CONTROL_PROCEEDING) {
250                         progress->subclass = f->subclass.integer;
251                         notification = refer_progress_notification_alloc(progress, 100, PJSIP_EVSUB_STATE_ACTIVE);
252                 } else if (f->subclass.integer == AST_CONTROL_ANSWER) {
253                         progress->subclass = f->subclass.integer;
254                         notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
255                 }
256         }
257
258         /* If a notification is due to be sent push it to the thread pool */
259         if (notification) {
260                 /* If the subscription is being terminated we don't need the frame hook any longer */
261                 if (notification->state == PJSIP_EVSUB_STATE_TERMINATED) {
262                         ast_debug(3, "Detaching REFER progress monitoring hook from '%s' as subscription is being terminated\n",
263                                 ast_channel_name(chan));
264                         ast_framehook_detach(chan, progress->framehook);
265                 }
266
267                 if (ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
268                         ao2_cleanup(notification);
269                 }
270         }
271
272         return f;
273 }
274
275 /*! \brief Destroy callback for monitoring framehook */
276 static void refer_progress_framehook_destroy(void *data)
277 {
278         struct refer_progress *progress = data;
279         struct refer_progress_notification *notification = refer_progress_notification_alloc(progress, 503, PJSIP_EVSUB_STATE_TERMINATED);
280
281         if (notification && ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
282                 ao2_cleanup(notification);
283         }
284
285         if (progress->bridge_sub) {
286                 progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
287         }
288
289         ao2_cleanup(progress);
290 }
291
292 /*! \brief Serialized callback for subscription termination */
293 static int refer_progress_terminate(void *data)
294 {
295         struct refer_progress *progress = data;
296
297         /* The subscription is no longer valid */
298         progress->sub = NULL;
299
300         return 0;
301 }
302
303 /*! \brief Callback for REFER subscription state changes */
304 static void refer_progress_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)
305 {
306         struct refer_progress *progress = pjsip_evsub_get_mod_data(sub, refer_progress_module.id);
307
308         /* If being destroyed queue it up to the serializer */
309         if (progress && (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED)) {
310                 /* To prevent a deadlock race condition we unlock the dialog so other serialized tasks can execute */
311                 ast_debug(3, "Subscription '%p' has been remotely terminated, waiting for other tasks to complete on progress monitor '%p'\n",
312                         sub, progress);
313
314                 /* It's possible that a task is waiting to remove us already, so bump the refcount of progress so it doesn't get destroyed */
315                 ao2_ref(progress, +1);
316                 pjsip_dlg_dec_lock(progress->dlg);
317                 ast_sip_push_task_synchronous(progress->serializer, refer_progress_terminate, progress);
318                 pjsip_dlg_inc_lock(progress->dlg);
319                 ao2_ref(progress, -1);
320
321                 ast_debug(3, "Subscription '%p' removed from progress monitor '%p'\n", sub, progress);
322
323                 /* Since it was unlocked it is possible for this to have been removed already, so check again */
324                 if (pjsip_evsub_get_mod_data(sub, refer_progress_module.id)) {
325                         pjsip_evsub_set_mod_data(sub, refer_progress_module.id, NULL);
326                         ao2_cleanup(progress);
327                 }
328         }
329 }
330
331 /*! \brief Callback structure for subscription */
332 static pjsip_evsub_user refer_progress_evsub_cb = {
333         .on_evsub_state = refer_progress_on_evsub_state,
334 };
335
336 /*! \brief Destructor for REFER progress sutrcture */
337 static void refer_progress_destroy(void *obj)
338 {
339         struct refer_progress *progress = obj;
340
341         if (progress->bridge_sub) {
342                 progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
343         }
344
345         ao2_cleanup(progress->transfer_data);
346
347         ast_free(progress->transferee);
348         ast_taskprocessor_unreference(progress->serializer);
349 }
350
351 /*! \brief Internal helper function which sets up a refer progress structure if needed */
352 static int refer_progress_alloc(struct ast_sip_session *session, pjsip_rx_data *rdata, struct refer_progress **progress)
353 {
354         const pj_str_t str_refer_sub = { "Refer-Sub", 9 };
355         pjsip_generic_string_hdr *refer_sub = NULL;
356         const pj_str_t str_true = { "true", 4 };
357         pjsip_hdr hdr_list;
358         char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
359
360         *progress = NULL;
361
362         /* Grab the optional Refer-Sub header, it can be used to suppress the implicit subscription */
363         refer_sub = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_sub, NULL);
364         if ((refer_sub && pj_strnicmp(&refer_sub->hvalue, &str_true, 4))) {
365                 return 0;
366         }
367
368         if (!(*progress = ao2_alloc(sizeof(struct refer_progress), refer_progress_destroy))) {
369                 return -1;
370         }
371
372         ast_debug(3, "Created progress monitor '%p' for transfer occurring from channel '%s' and endpoint '%s'\n",
373                 progress, ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
374
375         (*progress)->framehook = -1;
376
377         /* To prevent a potential deadlock we need the dialog so we can lock/unlock */
378         (*progress)->dlg = session->inv_session->dlg;
379
380         /* Create name with seq number appended. */
381         ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/refer/%s",
382                 ast_sorcery_object_get_id(session->endpoint));
383
384         if (!((*progress)->serializer = ast_sip_create_serializer(tps_name))) {
385                 goto error;
386         }
387
388         /* Create the implicit subscription for monitoring of this transfer */
389         if (pjsip_xfer_create_uas(session->inv_session->dlg, &refer_progress_evsub_cb, rdata, &(*progress)->sub) != PJ_SUCCESS) {
390                 goto error;
391         }
392
393         /* Associate the REFER progress structure with the subscription */
394         ao2_ref(*progress, +1);
395         pjsip_evsub_set_mod_data((*progress)->sub, refer_progress_module.id, *progress);
396
397         pj_list_init(&hdr_list);
398         if (refer_sub) {
399                 pjsip_hdr *hdr = (pjsip_hdr*)pjsip_generic_string_hdr_create(session->inv_session->dlg->pool, &str_refer_sub, &str_true);
400
401                 pj_list_push_back(&hdr_list, hdr);
402         }
403
404         /* Accept the REFER request */
405         ast_debug(3, "Accepting REFER request for progress monitor '%p'\n", *progress);
406         pjsip_xfer_accept((*progress)->sub, rdata, 202, &hdr_list);
407
408         return 0;
409
410 error:
411         ao2_cleanup(*progress);
412         *progress = NULL;
413         return -1;
414 }
415
416 /*! \brief Structure for attended transfer task */
417 struct refer_attended {
418         /*! \brief Transferer session */
419         struct ast_sip_session *transferer;
420         /*! \brief Transferer channel */
421         struct ast_channel *transferer_chan;
422         /*! \brief Second transferer session */
423         struct ast_sip_session *transferer_second;
424         /*! \brief Optional refer progress structure */
425         struct refer_progress *progress;
426 };
427
428 /*! \brief Destructor for attended transfer task */
429 static void refer_attended_destroy(void *obj)
430 {
431         struct refer_attended *attended = obj;
432
433         ao2_cleanup(attended->transferer);
434         ast_channel_cleanup(attended->transferer_chan);
435         ao2_cleanup(attended->transferer_second);
436         ao2_cleanup(attended->progress);
437 }
438
439 /*! \brief Allocator for attended transfer task */
440 static struct refer_attended *refer_attended_alloc(struct ast_sip_session *transferer,
441         struct ast_sip_session *transferer_second,
442         struct refer_progress *progress)
443 {
444         struct refer_attended *attended;
445
446         attended = ao2_alloc_options(sizeof(*attended), refer_attended_destroy,
447                 AO2_ALLOC_OPT_LOCK_NOLOCK);
448         if (!attended) {
449                 return NULL;
450         }
451
452         ao2_ref(transferer, +1);
453         attended->transferer = transferer;
454         ast_channel_ref(transferer->channel);
455         attended->transferer_chan = transferer->channel;
456         ao2_ref(transferer_second, +1);
457         attended->transferer_second = transferer_second;
458
459         if (progress) {
460                 ao2_ref(progress, +1);
461                 attended->progress = progress;
462         }
463
464         return attended;
465 }
466
467 static int defer_termination_cancel(void *data)
468 {
469         struct ast_sip_session *session = data;
470
471         ast_sip_session_defer_termination_cancel(session);
472         ao2_ref(session, -1);
473         return 0;
474 }
475
476 /*!
477  * \internal
478  * \brief Convert transfer enum to SIP response code.
479  * \since 13.3.0
480  *
481  * \param xfer_code Core transfer function enum result.
482  *
483  * \return SIP response code
484  */
485 static int xfer_response_code2sip(enum ast_transfer_result xfer_code)
486 {
487         int response;
488
489         response = 503;
490         switch (xfer_code) {
491         case AST_BRIDGE_TRANSFER_INVALID:
492                 response = 400;
493                 break;
494         case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
495                 response = 403;
496                 break;
497         case AST_BRIDGE_TRANSFER_FAIL:
498                 response = 500;
499                 break;
500         case AST_BRIDGE_TRANSFER_SUCCESS:
501                 response = 200;
502                 break;
503         }
504         return response;
505 }
506
507 /*! \brief Task for attended transfer executed by attended->transferer_second serializer */
508 static int refer_attended_task(void *data)
509 {
510         struct refer_attended *attended = data;
511         int response;
512
513         if (attended->transferer_second->channel) {
514                 ast_debug(3, "Performing a REFER attended transfer - Transferer #1: %s Transferer #2: %s\n",
515                         ast_channel_name(attended->transferer_chan),
516                         ast_channel_name(attended->transferer_second->channel));
517
518                 response = xfer_response_code2sip(ast_bridge_transfer_attended(
519                         attended->transferer_chan,
520                         attended->transferer_second->channel));
521
522                 ast_debug(3, "Final response for REFER attended transfer - Transferer #1: %s Transferer #2: %s is '%d'\n",
523                         ast_channel_name(attended->transferer_chan),
524                         ast_channel_name(attended->transferer_second->channel),
525                         response);
526         } else {
527                 ast_debug(3, "Received REFER request on channel '%s' but other channel has gone.\n",
528                         ast_channel_name(attended->transferer_chan));
529                 response = 603;
530         }
531
532         if (attended->progress) {
533                 struct refer_progress_notification *notification;
534
535                 notification = refer_progress_notification_alloc(attended->progress, response,
536                         PJSIP_EVSUB_STATE_TERMINATED);
537                 if (notification) {
538                         refer_progress_notify(notification);
539                 }
540         }
541
542         if (response != 200) {
543                 if (!ast_sip_push_task(attended->transferer->serializer,
544                         defer_termination_cancel, attended->transferer)) {
545                         /* Gave the ref to the pushed task. */
546                         attended->transferer = NULL;
547                 }
548         }
549
550         ao2_ref(attended, -1);
551         return 0;
552 }
553
554 /*! \brief Structure for blind transfer callback details */
555 struct refer_blind {
556         /*! \brief Context being used for transfer */
557         const char *context;
558         /*! \brief Optional progress structure */
559         struct refer_progress *progress;
560         /*! \brief REFER message */
561         pjsip_rx_data *rdata;
562         /*! \brief Optional Replaces header */
563         pjsip_replaces_hdr *replaces;
564         /*! \brief Optional Refer-To header */
565         pjsip_sip_uri *refer_to;
566 };
567
568 /*! \brief Blind transfer callback function */
569 static void refer_blind_callback(struct ast_channel *chan, struct transfer_channel_data *user_data_wrapper,
570         enum ast_transfer_type transfer_type)
571 {
572         struct refer_blind *refer = user_data_wrapper->data;
573         pjsip_generic_string_hdr *referred_by;
574
575         static const pj_str_t str_referred_by = { "Referred-By", 11 };
576
577         pbx_builtin_setvar_helper(chan, "SIPTRANSFER", "yes");
578
579         /* If progress monitoring is being done attach a frame hook so we can monitor it */
580         if (refer->progress) {
581                 struct ast_framehook_interface hook = {
582                         .version = AST_FRAMEHOOK_INTERFACE_VERSION,
583                         .event_cb = refer_progress_framehook,
584                         .destroy_cb = refer_progress_framehook_destroy,
585                         .data = refer->progress,
586                         .disable_inheritance = 1,
587                 };
588
589                 refer->progress->transferee = ast_strdup(ast_channel_uniqueid(chan));
590                 if (!refer->progress->transferee) {
591                         struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
592                                 PJSIP_EVSUB_STATE_TERMINATED);
593
594                         ast_log(LOG_WARNING, "Could not copy channel name '%s' during transfer - assuming success\n",
595                                 ast_channel_name(chan));
596
597                         if (notification) {
598                                 refer_progress_notify(notification);
599                         }
600                 }
601
602                 /* Progress needs a reference to the transfer_channel_data so that it can track the completed status of the transfer */
603                 ao2_ref(user_data_wrapper, +1);
604                 refer->progress->transfer_data = user_data_wrapper;
605
606                 /* We need to bump the reference count up on the progress structure since it is in the frame hook now */
607                 ao2_ref(refer->progress, +1);
608
609                 /* If we can't attach a frame hook for whatever reason send a notification of success immediately */
610                 if ((refer->progress->framehook = ast_framehook_attach(chan, &hook)) < 0) {
611                         struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
612                                 PJSIP_EVSUB_STATE_TERMINATED);
613
614                         ast_log(LOG_WARNING, "Could not attach REFER transfer progress monitoring hook to channel '%s' - assuming success\n",
615                                 ast_channel_name(chan));
616
617                         if (notification) {
618                                 refer_progress_notify(notification);
619                         }
620
621                         ao2_cleanup(refer->progress);
622                 }
623
624                 /* We need to bump the reference count for the stasis subscription */
625                 ao2_ref(refer->progress, +1);
626                 /* We also will need to detect if the transferee enters a bridge. This is currently the only reliable way to
627                  * detect if the transfer target has answered the call
628                  */
629                 refer->progress->bridge_sub = stasis_subscribe_pool(ast_bridge_topic_all(), refer_progress_bridge, refer->progress);
630                 if (!refer->progress->bridge_sub) {
631                         struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
632                                 PJSIP_EVSUB_STATE_TERMINATED);
633
634                         ast_log(LOG_WARNING, "Could not create bridge stasis subscription for monitoring progress on transfer of channel '%s' - assuming success\n",
635                                         ast_channel_name(chan));
636
637                         if (notification) {
638                                 refer_progress_notify(notification);
639                         }
640
641                         ast_framehook_detach(chan, refer->progress->framehook);
642
643                         ao2_cleanup(refer->progress);
644                 }
645         }
646
647         pbx_builtin_setvar_helper(chan, "SIPREFERRINGCONTEXT", S_OR(refer->context, NULL));
648
649         referred_by = pjsip_msg_find_hdr_by_name(refer->rdata->msg_info.msg,
650                 &str_referred_by, NULL);
651         if (referred_by) {
652                 size_t uri_size = pj_strlen(&referred_by->hvalue) + 1;
653                 char *uri = ast_alloca(uri_size);
654
655                 ast_copy_pj_str(uri, &referred_by->hvalue, uri_size);
656                 pbx_builtin_setvar_helper(chan, "__SIPREFERREDBYHDR", S_OR(uri, NULL));
657         } else {
658                 pbx_builtin_setvar_helper(chan, "SIPREFERREDBYHDR", NULL);
659         }
660
661         if (refer->replaces) {
662                 char replaces[512];
663                 char *replaces_val = NULL;
664                 int len;
665
666                 len = pjsip_hdr_print_on(refer->replaces, replaces, sizeof(replaces) - 1);
667                 if (len != -1) {
668                         /* pjsip_hdr_print_on does not NULL terminate the buffer */
669                         replaces[len] = '\0';
670                         replaces_val = replaces + sizeof("Replaces:");
671                 }
672                 pbx_builtin_setvar_helper(chan, "__SIPREPLACESHDR", replaces_val);
673         } else {
674                 pbx_builtin_setvar_helper(chan, "SIPREPLACESHDR", NULL);
675         }
676
677         if (refer->refer_to) {
678                 char refer_to[PJSIP_MAX_URL_SIZE];
679
680                 pjsip_uri_print(PJSIP_URI_IN_REQ_URI, refer->refer_to, refer_to, sizeof(refer_to));
681                 pbx_builtin_setvar_helper(chan, "SIPREFERTOHDR", S_OR(refer_to, NULL));
682         } else {
683                 pbx_builtin_setvar_helper(chan, "SIPREFERTOHDR", NULL);
684         }
685 }
686
687 /*!
688  * \internal
689  * \brief Set the passed in context variable to the determined transfer context.
690  * \since 13.3.0
691  *
692  * \param context Set to the determined transfer context.
693  * \param session INVITE dialog SIP session.
694  */
695 #define DETERMINE_TRANSFER_CONTEXT(context, session)                                                                    \
696         do {                                                                                                                                                            \
697                 ast_channel_lock((session)->channel);                                                                                   \
698                 context = pbx_builtin_getvar_helper((session)->channel, "TRANSFER_CONTEXT");    \
699                 if (ast_strlen_zero(context)) {                                                                                                 \
700                         context = (session)->endpoint->context;                                                                         \
701                 } else {                                                                                                                                                \
702                         context = ast_strdupa(context);                                                                                         \
703                 }                                                                                                                                                               \
704                 ast_channel_unlock((session)->channel);                                                                                 \
705         } while (0)                                                                                                                                                     \
706
707 static int refer_incoming_attended_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_sip_uri *target_uri,
708         pjsip_param *replaces_param, struct refer_progress *progress)
709 {
710         const pj_str_t str_replaces = { "Replaces", 8 };
711         pj_str_t replaces_content;
712         pjsip_replaces_hdr *replaces;
713         int parsed_len;
714         pjsip_dialog *dlg;
715
716         pj_strdup_with_null(rdata->tp_info.pool, &replaces_content, &replaces_param->value);
717
718         /* Parsing the parameter as a Replaces header easily grabs the needed information */
719         if (!(replaces = pjsip_parse_hdr(rdata->tp_info.pool, &str_replaces, replaces_content.ptr,
720                 pj_strlen(&replaces_content), &parsed_len))) {
721                 ast_log(LOG_ERROR, "Received REFER request on channel '%s' from endpoint '%s' with invalid Replaces header, rejecting\n",
722                         ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
723                 return 400;
724         }
725
726         /* See if the dialog is local, or remote */
727         if ((dlg = pjsip_ua_find_dialog(&replaces->call_id, &replaces->to_tag, &replaces->from_tag, PJ_TRUE))) {
728                 RAII_VAR(struct ast_sip_session *, other_session, ast_sip_dialog_get_session(dlg), ao2_cleanup);
729                 struct refer_attended *attended;
730
731                 pjsip_dlg_dec_lock(dlg);
732
733                 if (!other_session) {
734                         ast_debug(3, "Received REFER request on channel '%s' from endpoint '%s' for local dialog but no session exists on it\n",
735                                 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
736                         return 603;
737                 }
738
739                 /* We defer actually doing the attended transfer to the other session so no deadlock can occur */
740                 if (!(attended = refer_attended_alloc(session, other_session, progress))) {
741                         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",
742                                 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
743                         return 500;
744                 }
745
746                 if (ast_sip_session_defer_termination(session)) {
747                         ast_log(LOG_ERROR, "Received REFER request on channel '%s' from endpoint '%s' for local dialog but could not defer termination, rejecting\n",
748                                 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
749                         ao2_cleanup(attended);
750                         return 500;
751                 }
752
753                 /* Push it to the other session, which will have both channels with minimal locking */
754                 if (ast_sip_push_task(other_session->serializer, refer_attended_task, attended)) {
755                         ast_sip_session_defer_termination_cancel(session);
756                         ao2_cleanup(attended);
757                         return 500;
758                 }
759
760                 ast_debug(3, "Attended transfer from '%s' pushed to second channel serializer\n",
761                         ast_channel_name(session->channel));
762
763                 return 200;
764         } else {
765                 const char *context;
766                 struct refer_blind refer = { 0, };
767                 int response;
768
769                 DETERMINE_TRANSFER_CONTEXT(context, session);
770
771                 if (!ast_exists_extension(NULL, context, "external_replaces", 1, NULL)) {
772                         ast_log(LOG_ERROR, "Received REFER for remote session on channel '%s' from endpoint '%s' but 'external_replaces' extension not found in context %s\n",
773                                 ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), context);
774                         return 404;
775                 }
776
777                 refer.context = context;
778                 refer.progress = progress;
779                 refer.rdata = rdata;
780                 refer.replaces = replaces;
781                 refer.refer_to = target_uri;
782
783                 if (ast_sip_session_defer_termination(session)) {
784                         ast_log(LOG_ERROR, "Received REFER for remote session on channel '%s' from endpoint '%s' but could not defer termination, rejecting\n",
785                                 ast_channel_name(session->channel),
786                                 ast_sorcery_object_get_id(session->endpoint));
787                         return 500;
788                 }
789
790                 response = xfer_response_code2sip(ast_bridge_transfer_blind(1, session->channel,
791                         "external_replaces", context, refer_blind_callback, &refer));
792                 if (response != 200) {
793                         ast_sip_session_defer_termination_cancel(session);
794                 }
795                 return response;
796         }
797 }
798
799 static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_sip_uri *target,
800         struct refer_progress *progress)
801 {
802         const char *context;
803         char exten[AST_MAX_EXTENSION];
804         struct refer_blind refer = { 0, };
805         int response;
806
807         /* If no explicit transfer context has been provided use their configured context */
808         DETERMINE_TRANSFER_CONTEXT(context, session);
809
810         /* Using the user portion of the target URI see if it exists as a valid extension in their context */
811         ast_copy_pj_str(exten, &target->user, sizeof(exten));
812         if (!ast_exists_extension(NULL, context, exten, 1, NULL)) {
813                 ast_log(LOG_ERROR, "Channel '%s' from endpoint '%s' attempted blind transfer to '%s@%s' but target does not exist\n",
814                         ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), exten, context);
815                 return 404;
816         }
817
818         refer.context = context;
819         refer.progress = progress;
820         refer.rdata = rdata;
821         refer.refer_to = target;
822
823         if (ast_sip_session_defer_termination(session)) {
824                 ast_log(LOG_ERROR, "Channel '%s' from endpoint '%s' attempted blind transfer but could not defer termination, rejecting\n",
825                         ast_channel_name(session->channel),
826                         ast_sorcery_object_get_id(session->endpoint));
827                 return 500;
828         }
829
830         response = xfer_response_code2sip(ast_bridge_transfer_blind(1, session->channel,
831                 exten, context, refer_blind_callback, &refer));
832         if (response != 200) {
833                 ast_sip_session_defer_termination_cancel(session);
834         }
835         return response;
836 }
837
838 /*! \brief Structure used to retrieve channel from another session */
839 struct invite_replaces {
840         /*! \brief Session we want the channel from */
841         struct ast_sip_session *session;
842         /*! \brief Channel from the session (with reference) */
843         struct ast_channel *channel;
844         /*! \brief Bridge the channel is in */
845         struct ast_bridge *bridge;
846 };
847
848 /*! \brief Task for invite replaces */
849 static int invite_replaces(void *data)
850 {
851         struct invite_replaces *invite = data;
852
853         if (!invite->session->channel) {
854                 return -1;
855         }
856
857         ast_channel_ref(invite->session->channel);
858         invite->channel = invite->session->channel;
859
860         ast_channel_lock(invite->channel);
861         invite->bridge = ast_channel_get_bridge(invite->channel);
862         ast_channel_unlock(invite->channel);
863
864         return 0;
865 }
866
867 static int refer_incoming_invite_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
868 {
869         pjsip_dialog *other_dlg = NULL;
870         pjsip_tx_data *packet;
871         int response = 0;
872         RAII_VAR(struct ast_sip_session *, other_session, NULL, ao2_cleanup);
873         struct invite_replaces invite;
874
875         /* If a Replaces header is present make sure it is valid */
876         if (pjsip_replaces_verify_request(rdata, &other_dlg, PJ_TRUE, &packet) != PJ_SUCCESS) {
877                 response = packet->msg->line.status.code;
878                 ast_assert(response != 0);
879                 pjsip_tx_data_dec_ref(packet);
880                 goto inv_replace_failed;
881         }
882
883         /* If no other dialog exists then this INVITE request does not have a Replaces header */
884         if (!other_dlg) {
885                 return 0;
886         }
887
888         other_session = ast_sip_dialog_get_session(other_dlg);
889         pjsip_dlg_dec_lock(other_dlg);
890
891         /* Don't accept an in-dialog INVITE with Replaces as it does not make much sense */
892         if (session->inv_session->dlg->state == PJSIP_DIALOG_STATE_ESTABLISHED) {
893                 response = 488;
894                 goto inv_replace_failed;
895         }
896
897         if (!other_session) {
898                 ast_debug(3, "INVITE with Replaces received on channel '%s' from endpoint '%s', but requested session does not exist\n",
899                         ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
900                 response = 481;
901                 goto inv_replace_failed;
902         }
903
904         invite.session = other_session;
905
906         if (ast_sip_push_task_synchronous(other_session->serializer, invite_replaces, &invite)) {
907                 response = 481;
908                 goto inv_replace_failed;
909         }
910
911         ast_channel_lock(session->channel);
912         ast_setstate(session->channel, AST_STATE_RING);
913         ast_channel_unlock(session->channel);
914         ast_raw_answer(session->channel);
915
916         ast_debug(3, "INVITE with Replaces being attempted.  '%s' --> '%s'\n",
917                 ast_channel_name(session->channel), ast_channel_name(invite.channel));
918
919         if (!invite.bridge) {
920                 struct ast_channel *chan = session->channel;
921
922                 /*
923                  * This will use a synchronous task but we aren't operating in
924                  * the serializer at this point in time, so it won't deadlock.
925                  */
926                 if (!ast_channel_move(invite.channel, chan)) {
927                         /*
928                          * We can't directly use session->channel because ast_channel_move()
929                          * does a masquerade which changes session->channel to a different
930                          * channel.  To ensure we work on the right channel we store a
931                          * pointer locally before we begin so it remains valid.
932                          */
933                         ast_hangup(chan);
934                 } else {
935                         response = AST_CAUSE_FAILURE;
936                 }
937         } else {
938                 if (ast_bridge_impart(invite.bridge, session->channel, invite.channel, NULL,
939                         AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
940                         response = AST_CAUSE_FAILURE;
941                 }
942         }
943
944         ast_channel_unref(invite.channel);
945         ao2_cleanup(invite.bridge);
946
947         if (!response) {
948                 /*
949                  * On success we cannot use session->channel in the debug message.
950                  * This thread either no longer has a ref to session->channel or
951                  * session->channel is no longer the original channel.
952                  */
953                 ast_debug(3, "INVITE with Replaces successfully completed.\n");
954         } else {
955                 ast_debug(3, "INVITE with Replaces failed on channel '%s', hanging up with cause '%d'\n",
956                         ast_channel_name(session->channel), response);
957                 ast_channel_lock(session->channel);
958                 ast_channel_hangupcause_set(session->channel, response);
959                 ast_channel_unlock(session->channel);
960                 ast_hangup(session->channel);
961         }
962
963         return 1;
964
965 inv_replace_failed:
966         if (session->inv_session->dlg->state != PJSIP_DIALOG_STATE_ESTABLISHED) {
967                 ast_debug(3, "INVITE with Replaces failed on channel '%s', sending response of '%d'\n",
968                         ast_channel_name(session->channel), response);
969                 session->defer_terminate = 1;
970                 ast_hangup(session->channel);
971
972                 if (pjsip_inv_end_session(session->inv_session, response, NULL, &packet) == PJ_SUCCESS) {
973                         ast_sip_session_send_response(session, packet);
974                 }
975         } else {
976                 ast_debug(3, "INVITE with Replaces in-dialog on channel '%s', hanging up\n",
977                         ast_channel_name(session->channel));
978                 ast_queue_hangup(session->channel);
979         }
980
981         return 1;
982 }
983
984 static int refer_incoming_refer_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
985 {
986         pjsip_generic_string_hdr *refer_to;
987         char *uri;
988         size_t uri_size;
989         pjsip_uri *target;
990         pjsip_sip_uri *target_uri;
991         RAII_VAR(struct refer_progress *, progress, NULL, ao2_cleanup);
992         pjsip_param *replaces;
993         int response;
994
995         static const pj_str_t str_refer_to = { "Refer-To", 8 };
996         static const pj_str_t str_replaces = { "Replaces", 8 };
997
998         if (!session->channel) {
999                 /* No channel to refer.  Likely because the call was just hung up. */
1000                 pjsip_dlg_respond(session->inv_session->dlg, rdata, 404, NULL, NULL, NULL);
1001                 ast_debug(3, "Received a REFER on a session with no channel from endpoint '%s'.\n",
1002                         ast_sorcery_object_get_id(session->endpoint));
1003                 return 0;
1004         }
1005
1006         if (!session->endpoint->allowtransfer) {
1007                 pjsip_dlg_respond(session->inv_session->dlg, rdata, 603, NULL, NULL, NULL);
1008                 ast_log(LOG_WARNING, "Endpoint %s transfer attempt blocked due to configuration\n",
1009                                 ast_sorcery_object_get_id(session->endpoint));
1010                 return 0;
1011         }
1012
1013         /* A Refer-To header is required */
1014         refer_to = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL);
1015         if (!refer_to) {
1016                 pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
1017                 ast_debug(3, "Received a REFER without Refer-To on channel '%s' from endpoint '%s'\n",
1018                         ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
1019                 return 0;
1020         }
1021
1022         /* The ast_copy_pj_str to uri is needed because it puts the NULL terminator to the uri
1023          * as pjsip_parse_uri require a NULL terminated uri
1024          */
1025
1026         uri_size = pj_strlen(&refer_to->hvalue) + 1;
1027         uri = ast_alloca(uri_size);
1028         ast_copy_pj_str(uri, &refer_to->hvalue, uri_size);
1029
1030         target = pjsip_parse_uri(rdata->tp_info.pool, uri, uri_size - 1, 0);
1031
1032         if (!target
1033                 || (!PJSIP_URI_SCHEME_IS_SIP(target)
1034                         && !PJSIP_URI_SCHEME_IS_SIPS(target))) {
1035
1036                 pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
1037                 ast_debug(3, "Received a REFER without a parseable Refer-To ('%s') on channel '%s' from endpoint '%s'\n",
1038                         uri, ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
1039                 return 0;
1040         }
1041         target_uri = pjsip_uri_get_uri(target);
1042
1043         /* Set up REFER progress subscription if requested/possible */
1044         if (refer_progress_alloc(session, rdata, &progress)) {
1045                 pjsip_dlg_respond(session->inv_session->dlg, rdata, 500, NULL, NULL, NULL);
1046                 ast_debug(3, "Could not set up subscription for REFER on channel '%s' from endpoint '%s'\n",
1047                         ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
1048                 return 0;
1049         }
1050
1051         /* Determine if this is an attended or blind transfer */
1052         if ((replaces = pjsip_param_find(&target_uri->header_param, &str_replaces)) ||
1053                 (replaces = pjsip_param_find(&target_uri->other_param, &str_replaces))) {
1054                 response = refer_incoming_attended_request(session, rdata, target_uri, replaces, progress);
1055         } else {
1056                 response = refer_incoming_blind_request(session, rdata, target_uri, progress);
1057         }
1058
1059         if (!progress) {
1060                 /* The transferer has requested no subscription, so send a final response immediately */
1061                 pjsip_tx_data *tdata;
1062                 const pj_str_t str_refer_sub = { "Refer-Sub", 9 };
1063                 const pj_str_t str_false = { "false", 5 };
1064                 pjsip_hdr *hdr;
1065
1066                 ast_debug(3, "Progress monitoring not requested for REFER on channel '%s' from endpoint '%s', sending immediate response of '%d'\n",
1067                         ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), response);
1068
1069                 if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, response, NULL, &tdata) != PJ_SUCCESS) {
1070                         pjsip_dlg_respond(session->inv_session->dlg, rdata, response, NULL, NULL, NULL);
1071                         return 0;
1072                 }
1073
1074                 hdr = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool, &str_refer_sub, &str_false);
1075                 pjsip_msg_add_hdr(tdata->msg, hdr);
1076
1077                 pjsip_dlg_send_response(session->inv_session->dlg, pjsip_rdata_get_tsx(rdata), tdata);
1078         } else if (response != 200) {
1079                 /* Since this failed we can send a final NOTIFY now and terminate the subscription */
1080                 struct refer_progress_notification *notification = refer_progress_notification_alloc(progress, response, PJSIP_EVSUB_STATE_TERMINATED);
1081
1082                 if (notification) {
1083                         /* The refer_progress_notify function will call ao2_cleanup on this for us */
1084                         refer_progress_notify(notification);
1085                 }
1086         }
1087
1088         return 0;
1089 }
1090
1091 static int refer_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
1092 {
1093         if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_refer_method())) {
1094                 return refer_incoming_refer_request(session, rdata);
1095         } else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_invite_method)) {
1096                 return refer_incoming_invite_request(session, rdata);
1097         } else {
1098                 return 0;
1099         }
1100 }
1101
1102 /*!
1103  * \brief Use the value of a channel variable as the value of a SIP header
1104  *
1105  * This looks up a variable name on a channel, then takes that value and adds
1106  * it to an outgoing SIP request. If the header already exists on the message,
1107  * then no action is taken.
1108  *
1109  * \pre chan is locked.
1110  *
1111  * \param chan The channel on which to find the variable.
1112  * \param var_name The name of the channel variable to use.
1113  * \param header_name The name of the SIP header to add to the outgoing message.
1114  * \param tdata The outgoing SIP message on which to add the header
1115  */
1116 static void add_header_from_channel_var(struct ast_channel *chan, const char *var_name, const char *header_name, pjsip_tx_data *tdata)
1117 {
1118         const char *var_value;
1119         pj_str_t pj_header_name;
1120         pjsip_hdr *header;
1121
1122         var_value = pbx_builtin_getvar_helper(chan, var_name);
1123         if (ast_strlen_zero(var_value)) {
1124                 return;
1125         }
1126
1127         pj_cstr(&pj_header_name, header_name);
1128         header = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_header_name, NULL);
1129         if (header) {
1130                 return;
1131         }
1132         ast_sip_add_header(tdata, header_name, var_value);
1133 }
1134
1135 static void refer_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
1136 {
1137         if (pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_invite_method)
1138                 || !session->channel
1139                 || session->inv_session->state != PJSIP_INV_STATE_NULL) {
1140                 return;
1141         }
1142
1143         ast_channel_lock(session->channel);
1144         add_header_from_channel_var(session->channel, "SIPREPLACESHDR", "Replaces", tdata);
1145         add_header_from_channel_var(session->channel, "SIPREFERREDBYHDR", "Referred-By", tdata);
1146         ast_channel_unlock(session->channel);
1147 }
1148
1149 static struct ast_sip_session_supplement refer_supplement = {
1150         .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 1,
1151         .incoming_request = refer_incoming_request,
1152         .outgoing_request = refer_outgoing_request,
1153 };
1154
1155 static int load_module(void)
1156 {
1157         const pj_str_t str_norefersub = { "norefersub", 10 };
1158
1159         CHECK_PJSIP_SESSION_MODULE_LOADED();
1160
1161         pjsip_replaces_init_module(ast_sip_get_pjsip_endpoint());
1162         pjsip_xfer_init_module(ast_sip_get_pjsip_endpoint());
1163         pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), NULL, PJSIP_H_SUPPORTED, NULL, 1, &str_norefersub);
1164
1165         ast_sip_register_service(&refer_progress_module);
1166         ast_sip_session_register_supplement(&refer_supplement);
1167
1168         return AST_MODULE_LOAD_SUCCESS;
1169 }
1170
1171 static int unload_module(void)
1172 {
1173         ast_sip_session_unregister_supplement(&refer_supplement);
1174         ast_sip_unregister_service(&refer_progress_module);
1175
1176         return 0;
1177 }
1178
1179 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Blind and Attended Transfer Support",
1180         .support_level = AST_MODULE_SUPPORT_CORE,
1181         .load = load_module,
1182         .unload = unload_module,
1183         .load_pri = AST_MODPRI_APP_DEPEND,
1184 );