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 struct ast_channel *owner; /* Master Channel */
65 struct ast_channel *chan; /* Outbound channel */
66 struct local_pvt *next; /* Next entity */
69 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f)
71 struct ast_channel *other;
79 if (pthread_mutex_trylock(&other->lock)) {
80 /* Failed to lock. Release main lock and try again */
81 ast_pthread_mutex_unlock(&p->lock);
84 ast_pthread_mutex_lock(&p->lock);
87 ast_queue_frame(other, f, 0);
88 ast_pthread_mutex_unlock(&other->lock);
92 static int local_answer(struct ast_channel *ast)
94 struct local_pvt *p = ast->pvt->pvt;
95 int isoutbound = IS_OUTBOUND(ast, p);
97 ast_pthread_mutex_lock(&p->lock);
99 /* Pass along answer since somebody answered us */
100 struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
101 local_queue_frame(p, isoutbound, &answer);
103 ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
104 ast_pthread_mutex_unlock(&p->lock);
108 static struct ast_frame *local_read(struct ast_channel *ast)
110 static struct ast_frame null = { AST_FRAME_NULL, };
114 static int local_write(struct ast_channel *ast, struct ast_frame *f)
116 struct local_pvt *p = ast->pvt->pvt;
118 int isoutbound = IS_OUTBOUND(ast, p);
120 /* Just queue for delivery to the other side */
121 ast_pthread_mutex_lock(&p->lock);
122 res = local_queue_frame(p, isoutbound, f);
123 ast_pthread_mutex_unlock(&p->lock);
127 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
129 struct local_pvt *p = newchan->pvt->pvt;
130 ast_pthread_mutex_lock(&p->lock);
131 if ((p->owner != oldchan) && (p->chan != oldchan)) {
132 ast_log(LOG_WARNING, "old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
133 ast_pthread_mutex_unlock(&p->lock);
136 if (p->owner == oldchan)
140 ast_pthread_mutex_unlock(&p->lock);
144 static int local_indicate(struct ast_channel *ast, int condition)
146 struct local_pvt *p = ast->pvt->pvt;
148 struct ast_frame f = { AST_FRAME_CONTROL, };
149 int isoutbound = IS_OUTBOUND(ast, p);
150 /* Queue up a frame representing the indication as a control frame */
151 ast_pthread_mutex_lock(&p->lock);
152 f.subclass = condition;
153 res = local_queue_frame(p, isoutbound, &f);
154 ast_pthread_mutex_unlock(&p->lock);
158 static int local_digit(struct ast_channel *ast, char digit)
160 struct local_pvt *p = ast->pvt->pvt;
162 struct ast_frame f = { AST_FRAME_DTMF, };
163 int isoutbound = IS_OUTBOUND(ast, p);
164 ast_pthread_mutex_lock(&p->lock);
166 res = local_queue_frame(p, isoutbound, &f);
167 ast_pthread_mutex_unlock(&p->lock);
171 static int local_call(struct ast_channel *ast, char *dest, int timeout)
173 struct local_pvt *p = ast->pvt->pvt;
174 /* Start switch on sub channel */
175 return ast_pbx_start(p->chan);
178 static void local_destroy(struct local_pvt *p)
180 struct local_pvt *cur, *prev = NULL;
181 ast_pthread_mutex_lock(&locallock);
186 prev->next = cur->next;
195 ast_pthread_mutex_unlock(&locallock);
197 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
200 static int local_hangup(struct ast_channel *ast)
202 struct local_pvt *p = ast->pvt->pvt;
203 int isoutbound = IS_OUTBOUND(ast, p);
204 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
205 ast_pthread_mutex_lock(&p->lock);
210 ast->pvt->pvt = NULL;
211 if (!p->owner && !p->chan) {
212 /* Okay, done with the private part now, too. */
213 ast_pthread_mutex_unlock(&p->lock);
217 local_queue_frame(p, isoutbound, &f);
218 ast_pthread_mutex_unlock(&p->lock);
222 static struct local_pvt *local_alloc(char *data, int format)
224 struct local_pvt *tmp;
226 tmp = malloc(sizeof(struct local_pvt));
228 memset(tmp, 0, sizeof(struct local_pvt));
229 ast_pthread_mutex_init(&tmp->lock);
230 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
231 c = strchr(tmp->exten, '@');
235 strncpy(tmp->context, data, sizeof(tmp->context) - 1);
237 tmp->reqformat = format;
239 ast_pthread_mutex_lock(&locallock);
242 ast_pthread_mutex_unlock(&locallock);
248 static struct ast_channel *local_new(struct local_pvt *p, int state)
250 struct ast_channel *tmp, *tmp2;
251 tmp = ast_channel_alloc(1);
252 tmp2 = ast_channel_alloc(1);
255 ast_channel_free(tmp);
257 ast_channel_free(tmp2);
261 tmp->nativeformats = p->reqformat;
262 tmp->nativeformats = p->reqformat;
263 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-1", p->exten, p->context);
264 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-2", p->exten, p->context);
267 ast_setstate(tmp, state);
268 ast_setstate(tmp2, AST_STATE_RING);
269 tmp->writeformat = p->reqformat;;
270 tmp2->writeformat = p->reqformat;
271 tmp->pvt->rawwriteformat = p->reqformat;
272 tmp2->pvt->rawwriteformat = p->reqformat;
273 tmp->readformat = p->reqformat;
274 tmp2->readformat = p->reqformat;
275 tmp->pvt->rawreadformat = p->reqformat;
276 tmp2->pvt->rawreadformat = p->reqformat;
279 tmp->pvt->send_digit = local_digit;
280 tmp2->pvt->send_digit = local_digit;
281 tmp->pvt->call = local_call;
282 tmp2->pvt->call = local_call;
283 tmp->pvt->hangup = local_hangup;
284 tmp2->pvt->hangup = local_hangup;
285 tmp->pvt->answer = local_answer;
286 tmp2->pvt->answer = local_answer;
287 tmp->pvt->read = local_read;
288 tmp2->pvt->read = local_read;
289 tmp->pvt->write = local_write;
290 tmp2->pvt->write = local_write;
291 tmp->pvt->exception = local_read;
292 tmp2->pvt->exception = local_read;
293 tmp->pvt->indicate = local_indicate;
294 tmp2->pvt->indicate = local_indicate;
295 tmp->pvt->fixup = local_fixup;
296 tmp2->pvt->fixup = local_fixup;
299 ast_pthread_mutex_lock(&usecnt_lock);
301 ast_pthread_mutex_unlock(&usecnt_lock);
302 ast_update_use_count();
303 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
304 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
305 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
309 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
314 static struct ast_channel *local_request(char *type, int format, void *data)
317 struct ast_channel *chan = NULL;
318 p = local_alloc(data, format);
319 chan = local_new(p, AST_STATE_DOWN);
323 static int locals_show(int fd, int argc, char **argv)
328 return RESULT_SHOWUSAGE;
329 ast_pthread_mutex_lock(&locallock);
332 ast_pthread_mutex_lock(&p->lock);
333 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
334 ast_pthread_mutex_unlock(&p->lock);
338 ast_cli(fd, "No local channels in use\n");
339 ast_pthread_mutex_unlock(&locallock);
340 return RESULT_SUCCESS;
343 static char show_locals_usage[] =
344 "Usage: show locals\n"
345 " Provides summary information on locals.\n";
347 static struct ast_cli_entry cli_show_locals = {
348 { "show", "locals", NULL }, locals_show,
349 "Show status of local channels", show_locals_usage, NULL };
353 /* Make sure we can register our sip channel type */
354 if (ast_channel_register(type, tdesc, capability, local_request)) {
355 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
358 ast_cli_register(&cli_show_locals);
370 /* First, take us out of the channel loop */
371 ast_cli_unregister(&cli_show_locals);
372 ast_channel_unregister(type);
373 if (!ast_pthread_mutex_lock(&locallock)) {
374 /* Hangup all interfaces if they have an owner */
378 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
382 ast_pthread_mutex_unlock(&locallock);
384 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
393 ast_pthread_mutex_lock(&usecnt_lock);
395 ast_pthread_mutex_unlock(&usecnt_lock);
401 return ASTERISK_GPL_KEY;