res_pjsip_refer.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. /*! \brief REFER Progress structure */
  37. struct refer_progress {
  38. /*! \brief Subscription to provide updates on */
  39. pjsip_evsub *sub;
  40. /*! \brief Dialog for subscription */
  41. pjsip_dialog *dlg;
  42. /*! \brief Received packet, used to construct final response in case no subscription exists */
  43. pjsip_rx_data *rdata;
  44. /*! \brief Frame hook for monitoring REFER progress */
  45. int framehook;
  46. /*! \brief Last received subclass in frame hook */
  47. int subclass;
  48. /*! \brief Serializer for notifications */
  49. struct ast_taskprocessor *serializer;
  50. /*! \brief Stasis subscription for bridge events */
  51. struct stasis_subscription *bridge_sub;
  52. /*! \brief Reference to transfer_channel_data related to the refer */
  53. struct transfer_channel_data *transfer_data;
  54. /*! \brief Uniqueid of transferee channel */
  55. char *transferee;
  56. };
  57. /*! \brief REFER Progress notification structure */
  58. struct refer_progress_notification {
  59. /*! \brief Refer progress structure to send notification on */
  60. struct refer_progress *progress;
  61. /*! \brief SIP response code to send */
  62. int response;
  63. /*! \brief Subscription state */
  64. pjsip_evsub_state state;
  65. };
  66. /*! \brief REFER Progress module, used to attach REFER progress structure to subscriptions */
  67. static pjsip_module refer_progress_module = {
  68. .name = { "REFER Progress", 14 },
  69. .id = -1,
  70. };
  71. /*! \brief Destructor for REFER Progress notification structure */
  72. static void refer_progress_notification_destroy(void *obj)
  73. {
  74. struct refer_progress_notification *notification = obj;
  75. ao2_cleanup(notification->progress);
  76. }
  77. /*! \brief Allocator for REFER Progress notification structure */
  78. static struct refer_progress_notification *refer_progress_notification_alloc(struct refer_progress *progress, int response,
  79. pjsip_evsub_state state)
  80. {
  81. struct refer_progress_notification *notification = ao2_alloc(sizeof(*notification), refer_progress_notification_destroy);
  82. if (!notification) {
  83. return NULL;
  84. }
  85. ao2_ref(progress, +1);
  86. notification->progress = progress;
  87. notification->response = response;
  88. notification->state = state;
  89. return notification;
  90. }
  91. /*! \brief Serialized callback for subscription notification */
  92. static int refer_progress_notify(void *data)
  93. {
  94. RAII_VAR(struct refer_progress_notification *, notification, data, ao2_cleanup);
  95. pjsip_evsub *sub;
  96. pjsip_tx_data *tdata;
  97. /* If the subscription has already been terminated we can't send a notification */
  98. if (!(sub = notification->progress->sub)) {
  99. ast_debug(3, "Not sending NOTIFY of response '%d' and state '%u' on progress monitor '%p' as subscription has been terminated\n",
  100. notification->response, notification->state, notification->progress);
  101. return 0;
  102. }
  103. /* If the subscription is being terminated we want to actually remove the progress structure here to
  104. * stop a deadlock from occurring - basically terminated changes the state which queues a synchronous task
  105. * but we are already running a task... thus it would deadlock */
  106. if (notification->state == PJSIP_EVSUB_STATE_TERMINATED) {
  107. ast_debug(3, "Subscription '%p' is being terminated as a result of a NOTIFY, removing REFER progress structure early on progress monitor '%p'\n",
  108. notification->progress->sub, notification->progress);
  109. pjsip_dlg_inc_lock(notification->progress->dlg);
  110. pjsip_evsub_set_mod_data(notification->progress->sub, refer_progress_module.id, NULL);
  111. pjsip_dlg_dec_lock(notification->progress->dlg);
  112. /* This is for dropping the reference on the subscription */
  113. ao2_cleanup(notification->progress);
  114. notification->progress->sub = NULL;
  115. }
  116. ast_debug(3, "Sending NOTIFY with response '%d' and state '%u' on subscription '%p' and progress monitor '%p'\n",
  117. notification->response, notification->state, sub, notification->progress);
  118. /* Actually send the notification */
  119. if (pjsip_xfer_notify(sub, notification->state, notification->response, NULL, &tdata) == PJ_SUCCESS) {
  120. pjsip_xfer_send_request(sub, tdata);
  121. }
  122. return 0;
  123. }
  124. static void refer_progress_bridge(void *data, struct stasis_subscription *sub,
  125. struct stasis_message *message)
  126. {
  127. struct refer_progress *progress = data;
  128. struct ast_bridge_blob *enter_blob;
  129. struct refer_progress_notification *notification;
  130. struct ast_channel *chan;
  131. if (stasis_subscription_final_message(sub, message)) {
  132. ao2_ref(progress, -1);
  133. return;
  134. }
  135. if (ast_channel_entered_bridge_type() != stasis_message_type(message)) {
  136. /* Don't care */
  137. return;
  138. }
  139. enter_blob = stasis_message_data(message);
  140. if (strcmp(enter_blob->channel->uniqueid, progress->transferee)) {
  141. /* Don't care */
  142. return;
  143. }
  144. if (!progress->transfer_data->completed) {
  145. /* We can't act on this message because the transfer_channel_data doesn't show that
  146. * the transfer is ready to progress */
  147. return;
  148. }
  149. /* OMG the transferee is joining a bridge. His call got answered! */
  150. notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
  151. if (notification) {
  152. if (ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
  153. ao2_cleanup(notification);
  154. }
  155. progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
  156. }
  157. chan = ast_channel_get_by_name(progress->transferee);
  158. if (!chan) {
  159. /* The channel is already gone */
  160. return;
  161. }
  162. ast_channel_lock(chan);
  163. ast_debug(3, "Detaching REFER progress monitoring hook from '%s' as it has joined a bridge\n",
  164. ast_channel_name(chan));
  165. ast_framehook_detach(chan, progress->framehook);
  166. ast_channel_unlock(chan);
  167. ast_channel_unref(chan);
  168. }
  169. /*! \brief Progress monitoring frame hook - examines frames to determine state of transfer */
  170. static struct ast_frame *refer_progress_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
  171. {
  172. struct refer_progress *progress = data;
  173. struct refer_progress_notification *notification = NULL;
  174. /* We only care about frames *to* the channel */
  175. if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
  176. return f;
  177. }
  178. /* If the completed flag hasn't been raised, skip this pass. */
  179. if (!progress->transfer_data->completed) {
  180. return f;
  181. }
  182. /* Determine the state of the REFER based on the control frames (or voice frames) passing */
  183. if (f->frametype == AST_FRAME_VOICE && !progress->subclass) {
  184. /* Media is passing without progress, this means the call has been answered */
  185. notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
  186. } else if (f->frametype == AST_FRAME_CONTROL) {
  187. /* Based on the control frame being written we can send a NOTIFY advising of the progress */
  188. if ((f->subclass.integer == AST_CONTROL_RING) || (f->subclass.integer == AST_CONTROL_RINGING)) {
  189. progress->subclass = f->subclass.integer;
  190. notification = refer_progress_notification_alloc(progress, 180, PJSIP_EVSUB_STATE_ACTIVE);
  191. } else if (f->subclass.integer == AST_CONTROL_BUSY) {
  192. progress->subclass = f->subclass.integer;
  193. notification = refer_progress_notification_alloc(progress, 486, PJSIP_EVSUB_STATE_TERMINATED);
  194. } else if (f->subclass.integer == AST_CONTROL_CONGESTION) {
  195. progress->subclass = f->subclass.integer;
  196. notification = refer_progress_notification_alloc(progress, 503, PJSIP_EVSUB_STATE_TERMINATED);
  197. } else if (f->subclass.integer == AST_CONTROL_PROGRESS) {
  198. progress->subclass = f->subclass.integer;
  199. notification = refer_progress_notification_alloc(progress, 183, PJSIP_EVSUB_STATE_ACTIVE);
  200. } else if (f->subclass.integer == AST_CONTROL_PROCEEDING) {
  201. progress->subclass = f->subclass.integer;
  202. notification = refer_progress_notification_alloc(progress, 100, PJSIP_EVSUB_STATE_ACTIVE);
  203. } else if (f->subclass.integer == AST_CONTROL_ANSWER) {
  204. progress->subclass = f->subclass.integer;
  205. notification = refer_progress_notification_alloc(progress, 200, PJSIP_EVSUB_STATE_TERMINATED);
  206. }
  207. }
  208. /* If a notification is due to be sent push it to the thread pool */
  209. if (notification) {
  210. if (ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
  211. ao2_cleanup(notification);
  212. }
  213. /* If the subscription is being terminated we don't need the frame hook any longer */
  214. if (notification->state == PJSIP_EVSUB_STATE_TERMINATED) {
  215. ast_debug(3, "Detaching REFER progress monitoring hook from '%s' as subscription is being terminated\n",
  216. ast_channel_name(chan));
  217. ast_framehook_detach(chan, progress->framehook);
  218. }
  219. }
  220. return f;
  221. }
  222. /*! \brief Destroy callback for monitoring framehook */
  223. static void refer_progress_framehook_destroy(void *data)
  224. {
  225. struct refer_progress *progress = data;
  226. struct refer_progress_notification *notification = refer_progress_notification_alloc(progress, 503, PJSIP_EVSUB_STATE_TERMINATED);
  227. if (notification && ast_sip_push_task(progress->serializer, refer_progress_notify, notification)) {
  228. ao2_cleanup(notification);
  229. }
  230. if (progress->bridge_sub) {
  231. progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
  232. }
  233. ao2_cleanup(progress);
  234. }
  235. /*! \brief Serialized callback for subscription termination */
  236. static int refer_progress_terminate(void *data)
  237. {
  238. struct refer_progress *progress = data;
  239. /* The subscription is no longer valid */
  240. progress->sub = NULL;
  241. return 0;
  242. }
  243. /*! \brief Callback for REFER subscription state changes */
  244. static void refer_progress_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)
  245. {
  246. struct refer_progress *progress = pjsip_evsub_get_mod_data(sub, refer_progress_module.id);
  247. /* If being destroyed queue it up to the serializer */
  248. if (progress && (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED)) {
  249. /* To prevent a deadlock race condition we unlock the dialog so other serialized tasks can execute */
  250. ast_debug(3, "Subscription '%p' has been remotely terminated, waiting for other tasks to complete on progress monitor '%p'\n",
  251. sub, progress);
  252. /* It's possible that a task is waiting to remove us already, so bump the refcount of progress so it doesn't get destroyed */
  253. ao2_ref(progress, +1);
  254. pjsip_dlg_dec_lock(progress->dlg);
  255. ast_sip_push_task_synchronous(progress->serializer, refer_progress_terminate, progress);
  256. pjsip_dlg_inc_lock(progress->dlg);
  257. ao2_ref(progress, -1);
  258. ast_debug(3, "Subscription '%p' removed from progress monitor '%p'\n", sub, progress);
  259. /* Since it was unlocked it is possible for this to have been removed already, so check again */
  260. if (pjsip_evsub_get_mod_data(sub, refer_progress_module.id)) {
  261. pjsip_evsub_set_mod_data(sub, refer_progress_module.id, NULL);
  262. ao2_cleanup(progress);
  263. }
  264. }
  265. }
  266. /*! \brief Callback structure for subscription */
  267. static pjsip_evsub_user refer_progress_evsub_cb = {
  268. .on_evsub_state = refer_progress_on_evsub_state,
  269. };
  270. /*! \brief Destructor for REFER progress sutrcture */
  271. static void refer_progress_destroy(void *obj)
  272. {
  273. struct refer_progress *progress = obj;
  274. if (progress->bridge_sub) {
  275. progress->bridge_sub = stasis_unsubscribe(progress->bridge_sub);
  276. }
  277. ao2_cleanup(progress->transfer_data);
  278. ast_free(progress->transferee);
  279. ast_taskprocessor_unreference(progress->serializer);
  280. }
  281. /*! \brief Internal helper function which sets up a refer progress structure if needed */
  282. static int refer_progress_alloc(struct ast_sip_session *session, pjsip_rx_data *rdata, struct refer_progress **progress)
  283. {
  284. const pj_str_t str_refer_sub = { "Refer-Sub", 9 };
  285. pjsip_generic_string_hdr *refer_sub = NULL;
  286. const pj_str_t str_true = { "true", 4 };
  287. pjsip_tx_data *tdata;
  288. pjsip_hdr hdr_list;
  289. *progress = NULL;
  290. /* Grab the optional Refer-Sub header, it can be used to suppress the implicit subscription */
  291. refer_sub = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_sub, NULL);
  292. if ((refer_sub && pj_strnicmp(&refer_sub->hvalue, &str_true, 4))) {
  293. return 0;
  294. }
  295. if (!(*progress = ao2_alloc(sizeof(struct refer_progress), refer_progress_destroy))) {
  296. return -1;
  297. }
  298. ast_debug(3, "Created progress monitor '%p' for transfer occurring from channel '%s' and endpoint '%s'\n",
  299. progress, ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
  300. (*progress)->framehook = -1;
  301. /* To prevent a potential deadlock we need the dialog so we can lock/unlock */
  302. (*progress)->dlg = session->inv_session->dlg;
  303. if (!((*progress)->serializer = ast_sip_create_serializer())) {
  304. goto error;
  305. }
  306. /* Create the implicit subscription for monitoring of this transfer */
  307. if (pjsip_xfer_create_uas(session->inv_session->dlg, &refer_progress_evsub_cb, rdata, &(*progress)->sub) != PJ_SUCCESS) {
  308. goto error;
  309. }
  310. /* Associate the REFER progress structure with the subscription */
  311. ao2_ref(*progress, +1);
  312. pjsip_evsub_set_mod_data((*progress)->sub, refer_progress_module.id, *progress);
  313. pj_list_init(&hdr_list);
  314. if (refer_sub) {
  315. pjsip_hdr *hdr = (pjsip_hdr*)pjsip_generic_string_hdr_create(session->inv_session->dlg->pool, &str_refer_sub, &str_true);
  316. pj_list_push_back(&hdr_list, hdr);
  317. }
  318. /* Accept the REFER request */
  319. ast_debug(3, "Accepting REFER request for progress monitor '%p'\n", *progress);
  320. pjsip_xfer_accept((*progress)->sub, rdata, 202, &hdr_list);
  321. /* Send initial NOTIFY Request */
  322. ast_debug(3, "Sending initial 100 Trying NOTIFY for progress monitor '%p'\n", *progress);
  323. if (pjsip_xfer_notify((*progress)->sub, PJSIP_EVSUB_STATE_ACTIVE, 100, NULL, &tdata) == PJ_SUCCESS) {
  324. pjsip_xfer_send_request((*progress)->sub, tdata);
  325. }
  326. return 0;
  327. error:
  328. ao2_cleanup(*progress);
  329. *progress = NULL;
  330. return -1;
  331. }
  332. /*! \brief Structure for attended transfer task */
  333. struct refer_attended {
  334. /*! \brief Transferer session */
  335. struct ast_sip_session *transferer;
  336. /*! \brief Transferer channel */
  337. struct ast_channel *transferer_chan;
  338. /*! \brief Second transferer session */
  339. struct ast_sip_session *transferer_second ;
  340. /*! \brief Optional refer progress structure */
  341. struct refer_progress *progress;
  342. };
  343. /*! \brief Destructor for attended transfer task */
  344. static void refer_attended_destroy(void *obj)
  345. {
  346. struct refer_attended *attended = obj;
  347. ao2_cleanup(attended->transferer);
  348. ast_channel_unref(attended->transferer_chan);
  349. ao2_cleanup(attended->transferer_second);
  350. }
  351. /*! \brief Allocator for attended transfer task */
  352. static struct refer_attended *refer_attended_alloc(struct ast_sip_session *transferer, struct ast_sip_session *transferer_second,
  353. struct refer_progress *progress)
  354. {
  355. struct refer_attended *attended = ao2_alloc(sizeof(*attended), refer_attended_destroy);
  356. if (!attended) {
  357. return NULL;
  358. }
  359. ao2_ref(transferer, +1);
  360. attended->transferer = transferer;
  361. ast_channel_ref(transferer->channel);
  362. attended->transferer_chan = transferer->channel;
  363. ao2_ref(transferer_second, +1);
  364. attended->transferer_second = transferer_second;
  365. if (progress) {
  366. ao2_ref(progress, +1);
  367. attended->progress = progress;
  368. }
  369. return attended;
  370. }
  371. /*! \brief Task for attended transfer */
  372. static int refer_attended(void *data)
  373. {
  374. RAII_VAR(struct refer_attended *, attended, data, ao2_cleanup);
  375. int response = 0;
  376. if (!attended->transferer_second->channel) {
  377. return -1;
  378. }
  379. ast_debug(3, "Performing a REFER attended transfer - Transferer #1: %s Transferer #2: %s\n",
  380. ast_channel_name(attended->transferer_chan), ast_channel_name(attended->transferer_second->channel));
  381. switch (ast_bridge_transfer_attended(attended->transferer_chan, attended->transferer_second->channel)) {
  382. case AST_BRIDGE_TRANSFER_INVALID:
  383. response = 400;
  384. break;
  385. case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
  386. response = 403;
  387. break;
  388. case AST_BRIDGE_TRANSFER_FAIL:
  389. response = 500;
  390. break;
  391. case AST_BRIDGE_TRANSFER_SUCCESS:
  392. response = 200;
  393. ast_sip_session_defer_termination(attended->transferer);
  394. break;
  395. }
  396. ast_debug(3, "Final response for REFER attended transfer - Transferer #1: %s Transferer #2: %s is '%d'\n",
  397. ast_channel_name(attended->transferer_chan), ast_channel_name(attended->transferer_second->channel), response);
  398. if (attended->progress && response) {
  399. struct refer_progress_notification *notification = refer_progress_notification_alloc(attended->progress, response, PJSIP_EVSUB_STATE_TERMINATED);
  400. if (notification) {
  401. refer_progress_notify(notification);
  402. }
  403. }
  404. return 0;
  405. }
  406. /*! \brief Structure for blind transfer callback details */
  407. struct refer_blind {
  408. /*! \brief Context being used for transfer */
  409. const char *context;
  410. /*! \brief Optional progress structure */
  411. struct refer_progress *progress;
  412. /*! \brief REFER message */
  413. pjsip_rx_data *rdata;
  414. /*! \brief Optional Replaces header */
  415. pjsip_replaces_hdr *replaces;
  416. /*! \brief Optional Refer-To header */
  417. pjsip_sip_uri *refer_to;
  418. };
  419. /*! \brief Blind transfer callback function */
  420. static void refer_blind_callback(struct ast_channel *chan, struct transfer_channel_data *user_data_wrapper,
  421. enum ast_transfer_type transfer_type)
  422. {
  423. struct refer_blind *refer = user_data_wrapper->data;
  424. pjsip_generic_string_hdr *referred_by;
  425. static const pj_str_t str_referred_by = { "Referred-By", 11 };
  426. pbx_builtin_setvar_helper(chan, "SIPTRANSFER", "yes");
  427. /* If progress monitoring is being done attach a frame hook so we can monitor it */
  428. if (refer->progress) {
  429. struct ast_framehook_interface hook = {
  430. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  431. .event_cb = refer_progress_framehook,
  432. .destroy_cb = refer_progress_framehook_destroy,
  433. .data = refer->progress,
  434. .disable_inheritance = 1,
  435. };
  436. refer->progress->transferee = ast_strdup(ast_channel_uniqueid(chan));
  437. if (!refer->progress->transferee) {
  438. struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
  439. PJSIP_EVSUB_STATE_TERMINATED);
  440. ast_log(LOG_WARNING, "Could not copy channel name '%s' during transfer - assuming success\n",
  441. ast_channel_name(chan));
  442. if (notification) {
  443. refer_progress_notify(notification);
  444. }
  445. }
  446. /* Progress needs a reference to the transfer_channel_data so that it can track the completed status of the transfer */
  447. ao2_ref(user_data_wrapper, +1);
  448. refer->progress->transfer_data = user_data_wrapper;
  449. /* We need to bump the reference count up on the progress structure since it is in the frame hook now */
  450. ao2_ref(refer->progress, +1);
  451. /* If we can't attach a frame hook for whatever reason send a notification of success immediately */
  452. if ((refer->progress->framehook = ast_framehook_attach(chan, &hook)) < 0) {
  453. struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
  454. PJSIP_EVSUB_STATE_TERMINATED);
  455. ast_log(LOG_WARNING, "Could not attach REFER transfer progress monitoring hook to channel '%s' - assuming success\n",
  456. ast_channel_name(chan));
  457. if (notification) {
  458. refer_progress_notify(notification);
  459. }
  460. ao2_cleanup(refer->progress);
  461. }
  462. /* We need to bump the reference count for the stasis subscription */
  463. ao2_ref(refer->progress, +1);
  464. /* We also will need to detect if the transferee enters a bridge. This is currently the only reliable way to
  465. * detect if the transfer target has answered the call
  466. */
  467. refer->progress->bridge_sub = stasis_subscribe_pool(ast_bridge_topic_all(), refer_progress_bridge, refer->progress);
  468. if (!refer->progress->bridge_sub) {
  469. struct refer_progress_notification *notification = refer_progress_notification_alloc(refer->progress, 200,
  470. PJSIP_EVSUB_STATE_TERMINATED);
  471. ast_log(LOG_WARNING, "Could not create bridge stasis subscription for monitoring progress on transfer of channel '%s' - assuming success\n",
  472. ast_channel_name(chan));
  473. if (notification) {
  474. refer_progress_notify(notification);
  475. }
  476. ast_framehook_detach(chan, refer->progress->framehook);
  477. ao2_cleanup(refer->progress);
  478. }
  479. }
  480. pbx_builtin_setvar_helper(chan, "SIPREFERRINGCONTEXT", S_OR(refer->context, NULL));
  481. referred_by = pjsip_msg_find_hdr_by_name(refer->rdata->msg_info.msg,
  482. &str_referred_by, NULL);
  483. if (referred_by) {
  484. size_t uri_size = pj_strlen(&referred_by->hvalue) + 1;
  485. char *uri = ast_alloca(uri_size);
  486. ast_copy_pj_str(uri, &referred_by->hvalue, uri_size);
  487. pbx_builtin_setvar_helper(chan, "__SIPREFERREDBYHDR", S_OR(uri, NULL));
  488. } else {
  489. pbx_builtin_setvar_helper(chan, "SIPREFERREDBYHDR", NULL);
  490. }
  491. if (refer->replaces) {
  492. char replaces[512];
  493. pjsip_hdr_print_on(refer->replaces, replaces, sizeof(replaces));
  494. pbx_builtin_setvar_helper(chan, "__SIPREPLACESHDR", S_OR(replaces, NULL));
  495. } else {
  496. pbx_builtin_setvar_helper(chan, "SIPREPLACESHDR", NULL);
  497. }
  498. if (refer->refer_to) {
  499. char refer_to[PJSIP_MAX_URL_SIZE];
  500. pjsip_uri_print(PJSIP_URI_IN_REQ_URI, refer->refer_to, refer_to, sizeof(refer_to));
  501. pbx_builtin_setvar_helper(chan, "SIPREFERTOHDR", S_OR(refer_to, NULL));
  502. } else {
  503. pbx_builtin_setvar_helper(chan, "SIPREFERTOHDR", NULL);
  504. }
  505. }
  506. static int refer_incoming_attended_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_sip_uri *target_uri,
  507. pjsip_param *replaces_param, struct refer_progress *progress)
  508. {
  509. const pj_str_t str_replaces = { "Replaces", 8 };
  510. pj_str_t replaces_content;
  511. pjsip_replaces_hdr *replaces;
  512. int parsed_len;
  513. pjsip_dialog *dlg;
  514. pj_strdup_with_null(rdata->tp_info.pool, &replaces_content, &replaces_param->value);
  515. /* Parsing the parameter as a Replaces header easily grabs the needed information */
  516. if (!(replaces = pjsip_parse_hdr(rdata->tp_info.pool, &str_replaces, replaces_content.ptr,
  517. pj_strlen(&replaces_content), &parsed_len))) {
  518. ast_log(LOG_ERROR, "Received REFER request on channel '%s' from endpoint '%s' with invalid Replaces header, rejecting\n",
  519. ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
  520. return 400;
  521. }
  522. /* See if the dialog is local, or remote */
  523. if ((dlg = pjsip_ua_find_dialog(&replaces->call_id, &replaces->to_tag, &replaces->from_tag, PJ_TRUE))) {
  524. RAII_VAR(struct ast_sip_session *, other_session, ast_sip_dialog_get_session(dlg), ao2_cleanup);
  525. struct refer_attended *attended;
  526. pjsip_dlg_dec_lock(dlg);
  527. if (!other_session) {
  528. ast_debug(3, "Received REFER request on channel '%s' from endpoint '%s' for local dialog but no session exists on it\n",
  529. ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
  530. return 603;
  531. }
  532. /* We defer actually doing the attended transfer to the other session so no deadlock can occur */
  533. if (!(attended = refer_attended_alloc(session, other_session, progress))) {
  534. 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",
  535. ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
  536. return 500;
  537. }
  538. /* Push it to the other session, which will have both channels with minimal locking */
  539. if (ast_sip_push_task(other_session->serializer, refer_attended, attended)) {
  540. ao2_cleanup(attended);
  541. return 500;
  542. }
  543. ast_debug(3, "Attended transfer from '%s' pushed to second channel serializer\n",
  544. ast_channel_name(session->channel));
  545. return 200;
  546. } else {
  547. const char *context = (session->channel ? pbx_builtin_getvar_helper(session->channel, "TRANSFER_CONTEXT") : "");
  548. struct refer_blind refer = { 0, };
  549. if (ast_strlen_zero(context)) {
  550. context = session->endpoint->context;
  551. }
  552. if (!ast_exists_extension(NULL, context, "external_replaces", 1, NULL)) {
  553. ast_log(LOG_ERROR, "Received REFER for remote session on channel '%s' from endpoint '%s' but 'external_replaces' context does not exist for handling\n",
  554. ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
  555. return 404;
  556. }
  557. refer.context = context;
  558. refer.progress = progress;
  559. refer.rdata = rdata;
  560. refer.replaces = replaces;
  561. refer.refer_to = target_uri;
  562. switch (ast_bridge_transfer_blind(1, session->channel, "external_replaces", context, refer_blind_callback, &refer)) {
  563. case AST_BRIDGE_TRANSFER_INVALID:
  564. return 400;
  565. case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
  566. return 403;
  567. case AST_BRIDGE_TRANSFER_FAIL:
  568. return 500;
  569. case AST_BRIDGE_TRANSFER_SUCCESS:
  570. ast_sip_session_defer_termination(session);
  571. return 200;
  572. }
  573. return 503;
  574. }
  575. return 0;
  576. }
  577. static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_sip_uri *target,
  578. struct refer_progress *progress)
  579. {
  580. const char *context;
  581. char exten[AST_MAX_EXTENSION];
  582. struct refer_blind refer = { 0, };
  583. if (!session->channel) {
  584. return 404;
  585. }
  586. /* If no explicit transfer context has been provided use their configured context */
  587. context = pbx_builtin_getvar_helper(session->channel, "TRANSFER_CONTEXT");
  588. if (ast_strlen_zero(context)) {
  589. context = session->endpoint->context;
  590. }
  591. /* Using the user portion of the target URI see if it exists as a valid extension in their context */
  592. ast_copy_pj_str(exten, &target->user, sizeof(exten));
  593. if (!ast_exists_extension(NULL, context, exten, 1, NULL)) {
  594. ast_log(LOG_ERROR, "Channel '%s' from endpoint '%s' attempted blind transfer to '%s@%s' but target does not exist\n",
  595. ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), exten, context);
  596. return 404;
  597. }
  598. refer.context = context;
  599. refer.progress = progress;
  600. refer.rdata = rdata;
  601. refer.refer_to = target;
  602. switch (ast_bridge_transfer_blind(1, session->channel, exten, context, refer_blind_callback, &refer)) {
  603. case AST_BRIDGE_TRANSFER_INVALID:
  604. return 400;
  605. case AST_BRIDGE_TRANSFER_NOT_PERMITTED:
  606. return 403;
  607. case AST_BRIDGE_TRANSFER_FAIL:
  608. return 500;
  609. case AST_BRIDGE_TRANSFER_SUCCESS:
  610. ast_sip_session_defer_termination(session);
  611. return 200;
  612. }
  613. return 503;
  614. }
  615. /*! \brief Structure used to retrieve channel from another session */
  616. struct invite_replaces {
  617. /*! \brief Session we want the channel from */
  618. struct ast_sip_session *session;
  619. /*! \brief Channel from the session (with reference) */
  620. struct ast_channel *channel;
  621. /*! \brief Bridge the channel is in */
  622. struct ast_bridge *bridge;
  623. };
  624. /*! \brief Task for invite replaces */
  625. static int invite_replaces(void *data)
  626. {
  627. struct invite_replaces *invite = data;
  628. if (!invite->session->channel) {
  629. return -1;
  630. }
  631. ast_channel_ref(invite->session->channel);
  632. invite->channel = invite->session->channel;
  633. ast_channel_lock(invite->channel);
  634. invite->bridge = ast_channel_get_bridge(invite->channel);
  635. ast_channel_unlock(invite->channel);
  636. return 0;
  637. }
  638. static int refer_incoming_invite_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  639. {
  640. pjsip_dialog *other_dlg = NULL;
  641. pjsip_tx_data *packet;
  642. int response = 0;
  643. RAII_VAR(struct ast_sip_session *, other_session, NULL, ao2_cleanup);
  644. struct invite_replaces invite;
  645. /* If a Replaces header is present make sure it is valid */
  646. if (pjsip_replaces_verify_request(rdata, &other_dlg, PJ_TRUE, &packet) != PJ_SUCCESS) {
  647. response = packet->msg->line.status.code;
  648. pjsip_tx_data_dec_ref(packet);
  649. goto end;
  650. }
  651. /* If no other dialog exists then this INVITE request does not have a Replaces header */
  652. if (!other_dlg) {
  653. return 0;
  654. }
  655. other_session = ast_sip_dialog_get_session(other_dlg);
  656. pjsip_dlg_dec_lock(other_dlg);
  657. /* Don't accept an in-dialog INVITE with Replaces as it does not make much sense */
  658. if (session->inv_session->dlg->state == PJSIP_DIALOG_STATE_ESTABLISHED) {
  659. response = 488;
  660. goto end;
  661. }
  662. if (!other_session) {
  663. response = 481;
  664. ast_debug(3, "INVITE with Replaces received on channel '%s' from endpoint '%s', but requested session does not exist\n",
  665. ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
  666. goto end;
  667. }
  668. invite.session = other_session;
  669. if (ast_sip_push_task_synchronous(other_session->serializer, invite_replaces, &invite)) {
  670. response = 481;
  671. goto end;
  672. }
  673. ast_channel_lock(session->channel);
  674. ast_setstate(session->channel, AST_STATE_RING);
  675. ast_channel_unlock(session->channel);
  676. ast_raw_answer(session->channel);
  677. if (!invite.bridge) {
  678. struct ast_channel *chan = session->channel;
  679. /* This will use a synchronous task but we aren't operating in the serializer at this point in time, so it
  680. * won't deadlock */
  681. if (!ast_channel_move(invite.channel, session->channel)) {
  682. ast_hangup(chan);
  683. } else {
  684. response = 500;
  685. }
  686. } else {
  687. if (ast_bridge_impart(invite.bridge, session->channel, invite.channel, NULL,
  688. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  689. response = 500;
  690. }
  691. }
  692. if (!response) {
  693. ast_debug(3, "INVITE with Replaces successfully completed on channels '%s' and '%s'\n",
  694. ast_channel_name(session->channel), ast_channel_name(invite.channel));
  695. }
  696. ast_channel_unref(invite.channel);
  697. ao2_cleanup(invite.bridge);
  698. end:
  699. if (response) {
  700. if (session->inv_session->dlg->state != PJSIP_DIALOG_STATE_ESTABLISHED) {
  701. ast_debug(3, "INVITE with Replaces failed on channel '%s', sending response of '%d'\n",
  702. ast_channel_name(session->channel), response);
  703. session->defer_terminate = 1;
  704. ast_hangup(session->channel);
  705. session->channel = NULL;
  706. if (pjsip_inv_end_session(session->inv_session, response, NULL, &packet) == PJ_SUCCESS) {
  707. ast_sip_session_send_response(session, packet);
  708. }
  709. } else {
  710. ast_debug(3, "INVITE with Replaces in-dialog on channel '%s', hanging up\n",
  711. ast_channel_name(session->channel));
  712. ast_queue_hangup(session->channel);
  713. }
  714. }
  715. return 1;
  716. }
  717. static int refer_incoming_refer_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  718. {
  719. pjsip_generic_string_hdr *refer_to;
  720. char *uri;
  721. pjsip_uri *target;
  722. pjsip_sip_uri *target_uri;
  723. RAII_VAR(struct refer_progress *, progress, NULL, ao2_cleanup);
  724. pjsip_param *replaces;
  725. int response;
  726. static const pj_str_t str_refer_to = { "Refer-To", 8 };
  727. static const pj_str_t str_replaces = { "Replaces", 8 };
  728. if (!session->endpoint->allowtransfer) {
  729. pjsip_dlg_respond(session->inv_session->dlg, rdata, 603, NULL, NULL, NULL);
  730. ast_log(LOG_WARNING, "Endpoint %s transfer attempt blocked due to configuration\n",
  731. ast_sorcery_object_get_id(session->endpoint));
  732. return 0;
  733. }
  734. /* A Refer-To header is required */
  735. refer_to = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL);
  736. if (!refer_to) {
  737. pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
  738. ast_debug(3, "Received a REFER without Refer-To on channel '%s' from endpoint '%s'\n",
  739. ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
  740. return 0;
  741. }
  742. /* This is done on purpose (and is safe) - it's done so that the value passed to
  743. * pjsip_parse_uri is NULL terminated as required
  744. */
  745. uri = refer_to->hvalue.ptr;
  746. uri[refer_to->hvalue.slen] = '\0';
  747. target = pjsip_parse_uri(rdata->tp_info.pool, refer_to->hvalue.ptr, refer_to->hvalue.slen, 0);
  748. if (!target
  749. || (!PJSIP_URI_SCHEME_IS_SIP(target)
  750. && !PJSIP_URI_SCHEME_IS_SIPS(target))) {
  751. size_t uri_size = pj_strlen(&refer_to->hvalue) + 1;
  752. char *uri = ast_alloca(uri_size);
  753. ast_copy_pj_str(uri, &refer_to->hvalue, uri_size);
  754. pjsip_dlg_respond(session->inv_session->dlg, rdata, 400, NULL, NULL, NULL);
  755. ast_debug(3, "Received a REFER without a parseable Refer-To ('%s') on channel '%s' from endpoint '%s'\n",
  756. uri, ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
  757. return 0;
  758. }
  759. target_uri = pjsip_uri_get_uri(target);
  760. /* Set up REFER progress subscription if requested/possible */
  761. if (refer_progress_alloc(session, rdata, &progress)) {
  762. pjsip_dlg_respond(session->inv_session->dlg, rdata, 500, NULL, NULL, NULL);
  763. ast_debug(3, "Could not set up subscription for REFER on channel '%s' from endpoint '%s'\n",
  764. ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint));
  765. return 0;
  766. }
  767. /* Determine if this is an attended or blind transfer */
  768. if ((replaces = pjsip_param_find(&target_uri->header_param, &str_replaces)) ||
  769. (replaces = pjsip_param_find(&target_uri->other_param, &str_replaces))) {
  770. response = refer_incoming_attended_request(session, rdata, target_uri, replaces, progress);
  771. } else {
  772. response = refer_incoming_blind_request(session, rdata, target_uri, progress);
  773. }
  774. if (!progress) {
  775. /* The transferer has requested no subscription, so send a final response immediately */
  776. pjsip_tx_data *tdata;
  777. const pj_str_t str_refer_sub = { "Refer-Sub", 9 };
  778. const pj_str_t str_false = { "false", 5 };
  779. pjsip_hdr *hdr;
  780. ast_debug(3, "Progress monitoring not requested for REFER on channel '%s' from endpoint '%s', sending immediate response of '%d'\n",
  781. ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), response);
  782. if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, response, NULL, &tdata) != PJ_SUCCESS) {
  783. pjsip_dlg_respond(session->inv_session->dlg, rdata, response, NULL, NULL, NULL);
  784. return 0;
  785. }
  786. hdr = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool, &str_refer_sub, &str_false);
  787. pjsip_msg_add_hdr(tdata->msg, hdr);
  788. pjsip_dlg_send_response(session->inv_session->dlg, pjsip_rdata_get_tsx(rdata), tdata);
  789. } else if (response != 200) {
  790. /* Since this failed we can send a final NOTIFY now and terminate the subscription */
  791. struct refer_progress_notification *notification = refer_progress_notification_alloc(progress, response, PJSIP_EVSUB_STATE_TERMINATED);
  792. if (notification) {
  793. /* The refer_progress_notify function will call ao2_cleanup on this for us */
  794. refer_progress_notify(notification);
  795. }
  796. }
  797. return 0;
  798. }
  799. static int refer_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
  800. {
  801. if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_refer_method())) {
  802. return refer_incoming_refer_request(session, rdata);
  803. } else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_invite_method)) {
  804. return refer_incoming_invite_request(session, rdata);
  805. } else {
  806. return 0;
  807. }
  808. }
  809. static void refer_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
  810. {
  811. const char *hdr;
  812. if (pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_invite_method)
  813. || !session->channel
  814. || session->inv_session->state != PJSIP_INV_STATE_NULL) {
  815. return;
  816. }
  817. ast_channel_lock(session->channel);
  818. hdr = pbx_builtin_getvar_helper(session->channel, "SIPREPLACESHDR");
  819. if (!ast_strlen_zero(hdr)) {
  820. ast_sip_add_header(tdata, "Replaces", hdr);
  821. }
  822. hdr = pbx_builtin_getvar_helper(session->channel, "SIPREFERREDBYHDR");
  823. if (!ast_strlen_zero(hdr)) {
  824. ast_sip_add_header(tdata, "Referred-By", hdr);
  825. }
  826. ast_channel_unlock(session->channel);
  827. }
  828. static struct ast_sip_session_supplement refer_supplement = {
  829. .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 1,
  830. .incoming_request = refer_incoming_request,
  831. .outgoing_request = refer_outgoing_request,
  832. };
  833. static int load_module(void)
  834. {
  835. const pj_str_t str_norefersub = { "norefersub", 10 };
  836. CHECK_PJSIP_SESSION_MODULE_LOADED();
  837. pjsip_replaces_init_module(ast_sip_get_pjsip_endpoint());
  838. pjsip_xfer_init_module(ast_sip_get_pjsip_endpoint());
  839. pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), NULL, PJSIP_H_SUPPORTED, NULL, 1, &str_norefersub);
  840. ast_sip_register_service(&refer_progress_module);
  841. ast_sip_session_register_supplement(&refer_supplement);
  842. return AST_MODULE_LOAD_SUCCESS;
  843. }
  844. static int unload_module(void)
  845. {
  846. ast_sip_session_unregister_supplement(&refer_supplement);
  847. ast_sip_unregister_service(&refer_progress_module);
  848. return 0;
  849. }
  850. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Blind and Attended Transfer Support",
  851. .support_level = AST_MODULE_SUPPORT_CORE,
  852. .load = load_module,
  853. .unload = unload_module,
  854. .load_pri = AST_MODPRI_APP_DEPEND,
  855. );