chan_local.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Local Proxy Channel
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <pthread.h>
  15. #include <string.h>
  16. #include <asterisk/lock.h>
  17. #include <asterisk/channel.h>
  18. #include <asterisk/channel_pvt.h>
  19. #include <asterisk/config.h>
  20. #include <asterisk/logger.h>
  21. #include <asterisk/module.h>
  22. #include <asterisk/pbx.h>
  23. #include <asterisk/options.h>
  24. #include <asterisk/lock.h>
  25. #include <asterisk/sched.h>
  26. #include <asterisk/io.h>
  27. #include <asterisk/rtp.h>
  28. #include <asterisk/acl.h>
  29. #include <asterisk/callerid.h>
  30. #include <asterisk/file.h>
  31. #include <asterisk/cli.h>
  32. #include <asterisk/app.h>
  33. #include <asterisk/musiconhold.h>
  34. #include <asterisk/manager.h>
  35. #include <sys/socket.h>
  36. #include <errno.h>
  37. #include <unistd.h>
  38. #include <stdlib.h>
  39. #include <fcntl.h>
  40. #include <netdb.h>
  41. #include <arpa/inet.h>
  42. #include <sys/signal.h>
  43. static char *desc = "Local Proxy Channel";
  44. static char *type = "Local";
  45. static char *tdesc = "Local Proxy Channel Driver";
  46. static int capability = -1;
  47. static int usecnt =0;
  48. static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
  49. #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
  50. /* Protect the interface list (of sip_pvt's) */
  51. static ast_mutex_t locallock = AST_MUTEX_INITIALIZER;
  52. static struct local_pvt {
  53. ast_mutex_t lock; /* Channel private lock */
  54. char context[AST_MAX_EXTENSION]; /* Context to call */
  55. char exten[AST_MAX_EXTENSION]; /* Extension to call */
  56. int reqformat; /* Requested format */
  57. int glaredetect; /* Detect glare on hangup */
  58. int cancelqueue; /* Cancel queue */
  59. int alreadymasqed; /* Already masqueraded */
  60. int launchedpbx; /* Did we launch the PBX */
  61. int nooptimization;
  62. struct ast_channel *owner; /* Master Channel */
  63. struct ast_channel *chan; /* Outbound channel */
  64. struct local_pvt *next; /* Next entity */
  65. } *locals = NULL;
  66. static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, struct ast_channel *us)
  67. {
  68. struct ast_channel *other;
  69. retrylock:
  70. /* Recalculate outbound channel */
  71. if (isoutbound) {
  72. other = p->owner;
  73. } else {
  74. other = p->chan;
  75. }
  76. /* Set glare detection */
  77. p->glaredetect = 1;
  78. if (p->cancelqueue) {
  79. /* We had a glare on the hangup. Forget all this business,
  80. return and destroy p. */
  81. ast_mutex_unlock(&p->lock);
  82. free(p);
  83. return -1;
  84. }
  85. if (!other) {
  86. p->glaredetect = 0;
  87. return 0;
  88. }
  89. if (ast_mutex_trylock(&other->lock)) {
  90. /* Failed to lock. Release main lock and try again */
  91. ast_mutex_unlock(&p->lock);
  92. if (us) {
  93. if (ast_mutex_unlock(&us->lock)) {
  94. ast_log(LOG_WARNING, "%s wasn't locked while sending %d/%d\n",
  95. us->name, f->frametype, f->subclass);
  96. us = NULL;
  97. }
  98. }
  99. /* Wait just a bit */
  100. usleep(1);
  101. /* Only we can destroy ourselves, so we can't disappear here */
  102. if (us)
  103. ast_mutex_lock(&us->lock);
  104. ast_mutex_lock(&p->lock);
  105. goto retrylock;
  106. }
  107. ast_queue_frame(other, f, 0);
  108. ast_mutex_unlock(&other->lock);
  109. p->glaredetect = 0;
  110. return 0;
  111. }
  112. static int local_answer(struct ast_channel *ast)
  113. {
  114. struct local_pvt *p = ast->pvt->pvt;
  115. int isoutbound = IS_OUTBOUND(ast, p);
  116. int res = -1;
  117. ast_mutex_lock(&p->lock);
  118. if (isoutbound) {
  119. /* Pass along answer since somebody answered us */
  120. struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
  121. res = local_queue_frame(p, isoutbound, &answer, ast);
  122. } else
  123. ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
  124. ast_mutex_unlock(&p->lock);
  125. return res;
  126. }
  127. static void check_bridge(struct local_pvt *p, int isoutbound)
  128. {
  129. if (p->alreadymasqed || p->nooptimization)
  130. return;
  131. if (isoutbound && p->chan && p->chan->bridge && p->owner) {
  132. /* Masquerade bridged channel into owner */
  133. /* Lock everything we need, one by one, and give up if
  134. we can't get everything. Remember, we'll get another
  135. chance in just a little bit */
  136. if (!ast_mutex_trylock(&p->chan->bridge->lock)) {
  137. if (!ast_mutex_trylock(&p->owner->lock)) {
  138. ast_channel_masquerade(p->owner, p->chan->bridge);
  139. p->alreadymasqed = 1;
  140. ast_mutex_unlock(&p->owner->lock);
  141. }
  142. ast_mutex_unlock(&p->chan->bridge->lock);
  143. }
  144. } else if (!isoutbound && p->owner && p->owner->bridge && p->chan) {
  145. /* Masquerade bridged channel into chan */
  146. if (!ast_mutex_trylock(&p->owner->bridge->lock)) {
  147. if (!ast_mutex_trylock(&p->chan->lock)) {
  148. ast_channel_masquerade(p->chan, p->owner->bridge);
  149. p->alreadymasqed = 1;
  150. ast_mutex_unlock(&p->chan->lock);
  151. }
  152. ast_mutex_unlock(&p->owner->bridge->lock);
  153. }
  154. }
  155. }
  156. static struct ast_frame *local_read(struct ast_channel *ast)
  157. {
  158. static struct ast_frame null = { AST_FRAME_NULL, };
  159. return &null;
  160. }
  161. static int local_write(struct ast_channel *ast, struct ast_frame *f)
  162. {
  163. struct local_pvt *p = ast->pvt->pvt;
  164. int res = -1;
  165. int isoutbound = IS_OUTBOUND(ast, p);
  166. /* Just queue for delivery to the other side */
  167. ast_mutex_lock(&p->lock);
  168. res = local_queue_frame(p, isoutbound, f, ast);
  169. check_bridge(p, isoutbound);
  170. ast_mutex_unlock(&p->lock);
  171. return res;
  172. }
  173. static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  174. {
  175. struct local_pvt *p = newchan->pvt->pvt;
  176. ast_mutex_lock(&p->lock);
  177. if ((p->owner != oldchan) && (p->chan != oldchan)) {
  178. ast_log(LOG_WARNING, "old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
  179. ast_mutex_unlock(&p->lock);
  180. return -1;
  181. }
  182. if (p->owner == oldchan)
  183. p->owner = newchan;
  184. else
  185. p->chan = newchan;
  186. ast_mutex_unlock(&p->lock);
  187. return 0;
  188. }
  189. static int local_indicate(struct ast_channel *ast, int condition)
  190. {
  191. struct local_pvt *p = ast->pvt->pvt;
  192. int res = -1;
  193. struct ast_frame f = { AST_FRAME_CONTROL, };
  194. int isoutbound = IS_OUTBOUND(ast, p);
  195. /* Queue up a frame representing the indication as a control frame */
  196. ast_mutex_lock(&p->lock);
  197. f.subclass = condition;
  198. res = local_queue_frame(p, isoutbound, &f, ast);
  199. ast_mutex_unlock(&p->lock);
  200. return res;
  201. }
  202. static int local_digit(struct ast_channel *ast, char digit)
  203. {
  204. struct local_pvt *p = ast->pvt->pvt;
  205. int res = -1;
  206. struct ast_frame f = { AST_FRAME_DTMF, };
  207. int isoutbound = IS_OUTBOUND(ast, p);
  208. ast_mutex_lock(&p->lock);
  209. f.subclass = digit;
  210. res = local_queue_frame(p, isoutbound, &f, ast);
  211. ast_mutex_unlock(&p->lock);
  212. return res;
  213. }
  214. static int local_call(struct ast_channel *ast, char *dest, int timeout)
  215. {
  216. struct local_pvt *p = ast->pvt->pvt;
  217. int res;
  218. ast_mutex_lock(&p->lock);
  219. if (p->owner->callerid)
  220. p->chan->callerid = strdup(p->owner->callerid);
  221. else
  222. p->chan->callerid = NULL;
  223. if (p->owner->ani)
  224. p->chan->ani = strdup(p->owner->ani);
  225. else
  226. p->chan->ani = NULL;
  227. p->launchedpbx = 1;
  228. /* Start switch on sub channel */
  229. res = ast_pbx_start(p->chan);
  230. ast_mutex_unlock(&p->lock);
  231. return res;
  232. }
  233. /*
  234. static void local_destroy(struct local_pvt *p)
  235. {
  236. struct local_pvt *cur, *prev = NULL;
  237. ast_mutex_lock(&locallock);
  238. cur = locals;
  239. while(cur) {
  240. if (cur == p) {
  241. if (prev)
  242. prev->next = cur->next;
  243. else
  244. locals = cur->next;
  245. free(cur);
  246. break;
  247. }
  248. prev = cur;
  249. cur = cur->next;
  250. }
  251. ast_mutex_unlock(&locallock);
  252. if (!cur)
  253. ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
  254. }
  255. */
  256. static int local_hangup(struct ast_channel *ast)
  257. {
  258. struct local_pvt *p = ast->pvt->pvt;
  259. int isoutbound = IS_OUTBOUND(ast, p);
  260. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
  261. struct local_pvt *cur, *prev=NULL;
  262. struct ast_channel *ochan = NULL;
  263. int glaredetect;
  264. ast_mutex_lock(&p->lock);
  265. if (isoutbound) {
  266. p->chan = NULL;
  267. p->launchedpbx = 0;
  268. } else
  269. p->owner = NULL;
  270. ast->pvt->pvt = NULL;
  271. if (!p->owner && !p->chan) {
  272. /* Okay, done with the private part now, too. */
  273. glaredetect = p->glaredetect;
  274. /* If we have a queue holding, don't actually destroy p yet, but
  275. let local_queue do it. */
  276. if (p->glaredetect)
  277. p->cancelqueue = 1;
  278. ast_mutex_unlock(&p->lock);
  279. /* Remove from list */
  280. ast_mutex_lock(&locallock);
  281. cur = locals;
  282. while(cur) {
  283. if (cur == p) {
  284. if (prev)
  285. prev->next = cur->next;
  286. else
  287. locals = cur->next;
  288. break;
  289. }
  290. prev = cur;
  291. cur = cur->next;
  292. }
  293. ast_mutex_unlock(&locallock);
  294. /* And destroy */
  295. if (!glaredetect)
  296. free(p);
  297. return 0;
  298. }
  299. if (p->chan && !p->launchedpbx)
  300. /* Need to actually hangup since there is no PBX */
  301. ochan = p->chan;
  302. else
  303. local_queue_frame(p, isoutbound, &f, NULL);
  304. ast_mutex_unlock(&p->lock);
  305. if (ochan)
  306. ast_hangup(ochan);
  307. return 0;
  308. }
  309. static struct local_pvt *local_alloc(char *data, int format)
  310. {
  311. struct local_pvt *tmp;
  312. char *c;
  313. char *opts;
  314. tmp = malloc(sizeof(struct local_pvt));
  315. if (tmp) {
  316. memset(tmp, 0, sizeof(struct local_pvt));
  317. ast_mutex_init(&tmp->lock);
  318. strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
  319. opts = strchr(tmp->exten, '/');
  320. if (opts) {
  321. *opts='\0';
  322. opts++;
  323. if (strchr(opts, 'n'))
  324. tmp->nooptimization = 1;
  325. }
  326. c = strchr(tmp->exten, '@');
  327. if (c) {
  328. *c = '\0';
  329. c++;
  330. strncpy(tmp->context, c, sizeof(tmp->context) - 1);
  331. } else
  332. strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
  333. tmp->reqformat = format;
  334. if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
  335. ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->context, tmp->exten);
  336. free(tmp);
  337. tmp = NULL;
  338. } else {
  339. /* Add to list */
  340. ast_mutex_lock(&locallock);
  341. tmp->next = locals;
  342. locals = tmp;
  343. ast_mutex_unlock(&locallock);
  344. }
  345. }
  346. return tmp;
  347. }
  348. static struct ast_channel *local_new(struct local_pvt *p, int state)
  349. {
  350. struct ast_channel *tmp, *tmp2;
  351. int randnum = rand() & 0xffff;
  352. tmp = ast_channel_alloc(1);
  353. tmp2 = ast_channel_alloc(1);
  354. if (!tmp || !tmp2) {
  355. if (tmp)
  356. ast_channel_free(tmp);
  357. if (tmp2)
  358. ast_channel_free(tmp2);
  359. tmp = NULL;
  360. }
  361. if (tmp) {
  362. tmp->nativeformats = p->reqformat;
  363. tmp2->nativeformats = p->reqformat;
  364. snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-%04x,1", p->exten, p->context, randnum);
  365. snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-%04x,2", p->exten, p->context, randnum);
  366. tmp->type = type;
  367. tmp2->type = type;
  368. ast_setstate(tmp, state);
  369. ast_setstate(tmp2, AST_STATE_RING);
  370. tmp->writeformat = p->reqformat;;
  371. tmp2->writeformat = p->reqformat;
  372. tmp->pvt->rawwriteformat = p->reqformat;
  373. tmp2->pvt->rawwriteformat = p->reqformat;
  374. tmp->readformat = p->reqformat;
  375. tmp2->readformat = p->reqformat;
  376. tmp->pvt->rawreadformat = p->reqformat;
  377. tmp2->pvt->rawreadformat = p->reqformat;
  378. tmp->pvt->pvt = p;
  379. tmp2->pvt->pvt = p;
  380. tmp->pvt->send_digit = local_digit;
  381. tmp2->pvt->send_digit = local_digit;
  382. tmp->pvt->call = local_call;
  383. tmp2->pvt->call = local_call;
  384. tmp->pvt->hangup = local_hangup;
  385. tmp2->pvt->hangup = local_hangup;
  386. tmp->pvt->answer = local_answer;
  387. tmp2->pvt->answer = local_answer;
  388. tmp->pvt->read = local_read;
  389. tmp2->pvt->read = local_read;
  390. tmp->pvt->write = local_write;
  391. tmp2->pvt->write = local_write;
  392. tmp->pvt->exception = local_read;
  393. tmp2->pvt->exception = local_read;
  394. tmp->pvt->indicate = local_indicate;
  395. tmp2->pvt->indicate = local_indicate;
  396. tmp->pvt->fixup = local_fixup;
  397. tmp2->pvt->fixup = local_fixup;
  398. p->owner = tmp;
  399. p->chan = tmp2;
  400. ast_mutex_lock(&usecnt_lock);
  401. usecnt++;
  402. ast_mutex_unlock(&usecnt_lock);
  403. ast_update_use_count();
  404. strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
  405. strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
  406. strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
  407. tmp->priority = 1;
  408. tmp2->priority = 1;
  409. } else
  410. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  411. return tmp;
  412. }
  413. static struct ast_channel *local_request(char *type, int format, void *data)
  414. {
  415. struct local_pvt *p;
  416. struct ast_channel *chan = NULL;
  417. p = local_alloc(data, format);
  418. if (p)
  419. chan = local_new(p, AST_STATE_DOWN);
  420. return chan;
  421. }
  422. static int locals_show(int fd, int argc, char **argv)
  423. {
  424. struct local_pvt *p;
  425. if (argc != 2)
  426. return RESULT_SHOWUSAGE;
  427. ast_mutex_lock(&locallock);
  428. p = locals;
  429. while(p) {
  430. ast_mutex_lock(&p->lock);
  431. ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
  432. ast_mutex_unlock(&p->lock);
  433. p = p->next;
  434. }
  435. if (!locals)
  436. ast_cli(fd, "No local channels in use\n");
  437. ast_mutex_unlock(&locallock);
  438. return RESULT_SUCCESS;
  439. }
  440. static char show_locals_usage[] =
  441. "Usage: show locals\n"
  442. " Provides summary information on locals.\n";
  443. static struct ast_cli_entry cli_show_locals = {
  444. { "show", "locals", NULL }, locals_show,
  445. "Show status of local channels", show_locals_usage, NULL };
  446. int load_module()
  447. {
  448. /* Make sure we can register our sip channel type */
  449. if (ast_channel_register(type, tdesc, capability, local_request)) {
  450. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  451. return -1;
  452. }
  453. ast_cli_register(&cli_show_locals);
  454. return 0;
  455. }
  456. int reload()
  457. {
  458. return 0;
  459. }
  460. int unload_module()
  461. {
  462. struct local_pvt *p;
  463. /* First, take us out of the channel loop */
  464. ast_cli_unregister(&cli_show_locals);
  465. ast_channel_unregister(type);
  466. if (!ast_mutex_lock(&locallock)) {
  467. /* Hangup all interfaces if they have an owner */
  468. p = locals;
  469. while(p) {
  470. if (p->owner)
  471. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  472. p = p->next;
  473. }
  474. locals = NULL;
  475. ast_mutex_unlock(&locallock);
  476. } else {
  477. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  478. return -1;
  479. }
  480. return 0;
  481. }
  482. int usecount()
  483. {
  484. int res;
  485. ast_mutex_lock(&usecnt_lock);
  486. res = usecnt;
  487. ast_mutex_unlock(&usecnt_lock);
  488. return res;
  489. }
  490. char *key()
  491. {
  492. return ASTERISK_GPL_KEY;
  493. }
  494. char *description()
  495. {
  496. return desc;
  497. }