core_local.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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 Local proxy channel driver.
  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. /* ------------------------------------------------------------------- */
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/cli.h"
  36. #include "asterisk/manager.h"
  37. #include "asterisk/devicestate.h"
  38. #include "asterisk/astobj2.h"
  39. #include "asterisk/bridge.h"
  40. #include "asterisk/core_unreal.h"
  41. #include "asterisk/core_local.h"
  42. #include "asterisk/stasis.h"
  43. #include "asterisk/stasis_channels.h"
  44. #include "asterisk/_private.h"
  45. #include "asterisk/stasis_channels.h"
  46. /*** DOCUMENTATION
  47. <manager name="LocalOptimizeAway" language="en_US">
  48. <synopsis>
  49. Optimize away a local channel when possible.
  50. </synopsis>
  51. <syntax>
  52. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  53. <parameter name="Channel" required="true">
  54. <para>The channel name to optimize away.</para>
  55. </parameter>
  56. </syntax>
  57. <description>
  58. <para>A local channel created with "/n" will not automatically optimize away.
  59. Calling this command on the local channel will clear that flag and allow
  60. it to optimize away if it's bridged or when it becomes bridged.</para>
  61. </description>
  62. </manager>
  63. <managerEvent language="en_US" name="LocalBridge">
  64. <managerEventInstance class="EVENT_FLAG_CALL">
  65. <synopsis>Raised when two halves of a Local Channel form a bridge.</synopsis>
  66. <syntax>
  67. <channel_snapshot prefix="LocalOne"/>
  68. <channel_snapshot prefix="LocalTwo"/>
  69. <parameter name="Context">
  70. <para>The context in the dialplan that Channel2 starts in.</para>
  71. </parameter>
  72. <parameter name="Exten">
  73. <para>The extension in the dialplan that Channel2 starts in.</para>
  74. </parameter>
  75. <parameter name="LocalOptimization">
  76. <enumlist>
  77. <enum name="Yes"/>
  78. <enum name="No"/>
  79. </enumlist>
  80. </parameter>
  81. </syntax>
  82. </managerEventInstance>
  83. </managerEvent>
  84. <managerEvent language="en_US" name="LocalOptimizationBegin">
  85. <managerEventInstance class="EVENT_FLAG_CALL">
  86. <synopsis>Raised when two halves of a Local Channel begin to optimize
  87. themselves out of the media path.</synopsis>
  88. <syntax>
  89. <channel_snapshot prefix="LocalOne"/>
  90. <channel_snapshot prefix="LocalTwo"/>
  91. <channel_snapshot prefix="Source"/>
  92. <parameter name="DestUniqueId">
  93. <para>The unique ID of the bridge into which the local channel is optimizing.</para>
  94. </parameter>
  95. <parameter name="Id">
  96. <para>Identification for the optimization operation.</para>
  97. </parameter>
  98. </syntax>
  99. <see-also>
  100. <ref type="managerEvent">LocalOptimizationEnd</ref>
  101. <ref type="manager">LocalOptimizeAway</ref>
  102. </see-also>
  103. </managerEventInstance>
  104. </managerEvent>
  105. <managerEvent language="en_US" name="LocalOptimizationEnd">
  106. <managerEventInstance class="EVENT_FLAG_CALL">
  107. <synopsis>Raised when two halves of a Local Channel have finished optimizing
  108. themselves out of the media path.</synopsis>
  109. <syntax>
  110. <channel_snapshot prefix="LocalOne"/>
  111. <channel_snapshot prefix="LocalTwo"/>
  112. <parameter name="Success">
  113. <para>Indicates whether the local optimization succeeded.</para>
  114. </parameter>
  115. <parameter name="Id">
  116. <para>Identification for the optimization operation. Matches the <replaceable>Id</replaceable>
  117. from a previous <literal>LocalOptimizationBegin</literal></para>
  118. </parameter>
  119. </syntax>
  120. <see-also>
  121. <ref type="managerEvent">LocalOptimizationBegin</ref>
  122. <ref type="manager">LocalOptimizeAway</ref>
  123. </see-also>
  124. </managerEventInstance>
  125. </managerEvent>
  126. ***/
  127. static const char tdesc[] = "Local Proxy Channel Driver";
  128. static struct ao2_container *locals;
  129. static struct ast_channel *local_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
  130. static int local_call(struct ast_channel *ast, const char *dest, int timeout);
  131. static int local_hangup(struct ast_channel *ast);
  132. static int local_devicestate(const char *data);
  133. static void local_optimization_started_cb(struct ast_unreal_pvt *base, struct ast_channel *source,
  134. enum ast_unreal_channel_indicator dest, unsigned int id);
  135. static void local_optimization_finished_cb(struct ast_unreal_pvt *base, int success, unsigned int id);
  136. static struct ast_manager_event_blob *local_message_to_ami(struct stasis_message *msg);
  137. /*!
  138. * @{ \brief Define local channel message types.
  139. */
  140. STASIS_MESSAGE_TYPE_DEFN(ast_local_bridge_type,
  141. .to_ami = local_message_to_ami,
  142. );
  143. STASIS_MESSAGE_TYPE_DEFN(ast_local_optimization_begin_type,
  144. .to_ami = local_message_to_ami,
  145. );
  146. STASIS_MESSAGE_TYPE_DEFN(ast_local_optimization_end_type,
  147. .to_ami = local_message_to_ami,
  148. );
  149. /*! @} */
  150. /*! \brief Callbacks from the unreal core when channel optimization occurs */
  151. struct ast_unreal_pvt_callbacks local_unreal_callbacks = {
  152. .optimization_started = local_optimization_started_cb,
  153. .optimization_finished = local_optimization_finished_cb,
  154. };
  155. /* PBX interface structure for channel registration */
  156. static struct ast_channel_tech local_tech = {
  157. .type = "Local",
  158. .description = tdesc,
  159. .requester = local_request,
  160. .send_digit_begin = ast_unreal_digit_begin,
  161. .send_digit_end = ast_unreal_digit_end,
  162. .call = local_call,
  163. .hangup = local_hangup,
  164. .answer = ast_unreal_answer,
  165. .read = ast_unreal_read,
  166. .write = ast_unreal_write,
  167. .write_video = ast_unreal_write,
  168. .exception = ast_unreal_read,
  169. .indicate = ast_unreal_indicate,
  170. .fixup = ast_unreal_fixup,
  171. .send_html = ast_unreal_sendhtml,
  172. .send_text = ast_unreal_sendtext,
  173. .devicestate = local_devicestate,
  174. .queryoption = ast_unreal_queryoption,
  175. .setoption = ast_unreal_setoption,
  176. };
  177. /*! What to do with the ;2 channel when ast_call() happens. */
  178. enum local_call_action {
  179. /* The ast_call() will run dialplan on the ;2 channel. */
  180. LOCAL_CALL_ACTION_DIALPLAN,
  181. /* The ast_call() will impart the ;2 channel into a bridge. */
  182. LOCAL_CALL_ACTION_BRIDGE,
  183. /* The ast_call() will masquerade the ;2 channel into a channel. */
  184. LOCAL_CALL_ACTION_MASQUERADE,
  185. };
  186. /*! Join a bridge on ast_call() parameters. */
  187. struct local_bridge {
  188. /*! Bridge to join. */
  189. struct ast_bridge *join;
  190. /*! Channel to swap with when joining bridge. */
  191. struct ast_channel *swap;
  192. /*! Features that are specific to this channel when pushed into the bridge. */
  193. struct ast_bridge_features *features;
  194. };
  195. /*!
  196. * \brief the local pvt structure for all channels
  197. *
  198. * The local channel pvt has two ast_chan objects - the "owner" and the "next channel", the outbound channel
  199. *
  200. * ast_chan owner -> local_pvt -> ast_chan chan
  201. */
  202. struct local_pvt {
  203. /*! Unreal channel driver base class values. */
  204. struct ast_unreal_pvt base;
  205. /*! Additional action arguments */
  206. union {
  207. /*! Make ;2 join a bridge on ast_call(). */
  208. struct local_bridge bridge;
  209. /*! Make ;2 masquerade into this channel on ast_call(). */
  210. struct ast_channel *masq;
  211. } action;
  212. /*! What to do with the ;2 channel on ast_call(). */
  213. enum local_call_action type;
  214. /*! Context to call */
  215. char context[AST_MAX_CONTEXT];
  216. /*! Extension to call */
  217. char exten[AST_MAX_EXTENSION];
  218. };
  219. void ast_local_lock_all2(struct ast_channel *chan, void **tech_pvt,
  220. struct ast_channel **base_chan, struct ast_channel **base_owner)
  221. {
  222. struct local_pvt *p = ast_channel_tech_pvt(chan);
  223. *tech_pvt = NULL;
  224. *base_chan = NULL;
  225. *base_owner = NULL;
  226. if (p) {
  227. *tech_pvt = ao2_bump(p);
  228. ast_unreal_lock_all(&p->base, base_chan, base_owner);
  229. }
  230. }
  231. void ast_local_lock_all(struct ast_channel *chan, struct ast_channel **outchan,
  232. struct ast_channel **outowner)
  233. {
  234. void *tech_pvt;
  235. ast_local_lock_all2(chan, &tech_pvt, outchan, outowner);
  236. }
  237. void ast_local_unlock_all2(void *tech_pvt, struct ast_channel *base_chan,
  238. struct ast_channel *base_owner)
  239. {
  240. if (base_chan) {
  241. ast_channel_unlock(base_chan);
  242. ast_channel_unref(base_chan);
  243. }
  244. if (base_owner) {
  245. ast_channel_unlock(base_owner);
  246. ast_channel_unref(base_owner);
  247. }
  248. if (tech_pvt) {
  249. struct local_pvt *p = tech_pvt;
  250. ao2_unlock(&p->base);
  251. ao2_ref(tech_pvt, -1);
  252. }
  253. }
  254. void ast_local_unlock_all(struct ast_channel *chan)
  255. {
  256. struct local_pvt *p = ast_channel_tech_pvt(chan);
  257. struct ast_unreal_pvt *base;
  258. if (!p) {
  259. return;
  260. }
  261. base = &p->base;
  262. ast_local_unlock_all2(p, base->chan, base->owner);
  263. }
  264. struct ast_channel *ast_local_get_peer(struct ast_channel *ast)
  265. {
  266. struct local_pvt *p = ast_channel_tech_pvt(ast);
  267. struct local_pvt *found;
  268. struct ast_channel *peer;
  269. if (!p) {
  270. return NULL;
  271. }
  272. found = p ? ao2_find(locals, p, 0) : NULL;
  273. if (!found) {
  274. /* ast is either not a local channel or it has alredy been hungup */
  275. return NULL;
  276. }
  277. ao2_lock(found);
  278. if (ast == p->base.owner) {
  279. peer = p->base.chan;
  280. } else if (ast == p->base.chan) {
  281. peer = p->base.owner;
  282. } else {
  283. peer = NULL;
  284. }
  285. if (peer) {
  286. ast_channel_ref(peer);
  287. }
  288. ao2_unlock(found);
  289. ao2_ref(found, -1);
  290. return peer;
  291. }
  292. /*! \brief Adds devicestate to local channels */
  293. static int local_devicestate(const char *data)
  294. {
  295. int is_inuse = 0;
  296. int res = AST_DEVICE_INVALID;
  297. char *exten = ast_strdupa(data);
  298. char *context;
  299. char *opts;
  300. struct local_pvt *lp;
  301. struct ao2_iterator it;
  302. /* Strip options if they exist */
  303. opts = strchr(exten, '/');
  304. if (opts) {
  305. *opts = '\0';
  306. }
  307. context = strchr(exten, '@');
  308. if (!context) {
  309. ast_log(LOG_WARNING,
  310. "Someone used Local/%s somewhere without a @context. This is bad.\n", data);
  311. return AST_DEVICE_INVALID;
  312. }
  313. *context++ = '\0';
  314. it = ao2_iterator_init(locals, 0);
  315. for (; (lp = ao2_iterator_next(&it)); ao2_ref(lp, -1)) {
  316. ao2_lock(lp);
  317. if (!strcmp(exten, lp->exten)
  318. && !strcmp(context, lp->context)) {
  319. res = AST_DEVICE_NOT_INUSE;
  320. if (lp->base.owner
  321. && ast_test_flag(&lp->base, AST_UNREAL_CARETAKER_THREAD)) {
  322. is_inuse = 1;
  323. }
  324. }
  325. ao2_unlock(lp);
  326. if (is_inuse) {
  327. res = AST_DEVICE_INUSE;
  328. ao2_ref(lp, -1);
  329. break;
  330. }
  331. }
  332. ao2_iterator_destroy(&it);
  333. if (res == AST_DEVICE_INVALID) {
  334. ast_debug(3, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
  335. if (ast_exists_extension(NULL, context, exten, 1, NULL)) {
  336. res = AST_DEVICE_NOT_INUSE;
  337. }
  338. }
  339. return res;
  340. }
  341. static struct ast_multi_channel_blob *local_channel_optimization_blob(struct local_pvt *p,
  342. struct ast_json *json_object)
  343. {
  344. struct ast_multi_channel_blob *payload;
  345. RAII_VAR(struct ast_channel_snapshot *, local_one_snapshot, NULL, ao2_cleanup);
  346. RAII_VAR(struct ast_channel_snapshot *, local_two_snapshot, NULL, ao2_cleanup);
  347. local_one_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(p->base.owner));
  348. if (!local_one_snapshot) {
  349. return NULL;
  350. }
  351. local_two_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(p->base.chan));
  352. if (!local_two_snapshot) {
  353. return NULL;
  354. }
  355. payload = ast_multi_channel_blob_create(json_object);
  356. if (!payload) {
  357. return NULL;
  358. }
  359. ast_multi_channel_blob_add_channel(payload, "1", local_one_snapshot);
  360. ast_multi_channel_blob_add_channel(payload, "2", local_two_snapshot);
  361. return payload;
  362. }
  363. /*! \brief Callback for \ref ast_unreal_pvt_callbacks \ref optimization_started_cb */
  364. static void local_optimization_started_cb(struct ast_unreal_pvt *base, struct ast_channel *source,
  365. enum ast_unreal_channel_indicator dest, unsigned int id)
  366. {
  367. RAII_VAR(struct ast_json *, json_object, ast_json_null(), ast_json_unref);
  368. RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup);
  369. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  370. struct local_pvt *p = (struct local_pvt *)base;
  371. if (!ast_local_optimization_begin_type()) {
  372. return;
  373. }
  374. json_object = ast_json_pack("{s: i, s: i}",
  375. "dest", dest, "id", id);
  376. if (!json_object) {
  377. return;
  378. }
  379. payload = local_channel_optimization_blob(p, json_object);
  380. if (!payload) {
  381. return;
  382. }
  383. if (source) {
  384. RAII_VAR(struct ast_channel_snapshot *, source_snapshot, NULL, ao2_cleanup);
  385. source_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(source));
  386. if (!source_snapshot) {
  387. return;
  388. }
  389. ast_multi_channel_blob_add_channel(payload, "source", source_snapshot);
  390. }
  391. msg = stasis_message_create(ast_local_optimization_begin_type(), payload);
  392. if (!msg) {
  393. return;
  394. }
  395. stasis_publish(ast_channel_topic(p->base.owner), msg);
  396. }
  397. /*! \brief Callback for \ref ast_unreal_pvt_callbacks \ref optimization_finished_cb */
  398. static void local_optimization_finished_cb(struct ast_unreal_pvt *base, int success, unsigned int id)
  399. {
  400. RAII_VAR(struct ast_json *, json_object, ast_json_null(), ast_json_unref);
  401. RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup);
  402. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  403. struct local_pvt *p = (struct local_pvt *)base;
  404. if (!ast_local_optimization_end_type()) {
  405. return;
  406. }
  407. json_object = ast_json_pack("{s: i, s: i}", "success", success, "id", id);
  408. if (!json_object) {
  409. return;
  410. }
  411. payload = local_channel_optimization_blob(p, json_object);
  412. if (!payload) {
  413. return;
  414. }
  415. msg = stasis_message_create(ast_local_optimization_end_type(), payload);
  416. if (!msg) {
  417. return;
  418. }
  419. stasis_publish(ast_channel_topic(p->base.owner), msg);
  420. }
  421. static struct ast_manager_event_blob *local_message_to_ami(struct stasis_message *message)
  422. {
  423. struct ast_multi_channel_blob *obj = stasis_message_data(message);
  424. struct ast_json *blob = ast_multi_channel_blob_get_json(obj);
  425. struct ast_channel_snapshot *local_snapshot_one;
  426. struct ast_channel_snapshot *local_snapshot_two;
  427. RAII_VAR(struct ast_str *, local_channel_one, NULL, ast_free);
  428. RAII_VAR(struct ast_str *, local_channel_two, NULL, ast_free);
  429. RAII_VAR(struct ast_str *, event_buffer, NULL, ast_free);
  430. const char *event;
  431. local_snapshot_one = ast_multi_channel_blob_get_channel(obj, "1");
  432. local_snapshot_two = ast_multi_channel_blob_get_channel(obj, "2");
  433. if (!local_snapshot_one || !local_snapshot_two) {
  434. return NULL;
  435. }
  436. event_buffer = ast_str_create(1024);
  437. local_channel_one = ast_manager_build_channel_state_string_prefix(local_snapshot_one, "LocalOne");
  438. local_channel_two = ast_manager_build_channel_state_string_prefix(local_snapshot_two, "LocalTwo");
  439. if (!event_buffer || !local_channel_one || !local_channel_two) {
  440. return NULL;
  441. }
  442. if (stasis_message_type(message) == ast_local_optimization_begin_type()) {
  443. struct ast_channel_snapshot *source_snapshot;
  444. RAII_VAR(struct ast_str *, source_str, NULL, ast_free);
  445. const char *dest_uniqueid;
  446. source_snapshot = ast_multi_channel_blob_get_channel(obj, "source");
  447. if (source_snapshot) {
  448. source_str = ast_manager_build_channel_state_string_prefix(source_snapshot, "Source");
  449. if (!source_str) {
  450. return NULL;
  451. }
  452. }
  453. dest_uniqueid = ast_json_object_get(blob, "dest") == AST_UNREAL_OWNER ?
  454. local_snapshot_one->uniqueid : local_snapshot_two->uniqueid;
  455. event = "LocalOptimizationBegin";
  456. if (source_str) {
  457. ast_str_append(&event_buffer, 0, "%s", ast_str_buffer(source_str));
  458. }
  459. ast_str_append(&event_buffer, 0, "DestUniqueId: %s\r\n", dest_uniqueid);
  460. ast_str_append(&event_buffer, 0, "Id: %u\r\n", (unsigned int) ast_json_integer_get(ast_json_object_get(blob, "id")));
  461. } else if (stasis_message_type(message) == ast_local_optimization_end_type()) {
  462. event = "LocalOptimizationEnd";
  463. ast_str_append(&event_buffer, 0, "Success: %s\r\n", ast_json_integer_get(ast_json_object_get(blob, "success")) ? "Yes" : "No");
  464. ast_str_append(&event_buffer, 0, "Id: %u\r\n", (unsigned int) ast_json_integer_get(ast_json_object_get(blob, "id")));
  465. } else if (stasis_message_type(message) == ast_local_bridge_type()) {
  466. event = "LocalBridge";
  467. ast_str_append(&event_buffer, 0, "Context: %s\r\n", ast_json_string_get(ast_json_object_get(blob, "context")));
  468. ast_str_append(&event_buffer, 0, "Exten: %s\r\n", ast_json_string_get(ast_json_object_get(blob, "exten")));
  469. ast_str_append(&event_buffer, 0, "LocalOptimization: %s\r\n", ast_json_is_true(ast_json_object_get(blob, "can_optimize")) ? "Yes" : "No");
  470. } else {
  471. return NULL;
  472. }
  473. return ast_manager_event_blob_create(EVENT_FLAG_CALL, event,
  474. "%s"
  475. "%s"
  476. "%s",
  477. ast_str_buffer(local_channel_one),
  478. ast_str_buffer(local_channel_two),
  479. ast_str_buffer(event_buffer));
  480. }
  481. /*!
  482. * \internal
  483. * \brief Post the \ref ast_local_bridge_type \ref stasis message
  484. * \since 12.0.0
  485. *
  486. * \param p local_pvt to raise the local bridge message
  487. *
  488. * \return Nothing
  489. */
  490. static void publish_local_bridge_message(struct local_pvt *p)
  491. {
  492. RAII_VAR(struct ast_multi_channel_blob *, multi_blob, NULL, ao2_cleanup);
  493. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  494. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  495. RAII_VAR(struct ast_channel_snapshot *, one_snapshot, NULL, ao2_cleanup);
  496. RAII_VAR(struct ast_channel_snapshot *, two_snapshot, NULL, ao2_cleanup);
  497. struct ast_channel *owner;
  498. struct ast_channel *chan;
  499. if (!ast_local_bridge_type()) {
  500. return;
  501. }
  502. ast_unreal_lock_all(&p->base, &chan, &owner);
  503. blob = ast_json_pack("{s: s, s: s, s: b}",
  504. "context", p->context,
  505. "exten", p->exten,
  506. "can_optimize", !ast_test_flag(&p->base, AST_UNREAL_NO_OPTIMIZATION));
  507. if (!blob) {
  508. goto end;
  509. }
  510. multi_blob = ast_multi_channel_blob_create(blob);
  511. if (!multi_blob) {
  512. goto end;
  513. }
  514. one_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(owner));
  515. if (!one_snapshot) {
  516. goto end;
  517. }
  518. two_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan));
  519. if (!two_snapshot) {
  520. goto end;
  521. }
  522. ast_multi_channel_blob_add_channel(multi_blob, "1", one_snapshot);
  523. ast_multi_channel_blob_add_channel(multi_blob, "2", two_snapshot);
  524. msg = stasis_message_create(ast_local_bridge_type(), multi_blob);
  525. if (!msg) {
  526. goto end;
  527. }
  528. stasis_publish(ast_channel_topic(owner), msg);
  529. end:
  530. ast_channel_unlock(owner);
  531. ast_channel_unref(owner);
  532. ast_channel_unlock(chan);
  533. ast_channel_unref(chan);
  534. ao2_unlock(&p->base);
  535. }
  536. int ast_local_setup_bridge(struct ast_channel *ast, struct ast_bridge *bridge, struct ast_channel *swap, struct ast_bridge_features *features)
  537. {
  538. struct local_pvt *p;
  539. struct local_pvt *found;
  540. int res = -1;
  541. /* Sanity checks. */
  542. if (!ast || !bridge) {
  543. ast_bridge_features_destroy(features);
  544. return -1;
  545. }
  546. ast_channel_lock(ast);
  547. p = ast_channel_tech_pvt(ast);
  548. ast_channel_unlock(ast);
  549. found = p ? ao2_find(locals, p, 0) : NULL;
  550. if (found) {
  551. ao2_lock(found);
  552. if (found->type == LOCAL_CALL_ACTION_DIALPLAN
  553. && found->base.owner
  554. && found->base.chan
  555. && !ast_test_flag(&found->base, AST_UNREAL_CARETAKER_THREAD)) {
  556. ao2_ref(bridge, +1);
  557. if (swap) {
  558. ast_channel_ref(swap);
  559. }
  560. found->type = LOCAL_CALL_ACTION_BRIDGE;
  561. found->action.bridge.join = bridge;
  562. found->action.bridge.swap = swap;
  563. found->action.bridge.features = features;
  564. res = 0;
  565. } else {
  566. ast_bridge_features_destroy(features);
  567. }
  568. ao2_unlock(found);
  569. ao2_ref(found, -1);
  570. }
  571. return res;
  572. }
  573. int ast_local_setup_masquerade(struct ast_channel *ast, struct ast_channel *masq)
  574. {
  575. struct local_pvt *p;
  576. struct local_pvt *found;
  577. int res = -1;
  578. /* Sanity checks. */
  579. if (!ast || !masq) {
  580. return -1;
  581. }
  582. ast_channel_lock(ast);
  583. p = ast_channel_tech_pvt(ast);
  584. ast_channel_unlock(ast);
  585. found = p ? ao2_find(locals, p, 0) : NULL;
  586. if (found) {
  587. ao2_lock(found);
  588. if (found->type == LOCAL_CALL_ACTION_DIALPLAN
  589. && found->base.owner
  590. && found->base.chan
  591. && !ast_test_flag(&found->base, AST_UNREAL_CARETAKER_THREAD)) {
  592. ast_channel_ref(masq);
  593. found->type = LOCAL_CALL_ACTION_MASQUERADE;
  594. found->action.masq = masq;
  595. res = 0;
  596. }
  597. ao2_unlock(found);
  598. ao2_ref(found, -1);
  599. }
  600. return res;
  601. }
  602. /*! \brief Initiate new call, part of PBX interface
  603. * dest is the dial string */
  604. static int local_call(struct ast_channel *ast, const char *dest, int timeout)
  605. {
  606. struct local_pvt *p = ast_channel_tech_pvt(ast);
  607. int pvt_locked = 0;
  608. struct ast_channel *owner = NULL;
  609. struct ast_channel *chan = NULL;
  610. int res;
  611. char *reduced_dest = ast_strdupa(dest);
  612. char *slash;
  613. const char *chan_cid;
  614. if (!p) {
  615. return -1;
  616. }
  617. /* since we are letting go of channel locks that were locked coming into
  618. * this function, then we need to give the tech pvt a ref */
  619. ao2_ref(p, 1);
  620. ast_channel_unlock(ast);
  621. ast_unreal_lock_all(&p->base, &chan, &owner);
  622. pvt_locked = 1;
  623. if (owner != ast) {
  624. res = -1;
  625. goto return_cleanup;
  626. }
  627. if (!owner || !chan) {
  628. res = -1;
  629. goto return_cleanup;
  630. }
  631. ast_unreal_call_setup(owner, chan);
  632. /*
  633. * If the local channel has /n on the end of it, we need to lop
  634. * that off for our argument to setting up the CC_INTERFACES
  635. * variable.
  636. */
  637. if ((slash = strrchr(reduced_dest, '/'))) {
  638. *slash = '\0';
  639. }
  640. ast_set_cc_interfaces_chanvar(chan, reduced_dest);
  641. ao2_unlock(p);
  642. pvt_locked = 0;
  643. ast_channel_unlock(owner);
  644. chan_cid = S_COR(ast_channel_caller(chan)->id.number.valid,
  645. ast_channel_caller(chan)->id.number.str, NULL);
  646. if (chan_cid) {
  647. chan_cid = ast_strdupa(chan_cid);
  648. }
  649. ast_channel_unlock(chan);
  650. res = -1;
  651. switch (p->type) {
  652. case LOCAL_CALL_ACTION_DIALPLAN:
  653. if (!ast_exists_extension(NULL, p->context, p->exten, 1, chan_cid)) {
  654. ast_log(LOG_NOTICE, "No such extension/context %s@%s while calling Local channel\n",
  655. p->exten, p->context);
  656. } else {
  657. publish_local_bridge_message(p);
  658. /* Start switch on sub channel */
  659. res = ast_pbx_start(chan);
  660. }
  661. break;
  662. case LOCAL_CALL_ACTION_BRIDGE:
  663. publish_local_bridge_message(p);
  664. ast_answer(chan);
  665. res = ast_bridge_impart(p->action.bridge.join, chan, p->action.bridge.swap,
  666. p->action.bridge.features, AST_BRIDGE_IMPART_CHAN_INDEPENDENT);
  667. ao2_ref(p->action.bridge.join, -1);
  668. p->action.bridge.join = NULL;
  669. ao2_cleanup(p->action.bridge.swap);
  670. p->action.bridge.swap = NULL;
  671. p->action.bridge.features = NULL;
  672. break;
  673. case LOCAL_CALL_ACTION_MASQUERADE:
  674. publish_local_bridge_message(p);
  675. ast_answer(chan);
  676. res = ast_channel_move(p->action.masq, chan);
  677. if (!res) {
  678. /* Chan is now an orphaned zombie. Destroy it. */
  679. ast_hangup(chan);
  680. }
  681. p->action.masq = ast_channel_unref(p->action.masq);
  682. break;
  683. }
  684. if (!res) {
  685. ao2_lock(p);
  686. ast_set_flag(&p->base, AST_UNREAL_CARETAKER_THREAD);
  687. ao2_unlock(p);
  688. }
  689. /* we already unlocked them, clear them here so the cleanup label won't touch them. */
  690. owner = ast_channel_unref(owner);
  691. chan = ast_channel_unref(chan);
  692. return_cleanup:
  693. if (p) {
  694. if (pvt_locked) {
  695. ao2_unlock(p);
  696. }
  697. ao2_ref(p, -1);
  698. }
  699. if (chan) {
  700. ast_channel_unlock(chan);
  701. ast_channel_unref(chan);
  702. }
  703. /*
  704. * owner is supposed to be == to ast, if it is, don't unlock it
  705. * because ast must exit locked
  706. */
  707. if (owner) {
  708. if (owner != ast) {
  709. ast_channel_unlock(owner);
  710. ast_channel_lock(ast);
  711. }
  712. ast_channel_unref(owner);
  713. } else {
  714. /* we have to exit with ast locked */
  715. ast_channel_lock(ast);
  716. }
  717. return res;
  718. }
  719. /*! \brief Hangup a call through the local proxy channel */
  720. static int local_hangup(struct ast_channel *ast)
  721. {
  722. struct local_pvt *p = ast_channel_tech_pvt(ast);
  723. int res;
  724. if (!p) {
  725. return -1;
  726. }
  727. /* give the pvt a ref to fulfill calling requirements. */
  728. ao2_ref(p, +1);
  729. res = ast_unreal_hangup(&p->base, ast);
  730. if (!res) {
  731. int unlink;
  732. ao2_lock(p);
  733. unlink = !p->base.owner && !p->base.chan;
  734. ao2_unlock(p);
  735. if (unlink) {
  736. ao2_unlink(locals, p);
  737. }
  738. }
  739. ao2_ref(p, -1);
  740. return res;
  741. }
  742. /*!
  743. * \internal
  744. * \brief struct local_pvt destructor.
  745. *
  746. * \param vdoomed Object to destroy.
  747. *
  748. * \return Nothing
  749. */
  750. static void local_pvt_destructor(void *vdoomed)
  751. {
  752. struct local_pvt *doomed = vdoomed;
  753. switch (doomed->type) {
  754. case LOCAL_CALL_ACTION_DIALPLAN:
  755. break;
  756. case LOCAL_CALL_ACTION_BRIDGE:
  757. ao2_cleanup(doomed->action.bridge.join);
  758. ao2_cleanup(doomed->action.bridge.swap);
  759. ast_bridge_features_destroy(doomed->action.bridge.features);
  760. break;
  761. case LOCAL_CALL_ACTION_MASQUERADE:
  762. ao2_cleanup(doomed->action.masq);
  763. break;
  764. }
  765. ast_unreal_destructor(&doomed->base);
  766. }
  767. /*! \brief Create a call structure */
  768. static struct local_pvt *local_alloc(const char *data, struct ast_format_cap *cap)
  769. {
  770. struct local_pvt *pvt;
  771. char *parse;
  772. char *context;
  773. char *opts;
  774. pvt = (struct local_pvt *) ast_unreal_alloc(sizeof(*pvt), local_pvt_destructor, cap);
  775. if (!pvt) {
  776. return NULL;
  777. }
  778. pvt->base.callbacks = &local_unreal_callbacks;
  779. parse = ast_strdupa(data);
  780. /*
  781. * Local channels intercept MOH by default.
  782. *
  783. * This is a silly default because it represents state held by
  784. * the local channels. Unless local channel optimization is
  785. * disabled, the state will dissapear when the local channels
  786. * optimize out.
  787. */
  788. ast_set_flag(&pvt->base, AST_UNREAL_MOH_INTERCEPT);
  789. /* Look for options */
  790. if ((opts = strchr(parse, '/'))) {
  791. *opts++ = '\0';
  792. if (strchr(opts, 'n')) {
  793. ast_set_flag(&pvt->base, AST_UNREAL_NO_OPTIMIZATION);
  794. }
  795. if (strchr(opts, 'j')) {
  796. if (ast_test_flag(&pvt->base, AST_UNREAL_NO_OPTIMIZATION)) {
  797. ast_set_flag(&pvt->base.jb_conf, AST_JB_ENABLED);
  798. } else {
  799. ast_log(LOG_ERROR, "You must use the 'n' option with the 'j' option to enable the jitter buffer\n");
  800. }
  801. }
  802. if (strchr(opts, 'm')) {
  803. ast_clear_flag(&pvt->base, AST_UNREAL_MOH_INTERCEPT);
  804. }
  805. }
  806. /* Look for a context */
  807. if ((context = strchr(parse, '@'))) {
  808. *context++ = '\0';
  809. }
  810. ast_copy_string(pvt->context, S_OR(context, "default"), sizeof(pvt->context));
  811. ast_copy_string(pvt->exten, parse, sizeof(pvt->exten));
  812. snprintf(pvt->base.name, sizeof(pvt->base.name), "%s@%s", pvt->exten, pvt->context);
  813. return pvt; /* this is returned with a ref */
  814. }
  815. /*! \brief Part of PBX interface */
  816. static struct ast_channel *local_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
  817. {
  818. struct local_pvt *p;
  819. struct ast_channel *chan;
  820. struct ast_callid *callid;
  821. /* Allocate a new private structure and then Asterisk channels */
  822. p = local_alloc(data, cap);
  823. if (!p) {
  824. return NULL;
  825. }
  826. callid = ast_read_threadstorage_callid();
  827. chan = ast_unreal_new_channels(&p->base, &local_tech, AST_STATE_DOWN, AST_STATE_RING,
  828. p->exten, p->context, assignedids, requestor, callid);
  829. if (chan) {
  830. ao2_link(locals, p);
  831. }
  832. if (callid) {
  833. ast_callid_unref(callid);
  834. }
  835. ao2_ref(p, -1); /* kill the ref from the alloc */
  836. return chan;
  837. }
  838. /*! \brief CLI command "local show channels" */
  839. static char *locals_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  840. {
  841. struct local_pvt *p;
  842. struct ao2_iterator it;
  843. switch (cmd) {
  844. case CLI_INIT:
  845. e->command = "local show channels";
  846. e->usage =
  847. "Usage: local show channels\n"
  848. " Provides summary information on active local proxy channels.\n";
  849. return NULL;
  850. case CLI_GENERATE:
  851. return NULL;
  852. }
  853. if (a->argc != 3) {
  854. return CLI_SHOWUSAGE;
  855. }
  856. if (ao2_container_count(locals) == 0) {
  857. ast_cli(a->fd, "No local channels in use\n");
  858. return RESULT_SUCCESS;
  859. }
  860. it = ao2_iterator_init(locals, 0);
  861. while ((p = ao2_iterator_next(&it))) {
  862. ao2_lock(p);
  863. ast_cli(a->fd, "%s -- %s\n",
  864. p->base.owner ? ast_channel_name(p->base.owner) : "<unowned>",
  865. p->base.name);
  866. ao2_unlock(p);
  867. ao2_ref(p, -1);
  868. }
  869. ao2_iterator_destroy(&it);
  870. return CLI_SUCCESS;
  871. }
  872. static struct ast_cli_entry cli_local[] = {
  873. AST_CLI_DEFINE(locals_show, "List status of local channels"),
  874. };
  875. static int manager_optimize_away(struct mansession *s, const struct message *m)
  876. {
  877. const char *channel;
  878. struct local_pvt *p;
  879. struct local_pvt *found;
  880. struct ast_channel *chan;
  881. channel = astman_get_header(m, "Channel");
  882. if (ast_strlen_zero(channel)) {
  883. astman_send_error(s, m, "'Channel' not specified.");
  884. return 0;
  885. }
  886. chan = ast_channel_get_by_name(channel);
  887. if (!chan) {
  888. astman_send_error(s, m, "Channel does not exist.");
  889. return 0;
  890. }
  891. p = ast_channel_tech_pvt(chan);
  892. ast_channel_unref(chan);
  893. found = p ? ao2_find(locals, p, 0) : NULL;
  894. if (found) {
  895. ao2_lock(found);
  896. ast_clear_flag(&found->base, AST_UNREAL_NO_OPTIMIZATION);
  897. ao2_unlock(found);
  898. ao2_ref(found, -1);
  899. astman_send_ack(s, m, "Queued channel to be optimized away");
  900. } else {
  901. astman_send_error(s, m, "Unable to find channel");
  902. }
  903. return 0;
  904. }
  905. static int locals_cmp_cb(void *obj, void *arg, int flags)
  906. {
  907. return (obj == arg) ? CMP_MATCH : 0;
  908. }
  909. /*!
  910. * \internal
  911. * \brief Shutdown the local proxy channel.
  912. * \since 12.0.0
  913. *
  914. * \return Nothing
  915. */
  916. static void local_shutdown(void)
  917. {
  918. /* First, take us out of the channel loop */
  919. ast_cli_unregister_multiple(cli_local, ARRAY_LEN(cli_local));
  920. ast_manager_unregister("LocalOptimizeAway");
  921. ast_channel_unregister(&local_tech);
  922. ao2_ref(locals, -1);
  923. locals = NULL;
  924. ao2_cleanup(local_tech.capabilities);
  925. local_tech.capabilities = NULL;
  926. STASIS_MESSAGE_TYPE_CLEANUP(ast_local_optimization_begin_type);
  927. STASIS_MESSAGE_TYPE_CLEANUP(ast_local_optimization_end_type);
  928. STASIS_MESSAGE_TYPE_CLEANUP(ast_local_bridge_type);
  929. }
  930. int ast_local_init(void)
  931. {
  932. if (STASIS_MESSAGE_TYPE_INIT(ast_local_optimization_begin_type)) {
  933. return -1;
  934. }
  935. if (STASIS_MESSAGE_TYPE_INIT(ast_local_optimization_end_type)) {
  936. return -1;
  937. }
  938. if (STASIS_MESSAGE_TYPE_INIT(ast_local_bridge_type)) {
  939. return -1;
  940. }
  941. if (!(local_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  942. return -1;
  943. }
  944. ast_format_cap_append_by_type(local_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
  945. locals = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, locals_cmp_cb);
  946. if (!locals) {
  947. return -1;
  948. }
  949. /* Make sure we can register our channel type */
  950. if (ast_channel_register(&local_tech)) {
  951. ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
  952. return -1;
  953. }
  954. ast_cli_register_multiple(cli_local, ARRAY_LEN(cli_local));
  955. ast_manager_register_xml_core("LocalOptimizeAway", EVENT_FLAG_SYSTEM|EVENT_FLAG_CALL, manager_optimize_away);
  956. ast_register_cleanup(local_shutdown);
  957. return 0;
  958. }