chan_local.c 40 KB

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