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