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