chan_local.c 15 KB

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