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