cel.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007 - 2009, Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*!
  17. * \file
  18. *
  19. * \brief Channel Event Logging API
  20. *
  21. * \author Steve Murphy <murf@digium.com>
  22. * \author Russell Bryant <russell@digium.com>
  23. */
  24. /*! \li \ref cel.c uses the configuration file \ref cel.conf
  25. * \addtogroup configuration_file Configuration Files
  26. */
  27. /*!
  28. * \page cel.conf cel.conf
  29. * \verbinclude cel.conf.sample
  30. */
  31. /*** MODULEINFO
  32. <support_level>core</support_level>
  33. ***/
  34. #include "asterisk.h"
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include "asterisk/_private.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/pbx.h"
  39. #include "asterisk/cel.h"
  40. #include "asterisk/logger.h"
  41. #include "asterisk/linkedlists.h"
  42. #include "asterisk/utils.h"
  43. #include "asterisk/config.h"
  44. #include "asterisk/config_options.h"
  45. #include "asterisk/cli.h"
  46. #include "asterisk/astobj2.h"
  47. #include "asterisk/stasis_message_router.h"
  48. #include "asterisk/stasis_channels.h"
  49. #include "asterisk/stasis_bridges.h"
  50. #include "asterisk/bridge.h"
  51. #include "asterisk/parking.h"
  52. #include "asterisk/pickup.h"
  53. #include "asterisk/core_local.h"
  54. #include "asterisk/taskprocessor.h"
  55. /*** DOCUMENTATION
  56. <configInfo name="cel" language="en_US">
  57. <configFile name="cel.conf">
  58. <configObject name="general">
  59. <synopsis>Options that apply globally to Channel Event Logging (CEL)</synopsis>
  60. <configOption name="enable">
  61. <synopsis>Determines whether CEL is enabled</synopsis>
  62. </configOption>
  63. <configOption name="dateformat">
  64. <synopsis>The format to be used for dates when logging</synopsis>
  65. </configOption>
  66. <configOption name="apps">
  67. <synopsis>List of apps for CEL to track</synopsis>
  68. <description><para>A case-insensitive, comma-separated list of applications
  69. to track when one or both of APP_START and APP_END events are flagged for
  70. tracking</para></description>
  71. </configOption>
  72. <configOption name="events">
  73. <synopsis>List of events for CEL to track</synopsis>
  74. <description><para>A case-sensitive, comma-separated list of event names
  75. to track. These event names do not include the leading <literal>AST_CEL</literal>.
  76. </para>
  77. <enumlist>
  78. <enum name="ALL">
  79. <para>Special value which tracks all events.</para>
  80. </enum>
  81. <enum name="CHAN_START"/>
  82. <enum name="CHAN_END"/>
  83. <enum name="ANSWER"/>
  84. <enum name="HANGUP"/>
  85. <enum name="APP_START"/>
  86. <enum name="APP_END"/>
  87. <enum name="PARK_START"/>
  88. <enum name="PARK_END"/>
  89. <enum name="USER_DEFINED"/>
  90. <enum name="BRIDGE_ENTER"/>
  91. <enum name="BRIDGE_EXIT"/>
  92. <enum name="BLINDTRANSFER"/>
  93. <enum name="ATTENDEDTRANSFER"/>
  94. <enum name="PICKUP"/>
  95. <enum name="FORWARD"/>
  96. <enum name="LINKEDID_END"/>
  97. <enum name="LOCAL_OPTIMIZE"/>
  98. </enumlist>
  99. </description>
  100. </configOption>
  101. </configObject>
  102. </configFile>
  103. </configInfo>
  104. ***/
  105. /*! Message router for state that CEL needs to know about */
  106. static struct stasis_message_router *cel_state_router;
  107. /*! Topic for CEL-specific messages */
  108. static struct stasis_topic *cel_topic;
  109. /*! Aggregation topic for all topics CEL needs to know about */
  110. static struct stasis_topic *cel_aggregation_topic;
  111. /*! Subscription for forwarding the channel caching topic */
  112. static struct stasis_forward *cel_channel_forwarder;
  113. /*! Subscription for forwarding the channel caching topic */
  114. static struct stasis_forward *cel_bridge_forwarder;
  115. /*! Subscription for forwarding the parking topic */
  116. static struct stasis_forward *cel_parking_forwarder;
  117. /*! Subscription for forwarding the CEL-specific topic */
  118. static struct stasis_forward *cel_cel_forwarder;
  119. struct stasis_message_type *cel_generic_type(void);
  120. STASIS_MESSAGE_TYPE_DEFN(cel_generic_type);
  121. /*! Container for CEL backend information */
  122. static AO2_GLOBAL_OBJ_STATIC(cel_backends);
  123. /*! The number of buckets into which backend names will be hashed */
  124. #define BACKEND_BUCKETS 13
  125. /*! Container for dial end multichannel blobs for holding on to dial statuses */
  126. static AO2_GLOBAL_OBJ_STATIC(cel_dialstatus_store);
  127. /*!
  128. * \brief Maximum possible CEL event IDs
  129. * \note This limit is currently imposed by the eventset definition
  130. */
  131. #define CEL_MAX_EVENT_IDS 64
  132. /*!
  133. * \brief Number of buckets for the appset container
  134. */
  135. #define NUM_APP_BUCKETS 97
  136. /*!
  137. * \brief Number of buckets for the dialstatus container
  138. */
  139. #define NUM_DIALSTATUS_BUCKETS 251
  140. struct cel_linkedid {
  141. /*! Number of channels with this linkedid. */
  142. unsigned int count;
  143. /*! Linkedid stored at end of struct. */
  144. char id[0];
  145. };
  146. /*! Container of channel references to a linkedid for CEL purposes. */
  147. static AO2_GLOBAL_OBJ_STATIC(cel_linkedids);
  148. struct cel_dialstatus {
  149. /*! Uniqueid of the channel */
  150. char uniqueid[AST_MAX_UNIQUEID];
  151. /*! The dial status */
  152. char dialstatus[0];
  153. };
  154. /*! \brief Destructor for cel_config */
  155. static void cel_general_config_dtor(void *obj)
  156. {
  157. struct ast_cel_general_config *cfg = obj;
  158. ast_string_field_free_memory(cfg);
  159. ao2_cleanup(cfg->apps);
  160. cfg->apps = NULL;
  161. }
  162. void *ast_cel_general_config_alloc(void)
  163. {
  164. RAII_VAR(struct ast_cel_general_config *, cfg, NULL, ao2_cleanup);
  165. if (!(cfg = ao2_alloc(sizeof(*cfg), cel_general_config_dtor))) {
  166. return NULL;
  167. }
  168. if (ast_string_field_init(cfg, 64)) {
  169. return NULL;
  170. }
  171. if (!(cfg->apps = ast_str_container_alloc(NUM_APP_BUCKETS))) {
  172. return NULL;
  173. }
  174. ao2_ref(cfg, +1);
  175. return cfg;
  176. }
  177. /*! \brief A container that holds all config-related information */
  178. struct cel_config {
  179. struct ast_cel_general_config *general;
  180. };
  181. static AO2_GLOBAL_OBJ_STATIC(cel_configs);
  182. /*! \brief Destructor for cel_config */
  183. static void cel_config_dtor(void *obj)
  184. {
  185. struct cel_config *cfg = obj;
  186. ao2_cleanup(cfg->general);
  187. cfg->general = NULL;
  188. }
  189. static void *cel_config_alloc(void)
  190. {
  191. RAII_VAR(struct cel_config *, cfg, NULL, ao2_cleanup);
  192. if (!(cfg = ao2_alloc(sizeof(*cfg), cel_config_dtor))) {
  193. return NULL;
  194. }
  195. if (!(cfg->general = ast_cel_general_config_alloc())) {
  196. return NULL;
  197. }
  198. ao2_ref(cfg, +1);
  199. return cfg;
  200. }
  201. /*! \brief An aco_type structure to link the "general" category to the ast_cel_general_config type */
  202. static struct aco_type general_option = {
  203. .type = ACO_GLOBAL,
  204. .name = "general",
  205. .item_offset = offsetof(struct cel_config, general),
  206. .category_match = ACO_WHITELIST_EXACT,
  207. .category = "general",
  208. };
  209. /*! Config sections used by existing modules. Do not add to this list. */
  210. static const char *ignore_categories[] = {
  211. "manager",
  212. "radius",
  213. NULL,
  214. };
  215. static struct aco_type ignore_option = {
  216. .type = ACO_IGNORE,
  217. .name = "modules",
  218. .category = (const char*)ignore_categories,
  219. .category_match = ACO_WHITELIST_ARRAY,
  220. };
  221. /*! \brief The config file to be processed for the module. */
  222. static struct aco_file cel_conf = {
  223. .filename = "cel.conf", /*!< The name of the config file */
  224. .types = ACO_TYPES(&general_option, &ignore_option), /*!< The mapping object types to be processed */
  225. };
  226. static int cel_pre_apply_config(void);
  227. CONFIG_INFO_CORE("cel", cel_cfg_info, cel_configs, cel_config_alloc,
  228. .files = ACO_FILES(&cel_conf),
  229. .pre_apply_config = cel_pre_apply_config,
  230. );
  231. static int cel_pre_apply_config(void)
  232. {
  233. struct cel_config *cfg = aco_pending_config(&cel_cfg_info);
  234. if (!cfg->general) {
  235. return -1;
  236. }
  237. if (!ao2_container_count(cfg->general->apps)) {
  238. return 0;
  239. }
  240. if (cfg->general->events & ((int64_t) 1 << AST_CEL_APP_START)) {
  241. return 0;
  242. }
  243. if (cfg->general->events & ((int64_t) 1 << AST_CEL_APP_END)) {
  244. return 0;
  245. }
  246. ast_log(LOG_ERROR, "Applications are listed to be tracked, but APP events are not tracked\n");
  247. return -1;
  248. }
  249. static struct aco_type *general_options[] = ACO_TYPES(&general_option);
  250. /*!
  251. * \brief Map of ast_cel_event_type to strings
  252. */
  253. static const char * const cel_event_types[CEL_MAX_EVENT_IDS] = {
  254. [AST_CEL_ALL] = "ALL",
  255. [AST_CEL_CHANNEL_START] = "CHAN_START",
  256. [AST_CEL_CHANNEL_END] = "CHAN_END",
  257. [AST_CEL_ANSWER] = "ANSWER",
  258. [AST_CEL_HANGUP] = "HANGUP",
  259. [AST_CEL_APP_START] = "APP_START",
  260. [AST_CEL_APP_END] = "APP_END",
  261. [AST_CEL_PARK_START] = "PARK_START",
  262. [AST_CEL_PARK_END] = "PARK_END",
  263. [AST_CEL_USER_DEFINED] = "USER_DEFINED",
  264. [AST_CEL_BRIDGE_ENTER] = "BRIDGE_ENTER",
  265. [AST_CEL_BRIDGE_EXIT] = "BRIDGE_EXIT",
  266. [AST_CEL_BLINDTRANSFER] = "BLINDTRANSFER",
  267. [AST_CEL_ATTENDEDTRANSFER] = "ATTENDEDTRANSFER",
  268. [AST_CEL_PICKUP] = "PICKUP",
  269. [AST_CEL_FORWARD] = "FORWARD",
  270. [AST_CEL_LINKEDID_END] = "LINKEDID_END",
  271. [AST_CEL_LOCAL_OPTIMIZE] = "LOCAL_OPTIMIZE",
  272. };
  273. struct cel_backend {
  274. ast_cel_backend_cb callback; /*!< Callback for this backend */
  275. char name[0]; /*!< Name of this backend */
  276. };
  277. /*! \brief Hashing function for cel_backend */
  278. AO2_STRING_FIELD_HASH_FN(cel_backend, name)
  279. /*! \brief Comparator function for cel_backend */
  280. AO2_STRING_FIELD_CMP_FN(cel_backend, name)
  281. /*! \brief Hashing function for dialstatus container */
  282. AO2_STRING_FIELD_HASH_FN(cel_dialstatus, uniqueid)
  283. /*! \brief Comparator function for dialstatus container */
  284. AO2_STRING_FIELD_CMP_FN(cel_dialstatus, uniqueid)
  285. unsigned int ast_cel_check_enabled(void)
  286. {
  287. unsigned int enabled;
  288. struct cel_config *cfg = ao2_global_obj_ref(cel_configs);
  289. enabled = (!cfg || !cfg->general) ? 0 : cfg->general->enable;
  290. ao2_cleanup(cfg);
  291. return enabled;
  292. }
  293. static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  294. {
  295. unsigned int i;
  296. RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
  297. RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
  298. struct ao2_iterator iter;
  299. char *app;
  300. switch (cmd) {
  301. case CLI_INIT:
  302. e->command = "cel show status";
  303. e->usage =
  304. "Usage: cel show status\n"
  305. " Displays the Channel Event Logging system status.\n";
  306. return NULL;
  307. case CLI_GENERATE:
  308. return NULL;
  309. case CLI_HANDLER:
  310. break;
  311. }
  312. if (a->argc > 3) {
  313. return CLI_SHOWUSAGE;
  314. }
  315. ast_cli(a->fd, "CEL Logging: %s\n", ast_cel_check_enabled() ? "Enabled" : "Disabled");
  316. if (!cfg || !cfg->general || !cfg->general->enable) {
  317. return CLI_SUCCESS;
  318. }
  319. for (i = 0; i < (sizeof(cfg->general->events) * 8); i++) {
  320. const char *name;
  321. if (!(cfg->general->events & ((int64_t) 1 << i))) {
  322. continue;
  323. }
  324. name = ast_cel_get_type_name(i);
  325. if (strcasecmp(name, "Unknown")) {
  326. ast_cli(a->fd, "CEL Tracking Event: %s\n", name);
  327. }
  328. }
  329. iter = ao2_iterator_init(cfg->general->apps, 0);
  330. for (; (app = ao2_iterator_next(&iter)); ao2_ref(app, -1)) {
  331. ast_cli(a->fd, "CEL Tracking Application: %s\n", app);
  332. }
  333. ao2_iterator_destroy(&iter);
  334. if (backends) {
  335. struct cel_backend *backend;
  336. iter = ao2_iterator_init(backends, 0);
  337. for (; (backend = ao2_iterator_next(&iter)); ao2_ref(backend, -1)) {
  338. ast_cli(a->fd, "CEL Event Subscriber: %s\n", backend->name);
  339. }
  340. ao2_iterator_destroy(&iter);
  341. }
  342. return CLI_SUCCESS;
  343. }
  344. static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CEL status");
  345. enum ast_cel_event_type ast_cel_str_to_event_type(const char *name)
  346. {
  347. unsigned int i;
  348. for (i = 0; i < ARRAY_LEN(cel_event_types); i++) {
  349. if (cel_event_types[i] && !strcasecmp(name, cel_event_types[i])) {
  350. return i;
  351. }
  352. }
  353. ast_log(LOG_ERROR, "Unknown event name '%s'\n", name);
  354. return AST_CEL_INVALID_VALUE;
  355. }
  356. static int ast_cel_track_event(enum ast_cel_event_type et)
  357. {
  358. RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
  359. if (!cfg || !cfg->general) {
  360. return 0;
  361. }
  362. return (cfg->general->events & ((int64_t) 1 << et)) ? 1 : 0;
  363. }
  364. static int events_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  365. {
  366. struct ast_cel_general_config *cfg = obj;
  367. char *events = ast_strdupa(var->value);
  368. char *cur_event;
  369. while ((cur_event = strsep(&events, ","))) {
  370. enum ast_cel_event_type event_type;
  371. cur_event = ast_strip(cur_event);
  372. if (ast_strlen_zero(cur_event)) {
  373. continue;
  374. }
  375. event_type = ast_cel_str_to_event_type(cur_event);
  376. if (event_type == AST_CEL_ALL) {
  377. /* All events */
  378. cfg->events = (int64_t) -1;
  379. } else if (event_type == AST_CEL_INVALID_VALUE) {
  380. return -1;
  381. } else {
  382. cfg->events |= ((int64_t) 1 << event_type);
  383. }
  384. }
  385. return 0;
  386. }
  387. static int apps_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  388. {
  389. struct ast_cel_general_config *cfg = obj;
  390. char *apps = ast_strdupa(var->value);
  391. char *cur_app;
  392. while ((cur_app = strsep(&apps, ","))) {
  393. cur_app = ast_strip(cur_app);
  394. if (ast_strlen_zero(cur_app)) {
  395. continue;
  396. }
  397. cur_app = ast_str_to_lower(cur_app);
  398. ast_str_container_add(cfg->apps, cur_app);
  399. }
  400. return 0;
  401. }
  402. const char *ast_cel_get_type_name(enum ast_cel_event_type type)
  403. {
  404. return S_OR(cel_event_types[type], "Unknown");
  405. }
  406. static int cel_track_app(const char *const_app)
  407. {
  408. RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
  409. RAII_VAR(char *, app, NULL, ao2_cleanup);
  410. char *app_lower;
  411. if (!cfg || !cfg->general) {
  412. return 0;
  413. }
  414. app_lower = ast_str_to_lower(ast_strdupa(const_app));
  415. app = ao2_find(cfg->general->apps, app_lower, OBJ_SEARCH_KEY);
  416. if (!app) {
  417. return 0;
  418. }
  419. return 1;
  420. }
  421. static int cel_linkedid_ref(const char *linkedid);
  422. struct ast_event *ast_cel_create_event(struct ast_channel_snapshot *snapshot,
  423. enum ast_cel_event_type event_type, const char *userdefevname,
  424. struct ast_json *extra, const char *peer)
  425. {
  426. struct timeval eventtime = ast_tvnow();
  427. RAII_VAR(char *, extra_txt, NULL, ast_json_free);
  428. if (extra) {
  429. extra_txt = ast_json_dump_string(extra);
  430. }
  431. return ast_event_new(AST_EVENT_CEL,
  432. AST_EVENT_IE_CEL_EVENT_TYPE, AST_EVENT_IE_PLTYPE_UINT, event_type,
  433. AST_EVENT_IE_CEL_EVENT_TIME, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_sec,
  434. AST_EVENT_IE_CEL_EVENT_TIME_USEC, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_usec,
  435. AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, S_OR(userdefevname, ""),
  436. AST_EVENT_IE_CEL_CIDNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_name,
  437. AST_EVENT_IE_CEL_CIDNUM, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_number,
  438. AST_EVENT_IE_CEL_CIDANI, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_ani,
  439. AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_rdnis,
  440. AST_EVENT_IE_CEL_CIDDNID, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_dnid,
  441. AST_EVENT_IE_CEL_EXTEN, AST_EVENT_IE_PLTYPE_STR, snapshot->exten,
  442. AST_EVENT_IE_CEL_CONTEXT, AST_EVENT_IE_PLTYPE_STR, snapshot->context,
  443. AST_EVENT_IE_CEL_CHANNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->name,
  444. AST_EVENT_IE_CEL_APPNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->appl,
  445. AST_EVENT_IE_CEL_APPDATA, AST_EVENT_IE_PLTYPE_STR, snapshot->data,
  446. AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, snapshot->amaflags,
  447. AST_EVENT_IE_CEL_ACCTCODE, AST_EVENT_IE_PLTYPE_STR, snapshot->accountcode,
  448. AST_EVENT_IE_CEL_PEERACCT, AST_EVENT_IE_PLTYPE_STR, snapshot->peeraccount,
  449. AST_EVENT_IE_CEL_UNIQUEID, AST_EVENT_IE_PLTYPE_STR, snapshot->uniqueid,
  450. AST_EVENT_IE_CEL_LINKEDID, AST_EVENT_IE_PLTYPE_STR, snapshot->linkedid,
  451. AST_EVENT_IE_CEL_USERFIELD, AST_EVENT_IE_PLTYPE_STR, snapshot->userfield,
  452. AST_EVENT_IE_CEL_EXTRA, AST_EVENT_IE_PLTYPE_STR, S_OR(extra_txt, ""),
  453. AST_EVENT_IE_CEL_PEER, AST_EVENT_IE_PLTYPE_STR, S_OR(peer, ""),
  454. AST_EVENT_IE_END);
  455. }
  456. static int cel_backend_send_cb(void *obj, void *arg, int flags)
  457. {
  458. struct cel_backend *backend = obj;
  459. backend->callback(arg);
  460. return 0;
  461. }
  462. static int cel_report_event(struct ast_channel_snapshot *snapshot,
  463. enum ast_cel_event_type event_type, const char *userdefevname,
  464. struct ast_json *extra, const char *peer_str)
  465. {
  466. struct ast_event *ev;
  467. RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
  468. RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
  469. if (!cfg || !cfg->general || !cfg->general->enable || !backends) {
  470. return 0;
  471. }
  472. /* Record the linkedid of new channels if we are tracking LINKEDID_END even if we aren't
  473. * reporting on CHANNEL_START so we can track when to send LINKEDID_END */
  474. if (event_type == AST_CEL_CHANNEL_START
  475. && ast_cel_track_event(AST_CEL_LINKEDID_END)) {
  476. if (cel_linkedid_ref(snapshot->linkedid)) {
  477. return -1;
  478. }
  479. }
  480. if (!ast_cel_track_event(event_type)) {
  481. return 0;
  482. }
  483. if ((event_type == AST_CEL_APP_START || event_type == AST_CEL_APP_END)
  484. && !cel_track_app(snapshot->appl)) {
  485. return 0;
  486. }
  487. ev = ast_cel_create_event(snapshot, event_type, userdefevname, extra, peer_str);
  488. if (!ev) {
  489. return -1;
  490. }
  491. /* Distribute event to backends */
  492. ao2_callback(backends, OBJ_MULTIPLE | OBJ_NODATA, cel_backend_send_cb, ev);
  493. ast_event_destroy(ev);
  494. return 0;
  495. }
  496. /* called whenever a channel is destroyed or a linkedid is changed to
  497. * potentially emit a CEL_LINKEDID_END event */
  498. static void check_retire_linkedid(struct ast_channel_snapshot *snapshot)
  499. {
  500. RAII_VAR(struct ao2_container *, linkedids, ao2_global_obj_ref(cel_linkedids), ao2_cleanup);
  501. struct cel_linkedid *lid;
  502. if (!linkedids || ast_strlen_zero(snapshot->linkedid)) {
  503. /* The CEL module is shutdown. Abort. */
  504. return;
  505. }
  506. ao2_lock(linkedids);
  507. lid = ao2_find(linkedids, (void *) snapshot->linkedid, OBJ_SEARCH_KEY);
  508. if (!lid) {
  509. ao2_unlock(linkedids);
  510. /*
  511. * The user may have done a reload to start tracking linkedids
  512. * when a call was already in progress. This is an unusual kind
  513. * of change to make after starting Asterisk.
  514. */
  515. ast_log(LOG_ERROR, "Something weird happened, couldn't find linkedid %s\n",
  516. snapshot->linkedid);
  517. return;
  518. }
  519. if (!--lid->count) {
  520. /* No channels use this linkedid anymore. */
  521. ao2_unlink(linkedids, lid);
  522. ao2_unlock(linkedids);
  523. cel_report_event(snapshot, AST_CEL_LINKEDID_END, NULL, NULL, NULL);
  524. } else {
  525. ao2_unlock(linkedids);
  526. }
  527. ao2_ref(lid, -1);
  528. }
  529. /* Note that no 'chan_fixup' function is provided for this datastore type,
  530. * because the channels that will use it will never be involved in masquerades.
  531. */
  532. static const struct ast_datastore_info fabricated_channel_datastore = {
  533. .type = "CEL fabricated channel",
  534. .destroy = ast_free_ptr,
  535. };
  536. struct ast_channel *ast_cel_fabricate_channel_from_event(const struct ast_event *event)
  537. {
  538. struct varshead *headp;
  539. struct ast_var_t *newvariable;
  540. const char *mixed_name;
  541. char timebuf[30];
  542. struct ast_channel *tchan;
  543. struct ast_cel_event_record record = {
  544. .version = AST_CEL_EVENT_RECORD_VERSION,
  545. };
  546. struct ast_datastore *datastore;
  547. char *app_data;
  548. RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
  549. if (!cfg || !cfg->general) {
  550. return NULL;
  551. }
  552. /* do not call ast_channel_alloc because this is not really a real channel */
  553. if (!(tchan = ast_dummy_channel_alloc())) {
  554. return NULL;
  555. }
  556. headp = ast_channel_varshead(tchan);
  557. /* first, get the variables from the event */
  558. if (ast_cel_fill_record(event, &record)) {
  559. ast_channel_unref(tchan);
  560. return NULL;
  561. }
  562. /* next, fill the channel with their data */
  563. mixed_name = (record.event_type == AST_CEL_USER_DEFINED)
  564. ? record.user_defined_name : record.event_name;
  565. if ((newvariable = ast_var_assign("eventtype", mixed_name))) {
  566. AST_LIST_INSERT_HEAD(headp, newvariable, entries);
  567. }
  568. if (ast_strlen_zero(cfg->general->date_format)) {
  569. snprintf(timebuf, sizeof(timebuf), "%ld.%06ld", (long) record.event_time.tv_sec,
  570. (long) record.event_time.tv_usec);
  571. } else {
  572. struct ast_tm tm;
  573. ast_localtime(&record.event_time, &tm, NULL);
  574. ast_strftime(timebuf, sizeof(timebuf), cfg->general->date_format, &tm);
  575. }
  576. if ((newvariable = ast_var_assign("eventtime", timebuf))) {
  577. AST_LIST_INSERT_HEAD(headp, newvariable, entries);
  578. }
  579. if ((newvariable = ast_var_assign("eventenum", record.event_name))) {
  580. AST_LIST_INSERT_HEAD(headp, newvariable, entries);
  581. }
  582. if ((newvariable = ast_var_assign("userdeftype", record.user_defined_name))) {
  583. AST_LIST_INSERT_HEAD(headp, newvariable, entries);
  584. }
  585. if ((newvariable = ast_var_assign("eventextra", record.extra))) {
  586. AST_LIST_INSERT_HEAD(headp, newvariable, entries);
  587. }
  588. ast_channel_caller(tchan)->id.name.valid = 1;
  589. ast_channel_caller(tchan)->id.name.str = ast_strdup(record.caller_id_name);
  590. ast_channel_caller(tchan)->id.number.valid = 1;
  591. ast_channel_caller(tchan)->id.number.str = ast_strdup(record.caller_id_num);
  592. ast_channel_caller(tchan)->ani.number.valid = 1;
  593. ast_channel_caller(tchan)->ani.number.str = ast_strdup(record.caller_id_ani);
  594. ast_channel_redirecting(tchan)->from.number.valid = 1;
  595. ast_channel_redirecting(tchan)->from.number.str = ast_strdup(record.caller_id_rdnis);
  596. ast_channel_dialed(tchan)->number.str = ast_strdup(record.caller_id_dnid);
  597. ast_channel_exten_set(tchan, record.extension);
  598. ast_channel_context_set(tchan, record.context);
  599. ast_channel_name_set(tchan, record.channel_name);
  600. ast_channel_internal_set_fake_ids(tchan, record.unique_id, record.linked_id);
  601. ast_channel_accountcode_set(tchan, record.account_code);
  602. ast_channel_peeraccount_set(tchan, record.peer_account);
  603. ast_channel_userfield_set(tchan, record.user_field);
  604. if ((newvariable = ast_var_assign("BRIDGEPEER", record.peer))) {
  605. AST_LIST_INSERT_HEAD(headp, newvariable, entries);
  606. }
  607. ast_channel_amaflags_set(tchan, record.amaflag);
  608. /* We need to store an 'application name' and 'application
  609. * data' on the channel for logging purposes, but the channel
  610. * structure only provides a place to store pointers, and it
  611. * expects these pointers to be pointing to data that does not
  612. * need to be freed. This means that the channel's destructor
  613. * does not attempt to free any storage that these pointers
  614. * point to. However, we can't provide data in that form directly for
  615. * these structure members. In order to ensure that these data
  616. * elements have a lifetime that matches the channel's
  617. * lifetime, we'll put them in a datastore attached to the
  618. * channel, and set's the channel's pointers to point into the
  619. * datastore. The datastore will then be automatically destroyed
  620. * when the channel is destroyed.
  621. */
  622. if (!(datastore = ast_datastore_alloc(&fabricated_channel_datastore, NULL))) {
  623. ast_channel_unref(tchan);
  624. return NULL;
  625. }
  626. if (!(app_data = ast_malloc(strlen(record.application_name) + strlen(record.application_data) + 2))) {
  627. ast_datastore_free(datastore);
  628. ast_channel_unref(tchan);
  629. return NULL;
  630. }
  631. ast_channel_appl_set(tchan, strcpy(app_data, record.application_name));
  632. ast_channel_data_set(tchan, strcpy(app_data + strlen(record.application_name) + 1,
  633. record.application_data));
  634. datastore->data = app_data;
  635. ast_channel_datastore_add(tchan, datastore);
  636. return tchan;
  637. }
  638. static int cel_linkedid_ref(const char *linkedid)
  639. {
  640. RAII_VAR(struct ao2_container *, linkedids, ao2_global_obj_ref(cel_linkedids), ao2_cleanup);
  641. struct cel_linkedid *lid;
  642. if (ast_strlen_zero(linkedid)) {
  643. ast_log(LOG_ERROR, "The linkedid should never be empty\n");
  644. return -1;
  645. }
  646. if (!linkedids) {
  647. /* The CEL module is shutdown. Abort. */
  648. return -1;
  649. }
  650. ao2_lock(linkedids);
  651. lid = ao2_find(linkedids, (void *) linkedid, OBJ_SEARCH_KEY);
  652. if (!lid) {
  653. /*
  654. * Changes to the lid->count member are protected by the
  655. * container lock so the lid object does not need its own lock.
  656. */
  657. lid = ao2_alloc_options(sizeof(*lid) + strlen(linkedid) + 1, NULL,
  658. AO2_ALLOC_OPT_LOCK_NOLOCK);
  659. if (!lid) {
  660. ao2_unlock(linkedids);
  661. return -1;
  662. }
  663. strcpy(lid->id, linkedid);/* Safe */
  664. ao2_link(linkedids, lid);
  665. }
  666. ++lid->count;
  667. ao2_unlock(linkedids);
  668. ao2_ref(lid, -1);
  669. return 0;
  670. }
  671. int ast_cel_fill_record(const struct ast_event *e, struct ast_cel_event_record *r)
  672. {
  673. if (r->version != AST_CEL_EVENT_RECORD_VERSION) {
  674. ast_log(LOG_ERROR, "Module ABI mismatch for ast_cel_event_record. "
  675. "Please ensure all modules were compiled for "
  676. "this version of Asterisk.\n");
  677. return -1;
  678. }
  679. r->event_type = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TYPE);
  680. r->event_time.tv_sec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME);
  681. r->event_time.tv_usec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME_USEC);
  682. r->event_name = ast_cel_get_type_name(r->event_type);
  683. if (r->event_type == AST_CEL_USER_DEFINED) {
  684. r->user_defined_name = ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USEREVENT_NAME);
  685. } else {
  686. r->user_defined_name = "";
  687. }
  688. r->caller_id_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNAME), "");
  689. r->caller_id_num = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNUM), "");
  690. r->caller_id_ani = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDANI), "");
  691. r->caller_id_rdnis = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDRDNIS), "");
  692. r->caller_id_dnid = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDDNID), "");
  693. r->extension = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTEN), "");
  694. r->context = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CONTEXT), "");
  695. r->channel_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CHANNAME), "");
  696. r->application_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPNAME), "");
  697. r->application_data = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPDATA), "");
  698. r->account_code = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
  699. r->peer_account = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEERACCT), "");
  700. r->unique_id = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_UNIQUEID), "");
  701. r->linked_id = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_LINKEDID), "");
  702. r->amaflag = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_AMAFLAGS);
  703. r->user_field = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USERFIELD), "");
  704. r->peer = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEER), "");
  705. r->extra = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTRA), "");
  706. return 0;
  707. }
  708. /*! \brief Typedef for callbacks that get called on channel snapshot updates */
  709. typedef void (*cel_channel_snapshot_monitor)(
  710. struct ast_channel_snapshot *old_snapshot,
  711. struct ast_channel_snapshot *new_snapshot);
  712. static struct cel_dialstatus *get_dialstatus(const char *uniqueid)
  713. {
  714. struct ao2_container *dial_statuses = ao2_global_obj_ref(cel_dialstatus_store);
  715. struct cel_dialstatus *dialstatus = NULL;
  716. if (dial_statuses) {
  717. dialstatus = ao2_find(dial_statuses, uniqueid, OBJ_SEARCH_KEY | OBJ_UNLINK);
  718. ao2_ref(dial_statuses, -1);
  719. }
  720. return dialstatus;
  721. }
  722. static const char *get_blob_variable(struct ast_multi_channel_blob *blob, const char *varname)
  723. {
  724. struct ast_json *json = ast_multi_channel_blob_get_json(blob);
  725. if (!json) {
  726. return NULL;
  727. }
  728. json = ast_json_object_get(json, varname);
  729. if (!json) {
  730. return NULL;
  731. }
  732. return ast_json_string_get(json);
  733. }
  734. /*! \brief Handle channel state changes */
  735. static void cel_channel_state_change(
  736. struct ast_channel_snapshot *old_snapshot,
  737. struct ast_channel_snapshot *new_snapshot)
  738. {
  739. int is_hungup, was_hungup;
  740. if (!new_snapshot) {
  741. cel_report_event(old_snapshot, AST_CEL_CHANNEL_END, NULL, NULL, NULL);
  742. if (ast_cel_track_event(AST_CEL_LINKEDID_END)) {
  743. check_retire_linkedid(old_snapshot);
  744. }
  745. return;
  746. }
  747. if (!old_snapshot) {
  748. cel_report_event(new_snapshot, AST_CEL_CHANNEL_START, NULL, NULL, NULL);
  749. return;
  750. }
  751. was_hungup = ast_test_flag(&old_snapshot->flags, AST_FLAG_DEAD) ? 1 : 0;
  752. is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_DEAD) ? 1 : 0;
  753. if (!was_hungup && is_hungup) {
  754. struct ast_json *extra;
  755. struct cel_dialstatus *dialstatus = get_dialstatus(new_snapshot->uniqueid);
  756. extra = ast_json_pack("{s: i, s: s, s: s}",
  757. "hangupcause", new_snapshot->hangupcause,
  758. "hangupsource", new_snapshot->hangupsource,
  759. "dialstatus", dialstatus ? dialstatus->dialstatus : "");
  760. cel_report_event(new_snapshot, AST_CEL_HANGUP, NULL, extra, NULL);
  761. ast_json_unref(extra);
  762. ao2_cleanup(dialstatus);
  763. return;
  764. }
  765. if (old_snapshot->state != new_snapshot->state && new_snapshot->state == AST_STATE_UP) {
  766. cel_report_event(new_snapshot, AST_CEL_ANSWER, NULL, NULL, NULL);
  767. return;
  768. }
  769. }
  770. static void cel_channel_linkedid_change(
  771. struct ast_channel_snapshot *old_snapshot,
  772. struct ast_channel_snapshot *new_snapshot)
  773. {
  774. if (!old_snapshot || !new_snapshot) {
  775. return;
  776. }
  777. ast_assert(!ast_strlen_zero(new_snapshot->linkedid));
  778. ast_assert(!ast_strlen_zero(old_snapshot->linkedid));
  779. if (ast_cel_track_event(AST_CEL_LINKEDID_END)
  780. && strcmp(old_snapshot->linkedid, new_snapshot->linkedid)) {
  781. cel_linkedid_ref(new_snapshot->linkedid);
  782. check_retire_linkedid(old_snapshot);
  783. }
  784. }
  785. static void cel_channel_app_change(
  786. struct ast_channel_snapshot *old_snapshot,
  787. struct ast_channel_snapshot *new_snapshot)
  788. {
  789. if (new_snapshot && old_snapshot
  790. && !strcmp(old_snapshot->appl, new_snapshot->appl)) {
  791. return;
  792. }
  793. /* old snapshot has an application, end it */
  794. if (old_snapshot && !ast_strlen_zero(old_snapshot->appl)) {
  795. cel_report_event(old_snapshot, AST_CEL_APP_END, NULL, NULL, NULL);
  796. }
  797. /* new snapshot has an application, start it */
  798. if (new_snapshot && !ast_strlen_zero(new_snapshot->appl)) {
  799. cel_report_event(new_snapshot, AST_CEL_APP_START, NULL, NULL, NULL);
  800. }
  801. }
  802. /* \brief Handlers for channel snapshot changes.
  803. * \note Order of the handlers matters. Application changes must come before state
  804. * changes to ensure that hangup notifications occur after application changes.
  805. * Linkedid checking should always come last.
  806. */
  807. cel_channel_snapshot_monitor cel_channel_monitors[] = {
  808. cel_channel_app_change,
  809. cel_channel_state_change,
  810. cel_channel_linkedid_change,
  811. };
  812. static int cel_filter_channel_snapshot(struct ast_channel_snapshot *snapshot)
  813. {
  814. if (!snapshot) {
  815. return 0;
  816. }
  817. return snapshot->tech_properties & AST_CHAN_TP_INTERNAL;
  818. }
  819. static void cel_snapshot_update_cb(void *data, struct stasis_subscription *sub,
  820. struct stasis_message *message)
  821. {
  822. struct stasis_cache_update *update = stasis_message_data(message);
  823. if (ast_channel_snapshot_type() == update->type) {
  824. struct ast_channel_snapshot *old_snapshot;
  825. struct ast_channel_snapshot *new_snapshot;
  826. size_t i;
  827. old_snapshot = stasis_message_data(update->old_snapshot);
  828. new_snapshot = stasis_message_data(update->new_snapshot);
  829. if (cel_filter_channel_snapshot(old_snapshot) || cel_filter_channel_snapshot(new_snapshot)) {
  830. return;
  831. }
  832. for (i = 0; i < ARRAY_LEN(cel_channel_monitors); ++i) {
  833. cel_channel_monitors[i](old_snapshot, new_snapshot);
  834. }
  835. }
  836. }
  837. static struct ast_str *cel_generate_peer_str(
  838. struct ast_bridge_snapshot *bridge,
  839. struct ast_channel_snapshot *chan)
  840. {
  841. struct ast_str *peer_str = ast_str_create(32);
  842. struct ao2_iterator i;
  843. char *current_chan = NULL;
  844. if (!peer_str) {
  845. return NULL;
  846. }
  847. for (i = ao2_iterator_init(bridge->channels, 0);
  848. (current_chan = ao2_iterator_next(&i));
  849. ao2_cleanup(current_chan)) {
  850. struct ast_channel_snapshot *current_snapshot;
  851. /* Don't add the channel for which this message is being generated */
  852. if (!strcmp(current_chan, chan->uniqueid)) {
  853. continue;
  854. }
  855. current_snapshot = ast_channel_snapshot_get_latest(current_chan);
  856. if (!current_snapshot) {
  857. continue;
  858. }
  859. ast_str_append(&peer_str, 0, "%s,", current_snapshot->name);
  860. ao2_cleanup(current_snapshot);
  861. }
  862. ao2_iterator_destroy(&i);
  863. /* Rip off the trailing comma */
  864. ast_str_truncate(peer_str, -1);
  865. return peer_str;
  866. }
  867. static void cel_bridge_enter_cb(
  868. void *data, struct stasis_subscription *sub,
  869. struct stasis_message *message)
  870. {
  871. struct ast_bridge_blob *blob = stasis_message_data(message);
  872. struct ast_bridge_snapshot *snapshot = blob->bridge;
  873. struct ast_channel_snapshot *chan_snapshot = blob->channel;
  874. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
  875. RAII_VAR(struct ast_str *, peer_str, NULL, ast_free);
  876. if (cel_filter_channel_snapshot(chan_snapshot)) {
  877. return;
  878. }
  879. extra = ast_json_pack("{s: s, s: s}",
  880. "bridge_id", snapshot->uniqueid,
  881. "bridge_technology", snapshot->technology);
  882. if (!extra) {
  883. return;
  884. }
  885. peer_str = cel_generate_peer_str(snapshot, chan_snapshot);
  886. if (!peer_str) {
  887. return;
  888. }
  889. cel_report_event(chan_snapshot, AST_CEL_BRIDGE_ENTER, NULL, extra, ast_str_buffer(peer_str));
  890. }
  891. static void cel_bridge_leave_cb(
  892. void *data, struct stasis_subscription *sub,
  893. struct stasis_message *message)
  894. {
  895. struct ast_bridge_blob *blob = stasis_message_data(message);
  896. struct ast_bridge_snapshot *snapshot = blob->bridge;
  897. struct ast_channel_snapshot *chan_snapshot = blob->channel;
  898. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
  899. RAII_VAR(struct ast_str *, peer_str, NULL, ast_free);
  900. if (cel_filter_channel_snapshot(chan_snapshot)) {
  901. return;
  902. }
  903. extra = ast_json_pack("{s: s, s: s}",
  904. "bridge_id", snapshot->uniqueid,
  905. "bridge_technology", snapshot->technology);
  906. if (!extra) {
  907. return;
  908. }
  909. peer_str = cel_generate_peer_str(snapshot, chan_snapshot);
  910. if (!peer_str) {
  911. return;
  912. }
  913. cel_report_event(chan_snapshot, AST_CEL_BRIDGE_EXIT, NULL, extra, ast_str_buffer(peer_str));
  914. }
  915. static void cel_parking_cb(
  916. void *data, struct stasis_subscription *sub,
  917. struct stasis_message *message)
  918. {
  919. struct ast_parked_call_payload *parked_payload = stasis_message_data(message);
  920. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
  921. const char *reason = NULL;
  922. switch (parked_payload->event_type) {
  923. case PARKED_CALL:
  924. extra = ast_json_pack("{s: s, s: s}",
  925. "parker_dial_string", parked_payload->parker_dial_string,
  926. "parking_lot", parked_payload->parkinglot);
  927. if (extra) {
  928. cel_report_event(parked_payload->parkee, AST_CEL_PARK_START, NULL, extra, NULL);
  929. }
  930. return;
  931. case PARKED_CALL_TIMEOUT:
  932. reason = "ParkedCallTimeOut";
  933. break;
  934. case PARKED_CALL_GIVEUP:
  935. reason = "ParkedCallGiveUp";
  936. break;
  937. case PARKED_CALL_UNPARKED:
  938. reason = "ParkedCallUnparked";
  939. break;
  940. case PARKED_CALL_FAILED:
  941. reason = "ParkedCallFailed";
  942. break;
  943. case PARKED_CALL_SWAP:
  944. reason = "ParkedCallSwap";
  945. break;
  946. }
  947. if (parked_payload->retriever) {
  948. extra = ast_json_pack("{s: s, s: s}",
  949. "reason", reason ?: "",
  950. "retriever", parked_payload->retriever->name);
  951. } else {
  952. extra = ast_json_pack("{s: s}", "reason", reason ?: "");
  953. }
  954. if (extra) {
  955. cel_report_event(parked_payload->parkee, AST_CEL_PARK_END, NULL, extra, NULL);
  956. }
  957. }
  958. static void save_dialstatus(struct ast_multi_channel_blob *blob, struct ast_channel_snapshot *snapshot)
  959. {
  960. struct ao2_container *dial_statuses = ao2_global_obj_ref(cel_dialstatus_store);
  961. const char *dialstatus_string = get_blob_variable(blob, "dialstatus");
  962. struct cel_dialstatus *dialstatus;
  963. size_t dialstatus_string_len;
  964. if (!dial_statuses || ast_strlen_zero(dialstatus_string)) {
  965. ao2_cleanup(dial_statuses);
  966. return;
  967. }
  968. dialstatus = ao2_find(dial_statuses, snapshot->uniqueid, OBJ_SEARCH_KEY);
  969. if (dialstatus) {
  970. if (!strcasecmp(dialstatus_string, "ANSWER") && strcasecmp(dialstatus->dialstatus, "ANSWER")) {
  971. /* In the case of an answer after we already have a dial status we give
  972. * priority to the answer since the call was, well, answered. In the case of
  973. * failure dial status results we simply let the first failure be the status.
  974. */
  975. ao2_unlink(dial_statuses, dialstatus);
  976. ao2_ref(dialstatus, -1);
  977. } else {
  978. ao2_ref(dialstatus, -1);
  979. ao2_ref(dial_statuses, -1);
  980. return;
  981. }
  982. }
  983. dialstatus_string_len = strlen(dialstatus_string) + 1;
  984. dialstatus = ao2_alloc_options(sizeof(*dialstatus) + dialstatus_string_len, NULL,
  985. AO2_ALLOC_OPT_LOCK_NOLOCK);
  986. if (!dialstatus) {
  987. ao2_ref(dial_statuses, -1);
  988. return;
  989. }
  990. ast_copy_string(dialstatus->uniqueid, snapshot->uniqueid, sizeof(dialstatus->uniqueid));
  991. ast_copy_string(dialstatus->dialstatus, dialstatus_string, dialstatus_string_len);
  992. ao2_link(dial_statuses, dialstatus);
  993. ao2_ref(dialstatus, -1);
  994. ao2_ref(dial_statuses, -1);
  995. }
  996. static int is_valid_dialstatus(struct ast_multi_channel_blob *blob)
  997. {
  998. const char *dialstatus = get_blob_variable(blob, "dialstatus");
  999. int res = 0;
  1000. if (ast_strlen_zero(dialstatus)) {
  1001. res = 0;
  1002. } else if (!strcasecmp(dialstatus, "CHANUNAVAIL")) {
  1003. res = 1;
  1004. } else if (!strcasecmp(dialstatus, "CONGESTION")) {
  1005. res = 1;
  1006. } else if (!strcasecmp(dialstatus, "NOANSWER")) {
  1007. res = 1;
  1008. } else if (!strcasecmp(dialstatus, "BUSY")) {
  1009. res = 1;
  1010. } else if (!strcasecmp(dialstatus, "ANSWER")) {
  1011. res = 1;
  1012. } else if (!strcasecmp(dialstatus, "CANCEL")) {
  1013. res = 1;
  1014. } else if (!strcasecmp(dialstatus, "DONTCALL")) {
  1015. res = 1;
  1016. } else if (!strcasecmp(dialstatus, "TORTURE")) {
  1017. res = 1;
  1018. } else if (!strcasecmp(dialstatus, "INVALIDARGS")) {
  1019. res = 1;
  1020. }
  1021. return res;
  1022. }
  1023. static void cel_dial_cb(void *data, struct stasis_subscription *sub,
  1024. struct stasis_message *message)
  1025. {
  1026. struct ast_multi_channel_blob *blob = stasis_message_data(message);
  1027. struct ast_channel_snapshot *snapshot;
  1028. snapshot = ast_multi_channel_blob_get_channel(blob, "caller");
  1029. if (!snapshot || cel_filter_channel_snapshot(snapshot)) {
  1030. return;
  1031. }
  1032. if (!ast_strlen_zero(get_blob_variable(blob, "forward"))) {
  1033. struct ast_json *extra;
  1034. extra = ast_json_pack("{s: s}", "forward", get_blob_variable(blob, "forward"));
  1035. if (extra) {
  1036. cel_report_event(snapshot, AST_CEL_FORWARD, NULL, extra, NULL);
  1037. ast_json_unref(extra);
  1038. }
  1039. }
  1040. if (is_valid_dialstatus(blob)) {
  1041. save_dialstatus(blob, snapshot);
  1042. }
  1043. }
  1044. static void cel_generic_cb(
  1045. void *data, struct stasis_subscription *sub,
  1046. struct stasis_message *message)
  1047. {
  1048. struct ast_channel_blob *obj = stasis_message_data(message);
  1049. int event_type = ast_json_integer_get(ast_json_object_get(obj->blob, "event_type"));
  1050. struct ast_json *event_details = ast_json_object_get(obj->blob, "event_details");
  1051. switch (event_type) {
  1052. case AST_CEL_USER_DEFINED:
  1053. {
  1054. const char *event = ast_json_string_get(ast_json_object_get(event_details, "event"));
  1055. struct ast_json *extra = ast_json_object_get(event_details, "extra");
  1056. cel_report_event(obj->snapshot, event_type, event, extra, NULL);
  1057. break;
  1058. }
  1059. default:
  1060. ast_log(LOG_ERROR, "Unhandled %s event blob\n", ast_cel_get_type_name(event_type));
  1061. break;
  1062. }
  1063. }
  1064. static void cel_blind_transfer_cb(
  1065. void *data, struct stasis_subscription *sub,
  1066. struct stasis_message *message)
  1067. {
  1068. struct ast_blind_transfer_message *transfer_msg = stasis_message_data(message);
  1069. struct ast_channel_snapshot *chan_snapshot = transfer_msg->transferer;
  1070. struct ast_bridge_snapshot *bridge_snapshot = transfer_msg->bridge;
  1071. struct ast_json *extra;
  1072. if (transfer_msg->result != AST_BRIDGE_TRANSFER_SUCCESS) {
  1073. return;
  1074. }
  1075. extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}",
  1076. "extension", transfer_msg->exten,
  1077. "context", transfer_msg->context,
  1078. "bridge_id", bridge_snapshot->uniqueid,
  1079. "transferee_channel_name", transfer_msg->transferee ? transfer_msg->transferee->name : "N/A",
  1080. "transferee_channel_uniqueid", transfer_msg->transferee ? transfer_msg->transferee->uniqueid : "N/A");
  1081. if (extra) {
  1082. cel_report_event(chan_snapshot, AST_CEL_BLINDTRANSFER, NULL, extra, NULL);
  1083. ast_json_unref(extra);
  1084. }
  1085. }
  1086. static void cel_attended_transfer_cb(
  1087. void *data, struct stasis_subscription *sub,
  1088. struct stasis_message *message)
  1089. {
  1090. struct ast_attended_transfer_message *xfer = stasis_message_data(message);
  1091. struct ast_json *extra = NULL;
  1092. struct ast_bridge_snapshot *bridge1, *bridge2;
  1093. struct ast_channel_snapshot *channel1, *channel2;
  1094. /* Make sure bridge1 is always non-NULL */
  1095. if (!xfer->to_transferee.bridge_snapshot) {
  1096. bridge1 = xfer->to_transfer_target.bridge_snapshot;
  1097. bridge2 = xfer->to_transferee.bridge_snapshot;
  1098. channel1 = xfer->to_transfer_target.channel_snapshot;
  1099. channel2 = xfer->to_transferee.channel_snapshot;
  1100. } else {
  1101. bridge1 = xfer->to_transferee.bridge_snapshot;
  1102. bridge2 = xfer->to_transfer_target.bridge_snapshot;
  1103. channel1 = xfer->to_transferee.channel_snapshot;
  1104. channel2 = xfer->to_transfer_target.channel_snapshot;
  1105. }
  1106. switch (xfer->dest_type) {
  1107. case AST_ATTENDED_TRANSFER_DEST_FAIL:
  1108. return;
  1109. /* handle these three the same */
  1110. case AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE:
  1111. case AST_ATTENDED_TRANSFER_DEST_LINK:
  1112. case AST_ATTENDED_TRANSFER_DEST_THREEWAY:
  1113. extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: s, s: s}",
  1114. "bridge1_id", bridge1->uniqueid,
  1115. "channel2_name", channel2->name,
  1116. "channel2_uniqueid", channel2->uniqueid,
  1117. "bridge2_id", bridge2->uniqueid,
  1118. "transferee_channel_name", xfer->transferee ? xfer->transferee->name : "N/A",
  1119. "transferee_channel_uniqueid", xfer->transferee ? xfer->transferee->uniqueid : "N/A",
  1120. "transfer_target_channel_name", xfer->target ? xfer->target->name : "N/A",
  1121. "transfer_target_channel_uniqueid", xfer->target ? xfer->target->uniqueid : "N/A");
  1122. if (!extra) {
  1123. return;
  1124. }
  1125. break;
  1126. case AST_ATTENDED_TRANSFER_DEST_APP:
  1127. case AST_ATTENDED_TRANSFER_DEST_LOCAL_APP:
  1128. extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: s, s: s}",
  1129. "bridge1_id", bridge1->uniqueid,
  1130. "channel2_name", channel2->name,
  1131. "channel2_uniqueid", channel2->uniqueid,
  1132. "app", xfer->dest.app,
  1133. "transferee_channel_name", xfer->transferee ? xfer->transferee->name : "N/A",
  1134. "transferee_channel_uniqueid", xfer->transferee ? xfer->transferee->uniqueid : "N/A",
  1135. "transfer_target_channel_name", xfer->target ? xfer->target->name : "N/A",
  1136. "transfer_target_channel_uniqueid", xfer->target ? xfer->target->uniqueid : "N/A");
  1137. if (!extra) {
  1138. return;
  1139. }
  1140. break;
  1141. }
  1142. cel_report_event(channel1, AST_CEL_ATTENDEDTRANSFER, NULL, extra, NULL);
  1143. ast_json_unref(extra);
  1144. }
  1145. static void cel_pickup_cb(
  1146. void *data, struct stasis_subscription *sub,
  1147. struct stasis_message *message)
  1148. {
  1149. struct ast_multi_channel_blob *obj = stasis_message_data(message);
  1150. struct ast_channel_snapshot *channel = ast_multi_channel_blob_get_channel(obj, "channel");
  1151. struct ast_channel_snapshot *target = ast_multi_channel_blob_get_channel(obj, "target");
  1152. struct ast_json *extra;
  1153. if (!channel || !target) {
  1154. return;
  1155. }
  1156. extra = ast_json_pack("{s: s, s: s}",
  1157. "pickup_channel", channel->name,
  1158. "pickup_channel_uniqueid", channel->uniqueid);
  1159. if (!extra) {
  1160. return;
  1161. }
  1162. cel_report_event(target, AST_CEL_PICKUP, NULL, extra, NULL);
  1163. ast_json_unref(extra);
  1164. }
  1165. static void cel_local_cb(
  1166. void *data, struct stasis_subscription *sub,
  1167. struct stasis_message *message)
  1168. {
  1169. struct ast_multi_channel_blob *obj = stasis_message_data(message);
  1170. struct ast_channel_snapshot *localone = ast_multi_channel_blob_get_channel(obj, "1");
  1171. struct ast_channel_snapshot *localtwo = ast_multi_channel_blob_get_channel(obj, "2");
  1172. struct ast_json *extra;
  1173. if (!localone || !localtwo) {
  1174. return;
  1175. }
  1176. extra = ast_json_pack("{s: s, s: s}",
  1177. "local_two", localtwo->name,
  1178. "local_two_uniqueid", localtwo->uniqueid);
  1179. if (!extra) {
  1180. return;
  1181. }
  1182. cel_report_event(localone, AST_CEL_LOCAL_OPTIMIZE, NULL, extra, NULL);
  1183. ast_json_unref(extra);
  1184. }
  1185. static void destroy_routes(void)
  1186. {
  1187. stasis_message_router_unsubscribe_and_join(cel_state_router);
  1188. cel_state_router = NULL;
  1189. }
  1190. static void destroy_subscriptions(void)
  1191. {
  1192. ao2_cleanup(cel_aggregation_topic);
  1193. cel_aggregation_topic = NULL;
  1194. ao2_cleanup(cel_topic);
  1195. cel_topic = NULL;
  1196. cel_channel_forwarder = stasis_forward_cancel(cel_channel_forwarder);
  1197. cel_bridge_forwarder = stasis_forward_cancel(cel_bridge_forwarder);
  1198. cel_parking_forwarder = stasis_forward_cancel(cel_parking_forwarder);
  1199. cel_cel_forwarder = stasis_forward_cancel(cel_cel_forwarder);
  1200. }
  1201. static void cel_engine_cleanup(void)
  1202. {
  1203. destroy_routes();
  1204. destroy_subscriptions();
  1205. STASIS_MESSAGE_TYPE_CLEANUP(cel_generic_type);
  1206. ast_cli_unregister(&cli_status);
  1207. aco_info_destroy(&cel_cfg_info);
  1208. ao2_global_obj_release(cel_configs);
  1209. ao2_global_obj_release(cel_dialstatus_store);
  1210. ao2_global_obj_release(cel_linkedids);
  1211. ao2_global_obj_release(cel_backends);
  1212. }
  1213. /*!
  1214. * \brief Create the Stasis subscriptions for CEL
  1215. */
  1216. static int create_subscriptions(void)
  1217. {
  1218. cel_aggregation_topic = stasis_topic_create("cel_aggregation_topic");
  1219. if (!cel_aggregation_topic) {
  1220. return -1;
  1221. }
  1222. cel_topic = stasis_topic_create("cel_topic");
  1223. if (!cel_topic) {
  1224. return -1;
  1225. }
  1226. cel_channel_forwarder = stasis_forward_all(
  1227. ast_channel_topic_all_cached(),
  1228. cel_aggregation_topic);
  1229. if (!cel_channel_forwarder) {
  1230. return -1;
  1231. }
  1232. cel_bridge_forwarder = stasis_forward_all(
  1233. ast_bridge_topic_all_cached(),
  1234. cel_aggregation_topic);
  1235. if (!cel_bridge_forwarder) {
  1236. return -1;
  1237. }
  1238. cel_parking_forwarder = stasis_forward_all(
  1239. ast_parking_topic(),
  1240. cel_aggregation_topic);
  1241. if (!cel_parking_forwarder) {
  1242. return -1;
  1243. }
  1244. cel_cel_forwarder = stasis_forward_all(
  1245. ast_cel_topic(),
  1246. cel_aggregation_topic);
  1247. if (!cel_cel_forwarder) {
  1248. return -1;
  1249. }
  1250. return 0;
  1251. }
  1252. /*!
  1253. * \brief Create the Stasis message router and routes for CEL
  1254. */
  1255. static int create_routes(void)
  1256. {
  1257. int ret = 0;
  1258. cel_state_router = stasis_message_router_create(cel_aggregation_topic);
  1259. if (!cel_state_router) {
  1260. return -1;
  1261. }
  1262. stasis_message_router_set_congestion_limits(cel_state_router, -1,
  1263. 6 * AST_TASKPROCESSOR_HIGH_WATER_LEVEL);
  1264. ret |= stasis_message_router_add(cel_state_router,
  1265. stasis_cache_update_type(),
  1266. cel_snapshot_update_cb,
  1267. NULL);
  1268. ret |= stasis_message_router_add(cel_state_router,
  1269. ast_channel_dial_type(),
  1270. cel_dial_cb,
  1271. NULL);
  1272. ret |= stasis_message_router_add(cel_state_router,
  1273. ast_channel_entered_bridge_type(),
  1274. cel_bridge_enter_cb,
  1275. NULL);
  1276. ret |= stasis_message_router_add(cel_state_router,
  1277. ast_channel_left_bridge_type(),
  1278. cel_bridge_leave_cb,
  1279. NULL);
  1280. ret |= stasis_message_router_add(cel_state_router,
  1281. ast_parked_call_type(),
  1282. cel_parking_cb,
  1283. NULL);
  1284. ret |= stasis_message_router_add(cel_state_router,
  1285. cel_generic_type(),
  1286. cel_generic_cb,
  1287. NULL);
  1288. ret |= stasis_message_router_add(cel_state_router,
  1289. ast_blind_transfer_type(),
  1290. cel_blind_transfer_cb,
  1291. NULL);
  1292. ret |= stasis_message_router_add(cel_state_router,
  1293. ast_attended_transfer_type(),
  1294. cel_attended_transfer_cb,
  1295. NULL);
  1296. ret |= stasis_message_router_add(cel_state_router,
  1297. ast_call_pickup_type(),
  1298. cel_pickup_cb,
  1299. NULL);
  1300. ret |= stasis_message_router_add(cel_state_router,
  1301. ast_local_optimization_end_type(),
  1302. cel_local_cb,
  1303. NULL);
  1304. if (ret) {
  1305. ast_log(AST_LOG_ERROR, "Failed to register for Stasis messages\n");
  1306. }
  1307. return ret;
  1308. }
  1309. AO2_STRING_FIELD_HASH_FN(cel_linkedid, id)
  1310. AO2_STRING_FIELD_CMP_FN(cel_linkedid, id)
  1311. int ast_cel_engine_init(void)
  1312. {
  1313. struct ao2_container *container;
  1314. container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NUM_APP_BUCKETS,
  1315. cel_linkedid_hash_fn, NULL, cel_linkedid_cmp_fn);
  1316. ao2_global_obj_replace_unref(cel_linkedids, container);
  1317. ao2_cleanup(container);
  1318. if (!container) {
  1319. return -1;
  1320. }
  1321. container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  1322. NUM_DIALSTATUS_BUCKETS, cel_dialstatus_hash_fn, NULL, cel_dialstatus_cmp_fn);
  1323. ao2_global_obj_replace_unref(cel_dialstatus_store, container);
  1324. ao2_cleanup(container);
  1325. if (!container) {
  1326. return -1;
  1327. }
  1328. if (STASIS_MESSAGE_TYPE_INIT(cel_generic_type)) {
  1329. return -1;
  1330. }
  1331. if (ast_cli_register(&cli_status)) {
  1332. return -1;
  1333. }
  1334. container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, BACKEND_BUCKETS,
  1335. cel_backend_hash_fn, NULL, cel_backend_cmp_fn);
  1336. ao2_global_obj_replace_unref(cel_backends, container);
  1337. ao2_cleanup(container);
  1338. if (!container) {
  1339. return -1;
  1340. }
  1341. if (aco_info_init(&cel_cfg_info)) {
  1342. return -1;
  1343. }
  1344. aco_option_register(&cel_cfg_info, "enable", ACO_EXACT, general_options, "no", OPT_BOOL_T, 1, FLDSET(struct ast_cel_general_config, enable));
  1345. aco_option_register(&cel_cfg_info, "dateformat", ACO_EXACT, general_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_cel_general_config, date_format));
  1346. aco_option_register_custom(&cel_cfg_info, "apps", ACO_EXACT, general_options, "", apps_handler, 0);
  1347. aco_option_register_custom(&cel_cfg_info, "events", ACO_EXACT, general_options, "", events_handler, 0);
  1348. if (aco_process_config(&cel_cfg_info, 0)) {
  1349. struct cel_config *cel_cfg = cel_config_alloc();
  1350. if (!cel_cfg) {
  1351. return -1;
  1352. }
  1353. /* We couldn't process the configuration so create a default config. */
  1354. if (!aco_set_defaults(&general_option, "general", cel_cfg->general)) {
  1355. ast_log(LOG_NOTICE, "Failed to process CEL configuration; using defaults\n");
  1356. ao2_global_obj_replace_unref(cel_configs, cel_cfg);
  1357. }
  1358. ao2_ref(cel_cfg, -1);
  1359. }
  1360. if (create_subscriptions()) {
  1361. return -1;
  1362. }
  1363. if (ast_cel_check_enabled() && create_routes()) {
  1364. return -1;
  1365. }
  1366. ast_register_cleanup(cel_engine_cleanup);
  1367. return 0;
  1368. }
  1369. int ast_cel_engine_reload(void)
  1370. {
  1371. unsigned int was_enabled = ast_cel_check_enabled();
  1372. unsigned int is_enabled;
  1373. if (aco_process_config(&cel_cfg_info, 1) == ACO_PROCESS_ERROR) {
  1374. return -1;
  1375. }
  1376. is_enabled = ast_cel_check_enabled();
  1377. if (!was_enabled && is_enabled) {
  1378. if (create_routes()) {
  1379. return -1;
  1380. }
  1381. } else if (was_enabled && !is_enabled) {
  1382. destroy_routes();
  1383. }
  1384. ast_verb(3, "CEL logging %sabled.\n", is_enabled ? "en" : "dis");
  1385. return 0;
  1386. }
  1387. void ast_cel_publish_event(struct ast_channel *chan,
  1388. enum ast_cel_event_type event_type,
  1389. struct ast_json *blob)
  1390. {
  1391. struct ast_json *cel_blob;
  1392. struct stasis_message *message;
  1393. cel_blob = ast_json_pack("{s: i, s: o}",
  1394. "event_type", event_type,
  1395. "event_details", ast_json_ref(blob));
  1396. message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan), cel_generic_type(), cel_blob);
  1397. if (message) {
  1398. stasis_publish(ast_cel_topic(), message);
  1399. }
  1400. ao2_cleanup(message);
  1401. ast_json_unref(cel_blob);
  1402. }
  1403. struct stasis_topic *ast_cel_topic(void)
  1404. {
  1405. return cel_topic;
  1406. }
  1407. struct ast_cel_general_config *ast_cel_get_config(void)
  1408. {
  1409. RAII_VAR(struct cel_config *, mod_cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
  1410. if (!mod_cfg || !mod_cfg->general) {
  1411. return NULL;
  1412. }
  1413. ao2_ref(mod_cfg->general, +1);
  1414. return mod_cfg->general;
  1415. }
  1416. void ast_cel_set_config(struct ast_cel_general_config *config)
  1417. {
  1418. int was_enabled;
  1419. int is_enabled;
  1420. struct ast_cel_general_config *cleanup_config;
  1421. struct cel_config *mod_cfg = ao2_global_obj_ref(cel_configs);
  1422. if (mod_cfg) {
  1423. was_enabled = ast_cel_check_enabled();
  1424. cleanup_config = mod_cfg->general;
  1425. ao2_bump(config);
  1426. mod_cfg->general = config;
  1427. ao2_cleanup(cleanup_config);
  1428. is_enabled = ast_cel_check_enabled();
  1429. if (!was_enabled && is_enabled) {
  1430. create_routes();
  1431. } else if (was_enabled && !is_enabled) {
  1432. destroy_routes();
  1433. }
  1434. ao2_ref(mod_cfg, -1);
  1435. }
  1436. }
  1437. int ast_cel_backend_unregister(const char *name)
  1438. {
  1439. struct ao2_container *backends = ao2_global_obj_ref(cel_backends);
  1440. if (backends) {
  1441. ao2_find(backends, name, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK);
  1442. ao2_ref(backends, -1);
  1443. }
  1444. return 0;
  1445. }
  1446. int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
  1447. {
  1448. RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
  1449. struct cel_backend *backend;
  1450. if (!backends || ast_strlen_zero(name) || !backend_callback) {
  1451. return -1;
  1452. }
  1453. /* The backend object is immutable so it doesn't need a lock of its own. */
  1454. backend = ao2_alloc_options(sizeof(*backend) + 1 + strlen(name), NULL,
  1455. AO2_ALLOC_OPT_LOCK_NOLOCK);
  1456. if (!backend) {
  1457. return -1;
  1458. }
  1459. strcpy(backend->name, name);/* Safe */
  1460. backend->callback = backend_callback;
  1461. ao2_link(backends, backend);
  1462. ao2_ref(backend, -1);
  1463. return 0;
  1464. }