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