chan_local.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@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. * \author Mark Spencer <markster@digium.com>
  21. *
  22. * \brief Local Proxy Channel
  23. *
  24. * \ingroup channel_drivers
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <fcntl.h>
  32. #include <sys/signal.h>
  33. #include "asterisk/lock.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/config.h"
  36. #include "asterisk/module.h"
  37. #include "asterisk/pbx.h"
  38. #include "asterisk/sched.h"
  39. #include "asterisk/io.h"
  40. #include "asterisk/acl.h"
  41. #include "asterisk/callerid.h"
  42. #include "asterisk/file.h"
  43. #include "asterisk/cli.h"
  44. #include "asterisk/app.h"
  45. #include "asterisk/musiconhold.h"
  46. #include "asterisk/manager.h"
  47. #include "asterisk/stringfields.h"
  48. #include "asterisk/devicestate.h"
  49. #include "asterisk/astobj2.h"
  50. /*** DOCUMENTATION
  51. <manager name="LocalOptimizeAway" language="en_US">
  52. <synopsis>
  53. Optimize away a local channel when possible.
  54. </synopsis>
  55. <syntax>
  56. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  57. <parameter name="Channel" required="true">
  58. <para>The channel name to optimize away.</para>
  59. </parameter>
  60. </syntax>
  61. <description>
  62. <para>A local channel created with "/n" will not automatically optimize away.
  63. Calling this command on the local channel will clear that flag and allow
  64. it to optimize away if it's bridged or when it becomes bridged.</para>
  65. </description>
  66. </manager>
  67. ***/
  68. static const char tdesc[] = "Local Proxy Channel Driver";
  69. #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
  70. /* right now we are treating the locals astobj2 container as a
  71. * list. If there is ever a reason to make this more efficient
  72. * increasing the bucket size would help. */
  73. static const int BUCKET_SIZE = 1;
  74. static struct ao2_container *locals;
  75. static struct ast_jb_conf g_jb_conf = {
  76. .flags = 0,
  77. .max_size = -1,
  78. .resync_threshold = -1,
  79. .impl = "",
  80. .target_extra = -1,
  81. };
  82. static struct ast_channel *local_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
  83. static int local_digit_begin(struct ast_channel *ast, char digit);
  84. static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
  85. static int local_call(struct ast_channel *ast, char *dest, int timeout);
  86. static int local_hangup(struct ast_channel *ast);
  87. static int local_answer(struct ast_channel *ast);
  88. static struct ast_frame *local_read(struct ast_channel *ast);
  89. static int local_write(struct ast_channel *ast, struct ast_frame *f);
  90. static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
  91. static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  92. static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
  93. static int local_sendtext(struct ast_channel *ast, const char *text);
  94. static int local_devicestate(void *data);
  95. static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
  96. static int local_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
  97. static int local_setoption(struct ast_channel *chan, int option, void *data, int datalen);
  98. /* PBX interface structure for channel registration */
  99. static const struct ast_channel_tech local_tech = {
  100. .type = "Local",
  101. .description = tdesc,
  102. .capabilities = -1,
  103. .requester = local_request,
  104. .send_digit_begin = local_digit_begin,
  105. .send_digit_end = local_digit_end,
  106. .call = local_call,
  107. .hangup = local_hangup,
  108. .answer = local_answer,
  109. .read = local_read,
  110. .write = local_write,
  111. .write_video = local_write,
  112. .exception = local_read,
  113. .indicate = local_indicate,
  114. .fixup = local_fixup,
  115. .send_html = local_sendhtml,
  116. .send_text = local_sendtext,
  117. .devicestate = local_devicestate,
  118. .bridged_channel = local_bridgedchannel,
  119. .queryoption = local_queryoption,
  120. .setoption = local_setoption,
  121. };
  122. /*! \brief the local pvt structure for all channels
  123. The local channel pvt has two ast_chan objects - the "owner" and the "next channel", the outbound channel
  124. ast_chan owner -> local_pvt -> ast_chan chan -> yet-another-pvt-depending-on-channel-type
  125. */
  126. struct local_pvt {
  127. unsigned int flags; /*!< Private flags */
  128. char context[AST_MAX_CONTEXT]; /*!< Context to call */
  129. char exten[AST_MAX_EXTENSION]; /*!< Extension to call */
  130. format_t reqformat; /*!< Requested format */
  131. struct ast_jb_conf jb_conf; /*!< jitterbuffer configuration for this local channel */
  132. struct ast_channel *owner; /*!< Master Channel - Bridging happens here */
  133. struct ast_channel *chan; /*!< Outbound channel - PBX is run here */
  134. struct ast_module_user *u_owner; /*!< reference to keep the module loaded while in use */
  135. struct ast_module_user *u_chan; /*!< reference to keep the module loaded while in use */
  136. };
  137. #define LOCAL_ALREADY_MASQED (1 << 0) /*!< Already masqueraded */
  138. #define LOCAL_LAUNCHED_PBX (1 << 1) /*!< PBX was launched */
  139. #define LOCAL_NO_OPTIMIZATION (1 << 2) /*!< Do not optimize using masquerading */
  140. #define LOCAL_BRIDGE (1 << 3) /*!< Report back the "true" channel as being bridged to */
  141. #define LOCAL_MOH_PASSTHRU (1 << 4) /*!< Pass through music on hold start/stop frames */
  142. /*
  143. * \brief Send a pvt in with no locks held and get all locks
  144. *
  145. * \note NO locks should be held prior to calling this function
  146. * \note The pvt must have a ref held before calling this function
  147. * \note if outchan or outowner is set != NULL after calling this function
  148. * those channels are locked and reffed.
  149. * \note Batman.
  150. */
  151. static void awesome_locking(struct local_pvt *p, struct ast_channel **outchan, struct ast_channel **outowner)
  152. {
  153. struct ast_channel *chan = NULL;
  154. struct ast_channel *owner = NULL;
  155. for (;;) {
  156. ao2_lock(p);
  157. if (p->chan) {
  158. chan = p->chan;
  159. ast_channel_ref(chan);
  160. }
  161. if (p->owner) {
  162. owner = p->owner;
  163. ast_channel_ref(owner);
  164. }
  165. ao2_unlock(p);
  166. /* if we don't have both channels, then this is very easy */
  167. if (!owner || !chan) {
  168. if (owner) {
  169. ast_channel_lock(owner);
  170. } else if(chan) {
  171. ast_channel_lock(chan);
  172. }
  173. ao2_lock(p);
  174. } else {
  175. /* lock both channels first, then get the pvt lock */
  176. ast_channel_lock(chan);
  177. while (ast_channel_trylock(owner)) {
  178. CHANNEL_DEADLOCK_AVOIDANCE(chan);
  179. }
  180. ao2_lock(p);
  181. }
  182. /* Now that we have all the locks, validate that nothing changed */
  183. if (p->owner != owner || p->chan != chan) {
  184. if (owner) {
  185. ast_channel_unlock(owner);
  186. owner = ast_channel_unref(owner);
  187. }
  188. if (chan) {
  189. ast_channel_unlock(chan);
  190. chan = ast_channel_unref(chan);
  191. }
  192. ao2_unlock(p);
  193. continue;
  194. }
  195. break;
  196. }
  197. *outowner = p->owner;
  198. *outchan = p->chan;
  199. }
  200. /* Called with ast locked */
  201. static int local_setoption(struct ast_channel *ast, int option, void * data, int datalen)
  202. {
  203. int res = 0;
  204. struct local_pvt *p = NULL;
  205. struct ast_channel *otherchan = NULL;
  206. ast_chan_write_info_t *write_info;
  207. if (option != AST_OPTION_CHANNEL_WRITE) {
  208. return -1;
  209. }
  210. write_info = data;
  211. if (write_info->version != AST_CHAN_WRITE_INFO_T_VERSION) {
  212. ast_log(LOG_ERROR, "The chan_write_info_t type has changed, and this channel hasn't been updated!\n");
  213. return -1;
  214. }
  215. /* get the tech pvt */
  216. if (!(p = ast->tech_pvt)) {
  217. return -1;
  218. }
  219. ao2_ref(p, 1);
  220. ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
  221. /* get the channel we are supposed to write to */
  222. ao2_lock(p);
  223. otherchan = (write_info->chan == p->owner) ? p->chan : p->owner;
  224. if (!otherchan || otherchan == write_info->chan) {
  225. res = -1;
  226. otherchan = NULL;
  227. ao2_unlock(p);
  228. goto setoption_cleanup;
  229. }
  230. ast_channel_ref(otherchan);
  231. /* clear the pvt lock before grabbing the channel */
  232. ao2_unlock(p);
  233. ast_channel_lock(otherchan);
  234. res = write_info->write_fn(otherchan, write_info->function, write_info->data, write_info->value);
  235. ast_channel_unlock(otherchan);
  236. setoption_cleanup:
  237. if (p) {
  238. ao2_ref(p, -1);
  239. }
  240. if (otherchan) {
  241. ast_channel_unref(otherchan);
  242. }
  243. ast_channel_lock(ast); /* Lock back before we leave */
  244. return res;
  245. }
  246. /*! \brief Adds devicestate to local channels */
  247. static int local_devicestate(void *data)
  248. {
  249. char *exten = ast_strdupa(data);
  250. char *context = NULL, *opts = NULL;
  251. int res;
  252. struct local_pvt *lp;
  253. struct ao2_iterator it;
  254. if (!(context = strchr(exten, '@'))) {
  255. ast_log(LOG_WARNING, "Someone used Local/%s somewhere without a @context. This is bad.\n", exten);
  256. return AST_DEVICE_INVALID;
  257. }
  258. *context++ = '\0';
  259. /* Strip options if they exist */
  260. if ((opts = strchr(context, '/')))
  261. *opts = '\0';
  262. ast_debug(3, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
  263. res = ast_exists_extension(NULL, context, exten, 1, NULL);
  264. if (!res)
  265. return AST_DEVICE_INVALID;
  266. res = AST_DEVICE_NOT_INUSE;
  267. it = ao2_iterator_init(locals, 0);
  268. while ((lp = ao2_iterator_next(&it))) {
  269. if (!strcmp(exten, lp->exten) && !strcmp(context, lp->context) && lp->owner) {
  270. res = AST_DEVICE_INUSE;
  271. ao2_ref(lp, -1);
  272. break;
  273. }
  274. ao2_ref(lp, -1);
  275. }
  276. ao2_iterator_destroy(&it);
  277. return res;
  278. }
  279. /*! \brief Return the bridged channel of a Local channel */
  280. static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge)
  281. {
  282. struct local_pvt *p = bridge->tech_pvt;
  283. struct ast_channel *bridged = bridge;
  284. if (!p) {
  285. ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning <none>\n",
  286. chan->name, bridge->name);
  287. return NULL;
  288. }
  289. ao2_lock(p);
  290. if (ast_test_flag(p, LOCAL_BRIDGE)) {
  291. /* Find the opposite channel */
  292. bridged = (bridge == p->owner ? p->chan : p->owner);
  293. /* Now see if the opposite channel is bridged to anything */
  294. if (!bridged) {
  295. bridged = bridge;
  296. } else if (bridged->_bridge) {
  297. bridged = bridged->_bridge;
  298. }
  299. }
  300. ao2_unlock(p);
  301. return bridged;
  302. }
  303. /* Called with ast locked */
  304. static int local_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
  305. {
  306. struct local_pvt *p;
  307. struct ast_channel *bridged = NULL;
  308. struct ast_channel *tmp = NULL;
  309. int res = 0;
  310. if (option != AST_OPTION_T38_STATE) {
  311. /* AST_OPTION_T38_STATE is the only supported option at this time */
  312. return -1;
  313. }
  314. /* for some reason the channel is not locked in channel.c when this function is called */
  315. if (!(p = ast->tech_pvt)) {
  316. return -1;
  317. }
  318. ao2_lock(p);
  319. if (!(tmp = IS_OUTBOUND(ast, p) ? p->owner : p->chan)) {
  320. ao2_unlock(p);
  321. return -1;
  322. }
  323. ast_channel_ref(tmp);
  324. ao2_unlock(p);
  325. ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
  326. ast_channel_lock(tmp);
  327. if (!(bridged = ast_bridged_channel(tmp))) {
  328. res = -1;
  329. ast_channel_unlock(tmp);
  330. goto query_cleanup;
  331. }
  332. ast_channel_ref(bridged);
  333. ast_channel_unlock(tmp);
  334. query_cleanup:
  335. if (bridged) {
  336. res = ast_channel_queryoption(bridged, option, data, datalen, 0);
  337. bridged = ast_channel_unref(bridged);
  338. }
  339. if (tmp) {
  340. tmp = ast_channel_unref(tmp);
  341. }
  342. ast_channel_lock(ast); /* Lock back before we leave */
  343. return res;
  344. }
  345. /*! \brief queue a frame on a to either the p->owner or p->chan
  346. *
  347. * \note the local_pvt MUST have it's ref count bumped before entering this function and
  348. * decremented after this function is called. This is a side effect of the deadlock
  349. * avoidance that is necessary to lock 2 channels and a tech_pvt. Without a ref counted
  350. * local_pvt, it is impossible to guarantee it will not be destroyed by another thread
  351. * during deadlock avoidance.
  352. */
  353. static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f,
  354. struct ast_channel *us, int us_locked)
  355. {
  356. struct ast_channel *other = NULL;
  357. /* Recalculate outbound channel */
  358. other = isoutbound ? p->owner : p->chan;
  359. if (!other) {
  360. return 0;
  361. }
  362. /* do not queue frame if generator is on both local channels */
  363. if (us && us->generator && other->generator) {
  364. return 0;
  365. }
  366. /* grab a ref on the channel before unlocking the pvt,
  367. * other can not go away from us now regardless of locking */
  368. ast_channel_ref(other);
  369. if (us && us_locked) {
  370. ast_channel_unlock(us);
  371. }
  372. ao2_unlock(p);
  373. if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_RINGING) {
  374. ast_setstate(other, AST_STATE_RINGING);
  375. }
  376. ast_queue_frame(other, f);
  377. other = ast_channel_unref(other);
  378. if (us && us_locked) {
  379. ast_channel_lock(us);
  380. }
  381. ao2_lock(p);
  382. return 0;
  383. }
  384. static int local_answer(struct ast_channel *ast)
  385. {
  386. struct local_pvt *p = ast->tech_pvt;
  387. int isoutbound;
  388. int res = -1;
  389. if (!p)
  390. return -1;
  391. ao2_lock(p);
  392. ao2_ref(p, 1);
  393. isoutbound = IS_OUTBOUND(ast, p);
  394. if (isoutbound) {
  395. /* Pass along answer since somebody answered us */
  396. struct ast_frame answer = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
  397. res = local_queue_frame(p, isoutbound, &answer, ast, 1);
  398. } else {
  399. ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
  400. }
  401. ao2_unlock(p);
  402. ao2_ref(p, -1);
  403. return res;
  404. }
  405. /*!
  406. * \internal
  407. * \note This function assumes that we're only called from the "outbound" local channel side
  408. *
  409. * \note it is assummed p is locked and reffed before entering this function
  410. */
  411. static void check_bridge(struct local_pvt *p)
  412. {
  413. struct ast_channel_monitor *tmp;
  414. struct ast_channel *chan = NULL;
  415. struct ast_channel *bridged_chan = NULL;
  416. /* Do a few conditional checks early on just to see if this optimization is possible */
  417. if (ast_test_flag(p, LOCAL_NO_OPTIMIZATION)) {
  418. return;
  419. }
  420. if (ast_test_flag(p, LOCAL_ALREADY_MASQED) || !p->chan || !p->owner) {
  421. return;
  422. }
  423. /* Safely get the channel bridged to p->chan */
  424. chan = ast_channel_ref(p->chan);
  425. ao2_unlock(p); /* don't call bridged channel with the pvt locked */
  426. bridged_chan = ast_bridged_channel(chan);
  427. ao2_lock(p);
  428. chan = ast_channel_unref(chan);
  429. /* since we had to unlock p to get the bridged chan, validate our
  430. * data once again and verify the bridged channel is what we expect
  431. * it to be in order to perform this optimization */
  432. if (ast_test_flag(p, LOCAL_ALREADY_MASQED) || !p->owner || !p->chan || (p->chan->_bridge != bridged_chan)) {
  433. return;
  434. }
  435. /* only do the masquerade if we are being called on the outbound channel,
  436. if it has been bridged to another channel and if there are no pending
  437. frames on the owner channel (because they would be transferred to the
  438. outbound channel during the masquerade)
  439. */
  440. if (p->chan->_bridge /* Not ast_bridged_channel! Only go one step! */ && AST_LIST_EMPTY(&p->owner->readq)) {
  441. /* Masquerade bridged channel into owner */
  442. /* Lock everything we need, one by one, and give up if
  443. we can't get everything. Remember, we'll get another
  444. chance in just a little bit */
  445. if (!ast_channel_trylock(p->chan->_bridge)) {
  446. if (!ast_check_hangup(p->chan->_bridge)) {
  447. if (!ast_channel_trylock(p->owner)) {
  448. if (!ast_check_hangup(p->owner)) {
  449. if (p->owner->monitor && !p->chan->_bridge->monitor) {
  450. /* If a local channel is being monitored, we don't want a masquerade
  451. * to cause the monitor to go away. Since the masquerade swaps the monitors,
  452. * pre-swapping the monitors before the masquerade will ensure that the monitor
  453. * ends up where it is expected.
  454. */
  455. tmp = p->owner->monitor;
  456. p->owner->monitor = p->chan->_bridge->monitor;
  457. p->chan->_bridge->monitor = tmp;
  458. }
  459. if (p->chan->audiohooks) {
  460. struct ast_audiohook_list *audiohooks_swapper;
  461. audiohooks_swapper = p->chan->audiohooks;
  462. p->chan->audiohooks = p->owner->audiohooks;
  463. p->owner->audiohooks = audiohooks_swapper;
  464. }
  465. /* If any Caller ID was set, preserve it after masquerade like above. We must check
  466. * to see if Caller ID was set because otherwise we'll mistakingly copy info not
  467. * set from the dialplan and will overwrite the real channel Caller ID. The reason
  468. * for this whole preswapping action is because the Caller ID is set on the channel
  469. * thread (which is the to be masqueraded away local channel) before both local
  470. * channels are optimized away.
  471. */
  472. if (p->owner->caller.id.name.valid || p->owner->caller.id.number.valid
  473. || p->owner->caller.id.subaddress.valid || p->owner->caller.ani.name.valid
  474. || p->owner->caller.ani.number.valid || p->owner->caller.ani.subaddress.valid) {
  475. struct ast_party_caller tmp;
  476. tmp = p->owner->caller;
  477. p->owner->caller = p->chan->_bridge->caller;
  478. p->chan->_bridge->caller = tmp;
  479. }
  480. if (p->owner->redirecting.from.name.valid || p->owner->redirecting.from.number.valid
  481. || p->owner->redirecting.from.subaddress.valid || p->owner->redirecting.to.name.valid
  482. || p->owner->redirecting.to.number.valid || p->owner->redirecting.to.subaddress.valid) {
  483. struct ast_party_redirecting tmp;
  484. tmp = p->owner->redirecting;
  485. p->owner->redirecting = p->chan->_bridge->redirecting;
  486. p->chan->_bridge->redirecting = tmp;
  487. }
  488. if (p->owner->dialed.number.str || p->owner->dialed.subaddress.valid) {
  489. struct ast_party_dialed tmp;
  490. tmp = p->owner->dialed;
  491. p->owner->dialed = p->chan->_bridge->dialed;
  492. p->chan->_bridge->dialed = tmp;
  493. }
  494. ast_app_group_update(p->chan, p->owner);
  495. ast_channel_masquerade(p->owner, p->chan->_bridge);
  496. ast_set_flag(p, LOCAL_ALREADY_MASQED);
  497. }
  498. ast_channel_unlock(p->owner);
  499. }
  500. }
  501. ast_channel_unlock(p->chan->_bridge);
  502. }
  503. }
  504. }
  505. static struct ast_frame *local_read(struct ast_channel *ast)
  506. {
  507. return &ast_null_frame;
  508. }
  509. static int local_write(struct ast_channel *ast, struct ast_frame *f)
  510. {
  511. struct local_pvt *p = ast->tech_pvt;
  512. int res = -1;
  513. int isoutbound;
  514. if (!p) {
  515. return -1;
  516. }
  517. /* Just queue for delivery to the other side */
  518. ao2_ref(p, 1); /* ref for local_queue_frame */
  519. ao2_lock(p);
  520. isoutbound = IS_OUTBOUND(ast, p);
  521. if (isoutbound && f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
  522. check_bridge(p);
  523. }
  524. if (!ast_test_flag(p, LOCAL_ALREADY_MASQED)) {
  525. res = local_queue_frame(p, isoutbound, f, ast, 1);
  526. } else {
  527. ast_debug(1, "Not posting to queue since already masked on '%s'\n", ast->name);
  528. res = 0;
  529. }
  530. ao2_unlock(p);
  531. ao2_ref(p, -1);
  532. return res;
  533. }
  534. static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  535. {
  536. struct local_pvt *p = newchan->tech_pvt;
  537. if (!p)
  538. return -1;
  539. ao2_lock(p);
  540. if ((p->owner != oldchan) && (p->chan != oldchan)) {
  541. ast_log(LOG_WARNING, "Old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
  542. ao2_unlock(p);
  543. return -1;
  544. }
  545. if (p->owner == oldchan)
  546. p->owner = newchan;
  547. else
  548. p->chan = newchan;
  549. /* Do not let a masquerade cause a Local channel to be bridged to itself! */
  550. if (!ast_check_hangup(newchan) && ((p->owner && p->owner->_bridge == p->chan) || (p->chan && p->chan->_bridge == p->owner))) {
  551. ast_log(LOG_WARNING, "You can not bridge a Local channel to itself!\n");
  552. ao2_unlock(p);
  553. ast_queue_hangup(newchan);
  554. return -1;
  555. }
  556. ao2_unlock(p);
  557. return 0;
  558. }
  559. static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
  560. {
  561. struct local_pvt *p = ast->tech_pvt;
  562. int res = 0;
  563. struct ast_frame f = { AST_FRAME_CONTROL, };
  564. int isoutbound;
  565. if (!p)
  566. return -1;
  567. ao2_ref(p, 1); /* ref for local_queue_frame */
  568. /* If this is an MOH hold or unhold, do it on the Local channel versus real channel */
  569. if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_HOLD) {
  570. ast_moh_start(ast, data, NULL);
  571. } else if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_UNHOLD) {
  572. ast_moh_stop(ast);
  573. } else if (condition == AST_CONTROL_CONNECTED_LINE || condition == AST_CONTROL_REDIRECTING) {
  574. struct ast_channel *this_channel;
  575. struct ast_channel *the_other_channel;
  576. /* A connected line update frame may only contain a partial amount of data, such
  577. * as just a source, or just a ton, and not the full amount of information. However,
  578. * the collected information is all stored in the outgoing channel's connectedline
  579. * structure, so when receiving a connected line update on an outgoing local channel,
  580. * we need to transmit the collected connected line information instead of whatever
  581. * happens to be in this control frame. The same applies for redirecting information, which
  582. * is why it is handled here as well.*/
  583. ao2_lock(p);
  584. isoutbound = IS_OUTBOUND(ast, p);
  585. if (isoutbound) {
  586. this_channel = p->chan;
  587. the_other_channel = p->owner;
  588. } else {
  589. this_channel = p->owner;
  590. the_other_channel = p->chan;
  591. }
  592. if (the_other_channel) {
  593. unsigned char frame_data[1024];
  594. if (condition == AST_CONTROL_CONNECTED_LINE) {
  595. if (isoutbound) {
  596. ast_connected_line_copy_to_caller(&the_other_channel->caller, &this_channel->connected);
  597. }
  598. f.datalen = ast_connected_line_build_data(frame_data, sizeof(frame_data), &this_channel->connected, NULL);
  599. } else {
  600. f.datalen = ast_redirecting_build_data(frame_data, sizeof(frame_data), &this_channel->redirecting, NULL);
  601. }
  602. f.subclass.integer = condition;
  603. f.data.ptr = frame_data;
  604. res = local_queue_frame(p, isoutbound, &f, ast, 1);
  605. }
  606. ao2_unlock(p);
  607. } else {
  608. /* Queue up a frame representing the indication as a control frame */
  609. ao2_lock(p);
  610. isoutbound = IS_OUTBOUND(ast, p);
  611. f.subclass.integer = condition;
  612. f.data.ptr = (void*)data;
  613. f.datalen = datalen;
  614. res = local_queue_frame(p, isoutbound, &f, ast, 1);
  615. ao2_unlock(p);
  616. }
  617. ao2_ref(p, -1);
  618. return res;
  619. }
  620. static int local_digit_begin(struct ast_channel *ast, char digit)
  621. {
  622. struct local_pvt *p = ast->tech_pvt;
  623. int res = -1;
  624. struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
  625. int isoutbound;
  626. if (!p)
  627. return -1;
  628. ao2_ref(p, 1); /* ref for local_queue_frame */
  629. ao2_lock(p);
  630. isoutbound = IS_OUTBOUND(ast, p);
  631. f.subclass.integer = digit;
  632. res = local_queue_frame(p, isoutbound, &f, ast, 0);
  633. ao2_unlock(p);
  634. ao2_ref(p, -1);
  635. return res;
  636. }
  637. static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  638. {
  639. struct local_pvt *p = ast->tech_pvt;
  640. int res = -1;
  641. struct ast_frame f = { AST_FRAME_DTMF_END, };
  642. int isoutbound;
  643. if (!p)
  644. return -1;
  645. ao2_ref(p, 1); /* ref for local_queue_frame */
  646. ao2_lock(p);
  647. isoutbound = IS_OUTBOUND(ast, p);
  648. f.subclass.integer = digit;
  649. f.len = duration;
  650. res = local_queue_frame(p, isoutbound, &f, ast, 0);
  651. ao2_unlock(p);
  652. ao2_ref(p, -1);
  653. return res;
  654. }
  655. static int local_sendtext(struct ast_channel *ast, const char *text)
  656. {
  657. struct local_pvt *p = ast->tech_pvt;
  658. int res = -1;
  659. struct ast_frame f = { AST_FRAME_TEXT, };
  660. int isoutbound;
  661. if (!p)
  662. return -1;
  663. ao2_lock(p);
  664. ao2_ref(p, 1); /* ref for local_queue_frame */
  665. isoutbound = IS_OUTBOUND(ast, p);
  666. f.data.ptr = (char *) text;
  667. f.datalen = strlen(text) + 1;
  668. res = local_queue_frame(p, isoutbound, &f, ast, 0);
  669. ao2_unlock(p);
  670. ao2_ref(p, -1);
  671. return res;
  672. }
  673. static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
  674. {
  675. struct local_pvt *p = ast->tech_pvt;
  676. int res = -1;
  677. struct ast_frame f = { AST_FRAME_HTML, };
  678. int isoutbound;
  679. if (!p)
  680. return -1;
  681. ao2_lock(p);
  682. ao2_ref(p, 1); /* ref for local_queue_frame */
  683. isoutbound = IS_OUTBOUND(ast, p);
  684. f.subclass.integer = subclass;
  685. f.data.ptr = (char *)data;
  686. f.datalen = datalen;
  687. res = local_queue_frame(p, isoutbound, &f, ast, 0);
  688. ao2_unlock(p);
  689. ao2_ref(p, -1);
  690. return res;
  691. }
  692. /*! \brief Initiate new call, part of PBX interface
  693. * dest is the dial string */
  694. static int local_call(struct ast_channel *ast, char *dest, int timeout)
  695. {
  696. struct local_pvt *p = ast->tech_pvt;
  697. int pvt_locked = 0;
  698. struct ast_channel *owner = NULL;
  699. struct ast_channel *chan = NULL;
  700. int res;
  701. struct ast_var_t *varptr = NULL, *new;
  702. size_t len, namelen;
  703. char *reduced_dest = ast_strdupa(dest);
  704. char *slash;
  705. const char *exten;
  706. const char *context;
  707. if (!p) {
  708. return -1;
  709. }
  710. /* since we are letting go of channel locks that were locked coming into
  711. * this function, then we need to give the tech pvt a ref */
  712. ao2_ref(p, 1);
  713. ast_channel_unlock(ast);
  714. awesome_locking(p, &chan, &owner);
  715. pvt_locked = 1;
  716. if (owner != ast) {
  717. res = -1;
  718. goto return_cleanup;
  719. }
  720. if (!owner || !chan) {
  721. res = -1;
  722. goto return_cleanup;
  723. }
  724. /*
  725. * Note that cid_num and cid_name aren't passed in the ast_channel_alloc
  726. * call, so it's done here instead.
  727. *
  728. * All these failure points just return -1. The individual strings will
  729. * be cleared when we destroy the channel.
  730. */
  731. ast_party_redirecting_copy(&chan->redirecting, &owner->redirecting);
  732. ast_party_dialed_copy(&chan->dialed, &owner->dialed);
  733. ast_connected_line_copy_to_caller(&chan->caller, &owner->connected);
  734. ast_connected_line_copy_from_caller(&chan->connected, &owner->caller);
  735. ast_string_field_set(chan, language, owner->language);
  736. ast_string_field_set(chan, accountcode, owner->accountcode);
  737. ast_string_field_set(chan, musicclass, owner->musicclass);
  738. ast_cdr_update(chan);
  739. ast_channel_cc_params_init(chan, ast_channel_get_cc_config_params(owner));
  740. /* Make sure we inherit the ANSWERED_ELSEWHERE flag if it's set on the queue/dial call request in the dialplan */
  741. if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
  742. ast_set_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE);
  743. }
  744. /* copy the channel variables from the incoming channel to the outgoing channel */
  745. /* Note that due to certain assumptions, they MUST be in the same order */
  746. AST_LIST_TRAVERSE(&owner->varshead, varptr, entries) {
  747. namelen = strlen(varptr->name);
  748. len = sizeof(struct ast_var_t) + namelen + strlen(varptr->value) + 2;
  749. if ((new = ast_calloc(1, len))) {
  750. memcpy(new, varptr, len);
  751. new->value = &(new->name[0]) + namelen + 1;
  752. AST_LIST_INSERT_TAIL(&chan->varshead, new, entries);
  753. }
  754. }
  755. ast_channel_datastore_inherit(owner, chan);
  756. /* If the local channel has /n or /b on the end of it,
  757. * we need to lop that off for our argument to setting
  758. * up the CC_INTERFACES variable
  759. */
  760. if ((slash = strrchr(reduced_dest, '/'))) {
  761. *slash = '\0';
  762. }
  763. ast_set_cc_interfaces_chanvar(chan, reduced_dest);
  764. exten = ast_strdupa(chan->exten);
  765. context = ast_strdupa(chan->context);
  766. ao2_unlock(p);
  767. pvt_locked = 0;
  768. ast_channel_unlock(chan);
  769. if (!ast_exists_extension(chan, context, exten, 1,
  770. S_COR(owner->caller.id.number.valid, owner->caller.id.number.str, NULL))) {
  771. ast_log(LOG_NOTICE, "No such extension/context %s@%s while calling Local channel\n", exten, context);
  772. res = -1;
  773. chan = ast_channel_unref(chan); /* we already unlocked it, so clear it hear so the cleanup label won't touch it. */
  774. goto return_cleanup;
  775. }
  776. /* Start switch on sub channel */
  777. if (!(res = ast_pbx_start(chan))) {
  778. ao2_lock(p);
  779. ast_set_flag(p, LOCAL_LAUNCHED_PBX);
  780. ao2_unlock(p);
  781. }
  782. chan = ast_channel_unref(chan); /* chan is already unlocked, clear it here so the cleanup lable won't touch it. */
  783. return_cleanup:
  784. if (p) {
  785. if (pvt_locked) {
  786. ao2_unlock(p);
  787. }
  788. ao2_ref(p, -1);
  789. }
  790. if (chan) {
  791. ast_channel_unlock(chan);
  792. chan = ast_channel_unref(chan);
  793. }
  794. /* owner is supposed to be == to ast, if it
  795. * is, don't unlock it because ast must exit locked */
  796. if (owner) {
  797. if (owner != ast) {
  798. ast_channel_unlock(owner);
  799. ast_channel_lock(ast);
  800. }
  801. owner = ast_channel_unref(owner);
  802. } else {
  803. /* we have to exit with ast locked */
  804. ast_channel_lock(ast);
  805. }
  806. return res;
  807. }
  808. /*! \brief Hangup a call through the local proxy channel */
  809. static int local_hangup(struct ast_channel *ast)
  810. {
  811. struct local_pvt *p = ast->tech_pvt;
  812. int isoutbound;
  813. int hangup_chan = 0;
  814. int res = 0;
  815. struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_HANGUP }, .data.uint32 = ast->hangupcause };
  816. struct ast_channel *owner = NULL;
  817. struct ast_channel *chan = NULL;
  818. if (!p) {
  819. return -1;
  820. }
  821. /* give the pvt a ref since we are unlocking the channel. */
  822. ao2_ref(p, 1);
  823. /* the pvt isn't going anywhere, we gave it a ref */
  824. ast_channel_unlock(ast);
  825. /* lock everything */
  826. awesome_locking(p, &chan, &owner);
  827. if (ast != chan && ast != owner) {
  828. res = -1;
  829. goto local_hangup_cleanup;
  830. }
  831. isoutbound = IS_OUTBOUND(ast, p); /* just comparing pointer of ast */
  832. if (p->chan && ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
  833. ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
  834. ast_debug(2, "This local call has the ANSWERED_ELSEWHERE flag set.\n");
  835. }
  836. if (isoutbound) {
  837. const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
  838. if ((status) && (p->owner)) {
  839. p->owner->hangupcause = p->chan->hangupcause;
  840. pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
  841. }
  842. ast_clear_flag(p, LOCAL_LAUNCHED_PBX);
  843. ast_module_user_remove(p->u_chan);
  844. p->chan = NULL;
  845. } else {
  846. ast_module_user_remove(p->u_owner);
  847. if (p->chan) {
  848. ast_queue_hangup(p->chan);
  849. }
  850. p->owner = NULL;
  851. }
  852. ast->tech_pvt = NULL; /* this is one of our locked channels, doesn't matter which */
  853. if (!p->owner && !p->chan) {
  854. ao2_unlock(p);
  855. /* Remove from list */
  856. ao2_unlink(locals, p);
  857. ao2_ref(p, -1);
  858. p = NULL;
  859. res = 0;
  860. goto local_hangup_cleanup;
  861. }
  862. if (p->chan && !ast_test_flag(p, LOCAL_LAUNCHED_PBX)) {
  863. /* Need to actually hangup since there is no PBX */
  864. hangup_chan = 1;
  865. } else {
  866. local_queue_frame(p, isoutbound, &f, NULL, 0);
  867. }
  868. local_hangup_cleanup:
  869. if (p) {
  870. ao2_unlock(p);
  871. ao2_ref(p, -1);
  872. }
  873. if (chan) {
  874. ast_channel_unlock(chan);
  875. if (hangup_chan) {
  876. ast_hangup(chan);
  877. }
  878. chan = ast_channel_unref(chan);
  879. }
  880. if (owner) {
  881. ast_channel_unlock(owner);
  882. owner = ast_channel_unref(owner);
  883. }
  884. /* leave with the same stupid channel locked that came in */
  885. ast_channel_lock(ast);
  886. return res;
  887. }
  888. /*! \brief Create a call structure */
  889. static struct local_pvt *local_alloc(const char *data, format_t format)
  890. {
  891. struct local_pvt *tmp = NULL;
  892. char *c = NULL, *opts = NULL;
  893. if (!(tmp = ao2_alloc(sizeof(*tmp), NULL))) {
  894. return NULL;
  895. }
  896. /* Initialize private structure information */
  897. ast_copy_string(tmp->exten, data, sizeof(tmp->exten));
  898. memcpy(&tmp->jb_conf, &g_jb_conf, sizeof(tmp->jb_conf));
  899. /* Look for options */
  900. if ((opts = strchr(tmp->exten, '/'))) {
  901. *opts++ = '\0';
  902. if (strchr(opts, 'n'))
  903. ast_set_flag(tmp, LOCAL_NO_OPTIMIZATION);
  904. if (strchr(opts, 'j')) {
  905. if (ast_test_flag(tmp, LOCAL_NO_OPTIMIZATION))
  906. ast_set_flag(&tmp->jb_conf, AST_JB_ENABLED);
  907. else {
  908. ast_log(LOG_ERROR, "You must use the 'n' option for chan_local "
  909. "to use the 'j' option to enable the jitterbuffer\n");
  910. }
  911. }
  912. if (strchr(opts, 'b')) {
  913. ast_set_flag(tmp, LOCAL_BRIDGE);
  914. }
  915. if (strchr(opts, 'm')) {
  916. ast_set_flag(tmp, LOCAL_MOH_PASSTHRU);
  917. }
  918. }
  919. /* Look for a context */
  920. if ((c = strchr(tmp->exten, '@')))
  921. *c++ = '\0';
  922. ast_copy_string(tmp->context, c ? c : "default", sizeof(tmp->context));
  923. tmp->reqformat = format;
  924. #if 0
  925. /* We can't do this check here, because we don't know the CallerID yet, and
  926. * the CallerID could potentially affect what step is actually taken (or
  927. * even if that step exists). */
  928. if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
  929. ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
  930. tmp = local_pvt_destroy(tmp);
  931. } else {
  932. #endif
  933. /* Add to list */
  934. ao2_link(locals, tmp);
  935. #if 0
  936. }
  937. #endif
  938. return tmp; /* this is returned with a ref */
  939. }
  940. /*! \brief Start new local channel */
  941. static struct ast_channel *local_new(struct local_pvt *p, int state, const char *linkedid)
  942. {
  943. struct ast_channel *tmp = NULL, *tmp2 = NULL;
  944. int randnum = ast_random() & 0xffff, fmt = 0;
  945. const char *t;
  946. int ama;
  947. /* Allocate two new Asterisk channels */
  948. /* safe accountcode */
  949. if (p->owner && p->owner->accountcode)
  950. t = p->owner->accountcode;
  951. else
  952. t = "";
  953. if (p->owner)
  954. ama = p->owner->amaflags;
  955. else
  956. ama = 0;
  957. if (!(tmp = ast_channel_alloc(1, state, 0, 0, t, p->exten, p->context, linkedid, ama, "Local/%s@%s-%04x;1", p->exten, p->context, randnum))
  958. || !(tmp2 = ast_channel_alloc(1, AST_STATE_RING, 0, 0, t, p->exten, p->context, linkedid, ama, "Local/%s@%s-%04x;2", p->exten, p->context, randnum))) {
  959. if (tmp) {
  960. tmp = ast_channel_release(tmp);
  961. }
  962. ast_log(LOG_WARNING, "Unable to allocate channel structure(s)\n");
  963. return NULL;
  964. }
  965. tmp2->tech = tmp->tech = &local_tech;
  966. tmp->nativeformats = p->reqformat;
  967. tmp2->nativeformats = p->reqformat;
  968. /* Determine our read/write format and set it on each channel */
  969. fmt = ast_best_codec(p->reqformat);
  970. tmp->writeformat = fmt;
  971. tmp2->writeformat = fmt;
  972. tmp->rawwriteformat = fmt;
  973. tmp2->rawwriteformat = fmt;
  974. tmp->readformat = fmt;
  975. tmp2->readformat = fmt;
  976. tmp->rawreadformat = fmt;
  977. tmp2->rawreadformat = fmt;
  978. tmp->tech_pvt = p;
  979. tmp2->tech_pvt = p;
  980. p->owner = tmp;
  981. p->chan = tmp2;
  982. p->u_owner = ast_module_user_add(p->owner);
  983. p->u_chan = ast_module_user_add(p->chan);
  984. ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
  985. ast_copy_string(tmp2->context, p->context, sizeof(tmp2->context));
  986. ast_copy_string(tmp2->exten, p->exten, sizeof(tmp->exten));
  987. tmp->priority = 1;
  988. tmp2->priority = 1;
  989. ast_jb_configure(tmp, &p->jb_conf);
  990. return tmp;
  991. }
  992. /*! \brief Part of PBX interface */
  993. static struct ast_channel *local_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
  994. {
  995. struct local_pvt *p = NULL;
  996. struct ast_channel *chan = NULL;
  997. /* Allocate a new private structure and then Asterisk channel */
  998. if ((p = local_alloc(data, format))) {
  999. if (!(chan = local_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL))) {
  1000. ao2_unlink(locals, p);
  1001. }
  1002. if (chan && ast_channel_cc_params_init(chan, requestor ? ast_channel_get_cc_config_params((struct ast_channel *)requestor) : NULL)) {
  1003. chan = ast_channel_release(chan);
  1004. ao2_unlink(locals, p);
  1005. }
  1006. ao2_ref(p, -1); /* kill the ref from the alloc */
  1007. }
  1008. return chan;
  1009. }
  1010. /*! \brief CLI command "local show channels" */
  1011. static char *locals_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1012. {
  1013. struct local_pvt *p = NULL;
  1014. struct ao2_iterator it;
  1015. switch (cmd) {
  1016. case CLI_INIT:
  1017. e->command = "local show channels";
  1018. e->usage =
  1019. "Usage: local show channels\n"
  1020. " Provides summary information on active local proxy channels.\n";
  1021. return NULL;
  1022. case CLI_GENERATE:
  1023. return NULL;
  1024. }
  1025. if (a->argc != 3)
  1026. return CLI_SHOWUSAGE;
  1027. if (ao2_container_count(locals) == 0) {
  1028. ast_cli(a->fd, "No local channels in use\n");
  1029. return RESULT_SUCCESS;
  1030. }
  1031. it = ao2_iterator_init(locals, 0);
  1032. while ((p = ao2_iterator_next(&it))) {
  1033. ao2_lock(p);
  1034. ast_cli(a->fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
  1035. ao2_unlock(p);
  1036. ao2_ref(p, -1);
  1037. }
  1038. ao2_iterator_destroy(&it);
  1039. return CLI_SUCCESS;
  1040. }
  1041. static struct ast_cli_entry cli_local[] = {
  1042. AST_CLI_DEFINE(locals_show, "List status of local channels"),
  1043. };
  1044. static int manager_optimize_away(struct mansession *s, const struct message *m)
  1045. {
  1046. const char *channel;
  1047. struct local_pvt *p, *tmp = NULL;
  1048. struct ast_channel *c;
  1049. int found = 0;
  1050. struct ao2_iterator it;
  1051. channel = astman_get_header(m, "Channel");
  1052. if (ast_strlen_zero(channel)) {
  1053. astman_send_error(s, m, "'Channel' not specified.");
  1054. return 0;
  1055. }
  1056. c = ast_channel_get_by_name(channel);
  1057. if (!c) {
  1058. astman_send_error(s, m, "Channel does not exist.");
  1059. return 0;
  1060. }
  1061. p = c->tech_pvt;
  1062. ast_channel_unref(c);
  1063. c = NULL;
  1064. it = ao2_iterator_init(locals, 0);
  1065. while ((tmp = ao2_iterator_next(&it))) {
  1066. if (tmp == p) {
  1067. ao2_lock(tmp);
  1068. found = 1;
  1069. ast_clear_flag(tmp, LOCAL_NO_OPTIMIZATION);
  1070. ao2_unlock(tmp);
  1071. ao2_ref(tmp, -1);
  1072. break;
  1073. }
  1074. ao2_ref(tmp, -1);
  1075. }
  1076. ao2_iterator_destroy(&it);
  1077. if (found) {
  1078. astman_send_ack(s, m, "Queued channel to be optimized away");
  1079. } else {
  1080. astman_send_error(s, m, "Unable to find channel");
  1081. }
  1082. return 0;
  1083. }
  1084. static int locals_cmp_cb(void *obj, void *arg, int flags)
  1085. {
  1086. return (obj == arg) ? CMP_MATCH : 0;
  1087. }
  1088. /*! \brief Load module into PBX, register channel */
  1089. static int load_module(void)
  1090. {
  1091. if (!(locals = ao2_container_alloc(BUCKET_SIZE, NULL, locals_cmp_cb))) {
  1092. return AST_MODULE_LOAD_FAILURE;
  1093. }
  1094. /* Make sure we can register our channel type */
  1095. if (ast_channel_register(&local_tech)) {
  1096. ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
  1097. ao2_ref(locals, -1);
  1098. return AST_MODULE_LOAD_FAILURE;
  1099. }
  1100. ast_cli_register_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
  1101. ast_manager_register_xml("LocalOptimizeAway", EVENT_FLAG_SYSTEM|EVENT_FLAG_CALL, manager_optimize_away);
  1102. return AST_MODULE_LOAD_SUCCESS;
  1103. }
  1104. /*! \brief Unload the local proxy channel from Asterisk */
  1105. static int unload_module(void)
  1106. {
  1107. struct local_pvt *p = NULL;
  1108. struct ao2_iterator it;
  1109. /* First, take us out of the channel loop */
  1110. ast_cli_unregister_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
  1111. ast_manager_unregister("LocalOptimizeAway");
  1112. ast_channel_unregister(&local_tech);
  1113. it = ao2_iterator_init(locals, 0);
  1114. while ((p = ao2_iterator_next(&it))) {
  1115. if (p->owner) {
  1116. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  1117. }
  1118. ao2_ref(p, -1);
  1119. }
  1120. ao2_iterator_destroy(&it);
  1121. ao2_ref(locals, -1);
  1122. return 0;
  1123. }
  1124. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Local Proxy Channel (Note: used internally by other modules)",
  1125. .load = load_module,
  1126. .unload = unload_module,
  1127. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  1128. );