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