Add features (incomplete, highly experimental), fix DundiLookup app, debug improvemen...
[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 && ast_bridged_channel(p->chan) && 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(&(ast_bridged_channel(p->chan))->lock)) {
148                         if (!ast_mutex_trylock(&p->owner->lock)) {
149                                 ast_channel_masquerade(p->owner, ast_bridged_channel(p->chan));
150                                 p->alreadymasqed = 1;
151                                 ast_mutex_unlock(&p->owner->lock);
152                         }
153                         ast_mutex_unlock(&(ast_bridged_channel(p->chan)->lock));
154                 }
155         } else if (!isoutbound && p->owner && ast_bridged_channel(p->owner) && p->chan) {
156                 /* Masquerade bridged channel into chan */
157                 if (!ast_mutex_trylock(&(ast_bridged_channel(p->owner)->lock))) {
158                         if (!ast_mutex_trylock(&p->chan->lock)) {
159                                 ast_channel_masquerade(p->chan, ast_bridged_channel(p->owner));
160                                 p->alreadymasqed = 1;
161                                 ast_mutex_unlock(&p->chan->lock);
162                         }
163                         ast_mutex_unlock(&(ast_bridged_channel(p->owner)->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         if (!p->owner && !p->chan) {
316                 /* Okay, done with the private part now, too. */
317                 glaredetect = p->glaredetect;
318                 /* If we have a queue holding, don't actually destroy p yet, but
319                    let local_queue do it. */
320                 if (p->glaredetect)
321                         p->cancelqueue = 1;
322                 ast_mutex_unlock(&p->lock);
323                 /* Remove from list */
324                 ast_mutex_lock(&locallock);
325                 cur = locals;
326                 while(cur) {
327                         if (cur == p) {
328                                 if (prev)
329                                         prev->next = cur->next;
330                                 else
331                                         locals = cur->next;
332                                 break;
333                         }
334                         prev = cur;
335                         cur = cur->next;
336                 }
337                 ast_mutex_unlock(&locallock);
338                 /* Grab / release lock just in case */
339                 ast_mutex_lock(&p->lock);
340                 ast_mutex_unlock(&p->lock);
341                 /* And destroy */
342                 if (!glaredetect) {
343                         ast_mutex_destroy(&p->lock);
344                         free(p);
345                 }
346                 return 0;
347         }
348         if (p->chan && !p->launchedpbx)
349                 /* Need to actually hangup since there is no PBX */
350                 ochan = p->chan;
351         else
352                 local_queue_frame(p, isoutbound, &f, NULL);
353         ast_mutex_unlock(&p->lock);
354         if (ochan)
355                 ast_hangup(ochan);
356         return 0;
357 }
358
359 static struct local_pvt *local_alloc(char *data, int format)
360 {
361         struct local_pvt *tmp;
362         char *c;
363         char *opts;
364         tmp = malloc(sizeof(struct local_pvt));
365         if (tmp) {
366                 memset(tmp, 0, sizeof(struct local_pvt));
367                 ast_mutex_init(&tmp->lock);
368                 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
369                 opts = strchr(tmp->exten, '/');
370                 if (opts) {
371                         *opts='\0';
372                         opts++;
373                         if (strchr(opts, 'n'))
374                                 tmp->nooptimization = 1;
375                 }
376                 c = strchr(tmp->exten, '@');
377                 if (c) {
378                         *c = '\0';
379                         c++;
380                         strncpy(tmp->context, c, sizeof(tmp->context) - 1);
381                 } else
382                         strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
383                 tmp->reqformat = format;
384                 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
385                         ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
386                         ast_mutex_destroy(&tmp->lock);
387                         free(tmp);
388                         tmp = NULL;
389                 } else {
390                         /* Add to list */
391                         ast_mutex_lock(&locallock);
392                         tmp->next = locals;
393                         locals = tmp;
394                         ast_mutex_unlock(&locallock);
395                 }
396                 
397         }
398         return tmp;
399 }
400
401 static struct ast_channel *local_new(struct local_pvt *p, int state)
402 {
403         struct ast_channel *tmp, *tmp2;
404         int randnum = rand() & 0xffff;
405         tmp = ast_channel_alloc(1);
406         tmp2 = ast_channel_alloc(1);
407         if (!tmp || !tmp2) {
408                 if (tmp)
409                         ast_channel_free(tmp);
410                 if (tmp2)
411                         ast_channel_free(tmp2);
412                 tmp = NULL;
413         }
414         if (tmp) {
415                 tmp->nativeformats = p->reqformat;
416                 tmp2->nativeformats = p->reqformat;
417                 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-%04x,1", p->exten, p->context, randnum);
418                 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-%04x,2", p->exten, p->context, randnum);
419                 tmp->type = type;
420                 tmp2->type = type;
421                 ast_setstate(tmp, state);
422                 ast_setstate(tmp2, AST_STATE_RING);
423                 tmp->writeformat = p->reqformat;;
424                 tmp2->writeformat = p->reqformat;
425                 tmp->pvt->rawwriteformat = p->reqformat;
426                 tmp2->pvt->rawwriteformat = p->reqformat;
427                 tmp->readformat = p->reqformat;
428                 tmp2->readformat = p->reqformat;
429                 tmp->pvt->rawreadformat = p->reqformat;
430                 tmp2->pvt->rawreadformat = p->reqformat;
431                 tmp->pvt->pvt = p;
432                 tmp2->pvt->pvt = p;
433                 tmp->pvt->send_digit = local_digit;
434                 tmp2->pvt->send_digit = local_digit;
435                 tmp->pvt->call = local_call;
436                 tmp2->pvt->call = local_call;
437                 tmp->pvt->hangup = local_hangup;
438                 tmp2->pvt->hangup = local_hangup;
439                 tmp->pvt->answer = local_answer;
440                 tmp2->pvt->answer = local_answer;
441                 tmp->pvt->read = local_read;
442                 tmp2->pvt->read = local_read;
443                 tmp->pvt->write = local_write;
444                 tmp2->pvt->write = local_write;
445                 tmp->pvt->exception = local_read;
446                 tmp2->pvt->exception = local_read;
447                 tmp->pvt->indicate = local_indicate;
448                 tmp2->pvt->indicate = local_indicate;
449                 tmp->pvt->fixup = local_fixup;
450                 tmp2->pvt->fixup = local_fixup;
451                 p->owner = tmp;
452                 p->chan = tmp2;
453                 ast_mutex_lock(&usecnt_lock);
454                 usecnt++;
455                 ast_mutex_unlock(&usecnt_lock);
456                 ast_update_use_count();
457                 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
458                 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
459                 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
460                 tmp->priority = 1;
461                 tmp2->priority = 1;
462         } else
463                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
464         return tmp;
465 }
466
467
468 static struct ast_channel *local_request(const char *type, int format, void *data, int *cause)
469 {
470         struct local_pvt *p;
471         struct ast_channel *chan = NULL;
472         p = local_alloc(data, format);
473         if (p)
474                 chan = local_new(p, AST_STATE_DOWN);
475         return chan;
476 }
477
478 static int locals_show(int fd, int argc, char **argv)
479 {
480         struct local_pvt *p;
481
482         if (argc != 3)
483                 return RESULT_SHOWUSAGE;
484         ast_mutex_lock(&locallock);
485         p = locals;
486         while(p) {
487                 ast_mutex_lock(&p->lock);
488                 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
489                 ast_mutex_unlock(&p->lock);
490                 p = p->next;
491         }
492         if (!locals)
493                 ast_cli(fd, "No local channels in use\n");
494         ast_mutex_unlock(&locallock);
495         return RESULT_SUCCESS;
496 }
497
498 static char show_locals_usage[] = 
499 "Usage: local show channels\n"
500 "       Provides summary information on local channels.\n";
501
502 static struct ast_cli_entry cli_show_locals = {
503         { "local", "show", "channels", NULL }, locals_show, 
504         "Show status of local channels", show_locals_usage, NULL };
505
506 int load_module()
507 {
508         /* Make sure we can register our sip channel type */
509         if (ast_channel_register(type, tdesc, capability, local_request)) {
510                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
511                 return -1;
512         }
513         ast_cli_register(&cli_show_locals);
514         return 0;
515 }
516
517 int reload()
518 {
519         return 0;
520 }
521
522 int unload_module()
523 {
524         struct local_pvt *p;
525         /* First, take us out of the channel loop */
526         ast_cli_unregister(&cli_show_locals);
527         ast_channel_unregister(type);
528         if (!ast_mutex_lock(&locallock)) {
529                 /* Hangup all interfaces if they have an owner */
530                 p = locals;
531                 while(p) {
532                         if (p->owner)
533                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
534                         p = p->next;
535                 }
536                 locals = NULL;
537                 ast_mutex_unlock(&locallock);
538         } else {
539                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
540                 return -1;
541         }               
542         return 0;
543 }
544
545 int usecount()
546 {
547         int res;
548         ast_mutex_lock(&usecnt_lock);
549         res = usecnt;
550         ast_mutex_unlock(&usecnt_lock);
551         return res;
552 }
553
554 char *key()
555 {
556         return ASTERISK_GPL_KEY;
557 }
558
559 char *description()
560 {
561         return desc;
562 }
563