core_unreal.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013 Digium, Inc.
  5. *
  6. * Richard Mudgett <rmudgett@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief Unreal channel derivatives framework for channel drivers like local channels.
  21. *
  22. * \author Richard Mudgett <rmudgett@digium.com>
  23. *
  24. * See Also:
  25. * \arg \ref AstCREDITS
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/causes.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/stasis_channels.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/musiconhold.h"
  37. #include "asterisk/astobj2.h"
  38. #include "asterisk/bridge.h"
  39. #include "asterisk/core_unreal.h"
  40. static unsigned int name_sequence = 0;
  41. void ast_unreal_lock_all(struct ast_unreal_pvt *p, struct ast_channel **outchan, struct ast_channel **outowner)
  42. {
  43. struct ast_channel *chan = NULL;
  44. struct ast_channel *owner = NULL;
  45. ao2_lock(p);
  46. for (;;) {
  47. if (p->chan) {
  48. chan = p->chan;
  49. ast_channel_ref(chan);
  50. }
  51. if (p->owner) {
  52. owner = p->owner;
  53. ast_channel_ref(owner);
  54. }
  55. ao2_unlock(p);
  56. /* if we don't have both channels, then this is very easy */
  57. if (!owner || !chan) {
  58. if (owner) {
  59. ast_channel_lock(owner);
  60. } else if(chan) {
  61. ast_channel_lock(chan);
  62. }
  63. } else {
  64. /* lock both channels first, then get the pvt lock */
  65. ast_channel_lock_both(chan, owner);
  66. }
  67. ao2_lock(p);
  68. /* Now that we have all the locks, validate that nothing changed */
  69. if (p->owner != owner || p->chan != chan) {
  70. if (owner) {
  71. ast_channel_unlock(owner);
  72. owner = ast_channel_unref(owner);
  73. }
  74. if (chan) {
  75. ast_channel_unlock(chan);
  76. chan = ast_channel_unref(chan);
  77. }
  78. continue;
  79. }
  80. break;
  81. }
  82. *outowner = p->owner;
  83. *outchan = p->chan;
  84. }
  85. /* Called with ast locked */
  86. int ast_unreal_setoption(struct ast_channel *ast, int option, void *data, int datalen)
  87. {
  88. int res = 0;
  89. struct ast_unreal_pvt *p;
  90. struct ast_channel *otherchan = NULL;
  91. ast_chan_write_info_t *write_info;
  92. char *info_data;
  93. if (option != AST_OPTION_CHANNEL_WRITE) {
  94. return -1;
  95. }
  96. write_info = data;
  97. if (write_info->version != AST_CHAN_WRITE_INFO_T_VERSION) {
  98. ast_log(LOG_ERROR, "The chan_write_info_t type has changed, and this channel hasn't been updated!\n");
  99. return -1;
  100. }
  101. info_data = write_info->data;
  102. if (!strcmp(write_info->function, "CHANNEL")) {
  103. if (!strncasecmp(info_data, "hangup_handler_", 15)) {
  104. /* Block CHANNEL(hangup_handler_xxx) writes to the other unreal channel. */
  105. return 0;
  106. }
  107. /* Crossover the accountcode and peeraccount to cross the unreal bridge. */
  108. if (!strcasecmp(info_data, "accountcode")) {
  109. info_data = "peeraccount";
  110. } else if (!strcasecmp(info_data, "peeraccount")) {
  111. info_data = "accountcode";
  112. }
  113. }
  114. /* get the tech pvt */
  115. if (!(p = ast_channel_tech_pvt(ast))) {
  116. return -1;
  117. }
  118. ao2_ref(p, 1);
  119. ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
  120. /* get the channel we are supposed to write to */
  121. ao2_lock(p);
  122. otherchan = (write_info->chan == p->owner) ? p->chan : p->owner;
  123. if (!otherchan || otherchan == write_info->chan) {
  124. res = -1;
  125. otherchan = NULL;
  126. ao2_unlock(p);
  127. goto setoption_cleanup;
  128. }
  129. ast_channel_ref(otherchan);
  130. /* clear the pvt lock before grabbing the channel */
  131. ao2_unlock(p);
  132. ast_channel_lock(otherchan);
  133. res = write_info->write_fn(otherchan, write_info->function, info_data, write_info->value);
  134. ast_channel_unlock(otherchan);
  135. setoption_cleanup:
  136. ao2_ref(p, -1);
  137. if (otherchan) {
  138. ast_channel_unref(otherchan);
  139. }
  140. ast_channel_lock(ast); /* Lock back before we leave */
  141. return res;
  142. }
  143. /* Called with ast locked */
  144. int ast_unreal_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
  145. {
  146. struct ast_unreal_pvt *p;
  147. struct ast_channel *peer;
  148. struct ast_channel *other;
  149. int res = 0;
  150. if (option != AST_OPTION_T38_STATE) {
  151. /* AST_OPTION_T38_STATE is the only supported option at this time */
  152. return -1;
  153. }
  154. /* for some reason the channel is not locked in channel.c when this function is called */
  155. if (!(p = ast_channel_tech_pvt(ast))) {
  156. return -1;
  157. }
  158. ao2_lock(p);
  159. other = AST_UNREAL_IS_OUTBOUND(ast, p) ? p->owner : p->chan;
  160. if (!other) {
  161. ao2_unlock(p);
  162. return -1;
  163. }
  164. ast_channel_ref(other);
  165. ao2_unlock(p);
  166. ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
  167. peer = ast_channel_bridge_peer(other);
  168. if (peer) {
  169. res = ast_channel_queryoption(peer, option, data, datalen, 0);
  170. ast_channel_unref(peer);
  171. }
  172. ast_channel_unref(other);
  173. ast_channel_lock(ast); /* Lock back before we leave */
  174. return res;
  175. }
  176. /*!
  177. * \brief queue a frame onto either the p->owner or p->chan
  178. *
  179. * \note the ast_unreal_pvt MUST have it's ref count bumped before entering this function and
  180. * decremented after this function is called. This is a side effect of the deadlock
  181. * avoidance that is necessary to lock 2 channels and a tech_pvt. Without a ref counted
  182. * ast_unreal_pvt, it is impossible to guarantee it will not be destroyed by another thread
  183. * during deadlock avoidance.
  184. */
  185. static int unreal_queue_frame(struct ast_unreal_pvt *p, int isoutbound, struct ast_frame *f,
  186. struct ast_channel *us, int us_locked)
  187. {
  188. struct ast_channel *other;
  189. /* Recalculate outbound channel */
  190. other = isoutbound ? p->owner : p->chan;
  191. if (!other) {
  192. return 0;
  193. }
  194. /* do not queue media frames if a generator is on both unreal channels */
  195. if (us
  196. && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)
  197. && ast_channel_generator(us)
  198. && ast_channel_generator(other)) {
  199. return 0;
  200. }
  201. /* grab a ref on the channel before unlocking the pvt,
  202. * other can not go away from us now regardless of locking */
  203. ast_channel_ref(other);
  204. if (us && us_locked) {
  205. ast_channel_unlock(us);
  206. }
  207. ao2_unlock(p);
  208. if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_RINGING) {
  209. ast_setstate(other, AST_STATE_RINGING);
  210. }
  211. ast_queue_frame(other, f);
  212. other = ast_channel_unref(other);
  213. if (us && us_locked) {
  214. ast_channel_lock(us);
  215. }
  216. ao2_lock(p);
  217. return 0;
  218. }
  219. int ast_unreal_answer(struct ast_channel *ast)
  220. {
  221. struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
  222. int isoutbound;
  223. int res = -1;
  224. if (!p) {
  225. return -1;
  226. }
  227. ao2_ref(p, 1);
  228. ao2_lock(p);
  229. isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
  230. if (isoutbound) {
  231. /* Pass along answer since somebody answered us */
  232. struct ast_frame answer = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
  233. res = unreal_queue_frame(p, isoutbound, &answer, ast, 1);
  234. } else {
  235. ast_log(LOG_WARNING, "Huh? %s is being asked to answer?\n",
  236. ast_channel_name(ast));
  237. }
  238. ao2_unlock(p);
  239. ao2_ref(p, -1);
  240. return res;
  241. }
  242. /*!
  243. * \internal
  244. * \brief Check and optimize out the unreal channels between bridges.
  245. * \since 12.0.0
  246. *
  247. * \param ast Channel writing a frame into the unreal channels.
  248. * \param p Unreal channel private.
  249. *
  250. * \note It is assumed that ast is locked.
  251. * \note It is assumed that p is locked.
  252. *
  253. * \retval 0 if unreal channels were not optimized out.
  254. * \retval non-zero if unreal channels were optimized out.
  255. */
  256. static int got_optimized_out(struct ast_channel *ast, struct ast_unreal_pvt *p)
  257. {
  258. int res = 0;
  259. /* Do a few conditional checks early on just to see if this optimization is possible */
  260. if (ast_test_flag(p, AST_UNREAL_NO_OPTIMIZATION) || !p->chan || !p->owner) {
  261. return res;
  262. }
  263. if (ast == p->owner) {
  264. res = ast_bridge_unreal_optimize_out(p->owner, p->chan, p);
  265. } else if (ast == p->chan) {
  266. res = ast_bridge_unreal_optimize_out(p->chan, p->owner, p);
  267. }
  268. return res;
  269. }
  270. struct ast_frame *ast_unreal_read(struct ast_channel *ast)
  271. {
  272. return &ast_null_frame;
  273. }
  274. int ast_unreal_write(struct ast_channel *ast, struct ast_frame *f)
  275. {
  276. struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
  277. int res = -1;
  278. if (!p) {
  279. return -1;
  280. }
  281. /* Just queue for delivery to the other side */
  282. ao2_ref(p, 1);
  283. ao2_lock(p);
  284. switch (f->frametype) {
  285. case AST_FRAME_VOICE:
  286. case AST_FRAME_VIDEO:
  287. if (got_optimized_out(ast, p)) {
  288. break;
  289. }
  290. /* fall through */
  291. default:
  292. res = unreal_queue_frame(p, AST_UNREAL_IS_OUTBOUND(ast, p), f, ast, 1);
  293. break;
  294. }
  295. ao2_unlock(p);
  296. ao2_ref(p, -1);
  297. return res;
  298. }
  299. int ast_unreal_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  300. {
  301. struct ast_unreal_pvt *p = ast_channel_tech_pvt(newchan);
  302. struct ast_bridge *bridge_owner;
  303. struct ast_bridge *bridge_chan;
  304. if (!p) {
  305. return -1;
  306. }
  307. ao2_lock(p);
  308. if ((p->owner != oldchan) && (p->chan != oldchan)) {
  309. ast_log(LOG_WARNING, "Old channel %p wasn't %p or %p\n", oldchan, p->owner, p->chan);
  310. ao2_unlock(p);
  311. return -1;
  312. }
  313. if (p->owner == oldchan) {
  314. p->owner = newchan;
  315. } else {
  316. p->chan = newchan;
  317. }
  318. if (ast_check_hangup(newchan) || !p->owner || !p->chan) {
  319. ao2_unlock(p);
  320. return 0;
  321. }
  322. /* Do not let a masquerade cause an unreal channel to be bridged to itself! */
  323. bridge_owner = ast_channel_internal_bridge(p->owner);
  324. bridge_chan = ast_channel_internal_bridge(p->chan);
  325. if (bridge_owner && bridge_owner == bridge_chan) {
  326. ast_log(LOG_WARNING, "You can not bridge an unreal channel (%s) to itself!\n",
  327. ast_channel_name(newchan));
  328. ao2_unlock(p);
  329. ast_queue_hangup(newchan);
  330. return -1;
  331. }
  332. ao2_unlock(p);
  333. return 0;
  334. }
  335. /*!
  336. * \internal
  337. * \brief Queue up a frame representing the indication as a control frame.
  338. * \since 12.0.0
  339. *
  340. * \param p Unreal private structure.
  341. * \param ast Channel indicating the condition.
  342. * \param condition What is being indicated.
  343. * \param data Extra data.
  344. * \param datalen Length of extra data.
  345. *
  346. * \retval 0 on success.
  347. * \retval AST_T38_REQUEST_PARMS if successful and condition is AST_CONTROL_T38_PARAMETERS.
  348. * \retval -1 on error.
  349. */
  350. static int unreal_queue_indicate(struct ast_unreal_pvt *p, struct ast_channel *ast, int condition, const void *data, size_t datalen)
  351. {
  352. int res = 0;
  353. int isoutbound;
  354. ao2_lock(p);
  355. /*
  356. * Block -1 stop tones events if we are to be optimized out. We
  357. * don't need a flurry of these events on an unreal channel chain
  358. * when initially connected to slow the optimization process.
  359. */
  360. if (0 <= condition || ast_test_flag(p, AST_UNREAL_NO_OPTIMIZATION)) {
  361. struct ast_frame f = {
  362. .frametype = AST_FRAME_CONTROL,
  363. .subclass.integer = condition,
  364. .data.ptr = (void *) data,
  365. .datalen = datalen,
  366. };
  367. isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
  368. res = unreal_queue_frame(p, isoutbound, &f, ast, 1);
  369. if (!res
  370. && condition == AST_CONTROL_T38_PARAMETERS
  371. && datalen == sizeof(struct ast_control_t38_parameters)) {
  372. const struct ast_control_t38_parameters *parameters = data;
  373. if (parameters->request_response == AST_T38_REQUEST_PARMS) {
  374. res = AST_T38_REQUEST_PARMS;
  375. }
  376. }
  377. } else {
  378. ast_debug(4, "Blocked indication %d\n", condition);
  379. }
  380. ao2_unlock(p);
  381. return res;
  382. }
  383. /*!
  384. * \internal
  385. * \brief Handle COLP and redirecting conditions.
  386. * \since 12.0.0
  387. *
  388. * \param p Unreal private structure.
  389. * \param ast Channel indicating the condition.
  390. * \param condition What is being indicated.
  391. *
  392. * \retval 0 on success.
  393. * \retval -1 on error.
  394. */
  395. static int unreal_colp_redirect_indicate(struct ast_unreal_pvt *p, struct ast_channel *ast, int condition)
  396. {
  397. struct ast_channel *my_chan;
  398. struct ast_channel *my_owner;
  399. struct ast_channel *this_channel;
  400. struct ast_channel *the_other_channel;
  401. int isoutbound;
  402. int res = 0;
  403. unsigned char frame_data[1024];
  404. struct ast_frame f = {
  405. .frametype = AST_FRAME_CONTROL,
  406. .subclass.integer = condition,
  407. .data.ptr = frame_data,
  408. };
  409. /*
  410. * A connected line update frame may only contain a partial
  411. * amount of data, such as just a source, or just a ton, and not
  412. * the full amount of information. However, the collected
  413. * information is all stored in the outgoing channel's
  414. * connectedline structure, so when receiving a connected line
  415. * update on an outgoing unreal channel, we need to transmit the
  416. * collected connected line information instead of whatever
  417. * happens to be in this control frame. The same applies for
  418. * redirecting information, which is why it is handled here as
  419. * well.
  420. */
  421. ast_channel_unlock(ast);
  422. ast_unreal_lock_all(p, &my_chan, &my_owner);
  423. isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
  424. if (isoutbound) {
  425. this_channel = p->chan;
  426. the_other_channel = p->owner;
  427. } else {
  428. this_channel = p->owner;
  429. the_other_channel = p->chan;
  430. }
  431. if (the_other_channel) {
  432. if (condition == AST_CONTROL_CONNECTED_LINE) {
  433. ast_connected_line_copy_to_caller(ast_channel_caller(the_other_channel),
  434. ast_channel_connected(this_channel));
  435. f.datalen = ast_connected_line_build_data(frame_data, sizeof(frame_data),
  436. ast_channel_connected(this_channel), NULL);
  437. } else {
  438. f.datalen = ast_redirecting_build_data(frame_data, sizeof(frame_data),
  439. ast_channel_redirecting(this_channel), NULL);
  440. }
  441. }
  442. if (my_chan) {
  443. ast_channel_unlock(my_chan);
  444. ast_channel_unref(my_chan);
  445. }
  446. if (my_owner) {
  447. ast_channel_unlock(my_owner);
  448. ast_channel_unref(my_owner);
  449. }
  450. if (the_other_channel) {
  451. res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
  452. }
  453. ao2_unlock(p);
  454. ast_channel_lock(ast);
  455. return res;
  456. }
  457. int ast_unreal_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
  458. {
  459. struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
  460. int res = 0;
  461. if (!p) {
  462. return -1;
  463. }
  464. ao2_ref(p, 1); /* ref for unreal_queue_frame */
  465. switch (condition) {
  466. case AST_CONTROL_MASQUERADE_NOTIFY:
  467. /*
  468. * Always block this because this is the channel being
  469. * masqueraded; not anything down the chain.
  470. */
  471. break;
  472. case AST_CONTROL_CONNECTED_LINE:
  473. case AST_CONTROL_REDIRECTING:
  474. res = unreal_colp_redirect_indicate(p, ast, condition);
  475. break;
  476. case AST_CONTROL_HOLD:
  477. if (ast_test_flag(p, AST_UNREAL_MOH_INTERCEPT)) {
  478. ast_moh_start(ast, data, NULL);
  479. break;
  480. }
  481. res = unreal_queue_indicate(p, ast, condition, data, datalen);
  482. break;
  483. case AST_CONTROL_UNHOLD:
  484. if (ast_test_flag(p, AST_UNREAL_MOH_INTERCEPT)) {
  485. ast_moh_stop(ast);
  486. break;
  487. }
  488. res = unreal_queue_indicate(p, ast, condition, data, datalen);
  489. break;
  490. case AST_CONTROL_RINGING:
  491. /* Don't queue ringing frames if the channel is not in a "ring" state. Otherwise,
  492. * the real channel on the other end will likely start a playtones generator. It is
  493. * possible that this playtones generator will never be stopped under certain
  494. * circumstances.
  495. */
  496. if (ast_channel_state(ast) == AST_STATE_RING) {
  497. res = unreal_queue_indicate(p, ast, condition, data, datalen);
  498. } else {
  499. res = -1;
  500. }
  501. break;
  502. case AST_CONTROL_PVT_CAUSE_CODE:
  503. /* Return -1 so that asterisk core will correctly set up hangupcauses. */
  504. unreal_queue_indicate(p, ast, condition, data, datalen);
  505. res = -1;
  506. break;
  507. default:
  508. res = unreal_queue_indicate(p, ast, condition, data, datalen);
  509. break;
  510. }
  511. ao2_ref(p, -1);
  512. return res;
  513. }
  514. int ast_unreal_digit_begin(struct ast_channel *ast, char digit)
  515. {
  516. struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
  517. int res = -1;
  518. struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
  519. int isoutbound;
  520. if (!p) {
  521. return -1;
  522. }
  523. ao2_ref(p, 1); /* ref for unreal_queue_frame */
  524. ao2_lock(p);
  525. isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
  526. f.subclass.integer = digit;
  527. res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
  528. ao2_unlock(p);
  529. ao2_ref(p, -1);
  530. return res;
  531. }
  532. int ast_unreal_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  533. {
  534. struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
  535. int res = -1;
  536. struct ast_frame f = { AST_FRAME_DTMF_END, };
  537. int isoutbound;
  538. if (!p) {
  539. return -1;
  540. }
  541. ao2_ref(p, 1); /* ref for unreal_queue_frame */
  542. ao2_lock(p);
  543. isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
  544. f.subclass.integer = digit;
  545. f.len = duration;
  546. res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
  547. ao2_unlock(p);
  548. ao2_ref(p, -1);
  549. return res;
  550. }
  551. int ast_unreal_sendtext(struct ast_channel *ast, const char *text)
  552. {
  553. struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
  554. int res = -1;
  555. struct ast_frame f = { AST_FRAME_TEXT, };
  556. int isoutbound;
  557. if (!p) {
  558. return -1;
  559. }
  560. ao2_ref(p, 1); /* ref for unreal_queue_frame */
  561. ao2_lock(p);
  562. isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
  563. f.data.ptr = (char *) text;
  564. f.datalen = strlen(text) + 1;
  565. res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
  566. ao2_unlock(p);
  567. ao2_ref(p, -1);
  568. return res;
  569. }
  570. int ast_unreal_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
  571. {
  572. struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
  573. int res = -1;
  574. struct ast_frame f = { AST_FRAME_HTML, };
  575. int isoutbound;
  576. if (!p) {
  577. return -1;
  578. }
  579. ao2_ref(p, 1); /* ref for unreal_queue_frame */
  580. ao2_lock(p);
  581. isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
  582. f.subclass.integer = subclass;
  583. f.data.ptr = (char *)data;
  584. f.datalen = datalen;
  585. res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
  586. ao2_unlock(p);
  587. ao2_ref(p, -1);
  588. return res;
  589. }
  590. void ast_unreal_call_setup(struct ast_channel *semi1, struct ast_channel *semi2)
  591. {
  592. struct ast_var_t *varptr;
  593. struct ast_var_t *clone_var;
  594. ast_channel_stage_snapshot(semi2);
  595. /*
  596. * Note that cid_num and cid_name aren't passed in the
  597. * ast_channel_alloc calls in ast_unreal_new_channels(). It's
  598. * done here instead.
  599. */
  600. ast_party_redirecting_copy(ast_channel_redirecting(semi2), ast_channel_redirecting(semi1));
  601. ast_party_dialed_copy(ast_channel_dialed(semi2), ast_channel_dialed(semi1));
  602. /* Crossover the CallerID and conected-line to cross the unreal bridge. */
  603. ast_connected_line_copy_to_caller(ast_channel_caller(semi2), ast_channel_connected(semi1));
  604. ast_connected_line_copy_from_caller(ast_channel_connected(semi2), ast_channel_caller(semi1));
  605. ast_channel_language_set(semi2, ast_channel_language(semi1));
  606. ast_channel_musicclass_set(semi2, ast_channel_musicclass(semi1));
  607. ast_channel_parkinglot_set(semi2, ast_channel_parkinglot(semi1));
  608. /* Crossover the accountcode and peeraccount to cross the unreal bridge. */
  609. ast_channel_accountcode_set(semi2, ast_channel_peeraccount(semi1));
  610. ast_channel_peeraccount_set(semi2, ast_channel_accountcode(semi1));
  611. ast_channel_cc_params_init(semi2, ast_channel_get_cc_config_params(semi1));
  612. /*
  613. * Make sure we inherit the AST_CAUSE_ANSWERED_ELSEWHERE if it's
  614. * set on the queue/dial call request in the dialplan.
  615. */
  616. if (ast_channel_hangupcause(semi1) == AST_CAUSE_ANSWERED_ELSEWHERE) {
  617. ast_channel_hangupcause_set(semi2, AST_CAUSE_ANSWERED_ELSEWHERE);
  618. }
  619. /*
  620. * Copy the channel variables from the semi1 channel to the
  621. * outgoing channel.
  622. *
  623. * Note that due to certain assumptions, they MUST be in the
  624. * same order.
  625. */
  626. AST_LIST_TRAVERSE(ast_channel_varshead(semi1), varptr, entries) {
  627. clone_var = ast_var_assign(varptr->name, varptr->value);
  628. if (clone_var) {
  629. AST_LIST_INSERT_TAIL(ast_channel_varshead(semi2), clone_var, entries);
  630. ast_channel_publish_varset(semi2, ast_var_full_name(clone_var),
  631. ast_var_value(clone_var));
  632. }
  633. }
  634. ast_channel_datastore_inherit(semi1, semi2);
  635. ast_channel_stage_snapshot_done(semi2);
  636. }
  637. int ast_unreal_channel_push_to_bridge(struct ast_channel *ast, struct ast_bridge *bridge, unsigned int flags)
  638. {
  639. struct ast_bridge_features *features;
  640. struct ast_channel *chan;
  641. struct ast_channel *owner;
  642. RAII_VAR(struct ast_unreal_pvt *, p, NULL, ao2_cleanup);
  643. RAII_VAR(struct ast_callid *, bridge_callid, NULL, ast_callid_cleanup);
  644. ast_bridge_lock(bridge);
  645. bridge_callid = bridge->callid ? ast_callid_ref(bridge->callid) : NULL;
  646. ast_bridge_unlock(bridge);
  647. {
  648. SCOPED_CHANNELLOCK(lock, ast);
  649. p = ast_channel_tech_pvt(ast);
  650. if (!p) {
  651. return -1;
  652. }
  653. ao2_ref(p, +1);
  654. }
  655. {
  656. SCOPED_AO2LOCK(lock, p);
  657. chan = p->chan;
  658. if (!chan) {
  659. return -1;
  660. }
  661. owner = p->owner;
  662. if (!owner) {
  663. return -1;
  664. }
  665. ast_channel_ref(chan);
  666. ast_channel_ref(owner);
  667. }
  668. if (bridge_callid) {
  669. struct ast_callid *chan_callid;
  670. struct ast_callid *owner_callid;
  671. /* chan side call ID setting */
  672. ast_channel_lock(chan);
  673. chan_callid = ast_channel_callid(chan);
  674. if (!chan_callid) {
  675. ast_channel_callid_set(chan, bridge_callid);
  676. }
  677. ast_channel_unlock(chan);
  678. ast_callid_cleanup(chan_callid);
  679. /* owner side call ID setting */
  680. ast_channel_lock(owner);
  681. owner_callid = ast_channel_callid(owner);
  682. if (!owner_callid) {
  683. ast_channel_callid_set(owner, bridge_callid);
  684. }
  685. ast_channel_unlock(owner);
  686. ast_callid_cleanup(owner_callid);
  687. }
  688. /* We are done with the owner now that its call ID matches the bridge */
  689. ast_channel_unref(owner);
  690. owner = NULL;
  691. features = ast_bridge_features_new();
  692. if (!features) {
  693. ast_channel_unref(chan);
  694. return -1;
  695. }
  696. ast_set_flag(&features->feature_flags, flags);
  697. /* Impart the semi2 channel into the bridge */
  698. if (ast_bridge_impart(bridge, chan, NULL, features,
  699. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  700. ast_channel_unref(chan);
  701. return -1;
  702. }
  703. /* The bridge thread now controls the chan ref from the ast_unreal_pvt */
  704. ao2_lock(p);
  705. ast_set_flag(p, AST_UNREAL_CARETAKER_THREAD);
  706. ao2_unlock(p);
  707. ast_channel_unref(chan);
  708. return 0;
  709. }
  710. int ast_unreal_hangup(struct ast_unreal_pvt *p, struct ast_channel *ast)
  711. {
  712. int hangup_chan = 0;
  713. int res = 0;
  714. int cause;
  715. struct ast_channel *owner = NULL;
  716. struct ast_channel *chan = NULL;
  717. /* the pvt isn't going anywhere, it has a ref */
  718. ast_channel_unlock(ast);
  719. /* lock everything */
  720. ast_unreal_lock_all(p, &chan, &owner);
  721. if (ast != chan && ast != owner) {
  722. res = -1;
  723. goto unreal_hangup_cleanup;
  724. }
  725. cause = ast_channel_hangupcause(ast);
  726. if (ast == p->chan) {
  727. /* Outgoing side is hanging up. */
  728. ast_clear_flag(p, AST_UNREAL_CARETAKER_THREAD);
  729. p->chan = NULL;
  730. if (p->owner) {
  731. const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
  732. if (status) {
  733. ast_channel_hangupcause_set(p->owner, cause);
  734. pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
  735. }
  736. ast_queue_hangup_with_cause(p->owner, cause);
  737. }
  738. } else {
  739. /* Owner side is hanging up. */
  740. p->owner = NULL;
  741. if (p->chan) {
  742. if (cause == AST_CAUSE_ANSWERED_ELSEWHERE) {
  743. ast_channel_hangupcause_set(p->chan, AST_CAUSE_ANSWERED_ELSEWHERE);
  744. ast_debug(2, "%s has AST_CAUSE_ANSWERED_ELSEWHERE set.\n",
  745. ast_channel_name(p->chan));
  746. }
  747. if (!ast_test_flag(p, AST_UNREAL_CARETAKER_THREAD)) {
  748. /*
  749. * Need to actually hangup p->chan since nothing else is taking
  750. * care of it.
  751. */
  752. hangup_chan = 1;
  753. } else {
  754. ast_queue_hangup_with_cause(p->chan, cause);
  755. }
  756. }
  757. }
  758. /* this is one of our locked channels, doesn't matter which */
  759. ast_channel_tech_pvt_set(ast, NULL);
  760. ao2_ref(p, -1);
  761. unreal_hangup_cleanup:
  762. ao2_unlock(p);
  763. if (owner) {
  764. ast_channel_unlock(owner);
  765. ast_channel_unref(owner);
  766. }
  767. if (chan) {
  768. ast_channel_unlock(chan);
  769. if (hangup_chan) {
  770. ast_hangup(chan);
  771. }
  772. ast_channel_unref(chan);
  773. }
  774. /* leave with the channel locked that came in */
  775. ast_channel_lock(ast);
  776. return res;
  777. }
  778. void ast_unreal_destructor(void *vdoomed)
  779. {
  780. struct ast_unreal_pvt *doomed = vdoomed;
  781. ao2_cleanup(doomed->reqcap);
  782. doomed->reqcap = NULL;
  783. }
  784. struct ast_unreal_pvt *ast_unreal_alloc(size_t size, ao2_destructor_fn destructor, struct ast_format_cap *cap)
  785. {
  786. struct ast_unreal_pvt *unreal;
  787. static const struct ast_jb_conf jb_conf = {
  788. .flags = 0,
  789. .max_size = -1,
  790. .resync_threshold = -1,
  791. .impl = "",
  792. .target_extra = -1,
  793. };
  794. unreal = ao2_alloc(size, destructor);
  795. if (!unreal) {
  796. return NULL;
  797. }
  798. unreal->reqcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  799. if (!unreal->reqcap) {
  800. ao2_ref(unreal, -1);
  801. return NULL;
  802. }
  803. ast_format_cap_append_from_cap(unreal->reqcap, cap, AST_MEDIA_TYPE_UNKNOWN);
  804. memcpy(&unreal->jb_conf, &jb_conf, sizeof(unreal->jb_conf));
  805. return unreal;
  806. }
  807. struct ast_channel *ast_unreal_new_channels(struct ast_unreal_pvt *p,
  808. const struct ast_channel_tech *tech, int semi1_state, int semi2_state,
  809. const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
  810. const struct ast_channel *requestor, struct ast_callid *callid)
  811. {
  812. struct ast_channel *owner;
  813. struct ast_channel *chan;
  814. RAII_VAR(struct ast_format *, fmt, NULL, ao2_cleanup);
  815. struct ast_assigned_ids id1 = {NULL, NULL};
  816. struct ast_assigned_ids id2 = {NULL, NULL};
  817. int generated_seqno = ast_atomic_fetchadd_int((int *) &name_sequence, +1);
  818. /* set unique ids for the two channels */
  819. if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
  820. id1.uniqueid = assignedids->uniqueid;
  821. id2.uniqueid = assignedids->uniqueid2;
  822. }
  823. /* if id1 given but not id2, use default of id1;2 */
  824. if (id1.uniqueid && ast_strlen_zero(id2.uniqueid)) {
  825. char *uniqueid2;
  826. uniqueid2 = ast_alloca(strlen(id1.uniqueid) + 3);
  827. strcpy(uniqueid2, id1.uniqueid);/* Safe */
  828. strcat(uniqueid2, ";2");/* Safe */
  829. id2.uniqueid = uniqueid2;
  830. }
  831. /*
  832. * Allocate two new Asterisk channels
  833. *
  834. * Make sure that the ;2 channel gets the same linkedid as ;1.
  835. * You can't pass linkedid to both allocations since if linkedid
  836. * isn't set, then each channel will generate its own linkedid.
  837. */
  838. owner = ast_channel_alloc(1, semi1_state, NULL, NULL, NULL,
  839. exten, context, &id1, requestor, 0,
  840. "%s/%s-%08x;1", tech->type, p->name, (unsigned)generated_seqno);
  841. if (!owner) {
  842. ast_log(LOG_WARNING, "Unable to allocate owner channel structure\n");
  843. return NULL;
  844. }
  845. if (callid) {
  846. ast_channel_callid_set(owner, callid);
  847. }
  848. ast_channel_tech_set(owner, tech);
  849. ao2_ref(p, +1);
  850. ast_channel_tech_pvt_set(owner, p);
  851. ast_channel_nativeformats_set(owner, p->reqcap);
  852. /* Determine our read/write format and set it on each channel */
  853. fmt = ast_format_cap_get_format(p->reqcap, 0);
  854. if (!fmt) {
  855. ast_channel_tech_pvt_set(owner, NULL);
  856. ao2_ref(p, -1);
  857. ast_channel_unlock(owner);
  858. ast_channel_release(owner);
  859. return NULL;
  860. }
  861. ast_channel_set_writeformat(owner, fmt);
  862. ast_channel_set_rawwriteformat(owner, fmt);
  863. ast_channel_set_readformat(owner, fmt);
  864. ast_channel_set_rawreadformat(owner, fmt);
  865. ast_set_flag(ast_channel_flags(owner), AST_FLAG_DISABLE_DEVSTATE_CACHE);
  866. ast_jb_configure(owner, &p->jb_conf);
  867. if (ast_channel_cc_params_init(owner, requestor
  868. ? ast_channel_get_cc_config_params((struct ast_channel *) requestor) : NULL)) {
  869. ast_channel_tech_pvt_set(owner, NULL);
  870. ao2_ref(p, -1);
  871. ast_channel_tech_pvt_set(owner, NULL);
  872. ast_channel_unlock(owner);
  873. ast_channel_release(owner);
  874. return NULL;
  875. }
  876. p->owner = owner;
  877. ast_channel_unlock(owner);
  878. chan = ast_channel_alloc(1, semi2_state, NULL, NULL, NULL,
  879. exten, context, &id2, owner, 0,
  880. "%s/%s-%08x;2", tech->type, p->name, (unsigned)generated_seqno);
  881. if (!chan) {
  882. ast_log(LOG_WARNING, "Unable to allocate chan channel structure\n");
  883. ast_channel_tech_pvt_set(owner, NULL);
  884. ao2_ref(p, -1);
  885. ast_channel_tech_pvt_set(owner, NULL);
  886. ast_channel_release(owner);
  887. return NULL;
  888. }
  889. if (callid) {
  890. ast_channel_callid_set(chan, callid);
  891. }
  892. ast_channel_tech_set(chan, tech);
  893. ao2_ref(p, +1);
  894. ast_channel_tech_pvt_set(chan, p);
  895. ast_channel_nativeformats_set(chan, p->reqcap);
  896. /* Format was already determined when setting up owner */
  897. ast_channel_set_writeformat(chan, fmt);
  898. ast_channel_set_rawwriteformat(chan, fmt);
  899. ast_channel_set_readformat(chan, fmt);
  900. ast_channel_set_rawreadformat(chan, fmt);
  901. ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
  902. p->chan = chan;
  903. ast_channel_unlock(chan);
  904. return owner;
  905. }