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