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