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