Complete local channel implementation
[asterisk/asterisk.git] / channels / chan_local.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Local Proxy Channel
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <pthread.h>
16 #include <string.h>
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>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
41 #include <netdb.h>
42 #include <arpa/inet.h>
43 #include <sys/signal.h>
44
45 static char *desc = "Local Proxy Channel";
46 static char *type = "Local";
47 static char *tdesc = "Local Proxy Channel Driver";
48
49 static int capability = -1;
50
51 static int usecnt =0;
52 static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
53
54 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
55
56 /* Protect the interface list (of sip_pvt's) */
57 static pthread_mutex_t locallock = AST_MUTEX_INITIALIZER;
58
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 */
68 } *locals = NULL;
69
70 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f)
71 {
72         struct ast_channel *other;
73         if (isoutbound)
74                 other = p->owner;
75         else
76                 other = p->chan;
77         if (!other)
78                 return 0;
79 retrylock:              
80         if (pthread_mutex_trylock(&other->lock)) {
81                 /* Failed to lock.  Release main lock and try again */
82                 ast_pthread_mutex_unlock(&p->lock);
83                 /* Wait just a bit */
84                 usleep(1);
85                 ast_pthread_mutex_lock(&p->lock);
86                 goto retrylock;
87         }
88         if (f->frametype != AST_FRAME_VOICE)
89                 ast_verbose("Queueing frmae %d/%d on %s\n", f->frametype, f->subclass, other->name);
90         ast_queue_frame(other, f, 0);
91         ast_pthread_mutex_unlock(&other->lock);
92         return 0;
93 }
94
95 static int local_answer(struct ast_channel *ast)
96 {
97         struct local_pvt *p = ast->pvt->pvt;
98         int isoutbound = IS_OUTBOUND(ast, p);
99         int res = -1;
100         ast_pthread_mutex_lock(&p->lock);
101         if (isoutbound) {
102                 /* Pass along answer since somebody answered us */
103                 struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
104                 res = local_queue_frame(p, isoutbound, &answer);
105         } else
106                 ast_log(LOG_WARNING, "Huh?  Local is being asked to answer?\n");
107         ast_pthread_mutex_unlock(&p->lock);
108         return res;
109 }
110
111 static void check_bridge(struct local_pvt *p, int isoutbound)
112 {
113         if (p->alreadymasqed)
114                 return;
115         if (isoutbound && p->chan && p->chan->bridge && p->owner) {
116                 /* Masquerade bridged channel into owner */
117                 ast_channel_masquerade(p->owner, p->chan->bridge);
118                 p->alreadymasqed = 1;
119         } else if (!isoutbound && p->owner && p->owner->bridge && p->chan) {
120                 /* Masquerade bridged channel into chan */
121                 ast_channel_masquerade(p->chan, p->owner->bridge);
122                 p->alreadymasqed = 1;
123         }
124 }
125
126 static struct ast_frame  *local_read(struct ast_channel *ast)
127 {
128         static struct ast_frame null = { AST_FRAME_NULL, };
129         return &null;
130 }
131
132 static int local_write(struct ast_channel *ast, struct ast_frame *f)
133 {
134         struct local_pvt *p = ast->pvt->pvt;
135         int res = -1;
136         int isoutbound = IS_OUTBOUND(ast, p);
137
138
139         /* Just queue for delivery to the other side */
140         ast_pthread_mutex_lock(&p->lock);
141         res = local_queue_frame(p, isoutbound, f);
142         check_bridge(p, isoutbound);
143         ast_pthread_mutex_unlock(&p->lock);
144         return res;
145 }
146
147 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
148 {
149         struct local_pvt *p = newchan->pvt->pvt;
150         ast_pthread_mutex_lock(&p->lock);
151         if ((p->owner != oldchan) && (p->chan != oldchan)) {
152                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
153                 ast_pthread_mutex_unlock(&p->lock);
154                 return -1;
155         }
156         if (p->owner == oldchan)
157                 p->owner = newchan;
158         else
159                 p->chan = newchan;      
160         ast_pthread_mutex_unlock(&p->lock);
161         return 0;
162 }
163
164 static int local_indicate(struct ast_channel *ast, int condition)
165 {
166         struct local_pvt *p = ast->pvt->pvt;
167         int res = -1;
168         struct ast_frame f = { AST_FRAME_CONTROL, };
169         int isoutbound = IS_OUTBOUND(ast, p);
170         /* Queue up a frame representing the indication as a control frame */
171         ast_pthread_mutex_lock(&p->lock);
172         f.subclass = condition;
173         res = local_queue_frame(p, isoutbound, &f);
174         ast_pthread_mutex_unlock(&p->lock);
175         return res;
176 }
177
178 static int local_digit(struct ast_channel *ast, char digit)
179 {
180         struct local_pvt *p = ast->pvt->pvt;
181         int res = -1;
182         struct ast_frame f = { AST_FRAME_DTMF, };
183         int isoutbound = IS_OUTBOUND(ast, p);
184         ast_pthread_mutex_lock(&p->lock);
185         f.subclass = digit;
186         res = local_queue_frame(p, isoutbound, &f);
187         ast_pthread_mutex_unlock(&p->lock);
188         return res;
189 }
190
191 static int local_call(struct ast_channel *ast, char *dest, int timeout)
192 {
193         struct local_pvt *p = ast->pvt->pvt;
194         /* Start switch on sub channel */
195         return ast_pbx_start(p->chan);
196 }
197
198 static void local_destroy(struct local_pvt *p)
199 {
200         struct local_pvt *cur, *prev = NULL;
201         ast_pthread_mutex_lock(&locallock);
202         cur = locals;
203         while(cur) {
204                 if (cur == p) {
205                         if (prev)
206                                 prev->next = cur->next;
207                         else
208                                 locals = cur->next;
209                         free(cur);
210                         break;
211                 }
212                 prev = cur;
213                 cur = cur->next;
214         }
215         ast_pthread_mutex_unlock(&locallock);
216         if (!cur)
217                 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
218 }
219
220 static int local_hangup(struct ast_channel *ast)
221 {
222         struct local_pvt *p = ast->pvt->pvt;
223         int isoutbound = IS_OUTBOUND(ast, p);
224         struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
225         ast_pthread_mutex_lock(&p->lock);
226         if (isoutbound)
227                 p->chan = NULL;
228         else
229                 p->owner = NULL;
230         ast->pvt->pvt = NULL;
231         if (!p->owner && !p->chan) {
232                 /* Okay, done with the private part now, too. */
233                 ast_pthread_mutex_unlock(&p->lock);
234                 free(p);
235                 return 0;
236         }
237         local_queue_frame(p, isoutbound, &f);
238         ast_pthread_mutex_unlock(&p->lock);
239         return 0;
240 }
241
242 static struct local_pvt *local_alloc(char *data, int format)
243 {
244         struct local_pvt *tmp;
245         char *c;
246         tmp = malloc(sizeof(struct local_pvt));
247         if (tmp) {
248                 memset(tmp, 0, sizeof(struct local_pvt));
249                 ast_pthread_mutex_init(&tmp->lock);
250                 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
251                 c = strchr(tmp->exten, '@');
252                 if (c) {
253                         *c = '\0';
254                         c++;
255                         strncpy(tmp->context, c, sizeof(tmp->context) - 1);
256                 } else
257                         strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
258                 tmp->reqformat = format;
259                 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
260                         ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->context, tmp->exten);
261                         free(tmp);
262                         tmp = NULL;
263                 } else {
264                         /* Add to list */
265                         ast_pthread_mutex_lock(&locallock);
266                         tmp->next = locals;
267                         locals = tmp;
268                         ast_pthread_mutex_unlock(&locallock);
269                 }
270                 
271         }
272         return tmp;
273 }
274
275 static struct ast_channel *local_new(struct local_pvt *p, int state)
276 {
277         struct ast_channel *tmp, *tmp2;
278         tmp = ast_channel_alloc(1);
279         tmp2 = ast_channel_alloc(1);
280         if (!tmp || !tmp2) {
281                 if (tmp)
282                         ast_channel_free(tmp);
283                 if (tmp2)
284                         ast_channel_free(tmp2);
285                 tmp = NULL;
286         }
287         if (tmp) {
288                 tmp->nativeformats = p->reqformat;
289                 tmp2->nativeformats = p->reqformat;
290                 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-1", p->exten, p->context);
291                 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-2", p->exten, p->context);
292                 tmp->type = type;
293                 tmp2->type = type;
294                 ast_setstate(tmp, state);
295                 ast_setstate(tmp2, AST_STATE_RING);
296                 tmp->writeformat = p->reqformat;;
297                 tmp2->writeformat = p->reqformat;
298                 tmp->pvt->rawwriteformat = p->reqformat;
299                 tmp2->pvt->rawwriteformat = p->reqformat;
300                 tmp->readformat = p->reqformat;
301                 tmp2->readformat = p->reqformat;
302                 tmp->pvt->rawreadformat = p->reqformat;
303                 tmp2->pvt->rawreadformat = p->reqformat;
304                 tmp->pvt->pvt = p;
305                 tmp2->pvt->pvt = p;
306                 tmp->pvt->send_digit = local_digit;
307                 tmp2->pvt->send_digit = local_digit;
308                 tmp->pvt->call = local_call;
309                 tmp2->pvt->call = local_call;
310                 tmp->pvt->hangup = local_hangup;
311                 tmp2->pvt->hangup = local_hangup;
312                 tmp->pvt->answer = local_answer;
313                 tmp2->pvt->answer = local_answer;
314                 tmp->pvt->read = local_read;
315                 tmp2->pvt->read = local_read;
316                 tmp->pvt->write = local_write;
317                 tmp2->pvt->write = local_write;
318                 tmp->pvt->exception = local_read;
319                 tmp2->pvt->exception = local_read;
320                 tmp->pvt->indicate = local_indicate;
321                 tmp2->pvt->indicate = local_indicate;
322                 tmp->pvt->fixup = local_fixup;
323                 tmp2->pvt->fixup = local_fixup;
324                 p->owner = tmp;
325                 p->chan = tmp2;
326                 ast_pthread_mutex_lock(&usecnt_lock);
327                 usecnt++;
328                 ast_pthread_mutex_unlock(&usecnt_lock);
329                 ast_update_use_count();
330                 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
331                 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
332                 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
333                 tmp->priority = 1;
334                 tmp2->priority = 1;
335         } else
336                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
337         return tmp;
338 }
339
340
341 static struct ast_channel *local_request(char *type, int format, void *data)
342 {
343         struct local_pvt *p;
344         struct ast_channel *chan = NULL;
345         p = local_alloc(data, format);
346         if (p)
347                 chan = local_new(p, AST_STATE_DOWN);
348         return chan;
349 }
350
351 static int locals_show(int fd, int argc, char **argv)
352 {
353         struct local_pvt *p;
354
355         if (argc != 2)
356                 return RESULT_SHOWUSAGE;
357         ast_pthread_mutex_lock(&locallock);
358         p = locals;
359         while(p) {
360                 ast_pthread_mutex_lock(&p->lock);
361                 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
362                 ast_pthread_mutex_unlock(&p->lock);
363                 p = p->next;
364         }
365         if (!locals)
366                 ast_cli(fd, "No local channels in use\n");
367         ast_pthread_mutex_unlock(&locallock);
368         return RESULT_SUCCESS;
369 }
370
371 static char show_locals_usage[] = 
372 "Usage: show locals\n"
373 "       Provides summary information on locals.\n";
374
375 static struct ast_cli_entry cli_show_locals = {
376         { "show", "locals", NULL }, locals_show, 
377         "Show status of local channels", show_locals_usage, NULL };
378
379 int load_module()
380 {
381         /* Make sure we can register our sip channel type */
382         if (ast_channel_register(type, tdesc, capability, local_request)) {
383                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
384                 return -1;
385         }
386         ast_cli_register(&cli_show_locals);
387         return 0;
388 }
389
390 int reload()
391 {
392         return 0;
393 }
394
395 int unload_module()
396 {
397         struct local_pvt *p;
398         /* First, take us out of the channel loop */
399         ast_cli_unregister(&cli_show_locals);
400         ast_channel_unregister(type);
401         if (!ast_pthread_mutex_lock(&locallock)) {
402                 /* Hangup all interfaces if they have an owner */
403                 p = locals;
404                 while(p) {
405                         if (p->owner)
406                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
407                         p = p->next;
408                 }
409                 locals = NULL;
410                 ast_pthread_mutex_unlock(&locallock);
411         } else {
412                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
413                 return -1;
414         }               
415         return 0;
416 }
417
418 int usecount()
419 {
420         int res;
421         ast_pthread_mutex_lock(&usecnt_lock);
422         res = usecnt;
423         ast_pthread_mutex_unlock(&usecnt_lock);
424         return res;
425 }
426
427 char *key()
428 {
429         return ASTERISK_GPL_KEY;
430 }
431
432 char *description()
433 {
434         return desc;
435 }
436