res_pjsip_refer.c 35 KB

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