define a global null_frame object so when queueing a null frame, you don't
[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         struct feature_pvt *p = ast->tech_pvt;
245         struct ast_frame *f;
246         int x;
247         
248         f = &ast_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         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                         strncpy(tmp->tech, tech, sizeof(tmp->tech) - 1);
438                         strncpy(tmp->dest, dest, sizeof(tmp->dest) - 1);
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         if (!p->subchan) {
453                 ast_log(LOG_WARNING, "Called upon channel with no subchan:(\n");
454                 return NULL;
455         }
456         if (p->subs[index].owner) {
457                 ast_log(LOG_WARNING, "Called to put index %d already there!\n", index);
458                 return NULL;
459         }
460         tmp = ast_channel_alloc(0);
461         if (!tmp) {
462                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
463                 return NULL;
464         }
465         tmp->tech = &features_tech;
466         for (x=1;x<4;x++) {
467                 snprintf(tmp->name, sizeof(tmp->name), "Feature/%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, tmp->name))
472                                 break;
473                 }
474                 if (y >= 3)
475                         break;
476         }
477         tmp->type = type;
478         ast_setstate(tmp, state);
479         tmp->writeformat = p->subchan->writeformat;
480         tmp->rawwriteformat = p->subchan->rawwriteformat;
481         tmp->readformat = p->subchan->readformat;
482         tmp->rawreadformat = p->subchan->rawreadformat;
483         tmp->nativeformats = p->subchan->readformat;
484         tmp->tech_pvt = p;
485         p->subs[index].owner = tmp;
486         if (!p->owner)
487                 p->owner = tmp;
488         ast_mutex_lock(&usecnt_lock);
489         usecnt++;
490         ast_mutex_unlock(&usecnt_lock);
491         ast_update_use_count();
492         return tmp;
493 }
494
495
496 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause)
497 {
498         struct feature_pvt *p;
499         struct ast_channel *chan = NULL;
500
501         p = features_alloc(data, format);
502         if (p && !p->subs[SUB_REAL].owner)
503                 chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
504         if (chan)
505                 update_features(p,SUB_REAL);
506         return chan;
507 }
508
509 static int features_show(int fd, int argc, char **argv)
510 {
511         struct feature_pvt *p;
512
513         if (argc != 3)
514                 return RESULT_SHOWUSAGE;
515
516         if (AST_LIST_EMPTY(&features)) {
517                 ast_cli(fd, "No feature channels in use\n");
518                 return RESULT_SUCCESS;
519         }
520
521         AST_LIST_LOCK(&features);
522         AST_LIST_TRAVERSE(&features, p, list) {
523                 ast_mutex_lock(&p->lock);
524                 ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
525                 ast_mutex_unlock(&p->lock);
526         }
527         AST_LIST_UNLOCK(&features);
528         return RESULT_SUCCESS;
529 }
530
531 static char show_features_usage[] = 
532 "Usage: feature show channels\n"
533 "       Provides summary information on feature channels.\n";
534
535 static struct ast_cli_entry cli_show_features = {
536         { "feature", "show", "channels", NULL }, features_show, 
537         "Show status of feature channels", show_features_usage, NULL };
538
539 int load_module()
540 {
541         /* Make sure we can register our sip channel type */
542         if (ast_channel_register(&features_tech)) {
543                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
544                 return -1;
545         }
546         ast_cli_register(&cli_show_features);
547         return 0;
548 }
549
550 int reload()
551 {
552         return 0;
553 }
554
555 int unload_module()
556 {
557         struct feature_pvt *p;
558         
559         /* First, take us out of the channel loop */
560         ast_cli_unregister(&cli_show_features);
561         ast_channel_unregister(&features_tech);
562         
563         if (!AST_LIST_LOCK(&features))
564                 return -1;
565         /* Hangup all interfaces if they have an owner */
566         AST_LIST_TRAVERSE_SAFE_BEGIN(&features, p, list) {
567                 if (p->owner)
568                         ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
569                 AST_LIST_REMOVE_CURRENT(&features, list);
570                 free(p);
571         }
572         AST_LIST_TRAVERSE_SAFE_END
573         AST_LIST_UNLOCK(&features);
574         
575         return 0;
576 }
577
578 int usecount()
579 {
580         return usecnt;
581 }
582
583 char *key()
584 {
585         return ASTERISK_GPL_KEY;
586 }
587
588 char *description()
589 {
590         return (char *) desc;
591 }
592