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