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
17 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/channel_pvt.h>
20 #include <asterisk/config.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/module.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/options.h>
25 #include <asterisk/lock.h>
26 #include <asterisk/sched.h>
27 #include <asterisk/io.h>
28 #include <asterisk/rtp.h>
29 #include <asterisk/acl.h>
30 #include <asterisk/callerid.h>
31 #include <asterisk/file.h>
32 #include <asterisk/cli.h>
33 #include <asterisk/app.h>
34 #include <asterisk/musiconhold.h>
35 #include <asterisk/manager.h>
36 #include <sys/socket.h>
42 #include <arpa/inet.h>
43 #include <sys/signal.h>
45 static char *desc = "Local Proxy Channel";
46 static char *type = "Local";
47 static char *tdesc = "Local Proxy Channel Driver";
49 static int capability = -1;
52 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
54 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
56 /* Protect the interface list (of sip_pvt's) */
57 static ast_mutex_t locallock = AST_MUTEX_INITIALIZER;
59 static struct local_pvt {
60 ast_mutex_t lock; /* Channel private lock */
61 char context[AST_MAX_EXTENSION]; /* Context to call */
62 char exten[AST_MAX_EXTENSION]; /* Extension to call */
63 int reqformat; /* Requested format */
64 int glaredetect; /* Detect glare on hangup */
65 int cancelqueue; /* Cancel queue */
66 int alreadymasqed; /* Already masqueraded */
67 int launchedpbx; /* Did we launch the PBX */
69 struct ast_channel *owner; /* Master Channel */
70 struct ast_channel *chan; /* Outbound channel */
71 struct local_pvt *next; /* Next entity */
74 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, struct ast_channel *us)
76 struct ast_channel *other;
78 /* Recalculate outbound channel */
84 /* Set glare detection */
87 /* We had a glare on the hangup. Forget all this business,
88 return and destroy p. */
89 ast_mutex_unlock(&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;
124 int isoutbound = IS_OUTBOUND(ast, p);
126 ast_mutex_lock(&p->lock);
128 /* Pass along answer since somebody answered us */
129 struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
130 res = local_queue_frame(p, isoutbound, &answer, ast);
132 ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
133 ast_mutex_unlock(&p->lock);
137 static void check_bridge(struct local_pvt *p, int isoutbound)
139 if (p->alreadymasqed || p->nooptimization)
141 if (isoutbound && p->chan && p->chan->bridge && p->owner) {
142 /* Masquerade bridged channel into owner */
143 /* Lock everything we need, one by one, and give up if
144 we can't get everything. Remember, we'll get another
145 chance in just a little bit */
146 if (!ast_mutex_trylock(&p->chan->bridge->lock)) {
147 if (!ast_mutex_trylock(&p->owner->lock)) {
148 ast_channel_masquerade(p->owner, p->chan->bridge);
149 p->alreadymasqed = 1;
150 ast_mutex_unlock(&p->owner->lock);
152 ast_mutex_unlock(&p->chan->bridge->lock);
154 } else if (!isoutbound && p->owner && p->owner->bridge && p->chan) {
155 /* Masquerade bridged channel into chan */
156 if (!ast_mutex_trylock(&p->owner->bridge->lock)) {
157 if (!ast_mutex_trylock(&p->chan->lock)) {
158 ast_channel_masquerade(p->chan, p->owner->bridge);
159 p->alreadymasqed = 1;
160 ast_mutex_unlock(&p->chan->lock);
162 ast_mutex_unlock(&p->owner->bridge->lock);
167 static struct ast_frame *local_read(struct ast_channel *ast)
169 static struct ast_frame null = { AST_FRAME_NULL, };
173 static int local_write(struct ast_channel *ast, struct ast_frame *f)
175 struct local_pvt *p = ast->pvt->pvt;
177 int isoutbound = IS_OUTBOUND(ast, p);
180 /* Just queue for delivery to the other side */
181 ast_mutex_lock(&p->lock);
182 res = local_queue_frame(p, isoutbound, f, ast);
183 check_bridge(p, isoutbound);
184 ast_mutex_unlock(&p->lock);
188 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
190 struct local_pvt *p = newchan->pvt->pvt;
191 ast_mutex_lock(&p->lock);
192 if ((p->owner != oldchan) && (p->chan != oldchan)) {
193 ast_log(LOG_WARNING, "old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
194 ast_mutex_unlock(&p->lock);
197 if (p->owner == oldchan)
201 ast_mutex_unlock(&p->lock);
205 static int local_indicate(struct ast_channel *ast, int condition)
207 struct local_pvt *p = ast->pvt->pvt;
209 struct ast_frame f = { AST_FRAME_CONTROL, };
210 int isoutbound = IS_OUTBOUND(ast, p);
211 /* Queue up a frame representing the indication as a control frame */
212 ast_mutex_lock(&p->lock);
213 f.subclass = condition;
214 res = local_queue_frame(p, isoutbound, &f, ast);
215 ast_mutex_unlock(&p->lock);
219 static int local_digit(struct ast_channel *ast, char digit)
221 struct local_pvt *p = ast->pvt->pvt;
223 struct ast_frame f = { AST_FRAME_DTMF, };
224 int isoutbound = IS_OUTBOUND(ast, p);
225 ast_mutex_lock(&p->lock);
227 res = local_queue_frame(p, isoutbound, &f, ast);
228 ast_mutex_unlock(&p->lock);
232 static int local_call(struct ast_channel *ast, char *dest, int timeout)
234 struct local_pvt *p = ast->pvt->pvt;
237 ast_mutex_lock(&p->lock);
238 if (p->owner->callerid)
239 p->chan->callerid = strdup(p->owner->callerid);
241 p->chan->callerid = NULL;
243 p->chan->rdnis = strdup(p->owner->rdnis);
245 p->chan->rdnis = NULL;
247 p->chan->ani = strdup(p->owner->ani);
251 /* Start switch on sub channel */
252 res = ast_pbx_start(p->chan);
253 ast_mutex_unlock(&p->lock);
258 static void local_destroy(struct local_pvt *p)
260 struct local_pvt *cur, *prev = NULL;
261 ast_mutex_lock(&locallock);
266 prev->next = cur->next;
275 ast_mutex_unlock(&locallock);
277 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
281 static int local_hangup(struct ast_channel *ast)
283 struct local_pvt *p = ast->pvt->pvt;
284 int isoutbound = IS_OUTBOUND(ast, p);
285 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
286 struct local_pvt *cur, *prev=NULL;
287 struct ast_channel *ochan = NULL;
289 ast_mutex_lock(&p->lock);
295 ast->pvt->pvt = NULL;
297 if (!p->owner && !p->chan) {
298 /* Okay, done with the private part now, too. */
299 glaredetect = p->glaredetect;
300 /* If we have a queue holding, don't actually destroy p yet, but
301 let local_queue do it. */
304 ast_mutex_unlock(&p->lock);
305 /* Remove from list */
306 ast_mutex_lock(&locallock);
311 prev->next = cur->next;
319 ast_mutex_unlock(&locallock);
325 if (p->chan && !p->launchedpbx)
326 /* Need to actually hangup since there is no PBX */
329 local_queue_frame(p, isoutbound, &f, NULL);
330 ast_mutex_unlock(&p->lock);
336 static struct local_pvt *local_alloc(char *data, int format)
338 struct local_pvt *tmp;
341 tmp = malloc(sizeof(struct local_pvt));
343 memset(tmp, 0, sizeof(struct local_pvt));
344 ast_mutex_init(&tmp->lock);
345 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
346 opts = strchr(tmp->exten, '/');
350 if (strchr(opts, 'n'))
351 tmp->nooptimization = 1;
353 c = strchr(tmp->exten, '@');
357 strncpy(tmp->context, c, sizeof(tmp->context) - 1);
359 strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
360 tmp->reqformat = format;
361 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
362 ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->context, tmp->exten);
367 ast_mutex_lock(&locallock);
370 ast_mutex_unlock(&locallock);
377 static struct ast_channel *local_new(struct local_pvt *p, int state)
379 struct ast_channel *tmp, *tmp2;
380 int randnum = rand() & 0xffff;
381 tmp = ast_channel_alloc(1);
382 tmp2 = ast_channel_alloc(1);
385 ast_channel_free(tmp);
387 ast_channel_free(tmp2);
391 tmp->nativeformats = p->reqformat;
392 tmp2->nativeformats = p->reqformat;
393 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-%04x,1", p->exten, p->context, randnum);
394 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-%04x,2", p->exten, p->context, randnum);
397 ast_setstate(tmp, state);
398 ast_setstate(tmp2, AST_STATE_RING);
399 tmp->writeformat = p->reqformat;;
400 tmp2->writeformat = p->reqformat;
401 tmp->pvt->rawwriteformat = p->reqformat;
402 tmp2->pvt->rawwriteformat = p->reqformat;
403 tmp->readformat = p->reqformat;
404 tmp2->readformat = p->reqformat;
405 tmp->pvt->rawreadformat = p->reqformat;
406 tmp2->pvt->rawreadformat = p->reqformat;
409 tmp->pvt->send_digit = local_digit;
410 tmp2->pvt->send_digit = local_digit;
411 tmp->pvt->call = local_call;
412 tmp2->pvt->call = local_call;
413 tmp->pvt->hangup = local_hangup;
414 tmp2->pvt->hangup = local_hangup;
415 tmp->pvt->answer = local_answer;
416 tmp2->pvt->answer = local_answer;
417 tmp->pvt->read = local_read;
418 tmp2->pvt->read = local_read;
419 tmp->pvt->write = local_write;
420 tmp2->pvt->write = local_write;
421 tmp->pvt->exception = local_read;
422 tmp2->pvt->exception = local_read;
423 tmp->pvt->indicate = local_indicate;
424 tmp2->pvt->indicate = local_indicate;
425 tmp->pvt->fixup = local_fixup;
426 tmp2->pvt->fixup = local_fixup;
429 ast_mutex_lock(&usecnt_lock);
431 ast_mutex_unlock(&usecnt_lock);
432 ast_update_use_count();
433 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
434 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
435 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
439 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
444 static struct ast_channel *local_request(char *type, int format, void *data)
447 struct ast_channel *chan = NULL;
448 p = local_alloc(data, format);
450 chan = local_new(p, AST_STATE_DOWN);
454 static int locals_show(int fd, int argc, char **argv)
459 return RESULT_SHOWUSAGE;
460 ast_mutex_lock(&locallock);
463 ast_mutex_lock(&p->lock);
464 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
465 ast_mutex_unlock(&p->lock);
469 ast_cli(fd, "No local channels in use\n");
470 ast_mutex_unlock(&locallock);
471 return RESULT_SUCCESS;
474 static char show_locals_usage[] =
475 "Usage: local show channels\n"
476 " Provides summary information on local channels.\n";
478 static struct ast_cli_entry cli_show_locals = {
479 { "local", "show", "channels", NULL }, locals_show,
480 "Show status of local channels", show_locals_usage, NULL };
484 /* Make sure we can register our sip channel type */
485 if (ast_channel_register(type, tdesc, capability, local_request)) {
486 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
489 ast_cli_register(&cli_show_locals);
501 /* First, take us out of the channel loop */
502 ast_cli_unregister(&cli_show_locals);
503 ast_channel_unregister(type);
504 if (!ast_mutex_lock(&locallock)) {
505 /* Hangup all interfaces if they have an owner */
509 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
513 ast_mutex_unlock(&locallock);
515 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
524 ast_mutex_lock(&usecnt_lock);
526 ast_mutex_unlock(&usecnt_lock);
532 return ASTERISK_GPL_KEY;