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