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