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