remove extraneous svn:executable properties
[asterisk/asterisk.git] / channels / chan_features.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief feature Proxy Channel
22  * 
23  * \note *** Experimental code ****
24  * 
25  * \ingroup channel_drivers
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include <netdb.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <sys/signal.h>
39
40 #include "asterisk.h"
41
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43
44 #include "asterisk/lock.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/config.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/module.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/options.h"
51 #include "asterisk/lock.h"
52 #include "asterisk/sched.h"
53 #include "asterisk/io.h"
54 #include "asterisk/rtp.h"
55 #include "asterisk/acl.h"
56 #include "asterisk/callerid.h"
57 #include "asterisk/file.h"
58 #include "asterisk/cli.h"
59 #include "asterisk/app.h"
60 #include "asterisk/musiconhold.h"
61 #include "asterisk/manager.h"
62
63 static const char desc[] = "Feature Proxy Channel";
64 static const char type[] = "Feature";
65 static const char tdesc[] = "Feature Proxy Channel Driver";
66
67 static int usecnt =0;
68 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
69
70 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
71
72 /* Protect the interface list (of feature_pvt's) */
73 AST_MUTEX_DEFINE_STATIC(featurelock);
74
75 struct feature_sub {
76         struct ast_channel *owner;
77         int inthreeway;
78         int pfd;
79         int timingfdbackup;
80         int alertpipebackup[2];
81 };
82
83 static struct feature_pvt {
84         ast_mutex_t lock;                       /* Channel private lock */
85         char tech[AST_MAX_EXTENSION];           /* Technology to abstract */
86         char dest[AST_MAX_EXTENSION];           /* Destination to abstract */
87         struct ast_channel *subchan;
88         struct feature_sub subs[3];             /* Subs */
89         struct ast_channel *owner;              /* Current Master Channel */
90         struct feature_pvt *next;               /* Next entity */
91 } *features = NULL;
92
93 #define SUB_REAL        0                       /* Active call */
94 #define SUB_CALLWAIT    1                       /* Call-Waiting call on hold */
95 #define SUB_THREEWAY    2                       /* Three-way call */
96
97 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause);
98 static int features_digit(struct ast_channel *ast, char digit);
99 static int features_call(struct ast_channel *ast, char *dest, int timeout);
100 static int features_hangup(struct ast_channel *ast);
101 static int features_answer(struct ast_channel *ast);
102 static struct ast_frame *features_read(struct ast_channel *ast);
103 static int features_write(struct ast_channel *ast, struct ast_frame *f);
104 static int features_indicate(struct ast_channel *ast, int condition);
105 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
106
107 static const struct ast_channel_tech features_tech = {
108         .type = type,
109         .description = tdesc,
110         .capabilities = -1,
111         .requester = features_request,
112         .send_digit = features_digit,
113         .call = features_call,
114         .hangup = features_hangup,
115         .answer = features_answer,
116         .read = features_read,
117         .write = features_write,
118         .exception = features_read,
119         .indicate = features_indicate,
120         .fixup = features_fixup,
121 };
122
123 static inline void init_sub(struct feature_sub *sub)
124 {
125         sub->inthreeway = 0;
126         sub->pfd = -1;
127         sub->timingfdbackup = -1;
128         sub->alertpipebackup[0] = sub->alertpipebackup[1] = -1;
129 }
130
131 static inline int indexof(struct feature_pvt *p, struct ast_channel *owner, int nullok)
132 {
133         int x;
134         if (!owner) {
135                 ast_log(LOG_WARNING, "indexof called on NULL owner??\n");
136                 return -1;
137         }
138         for (x=0; x<3; x++) {
139                 if (owner == p->subs[x].owner)
140                         return x;
141         }
142         return -1;
143 }
144
145 #if 0
146 static void wakeup_sub(struct feature_pvt *p, int a)
147 {
148         struct ast_frame null = { AST_FRAME_NULL, };
149         for (;;) {
150                 if (p->subs[a].owner) {
151                         if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
152                                 ast_mutex_unlock(&p->lock);
153                                 usleep(1);
154                                 ast_mutex_lock(&p->lock);
155                         } else {
156                                 ast_queue_frame(p->subs[a].owner, &null);
157                                 ast_mutex_unlock(&p->subs[a].owner->lock);
158                                 break;
159                         }
160                 } else
161                         break;
162         }
163 }
164 #endif
165
166 static void restore_channel(struct feature_pvt *p, int index)
167 {
168         /* Restore timing/alertpipe */
169         p->subs[index].owner->timingfd = p->subs[index].timingfdbackup;
170         p->subs[index].owner->alertpipe[0] = p->subs[index].alertpipebackup[0];
171         p->subs[index].owner->alertpipe[1] = p->subs[index].alertpipebackup[1];
172         p->subs[index].owner->fds[AST_MAX_FDS-1] = p->subs[index].alertpipebackup[0];
173         p->subs[index].owner->fds[AST_MAX_FDS-2] = p->subs[index].timingfdbackup;
174 }
175
176 static void update_features(struct feature_pvt *p, int index)
177 {
178         int x;
179         if (p->subs[index].owner) {
180                 for (x=0; x<AST_MAX_FDS; x++) {
181                         if (index) 
182                                 p->subs[index].owner->fds[x] = -1;
183                         else
184                                 p->subs[index].owner->fds[x] = p->subchan->fds[x];
185                 }
186                 if (!index) {
187                         /* Copy timings from master channel */
188                         p->subs[index].owner->timingfd = p->subchan->timingfd;
189                         p->subs[index].owner->alertpipe[0] = p->subchan->alertpipe[0];
190                         p->subs[index].owner->alertpipe[1] = p->subchan->alertpipe[1];
191                         if (p->subs[index].owner->nativeformats != p->subchan->readformat) {
192                                 p->subs[index].owner->nativeformats = p->subchan->readformat;
193                                 if (p->subs[index].owner->readformat)
194                                         ast_set_read_format(p->subs[index].owner, p->subs[index].owner->readformat);
195                                 if (p->subs[index].owner->writeformat)
196                                         ast_set_write_format(p->subs[index].owner, p->subs[index].owner->writeformat);
197                         }
198                 } else{
199                         restore_channel(p, index);
200                 }
201         }
202 }
203
204 #if 0
205 static void swap_subs(struct feature_pvt *p, int a, int b)
206 {
207         int tinthreeway;
208         struct ast_channel *towner;
209
210         ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
211
212         towner = p->subs[a].owner;
213         tinthreeway = p->subs[a].inthreeway;
214
215         p->subs[a].owner = p->subs[b].owner;
216         p->subs[a].inthreeway = p->subs[b].inthreeway;
217
218         p->subs[b].owner = towner;
219         p->subs[b].inthreeway = tinthreeway;
220         update_features(p,a);
221         update_features(p,b);
222         wakeup_sub(p, a);
223         wakeup_sub(p, b);
224 }
225 #endif
226
227 static int features_answer(struct ast_channel *ast)
228 {
229         struct feature_pvt *p = ast->tech_pvt;
230         int res = -1;
231         int x;
232
233         ast_mutex_lock(&p->lock);
234         x = indexof(p, ast, 0);
235         if (!x && p->subchan)
236                 res = ast_answer(p->subchan);
237         ast_mutex_unlock(&p->lock);
238         return res;
239 }
240
241 static struct ast_frame  *features_read(struct ast_channel *ast)
242 {
243         static struct ast_frame null_frame = { AST_FRAME_NULL, };
244         struct feature_pvt *p = ast->tech_pvt;
245         struct ast_frame *f;
246         int x;
247         
248         f = &null_frame;
249         ast_mutex_lock(&p->lock);
250         x = indexof(p, ast, 0);
251         if (!x && p->subchan) {
252                 update_features(p, x);
253                 f = ast_read(p->subchan);
254         }
255         ast_mutex_unlock(&p->lock);
256         return f;
257 }
258
259 static int features_write(struct ast_channel *ast, struct ast_frame *f)
260 {
261         struct feature_pvt *p = ast->tech_pvt;
262         int res = -1;
263         int x;
264
265         ast_mutex_lock(&p->lock);
266         x = indexof(p, ast, 0);
267         if (!x && p->subchan)
268                 res = ast_write(p->subchan, f);
269         ast_mutex_unlock(&p->lock);
270         return res;
271 }
272
273 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
274 {
275         struct feature_pvt *p = newchan->tech_pvt;
276         int x;
277
278         ast_mutex_lock(&p->lock);
279         if (p->owner == oldchan)
280                 p->owner = newchan;
281         for (x = 0; x < 3; x++) {
282                 if (p->subs[x].owner == oldchan)
283                         p->subs[x].owner = newchan;
284         }
285         ast_mutex_unlock(&p->lock);
286         return 0;
287 }
288
289 static int features_indicate(struct ast_channel *ast, int condition)
290 {
291         struct feature_pvt *p = ast->tech_pvt;
292         int res = -1;
293         int x;
294
295         /* Queue up a frame representing the indication as a control frame */
296         ast_mutex_lock(&p->lock);
297         x = indexof(p, ast, 0);
298         if (!x && p->subchan)
299                 res = ast_indicate(p->subchan, condition);
300         ast_mutex_unlock(&p->lock);
301         return res;
302 }
303
304 static int features_digit(struct ast_channel *ast, char digit)
305 {
306         struct feature_pvt *p = ast->tech_pvt;
307         int res = -1;
308         int x;
309
310         /* Queue up a frame representing the indication as a control frame */
311         ast_mutex_lock(&p->lock);
312         x = indexof(p, ast, 0);
313         if (!x && p->subchan)
314                 res = ast_senddigit(p->subchan, digit);
315         ast_mutex_unlock(&p->lock);
316         return res;
317 }
318
319 static int features_call(struct ast_channel *ast, char *dest, int timeout)
320 {
321         struct feature_pvt *p = ast->tech_pvt;
322         int res = -1;
323         int x;
324         char *dest2;
325                 
326         dest2 = strchr(dest, '/');
327         if (dest2) {
328                 ast_mutex_lock(&p->lock);
329                 x = indexof(p, ast, 0);
330                 if (!x && p->subchan) {
331                         if (p->owner->cid.cid_num)
332                                 p->subchan->cid.cid_num = strdup(p->owner->cid.cid_num);
333                         else 
334                                 p->subchan->cid.cid_num = NULL;
335                 
336                         if (p->owner->cid.cid_name)
337                                 p->subchan->cid.cid_name = strdup(p->owner->cid.cid_name);
338                         else 
339                                 p->subchan->cid.cid_name = NULL;
340                 
341                         if (p->owner->cid.cid_rdnis)
342                                 p->subchan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
343                         else
344                                 p->subchan->cid.cid_rdnis = NULL;
345                 
346                         if (p->owner->cid.cid_ani)
347                                 p->subchan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
348                         else
349                                 p->subchan->cid.cid_ani = NULL;
350                 
351                         p->subchan->cid.cid_pres = p->owner->cid.cid_pres;
352                         strncpy(p->subchan->language, p->owner->language, sizeof(p->subchan->language) - 1);
353                         strncpy(p->subchan->accountcode, p->owner->accountcode, sizeof(p->subchan->accountcode) - 1);
354                         p->subchan->cdrflags = p->owner->cdrflags;
355                         res = ast_call(p->subchan, dest2, timeout);
356                         update_features(p, x);
357                 } else
358                         ast_log(LOG_NOTICE, "Uhm yah, not quite there with the call waiting...\n");
359                 ast_mutex_unlock(&p->lock);
360         }
361         return res;
362 }
363
364 static int features_hangup(struct ast_channel *ast)
365 {
366         struct feature_pvt *p = ast->tech_pvt;
367         struct feature_pvt *cur, *prev=NULL;
368         int x;
369
370         ast_mutex_lock(&p->lock);
371         x = indexof(p, ast, 0);
372         if (x > -1) {
373                 restore_channel(p, x);
374                 p->subs[x].owner = NULL;
375                 /* XXX Re-arrange, unconference, etc XXX */
376         }
377         ast->tech_pvt = NULL;
378         
379         
380         if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
381                 ast_mutex_unlock(&p->lock);
382                 /* Remove from list */
383                 ast_mutex_lock(&featurelock);
384                 cur = features;
385                 while(cur) {
386                         if (cur == p) {
387                                 if (prev)
388                                         prev->next = cur->next;
389                                 else
390                                         features = cur->next;
391                                 break;
392                         }
393                         prev = cur;
394                         cur = cur->next;
395                 }
396                 ast_mutex_unlock(&featurelock);
397                 ast_mutex_lock(&p->lock);
398                 /* And destroy */
399                 if (p->subchan)
400                         ast_hangup(p->subchan);
401                 ast_mutex_unlock(&p->lock);
402                 ast_mutex_destroy(&p->lock);
403                 free(p);
404                 return 0;
405         }
406         ast_mutex_unlock(&p->lock);
407         return 0;
408 }
409
410 static struct feature_pvt *features_alloc(char *data, int format)
411 {
412         struct feature_pvt *tmp;
413         char *dest=NULL;
414         char *tech;
415         int x;
416         int status;
417         struct ast_channel *chan;
418         
419         tech = ast_strdupa(data);
420         if (tech) {
421                 dest = strchr(tech, '/');
422                 if (dest) {
423                         *dest = '\0';
424                         dest++;
425                 }
426         }
427         if (!tech || !dest) {
428                 ast_log(LOG_NOTICE, "Format for feature channel is Feature/Tech/Dest ('%s' not valid)!\n", 
429                         data);
430                 return NULL;
431         }
432         ast_mutex_lock(&featurelock);
433         tmp = features;
434         while(tmp) {
435                 if (!strcasecmp(tmp->tech, tech) && !strcmp(tmp->dest, dest))
436                         break;
437                 tmp = tmp->next;
438         }
439         ast_mutex_unlock(&featurelock);
440         if (!tmp) {
441                 chan = ast_request(tech, format, dest, &status);
442                 if (!chan) {
443                         ast_log(LOG_NOTICE, "Unable to allocate subchannel '%s/%s'\n", tech, dest);
444                         return NULL;
445                 }
446                 tmp = malloc(sizeof(struct feature_pvt));
447                 if (tmp) {
448                         memset(tmp, 0, sizeof(struct feature_pvt));
449                         for (x=0;x<3;x++)
450                                 init_sub(tmp->subs + x);
451                         ast_mutex_init(&tmp->lock);
452                         strncpy(tmp->tech, tech, sizeof(tmp->tech) - 1);
453                         strncpy(tmp->dest, dest, sizeof(tmp->dest) - 1);
454                         tmp->subchan = chan;
455                         ast_mutex_lock(&featurelock);
456                         tmp->next = features;
457                         features = tmp;
458                         ast_mutex_unlock(&featurelock);
459                 }
460         }
461         return tmp;
462 }
463
464 static struct ast_channel *features_new(struct feature_pvt *p, int state, int index)
465 {
466         struct ast_channel *tmp;
467         int x,y;
468         if (!p->subchan) {
469                 ast_log(LOG_WARNING, "Called upon channel with no subchan:(\n");
470                 return NULL;
471         }
472         if (p->subs[index].owner) {
473                 ast_log(LOG_WARNING, "Called to put index %d already there!\n", index);
474                 return NULL;
475         }
476         tmp = ast_channel_alloc(0);
477         if (!tmp) {
478                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
479                 return NULL;
480         }
481         tmp->tech = &features_tech;
482         for (x=1;x<4;x++) {
483                 snprintf(tmp->name, sizeof(tmp->name), "Feature/%s/%s-%d", p->tech, p->dest, x);
484                 for (y=0;y<3;y++) {
485                         if (y == index)
486                                 continue;
487                         if (p->subs[y].owner && !strcasecmp(p->subs[y].owner->name, tmp->name))
488                                 break;
489                 }
490                 if (y >= 3)
491                         break;
492         }
493         tmp->type = type;
494         ast_setstate(tmp, state);
495         tmp->writeformat = p->subchan->writeformat;
496         tmp->rawwriteformat = p->subchan->rawwriteformat;
497         tmp->readformat = p->subchan->readformat;
498         tmp->rawreadformat = p->subchan->rawreadformat;
499         tmp->nativeformats = p->subchan->readformat;
500         tmp->tech_pvt = p;
501         p->subs[index].owner = tmp;
502         if (!p->owner)
503                 p->owner = tmp;
504         ast_mutex_lock(&usecnt_lock);
505         usecnt++;
506         ast_mutex_unlock(&usecnt_lock);
507         ast_update_use_count();
508         return tmp;
509 }
510
511
512 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause)
513 {
514         struct feature_pvt *p;
515         struct ast_channel *chan = NULL;
516
517         p = features_alloc(data, format);
518         if (p && !p->subs[SUB_REAL].owner)
519                 chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
520         if (chan)
521                 update_features(p,SUB_REAL);
522         return chan;
523 }
524
525 static int features_show(int fd, int argc, char **argv)
526 {
527         struct feature_pvt *p;
528
529         if (argc != 3)
530                 return RESULT_SHOWUSAGE;
531         ast_mutex_lock(&featurelock);
532         p = features;
533         while(p) {
534                 ast_mutex_lock(&p->lock);
535                 ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
536                 ast_mutex_unlock(&p->lock);
537                 p = p->next;
538         }
539         if (!features)
540                 ast_cli(fd, "No feature channels in use\n");
541         ast_mutex_unlock(&featurelock);
542         return RESULT_SUCCESS;
543 }
544
545 static char show_features_usage[] = 
546 "Usage: feature show channels\n"
547 "       Provides summary information on feature channels.\n";
548
549 static struct ast_cli_entry cli_show_features = {
550         { "feature", "show", "channels", NULL }, features_show, 
551         "Show status of feature channels", show_features_usage, NULL };
552
553 int load_module()
554 {
555         /* Make sure we can register our sip channel type */
556         if (ast_channel_register(&features_tech)) {
557                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
558                 return -1;
559         }
560         ast_cli_register(&cli_show_features);
561         return 0;
562 }
563
564 int reload()
565 {
566         return 0;
567 }
568
569 int unload_module()
570 {
571         struct feature_pvt *p;
572         /* First, take us out of the channel loop */
573         ast_cli_unregister(&cli_show_features);
574         ast_channel_unregister(&features_tech);
575         if (!ast_mutex_lock(&featurelock)) {
576                 /* Hangup all interfaces if they have an owner */
577                 p = features;
578                 while(p) {
579                         if (p->owner)
580                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
581                         p = p->next;
582                 }
583                 features = NULL;
584                 ast_mutex_unlock(&featurelock);
585         } else {
586                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
587                 return -1;
588         }               
589         return 0;
590 }
591
592 int usecount()
593 {
594         return usecnt;
595 }
596
597 char *key()
598 {
599         return ASTERISK_GPL_KEY;
600 }
601
602 char *description()
603 {
604         return (char *) desc;
605 }
606