res_pjsip_refer.c 43 KB

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