bc177f676080aa223fd12cf7fb6a815db0c42034
[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 <string.h>
16 #include "asterisk/lock.h"
17 #include "asterisk/channel.h"
18 #include "asterisk/config.h"
19 #include "asterisk/logger.h"
20 #include "asterisk/module.h"
21 #include "asterisk/pbx.h"
22 #include "asterisk/options.h"
23 #include "asterisk/lock.h"
24 #include "asterisk/sched.h"
25 #include "asterisk/io.h"
26 #include "asterisk/rtp.h"
27 #include "asterisk/acl.h"
28 #include "asterisk/callerid.h"
29 #include "asterisk/file.h"
30 #include "asterisk/cli.h"
31 #include "asterisk/app.h"
32 #include "asterisk/musiconhold.h"
33 #include "asterisk/manager.h"
34 #include <sys/socket.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <fcntl.h>
39 #include <netdb.h>
40 #include <arpa/inet.h>
41 #include <sys/signal.h>
42
43 static const char desc[] = "Local Proxy Channel";
44 static const char type[] = "Local";
45 static const char tdesc[] = "Local Proxy Channel Driver";
46
47 static int usecnt =0;
48 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
49
50 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
51
52 /* Protect the interface list (of sip_pvt's) */
53 AST_MUTEX_DEFINE_STATIC(locallock);
54
55 static struct ast_channel *local_request(const char *type, int format, void *data, int *cause);
56 static int local_digit(struct ast_channel *ast, char digit);
57 static int local_call(struct ast_channel *ast, char *dest, int timeout);
58 static int local_hangup(struct ast_channel *ast);
59 static int local_answer(struct ast_channel *ast);
60 static struct ast_frame *local_read(struct ast_channel *ast);
61 static int local_write(struct ast_channel *ast, struct ast_frame *f);
62 static int local_indicate(struct ast_channel *ast, int condition);
63 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
64 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
65
66 static const struct ast_channel_tech local_tech = {
67         .type = type,
68         .description = tdesc,
69         .capabilities = -1,
70         .requester = local_request,
71         .send_digit = local_digit,
72         .call = local_call,
73         .hangup = local_hangup,
74         .answer = local_answer,
75         .read = local_read,
76         .write = local_write,
77         .exception = local_read,
78         .indicate = local_indicate,
79         .fixup = local_fixup,
80         .send_html = local_sendhtml,
81 };
82
83 static struct local_pvt {
84         ast_mutex_t lock;                               /* Channel private lock */
85         char context[AST_MAX_EXTENSION];        /* Context to call */
86         char exten[AST_MAX_EXTENSION];          /* Extension to call */
87         int reqformat;                                          /* Requested format */
88         int glaredetect;                                        /* Detect glare on hangup */
89         int cancelqueue;                                        /* Cancel queue */
90         int alreadymasqed;                                      /* Already masqueraded */
91         int launchedpbx;                                        /* Did we launch the PBX */
92         int nooptimization;
93         struct ast_channel *owner;                      /* Master Channel */
94         struct ast_channel *chan;                       /* Outbound channel */
95         struct local_pvt *next;                         /* Next entity */
96 } *locals = NULL;
97
98 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, struct ast_channel *us)
99 {
100         struct ast_channel *other;
101 retrylock:              
102         /* Recalculate outbound channel */
103         if (isoutbound) {
104                 other = p->owner;
105         } else {
106                 other = p->chan;
107         }
108         /* Set glare detection */
109         p->glaredetect = 1;
110         if (p->cancelqueue) {
111                 /* We had a glare on the hangup.  Forget all this business,
112                 return and destroy p.  */
113                 ast_mutex_unlock(&p->lock);
114                 ast_mutex_destroy(&p->lock);
115                 free(p);
116                 return -1;
117         }
118         if (!other) {
119                 p->glaredetect = 0;
120                 return 0;
121         }
122         if (ast_mutex_trylock(&other->lock)) {
123                 /* Failed to lock.  Release main lock and try again */
124                 ast_mutex_unlock(&p->lock);
125                 if (us) {
126                         if (ast_mutex_unlock(&us->lock)) {
127                                 ast_log(LOG_WARNING, "%s wasn't locked while sending %d/%d\n",
128                                         us->name, f->frametype, f->subclass);
129                                 us = NULL;
130                         }
131                 }
132                 /* Wait just a bit */
133                 usleep(1);
134                 /* Only we can destroy ourselves, so we can't disappear here */
135                 if (us)
136                         ast_mutex_lock(&us->lock);
137                 ast_mutex_lock(&p->lock);
138                 goto retrylock;
139         }
140         ast_queue_frame(other, f);
141         ast_mutex_unlock(&other->lock);
142         p->glaredetect = 0;
143         return 0;
144 }
145
146 static int local_answer(struct ast_channel *ast)
147 {
148         struct local_pvt *p = ast->tech_pvt;
149         int isoutbound;
150         int res = -1;
151         ast_mutex_lock(&p->lock);
152         isoutbound = IS_OUTBOUND(ast, p);
153         if (isoutbound) {
154                 /* Pass along answer since somebody answered us */
155                 struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
156                 res = local_queue_frame(p, isoutbound, &answer, ast);
157         } else
158                 ast_log(LOG_WARNING, "Huh?  Local is being asked to answer?\n");
159         ast_mutex_unlock(&p->lock);
160         return res;
161 }
162
163 static void check_bridge(struct local_pvt *p, int isoutbound)
164 {
165         if (p->alreadymasqed || p->nooptimization)
166                 return;
167         if (isoutbound && p->chan && p->chan->_bridge /* Not ast_bridged_channel!  Only go one step! */ && p->owner && !p->owner->readq) {
168                 /* Masquerade bridged channel into owner */
169                 /* Lock everything we need, one by one, and give up if
170                    we can't get everything.  Remember, we'll get another
171                    chance in just a little bit */
172                 if (!ast_mutex_trylock(&(p->chan->_bridge)->lock)) {
173                         if (!ast_mutex_trylock(&p->owner->lock)) {
174                                 ast_channel_masquerade(p->owner, p->chan->_bridge);
175                                 p->alreadymasqed = 1;
176                                 ast_mutex_unlock(&p->owner->lock);
177                         }
178                         ast_mutex_unlock(&(p->chan->_bridge)->lock);
179                 }
180         } else if (!isoutbound && p->owner && p->owner->_bridge && p->chan && !p->chan->readq) {
181                 /* Masquerade bridged channel into chan */
182                 if (!ast_mutex_trylock(&(p->owner->_bridge)->lock)) {
183                         if (!ast_mutex_trylock(&p->chan->lock)) {
184                                 ast_channel_masquerade(p->chan, p->owner->_bridge);
185                                 p->alreadymasqed = 1;
186                                 ast_mutex_unlock(&p->chan->lock);
187                         }
188                         ast_mutex_unlock(&(p->owner->_bridge)->lock);
189                 }
190         }
191 }
192
193 static struct ast_frame  *local_read(struct ast_channel *ast)
194 {
195         static struct ast_frame null = { AST_FRAME_NULL, };
196         return &null;
197 }
198
199 static int local_write(struct ast_channel *ast, struct ast_frame *f)
200 {
201         struct local_pvt *p = ast->tech_pvt;
202         int res = -1;
203         int isoutbound;
204
205
206         /* Just queue for delivery to the other side */
207         ast_mutex_lock(&p->lock);
208         isoutbound = IS_OUTBOUND(ast, p);
209         if (f && (f->frametype == AST_FRAME_VOICE)) 
210                 check_bridge(p, isoutbound);
211         if (!p->alreadymasqed)
212                 res = local_queue_frame(p, isoutbound, f, ast);
213         else {
214                 ast_log(LOG_DEBUG, "Not posting to queue since already masked on '%s'\n", ast->name);
215                 res = 0;
216         }
217         ast_mutex_unlock(&p->lock);
218         return res;
219 }
220
221 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
222 {
223         struct local_pvt *p = newchan->tech_pvt;
224         ast_mutex_lock(&p->lock);
225         if ((p->owner != oldchan) && (p->chan != oldchan)) {
226                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
227                 ast_mutex_unlock(&p->lock);
228                 return -1;
229         }
230         if (p->owner == oldchan)
231                 p->owner = newchan;
232         else
233                 p->chan = newchan;      
234         ast_mutex_unlock(&p->lock);
235         return 0;
236 }
237
238 static int local_indicate(struct ast_channel *ast, int condition)
239 {
240         struct local_pvt *p = ast->tech_pvt;
241         int res = -1;
242         struct ast_frame f = { AST_FRAME_CONTROL, };
243         int isoutbound;
244         /* Queue up a frame representing the indication as a control frame */
245         ast_mutex_lock(&p->lock);
246         isoutbound = IS_OUTBOUND(ast, p);
247         f.subclass = condition;
248         res = local_queue_frame(p, isoutbound, &f, ast);
249         ast_mutex_unlock(&p->lock);
250         return res;
251 }
252
253 static int local_digit(struct ast_channel *ast, char digit)
254 {
255         struct local_pvt *p = ast->tech_pvt;
256         int res = -1;
257         struct ast_frame f = { AST_FRAME_DTMF, };
258         int isoutbound;
259         ast_mutex_lock(&p->lock);
260         isoutbound = IS_OUTBOUND(ast, p);
261         f.subclass = digit;
262         res = local_queue_frame(p, isoutbound, &f, ast);
263         ast_mutex_unlock(&p->lock);
264         return res;
265 }
266
267 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
268 {
269         struct local_pvt *p = ast->tech_pvt;
270         int res = -1;
271         struct ast_frame f = { AST_FRAME_HTML, };
272         int isoutbound;
273         ast_mutex_lock(&p->lock);
274         isoutbound = IS_OUTBOUND(ast, p);
275         f.subclass = subclass;
276         f.data = (char *)data;
277         f.datalen = datalen;
278         res = local_queue_frame(p, isoutbound, &f, ast);
279         ast_mutex_unlock(&p->lock);
280         return res;
281 }
282
283 static int local_call(struct ast_channel *ast, char *dest, int timeout)
284 {
285         struct local_pvt *p = ast->tech_pvt;
286         int res;
287         
288         ast_mutex_lock(&p->lock);
289         if (p->owner->cid.cid_num)
290                 p->chan->cid.cid_num = strdup(p->owner->cid.cid_num);
291         else 
292                 p->chan->cid.cid_num = NULL;
293
294         if (p->owner->cid.cid_name)
295                 p->chan->cid.cid_name = strdup(p->owner->cid.cid_name);
296         else 
297                 p->chan->cid.cid_name = NULL;
298
299         if (p->owner->cid.cid_rdnis)
300                 p->chan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
301         else
302                 p->chan->cid.cid_rdnis = NULL;
303
304         if (p->owner->cid.cid_ani)
305                 p->chan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
306         else
307                 p->chan->cid.cid_ani = NULL;
308
309         strncpy(p->chan->language, p->owner->language, sizeof(p->chan->language) - 1);
310         strncpy(p->chan->accountcode, p->owner->accountcode, sizeof(p->chan->accountcode) - 1);
311         p->chan->cdrflags = p->owner->cdrflags;
312         ast_channel_inherit_variables(p->owner, p->chan);
313         p->launchedpbx = 1;
314         /* Start switch on sub channel */
315         res = ast_pbx_start(p->chan);
316         ast_mutex_unlock(&p->lock);
317         return res;
318 }
319
320 /*
321 static void local_destroy(struct local_pvt *p)
322 {
323         struct local_pvt *cur, *prev = NULL;
324         ast_mutex_lock(&locallock);
325         cur = locals;
326         while(cur) {
327                 if (cur == p) {
328                         if (prev)
329                                 prev->next = cur->next;
330                         else
331                                 locals = cur->next;
332                         ast_mutex_destroy(cur);
333                         free(cur);
334                         break;
335                 }
336                 prev = cur;
337                 cur = cur->next;
338         }
339         ast_mutex_unlock(&locallock);
340         if (!cur)
341                 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
342 }
343 */
344
345 static int local_hangup(struct ast_channel *ast)
346 {
347         struct local_pvt *p = ast->tech_pvt;
348         int isoutbound;
349         struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
350         struct local_pvt *cur, *prev=NULL;
351         struct ast_channel *ochan = NULL;
352         int glaredetect;
353         ast_mutex_lock(&p->lock);
354         isoutbound = IS_OUTBOUND(ast, p);
355         if (isoutbound) {
356                 p->chan = NULL;
357                 p->launchedpbx = 0;
358         } else
359                 p->owner = NULL;
360         ast->tech_pvt = NULL;
361         
362         ast_mutex_lock(&usecnt_lock);
363         usecnt--;
364         ast_mutex_unlock(&usecnt_lock);
365         
366         if (!p->owner && !p->chan) {
367                 /* Okay, done with the private part now, too. */
368                 glaredetect = p->glaredetect;
369                 /* If we have a queue holding, don't actually destroy p yet, but
370                    let local_queue do it. */
371                 if (p->glaredetect)
372                         p->cancelqueue = 1;
373                 ast_mutex_unlock(&p->lock);
374                 /* Remove from list */
375                 ast_mutex_lock(&locallock);
376                 cur = locals;
377                 while(cur) {
378                         if (cur == p) {
379                                 if (prev)
380                                         prev->next = cur->next;
381                                 else
382                                         locals = cur->next;
383                                 break;
384                         }
385                         prev = cur;
386                         cur = cur->next;
387                 }
388                 ast_mutex_unlock(&locallock);
389                 /* Grab / release lock just in case */
390                 ast_mutex_lock(&p->lock);
391                 ast_mutex_unlock(&p->lock);
392                 /* And destroy */
393                 if (!glaredetect) {
394                         ast_mutex_destroy(&p->lock);
395                         free(p);
396                 }
397                 return 0;
398         }
399         if (p->chan && !p->launchedpbx)
400                 /* Need to actually hangup since there is no PBX */
401                 ochan = p->chan;
402         else
403                 local_queue_frame(p, isoutbound, &f, NULL);
404         ast_mutex_unlock(&p->lock);
405         if (ochan)
406                 ast_hangup(ochan);
407         return 0;
408 }
409
410 static struct local_pvt *local_alloc(char *data, int format)
411 {
412         struct local_pvt *tmp;
413         char *c;
414         char *opts;
415         tmp = malloc(sizeof(struct local_pvt));
416         if (tmp) {
417                 memset(tmp, 0, sizeof(struct local_pvt));
418                 ast_mutex_init(&tmp->lock);
419                 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
420                 opts = strchr(tmp->exten, '/');
421                 if (opts) {
422                         *opts='\0';
423                         opts++;
424                         if (strchr(opts, 'n'))
425                                 tmp->nooptimization = 1;
426                 }
427                 c = strchr(tmp->exten, '@');
428                 if (c) {
429                         *c = '\0';
430                         c++;
431                         strncpy(tmp->context, c, sizeof(tmp->context) - 1);
432                 } else
433                         strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
434                 tmp->reqformat = format;
435                 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
436                         ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
437                         ast_mutex_destroy(&tmp->lock);
438                         free(tmp);
439                         tmp = NULL;
440                 } else {
441                         /* Add to list */
442                         ast_mutex_lock(&locallock);
443                         tmp->next = locals;
444                         locals = tmp;
445                         ast_mutex_unlock(&locallock);
446                 }
447                 
448         }
449         return tmp;
450 }
451
452 static struct ast_channel *local_new(struct local_pvt *p, int state)
453 {
454         struct ast_channel *tmp, *tmp2;
455         int randnum = rand() & 0xffff;
456         tmp = ast_channel_alloc(1);
457         tmp2 = ast_channel_alloc(1);
458         if (!tmp || !tmp2) {
459                 if (tmp)
460                         ast_channel_free(tmp);
461                 if (tmp2)
462                         ast_channel_free(tmp2);
463                 tmp = NULL;
464         }
465         if (tmp) {
466                 tmp2->tech = tmp->tech = &local_tech;
467                 tmp->nativeformats = p->reqformat;
468                 tmp2->nativeformats = p->reqformat;
469                 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-%04x,1", p->exten, p->context, randnum);
470                 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-%04x,2", p->exten, p->context, randnum);
471                 tmp->type = type;
472                 tmp2->type = type;
473                 ast_setstate(tmp, state);
474                 ast_setstate(tmp2, AST_STATE_RING);
475                 tmp->writeformat = p->reqformat;;
476                 tmp2->writeformat = p->reqformat;
477                 tmp->rawwriteformat = p->reqformat;
478                 tmp2->rawwriteformat = p->reqformat;
479                 tmp->readformat = p->reqformat;
480                 tmp2->readformat = p->reqformat;
481                 tmp->rawreadformat = p->reqformat;
482                 tmp2->rawreadformat = p->reqformat;
483                 tmp->tech_pvt = p;
484                 tmp2->tech_pvt = p;
485                 p->owner = tmp;
486                 p->chan = tmp2;
487                 ast_mutex_lock(&usecnt_lock);
488                 usecnt++;
489                 usecnt++;
490                 ast_mutex_unlock(&usecnt_lock);
491                 ast_update_use_count();
492                 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
493                 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
494                 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
495                 tmp->priority = 1;
496                 tmp2->priority = 1;
497         } else
498                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
499         return tmp;
500 }
501
502
503 static struct ast_channel *local_request(const char *type, int format, void *data, int *cause)
504 {
505         struct local_pvt *p;
506         struct ast_channel *chan = NULL;
507         p = local_alloc(data, format);
508         if (p)
509                 chan = local_new(p, AST_STATE_DOWN);
510         return chan;
511 }
512
513 static int locals_show(int fd, int argc, char **argv)
514 {
515         struct local_pvt *p;
516
517         if (argc != 3)
518                 return RESULT_SHOWUSAGE;
519         ast_mutex_lock(&locallock);
520         p = locals;
521         while(p) {
522                 ast_mutex_lock(&p->lock);
523                 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
524                 ast_mutex_unlock(&p->lock);
525                 p = p->next;
526         }
527         if (!locals)
528                 ast_cli(fd, "No local channels in use\n");
529         ast_mutex_unlock(&locallock);
530         return RESULT_SUCCESS;
531 }
532
533 static char show_locals_usage[] = 
534 "Usage: local show channels\n"
535 "       Provides summary information on local channels.\n";
536
537 static struct ast_cli_entry cli_show_locals = {
538         { "local", "show", "channels", NULL }, locals_show, 
539         "Show status of local channels", show_locals_usage, NULL };
540
541 int load_module()
542 {
543         /* Make sure we can register our channel type */
544         if (ast_channel_register(&local_tech)) {
545                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
546                 return -1;
547         }
548         ast_cli_register(&cli_show_locals);
549         return 0;
550 }
551
552 int reload()
553 {
554         return 0;
555 }
556
557 int unload_module()
558 {
559         struct local_pvt *p;
560         /* First, take us out of the channel loop */
561         ast_cli_unregister(&cli_show_locals);
562         ast_channel_unregister(&local_tech);
563         if (!ast_mutex_lock(&locallock)) {
564                 /* Hangup all interfaces if they have an owner */
565                 p = locals;
566                 while(p) {
567                         if (p->owner)
568                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
569                         p = p->next;
570                 }
571                 locals = NULL;
572                 ast_mutex_unlock(&locallock);
573         } else {
574                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
575                 return -1;
576         }               
577         return 0;
578 }
579
580 int usecount()
581 {
582         int res;
583         ast_mutex_lock(&usecnt_lock);
584         res = usecnt;
585         ast_mutex_unlock(&usecnt_lock);
586         return res;
587 }
588
589 char *key()
590 {
591         return ASTERISK_GPL_KEY;
592 }
593
594 char *description()
595 {
596         return (char *) desc;
597 }
598