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