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 pthread_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 pthread_mutex_t locallock = AST_MUTEX_INITIALIZER;
59 static struct local_pvt {
60 pthread_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 alreadymasqed; /* Already masqueraded */
65 struct ast_channel *owner; /* Master Channel */
66 struct ast_channel *chan; /* Outbound channel */
67 struct local_pvt *next; /* Next entity */
70 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f)
72 struct ast_channel *other;
80 if (pthread_mutex_trylock(&other->lock)) {
81 /* Failed to lock. Release main lock and try again */
82 ast_pthread_mutex_unlock(&p->lock);
85 ast_pthread_mutex_lock(&p->lock);
88 ast_queue_frame(other, f, 0);
89 ast_pthread_mutex_unlock(&other->lock);
93 static int local_answer(struct ast_channel *ast)
95 struct local_pvt *p = ast->pvt->pvt;
96 int isoutbound = IS_OUTBOUND(ast, p);
98 ast_pthread_mutex_lock(&p->lock);
100 /* Pass along answer since somebody answered us */
101 struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
102 res = local_queue_frame(p, isoutbound, &answer);
104 ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
105 ast_pthread_mutex_unlock(&p->lock);
109 static void check_bridge(struct local_pvt *p, int isoutbound)
111 if (p->alreadymasqed)
113 if (isoutbound && p->chan && p->chan->bridge && p->owner) {
114 /* Masquerade bridged channel into owner */
115 ast_channel_masquerade(p->owner, p->chan->bridge);
116 p->alreadymasqed = 1;
117 } else if (!isoutbound && p->owner && p->owner->bridge && p->chan) {
118 /* Masquerade bridged channel into chan */
119 ast_channel_masquerade(p->chan, p->owner->bridge);
120 p->alreadymasqed = 1;
124 static struct ast_frame *local_read(struct ast_channel *ast)
126 static struct ast_frame null = { AST_FRAME_NULL, };
130 static int local_write(struct ast_channel *ast, struct ast_frame *f)
132 struct local_pvt *p = ast->pvt->pvt;
134 int isoutbound = IS_OUTBOUND(ast, p);
137 /* Just queue for delivery to the other side */
138 ast_pthread_mutex_lock(&p->lock);
139 res = local_queue_frame(p, isoutbound, f);
140 check_bridge(p, isoutbound);
141 ast_pthread_mutex_unlock(&p->lock);
145 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
147 struct local_pvt *p = newchan->pvt->pvt;
148 ast_pthread_mutex_lock(&p->lock);
149 if ((p->owner != oldchan) && (p->chan != oldchan)) {
150 ast_log(LOG_WARNING, "old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
151 ast_pthread_mutex_unlock(&p->lock);
154 if (p->owner == oldchan)
158 ast_pthread_mutex_unlock(&p->lock);
162 static int local_indicate(struct ast_channel *ast, int condition)
164 struct local_pvt *p = ast->pvt->pvt;
166 struct ast_frame f = { AST_FRAME_CONTROL, };
167 int isoutbound = IS_OUTBOUND(ast, p);
168 /* Queue up a frame representing the indication as a control frame */
169 ast_pthread_mutex_lock(&p->lock);
170 f.subclass = condition;
171 res = local_queue_frame(p, isoutbound, &f);
172 ast_pthread_mutex_unlock(&p->lock);
176 static int local_digit(struct ast_channel *ast, char digit)
178 struct local_pvt *p = ast->pvt->pvt;
180 struct ast_frame f = { AST_FRAME_DTMF, };
181 int isoutbound = IS_OUTBOUND(ast, p);
182 ast_pthread_mutex_lock(&p->lock);
184 res = local_queue_frame(p, isoutbound, &f);
185 ast_pthread_mutex_unlock(&p->lock);
189 static int local_call(struct ast_channel *ast, char *dest, int timeout)
191 struct local_pvt *p = ast->pvt->pvt;
192 /* Start switch on sub channel */
193 return ast_pbx_start(p->chan);
196 static void local_destroy(struct local_pvt *p)
198 struct local_pvt *cur, *prev = NULL;
199 ast_pthread_mutex_lock(&locallock);
204 prev->next = cur->next;
213 ast_pthread_mutex_unlock(&locallock);
215 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
218 static int local_hangup(struct ast_channel *ast)
220 struct local_pvt *p = ast->pvt->pvt;
221 int isoutbound = IS_OUTBOUND(ast, p);
222 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
223 struct local_pvt *cur, *prev=NULL;
224 ast_pthread_mutex_lock(&p->lock);
229 ast->pvt->pvt = NULL;
230 if (!p->owner && !p->chan) {
231 /* Okay, done with the private part now, too. */
232 ast_pthread_mutex_unlock(&p->lock);
233 /* Remove from list */
234 ast_pthread_mutex_lock(&locallock);
239 prev->next == cur->next;
247 ast_pthread_mutex_unlock(&locallock);
252 local_queue_frame(p, isoutbound, &f);
253 ast_pthread_mutex_unlock(&p->lock);
257 static struct local_pvt *local_alloc(char *data, int format)
259 struct local_pvt *tmp;
261 tmp = malloc(sizeof(struct local_pvt));
263 memset(tmp, 0, sizeof(struct local_pvt));
264 ast_pthread_mutex_init(&tmp->lock);
265 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
266 c = strchr(tmp->exten, '@');
270 strncpy(tmp->context, c, sizeof(tmp->context) - 1);
272 strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
273 tmp->reqformat = format;
274 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
275 ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->context, tmp->exten);
280 ast_pthread_mutex_lock(&locallock);
283 ast_pthread_mutex_unlock(&locallock);
290 static struct ast_channel *local_new(struct local_pvt *p, int state)
292 struct ast_channel *tmp, *tmp2;
293 tmp = ast_channel_alloc(1);
294 tmp2 = ast_channel_alloc(1);
297 ast_channel_free(tmp);
299 ast_channel_free(tmp2);
303 tmp->nativeformats = p->reqformat;
304 tmp2->nativeformats = p->reqformat;
305 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-1", p->exten, p->context);
306 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-2", p->exten, p->context);
309 ast_setstate(tmp, state);
310 ast_setstate(tmp2, AST_STATE_RING);
311 tmp->writeformat = p->reqformat;;
312 tmp2->writeformat = p->reqformat;
313 tmp->pvt->rawwriteformat = p->reqformat;
314 tmp2->pvt->rawwriteformat = p->reqformat;
315 tmp->readformat = p->reqformat;
316 tmp2->readformat = p->reqformat;
317 tmp->pvt->rawreadformat = p->reqformat;
318 tmp2->pvt->rawreadformat = p->reqformat;
321 tmp->pvt->send_digit = local_digit;
322 tmp2->pvt->send_digit = local_digit;
323 tmp->pvt->call = local_call;
324 tmp2->pvt->call = local_call;
325 tmp->pvt->hangup = local_hangup;
326 tmp2->pvt->hangup = local_hangup;
327 tmp->pvt->answer = local_answer;
328 tmp2->pvt->answer = local_answer;
329 tmp->pvt->read = local_read;
330 tmp2->pvt->read = local_read;
331 tmp->pvt->write = local_write;
332 tmp2->pvt->write = local_write;
333 tmp->pvt->exception = local_read;
334 tmp2->pvt->exception = local_read;
335 tmp->pvt->indicate = local_indicate;
336 tmp2->pvt->indicate = local_indicate;
337 tmp->pvt->fixup = local_fixup;
338 tmp2->pvt->fixup = local_fixup;
341 ast_pthread_mutex_lock(&usecnt_lock);
343 ast_pthread_mutex_unlock(&usecnt_lock);
344 ast_update_use_count();
345 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
346 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
347 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
351 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
356 static struct ast_channel *local_request(char *type, int format, void *data)
359 struct ast_channel *chan = NULL;
360 p = local_alloc(data, format);
362 chan = local_new(p, AST_STATE_DOWN);
366 static int locals_show(int fd, int argc, char **argv)
371 return RESULT_SHOWUSAGE;
372 ast_pthread_mutex_lock(&locallock);
375 ast_pthread_mutex_lock(&p->lock);
376 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
377 ast_pthread_mutex_unlock(&p->lock);
381 ast_cli(fd, "No local channels in use\n");
382 ast_pthread_mutex_unlock(&locallock);
383 return RESULT_SUCCESS;
386 static char show_locals_usage[] =
387 "Usage: show locals\n"
388 " Provides summary information on locals.\n";
390 static struct ast_cli_entry cli_show_locals = {
391 { "show", "locals", NULL }, locals_show,
392 "Show status of local channels", show_locals_usage, NULL };
396 /* Make sure we can register our sip channel type */
397 if (ast_channel_register(type, tdesc, capability, local_request)) {
398 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
401 ast_cli_register(&cli_show_locals);
413 /* First, take us out of the channel loop */
414 ast_cli_unregister(&cli_show_locals);
415 ast_channel_unregister(type);
416 if (!ast_pthread_mutex_lock(&locallock)) {
417 /* Hangup all interfaces if they have an owner */
421 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
425 ast_pthread_mutex_unlock(&locallock);
427 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
436 ast_pthread_mutex_lock(&usecnt_lock);
438 ast_pthread_mutex_unlock(&usecnt_lock);
444 return ASTERISK_GPL_KEY;