core_unreal.c 25 KB

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