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;
193 if (p->owner->callerid)
194 p->chan->callerid = strdup(p->owner->callerid);
196 p->chan->callerid = NULL;
198 p->chan->ani = strdup(p->owner->ani);
201 /* Start switch on sub channel */
202 return ast_pbx_start(p->chan);
205 static void local_destroy(struct local_pvt *p)
207 struct local_pvt *cur, *prev = NULL;
208 ast_pthread_mutex_lock(&locallock);
213 prev->next = cur->next;
222 ast_pthread_mutex_unlock(&locallock);
224 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
227 static int local_hangup(struct ast_channel *ast)
229 struct local_pvt *p = ast->pvt->pvt;
230 int isoutbound = IS_OUTBOUND(ast, p);
231 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
232 struct local_pvt *cur, *prev=NULL;
233 ast_pthread_mutex_lock(&p->lock);
238 ast->pvt->pvt = NULL;
239 if (!p->owner && !p->chan) {
240 /* Okay, done with the private part now, too. */
241 ast_pthread_mutex_unlock(&p->lock);
242 /* Remove from list */
243 ast_pthread_mutex_lock(&locallock);
248 prev->next = cur->next;
256 ast_pthread_mutex_unlock(&locallock);
261 local_queue_frame(p, isoutbound, &f);
262 ast_pthread_mutex_unlock(&p->lock);
266 static struct local_pvt *local_alloc(char *data, int format)
268 struct local_pvt *tmp;
270 tmp = malloc(sizeof(struct local_pvt));
272 memset(tmp, 0, sizeof(struct local_pvt));
273 ast_pthread_mutex_init(&tmp->lock);
274 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
275 c = strchr(tmp->exten, '@');
279 strncpy(tmp->context, c, sizeof(tmp->context) - 1);
281 strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
282 tmp->reqformat = format;
283 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
284 ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->context, tmp->exten);
289 ast_pthread_mutex_lock(&locallock);
292 ast_pthread_mutex_unlock(&locallock);
299 static struct ast_channel *local_new(struct local_pvt *p, int state)
301 struct ast_channel *tmp, *tmp2;
302 tmp = ast_channel_alloc(1);
303 tmp2 = ast_channel_alloc(1);
306 ast_channel_free(tmp);
308 ast_channel_free(tmp2);
312 tmp->nativeformats = p->reqformat;
313 tmp2->nativeformats = p->reqformat;
314 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-1", p->exten, p->context);
315 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-2", p->exten, p->context);
318 ast_setstate(tmp, state);
319 ast_setstate(tmp2, AST_STATE_RING);
320 tmp->writeformat = p->reqformat;;
321 tmp2->writeformat = p->reqformat;
322 tmp->pvt->rawwriteformat = p->reqformat;
323 tmp2->pvt->rawwriteformat = p->reqformat;
324 tmp->readformat = p->reqformat;
325 tmp2->readformat = p->reqformat;
326 tmp->pvt->rawreadformat = p->reqformat;
327 tmp2->pvt->rawreadformat = p->reqformat;
330 tmp->pvt->send_digit = local_digit;
331 tmp2->pvt->send_digit = local_digit;
332 tmp->pvt->call = local_call;
333 tmp2->pvt->call = local_call;
334 tmp->pvt->hangup = local_hangup;
335 tmp2->pvt->hangup = local_hangup;
336 tmp->pvt->answer = local_answer;
337 tmp2->pvt->answer = local_answer;
338 tmp->pvt->read = local_read;
339 tmp2->pvt->read = local_read;
340 tmp->pvt->write = local_write;
341 tmp2->pvt->write = local_write;
342 tmp->pvt->exception = local_read;
343 tmp2->pvt->exception = local_read;
344 tmp->pvt->indicate = local_indicate;
345 tmp2->pvt->indicate = local_indicate;
346 tmp->pvt->fixup = local_fixup;
347 tmp2->pvt->fixup = local_fixup;
350 ast_pthread_mutex_lock(&usecnt_lock);
352 ast_pthread_mutex_unlock(&usecnt_lock);
353 ast_update_use_count();
354 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
355 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
356 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
360 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
365 static struct ast_channel *local_request(char *type, int format, void *data)
368 struct ast_channel *chan = NULL;
369 p = local_alloc(data, format);
371 chan = local_new(p, AST_STATE_DOWN);
375 static int locals_show(int fd, int argc, char **argv)
380 return RESULT_SHOWUSAGE;
381 ast_pthread_mutex_lock(&locallock);
384 ast_pthread_mutex_lock(&p->lock);
385 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
386 ast_pthread_mutex_unlock(&p->lock);
390 ast_cli(fd, "No local channels in use\n");
391 ast_pthread_mutex_unlock(&locallock);
392 return RESULT_SUCCESS;
395 static char show_locals_usage[] =
396 "Usage: show locals\n"
397 " Provides summary information on locals.\n";
399 static struct ast_cli_entry cli_show_locals = {
400 { "show", "locals", NULL }, locals_show,
401 "Show status of local channels", show_locals_usage, NULL };
405 /* Make sure we can register our sip channel type */
406 if (ast_channel_register(type, tdesc, capability, local_request)) {
407 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
410 ast_cli_register(&cli_show_locals);
422 /* First, take us out of the channel loop */
423 ast_cli_unregister(&cli_show_locals);
424 ast_channel_unregister(type);
425 if (!ast_pthread_mutex_lock(&locallock)) {
426 /* Hangup all interfaces if they have an owner */
430 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
434 ast_pthread_mutex_unlock(&locallock);
436 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
445 ast_pthread_mutex_lock(&usecnt_lock);
447 ast_pthread_mutex_unlock(&usecnt_lock);
453 return ASTERISK_GPL_KEY;