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