res_stasis.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@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. /*! \file
  19. *
  20. * \brief Stasis application support.
  21. *
  22. * \author David M. Lee, II <dlee@digium.com>
  23. *
  24. * <code>res_stasis.so</code> brings together the various components of the
  25. * Stasis application infrastructure.
  26. *
  27. * First, there's the Stasis application handler, stasis_app_exec(). This is
  28. * called by <code>app_stasis.so</code> to give control of a channel to the
  29. * Stasis application code from the dialplan.
  30. *
  31. * While a channel is in stasis_app_exec(), it has a \ref stasis_app_control
  32. * object, which may be used to control the channel.
  33. *
  34. * To control the channel, commands may be sent to channel using
  35. * stasis_app_send_command() and stasis_app_send_async_command().
  36. *
  37. * Alongside this, applications may be registered/unregistered using
  38. * stasis_app_register()/stasis_app_unregister(). While a channel is in Stasis,
  39. * events received on the channel's topic are converted to JSON and forwarded to
  40. * the \ref stasis_app_cb. The application may also subscribe to the channel to
  41. * continue to receive messages even after the channel has left Stasis, but it
  42. * will not be able to control it.
  43. *
  44. * Given all the stuff that comes together in this module, it's been broken up
  45. * into several pieces that are in <code>res/stasis/</code> and compiled into
  46. * <code>res_stasis.so</code>.
  47. */
  48. /*** MODULEINFO
  49. <support_level>core</support_level>
  50. ***/
  51. #include "asterisk.h"
  52. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  53. #include "asterisk/astobj2.h"
  54. #include "asterisk/callerid.h"
  55. #include "asterisk/module.h"
  56. #include "asterisk/stasis_app_impl.h"
  57. #include "asterisk/stasis_channels.h"
  58. #include "asterisk/stasis_bridges.h"
  59. #include "asterisk/stasis_endpoints.h"
  60. #include "asterisk/stasis_message_router.h"
  61. #include "asterisk/strings.h"
  62. #include "stasis/app.h"
  63. #include "stasis/control.h"
  64. #include "stasis/messaging.h"
  65. #include "stasis/stasis_bridge.h"
  66. #include "asterisk/core_unreal.h"
  67. #include "asterisk/musiconhold.h"
  68. #include "asterisk/causes.h"
  69. #include "asterisk/stringfields.h"
  70. #include "asterisk/bridge_after.h"
  71. #include "asterisk/format_cache.h"
  72. /*! Time to wait for a frame in the application */
  73. #define MAX_WAIT_MS 200
  74. /*!
  75. * \brief Number of buckets for the Stasis application hash table. Remember to
  76. * keep it a prime number!
  77. */
  78. #define APPS_NUM_BUCKETS 127
  79. /*!
  80. * \brief Number of buckets for the Stasis application hash table. Remember to
  81. * keep it a prime number!
  82. */
  83. #define CONTROLS_NUM_BUCKETS 127
  84. /*!
  85. * \brief Number of buckets for the Stasis bridges hash table. Remember to
  86. * keep it a prime number!
  87. */
  88. #define BRIDGES_NUM_BUCKETS 127
  89. /*!
  90. * \brief Stasis application container.
  91. */
  92. struct ao2_container *apps_registry;
  93. struct ao2_container *app_controls;
  94. struct ao2_container *app_bridges;
  95. struct ao2_container *app_bridges_moh;
  96. struct ao2_container *app_bridges_playback;
  97. static struct ast_json *stasis_end_to_json(struct stasis_message *message,
  98. const struct stasis_message_sanitizer *sanitize)
  99. {
  100. struct ast_channel_blob *payload = stasis_message_data(message);
  101. if (sanitize && sanitize->channel_snapshot &&
  102. sanitize->channel_snapshot(payload->snapshot)) {
  103. return NULL;
  104. }
  105. return ast_json_pack("{s: s, s: o, s: o}",
  106. "type", "StasisEnd",
  107. "timestamp", ast_json_timeval(ast_tvnow(), NULL),
  108. "channel", ast_channel_snapshot_to_json(payload->snapshot, sanitize));
  109. }
  110. STASIS_MESSAGE_TYPE_DEFN_LOCAL(end_message_type,
  111. .to_json = stasis_end_to_json);
  112. struct start_message_blob {
  113. struct ast_channel_snapshot *channel; /*!< Channel that is entering Stasis() */
  114. struct ast_channel_snapshot *replace_channel; /*!< Channel that is being replaced (optional) */
  115. struct ast_json *blob; /*!< JSON blob containing timestamp and args */
  116. };
  117. static struct ast_json *stasis_start_to_json(struct stasis_message *message,
  118. const struct stasis_message_sanitizer *sanitize)
  119. {
  120. struct start_message_blob *payload = stasis_message_data(message);
  121. struct ast_json *msg;
  122. if (sanitize && sanitize->channel_snapshot &&
  123. sanitize->channel_snapshot(payload->channel)) {
  124. return NULL;
  125. }
  126. msg = ast_json_pack("{s: s, s: O, s: O, s: o}",
  127. "type", "StasisStart",
  128. "timestamp", ast_json_object_get(payload->blob, "timestamp"),
  129. "args", ast_json_object_get(payload->blob, "args"),
  130. "channel", ast_channel_snapshot_to_json(payload->channel, NULL));
  131. if (!msg) {
  132. ast_log(LOG_ERROR, "Failed to pack JSON for StasisStart message\n");
  133. return NULL;
  134. }
  135. if (payload->replace_channel) {
  136. int res = ast_json_object_set(msg, "replace_channel",
  137. ast_channel_snapshot_to_json(payload->replace_channel, NULL));
  138. if (res) {
  139. ast_json_unref(msg);
  140. ast_log(LOG_ERROR, "Failed to append JSON for StasisStart message\n");
  141. return NULL;
  142. }
  143. }
  144. return msg;
  145. }
  146. STASIS_MESSAGE_TYPE_DEFN_LOCAL(start_message_type,
  147. .to_json = stasis_start_to_json);
  148. const char *stasis_app_name(const struct stasis_app *app)
  149. {
  150. return app_name(app);
  151. }
  152. /*! AO2 hash function for \ref app */
  153. static int app_hash(const void *obj, const int flags)
  154. {
  155. const struct stasis_app *app;
  156. const char *key;
  157. switch (flags & OBJ_SEARCH_MASK) {
  158. case OBJ_SEARCH_KEY:
  159. key = obj;
  160. break;
  161. case OBJ_SEARCH_OBJECT:
  162. app = obj;
  163. key = stasis_app_name(app);
  164. break;
  165. default:
  166. /* Hash can only work on something with a full key. */
  167. ast_assert(0);
  168. return 0;
  169. }
  170. return ast_str_hash(key);
  171. }
  172. /*! AO2 comparison function for \ref app */
  173. static int app_compare(void *obj, void *arg, int flags)
  174. {
  175. const struct stasis_app *object_left = obj;
  176. const struct stasis_app *object_right = arg;
  177. const char *right_key = arg;
  178. int cmp;
  179. switch (flags & OBJ_SEARCH_MASK) {
  180. case OBJ_SEARCH_OBJECT:
  181. right_key = stasis_app_name(object_right);
  182. /* Fall through */
  183. case OBJ_SEARCH_KEY:
  184. cmp = strcmp(stasis_app_name(object_left), right_key);
  185. break;
  186. case OBJ_SEARCH_PARTIAL_KEY:
  187. /*
  188. * We could also use a partial key struct containing a length
  189. * so strlen() does not get called for every comparison instead.
  190. */
  191. cmp = strncmp(stasis_app_name(object_left), right_key, strlen(right_key));
  192. break;
  193. default:
  194. /*
  195. * What arg points to is specific to this traversal callback
  196. * and has no special meaning to astobj2.
  197. */
  198. cmp = 0;
  199. break;
  200. }
  201. if (cmp) {
  202. return 0;
  203. }
  204. /*
  205. * At this point the traversal callback is identical to a sorted
  206. * container.
  207. */
  208. return CMP_MATCH;
  209. }
  210. /*! AO2 hash function for \ref stasis_app_control */
  211. static int control_hash(const void *obj, const int flags)
  212. {
  213. const struct stasis_app_control *control;
  214. const char *key;
  215. switch (flags & OBJ_SEARCH_MASK) {
  216. case OBJ_SEARCH_KEY:
  217. key = obj;
  218. break;
  219. case OBJ_SEARCH_OBJECT:
  220. control = obj;
  221. key = stasis_app_control_get_channel_id(control);
  222. break;
  223. default:
  224. /* Hash can only work on something with a full key. */
  225. ast_assert(0);
  226. return 0;
  227. }
  228. return ast_str_hash(key);
  229. }
  230. /*! AO2 comparison function for \ref stasis_app_control */
  231. static int control_compare(void *obj, void *arg, int flags)
  232. {
  233. const struct stasis_app_control *object_left = obj;
  234. const struct stasis_app_control *object_right = arg;
  235. const char *right_key = arg;
  236. int cmp;
  237. switch (flags & OBJ_SEARCH_MASK) {
  238. case OBJ_SEARCH_OBJECT:
  239. right_key = stasis_app_control_get_channel_id(object_right);
  240. /* Fall through */
  241. case OBJ_SEARCH_KEY:
  242. cmp = strcmp(stasis_app_control_get_channel_id(object_left), right_key);
  243. break;
  244. case OBJ_SEARCH_PARTIAL_KEY:
  245. /*
  246. * We could also use a partial key struct containing a length
  247. * so strlen() does not get called for every comparison instead.
  248. */
  249. cmp = strncmp(stasis_app_control_get_channel_id(object_left), right_key, strlen(right_key));
  250. break;
  251. default:
  252. /*
  253. * What arg points to is specific to this traversal callback
  254. * and has no special meaning to astobj2.
  255. */
  256. cmp = 0;
  257. break;
  258. }
  259. if (cmp) {
  260. return 0;
  261. }
  262. /*
  263. * At this point the traversal callback is identical to a sorted
  264. * container.
  265. */
  266. return CMP_MATCH;
  267. }
  268. static int cleanup_cb(void *obj, void *arg, int flags)
  269. {
  270. struct stasis_app *app = obj;
  271. if (!app_is_finished(app)) {
  272. return 0;
  273. }
  274. ast_verb(1, "Shutting down application '%s'\n", stasis_app_name(app));
  275. app_shutdown(app);
  276. return CMP_MATCH;
  277. }
  278. /*!
  279. * \brief Clean up any old apps that we don't need any more.
  280. */
  281. static void cleanup(void)
  282. {
  283. ao2_callback(apps_registry, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK,
  284. cleanup_cb, NULL);
  285. }
  286. struct stasis_app_control *stasis_app_control_create(struct ast_channel *chan)
  287. {
  288. return control_create(chan, NULL);
  289. }
  290. struct stasis_app_control *stasis_app_control_find_by_channel(
  291. const struct ast_channel *chan)
  292. {
  293. if (chan == NULL) {
  294. return NULL;
  295. }
  296. return stasis_app_control_find_by_channel_id(
  297. ast_channel_uniqueid(chan));
  298. }
  299. struct stasis_app_control *stasis_app_control_find_by_channel_id(
  300. const char *channel_id)
  301. {
  302. return ao2_find(app_controls, channel_id, OBJ_SEARCH_KEY);
  303. }
  304. /*! AO2 hash function for bridges container */
  305. static int bridges_hash(const void *obj, const int flags)
  306. {
  307. const struct ast_bridge *bridge;
  308. const char *key;
  309. switch (flags & OBJ_SEARCH_MASK) {
  310. case OBJ_SEARCH_KEY:
  311. key = obj;
  312. break;
  313. case OBJ_SEARCH_OBJECT:
  314. bridge = obj;
  315. key = bridge->uniqueid;
  316. break;
  317. default:
  318. /* Hash can only work on something with a full key. */
  319. ast_assert(0);
  320. return 0;
  321. }
  322. return ast_str_hash(key);
  323. }
  324. /*! AO2 comparison function for bridges container */
  325. static int bridges_compare(void *obj, void *arg, int flags)
  326. {
  327. const struct ast_bridge *object_left = obj;
  328. const struct ast_bridge *object_right = arg;
  329. const char *right_key = arg;
  330. int cmp;
  331. switch (flags & OBJ_SEARCH_MASK) {
  332. case OBJ_SEARCH_OBJECT:
  333. right_key = object_right->uniqueid;
  334. /* Fall through */
  335. case OBJ_SEARCH_KEY:
  336. cmp = strcmp(object_left->uniqueid, right_key);
  337. break;
  338. case OBJ_SEARCH_PARTIAL_KEY:
  339. /*
  340. * We could also use a partial key struct containing a length
  341. * so strlen() does not get called for every comparison instead.
  342. */
  343. cmp = strncmp(object_left->uniqueid, right_key, strlen(right_key));
  344. break;
  345. default:
  346. /*
  347. * What arg points to is specific to this traversal callback
  348. * and has no special meaning to astobj2.
  349. */
  350. cmp = 0;
  351. break;
  352. }
  353. if (cmp) {
  354. return 0;
  355. }
  356. /*
  357. * At this point the traversal callback is identical to a sorted
  358. * container.
  359. */
  360. return CMP_MATCH;
  361. }
  362. /*!
  363. * Used with app_bridges_moh and app_bridge_control, they provide links
  364. * between bridges and channels used for ARI application purposes
  365. */
  366. struct stasis_app_bridge_channel_wrapper {
  367. AST_DECLARE_STRING_FIELDS(
  368. AST_STRING_FIELD(channel_id);
  369. AST_STRING_FIELD(bridge_id);
  370. );
  371. };
  372. static void stasis_app_bridge_channel_wrapper_destructor(void *obj)
  373. {
  374. struct stasis_app_bridge_channel_wrapper *wrapper = obj;
  375. ast_string_field_free_memory(wrapper);
  376. }
  377. /*! AO2 hash function for the bridges moh container */
  378. static int bridges_channel_hash_fn(const void *obj, const int flags)
  379. {
  380. const struct stasis_app_bridge_channel_wrapper *wrapper;
  381. const char *key;
  382. switch (flags & OBJ_SEARCH_MASK) {
  383. case OBJ_SEARCH_KEY:
  384. key = obj;
  385. break;
  386. case OBJ_SEARCH_OBJECT:
  387. wrapper = obj;
  388. key = wrapper->bridge_id;
  389. break;
  390. default:
  391. /* Hash can only work on something with a full key. */
  392. ast_assert(0);
  393. return 0;
  394. }
  395. return ast_str_hash(key);
  396. }
  397. static int bridges_channel_sort_fn(const void *obj_left, const void *obj_right, const int flags)
  398. {
  399. const struct stasis_app_bridge_channel_wrapper *left = obj_left;
  400. const struct stasis_app_bridge_channel_wrapper *right = obj_right;
  401. const char *right_key = obj_right;
  402. int cmp;
  403. switch (flags & OBJ_SEARCH_MASK) {
  404. case OBJ_SEARCH_OBJECT:
  405. right_key = right->bridge_id;
  406. /* Fall through */
  407. case OBJ_SEARCH_KEY:
  408. cmp = strcmp(left->bridge_id, right_key);
  409. break;
  410. case OBJ_SEARCH_PARTIAL_KEY:
  411. cmp = strncmp(left->bridge_id, right_key, strlen(right_key));
  412. break;
  413. default:
  414. /* Sort can only work on something with a full or partial key. */
  415. ast_assert(0);
  416. cmp = 0;
  417. break;
  418. }
  419. return cmp;
  420. }
  421. /*! Removes the bridge to music on hold channel link */
  422. static void remove_bridge_moh(char *bridge_id)
  423. {
  424. ao2_find(app_bridges_moh, bridge_id, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
  425. ast_free(bridge_id);
  426. }
  427. /*! After bridge failure callback for moh channels */
  428. static void moh_after_bridge_cb_failed(enum ast_bridge_after_cb_reason reason, void *data)
  429. {
  430. char *bridge_id = data;
  431. remove_bridge_moh(bridge_id);
  432. }
  433. /*! After bridge callback for moh channels */
  434. static void moh_after_bridge_cb(struct ast_channel *chan, void *data)
  435. {
  436. char *bridge_id = data;
  437. remove_bridge_moh(bridge_id);
  438. }
  439. /*! Request a bridge MOH channel */
  440. static struct ast_channel *prepare_bridge_moh_channel(void)
  441. {
  442. RAII_VAR(struct ast_format_cap *, cap, NULL, ao2_cleanup);
  443. cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  444. if (!cap) {
  445. return NULL;
  446. }
  447. ast_format_cap_append(cap, ast_format_slin, 0);
  448. return ast_request("Announcer", cap, NULL, NULL, "ARI_MOH", NULL);
  449. }
  450. /*! Provides the moh channel with a thread so it can actually play its music */
  451. static void *moh_channel_thread(void *data)
  452. {
  453. struct ast_channel *moh_channel = data;
  454. while (!ast_safe_sleep(moh_channel, 1000)) {
  455. }
  456. ast_moh_stop(moh_channel);
  457. ast_hangup(moh_channel);
  458. return NULL;
  459. }
  460. /*!
  461. * \internal
  462. * \brief Creates, pushes, and links a channel for playing music on hold to bridge
  463. *
  464. * \param bridge Which bridge this moh channel exists for
  465. *
  466. * \retval NULL if the channel could not be created, pushed, or linked
  467. * \retval Reference to the channel on success
  468. */
  469. static struct ast_channel *bridge_moh_create(struct ast_bridge *bridge)
  470. {
  471. RAII_VAR(struct stasis_app_bridge_channel_wrapper *, new_wrapper, NULL, ao2_cleanup);
  472. RAII_VAR(char *, bridge_id, ast_strdup(bridge->uniqueid), ast_free);
  473. struct ast_channel *chan;
  474. pthread_t threadid;
  475. if (!bridge_id) {
  476. return NULL;
  477. }
  478. chan = prepare_bridge_moh_channel();
  479. if (!chan) {
  480. return NULL;
  481. }
  482. if (stasis_app_channel_unreal_set_internal(chan)) {
  483. ast_hangup(chan);
  484. return NULL;
  485. }
  486. /* The after bridge callback assumes responsibility of the bridge_id. */
  487. if (ast_bridge_set_after_callback(chan,
  488. moh_after_bridge_cb, moh_after_bridge_cb_failed, bridge_id)) {
  489. ast_hangup(chan);
  490. return NULL;
  491. }
  492. bridge_id = NULL;
  493. if (ast_unreal_channel_push_to_bridge(chan, bridge,
  494. AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE | AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
  495. ast_hangup(chan);
  496. return NULL;
  497. }
  498. new_wrapper = ao2_alloc_options(sizeof(*new_wrapper),
  499. stasis_app_bridge_channel_wrapper_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  500. if (!new_wrapper) {
  501. ast_hangup(chan);
  502. return NULL;
  503. }
  504. if (ast_string_field_init(new_wrapper, 32)) {
  505. ast_hangup(chan);
  506. return NULL;
  507. }
  508. ast_string_field_set(new_wrapper, bridge_id, bridge->uniqueid);
  509. ast_string_field_set(new_wrapper, channel_id, ast_channel_uniqueid(chan));
  510. if (!ao2_link_flags(app_bridges_moh, new_wrapper, OBJ_NOLOCK)) {
  511. ast_hangup(chan);
  512. return NULL;
  513. }
  514. if (ast_pthread_create_detached(&threadid, NULL, moh_channel_thread, chan)) {
  515. ast_log(LOG_ERROR, "Failed to create channel thread. Abandoning MOH channel creation.\n");
  516. ao2_unlink_flags(app_bridges_moh, new_wrapper, OBJ_NOLOCK);
  517. ast_hangup(chan);
  518. return NULL;
  519. }
  520. return chan;
  521. }
  522. struct ast_channel *stasis_app_bridge_moh_channel(struct ast_bridge *bridge)
  523. {
  524. RAII_VAR(struct stasis_app_bridge_channel_wrapper *, moh_wrapper, NULL, ao2_cleanup);
  525. {
  526. SCOPED_AO2LOCK(lock, app_bridges_moh);
  527. moh_wrapper = ao2_find(app_bridges_moh, bridge->uniqueid, OBJ_SEARCH_KEY | OBJ_NOLOCK);
  528. if (!moh_wrapper) {
  529. return bridge_moh_create(bridge);
  530. }
  531. }
  532. return ast_channel_get_by_name(moh_wrapper->channel_id);
  533. }
  534. int stasis_app_bridge_moh_stop(struct ast_bridge *bridge)
  535. {
  536. RAII_VAR(struct stasis_app_bridge_channel_wrapper *, moh_wrapper, NULL, ao2_cleanup);
  537. struct ast_channel *chan;
  538. moh_wrapper = ao2_find(app_bridges_moh, bridge->uniqueid, OBJ_SEARCH_KEY | OBJ_UNLINK);
  539. if (!moh_wrapper) {
  540. return -1;
  541. }
  542. chan = ast_channel_get_by_name(moh_wrapper->channel_id);
  543. if (!chan) {
  544. return -1;
  545. }
  546. ast_moh_stop(chan);
  547. ast_softhangup(chan, AST_CAUSE_NORMAL_CLEARING);
  548. ao2_cleanup(chan);
  549. return 0;
  550. }
  551. /*! Removes the bridge to playback channel link */
  552. static void remove_bridge_playback(char *bridge_id)
  553. {
  554. struct stasis_app_bridge_channel_wrapper *wrapper;
  555. struct stasis_app_control *control;
  556. wrapper = ao2_find(app_bridges_playback, bridge_id, OBJ_SEARCH_KEY | OBJ_UNLINK);
  557. if (wrapper) {
  558. control = stasis_app_control_find_by_channel_id(wrapper->channel_id);
  559. if (control) {
  560. ao2_unlink(app_controls, control);
  561. ao2_ref(control, -1);
  562. }
  563. ao2_ref(wrapper, -1);
  564. }
  565. ast_free(bridge_id);
  566. }
  567. static void playback_after_bridge_cb_failed(enum ast_bridge_after_cb_reason reason, void *data)
  568. {
  569. char *bridge_id = data;
  570. remove_bridge_playback(bridge_id);
  571. }
  572. static void playback_after_bridge_cb(struct ast_channel *chan, void *data)
  573. {
  574. char *bridge_id = data;
  575. remove_bridge_playback(bridge_id);
  576. }
  577. int stasis_app_bridge_playback_channel_add(struct ast_bridge *bridge,
  578. struct ast_channel *chan,
  579. struct stasis_app_control *control)
  580. {
  581. RAII_VAR(struct stasis_app_bridge_channel_wrapper *, new_wrapper, NULL, ao2_cleanup);
  582. char *bridge_id = ast_strdup(bridge->uniqueid);
  583. if (!bridge_id) {
  584. return -1;
  585. }
  586. if (ast_bridge_set_after_callback(chan,
  587. playback_after_bridge_cb, playback_after_bridge_cb_failed, bridge_id)) {
  588. ast_free(bridge_id);
  589. return -1;
  590. }
  591. new_wrapper = ao2_alloc_options(sizeof(*new_wrapper),
  592. stasis_app_bridge_channel_wrapper_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  593. if (!new_wrapper) {
  594. return -1;
  595. }
  596. if (ast_string_field_init(new_wrapper, 32)) {
  597. return -1;
  598. }
  599. ast_string_field_set(new_wrapper, bridge_id, bridge->uniqueid);
  600. ast_string_field_set(new_wrapper, channel_id, ast_channel_uniqueid(chan));
  601. if (!ao2_link(app_bridges_playback, new_wrapper)) {
  602. return -1;
  603. }
  604. ao2_link(app_controls, control);
  605. return 0;
  606. }
  607. struct ast_channel *stasis_app_bridge_playback_channel_find(struct ast_bridge *bridge)
  608. {
  609. struct stasis_app_bridge_channel_wrapper *playback_wrapper;
  610. struct ast_channel *chan;
  611. playback_wrapper = ao2_find(app_bridges_playback, bridge->uniqueid, OBJ_SEARCH_KEY);
  612. if (!playback_wrapper) {
  613. return NULL;
  614. }
  615. chan = ast_channel_get_by_name(playback_wrapper->channel_id);
  616. ao2_ref(playback_wrapper, -1);
  617. return chan;
  618. }
  619. struct ast_bridge *stasis_app_bridge_find_by_id(
  620. const char *bridge_id)
  621. {
  622. return ao2_find(app_bridges, bridge_id, OBJ_SEARCH_KEY);
  623. }
  624. /*!
  625. * \brief In addition to running ao2_cleanup(), this function also removes the
  626. * object from the app_controls container.
  627. */
  628. static void control_unlink(struct stasis_app_control *control)
  629. {
  630. if (!control) {
  631. return;
  632. }
  633. ao2_unlink(app_controls, control);
  634. ao2_cleanup(control);
  635. }
  636. struct ast_bridge *stasis_app_bridge_create(const char *type, const char *name, const char *id)
  637. {
  638. struct ast_bridge *bridge;
  639. char *requested_type, *requested_types = ast_strdupa(S_OR(type, "mixing"));
  640. int capabilities = 0;
  641. int flags = AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM | AST_BRIDGE_FLAG_MERGE_INHIBIT_TO
  642. | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO
  643. | AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY;
  644. while ((requested_type = strsep(&requested_types, ","))) {
  645. requested_type = ast_strip(requested_type);
  646. if (!strcmp(requested_type, "mixing")) {
  647. capabilities |= STASIS_BRIDGE_MIXING_CAPABILITIES;
  648. flags |= AST_BRIDGE_FLAG_SMART;
  649. } else if (!strcmp(requested_type, "holding")) {
  650. capabilities |= AST_BRIDGE_CAPABILITY_HOLDING;
  651. } else if (!strcmp(requested_type, "dtmf_events") ||
  652. !strcmp(requested_type, "proxy_media")) {
  653. capabilities &= ~AST_BRIDGE_CAPABILITY_NATIVE;
  654. }
  655. }
  656. if (!capabilities
  657. /* Holding and mixing capabilities don't mix. */
  658. || ((capabilities & AST_BRIDGE_CAPABILITY_HOLDING)
  659. && (capabilities & (STASIS_BRIDGE_MIXING_CAPABILITIES)))) {
  660. return NULL;
  661. }
  662. bridge = bridge_stasis_new(capabilities, flags, name, id);
  663. if (bridge) {
  664. if (!ao2_link(app_bridges, bridge)) {
  665. ast_bridge_destroy(bridge, 0);
  666. bridge = NULL;
  667. }
  668. }
  669. return bridge;
  670. }
  671. void stasis_app_bridge_destroy(const char *bridge_id)
  672. {
  673. struct ast_bridge *bridge = stasis_app_bridge_find_by_id(bridge_id);
  674. if (!bridge) {
  675. return;
  676. }
  677. ao2_unlink(app_bridges, bridge);
  678. ast_bridge_destroy(bridge, 0);
  679. }
  680. struct replace_channel_store {
  681. struct ast_channel_snapshot *snapshot;
  682. char *app;
  683. };
  684. static void replace_channel_destroy(void *obj)
  685. {
  686. struct replace_channel_store *replace = obj;
  687. ao2_cleanup(replace->snapshot);
  688. ast_free(replace->app);
  689. ast_free(replace);
  690. }
  691. static const struct ast_datastore_info replace_channel_store_info = {
  692. .type = "replace-channel-store",
  693. .destroy = replace_channel_destroy,
  694. };
  695. static struct replace_channel_store *get_replace_channel_store(struct ast_channel *chan, int no_create)
  696. {
  697. struct ast_datastore *datastore;
  698. SCOPED_CHANNELLOCK(lock, chan);
  699. datastore = ast_channel_datastore_find(chan, &replace_channel_store_info, NULL);
  700. if (!datastore) {
  701. if (no_create) {
  702. return NULL;
  703. }
  704. datastore = ast_datastore_alloc(&replace_channel_store_info, NULL);
  705. if (!datastore) {
  706. return NULL;
  707. }
  708. ast_channel_datastore_add(chan, datastore);
  709. }
  710. if (!datastore->data) {
  711. datastore->data = ast_calloc(1, sizeof(struct replace_channel_store));
  712. }
  713. return datastore->data;
  714. }
  715. int app_set_replace_channel_snapshot(struct ast_channel *chan, struct ast_channel_snapshot *replace_snapshot)
  716. {
  717. struct replace_channel_store *replace = get_replace_channel_store(chan, 0);
  718. if (!replace) {
  719. return -1;
  720. }
  721. ao2_replace(replace->snapshot, replace_snapshot);
  722. return 0;
  723. }
  724. int app_set_replace_channel_app(struct ast_channel *chan, const char *replace_app)
  725. {
  726. struct replace_channel_store *replace = get_replace_channel_store(chan, 0);
  727. if (!replace) {
  728. return -1;
  729. }
  730. ast_free(replace->app);
  731. replace->app = NULL;
  732. if (replace_app) {
  733. replace->app = ast_strdup(replace_app);
  734. if (!replace->app) {
  735. return -1;
  736. }
  737. }
  738. return 0;
  739. }
  740. static struct ast_channel_snapshot *get_replace_channel_snapshot(struct ast_channel *chan)
  741. {
  742. struct replace_channel_store *replace = get_replace_channel_store(chan, 1);
  743. struct ast_channel_snapshot *replace_channel_snapshot;
  744. if (!replace) {
  745. return NULL;
  746. }
  747. replace_channel_snapshot = replace->snapshot;
  748. replace->snapshot = NULL;
  749. return replace_channel_snapshot;
  750. }
  751. char *app_get_replace_channel_app(struct ast_channel *chan)
  752. {
  753. struct replace_channel_store *replace = get_replace_channel_store(chan, 1);
  754. char *replace_channel_app;
  755. if (!replace) {
  756. return NULL;
  757. }
  758. replace_channel_app = replace->app;
  759. replace->app = NULL;
  760. return replace_channel_app;
  761. }
  762. static void start_message_blob_dtor(void *obj)
  763. {
  764. struct start_message_blob *payload = obj;
  765. ao2_cleanup(payload->channel);
  766. ao2_cleanup(payload->replace_channel);
  767. ast_json_unref(payload->blob);
  768. }
  769. static int send_start_msg_snapshots(struct ast_channel *chan, struct stasis_app *app,
  770. int argc, char *argv[], struct ast_channel_snapshot *snapshot,
  771. struct ast_channel_snapshot *replace_channel_snapshot)
  772. {
  773. RAII_VAR(struct ast_json *, json_blob, NULL, ast_json_unref);
  774. struct ast_json *json_args;
  775. RAII_VAR(struct start_message_blob *, payload, NULL, ao2_cleanup);
  776. struct stasis_message *msg;
  777. int i;
  778. if (app_subscribe_channel(app, chan)) {
  779. ast_log(LOG_ERROR, "Error subscribing app '%s' to channel '%s'\n",
  780. app_name(app), ast_channel_name(chan));
  781. return -1;
  782. }
  783. payload = ao2_alloc(sizeof(*payload), start_message_blob_dtor);
  784. if (!payload) {
  785. ast_log(LOG_ERROR, "Error packing JSON for StasisStart message\n");
  786. return -1;
  787. }
  788. payload->channel = ao2_bump(snapshot);
  789. payload->replace_channel = ao2_bump(replace_channel_snapshot);
  790. json_blob = ast_json_pack("{s: s, s: o, s: []}",
  791. "app", app_name(app),
  792. "timestamp", ast_json_timeval(ast_tvnow(), NULL),
  793. "args");
  794. if (!json_blob) {
  795. ast_log(LOG_ERROR, "Error packing JSON for StasisStart message\n");
  796. return -1;
  797. }
  798. /* Append arguments to args array */
  799. json_args = ast_json_object_get(json_blob, "args");
  800. ast_assert(json_args != NULL);
  801. for (i = 0; i < argc; ++i) {
  802. int r = ast_json_array_append(json_args,
  803. ast_json_string_create(argv[i]));
  804. if (r != 0) {
  805. ast_log(LOG_ERROR, "Error appending to StasisStart message\n");
  806. return -1;
  807. }
  808. }
  809. payload->blob = ast_json_ref(json_blob);
  810. msg = stasis_message_create(start_message_type(), payload);
  811. if (!msg) {
  812. ast_log(LOG_ERROR, "Error sending StasisStart message\n");
  813. return -1;
  814. }
  815. if (replace_channel_snapshot) {
  816. app_unsubscribe_channel_id(app, replace_channel_snapshot->uniqueid);
  817. }
  818. stasis_publish(ast_app_get_topic(app), msg);
  819. ao2_ref(msg, -1);
  820. return 0;
  821. }
  822. static int send_start_msg(struct stasis_app *app, struct ast_channel *chan,
  823. int argc, char *argv[])
  824. {
  825. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  826. RAII_VAR(struct ast_channel_snapshot *, replace_channel_snapshot,
  827. NULL, ao2_cleanup);
  828. ast_assert(chan != NULL);
  829. replace_channel_snapshot = get_replace_channel_snapshot(chan);
  830. /* Set channel info */
  831. ast_channel_lock(chan);
  832. snapshot = ast_channel_snapshot_create(chan);
  833. ast_channel_unlock(chan);
  834. if (!snapshot) {
  835. return -1;
  836. }
  837. return send_start_msg_snapshots(chan, app, argc, argv, snapshot, replace_channel_snapshot);
  838. }
  839. static void remove_masquerade_store(struct ast_channel *chan);
  840. int app_send_end_msg(struct stasis_app *app, struct ast_channel *chan)
  841. {
  842. struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
  843. struct ast_json *blob;
  844. struct stasis_message *msg;
  845. if (sanitize && sanitize->channel
  846. && sanitize->channel(chan)) {
  847. return 0;
  848. }
  849. blob = ast_json_pack("{s: s}", "app", app_name(app));
  850. if (!blob) {
  851. ast_log(LOG_ERROR, "Error packing JSON for StasisEnd message\n");
  852. return -1;
  853. }
  854. remove_masquerade_store(chan);
  855. app_unsubscribe_channel(app, chan);
  856. msg = ast_channel_blob_create(chan, end_message_type(), blob);
  857. if (msg) {
  858. stasis_publish(ast_app_get_topic(app), msg);
  859. }
  860. ao2_cleanup(msg);
  861. ast_json_unref(blob);
  862. return 0;
  863. }
  864. static int masq_match_cb(void *obj, void *data, int flags)
  865. {
  866. struct stasis_app_control *control = obj;
  867. struct ast_channel *chan = data;
  868. if (!strcmp(ast_channel_uniqueid(chan),
  869. stasis_app_control_get_channel_id(control))) {
  870. return CMP_MATCH;
  871. }
  872. return 0;
  873. }
  874. static void channel_stolen_cb(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  875. {
  876. struct stasis_app_control *control;
  877. /* find control */
  878. control = ao2_callback(app_controls, 0, masq_match_cb, old_chan);
  879. if (!control) {
  880. ast_log(LOG_ERROR, "Could not find control for masqueraded channel\n");
  881. return;
  882. }
  883. /* send the StasisEnd message to the app */
  884. stasis_app_channel_set_stasis_end_published(new_chan);
  885. app_send_end_msg(control_app(control), new_chan);
  886. /* remove the datastore */
  887. remove_masquerade_store(old_chan);
  888. ao2_cleanup(control);
  889. }
  890. static void channel_replaced_cb(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  891. {
  892. RAII_VAR(struct ast_channel_snapshot *, new_snapshot, NULL, ao2_cleanup);
  893. RAII_VAR(struct ast_channel_snapshot *, old_snapshot, NULL, ao2_cleanup);
  894. struct stasis_app_control *control;
  895. /* At this point, new_chan is the channel pointer that is in Stasis() and
  896. * has the unknown channel's name in it while old_chan is the channel pointer
  897. * that is not in Stasis(), but has the guts of the channel that Stasis() knows
  898. * about */
  899. /* grab a snapshot for the channel that is jumping into Stasis() */
  900. new_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(new_chan));
  901. if (!new_snapshot) {
  902. ast_log(LOG_ERROR, "Could not get snapshot for masquerading channel\n");
  903. return;
  904. }
  905. /* grab a snapshot for the channel that has been kicked out of Stasis() */
  906. old_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(old_chan));
  907. if (!old_snapshot) {
  908. ast_log(LOG_ERROR, "Could not get snapshot for masqueraded channel\n");
  909. return;
  910. }
  911. /* find, unlink, and relink control since the channel has a new name and
  912. * its hash has likely changed */
  913. control = ao2_callback(app_controls, OBJ_UNLINK, masq_match_cb, new_chan);
  914. if (!control) {
  915. ast_log(LOG_ERROR, "Could not find control for masquerading channel\n");
  916. return;
  917. }
  918. ao2_link(app_controls, control);
  919. /* send the StasisStart with replace_channel to the app */
  920. send_start_msg_snapshots(new_chan, control_app(control), 0, NULL, new_snapshot,
  921. old_snapshot);
  922. /* send the StasisEnd message to the app */
  923. app_send_end_msg(control_app(control), old_chan);
  924. ao2_cleanup(control);
  925. }
  926. static const struct ast_datastore_info masquerade_store_info = {
  927. .type = "stasis-masqerade",
  928. .chan_fixup = channel_stolen_cb,
  929. .chan_breakdown = channel_replaced_cb,
  930. };
  931. static int has_masquerade_store(struct ast_channel *chan)
  932. {
  933. SCOPED_CHANNELLOCK(lock, chan);
  934. return !!ast_channel_datastore_find(chan, &masquerade_store_info, NULL);
  935. }
  936. static int add_masquerade_store(struct ast_channel *chan)
  937. {
  938. struct ast_datastore *datastore;
  939. SCOPED_CHANNELLOCK(lock, chan);
  940. if (ast_channel_datastore_find(chan, &masquerade_store_info, NULL)) {
  941. return 0;
  942. }
  943. datastore = ast_datastore_alloc(&masquerade_store_info, NULL);
  944. if (!datastore) {
  945. return -1;
  946. }
  947. ast_channel_datastore_add(chan, datastore);
  948. return 0;
  949. }
  950. static void remove_masquerade_store(struct ast_channel *chan)
  951. {
  952. struct ast_datastore *datastore;
  953. SCOPED_CHANNELLOCK(lock, chan);
  954. datastore = ast_channel_datastore_find(chan, &masquerade_store_info, NULL);
  955. if (!datastore) {
  956. return;
  957. }
  958. ast_channel_datastore_remove(chan, datastore);
  959. ast_datastore_free(datastore);
  960. }
  961. void stasis_app_control_execute_until_exhausted(struct ast_channel *chan, struct stasis_app_control *control)
  962. {
  963. while (!control_is_done(control)) {
  964. int command_count;
  965. command_count = control_dispatch_all(control, chan);
  966. ao2_lock(control);
  967. if (control_command_count(control)) {
  968. /* If the command queue isn't empty, something added to the queue before it was locked. */
  969. ao2_unlock(control);
  970. continue;
  971. }
  972. if (command_count == 0 || ast_channel_fdno(chan) == -1) {
  973. control_mark_done(control);
  974. ao2_unlock(control);
  975. break;
  976. }
  977. ao2_unlock(control);
  978. }
  979. }
  980. int stasis_app_control_is_done(struct stasis_app_control *control)
  981. {
  982. return control_is_done(control);
  983. }
  984. struct ast_datastore_info set_end_published_info = {
  985. .type = "stasis_end_published",
  986. };
  987. void stasis_app_channel_set_stasis_end_published(struct ast_channel *chan)
  988. {
  989. struct ast_datastore *datastore;
  990. datastore = ast_datastore_alloc(&set_end_published_info, NULL);
  991. ast_channel_lock(chan);
  992. ast_channel_datastore_add(chan, datastore);
  993. ast_channel_unlock(chan);
  994. }
  995. int stasis_app_channel_is_stasis_end_published(struct ast_channel *chan)
  996. {
  997. struct ast_datastore *datastore;
  998. ast_channel_lock(chan);
  999. datastore = ast_channel_datastore_find(chan, &set_end_published_info, NULL);
  1000. ast_channel_unlock(chan);
  1001. return datastore ? 1 : 0;
  1002. }
  1003. static void remove_stasis_end_published(struct ast_channel *chan)
  1004. {
  1005. struct ast_datastore *datastore;
  1006. ast_channel_lock(chan);
  1007. datastore = ast_channel_datastore_find(chan, &set_end_published_info, NULL);
  1008. ast_channel_unlock(chan);
  1009. if (datastore) {
  1010. ast_channel_datastore_remove(chan, datastore);
  1011. ast_datastore_free(datastore);
  1012. }
  1013. }
  1014. /*! /brief Stasis dialplan application callback */
  1015. int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
  1016. char *argv[])
  1017. {
  1018. SCOPED_MODULE_USE(ast_module_info->self);
  1019. RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
  1020. RAII_VAR(struct stasis_app_control *, control, NULL, control_unlink);
  1021. struct ast_bridge *bridge = NULL;
  1022. int res = 0;
  1023. int needs_depart;
  1024. ast_assert(chan != NULL);
  1025. /* Just in case there's a lingering indication that the channel has had a stasis
  1026. * end published on it, remove that now.
  1027. */
  1028. remove_stasis_end_published(chan);
  1029. if (!apps_registry) {
  1030. return -1;
  1031. }
  1032. app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY);
  1033. if (!app) {
  1034. ast_log(LOG_ERROR,
  1035. "Stasis app '%s' not registered\n", app_name);
  1036. return -1;
  1037. }
  1038. if (!app_is_active(app)) {
  1039. ast_log(LOG_ERROR,
  1040. "Stasis app '%s' not active\n", app_name);
  1041. return -1;
  1042. }
  1043. control = control_create(chan, app);
  1044. if (!control) {
  1045. ast_log(LOG_ERROR, "Allocated failed\n");
  1046. return -1;
  1047. }
  1048. ao2_link(app_controls, control);
  1049. if (add_masquerade_store(chan)) {
  1050. ast_log(LOG_ERROR, "Failed to attach masquerade detector\n");
  1051. return -1;
  1052. }
  1053. res = send_start_msg(app, chan, argc, argv);
  1054. if (res != 0) {
  1055. ast_log(LOG_ERROR,
  1056. "Error sending start message to '%s'\n", app_name);
  1057. remove_masquerade_store(chan);
  1058. return -1;
  1059. }
  1060. /* Pull queued prestart commands and execute */
  1061. control_prestart_dispatch_all(control, chan);
  1062. while (!control_is_done(control)) {
  1063. RAII_VAR(struct ast_frame *, f, NULL, ast_frame_dtor);
  1064. int r;
  1065. int command_count;
  1066. RAII_VAR(struct ast_bridge *, last_bridge, NULL, ao2_cleanup);
  1067. /* Check to see if a bridge absorbed our hangup frame */
  1068. if (ast_check_hangup_locked(chan)) {
  1069. break;
  1070. }
  1071. last_bridge = bridge;
  1072. bridge = ao2_bump(stasis_app_get_bridge(control));
  1073. if (bridge != last_bridge) {
  1074. app_unsubscribe_bridge(app, last_bridge);
  1075. app_subscribe_bridge(app, bridge);
  1076. }
  1077. if (bridge) {
  1078. /* Bridge is handling channel frames */
  1079. control_wait(control);
  1080. control_dispatch_all(control, chan);
  1081. continue;
  1082. }
  1083. r = ast_waitfor(chan, MAX_WAIT_MS);
  1084. if (r < 0) {
  1085. ast_debug(3, "%s: Poll error\n",
  1086. ast_channel_uniqueid(chan));
  1087. break;
  1088. }
  1089. command_count = control_dispatch_all(control, chan);
  1090. if (command_count > 0 && ast_channel_fdno(chan) == -1) {
  1091. /* Command drained the channel; wait for next frame */
  1092. continue;
  1093. }
  1094. if (r == 0) {
  1095. /* Timeout */
  1096. continue;
  1097. }
  1098. f = ast_read(chan);
  1099. if (!f) {
  1100. /* Continue on in the dialplan */
  1101. ast_debug(3, "%s: Hangup (no more frames)\n",
  1102. ast_channel_uniqueid(chan));
  1103. break;
  1104. }
  1105. if (f->frametype == AST_FRAME_CONTROL) {
  1106. if (f->subclass.integer == AST_CONTROL_HANGUP) {
  1107. /* Continue on in the dialplan */
  1108. ast_debug(3, "%s: Hangup\n",
  1109. ast_channel_uniqueid(chan));
  1110. break;
  1111. }
  1112. }
  1113. }
  1114. ast_channel_lock(chan);
  1115. needs_depart = ast_channel_is_bridged(chan);
  1116. ast_channel_unlock(chan);
  1117. if (needs_depart) {
  1118. ast_bridge_depart(chan);
  1119. }
  1120. app_unsubscribe_bridge(app, stasis_app_get_bridge(control));
  1121. ao2_cleanup(bridge);
  1122. /* Only publish a stasis_end event if it hasn't already been published */
  1123. if (!stasis_app_channel_is_stasis_end_published(chan)) {
  1124. /* A masquerade has occurred and this message will be wrong so it
  1125. * has already been sent elsewhere. */
  1126. res = has_masquerade_store(chan) && app_send_end_msg(app, chan);
  1127. if (res != 0) {
  1128. ast_log(LOG_ERROR,
  1129. "Error sending end message to %s\n", app_name);
  1130. return res;
  1131. }
  1132. } else {
  1133. remove_stasis_end_published(chan);
  1134. }
  1135. /* There's an off chance that app is ready for cleanup. Go ahead
  1136. * and clean up, just in case
  1137. */
  1138. cleanup();
  1139. /* The control needs to be removed from the controls container in
  1140. * case a new PBX is started and ends up coming back into Stasis.
  1141. */
  1142. ao2_cleanup(app);
  1143. app = NULL;
  1144. control_unlink(control);
  1145. control = NULL;
  1146. if (!ast_check_hangup_locked(chan) && !ast_channel_pbx(chan)) {
  1147. struct ast_pbx_args pbx_args;
  1148. memset(&pbx_args, 0, sizeof(pbx_args));
  1149. pbx_args.no_hangup_chan = 1;
  1150. res = ast_pbx_run_args(chan, &pbx_args);
  1151. }
  1152. return res;
  1153. }
  1154. int stasis_app_send(const char *app_name, struct ast_json *message)
  1155. {
  1156. RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
  1157. if (!apps_registry) {
  1158. return -1;
  1159. }
  1160. app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY);
  1161. if (!app) {
  1162. /* XXX We can do a better job handling late binding, queueing up
  1163. * the call for a few seconds to wait for the app to register.
  1164. */
  1165. ast_log(LOG_WARNING,
  1166. "Stasis app '%s' not registered\n", app_name);
  1167. return -1;
  1168. }
  1169. app_send(app, message);
  1170. return 0;
  1171. }
  1172. static struct stasis_app *find_app_by_name(const char *app_name)
  1173. {
  1174. struct stasis_app *res = NULL;
  1175. if (!apps_registry) {
  1176. return NULL;
  1177. }
  1178. if (!ast_strlen_zero(app_name)) {
  1179. res = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY);
  1180. }
  1181. if (!res) {
  1182. ast_log(LOG_WARNING, "Could not find app '%s'\n",
  1183. app_name ? : "(null)");
  1184. }
  1185. return res;
  1186. }
  1187. static int append_name(void *obj, void *arg, int flags)
  1188. {
  1189. struct stasis_app *app = obj;
  1190. struct ao2_container *apps = arg;
  1191. ast_str_container_add(apps, stasis_app_name(app));
  1192. return 0;
  1193. }
  1194. struct ao2_container *stasis_app_get_all(void)
  1195. {
  1196. RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup);
  1197. if (!apps_registry) {
  1198. return NULL;
  1199. }
  1200. apps = ast_str_container_alloc(1);
  1201. if (!apps) {
  1202. return NULL;
  1203. }
  1204. ao2_callback(apps_registry, OBJ_NODATA, append_name, apps);
  1205. return ao2_bump(apps);
  1206. }
  1207. int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data)
  1208. {
  1209. RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
  1210. if (!apps_registry) {
  1211. return -1;
  1212. }
  1213. ao2_lock(apps_registry);
  1214. app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY | OBJ_NOLOCK);
  1215. if (app) {
  1216. app_update(app, handler, data);
  1217. } else {
  1218. app = app_create(app_name, handler, data);
  1219. if (app) {
  1220. ao2_link_flags(apps_registry, app, OBJ_NOLOCK);
  1221. } else {
  1222. ao2_unlock(apps_registry);
  1223. return -1;
  1224. }
  1225. }
  1226. /* We lazily clean up the apps_registry, because it's good enough to
  1227. * prevent memory leaks, and we're lazy.
  1228. */
  1229. cleanup();
  1230. ao2_unlock(apps_registry);
  1231. return 0;
  1232. }
  1233. void stasis_app_unregister(const char *app_name)
  1234. {
  1235. RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
  1236. if (!app_name) {
  1237. return;
  1238. }
  1239. if (!apps_registry) {
  1240. return;
  1241. }
  1242. app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY);
  1243. if (!app) {
  1244. ast_log(LOG_ERROR,
  1245. "Stasis app '%s' not registered\n", app_name);
  1246. return;
  1247. }
  1248. app_deactivate(app);
  1249. /* There's a decent chance that app is ready for cleanup. Go ahead
  1250. * and clean up, just in case
  1251. */
  1252. cleanup();
  1253. }
  1254. /*!
  1255. * \internal \brief List of registered event sources.
  1256. */
  1257. AST_RWLIST_HEAD_STATIC(event_sources, stasis_app_event_source);
  1258. void stasis_app_register_event_source(struct stasis_app_event_source *obj)
  1259. {
  1260. SCOPED_LOCK(lock, &event_sources, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  1261. AST_LIST_INSERT_TAIL(&event_sources, obj, next);
  1262. /* only need to bump the module ref on non-core sources because the
  1263. core ones are [un]registered by this module. */
  1264. if (!stasis_app_is_core_event_source(obj)) {
  1265. ast_module_ref(ast_module_info->self);
  1266. }
  1267. }
  1268. void stasis_app_unregister_event_source(struct stasis_app_event_source *obj)
  1269. {
  1270. struct stasis_app_event_source *source;
  1271. SCOPED_LOCK(lock, &event_sources, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  1272. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&event_sources, source, next) {
  1273. if (source == obj) {
  1274. AST_RWLIST_REMOVE_CURRENT(next);
  1275. if (!stasis_app_is_core_event_source(obj)) {
  1276. ast_module_unref(ast_module_info->self);
  1277. }
  1278. break;
  1279. }
  1280. }
  1281. AST_RWLIST_TRAVERSE_SAFE_END;
  1282. }
  1283. /*!
  1284. * \internal
  1285. * \brief Convert event source data to JSON.
  1286. *
  1287. * Calls each event source that has a "to_json" handler allowing each
  1288. * source to add data to the given JSON object.
  1289. *
  1290. * \param app application associated with the event source
  1291. * \param json a json object to "fill"
  1292. *
  1293. * \retval The given json object.
  1294. */
  1295. static struct ast_json *app_event_sources_to_json(
  1296. const struct stasis_app *app, struct ast_json *json)
  1297. {
  1298. struct stasis_app_event_source *source;
  1299. SCOPED_LOCK(lock, &event_sources, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
  1300. AST_LIST_TRAVERSE(&event_sources, source, next) {
  1301. if (source->to_json) {
  1302. source->to_json(app, json);
  1303. }
  1304. }
  1305. return json;
  1306. }
  1307. static struct ast_json *stasis_app_object_to_json(struct stasis_app *app)
  1308. {
  1309. if (!app) {
  1310. return NULL;
  1311. }
  1312. return app_event_sources_to_json(app, app_to_json(app));
  1313. }
  1314. struct ast_json *stasis_app_to_json(const char *app_name)
  1315. {
  1316. RAII_VAR(struct stasis_app *, app, find_app_by_name(app_name), ao2_cleanup);
  1317. return stasis_app_object_to_json(app);
  1318. }
  1319. /*!
  1320. * \internal
  1321. * \brief Finds an event source that matches a uri scheme.
  1322. *
  1323. * Uri(s) should begin with a particular scheme that can be matched
  1324. * against an event source.
  1325. *
  1326. * \param uri uri containing a scheme to match
  1327. *
  1328. * \retval an event source if found, NULL otherwise.
  1329. */
  1330. static struct stasis_app_event_source *app_event_source_find(const char *uri)
  1331. {
  1332. struct stasis_app_event_source *source;
  1333. SCOPED_LOCK(lock, &event_sources, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
  1334. AST_LIST_TRAVERSE(&event_sources, source, next) {
  1335. if (ast_begins_with(uri, source->scheme)) {
  1336. return source;
  1337. }
  1338. }
  1339. return NULL;
  1340. }
  1341. /*!
  1342. * \internal
  1343. * \brief Callback for subscription handling
  1344. *
  1345. * \param app [un]subscribing application
  1346. * \param uri scheme:id of an event source
  1347. * \param event_source being [un]subscribed [from]to
  1348. *
  1349. * \retval stasis_app_subscribe_res return code.
  1350. */
  1351. typedef enum stasis_app_subscribe_res (*app_subscription_handler)(
  1352. struct stasis_app *app, const char *uri,
  1353. struct stasis_app_event_source *event_source);
  1354. /*!
  1355. * \internal
  1356. * \brief Subscriptions handler for application [un]subscribing.
  1357. *
  1358. * \param app_name Name of the application to subscribe.
  1359. * \param event_source_uris URIs for the event sources to subscribe to.
  1360. * \param event_sources_count Array size of event_source_uris.
  1361. * \param json Optional output pointer for JSON representation of the app
  1362. * after adding the subscription.
  1363. * \param handler [un]subscribe handler
  1364. *
  1365. * \retval stasis_app_subscribe_res return code.
  1366. */
  1367. static enum stasis_app_subscribe_res app_handle_subscriptions(
  1368. const char *app_name, const char **event_source_uris,
  1369. int event_sources_count, struct ast_json **json,
  1370. app_subscription_handler handler)
  1371. {
  1372. RAII_VAR(struct stasis_app *, app, find_app_by_name(app_name), ao2_cleanup);
  1373. int i;
  1374. if (!app) {
  1375. return STASIS_ASR_APP_NOT_FOUND;
  1376. }
  1377. for (i = 0; i < event_sources_count; ++i) {
  1378. const char *uri = event_source_uris[i];
  1379. enum stasis_app_subscribe_res res = STASIS_ASR_INTERNAL_ERROR;
  1380. struct stasis_app_event_source *event_source;
  1381. if (!(event_source = app_event_source_find(uri))) {
  1382. ast_log(LOG_WARNING, "Invalid scheme: %s\n", uri);
  1383. return STASIS_ASR_EVENT_SOURCE_BAD_SCHEME;
  1384. }
  1385. if (handler &&
  1386. ((res = handler(app, uri, event_source)))) {
  1387. return res;
  1388. }
  1389. }
  1390. if (json) {
  1391. ast_debug(3, "%s: Successful; setting results\n", app_name);
  1392. *json = stasis_app_object_to_json(app);
  1393. }
  1394. return STASIS_ASR_OK;
  1395. }
  1396. enum stasis_app_subscribe_res stasis_app_subscribe_channel(const char *app_name,
  1397. struct ast_channel *chan)
  1398. {
  1399. RAII_VAR(struct stasis_app *, app, find_app_by_name(app_name), ao2_cleanup);
  1400. int res;
  1401. if (!app) {
  1402. return STASIS_ASR_APP_NOT_FOUND;
  1403. }
  1404. ast_debug(3, "%s: Subscribing to %s\n", app_name, ast_channel_uniqueid(chan));
  1405. res = app_subscribe_channel(app, chan);
  1406. if (res != 0) {
  1407. ast_log(LOG_ERROR, "Error subscribing app '%s' to channel '%s'\n",
  1408. app_name, ast_channel_uniqueid(chan));
  1409. return STASIS_ASR_INTERNAL_ERROR;
  1410. }
  1411. return STASIS_ASR_OK;
  1412. }
  1413. /*!
  1414. * \internal
  1415. * \brief Subscribe an app to an event source.
  1416. *
  1417. * \param app subscribing application
  1418. * \param uri scheme:id of an event source
  1419. * \param event_source being subscribed to
  1420. *
  1421. * \retval stasis_app_subscribe_res return code.
  1422. */
  1423. static enum stasis_app_subscribe_res app_subscribe(
  1424. struct stasis_app *app, const char *uri,
  1425. struct stasis_app_event_source *event_source)
  1426. {
  1427. const char *app_name = stasis_app_name(app);
  1428. RAII_VAR(void *, obj, NULL, ao2_cleanup);
  1429. ast_debug(3, "%s: Checking %s\n", app_name, uri);
  1430. if (!event_source->find ||
  1431. (!(obj = event_source->find(app, uri + strlen(event_source->scheme))))) {
  1432. ast_log(LOG_WARNING, "Event source not found: %s\n", uri);
  1433. return STASIS_ASR_EVENT_SOURCE_NOT_FOUND;
  1434. }
  1435. ast_debug(3, "%s: Subscribing to %s\n", app_name, uri);
  1436. if (!event_source->subscribe || (event_source->subscribe(app, obj))) {
  1437. ast_log(LOG_WARNING, "Error subscribing app '%s' to '%s'\n",
  1438. app_name, uri);
  1439. return STASIS_ASR_INTERNAL_ERROR;
  1440. }
  1441. return STASIS_ASR_OK;
  1442. }
  1443. enum stasis_app_subscribe_res stasis_app_subscribe(const char *app_name,
  1444. const char **event_source_uris, int event_sources_count,
  1445. struct ast_json **json)
  1446. {
  1447. return app_handle_subscriptions(
  1448. app_name, event_source_uris, event_sources_count,
  1449. json, app_subscribe);
  1450. }
  1451. /*!
  1452. * \internal
  1453. * \brief Unsubscribe an app from an event source.
  1454. *
  1455. * \param app application to unsubscribe
  1456. * \param uri scheme:id of an event source
  1457. * \param event_source being unsubscribed from
  1458. *
  1459. * \retval stasis_app_subscribe_res return code.
  1460. */
  1461. static enum stasis_app_subscribe_res app_unsubscribe(
  1462. struct stasis_app *app, const char *uri,
  1463. struct stasis_app_event_source *event_source)
  1464. {
  1465. const char *app_name = stasis_app_name(app);
  1466. const char *id = uri + strlen(event_source->scheme);
  1467. if (!event_source->is_subscribed ||
  1468. (!event_source->is_subscribed(app, id))) {
  1469. return STASIS_ASR_EVENT_SOURCE_NOT_FOUND;
  1470. }
  1471. ast_debug(3, "%s: Unsubscribing from %s\n", app_name, uri);
  1472. if (!event_source->unsubscribe || (event_source->unsubscribe(app, id))) {
  1473. ast_log(LOG_WARNING, "Error unsubscribing app '%s' to '%s'\n",
  1474. app_name, uri);
  1475. return -1;
  1476. }
  1477. return 0;
  1478. }
  1479. enum stasis_app_subscribe_res stasis_app_unsubscribe(const char *app_name,
  1480. const char **event_source_uris, int event_sources_count,
  1481. struct ast_json **json)
  1482. {
  1483. return app_handle_subscriptions(
  1484. app_name, event_source_uris, event_sources_count,
  1485. json, app_unsubscribe);
  1486. }
  1487. enum stasis_app_user_event_res stasis_app_user_event(const char *app_name,
  1488. const char *event_name,
  1489. const char **source_uris, int sources_count,
  1490. struct ast_json *json_variables)
  1491. {
  1492. RAII_VAR(struct stasis_app *, app, find_app_by_name(app_name), ao2_cleanup);
  1493. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  1494. RAII_VAR(struct ast_multi_object_blob *, multi, NULL, ao2_cleanup);
  1495. RAII_VAR(void *, obj, NULL, ao2_cleanup);
  1496. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  1497. enum stasis_app_subscribe_res res = STASIS_APP_USER_INTERNAL_ERROR;
  1498. struct ast_json *json_value;
  1499. int have_channel = 0;
  1500. int i;
  1501. if (!app) {
  1502. ast_log(LOG_WARNING, "App %s not found\n", app_name);
  1503. return STASIS_APP_USER_APP_NOT_FOUND;
  1504. }
  1505. if (!ast_multi_user_event_type()) {
  1506. return res;
  1507. }
  1508. blob = json_variables;
  1509. if (!blob) {
  1510. blob = ast_json_pack("{}");
  1511. }
  1512. json_value = ast_json_string_create(event_name);
  1513. if (!json_value) {
  1514. ast_log(LOG_ERROR, "unable to create json string\n");
  1515. return res;
  1516. }
  1517. if (ast_json_object_set(blob, "eventname", json_value)) {
  1518. ast_log(LOG_ERROR, "unable to set eventname to blob\n");
  1519. return res;
  1520. }
  1521. multi = ast_multi_object_blob_create(blob);
  1522. for (i = 0; i < sources_count; ++i) {
  1523. const char *uri = source_uris[i];
  1524. void *snapshot=NULL;
  1525. enum stasis_user_multi_object_snapshot_type type;
  1526. if (ast_begins_with(uri, "channel:")) {
  1527. type = STASIS_UMOS_CHANNEL;
  1528. snapshot = ast_channel_snapshot_get_latest(uri + 8);
  1529. have_channel = 1;
  1530. } else if (ast_begins_with(uri, "bridge:")) {
  1531. type = STASIS_UMOS_BRIDGE;
  1532. snapshot = ast_bridge_snapshot_get_latest(uri + 7);
  1533. } else if (ast_begins_with(uri, "endpoint:")) {
  1534. type = STASIS_UMOS_ENDPOINT;
  1535. snapshot = ast_endpoint_latest_snapshot(uri + 9, NULL);
  1536. } else {
  1537. ast_log(LOG_WARNING, "Invalid scheme: %s\n", uri);
  1538. return STASIS_APP_USER_EVENT_SOURCE_BAD_SCHEME;
  1539. }
  1540. if (!snapshot) {
  1541. ast_log(LOG_ERROR, "Unable to get snapshot for %s\n", uri);
  1542. return STASIS_APP_USER_EVENT_SOURCE_NOT_FOUND;
  1543. }
  1544. ast_multi_object_blob_add(multi, type, snapshot);
  1545. }
  1546. message = stasis_message_create(ast_multi_user_event_type(), multi);
  1547. if (!message) {
  1548. ast_log(LOG_ERROR, "Unable to create stasis user event message\n");
  1549. return res;
  1550. }
  1551. /*
  1552. * Publishing to two different topics is normally to be avoided -- except
  1553. * in this case both are final destinations with no forwards (only listeners).
  1554. * The message has to be delivered to the application topic for ARI, but a
  1555. * copy is also delivered directly to the manager for AMI if there is a channel.
  1556. */
  1557. stasis_publish(ast_app_get_topic(app), message);
  1558. if (have_channel) {
  1559. stasis_publish(ast_manager_get_topic(), message);
  1560. }
  1561. return STASIS_APP_USER_OK;
  1562. }
  1563. void stasis_app_ref(void)
  1564. {
  1565. ast_module_ref(ast_module_info->self);
  1566. }
  1567. void stasis_app_unref(void)
  1568. {
  1569. ast_module_unref(ast_module_info->self);
  1570. }
  1571. static int unload_module(void)
  1572. {
  1573. stasis_app_unregister_event_sources();
  1574. messaging_cleanup();
  1575. cleanup();
  1576. ao2_cleanup(apps_registry);
  1577. apps_registry = NULL;
  1578. ao2_cleanup(app_controls);
  1579. app_controls = NULL;
  1580. ao2_cleanup(app_bridges);
  1581. app_bridges = NULL;
  1582. ao2_cleanup(app_bridges_moh);
  1583. app_bridges_moh = NULL;
  1584. ao2_cleanup(app_bridges_playback);
  1585. app_bridges_playback = NULL;
  1586. STASIS_MESSAGE_TYPE_CLEANUP(end_message_type);
  1587. STASIS_MESSAGE_TYPE_CLEANUP(start_message_type);
  1588. return 0;
  1589. }
  1590. /* \brief Sanitization callback for channel snapshots */
  1591. static int channel_snapshot_sanitizer(const struct ast_channel_snapshot *snapshot)
  1592. {
  1593. if (!snapshot || !(snapshot->tech_properties & AST_CHAN_TP_INTERNAL)) {
  1594. return 0;
  1595. }
  1596. return 1;
  1597. }
  1598. /* \brief Sanitization callback for channels */
  1599. static int channel_sanitizer(const struct ast_channel *chan)
  1600. {
  1601. if (!chan || !(ast_channel_tech(chan)->properties & AST_CHAN_TP_INTERNAL)) {
  1602. return 0;
  1603. }
  1604. return 1;
  1605. }
  1606. /* \brief Sanitization callback for channel unique IDs */
  1607. static int channel_id_sanitizer(const char *id)
  1608. {
  1609. RAII_VAR(struct ast_channel_snapshot *, snapshot, ast_channel_snapshot_get_latest(id), ao2_cleanup);
  1610. return channel_snapshot_sanitizer(snapshot);
  1611. }
  1612. /* \brief Sanitization callbacks for communication to Stasis applications */
  1613. struct stasis_message_sanitizer app_sanitizer = {
  1614. .channel_id = channel_id_sanitizer,
  1615. .channel_snapshot = channel_snapshot_sanitizer,
  1616. .channel = channel_sanitizer,
  1617. };
  1618. struct stasis_message_sanitizer *stasis_app_get_sanitizer(void)
  1619. {
  1620. return &app_sanitizer;
  1621. }
  1622. static const struct ast_datastore_info stasis_internal_channel_info = {
  1623. .type = "stasis-internal-channel",
  1624. };
  1625. static int set_internal_datastore(struct ast_channel *chan)
  1626. {
  1627. struct ast_datastore *datastore;
  1628. datastore = ast_channel_datastore_find(chan, &stasis_internal_channel_info, NULL);
  1629. if (!datastore) {
  1630. datastore = ast_datastore_alloc(&stasis_internal_channel_info, NULL);
  1631. if (!datastore) {
  1632. return -1;
  1633. }
  1634. ast_channel_datastore_add(chan, datastore);
  1635. }
  1636. return 0;
  1637. }
  1638. int stasis_app_channel_unreal_set_internal(struct ast_channel *chan)
  1639. {
  1640. struct ast_channel *outchan = NULL, *outowner = NULL;
  1641. int res = 0;
  1642. struct ast_unreal_pvt *unreal_pvt = ast_channel_tech_pvt(chan);
  1643. ao2_ref(unreal_pvt, +1);
  1644. ast_unreal_lock_all(unreal_pvt, &outowner, &outchan);
  1645. if (outowner) {
  1646. res |= set_internal_datastore(outowner);
  1647. ast_channel_unlock(outowner);
  1648. ast_channel_unref(outowner);
  1649. }
  1650. if (outchan) {
  1651. res |= set_internal_datastore(outchan);
  1652. ast_channel_unlock(outchan);
  1653. ast_channel_unref(outchan);
  1654. }
  1655. ao2_unlock(unreal_pvt);
  1656. ao2_ref(unreal_pvt, -1);
  1657. return res;
  1658. }
  1659. int stasis_app_channel_set_internal(struct ast_channel *chan)
  1660. {
  1661. int res;
  1662. ast_channel_lock(chan);
  1663. res = set_internal_datastore(chan);
  1664. ast_channel_unlock(chan);
  1665. return res;
  1666. }
  1667. int stasis_app_channel_is_internal(struct ast_channel *chan)
  1668. {
  1669. struct ast_datastore *datastore;
  1670. int res = 0;
  1671. ast_channel_lock(chan);
  1672. datastore = ast_channel_datastore_find(chan, &stasis_internal_channel_info, NULL);
  1673. if (datastore) {
  1674. res = 1;
  1675. }
  1676. ast_channel_unlock(chan);
  1677. return res;
  1678. }
  1679. static int load_module(void)
  1680. {
  1681. if (STASIS_MESSAGE_TYPE_INIT(start_message_type) != 0) {
  1682. return AST_MODULE_LOAD_DECLINE;
  1683. }
  1684. if (STASIS_MESSAGE_TYPE_INIT(end_message_type) != 0) {
  1685. return AST_MODULE_LOAD_DECLINE;
  1686. }
  1687. apps_registry = ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
  1688. app_controls = ao2_container_alloc(CONTROLS_NUM_BUCKETS, control_hash, control_compare);
  1689. app_bridges = ao2_container_alloc(BRIDGES_NUM_BUCKETS, bridges_hash, bridges_compare);
  1690. app_bridges_moh = ao2_container_alloc_hash(
  1691. AO2_ALLOC_OPT_LOCK_MUTEX, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT,
  1692. 37, bridges_channel_hash_fn, bridges_channel_sort_fn, NULL);
  1693. app_bridges_playback = ao2_container_alloc_hash(
  1694. AO2_ALLOC_OPT_LOCK_MUTEX, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT,
  1695. 37, bridges_channel_hash_fn, bridges_channel_sort_fn, NULL);
  1696. if (!apps_registry || !app_controls || !app_bridges || !app_bridges_moh || !app_bridges_playback) {
  1697. unload_module();
  1698. return AST_MODULE_LOAD_FAILURE;
  1699. }
  1700. if (messaging_init()) {
  1701. unload_module();
  1702. return AST_MODULE_LOAD_FAILURE;
  1703. }
  1704. bridge_stasis_init();
  1705. stasis_app_register_event_sources();
  1706. return AST_MODULE_LOAD_SUCCESS;
  1707. }
  1708. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application support",
  1709. .support_level = AST_MODULE_SUPPORT_CORE,
  1710. .load = load_module,
  1711. .unload = unload_module,
  1712. );