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