update MANY more files with proper copyright/license info (thanks Ian!)
[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 /*
20  *
21  * feature Proxy Channel
22  * 
23  * *** Experimental code ****
24  * 
25  */
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/socket.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <fcntl.h>
34 #include <netdb.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <sys/signal.h>
38
39 #include "asterisk.h"
40
41 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42
43 #include "asterisk/lock.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/config.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/module.h"
48 #include "asterisk/pbx.h"
49 #include "asterisk/options.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/sched.h"
52 #include "asterisk/io.h"
53 #include "asterisk/rtp.h"
54 #include "asterisk/acl.h"
55 #include "asterisk/callerid.h"
56 #include "asterisk/file.h"
57 #include "asterisk/cli.h"
58 #include "asterisk/app.h"
59 #include "asterisk/musiconhold.h"
60 #include "asterisk/manager.h"
61
62 static const char desc[] = "Feature Proxy Channel";
63 static const char type[] = "Feature";
64 static const char tdesc[] = "Feature Proxy Channel Driver";
65
66 static int usecnt =0;
67 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
68
69 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
70
71 /* Protect the interface list (of feature_pvt's) */
72 AST_MUTEX_DEFINE_STATIC(featurelock);
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 static 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         struct feature_pvt *next;               /* Next entity */
90 } *features = NULL;
91
92 #define SUB_REAL        0                       /* Active call */
93 #define SUB_CALLWAIT    1                       /* Call-Waiting call on hold */
94 #define SUB_THREEWAY    2                       /* Three-way call */
95
96 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause);
97 static int features_digit(struct ast_channel *ast, char digit);
98 static int features_call(struct ast_channel *ast, char *dest, int timeout);
99 static int features_hangup(struct ast_channel *ast);
100 static int features_answer(struct ast_channel *ast);
101 static struct ast_frame *features_read(struct ast_channel *ast);
102 static int features_write(struct ast_channel *ast, struct ast_frame *f);
103 static int features_indicate(struct ast_channel *ast, int condition);
104 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
105
106 static const struct ast_channel_tech features_tech = {
107         .type = type,
108         .description = tdesc,
109         .capabilities = -1,
110         .requester = features_request,
111         .send_digit = features_digit,
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_MAX_FDS-1] = p->subs[index].alertpipebackup[0];
172         p->subs[index].owner->fds[AST_MAX_FDS-2] = 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         ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
210
211         towner = p->subs[a].owner;
212         tinthreeway = p->subs[a].inthreeway;
213
214         p->subs[a].owner = p->subs[b].owner;
215         p->subs[a].inthreeway = p->subs[b].inthreeway;
216
217         p->subs[b].owner = towner;
218         p->subs[b].inthreeway = tinthreeway;
219         update_features(p,a);
220         update_features(p,b);
221         wakeup_sub(p, a);
222         wakeup_sub(p, b);
223 }
224 #endif
225
226 static int features_answer(struct ast_channel *ast)
227 {
228         struct feature_pvt *p = ast->tech_pvt;
229         int res = -1;
230         int x;
231
232         ast_mutex_lock(&p->lock);
233         x = indexof(p, ast, 0);
234         if (!x && p->subchan)
235                 res = ast_answer(p->subchan);
236         ast_mutex_unlock(&p->lock);
237         return res;
238 }
239
240 static struct ast_frame  *features_read(struct ast_channel *ast)
241 {
242         static struct ast_frame null_frame = { AST_FRAME_NULL, };
243         struct feature_pvt *p = ast->tech_pvt;
244         struct ast_frame *f;
245         int x;
246         
247         f = &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)
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(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(p->subchan, digit);
314         ast_mutex_unlock(&p->lock);
315         return res;
316 }
317
318 static int features_call(struct ast_channel *ast, char *dest, int timeout)
319 {
320         struct feature_pvt *p = ast->tech_pvt;
321         int res = -1;
322         int x;
323         char *dest2;
324                 
325         dest2 = strchr(dest, '/');
326         if (dest2) {
327                 ast_mutex_lock(&p->lock);
328                 x = indexof(p, ast, 0);
329                 if (!x && p->subchan) {
330                         if (p->owner->cid.cid_num)
331                                 p->subchan->cid.cid_num = strdup(p->owner->cid.cid_num);
332                         else 
333                                 p->subchan->cid.cid_num = NULL;
334                 
335                         if (p->owner->cid.cid_name)
336                                 p->subchan->cid.cid_name = strdup(p->owner->cid.cid_name);
337                         else 
338                                 p->subchan->cid.cid_name = NULL;
339                 
340                         if (p->owner->cid.cid_rdnis)
341                                 p->subchan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
342                         else
343                                 p->subchan->cid.cid_rdnis = NULL;
344                 
345                         if (p->owner->cid.cid_ani)
346                                 p->subchan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
347                         else
348                                 p->subchan->cid.cid_ani = NULL;
349                 
350                         p->subchan->cid.cid_pres = p->owner->cid.cid_pres;
351                         strncpy(p->subchan->language, p->owner->language, sizeof(p->subchan->language) - 1);
352                         strncpy(p->subchan->accountcode, p->owner->accountcode, sizeof(p->subchan->accountcode) - 1);
353                         p->subchan->cdrflags = p->owner->cdrflags;
354                         res = ast_call(p->subchan, dest2, timeout);
355                         update_features(p, x);
356                 } else
357                         ast_log(LOG_NOTICE, "Uhm yah, not quite there with the call waiting...\n");
358                 ast_mutex_unlock(&p->lock);
359         }
360         return res;
361 }
362
363 static int features_hangup(struct ast_channel *ast)
364 {
365         struct feature_pvt *p = ast->tech_pvt;
366         struct feature_pvt *cur, *prev=NULL;
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         
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_mutex_lock(&featurelock);
383                 cur = features;
384                 while(cur) {
385                         if (cur == p) {
386                                 if (prev)
387                                         prev->next = cur->next;
388                                 else
389                                         features = cur->next;
390                                 break;
391                         }
392                         prev = cur;
393                         cur = cur->next;
394                 }
395                 ast_mutex_unlock(&featurelock);
396                 ast_mutex_lock(&p->lock);
397                 /* And destroy */
398                 if (p->subchan)
399                         ast_hangup(p->subchan);
400                 ast_mutex_unlock(&p->lock);
401                 ast_mutex_destroy(&p->lock);
402                 free(p);
403                 return 0;
404         }
405         ast_mutex_unlock(&p->lock);
406         return 0;
407 }
408
409 static struct feature_pvt *features_alloc(char *data, int format)
410 {
411         struct feature_pvt *tmp;
412         char *dest=NULL;
413         char *tech;
414         int x;
415         int status;
416         struct ast_channel *chan;
417         
418         tech = ast_strdupa(data);
419         if (tech) {
420                 dest = strchr(tech, '/');
421                 if (dest) {
422                         *dest = '\0';
423                         dest++;
424                 }
425         }
426         if (!tech || !dest) {
427                 ast_log(LOG_NOTICE, "Format for feature channel is Feature/Tech/Dest ('%s' not valid)!\n", 
428                         data);
429                 return NULL;
430         }
431         ast_mutex_lock(&featurelock);
432         tmp = features;
433         while(tmp) {
434                 if (!strcasecmp(tmp->tech, tech) && !strcmp(tmp->dest, dest))
435                         break;
436                 tmp = tmp->next;
437         }
438         ast_mutex_unlock(&featurelock);
439         if (!tmp) {
440                 chan = ast_request(tech, format, dest, &status);
441                 if (!chan) {
442                         ast_log(LOG_NOTICE, "Unable to allocate subchannel '%s/%s'\n", tech, dest);
443                         return NULL;
444                 }
445                 tmp = malloc(sizeof(struct feature_pvt));
446                 if (tmp) {
447                         memset(tmp, 0, sizeof(struct feature_pvt));
448                         for (x=0;x<3;x++)
449                                 init_sub(tmp->subs + x);
450                         ast_mutex_init(&tmp->lock);
451                         strncpy(tmp->tech, tech, sizeof(tmp->tech) - 1);
452                         strncpy(tmp->dest, dest, sizeof(tmp->dest) - 1);
453                         tmp->subchan = chan;
454                         ast_mutex_lock(&featurelock);
455                         tmp->next = features;
456                         features = tmp;
457                         ast_mutex_unlock(&featurelock);
458                 }
459         }
460         return tmp;
461 }
462
463 static struct ast_channel *features_new(struct feature_pvt *p, int state, int index)
464 {
465         struct ast_channel *tmp;
466         int x,y;
467         if (!p->subchan) {
468                 ast_log(LOG_WARNING, "Called upon channel with no subchan:(\n");
469                 return NULL;
470         }
471         if (p->subs[index].owner) {
472                 ast_log(LOG_WARNING, "Called to put index %d already there!\n", index);
473                 return NULL;
474         }
475         tmp = ast_channel_alloc(0);
476         if (!tmp) {
477                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
478                 return NULL;
479         }
480         tmp->tech = &features_tech;
481         for (x=1;x<4;x++) {
482                 snprintf(tmp->name, sizeof(tmp->name), "Feature/%s/%s-%d", p->tech, p->dest, x);
483                 for (y=0;y<3;y++) {
484                         if (y == index)
485                                 continue;
486                         if (p->subs[y].owner && !strcasecmp(p->subs[y].owner->name, tmp->name))
487                                 break;
488                 }
489                 if (y >= 3)
490                         break;
491         }
492         tmp->type = type;
493         ast_setstate(tmp, state);
494         tmp->writeformat = p->subchan->writeformat;
495         tmp->rawwriteformat = p->subchan->rawwriteformat;
496         tmp->readformat = p->subchan->readformat;
497         tmp->rawreadformat = p->subchan->rawreadformat;
498         tmp->nativeformats = p->subchan->readformat;
499         tmp->tech_pvt = p;
500         p->subs[index].owner = tmp;
501         if (!p->owner)
502                 p->owner = tmp;
503         ast_mutex_lock(&usecnt_lock);
504         usecnt++;
505         ast_mutex_unlock(&usecnt_lock);
506         ast_update_use_count();
507         return tmp;
508 }
509
510
511 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause)
512 {
513         struct feature_pvt *p;
514         struct ast_channel *chan = NULL;
515
516         p = features_alloc(data, format);
517         if (p && !p->subs[SUB_REAL].owner)
518                 chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
519         if (chan)
520                 update_features(p,SUB_REAL);
521         return chan;
522 }
523
524 static int features_show(int fd, int argc, char **argv)
525 {
526         struct feature_pvt *p;
527
528         if (argc != 3)
529                 return RESULT_SHOWUSAGE;
530         ast_mutex_lock(&featurelock);
531         p = features;
532         while(p) {
533                 ast_mutex_lock(&p->lock);
534                 ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
535                 ast_mutex_unlock(&p->lock);
536                 p = p->next;
537         }
538         if (!features)
539                 ast_cli(fd, "No feature channels in use\n");
540         ast_mutex_unlock(&featurelock);
541         return RESULT_SUCCESS;
542 }
543
544 static char show_features_usage[] = 
545 "Usage: feature show channels\n"
546 "       Provides summary information on feature channels.\n";
547
548 static struct ast_cli_entry cli_show_features = {
549         { "feature", "show", "channels", NULL }, features_show, 
550         "Show status of feature channels", show_features_usage, NULL };
551
552 int load_module()
553 {
554         /* Make sure we can register our sip channel type */
555         if (ast_channel_register(&features_tech)) {
556                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
557                 return -1;
558         }
559         ast_cli_register(&cli_show_features);
560         return 0;
561 }
562
563 int reload()
564 {
565         return 0;
566 }
567
568 int unload_module()
569 {
570         struct feature_pvt *p;
571         /* First, take us out of the channel loop */
572         ast_cli_unregister(&cli_show_features);
573         ast_channel_unregister(&features_tech);
574         if (!ast_mutex_lock(&featurelock)) {
575                 /* Hangup all interfaces if they have an owner */
576                 p = features;
577                 while(p) {
578                         if (p->owner)
579                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
580                         p = p->next;
581                 }
582                 features = NULL;
583                 ast_mutex_unlock(&featurelock);
584         } else {
585                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
586                 return -1;
587         }               
588         return 0;
589 }
590
591 int usecount()
592 {
593         return usecnt;
594 }
595
596 char *key()
597 {
598         return ASTERISK_GPL_KEY;
599 }
600
601 char *description()
602 {
603         return (char *) desc;
604 }
605