Make check_bridge back off if it cant get all the locks it wants
[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 ast_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 ast_mutex_t locallock = AST_MUTEX_INITIALIZER;
58
59 static struct local_pvt {
60         ast_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 glaredetect;                                        /* Detect glare on hangup */
65         int cancelqueue;                                        /* Cancel queue */
66         int alreadymasqed;                                      /* Already masqueraded */
67         struct ast_channel *owner;                      /* Master Channel */
68         struct ast_channel *chan;                       /* Outbound channel */
69         struct local_pvt *next;                         /* Next entity */
70 } *locals = NULL;
71
72 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, struct ast_channel *us)
73 {
74         struct ast_channel *other;
75         if (isoutbound) {
76                 other = p->owner;
77         } else {
78                 other = p->chan;
79         }
80         if (!other)
81                 return 0;
82         p->glaredetect = 1;
83 retrylock:              
84         if (p->cancelqueue) {
85                 /* We had a glare on the hangup.  Forget all this business,
86                 return and destroy p.  */
87                 ast_mutex_unlock(&p->lock);
88                 free(p);
89                 return -1;
90         }
91         if (ast_mutex_trylock(&other->lock)) {
92                 /* Failed to lock.  Release main lock and try again */
93                 ast_mutex_unlock(&p->lock);
94                 if (us) {
95                         if (ast_mutex_unlock(&us->lock)) {
96                                 ast_log(LOG_WARNING, "%s wasn't locked while sending %d/%d\n",
97                                         us->name, f->frametype, f->subclass);
98                                 us = NULL;
99                         }
100                 }
101                 /* Wait just a bit */
102                 usleep(1);
103                 /* Only we can destroy ourselves, so we can't disappear here */
104                 if (us)
105                         ast_mutex_lock(&us->lock);
106                 ast_mutex_lock(&p->lock);
107                 goto retrylock;
108         }
109         ast_queue_frame(other, f, 0);
110         ast_mutex_unlock(&other->lock);
111         p->glaredetect = 0;
112         return 0;
113 }
114
115 static int local_answer(struct ast_channel *ast)
116 {
117         struct local_pvt *p = ast->pvt->pvt;
118         int isoutbound = IS_OUTBOUND(ast, p);
119         int res = -1;
120         ast_mutex_lock(&p->lock);
121         if (isoutbound) {
122                 /* Pass along answer since somebody answered us */
123                 struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
124                 res = local_queue_frame(p, isoutbound, &answer, ast);
125         } else
126                 ast_log(LOG_WARNING, "Huh?  Local is being asked to answer?\n");
127         ast_mutex_unlock(&p->lock);
128         return res;
129 }
130
131 static void check_bridge(struct local_pvt *p, int isoutbound)
132 {
133         if (p->alreadymasqed)
134                 return;
135         if (isoutbound && p->chan && p->chan->bridge && p->owner) {
136                 /* Masquerade bridged channel into owner */
137                 /* Lock everything we need, one by one, and give up if
138                    we can't get everything.  Remember, we'll get another
139                    chance in just a little bit */
140                 if (!ast_mutex_trylock(&p->chan->bridge->lock)) {
141                         if (!ast_mutex_trylock(&p->owner->lock)) {
142                                 ast_channel_masquerade(p->owner, p->chan->bridge);
143                                 p->alreadymasqed = 1;
144                                 ast_mutex_unlock(&p->owner->lock);
145                         }
146                         ast_mutex_unlock(&p->chan->bridge->lock);
147                 }
148         } else if (!isoutbound && p->owner && p->owner->bridge && p->chan) {
149                 /* Masquerade bridged channel into chan */
150                 if (!ast_mutex_trylock(&p->owner->bridge->lock)) {
151                         if (!ast_mutex_trylock(&p->chan->lock)) {
152                                 ast_channel_masquerade(p->chan, p->owner->bridge);
153                                 p->alreadymasqed = 1;
154                                 ast_mutex_unlock(&p->chan->lock);
155                         }
156                         ast_mutex_unlock(&p->owner->bridge->lock);
157                 }
158         }
159 }
160
161 static struct ast_frame  *local_read(struct ast_channel *ast)
162 {
163         static struct ast_frame null = { AST_FRAME_NULL, };
164         return &null;
165 }
166
167 static int local_write(struct ast_channel *ast, struct ast_frame *f)
168 {
169         struct local_pvt *p = ast->pvt->pvt;
170         int res = -1;
171         int isoutbound = IS_OUTBOUND(ast, p);
172
173
174         /* Just queue for delivery to the other side */
175         ast_mutex_lock(&p->lock);
176         res = local_queue_frame(p, isoutbound, f, ast);
177         check_bridge(p, isoutbound);
178         ast_mutex_unlock(&p->lock);
179         return res;
180 }
181
182 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
183 {
184         struct local_pvt *p = newchan->pvt->pvt;
185         ast_mutex_lock(&p->lock);
186         if ((p->owner != oldchan) && (p->chan != oldchan)) {
187                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
188                 ast_mutex_unlock(&p->lock);
189                 return -1;
190         }
191         if (p->owner == oldchan)
192                 p->owner = newchan;
193         else
194                 p->chan = newchan;      
195         ast_mutex_unlock(&p->lock);
196         return 0;
197 }
198
199 static int local_indicate(struct ast_channel *ast, int condition)
200 {
201         struct local_pvt *p = ast->pvt->pvt;
202         int res = -1;
203         struct ast_frame f = { AST_FRAME_CONTROL, };
204         int isoutbound = IS_OUTBOUND(ast, p);
205         /* Queue up a frame representing the indication as a control frame */
206         ast_mutex_lock(&p->lock);
207         f.subclass = condition;
208         res = local_queue_frame(p, isoutbound, &f, ast);
209         ast_mutex_unlock(&p->lock);
210         return res;
211 }
212
213 static int local_digit(struct ast_channel *ast, char digit)
214 {
215         struct local_pvt *p = ast->pvt->pvt;
216         int res = -1;
217         struct ast_frame f = { AST_FRAME_DTMF, };
218         int isoutbound = IS_OUTBOUND(ast, p);
219         ast_mutex_lock(&p->lock);
220         f.subclass = digit;
221         res = local_queue_frame(p, isoutbound, &f, ast);
222         ast_mutex_unlock(&p->lock);
223         return res;
224 }
225
226 static int local_call(struct ast_channel *ast, char *dest, int timeout)
227 {
228         struct local_pvt *p = ast->pvt->pvt;
229
230         if (p->owner->callerid)
231                 p->chan->callerid = strdup(p->owner->callerid);
232         else
233                 p->chan->callerid = NULL;
234         if (p->owner->ani)
235                 p->chan->ani = strdup(p->owner->ani);
236         else
237                 p->chan->ani = NULL;
238         /* Start switch on sub channel */
239         return ast_pbx_start(p->chan);
240 }
241
242 static void local_destroy(struct local_pvt *p)
243 {
244         struct local_pvt *cur, *prev = NULL;
245         ast_mutex_lock(&locallock);
246         cur = locals;
247         while(cur) {
248                 if (cur == p) {
249                         if (prev)
250                                 prev->next = cur->next;
251                         else
252                                 locals = cur->next;
253                         free(cur);
254                         break;
255                 }
256                 prev = cur;
257                 cur = cur->next;
258         }
259         ast_mutex_unlock(&locallock);
260         if (!cur)
261                 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
262 }
263
264 static int local_hangup(struct ast_channel *ast)
265 {
266         struct local_pvt *p = ast->pvt->pvt;
267         int isoutbound = IS_OUTBOUND(ast, p);
268         struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
269         struct local_pvt *cur, *prev=NULL;
270         struct ast_channel *ochan = NULL;
271         int glaredetect;
272         ast_mutex_lock(&p->lock);
273         if (isoutbound)
274                 p->chan = NULL;
275         else
276                 p->owner = NULL;
277         ast->pvt->pvt = NULL;
278         
279         if (!p->owner && !p->chan) {
280                 /* Okay, done with the private part now, too. */
281                 glaredetect = p->glaredetect;
282                 /* If we have a queue holding, don't actually destroy p yet, but
283                    let local_queue do it. */
284                 if (p->glaredetect)
285                         p->cancelqueue = 1;
286                 ast_mutex_unlock(&p->lock);
287                 /* Remove from list */
288                 ast_mutex_lock(&locallock);
289                 cur = locals;
290                 while(cur) {
291                         if (cur == p) {
292                                 if (prev)
293                                         prev->next = cur->next;
294                                 else
295                                         locals = cur->next;
296                                 break;
297                         }
298                         prev = cur;
299                         cur = cur->next;
300                 }
301                 ast_mutex_unlock(&locallock);
302                 /* And destroy */
303                 if (!glaredetect)
304                         free(p);
305                 return 0;
306         }
307         if (p->chan && !p->chan->pbx)
308                 /* Need to actually hangup since there is no PBX */
309                 ochan = p->chan;
310         else
311                 local_queue_frame(p, isoutbound, &f, NULL);
312         ast_mutex_unlock(&p->lock);
313         if (ochan)
314                 ast_hangup(ochan);
315         return 0;
316 }
317
318 static struct local_pvt *local_alloc(char *data, int format)
319 {
320         struct local_pvt *tmp;
321         char *c;
322         tmp = malloc(sizeof(struct local_pvt));
323         if (tmp) {
324                 memset(tmp, 0, sizeof(struct local_pvt));
325                 ast_mutex_init(&tmp->lock);
326                 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
327                 c = strchr(tmp->exten, '@');
328                 if (c) {
329                         *c = '\0';
330                         c++;
331                         strncpy(tmp->context, c, sizeof(tmp->context) - 1);
332                 } else
333                         strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
334                 tmp->reqformat = format;
335                 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
336                         ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->context, tmp->exten);
337                         free(tmp);
338                         tmp = NULL;
339                 } else {
340                         /* Add to list */
341                         ast_mutex_lock(&locallock);
342                         tmp->next = locals;
343                         locals = tmp;
344                         ast_mutex_unlock(&locallock);
345                 }
346                 
347         }
348         return tmp;
349 }
350
351 static struct ast_channel *local_new(struct local_pvt *p, int state)
352 {
353         struct ast_channel *tmp, *tmp2;
354         int randnum = rand() & 0xffff;
355         tmp = ast_channel_alloc(1);
356         tmp2 = ast_channel_alloc(1);
357         if (!tmp || !tmp2) {
358                 if (tmp)
359                         ast_channel_free(tmp);
360                 if (tmp2)
361                         ast_channel_free(tmp2);
362                 tmp = NULL;
363         }
364         if (tmp) {
365                 tmp->nativeformats = p->reqformat;
366                 tmp2->nativeformats = p->reqformat;
367                 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-%04x.1", p->exten, p->context, randnum);
368                 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-%04x,2", p->exten, p->context, randnum);
369                 tmp->type = type;
370                 tmp2->type = type;
371                 ast_setstate(tmp, state);
372                 ast_setstate(tmp2, AST_STATE_RING);
373                 tmp->writeformat = p->reqformat;;
374                 tmp2->writeformat = p->reqformat;
375                 tmp->pvt->rawwriteformat = p->reqformat;
376                 tmp2->pvt->rawwriteformat = p->reqformat;
377                 tmp->readformat = p->reqformat;
378                 tmp2->readformat = p->reqformat;
379                 tmp->pvt->rawreadformat = p->reqformat;
380                 tmp2->pvt->rawreadformat = p->reqformat;
381                 tmp->pvt->pvt = p;
382                 tmp2->pvt->pvt = p;
383                 tmp->pvt->send_digit = local_digit;
384                 tmp2->pvt->send_digit = local_digit;
385                 tmp->pvt->call = local_call;
386                 tmp2->pvt->call = local_call;
387                 tmp->pvt->hangup = local_hangup;
388                 tmp2->pvt->hangup = local_hangup;
389                 tmp->pvt->answer = local_answer;
390                 tmp2->pvt->answer = local_answer;
391                 tmp->pvt->read = local_read;
392                 tmp2->pvt->read = local_read;
393                 tmp->pvt->write = local_write;
394                 tmp2->pvt->write = local_write;
395                 tmp->pvt->exception = local_read;
396                 tmp2->pvt->exception = local_read;
397                 tmp->pvt->indicate = local_indicate;
398                 tmp2->pvt->indicate = local_indicate;
399                 tmp->pvt->fixup = local_fixup;
400                 tmp2->pvt->fixup = local_fixup;
401                 p->owner = tmp;
402                 p->chan = tmp2;
403                 ast_mutex_lock(&usecnt_lock);
404                 usecnt++;
405                 ast_mutex_unlock(&usecnt_lock);
406                 ast_update_use_count();
407                 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
408                 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
409                 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
410                 tmp->priority = 1;
411                 tmp2->priority = 1;
412         } else
413                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
414         return tmp;
415 }
416
417
418 static struct ast_channel *local_request(char *type, int format, void *data)
419 {
420         struct local_pvt *p;
421         struct ast_channel *chan = NULL;
422         p = local_alloc(data, format);
423         if (p)
424                 chan = local_new(p, AST_STATE_DOWN);
425         return chan;
426 }
427
428 static int locals_show(int fd, int argc, char **argv)
429 {
430         struct local_pvt *p;
431
432         if (argc != 2)
433                 return RESULT_SHOWUSAGE;
434         ast_mutex_lock(&locallock);
435         p = locals;
436         while(p) {
437                 ast_mutex_lock(&p->lock);
438                 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
439                 ast_mutex_unlock(&p->lock);
440                 p = p->next;
441         }
442         if (!locals)
443                 ast_cli(fd, "No local channels in use\n");
444         ast_mutex_unlock(&locallock);
445         return RESULT_SUCCESS;
446 }
447
448 static char show_locals_usage[] = 
449 "Usage: show locals\n"
450 "       Provides summary information on locals.\n";
451
452 static struct ast_cli_entry cli_show_locals = {
453         { "show", "locals", NULL }, locals_show, 
454         "Show status of local channels", show_locals_usage, NULL };
455
456 int load_module()
457 {
458         /* Make sure we can register our sip channel type */
459         if (ast_channel_register(type, tdesc, capability, local_request)) {
460                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
461                 return -1;
462         }
463         ast_cli_register(&cli_show_locals);
464         return 0;
465 }
466
467 int reload()
468 {
469         return 0;
470 }
471
472 int unload_module()
473 {
474         struct local_pvt *p;
475         /* First, take us out of the channel loop */
476         ast_cli_unregister(&cli_show_locals);
477         ast_channel_unregister(type);
478         if (!ast_mutex_lock(&locallock)) {
479                 /* Hangup all interfaces if they have an owner */
480                 p = locals;
481                 while(p) {
482                         if (p->owner)
483                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
484                         p = p->next;
485                 }
486                 locals = NULL;
487                 ast_mutex_unlock(&locallock);
488         } else {
489                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
490                 return -1;
491         }               
492         return 0;
493 }
494
495 int usecount()
496 {
497         int res;
498         ast_mutex_lock(&usecnt_lock);
499         res = usecnt;
500         ast_mutex_unlock(&usecnt_lock);
501         return res;
502 }
503
504 char *key()
505 {
506         return ASTERISK_GPL_KEY;
507 }
508
509 char *description()
510 {
511         return desc;
512 }
513