2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
16 #include <asterisk/lock.h>
17 #include <asterisk/channel.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>
40 #include <arpa/inet.h>
41 #include <sys/signal.h>
43 static const char desc[] = "Local Proxy Channel";
44 static const char type[] = "Local";
45 static const char tdesc[] = "Local Proxy Channel Driver";
48 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
50 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
52 /* Protect the interface list (of sip_pvt's) */
53 AST_MUTEX_DEFINE_STATIC(locallock);
55 static struct ast_channel *local_request(const char *type, int format, void *data, int *cause);
56 static int local_digit(struct ast_channel *ast, char digit);
57 static int local_call(struct ast_channel *ast, char *dest, int timeout);
58 static int local_hangup(struct ast_channel *ast);
59 static int local_answer(struct ast_channel *ast);
60 static struct ast_frame *local_read(struct ast_channel *ast);
61 static int local_write(struct ast_channel *ast, struct ast_frame *f);
62 static int local_indicate(struct ast_channel *ast, int condition);
63 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
64 static int local_sendhtml(struct ast_channel *ast, int subclass, char *data, int datalen);
66 static const struct ast_channel_tech local_tech = {
70 .requester = local_request,
71 .send_digit = local_digit,
73 .hangup = local_hangup,
74 .answer = local_answer,
77 .exception = local_read,
78 .indicate = local_indicate,
80 .send_html = local_sendhtml,
83 static struct local_pvt {
84 ast_mutex_t lock; /* Channel private lock */
85 char context[AST_MAX_EXTENSION]; /* Context to call */
86 char exten[AST_MAX_EXTENSION]; /* Extension to call */
87 int reqformat; /* Requested format */
88 int glaredetect; /* Detect glare on hangup */
89 int cancelqueue; /* Cancel queue */
90 int alreadymasqed; /* Already masqueraded */
91 int launchedpbx; /* Did we launch the PBX */
93 struct ast_channel *owner; /* Master Channel */
94 struct ast_channel *chan; /* Outbound channel */
95 struct local_pvt *next; /* Next entity */
98 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, struct ast_channel *us)
100 struct ast_channel *other;
102 /* Recalculate outbound channel */
108 /* Set glare detection */
110 if (p->cancelqueue) {
111 /* We had a glare on the hangup. Forget all this business,
112 return and destroy p. */
113 ast_mutex_unlock(&p->lock);
114 ast_mutex_destroy(&p->lock);
122 if (ast_mutex_trylock(&other->lock)) {
123 /* Failed to lock. Release main lock and try again */
124 ast_mutex_unlock(&p->lock);
126 if (ast_mutex_unlock(&us->lock)) {
127 ast_log(LOG_WARNING, "%s wasn't locked while sending %d/%d\n",
128 us->name, f->frametype, f->subclass);
132 /* Wait just a bit */
134 /* Only we can destroy ourselves, so we can't disappear here */
136 ast_mutex_lock(&us->lock);
137 ast_mutex_lock(&p->lock);
140 ast_queue_frame(other, f);
141 ast_mutex_unlock(&other->lock);
146 static int local_answer(struct ast_channel *ast)
148 struct local_pvt *p = ast->tech_pvt;
151 ast_mutex_lock(&p->lock);
152 isoutbound = IS_OUTBOUND(ast, p);
154 /* Pass along answer since somebody answered us */
155 struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
156 res = local_queue_frame(p, isoutbound, &answer, ast);
158 ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
159 ast_mutex_unlock(&p->lock);
163 static void check_bridge(struct local_pvt *p, int isoutbound)
165 if (p->alreadymasqed || p->nooptimization)
167 if (isoutbound && p->chan && p->chan->_bridge /* Not ast_bridged_channel! Only go one step! */ && p->owner && !p->owner->readq) {
168 /* Masquerade bridged channel into owner */
169 /* Lock everything we need, one by one, and give up if
170 we can't get everything. Remember, we'll get another
171 chance in just a little bit */
172 if (!ast_mutex_trylock(&(p->chan->_bridge)->lock)) {
173 if (!ast_mutex_trylock(&p->owner->lock)) {
174 ast_channel_masquerade(p->owner, p->chan->_bridge);
175 p->alreadymasqed = 1;
176 ast_mutex_unlock(&p->owner->lock);
178 ast_mutex_unlock(&(p->chan->_bridge)->lock);
180 } else if (!isoutbound && p->owner && p->owner->_bridge && p->chan && !p->chan->readq) {
181 /* Masquerade bridged channel into chan */
182 if (!ast_mutex_trylock(&(p->owner->_bridge)->lock)) {
183 if (!ast_mutex_trylock(&p->chan->lock)) {
184 ast_channel_masquerade(p->chan, p->owner->_bridge);
185 p->alreadymasqed = 1;
186 ast_mutex_unlock(&p->chan->lock);
188 ast_mutex_unlock(&(p->owner->_bridge)->lock);
193 static struct ast_frame *local_read(struct ast_channel *ast)
195 static struct ast_frame null = { AST_FRAME_NULL, };
199 static int local_write(struct ast_channel *ast, struct ast_frame *f)
201 struct local_pvt *p = ast->tech_pvt;
206 /* Just queue for delivery to the other side */
207 ast_mutex_lock(&p->lock);
208 isoutbound = IS_OUTBOUND(ast, p);
209 res = local_queue_frame(p, isoutbound, f, ast);
210 check_bridge(p, isoutbound);
211 ast_mutex_unlock(&p->lock);
215 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
217 struct local_pvt *p = newchan->tech_pvt;
218 ast_mutex_lock(&p->lock);
219 if ((p->owner != oldchan) && (p->chan != oldchan)) {
220 ast_log(LOG_WARNING, "old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
221 ast_mutex_unlock(&p->lock);
224 if (p->owner == oldchan)
228 ast_mutex_unlock(&p->lock);
232 static int local_indicate(struct ast_channel *ast, int condition)
234 struct local_pvt *p = ast->tech_pvt;
236 struct ast_frame f = { AST_FRAME_CONTROL, };
238 /* Queue up a frame representing the indication as a control frame */
239 ast_mutex_lock(&p->lock);
240 isoutbound = IS_OUTBOUND(ast, p);
241 f.subclass = condition;
242 res = local_queue_frame(p, isoutbound, &f, ast);
243 ast_mutex_unlock(&p->lock);
247 static int local_digit(struct ast_channel *ast, char digit)
249 struct local_pvt *p = ast->tech_pvt;
251 struct ast_frame f = { AST_FRAME_DTMF, };
253 ast_mutex_lock(&p->lock);
254 isoutbound = IS_OUTBOUND(ast, p);
256 res = local_queue_frame(p, isoutbound, &f, ast);
257 ast_mutex_unlock(&p->lock);
261 static int local_sendhtml(struct ast_channel *ast, int subclass, char *data, int datalen)
263 struct local_pvt *p = ast->tech_pvt;
265 struct ast_frame f = { AST_FRAME_HTML, };
267 ast_mutex_lock(&p->lock);
268 isoutbound = IS_OUTBOUND(ast, p);
269 f.subclass = subclass;
272 res = local_queue_frame(p, isoutbound, &f, ast);
273 ast_mutex_unlock(&p->lock);
277 static int local_call(struct ast_channel *ast, char *dest, int timeout)
279 struct local_pvt *p = ast->tech_pvt;
282 ast_mutex_lock(&p->lock);
283 if (p->owner->cid.cid_num)
284 p->chan->cid.cid_num = strdup(p->owner->cid.cid_num);
286 p->chan->cid.cid_num = NULL;
288 if (p->owner->cid.cid_name)
289 p->chan->cid.cid_name = strdup(p->owner->cid.cid_name);
291 p->chan->cid.cid_name = NULL;
293 if (p->owner->cid.cid_rdnis)
294 p->chan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
296 p->chan->cid.cid_rdnis = NULL;
298 if (p->owner->cid.cid_ani)
299 p->chan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
301 p->chan->cid.cid_ani = NULL;
303 strncpy(p->chan->language, p->owner->language, sizeof(p->chan->language) - 1);
304 strncpy(p->chan->accountcode, p->owner->accountcode, sizeof(p->chan->accountcode) - 1);
305 p->chan->cdrflags = p->owner->cdrflags;
308 /* Start switch on sub channel */
309 res = ast_pbx_start(p->chan);
310 ast_mutex_unlock(&p->lock);
315 static void local_destroy(struct local_pvt *p)
317 struct local_pvt *cur, *prev = NULL;
318 ast_mutex_lock(&locallock);
323 prev->next = cur->next;
326 ast_mutex_destroy(cur);
333 ast_mutex_unlock(&locallock);
335 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
339 static int local_hangup(struct ast_channel *ast)
341 struct local_pvt *p = ast->tech_pvt;
343 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
344 struct local_pvt *cur, *prev=NULL;
345 struct ast_channel *ochan = NULL;
347 ast_mutex_lock(&p->lock);
348 isoutbound = IS_OUTBOUND(ast, p);
354 ast->tech_pvt = NULL;
356 ast_mutex_lock(&usecnt_lock);
358 ast_mutex_unlock(&usecnt_lock);
360 if (!p->owner && !p->chan) {
361 /* Okay, done with the private part now, too. */
362 glaredetect = p->glaredetect;
363 /* If we have a queue holding, don't actually destroy p yet, but
364 let local_queue do it. */
367 ast_mutex_unlock(&p->lock);
368 /* Remove from list */
369 ast_mutex_lock(&locallock);
374 prev->next = cur->next;
382 ast_mutex_unlock(&locallock);
383 /* Grab / release lock just in case */
384 ast_mutex_lock(&p->lock);
385 ast_mutex_unlock(&p->lock);
388 ast_mutex_destroy(&p->lock);
393 if (p->chan && !p->launchedpbx)
394 /* Need to actually hangup since there is no PBX */
397 local_queue_frame(p, isoutbound, &f, NULL);
398 ast_mutex_unlock(&p->lock);
404 static struct local_pvt *local_alloc(char *data, int format)
406 struct local_pvt *tmp;
409 tmp = malloc(sizeof(struct local_pvt));
411 memset(tmp, 0, sizeof(struct local_pvt));
412 ast_mutex_init(&tmp->lock);
413 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
414 opts = strchr(tmp->exten, '/');
418 if (strchr(opts, 'n'))
419 tmp->nooptimization = 1;
421 c = strchr(tmp->exten, '@');
425 strncpy(tmp->context, c, sizeof(tmp->context) - 1);
427 strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
428 tmp->reqformat = format;
429 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
430 ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
431 ast_mutex_destroy(&tmp->lock);
436 ast_mutex_lock(&locallock);
439 ast_mutex_unlock(&locallock);
446 static struct ast_channel *local_new(struct local_pvt *p, int state)
448 struct ast_channel *tmp, *tmp2;
449 int randnum = rand() & 0xffff;
450 tmp = ast_channel_alloc(1);
451 tmp2 = ast_channel_alloc(1);
454 ast_channel_free(tmp);
456 ast_channel_free(tmp2);
460 tmp2->tech = tmp->tech = &local_tech;
461 tmp->nativeformats = p->reqformat;
462 tmp2->nativeformats = p->reqformat;
463 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-%04x,1", p->exten, p->context, randnum);
464 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-%04x,2", p->exten, p->context, randnum);
467 ast_setstate(tmp, state);
468 ast_setstate(tmp2, AST_STATE_RING);
469 tmp->writeformat = p->reqformat;;
470 tmp2->writeformat = p->reqformat;
471 tmp->rawwriteformat = p->reqformat;
472 tmp2->rawwriteformat = p->reqformat;
473 tmp->readformat = p->reqformat;
474 tmp2->readformat = p->reqformat;
475 tmp->rawreadformat = p->reqformat;
476 tmp2->rawreadformat = p->reqformat;
481 ast_mutex_lock(&usecnt_lock);
484 ast_mutex_unlock(&usecnt_lock);
485 ast_update_use_count();
486 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
487 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
488 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
492 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
497 static struct ast_channel *local_request(const char *type, int format, void *data, int *cause)
500 struct ast_channel *chan = NULL;
501 p = local_alloc(data, format);
503 chan = local_new(p, AST_STATE_DOWN);
507 static int locals_show(int fd, int argc, char **argv)
512 return RESULT_SHOWUSAGE;
513 ast_mutex_lock(&locallock);
516 ast_mutex_lock(&p->lock);
517 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
518 ast_mutex_unlock(&p->lock);
522 ast_cli(fd, "No local channels in use\n");
523 ast_mutex_unlock(&locallock);
524 return RESULT_SUCCESS;
527 static char show_locals_usage[] =
528 "Usage: local show channels\n"
529 " Provides summary information on local channels.\n";
531 static struct ast_cli_entry cli_show_locals = {
532 { "local", "show", "channels", NULL }, locals_show,
533 "Show status of local channels", show_locals_usage, NULL };
537 /* Make sure we can register our channel type */
538 if (ast_channel_register(&local_tech)) {
539 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
542 ast_cli_register(&cli_show_locals);
554 /* First, take us out of the channel loop */
555 ast_cli_unregister(&cli_show_locals);
556 ast_channel_unregister(&local_tech);
557 if (!ast_mutex_lock(&locallock)) {
558 /* Hangup all interfaces if they have an owner */
562 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
566 ast_mutex_unlock(&locallock);
568 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
577 ast_mutex_lock(&usecnt_lock);
579 ast_mutex_unlock(&usecnt_lock);
585 return ASTERISK_GPL_KEY;
590 return (char *) desc;