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/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>
41 #include <arpa/inet.h>
42 #include <sys/signal.h>
44 static char *desc = "Local Proxy Channel";
45 static char *type = "Local";
46 static char *tdesc = "Local Proxy Channel Driver";
48 static int capability = -1;
51 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
53 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
55 /* Protect the interface list (of sip_pvt's) */
56 AST_MUTEX_DEFINE_STATIC(locallock);
58 static struct local_pvt {
59 ast_mutex_t lock; /* Channel private lock */
60 char context[AST_MAX_EXTENSION]; /* Context to call */
61 char exten[AST_MAX_EXTENSION]; /* Extension to call */
62 int reqformat; /* Requested format */
63 int glaredetect; /* Detect glare on hangup */
64 int cancelqueue; /* Cancel queue */
65 int alreadymasqed; /* Already masqueraded */
66 int launchedpbx; /* Did we launch the PBX */
68 struct ast_channel *owner; /* Master Channel */
69 struct ast_channel *chan; /* Outbound channel */
70 struct local_pvt *next; /* Next entity */
73 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, struct ast_channel *us)
75 struct ast_channel *other;
77 /* Recalculate outbound channel */
83 /* Set glare detection */
86 /* We had a glare on the hangup. Forget all this business,
87 return and destroy p. */
88 ast_mutex_unlock(&p->lock);
89 ast_mutex_destroy(&p->lock);
97 if (ast_mutex_trylock(&other->lock)) {
98 /* Failed to lock. Release main lock and try again */
99 ast_mutex_unlock(&p->lock);
101 if (ast_mutex_unlock(&us->lock)) {
102 ast_log(LOG_WARNING, "%s wasn't locked while sending %d/%d\n",
103 us->name, f->frametype, f->subclass);
107 /* Wait just a bit */
109 /* Only we can destroy ourselves, so we can't disappear here */
111 ast_mutex_lock(&us->lock);
112 ast_mutex_lock(&p->lock);
115 ast_queue_frame(other, f);
116 ast_mutex_unlock(&other->lock);
121 static int local_answer(struct ast_channel *ast)
123 struct local_pvt *p = ast->pvt->pvt;
126 ast_mutex_lock(&p->lock);
127 isoutbound = IS_OUTBOUND(ast, p);
129 /* Pass along answer since somebody answered us */
130 struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
131 res = local_queue_frame(p, isoutbound, &answer, ast);
133 ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
134 ast_mutex_unlock(&p->lock);
138 static void check_bridge(struct local_pvt *p, int isoutbound)
140 if (p->alreadymasqed || p->nooptimization)
142 if (isoutbound && p->chan && ast_bridged_channel(p->chan) && p->owner) {
143 /* Masquerade bridged channel into owner */
144 /* Lock everything we need, one by one, and give up if
145 we can't get everything. Remember, we'll get another
146 chance in just a little bit */
147 if (!ast_mutex_trylock(&(ast_bridged_channel(p->chan))->lock)) {
148 if (!ast_mutex_trylock(&p->owner->lock)) {
149 ast_channel_masquerade(p->owner, ast_bridged_channel(p->chan));
150 p->alreadymasqed = 1;
151 ast_mutex_unlock(&p->owner->lock);
153 ast_mutex_unlock(&(ast_bridged_channel(p->chan)->lock));
155 } else if (!isoutbound && p->owner && ast_bridged_channel(p->owner) && p->chan) {
156 /* Masquerade bridged channel into chan */
157 if (!ast_mutex_trylock(&(ast_bridged_channel(p->owner)->lock))) {
158 if (!ast_mutex_trylock(&p->chan->lock)) {
159 ast_channel_masquerade(p->chan, ast_bridged_channel(p->owner));
160 p->alreadymasqed = 1;
161 ast_mutex_unlock(&p->chan->lock);
163 ast_mutex_unlock(&(ast_bridged_channel(p->owner)->lock));
168 static struct ast_frame *local_read(struct ast_channel *ast)
170 static struct ast_frame null = { AST_FRAME_NULL, };
174 static int local_write(struct ast_channel *ast, struct ast_frame *f)
176 struct local_pvt *p = ast->pvt->pvt;
181 /* Just queue for delivery to the other side */
182 ast_mutex_lock(&p->lock);
183 isoutbound = IS_OUTBOUND(ast, p);
184 res = local_queue_frame(p, isoutbound, f, ast);
185 check_bridge(p, isoutbound);
186 ast_mutex_unlock(&p->lock);
190 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
192 struct local_pvt *p = newchan->pvt->pvt;
193 ast_mutex_lock(&p->lock);
194 if ((p->owner != oldchan) && (p->chan != oldchan)) {
195 ast_log(LOG_WARNING, "old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
196 ast_mutex_unlock(&p->lock);
199 if (p->owner == oldchan)
203 ast_mutex_unlock(&p->lock);
207 static int local_indicate(struct ast_channel *ast, int condition)
209 struct local_pvt *p = ast->pvt->pvt;
211 struct ast_frame f = { AST_FRAME_CONTROL, };
213 /* Queue up a frame representing the indication as a control frame */
214 ast_mutex_lock(&p->lock);
215 isoutbound = IS_OUTBOUND(ast, p);
216 f.subclass = condition;
217 res = local_queue_frame(p, isoutbound, &f, ast);
218 ast_mutex_unlock(&p->lock);
222 static int local_digit(struct ast_channel *ast, char digit)
224 struct local_pvt *p = ast->pvt->pvt;
226 struct ast_frame f = { AST_FRAME_DTMF, };
228 ast_mutex_lock(&p->lock);
229 isoutbound = IS_OUTBOUND(ast, p);
231 res = local_queue_frame(p, isoutbound, &f, ast);
232 ast_mutex_unlock(&p->lock);
236 static int local_call(struct ast_channel *ast, char *dest, int timeout)
238 struct local_pvt *p = ast->pvt->pvt;
241 ast_mutex_lock(&p->lock);
242 if (p->owner->cid.cid_num)
243 p->chan->cid.cid_num = strdup(p->owner->cid.cid_num);
245 p->chan->cid.cid_num = NULL;
247 if (p->owner->cid.cid_name)
248 p->chan->cid.cid_name = strdup(p->owner->cid.cid_name);
250 p->chan->cid.cid_name = NULL;
252 if (p->owner->cid.cid_rdnis)
253 p->chan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
255 p->chan->cid.cid_rdnis = NULL;
257 if (p->owner->cid.cid_ani)
258 p->chan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
260 p->chan->cid.cid_ani = NULL;
262 strncpy(p->chan->language, p->owner->language, sizeof(p->chan->language) - 1);
263 strncpy(p->chan->accountcode, p->owner->accountcode, sizeof(p->chan->accountcode) - 1);
264 p->chan->cdrflags = p->owner->cdrflags;
267 /* Start switch on sub channel */
268 res = ast_pbx_start(p->chan);
269 ast_mutex_unlock(&p->lock);
274 static void local_destroy(struct local_pvt *p)
276 struct local_pvt *cur, *prev = NULL;
277 ast_mutex_lock(&locallock);
282 prev->next = cur->next;
285 ast_mutex_destroy(cur);
292 ast_mutex_unlock(&locallock);
294 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
298 static int local_hangup(struct ast_channel *ast)
300 struct local_pvt *p = ast->pvt->pvt;
302 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
303 struct local_pvt *cur, *prev=NULL;
304 struct ast_channel *ochan = NULL;
306 ast_mutex_lock(&p->lock);
307 isoutbound = IS_OUTBOUND(ast, p);
313 ast->pvt->pvt = NULL;
315 if (!p->owner && !p->chan) {
316 /* Okay, done with the private part now, too. */
317 glaredetect = p->glaredetect;
318 /* If we have a queue holding, don't actually destroy p yet, but
319 let local_queue do it. */
322 ast_mutex_unlock(&p->lock);
323 /* Remove from list */
324 ast_mutex_lock(&locallock);
329 prev->next = cur->next;
337 ast_mutex_unlock(&locallock);
340 ast_mutex_destroy(&p->lock);
345 if (p->chan && !p->launchedpbx)
346 /* Need to actually hangup since there is no PBX */
349 local_queue_frame(p, isoutbound, &f, NULL);
350 ast_mutex_unlock(&p->lock);
356 static struct local_pvt *local_alloc(char *data, int format)
358 struct local_pvt *tmp;
361 tmp = malloc(sizeof(struct local_pvt));
363 memset(tmp, 0, sizeof(struct local_pvt));
364 ast_mutex_init(&tmp->lock);
365 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
366 opts = strchr(tmp->exten, '/');
370 if (strchr(opts, 'n'))
371 tmp->nooptimization = 1;
373 c = strchr(tmp->exten, '@');
377 strncpy(tmp->context, c, sizeof(tmp->context) - 1);
379 strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
380 tmp->reqformat = format;
381 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
382 ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
383 ast_mutex_destroy(&tmp->lock);
388 ast_mutex_lock(&locallock);
391 ast_mutex_unlock(&locallock);
398 static struct ast_channel *local_new(struct local_pvt *p, int state)
400 struct ast_channel *tmp, *tmp2;
401 int randnum = rand() & 0xffff;
402 tmp = ast_channel_alloc(1);
403 tmp2 = ast_channel_alloc(1);
406 ast_channel_free(tmp);
408 ast_channel_free(tmp2);
412 tmp->nativeformats = p->reqformat;
413 tmp2->nativeformats = p->reqformat;
414 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-%04x,1", p->exten, p->context, randnum);
415 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-%04x,2", p->exten, p->context, randnum);
418 ast_setstate(tmp, state);
419 ast_setstate(tmp2, AST_STATE_RING);
420 tmp->writeformat = p->reqformat;;
421 tmp2->writeformat = p->reqformat;
422 tmp->pvt->rawwriteformat = p->reqformat;
423 tmp2->pvt->rawwriteformat = p->reqformat;
424 tmp->readformat = p->reqformat;
425 tmp2->readformat = p->reqformat;
426 tmp->pvt->rawreadformat = p->reqformat;
427 tmp2->pvt->rawreadformat = p->reqformat;
430 tmp->pvt->send_digit = local_digit;
431 tmp2->pvt->send_digit = local_digit;
432 tmp->pvt->call = local_call;
433 tmp2->pvt->call = local_call;
434 tmp->pvt->hangup = local_hangup;
435 tmp2->pvt->hangup = local_hangup;
436 tmp->pvt->answer = local_answer;
437 tmp2->pvt->answer = local_answer;
438 tmp->pvt->read = local_read;
439 tmp2->pvt->read = local_read;
440 tmp->pvt->write = local_write;
441 tmp2->pvt->write = local_write;
442 tmp->pvt->exception = local_read;
443 tmp2->pvt->exception = local_read;
444 tmp->pvt->indicate = local_indicate;
445 tmp2->pvt->indicate = local_indicate;
446 tmp->pvt->fixup = local_fixup;
447 tmp2->pvt->fixup = local_fixup;
450 ast_mutex_lock(&usecnt_lock);
452 ast_mutex_unlock(&usecnt_lock);
453 ast_update_use_count();
454 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
455 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
456 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
460 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
465 static struct ast_channel *local_request(const char *type, int format, void *data)
468 struct ast_channel *chan = NULL;
469 p = local_alloc(data, format);
471 chan = local_new(p, AST_STATE_DOWN);
475 static int locals_show(int fd, int argc, char **argv)
480 return RESULT_SHOWUSAGE;
481 ast_mutex_lock(&locallock);
484 ast_mutex_lock(&p->lock);
485 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
486 ast_mutex_unlock(&p->lock);
490 ast_cli(fd, "No local channels in use\n");
491 ast_mutex_unlock(&locallock);
492 return RESULT_SUCCESS;
495 static char show_locals_usage[] =
496 "Usage: local show channels\n"
497 " Provides summary information on local channels.\n";
499 static struct ast_cli_entry cli_show_locals = {
500 { "local", "show", "channels", NULL }, locals_show,
501 "Show status of local channels", show_locals_usage, NULL };
505 /* Make sure we can register our sip channel type */
506 if (ast_channel_register(type, tdesc, capability, local_request)) {
507 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
510 ast_cli_register(&cli_show_locals);
522 /* First, take us out of the channel loop */
523 ast_cli_unregister(&cli_show_locals);
524 ast_channel_unregister(type);
525 if (!ast_mutex_lock(&locallock)) {
526 /* Hangup all interfaces if they have an owner */
530 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
534 ast_mutex_unlock(&locallock);
536 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
545 ast_mutex_lock(&usecnt_lock);
547 ast_mutex_unlock(&usecnt_lock);
553 return ASTERISK_GPL_KEY;