Merge major BSD mutex and symbol conflict patches (bug #1816) (link patch still pending)
[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 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
53
54 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
55
56 /* Protect the interface list (of sip_pvt's) */
57 AST_MUTEX_DEFINE_STATIC(locallock);
58
59 static struct local_pvt {
60         ast_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 glaredetect;                                        /* Detect glare on hangup */
65         int cancelqueue;                                        /* Cancel queue */
66         int alreadymasqed;                                      /* Already masqueraded */
67         int launchedpbx;                                        /* Did we launch the PBX */
68         int nooptimization;
69         struct ast_channel *owner;                      /* Master Channel */
70         struct ast_channel *chan;                       /* Outbound channel */
71         struct local_pvt *next;                         /* Next entity */
72 } *locals = NULL;
73
74 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, struct ast_channel *us)
75 {
76         struct ast_channel *other;
77 retrylock:              
78         /* Recalculate outbound channel */
79         if (isoutbound) {
80                 other = p->owner;
81         } else {
82                 other = p->chan;
83         }
84         /* Set glare detection */
85         p->glaredetect = 1;
86         if (p->cancelqueue) {
87                 /* We had a glare on the hangup.  Forget all this business,
88                 return and destroy p.  */
89                 ast_mutex_unlock(&p->lock);
90                 ast_mutex_destroy(&p->lock);
91                 free(p);
92                 return -1;
93         }
94         if (!other) {
95                 p->glaredetect = 0;
96                 return 0;
97         }
98         if (ast_mutex_trylock(&other->lock)) {
99                 /* Failed to lock.  Release main lock and try again */
100                 ast_mutex_unlock(&p->lock);
101                 if (us) {
102                         if (ast_mutex_unlock(&us->lock)) {
103                                 ast_log(LOG_WARNING, "%s wasn't locked while sending %d/%d\n",
104                                         us->name, f->frametype, f->subclass);
105                                 us = NULL;
106                         }
107                 }
108                 /* Wait just a bit */
109                 usleep(1);
110                 /* Only we can destroy ourselves, so we can't disappear here */
111                 if (us)
112                         ast_mutex_lock(&us->lock);
113                 ast_mutex_lock(&p->lock);
114                 goto retrylock;
115         }
116         ast_queue_frame(other, f);
117         ast_mutex_unlock(&other->lock);
118         p->glaredetect = 0;
119         return 0;
120 }
121
122 static int local_answer(struct ast_channel *ast)
123 {
124         struct local_pvt *p = ast->pvt->pvt;
125         int isoutbound;
126         int res = -1;
127         ast_mutex_lock(&p->lock);
128         isoutbound = IS_OUTBOUND(ast, p);
129         if (isoutbound) {
130                 /* Pass along answer since somebody answered us */
131                 struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
132                 res = local_queue_frame(p, isoutbound, &answer, ast);
133         } else
134                 ast_log(LOG_WARNING, "Huh?  Local is being asked to answer?\n");
135         ast_mutex_unlock(&p->lock);
136         return res;
137 }
138
139 static void check_bridge(struct local_pvt *p, int isoutbound)
140 {
141         if (p->alreadymasqed || p->nooptimization)
142                 return;
143         if (isoutbound && p->chan && p->chan->bridge && p->owner) {
144                 /* Masquerade bridged channel into owner */
145                 /* Lock everything we need, one by one, and give up if
146                    we can't get everything.  Remember, we'll get another
147                    chance in just a little bit */
148                 if (!ast_mutex_trylock(&p->chan->bridge->lock)) {
149                         if (!ast_mutex_trylock(&p->owner->lock)) {
150                                 ast_channel_masquerade(p->owner, p->chan->bridge);
151                                 p->alreadymasqed = 1;
152                                 ast_mutex_unlock(&p->owner->lock);
153                         }
154                         ast_mutex_unlock(&p->chan->bridge->lock);
155                 }
156         } else if (!isoutbound && p->owner && p->owner->bridge && p->chan) {
157                 /* Masquerade bridged channel into chan */
158                 if (!ast_mutex_trylock(&p->owner->bridge->lock)) {
159                         if (!ast_mutex_trylock(&p->chan->lock)) {
160                                 ast_channel_masquerade(p->chan, p->owner->bridge);
161                                 p->alreadymasqed = 1;
162                                 ast_mutex_unlock(&p->chan->lock);
163                         }
164                         ast_mutex_unlock(&p->owner->bridge->lock);
165                 }
166         }
167 }
168
169 static struct ast_frame  *local_read(struct ast_channel *ast)
170 {
171         static struct ast_frame null = { AST_FRAME_NULL, };
172         return &null;
173 }
174
175 static int local_write(struct ast_channel *ast, struct ast_frame *f)
176 {
177         struct local_pvt *p = ast->pvt->pvt;
178         int res = -1;
179         int isoutbound;
180
181
182         /* Just queue for delivery to the other side */
183         ast_mutex_lock(&p->lock);
184         isoutbound = IS_OUTBOUND(ast, p);
185         res = local_queue_frame(p, isoutbound, f, ast);
186         check_bridge(p, isoutbound);
187         ast_mutex_unlock(&p->lock);
188         return res;
189 }
190
191 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
192 {
193         struct local_pvt *p = newchan->pvt->pvt;
194         ast_mutex_lock(&p->lock);
195         if ((p->owner != oldchan) && (p->chan != oldchan)) {
196                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
197                 ast_mutex_unlock(&p->lock);
198                 return -1;
199         }
200         if (p->owner == oldchan)
201                 p->owner = newchan;
202         else
203                 p->chan = newchan;      
204         ast_mutex_unlock(&p->lock);
205         return 0;
206 }
207
208 static int local_indicate(struct ast_channel *ast, int condition)
209 {
210         struct local_pvt *p = ast->pvt->pvt;
211         int res = -1;
212         struct ast_frame f = { AST_FRAME_CONTROL, };
213         int isoutbound;
214         /* Queue up a frame representing the indication as a control frame */
215         ast_mutex_lock(&p->lock);
216         isoutbound = IS_OUTBOUND(ast, p);
217         f.subclass = condition;
218         res = local_queue_frame(p, isoutbound, &f, ast);
219         ast_mutex_unlock(&p->lock);
220         return res;
221 }
222
223 static int local_digit(struct ast_channel *ast, char digit)
224 {
225         struct local_pvt *p = ast->pvt->pvt;
226         int res = -1;
227         struct ast_frame f = { AST_FRAME_DTMF, };
228         int isoutbound;
229         ast_mutex_lock(&p->lock);
230         isoutbound = IS_OUTBOUND(ast, p);
231         f.subclass = digit;
232         res = local_queue_frame(p, isoutbound, &f, ast);
233         ast_mutex_unlock(&p->lock);
234         return res;
235 }
236
237 static int local_call(struct ast_channel *ast, char *dest, int timeout)
238 {
239         struct local_pvt *p = ast->pvt->pvt;
240         int res;
241         
242         ast_mutex_lock(&p->lock);
243         if (p->owner->callerid)
244                 p->chan->callerid = strdup(p->owner->callerid);
245         else
246                 p->chan->callerid = NULL;
247         if (p->owner->rdnis)
248                 p->chan->rdnis = strdup(p->owner->rdnis);
249         else
250                 p->chan->rdnis = NULL;
251         if (p->owner->ani)
252                 p->chan->ani = strdup(p->owner->ani);
253         else
254                 p->chan->ani = NULL;
255         strncpy(p->chan->language, p->owner->language, sizeof(p->chan->language) - 1);
256         p->launchedpbx = 1;
257         /* Start switch on sub channel */
258         res = ast_pbx_start(p->chan);
259         ast_mutex_unlock(&p->lock);
260         return res;
261 }
262
263 /*
264 static void local_destroy(struct local_pvt *p)
265 {
266         struct local_pvt *cur, *prev = NULL;
267         ast_mutex_lock(&locallock);
268         cur = locals;
269         while(cur) {
270                 if (cur == p) {
271                         if (prev)
272                                 prev->next = cur->next;
273                         else
274                                 locals = cur->next;
275                         ast_mutex_destroy(cur);
276                         free(cur);
277                         break;
278                 }
279                 prev = cur;
280                 cur = cur->next;
281         }
282         ast_mutex_unlock(&locallock);
283         if (!cur)
284                 ast_log(LOG_WARNING, "Unable ot find local '%s@%s' in local list\n", p->exten, p->context);
285 }
286 */
287
288 static int local_hangup(struct ast_channel *ast)
289 {
290         struct local_pvt *p = ast->pvt->pvt;
291         int isoutbound;
292         struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
293         struct local_pvt *cur, *prev=NULL;
294         struct ast_channel *ochan = NULL;
295         int glaredetect;
296         ast_mutex_lock(&p->lock);
297         isoutbound = IS_OUTBOUND(ast, p);
298         if (isoutbound) {
299                 p->chan = NULL;
300                 p->launchedpbx = 0;
301         } else
302                 p->owner = NULL;
303         ast->pvt->pvt = NULL;
304         
305         if (!p->owner && !p->chan) {
306                 /* Okay, done with the private part now, too. */
307                 glaredetect = p->glaredetect;
308                 /* If we have a queue holding, don't actually destroy p yet, but
309                    let local_queue do it. */
310                 if (p->glaredetect)
311                         p->cancelqueue = 1;
312                 ast_mutex_unlock(&p->lock);
313                 /* Remove from list */
314                 ast_mutex_lock(&locallock);
315                 cur = locals;
316                 while(cur) {
317                         if (cur == p) {
318                                 if (prev)
319                                         prev->next = cur->next;
320                                 else
321                                         locals = cur->next;
322                                 break;
323                         }
324                         prev = cur;
325                         cur = cur->next;
326                 }
327                 ast_mutex_unlock(&locallock);
328                 /* And destroy */
329                 if (!glaredetect) {
330                         ast_mutex_destroy(&p->lock);
331                         free(p);
332                 }
333                 return 0;
334         }
335         if (p->chan && !p->launchedpbx)
336                 /* Need to actually hangup since there is no PBX */
337                 ochan = p->chan;
338         else
339                 local_queue_frame(p, isoutbound, &f, NULL);
340         ast_mutex_unlock(&p->lock);
341         if (ochan)
342                 ast_hangup(ochan);
343         return 0;
344 }
345
346 static struct local_pvt *local_alloc(char *data, int format)
347 {
348         struct local_pvt *tmp;
349         char *c;
350         char *opts;
351         tmp = malloc(sizeof(struct local_pvt));
352         if (tmp) {
353                 memset(tmp, 0, sizeof(struct local_pvt));
354                 ast_mutex_init(&tmp->lock);
355                 strncpy(tmp->exten, data, sizeof(tmp->exten) - 1);
356                 opts = strchr(tmp->exten, '/');
357                 if (opts) {
358                         *opts='\0';
359                         opts++;
360                         if (strchr(opts, 'n'))
361                                 tmp->nooptimization = 1;
362                 }
363                 c = strchr(tmp->exten, '@');
364                 if (c) {
365                         *c = '\0';
366                         c++;
367                         strncpy(tmp->context, c, sizeof(tmp->context) - 1);
368                 } else
369                         strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
370                 tmp->reqformat = format;
371                 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
372                         ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->context, tmp->exten);
373                         ast_mutex_destroy(&tmp->lock);
374                         free(tmp);
375                         tmp = NULL;
376                 } else {
377                         /* Add to list */
378                         ast_mutex_lock(&locallock);
379                         tmp->next = locals;
380                         locals = tmp;
381                         ast_mutex_unlock(&locallock);
382                 }
383                 
384         }
385         return tmp;
386 }
387
388 static struct ast_channel *local_new(struct local_pvt *p, int state)
389 {
390         struct ast_channel *tmp, *tmp2;
391         int randnum = rand() & 0xffff;
392         tmp = ast_channel_alloc(1);
393         tmp2 = ast_channel_alloc(1);
394         if (!tmp || !tmp2) {
395                 if (tmp)
396                         ast_channel_free(tmp);
397                 if (tmp2)
398                         ast_channel_free(tmp2);
399                 tmp = NULL;
400         }
401         if (tmp) {
402                 tmp->nativeformats = p->reqformat;
403                 tmp2->nativeformats = p->reqformat;
404                 snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-%04x,1", p->exten, p->context, randnum);
405                 snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-%04x,2", p->exten, p->context, randnum);
406                 tmp->type = type;
407                 tmp2->type = type;
408                 ast_setstate(tmp, state);
409                 ast_setstate(tmp2, AST_STATE_RING);
410                 tmp->writeformat = p->reqformat;;
411                 tmp2->writeformat = p->reqformat;
412                 tmp->pvt->rawwriteformat = p->reqformat;
413                 tmp2->pvt->rawwriteformat = p->reqformat;
414                 tmp->readformat = p->reqformat;
415                 tmp2->readformat = p->reqformat;
416                 tmp->pvt->rawreadformat = p->reqformat;
417                 tmp2->pvt->rawreadformat = p->reqformat;
418                 tmp->pvt->pvt = p;
419                 tmp2->pvt->pvt = p;
420                 tmp->pvt->send_digit = local_digit;
421                 tmp2->pvt->send_digit = local_digit;
422                 tmp->pvt->call = local_call;
423                 tmp2->pvt->call = local_call;
424                 tmp->pvt->hangup = local_hangup;
425                 tmp2->pvt->hangup = local_hangup;
426                 tmp->pvt->answer = local_answer;
427                 tmp2->pvt->answer = local_answer;
428                 tmp->pvt->read = local_read;
429                 tmp2->pvt->read = local_read;
430                 tmp->pvt->write = local_write;
431                 tmp2->pvt->write = local_write;
432                 tmp->pvt->exception = local_read;
433                 tmp2->pvt->exception = local_read;
434                 tmp->pvt->indicate = local_indicate;
435                 tmp2->pvt->indicate = local_indicate;
436                 tmp->pvt->fixup = local_fixup;
437                 tmp2->pvt->fixup = local_fixup;
438                 p->owner = tmp;
439                 p->chan = tmp2;
440                 ast_mutex_lock(&usecnt_lock);
441                 usecnt++;
442                 ast_mutex_unlock(&usecnt_lock);
443                 ast_update_use_count();
444                 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
445                 strncpy(tmp2->context, p->context, sizeof(tmp2->context)-1);
446                 strncpy(tmp2->exten, p->exten, sizeof(tmp->exten)-1);
447                 tmp->priority = 1;
448                 tmp2->priority = 1;
449         } else
450                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
451         return tmp;
452 }
453
454
455 static struct ast_channel *local_request(char *type, int format, void *data)
456 {
457         struct local_pvt *p;
458         struct ast_channel *chan = NULL;
459         p = local_alloc(data, format);
460         if (p)
461                 chan = local_new(p, AST_STATE_DOWN);
462         return chan;
463 }
464
465 static int locals_show(int fd, int argc, char **argv)
466 {
467         struct local_pvt *p;
468
469         if (argc != 3)
470                 return RESULT_SHOWUSAGE;
471         ast_mutex_lock(&locallock);
472         p = locals;
473         while(p) {
474                 ast_mutex_lock(&p->lock);
475                 ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
476                 ast_mutex_unlock(&p->lock);
477                 p = p->next;
478         }
479         if (!locals)
480                 ast_cli(fd, "No local channels in use\n");
481         ast_mutex_unlock(&locallock);
482         return RESULT_SUCCESS;
483 }
484
485 static char show_locals_usage[] = 
486 "Usage: local show channels\n"
487 "       Provides summary information on local channels.\n";
488
489 static struct ast_cli_entry cli_show_locals = {
490         { "local", "show", "channels", NULL }, locals_show, 
491         "Show status of local channels", show_locals_usage, NULL };
492
493 int load_module()
494 {
495         /* Make sure we can register our sip channel type */
496         if (ast_channel_register(type, tdesc, capability, local_request)) {
497                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
498                 return -1;
499         }
500         ast_cli_register(&cli_show_locals);
501         return 0;
502 }
503
504 int reload()
505 {
506         return 0;
507 }
508
509 int unload_module()
510 {
511         struct local_pvt *p;
512         /* First, take us out of the channel loop */
513         ast_cli_unregister(&cli_show_locals);
514         ast_channel_unregister(type);
515         if (!ast_mutex_lock(&locallock)) {
516                 /* Hangup all interfaces if they have an owner */
517                 p = locals;
518                 while(p) {
519                         if (p->owner)
520                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
521                         p = p->next;
522                 }
523                 locals = NULL;
524                 ast_mutex_unlock(&locallock);
525         } else {
526                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
527                 return -1;
528         }               
529         return 0;
530 }
531
532 int usecount()
533 {
534         int res;
535         ast_mutex_lock(&usecnt_lock);
536         res = usecnt;
537         ast_mutex_unlock(&usecnt_lock);
538         return res;
539 }
540
541 char *key()
542 {
543         return ASTERISK_GPL_KEY;
544 }
545
546 char *description()
547 {
548         return desc;
549 }
550