Merge Rizzo's waitfor update (bug #4584)
[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 <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/socket.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <netdb.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <sys/signal.h>
41
42 #include "asterisk.h"
43
44 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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
65 static const char desc[] = "Feature Proxy Channel";
66 static const char type[] = "Feature";
67 static const char tdesc[] = "Feature Proxy Channel Driver";
68
69 static int usecnt =0;
70 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
71
72 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
73
74 struct feature_sub {
75         struct ast_channel *owner;
76         int inthreeway;
77         int pfd;
78         int timingfdbackup;
79         int alertpipebackup[2];
80 };
81
82 struct feature_pvt {
83         ast_mutex_t lock;                       /* Channel private lock */
84         char tech[AST_MAX_EXTENSION];           /* Technology to abstract */
85         char dest[AST_MAX_EXTENSION];           /* Destination to abstract */
86         struct ast_channel *subchan;
87         struct feature_sub subs[3];             /* Subs */
88         struct ast_channel *owner;              /* Current Master Channel */
89         AST_LIST_ENTRY(feature_pvt) list;       /* Next entity */
90 };
91
92 static AST_LIST_HEAD_STATIC(features, feature_pvt);
93
94 #define SUB_REAL        0                       /* Active call */
95 #define SUB_CALLWAIT    1                       /* Call-Waiting call on hold */
96 #define SUB_THREEWAY    2                       /* Three-way call */
97
98 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause);
99 static int features_digit(struct ast_channel *ast, char digit);
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);
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 = type,
110         .description = tdesc,
111         .capabilities = -1,
112         .requester = features_request,
113         .send_digit = features_digit,
114         .call = features_call,
115         .hangup = features_hangup,
116         .answer = features_answer,
117         .read = features_read,
118         .write = features_write,
119         .exception = features_read,
120         .indicate = features_indicate,
121         .fixup = features_fixup,
122 };
123
124 static inline void init_sub(struct feature_sub *sub)
125 {
126         sub->inthreeway = 0;
127         sub->pfd = -1;
128         sub->timingfdbackup = -1;
129         sub->alertpipebackup[0] = sub->alertpipebackup[1] = -1;
130 }
131
132 static inline int indexof(struct feature_pvt *p, struct ast_channel *owner, int nullok)
133 {
134         int x;
135         if (!owner) {
136                 ast_log(LOG_WARNING, "indexof called on NULL owner??\n");
137                 return -1;
138         }
139         for (x=0; x<3; x++) {
140                 if (owner == p->subs[x].owner)
141                         return x;
142         }
143         return -1;
144 }
145
146 #if 0
147 static void wakeup_sub(struct feature_pvt *p, int a)
148 {
149         struct ast_frame null = { AST_FRAME_NULL, };
150         for (;;) {
151                 if (p->subs[a].owner) {
152                         if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
153                                 ast_mutex_unlock(&p->lock);
154                                 usleep(1);
155                                 ast_mutex_lock(&p->lock);
156                         } else {
157                                 ast_queue_frame(p->subs[a].owner, &null);
158                                 ast_mutex_unlock(&p->subs[a].owner->lock);
159                                 break;
160                         }
161                 } else
162                         break;
163         }
164 }
165 #endif
166
167 static void restore_channel(struct feature_pvt *p, int index)
168 {
169         /* Restore timing/alertpipe */
170         p->subs[index].owner->timingfd = p->subs[index].timingfdbackup;
171         p->subs[index].owner->alertpipe[0] = p->subs[index].alertpipebackup[0];
172         p->subs[index].owner->alertpipe[1] = p->subs[index].alertpipebackup[1];
173         p->subs[index].owner->fds[AST_ALERT_FD] = p->subs[index].alertpipebackup[0];
174         p->subs[index].owner->fds[AST_TIMING_FD] = p->subs[index].timingfdbackup;
175 }
176
177 static void update_features(struct feature_pvt *p, int index)
178 {
179         int x;
180         if (p->subs[index].owner) {
181                 for (x=0; x<AST_MAX_FDS; x++) {
182                         if (index) 
183                                 p->subs[index].owner->fds[x] = -1;
184                         else
185                                 p->subs[index].owner->fds[x] = p->subchan->fds[x];
186                 }
187                 if (!index) {
188                         /* Copy timings from master channel */
189                         p->subs[index].owner->timingfd = p->subchan->timingfd;
190                         p->subs[index].owner->alertpipe[0] = p->subchan->alertpipe[0];
191                         p->subs[index].owner->alertpipe[1] = p->subchan->alertpipe[1];
192                         if (p->subs[index].owner->nativeformats != p->subchan->readformat) {
193                                 p->subs[index].owner->nativeformats = p->subchan->readformat;
194                                 if (p->subs[index].owner->readformat)
195                                         ast_set_read_format(p->subs[index].owner, p->subs[index].owner->readformat);
196                                 if (p->subs[index].owner->writeformat)
197                                         ast_set_write_format(p->subs[index].owner, p->subs[index].owner->writeformat);
198                         }
199                 } else{
200                         restore_channel(p, index);
201                 }
202         }
203 }
204
205 #if 0
206 static void swap_subs(struct feature_pvt *p, int a, int b)
207 {
208         int tinthreeway;
209         struct ast_channel *towner;
210
211         ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
212
213         towner = p->subs[a].owner;
214         tinthreeway = p->subs[a].inthreeway;
215
216         p->subs[a].owner = p->subs[b].owner;
217         p->subs[a].inthreeway = p->subs[b].inthreeway;
218
219         p->subs[b].owner = towner;
220         p->subs[b].inthreeway = tinthreeway;
221         update_features(p,a);
222         update_features(p,b);
223         wakeup_sub(p, a);
224         wakeup_sub(p, b);
225 }
226 #endif
227
228 static int features_answer(struct ast_channel *ast)
229 {
230         struct feature_pvt *p = ast->tech_pvt;
231         int res = -1;
232         int x;
233
234         ast_mutex_lock(&p->lock);
235         x = indexof(p, ast, 0);
236         if (!x && p->subchan)
237                 res = ast_answer(p->subchan);
238         ast_mutex_unlock(&p->lock);
239         return res;
240 }
241
242 static struct ast_frame  *features_read(struct ast_channel *ast)
243 {
244         static struct ast_frame null_frame = { AST_FRAME_NULL, };
245         struct feature_pvt *p = ast->tech_pvt;
246         struct ast_frame *f;
247         int x;
248         
249         f = &null_frame;
250         ast_mutex_lock(&p->lock);
251         x = indexof(p, ast, 0);
252         if (!x && p->subchan) {
253                 update_features(p, x);
254                 f = ast_read(p->subchan);
255         }
256         ast_mutex_unlock(&p->lock);
257         return f;
258 }
259
260 static int features_write(struct ast_channel *ast, struct ast_frame *f)
261 {
262         struct feature_pvt *p = ast->tech_pvt;
263         int res = -1;
264         int x;
265
266         ast_mutex_lock(&p->lock);
267         x = indexof(p, ast, 0);
268         if (!x && p->subchan)
269                 res = ast_write(p->subchan, f);
270         ast_mutex_unlock(&p->lock);
271         return res;
272 }
273
274 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
275 {
276         struct feature_pvt *p = newchan->tech_pvt;
277         int x;
278
279         ast_mutex_lock(&p->lock);
280         if (p->owner == oldchan)
281                 p->owner = newchan;
282         for (x = 0; x < 3; x++) {
283                 if (p->subs[x].owner == oldchan)
284                         p->subs[x].owner = newchan;
285         }
286         ast_mutex_unlock(&p->lock);
287         return 0;
288 }
289
290 static int features_indicate(struct ast_channel *ast, int condition)
291 {
292         struct feature_pvt *p = ast->tech_pvt;
293         int res = -1;
294         int x;
295
296         /* Queue up a frame representing the indication as a control frame */
297         ast_mutex_lock(&p->lock);
298         x = indexof(p, ast, 0);
299         if (!x && p->subchan)
300                 res = ast_indicate(p->subchan, condition);
301         ast_mutex_unlock(&p->lock);
302         return res;
303 }
304
305 static int features_digit(struct ast_channel *ast, char digit)
306 {
307         struct feature_pvt *p = ast->tech_pvt;
308         int res = -1;
309         int x;
310
311         /* Queue up a frame representing the indication as a control frame */
312         ast_mutex_lock(&p->lock);
313         x = indexof(p, ast, 0);
314         if (!x && p->subchan)
315                 res = ast_senddigit(p->subchan, digit);
316         ast_mutex_unlock(&p->lock);
317         return res;
318 }
319
320 static int features_call(struct ast_channel *ast, char *dest, int timeout)
321 {
322         struct feature_pvt *p = ast->tech_pvt;
323         int res = -1;
324         int x;
325         char *dest2;
326                 
327         dest2 = strchr(dest, '/');
328         if (dest2) {
329                 ast_mutex_lock(&p->lock);
330                 x = indexof(p, ast, 0);
331                 if (!x && p->subchan) {
332                         if (p->owner->cid.cid_num)
333                                 p->subchan->cid.cid_num = strdup(p->owner->cid.cid_num);
334                         else 
335                                 p->subchan->cid.cid_num = NULL;
336                 
337                         if (p->owner->cid.cid_name)
338                                 p->subchan->cid.cid_name = strdup(p->owner->cid.cid_name);
339                         else 
340                                 p->subchan->cid.cid_name = NULL;
341                 
342                         if (p->owner->cid.cid_rdnis)
343                                 p->subchan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
344                         else
345                                 p->subchan->cid.cid_rdnis = NULL;
346                 
347                         if (p->owner->cid.cid_ani)
348                                 p->subchan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
349                         else
350                                 p->subchan->cid.cid_ani = NULL;
351                 
352                         p->subchan->cid.cid_pres = p->owner->cid.cid_pres;
353                         strncpy(p->subchan->language, p->owner->language, sizeof(p->subchan->language) - 1);
354                         strncpy(p->subchan->accountcode, p->owner->accountcode, sizeof(p->subchan->accountcode) - 1);
355                         p->subchan->cdrflags = p->owner->cdrflags;
356                         res = ast_call(p->subchan, dest2, timeout);
357                         update_features(p, x);
358                 } else
359                         ast_log(LOG_NOTICE, "Uhm yah, not quite there with the call waiting...\n");
360                 ast_mutex_unlock(&p->lock);
361         }
362         return res;
363 }
364
365 static int features_hangup(struct ast_channel *ast)
366 {
367         struct feature_pvt *p = ast->tech_pvt;
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         if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
380                 ast_mutex_unlock(&p->lock);
381                 /* Remove from list */
382                 AST_LIST_LOCK(&features);
383                 AST_LIST_REMOVE(&features, p, list);
384                 AST_LIST_UNLOCK(&features);
385                 ast_mutex_lock(&p->lock);
386                 /* And destroy */
387                 if (p->subchan)
388                         ast_hangup(p->subchan);
389                 ast_mutex_unlock(&p->lock);
390                 ast_mutex_destroy(&p->lock);
391                 free(p);
392                 return 0;
393         }
394         ast_mutex_unlock(&p->lock);
395         return 0;
396 }
397
398 static struct feature_pvt *features_alloc(char *data, int format)
399 {
400         struct feature_pvt *tmp;
401         char *dest=NULL;
402         char *tech;
403         int x;
404         int status;
405         struct ast_channel *chan;
406         
407         tech = ast_strdupa(data);
408         if (tech) {
409                 dest = strchr(tech, '/');
410                 if (dest) {
411                         *dest = '\0';
412                         dest++;
413                 }
414         }
415         if (!tech || !dest) {
416                 ast_log(LOG_NOTICE, "Format for feature channel is Feature/Tech/Dest ('%s' not valid)!\n", 
417                         data);
418                 return NULL;
419         }
420         AST_LIST_LOCK(&features);
421         AST_LIST_TRAVERSE(&features, tmp, list) {
422                 if (!strcasecmp(tmp->tech, tech) && !strcmp(tmp->dest, dest))
423                         break;
424         }
425         AST_LIST_UNLOCK(&features);
426         if (!tmp) {
427                 chan = ast_request(tech, format, dest, &status);
428                 if (!chan) {
429                         ast_log(LOG_NOTICE, "Unable to allocate subchannel '%s/%s'\n", tech, dest);
430                         return NULL;
431                 }
432                 tmp = malloc(sizeof(struct feature_pvt));
433                 if (tmp) {
434                         memset(tmp, 0, sizeof(struct feature_pvt));
435                         for (x=0;x<3;x++)
436                                 init_sub(tmp->subs + x);
437                         ast_mutex_init(&tmp->lock);
438                         strncpy(tmp->tech, tech, sizeof(tmp->tech) - 1);
439                         strncpy(tmp->dest, dest, sizeof(tmp->dest) - 1);
440                         tmp->subchan = chan;
441                         AST_LIST_LOCK(&features);
442                         AST_LIST_INSERT_HEAD(&features, tmp, list);
443                         AST_LIST_UNLOCK(&features);
444                 }
445         }
446         return tmp;
447 }
448
449 static struct ast_channel *features_new(struct feature_pvt *p, int state, int index)
450 {
451         struct ast_channel *tmp;
452         int x,y;
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         tmp = ast_channel_alloc(0);
462         if (!tmp) {
463                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
464                 return NULL;
465         }
466         tmp->tech = &features_tech;
467         for (x=1;x<4;x++) {
468                 snprintf(tmp->name, sizeof(tmp->name), "Feature/%s/%s-%d", p->tech, p->dest, x);
469                 for (y=0;y<3;y++) {
470                         if (y == index)
471                                 continue;
472                         if (p->subs[y].owner && !strcasecmp(p->subs[y].owner->name, tmp->name))
473                                 break;
474                 }
475                 if (y >= 3)
476                         break;
477         }
478         tmp->type = type;
479         ast_setstate(tmp, state);
480         tmp->writeformat = p->subchan->writeformat;
481         tmp->rawwriteformat = p->subchan->rawwriteformat;
482         tmp->readformat = p->subchan->readformat;
483         tmp->rawreadformat = p->subchan->rawreadformat;
484         tmp->nativeformats = p->subchan->readformat;
485         tmp->tech_pvt = p;
486         p->subs[index].owner = tmp;
487         if (!p->owner)
488                 p->owner = tmp;
489         ast_mutex_lock(&usecnt_lock);
490         usecnt++;
491         ast_mutex_unlock(&usecnt_lock);
492         ast_update_use_count();
493         return tmp;
494 }
495
496
497 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause)
498 {
499         struct feature_pvt *p;
500         struct ast_channel *chan = NULL;
501
502         p = features_alloc(data, format);
503         if (p && !p->subs[SUB_REAL].owner)
504                 chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
505         if (chan)
506                 update_features(p,SUB_REAL);
507         return chan;
508 }
509
510 static int features_show(int fd, int argc, char **argv)
511 {
512         struct feature_pvt *p;
513
514         if (argc != 3)
515                 return RESULT_SHOWUSAGE;
516
517         if (AST_LIST_EMPTY(&features)) {
518                 ast_cli(fd, "No feature channels in use\n");
519                 return RESULT_SUCCESS;
520         }
521
522         AST_LIST_LOCK(&features);
523         AST_LIST_TRAVERSE(&features, p, list) {
524                 ast_mutex_lock(&p->lock);
525                 ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
526                 ast_mutex_unlock(&p->lock);
527         }
528         AST_LIST_UNLOCK(&features);
529         return RESULT_SUCCESS;
530 }
531
532 static char show_features_usage[] = 
533 "Usage: feature show channels\n"
534 "       Provides summary information on feature channels.\n";
535
536 static struct ast_cli_entry cli_show_features = {
537         { "feature", "show", "channels", NULL }, features_show, 
538         "Show status of feature channels", show_features_usage, NULL };
539
540 int load_module()
541 {
542         /* Make sure we can register our sip channel type */
543         if (ast_channel_register(&features_tech)) {
544                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
545                 return -1;
546         }
547         ast_cli_register(&cli_show_features);
548         return 0;
549 }
550
551 int reload()
552 {
553         return 0;
554 }
555
556 int unload_module()
557 {
558         struct feature_pvt *p;
559         
560         /* First, take us out of the channel loop */
561         ast_cli_unregister(&cli_show_features);
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                 free(p);
572         }
573         AST_LIST_TRAVERSE_SAFE_END
574         AST_LIST_UNLOCK(&features);
575         
576         return 0;
577 }
578
579 int usecount()
580 {
581         return usecnt;
582 }
583
584 char *key()
585 {
586         return ASTERISK_GPL_KEY;
587 }
588
589 char *description()
590 {
591         return (char *) desc;
592 }
593