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