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