e6ee96450d47d19efddbf28491292566a5e0182a
[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) {
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) {
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_call(struct ast_channel *ast, char *dest, int timeout)
237 {
238         struct local_pvt *p = ast->pvt->pvt;
239         int res;
240         
241         ast_mutex_lock(&p->lock);
242         if (p->owner->cid.cid_num)
243                 p->chan->cid.cid_num = strdup(p->owner->cid.cid_num);
244         else 
245                 p->chan->cid.cid_num = NULL;
246
247         if (p->owner->cid.cid_name)
248                 p->chan->cid.cid_name = strdup(p->owner->cid.cid_name);
249         else 
250                 p->chan->cid.cid_name = NULL;
251
252         if (p->owner->cid.cid_rdnis)
253                 p->chan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
254         else
255                 p->chan->cid.cid_rdnis = NULL;
256
257         if (p->owner->cid.cid_ani)
258                 p->chan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
259         else
260                 p->chan->cid.cid_ani = NULL;
261
262         strncpy(p->chan->language, p->owner->language, sizeof(p->chan->language) - 1);
263         strncpy(p->chan->accountcode, p->owner->accountcode, sizeof(p->chan->accountcode) - 1);
264         p->chan->cdrflags = p->owner->cdrflags;
265         
266         p->launchedpbx = 1;
267         /* Start switch on sub channel */
268         res = ast_pbx_start(p->chan);
269         ast_mutex_unlock(&p->lock);
270         return res;
271 }
272
273 /*
274 static void local_destroy(struct local_pvt *p)
275 {
276         struct local_pvt *cur, *prev = NULL;
277         ast_mutex_lock(&locallock);
278         cur = locals;
279         while(cur) {
280                 if (cur == p) {
281                         if (prev)
282                                 prev->next = cur->next;
283                         else
284                                 locals = cur->next;
285                         ast_mutex_destroy(cur);
286                         free(cur);
287                         break;
288                 }
289                 prev = cur;
290                 cur = cur->next;
291         }
292         ast_mutex_unlock(&locallock);
293         if (!cur)
294                 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
295 }
296 */
297
298 static int local_hangup(struct ast_channel *ast)
299 {
300         struct local_pvt *p = ast->pvt->pvt;
301         int isoutbound;
302         struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
303         struct local_pvt *cur, *prev=NULL;
304         struct ast_channel *ochan = NULL;
305         int glaredetect;
306         ast_mutex_lock(&p->lock);
307         isoutbound = IS_OUTBOUND(ast, p);
308         if (isoutbound) {
309                 p->chan = NULL;
310                 p->launchedpbx = 0;
311         } else
312                 p->owner = NULL;
313         ast->pvt->pvt = NULL;
314         
315         ast_mutex_lock(&usecnt_lock);
316         usecnt--;
317         ast_mutex_unlock(&usecnt_lock);
318         
319         if (!p->owner && !p->chan) {
320                 /* Okay, done with the private part now, too. */
321                 glaredetect = p->glaredetect;
322                 /* If we have a queue holding, don't actually destroy p yet, but
323                    let local_queue do it. */
324                 if (p->glaredetect)
325                         p->cancelqueue = 1;
326                 ast_mutex_unlock(&p->lock);
327                 /* Remove from list */
328                 ast_mutex_lock(&locallock);
329                 cur = locals;
330                 while(cur) {
331                         if (cur == p) {
332                                 if (prev)
333                                         prev->next = cur->next;
334                                 else
335                                         locals = cur->next;
336                                 break;
337                         }
338                         prev = cur;
339                         cur = cur->next;
340                 }
341                 ast_mutex_unlock(&locallock);
342                 /* Grab / release lock just in case */
343                 ast_mutex_lock(&p->lock);
344                 ast_mutex_unlock(&p->lock);
345                 /* And destroy */
346                 if (!glaredetect) {
347                         ast_mutex_destroy(&p->lock);
348                         free(p);
349                 }
350                 return 0;
351         }
352         if (p->chan && !p->launchedpbx)
353                 /* Need to actually hangup since there is no PBX */
354                 ochan = p->chan;
355         else
356                 local_queue_frame(p, isoutbound, &f, NULL);
357         ast_mutex_unlock(&p->lock);
358         if (ochan)
359                 ast_hangup(ochan);
360         return 0;
361 }
362
363 static struct local_pvt *local_alloc(char *data, int format)
364 {
365         struct local_pvt *tmp;
366         char *c;
367         char *opts;
368         tmp = malloc(sizeof(struct local_pvt));
369         if (tmp) {
370                 memset(tmp, 0, sizeof(struct local_pvt));
371                 ast_mutex_init(&tmp->lock);
372                 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
373                 opts = strchr(tmp->exten, '/');
374                 if (opts) {
375                         *opts='\0';
376                         opts++;
377                         if (strchr(opts, 'n'))
378                                 tmp->nooptimization = 1;
379                 }
380                 c = strchr(tmp->exten, '@');
381                 if (c) {
382                         *c = '\0';
383                         c++;
384                         strncpy(tmp->context, c, sizeof(tmp->context) - 1);
385                 } else
386                         strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
387                 tmp->reqformat = format;
388                 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
389                         ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
390                         ast_mutex_destroy(&tmp->lock);
391                         free(tmp);
392                         tmp = NULL;
393                 } else {
394                         /* Add to list */
395                         ast_mutex_lock(&locallock);
396                         tmp->next = locals;
397                         locals = tmp;
398                         ast_mutex_unlock(&locallock);
399                 }
400                 
401         }
402         return tmp;
403 }
404
405 static struct ast_channel *local_new(struct local_pvt *p, int state)
406 {
407         struct ast_channel *tmp, *tmp2;
408         int randnum = rand() & 0xffff;
409         tmp = ast_channel_alloc(1);
410         tmp2 = ast_channel_alloc(1);
411         if (!tmp || !tmp2) {
412                 if (tmp)
413                         ast_channel_free(tmp);
414                 if (tmp2)
415                         ast_channel_free(tmp2);
416                 tmp = NULL;
417         }
418         if (tmp) {
419                 tmp->nativeformats = p->reqformat;
420                 tmp2->nativeformats = p->reqformat;
421                 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-%04x,1", p->exten, p->context, randnum);
422                 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-%04x,2", p->exten, p->context, randnum);
423                 tmp->type = type;
424                 tmp2->type = type;
425                 ast_setstate(tmp, state);
426                 ast_setstate(tmp2, AST_STATE_RING);
427                 tmp->writeformat = p->reqformat;;
428                 tmp2->writeformat = p->reqformat;
429                 tmp->pvt->rawwriteformat = p->reqformat;
430                 tmp2->pvt->rawwriteformat = p->reqformat;
431                 tmp->readformat = p->reqformat;
432                 tmp2->readformat = p->reqformat;
433                 tmp->pvt->rawreadformat = p->reqformat;
434                 tmp2->pvt->rawreadformat = p->reqformat;
435                 tmp->pvt->pvt = p;
436                 tmp2->pvt->pvt = p;
437                 tmp->pvt->send_digit = local_digit;
438                 tmp2->pvt->send_digit = local_digit;
439                 tmp->pvt->call = local_call;
440                 tmp2->pvt->call = local_call;
441                 tmp->pvt->hangup = local_hangup;
442                 tmp2->pvt->hangup = local_hangup;
443                 tmp->pvt->answer = local_answer;
444                 tmp2->pvt->answer = local_answer;
445                 tmp->pvt->read = local_read;
446                 tmp2->pvt->read = local_read;
447                 tmp->pvt->write = local_write;
448                 tmp2->pvt->write = local_write;
449                 tmp->pvt->exception = local_read;
450                 tmp2->pvt->exception = local_read;
451                 tmp->pvt->indicate = local_indicate;
452                 tmp2->pvt->indicate = local_indicate;
453                 tmp->pvt->fixup = local_fixup;
454                 tmp2->pvt->fixup = local_fixup;
455                 p->owner = tmp;
456                 p->chan = tmp2;
457                 ast_mutex_lock(&usecnt_lock);
458                 usecnt++;
459                 usecnt++;
460                 ast_mutex_unlock(&usecnt_lock);
461                 ast_update_use_count();
462                 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
463                 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
464                 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
465                 tmp->priority = 1;
466                 tmp2->priority = 1;
467         } else
468                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
469         return tmp;
470 }
471
472
473 static struct ast_channel *local_request(const char *type, int format, void *data, int *cause)
474 {
475         struct local_pvt *p;
476         struct ast_channel *chan = NULL;
477         p = local_alloc(data, format);
478         if (p)
479                 chan = local_new(p, AST_STATE_DOWN);
480         return chan;
481 }
482
483 static int locals_show(int fd, int argc, char **argv)
484 {
485         struct local_pvt *p;
486
487         if (argc != 3)
488                 return RESULT_SHOWUSAGE;
489         ast_mutex_lock(&locallock);
490         p = locals;
491         while(p) {
492                 ast_mutex_lock(&p->lock);
493                 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
494                 ast_mutex_unlock(&p->lock);
495                 p = p->next;
496         }
497         if (!locals)
498                 ast_cli(fd, "No local channels in use\n");
499         ast_mutex_unlock(&locallock);
500         return RESULT_SUCCESS;
501 }
502
503 static char show_locals_usage[] = 
504 "Usage: local show channels\n"
505 "       Provides summary information on local channels.\n";
506
507 static struct ast_cli_entry cli_show_locals = {
508         { "local", "show", "channels", NULL }, locals_show, 
509         "Show status of local channels", show_locals_usage, NULL };
510
511 int load_module()
512 {
513         /* Make sure we can register our sip channel type */
514         if (ast_channel_register(type, tdesc, capability, local_request)) {
515                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
516                 return -1;
517         }
518         ast_cli_register(&cli_show_locals);
519         return 0;
520 }
521
522 int reload()
523 {
524         return 0;
525 }
526
527 int unload_module()
528 {
529         struct local_pvt *p;
530         /* First, take us out of the channel loop */
531         ast_cli_unregister(&cli_show_locals);
532         ast_channel_unregister(type);
533         if (!ast_mutex_lock(&locallock)) {
534                 /* Hangup all interfaces if they have an owner */
535                 p = locals;
536                 while(p) {
537                         if (p->owner)
538                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
539                         p = p->next;
540                 }
541                 locals = NULL;
542                 ast_mutex_unlock(&locallock);
543         } else {
544                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
545                 return -1;
546         }               
547         return 0;
548 }
549
550 int usecount()
551 {
552         int res;
553         ast_mutex_lock(&usecnt_lock);
554         res = usecnt;
555         ast_mutex_unlock(&usecnt_lock);
556         return res;
557 }
558
559 char *key()
560 {
561         return ASTERISK_GPL_KEY;
562 }
563
564 char *description()
565 {
566         return desc;
567 }
568