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