Make call forwarding work properly by using new Local 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 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         /* Start switch on sub channel */
193         return ast_pbx_start(p->chan);
194 }
195
196 static void local_destroy(struct local_pvt *p)
197 {
198         struct local_pvt *cur, *prev = NULL;
199         ast_pthread_mutex_lock(&locallock);
200         cur = locals;
201         while(cur) {
202                 if (cur == p) {
203                         if (prev)
204                                 prev->next = cur->next;
205                         else
206                                 locals = cur->next;
207                         free(cur);
208                         break;
209                 }
210                 prev = cur;
211                 cur = cur->next;
212         }
213         ast_pthread_mutex_unlock(&locallock);
214         if (!cur)
215                 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
216 }
217
218 static int local_hangup(struct ast_channel *ast)
219 {
220         struct local_pvt *p = ast->pvt->pvt;
221         int isoutbound = IS_OUTBOUND(ast, p);
222         struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
223         ast_pthread_mutex_lock(&p->lock);
224         if (isoutbound)
225                 p->chan = NULL;
226         else
227                 p->owner = NULL;
228         ast->pvt->pvt = NULL;
229         if (!p->owner && !p->chan) {
230                 /* Okay, done with the private part now, too. */
231                 ast_pthread_mutex_unlock(&p->lock);
232                 free(p);
233                 return 0;
234         }
235         local_queue_frame(p, isoutbound, &f);
236         ast_pthread_mutex_unlock(&p->lock);
237         return 0;
238 }
239
240 static struct local_pvt *local_alloc(char *data, int format)
241 {
242         struct local_pvt *tmp;
243         char *c;
244         tmp = malloc(sizeof(struct local_pvt));
245         if (tmp) {
246                 memset(tmp, 0, sizeof(struct local_pvt));
247                 ast_pthread_mutex_init(&tmp->lock);
248                 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
249                 c = strchr(tmp->exten, '@');
250                 if (c) {
251                         *c = '\0';
252                         c++;
253                         strncpy(tmp->context, c, sizeof(tmp->context) - 1);
254                 } else
255                         strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
256                 tmp->reqformat = format;
257                 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
258                         ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->context, tmp->exten);
259                         free(tmp);
260                         tmp = NULL;
261                 } else {
262                         /* Add to list */
263                         ast_pthread_mutex_lock(&locallock);
264                         tmp->next = locals;
265                         locals = tmp;
266                         ast_pthread_mutex_unlock(&locallock);
267                 }
268                 
269         }
270         return tmp;
271 }
272
273 static struct ast_channel *local_new(struct local_pvt *p, int state)
274 {
275         struct ast_channel *tmp, *tmp2;
276         tmp = ast_channel_alloc(1);
277         tmp2 = ast_channel_alloc(1);
278         if (!tmp || !tmp2) {
279                 if (tmp)
280                         ast_channel_free(tmp);
281                 if (tmp2)
282                         ast_channel_free(tmp2);
283                 tmp = NULL;
284         }
285         if (tmp) {
286                 tmp->nativeformats = p->reqformat;
287                 tmp2->nativeformats = p->reqformat;
288                 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-1", p->exten, p->context);
289                 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-2", p->exten, p->context);
290                 tmp->type = type;
291                 tmp2->type = type;
292                 ast_setstate(tmp, state);
293                 ast_setstate(tmp2, AST_STATE_RING);
294                 tmp->writeformat = p->reqformat;;
295                 tmp2->writeformat = p->reqformat;
296                 tmp->pvt->rawwriteformat = p->reqformat;
297                 tmp2->pvt->rawwriteformat = p->reqformat;
298                 tmp->readformat = p->reqformat;
299                 tmp2->readformat = p->reqformat;
300                 tmp->pvt->rawreadformat = p->reqformat;
301                 tmp2->pvt->rawreadformat = p->reqformat;
302                 tmp->pvt->pvt = p;
303                 tmp2->pvt->pvt = p;
304                 tmp->pvt->send_digit = local_digit;
305                 tmp2->pvt->send_digit = local_digit;
306                 tmp->pvt->call = local_call;
307                 tmp2->pvt->call = local_call;
308                 tmp->pvt->hangup = local_hangup;
309                 tmp2->pvt->hangup = local_hangup;
310                 tmp->pvt->answer = local_answer;
311                 tmp2->pvt->answer = local_answer;
312                 tmp->pvt->read = local_read;
313                 tmp2->pvt->read = local_read;
314                 tmp->pvt->write = local_write;
315                 tmp2->pvt->write = local_write;
316                 tmp->pvt->exception = local_read;
317                 tmp2->pvt->exception = local_read;
318                 tmp->pvt->indicate = local_indicate;
319                 tmp2->pvt->indicate = local_indicate;
320                 tmp->pvt->fixup = local_fixup;
321                 tmp2->pvt->fixup = local_fixup;
322                 p->owner = tmp;
323                 p->chan = tmp2;
324                 ast_pthread_mutex_lock(&usecnt_lock);
325                 usecnt++;
326                 ast_pthread_mutex_unlock(&usecnt_lock);
327                 ast_update_use_count();
328                 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
329                 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
330                 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
331                 tmp->priority = 1;
332                 tmp2->priority = 1;
333         } else
334                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
335         return tmp;
336 }
337
338
339 static struct ast_channel *local_request(char *type, int format, void *data)
340 {
341         struct local_pvt *p;
342         struct ast_channel *chan = NULL;
343         p = local_alloc(data, format);
344         if (p)
345                 chan = local_new(p, AST_STATE_DOWN);
346         return chan;
347 }
348
349 static int locals_show(int fd, int argc, char **argv)
350 {
351         struct local_pvt *p;
352
353         if (argc != 2)
354                 return RESULT_SHOWUSAGE;
355         ast_pthread_mutex_lock(&locallock);
356         p = locals;
357         while(p) {
358                 ast_pthread_mutex_lock(&p->lock);
359                 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
360                 ast_pthread_mutex_unlock(&p->lock);
361                 p = p->next;
362         }
363         if (!locals)
364                 ast_cli(fd, "No local channels in use\n");
365         ast_pthread_mutex_unlock(&locallock);
366         return RESULT_SUCCESS;
367 }
368
369 static char show_locals_usage[] = 
370 "Usage: show locals\n"
371 "       Provides summary information on locals.\n";
372
373 static struct ast_cli_entry cli_show_locals = {
374         { "show", "locals", NULL }, locals_show, 
375         "Show status of local channels", show_locals_usage, NULL };
376
377 int load_module()
378 {
379         /* Make sure we can register our sip channel type */
380         if (ast_channel_register(type, tdesc, capability, local_request)) {
381                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
382                 return -1;
383         }
384         ast_cli_register(&cli_show_locals);
385         return 0;
386 }
387
388 int reload()
389 {
390         return 0;
391 }
392
393 int unload_module()
394 {
395         struct local_pvt *p;
396         /* First, take us out of the channel loop */
397         ast_cli_unregister(&cli_show_locals);
398         ast_channel_unregister(type);
399         if (!ast_pthread_mutex_lock(&locallock)) {
400                 /* Hangup all interfaces if they have an owner */
401                 p = locals;
402                 while(p) {
403                         if (p->owner)
404                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
405                         p = p->next;
406                 }
407                 locals = NULL;
408                 ast_pthread_mutex_unlock(&locallock);
409         } else {
410                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
411                 return -1;
412         }               
413         return 0;
414 }
415
416 int usecount()
417 {
418         int res;
419         ast_pthread_mutex_lock(&usecnt_lock);
420         res = usecnt;
421         ast_pthread_mutex_unlock(&usecnt_lock);
422         return res;
423 }
424
425 char *key()
426 {
427         return ASTERISK_GPL_KEY;
428 }
429
430 char *description()
431 {
432         return desc;
433 }
434