00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <unistd.h>
00031 #include <sys/socket.h>
00032 #include <errno.h>
00033 #include <stdlib.h>
00034 #include <fcntl.h>
00035 #include <netdb.h>
00036 #include <netinet/in.h>
00037 #include <arpa/inet.h>
00038 #include <sys/signal.h>
00039
00040 #include "asterisk.h"
00041
00042 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 41411 $")
00043
00044 #include "asterisk/lock.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/config.h"
00047 #include "asterisk/logger.h"
00048 #include "asterisk/module.h"
00049 #include "asterisk/pbx.h"
00050 #include "asterisk/options.h"
00051 #include "asterisk/lock.h"
00052 #include "asterisk/sched.h"
00053 #include "asterisk/io.h"
00054 #include "asterisk/rtp.h"
00055 #include "asterisk/acl.h"
00056 #include "asterisk/callerid.h"
00057 #include "asterisk/file.h"
00058 #include "asterisk/cli.h"
00059 #include "asterisk/app.h"
00060 #include "asterisk/musiconhold.h"
00061 #include "asterisk/manager.h"
00062
00063 static const char desc[] = "Feature Proxy Channel";
00064 static const char type[] = "Feature";
00065 static const char tdesc[] = "Feature Proxy Channel Driver";
00066
00067 static int usecnt =0;
00068 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
00069
00070 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
00071
00072
00073 AST_MUTEX_DEFINE_STATIC(featurelock);
00074
00075 struct feature_sub {
00076 struct ast_channel *owner;
00077 int inthreeway;
00078 int pfd;
00079 int timingfdbackup;
00080 int alertpipebackup[2];
00081 };
00082
00083 static struct feature_pvt {
00084 ast_mutex_t lock;
00085 char tech[AST_MAX_EXTENSION];
00086 char dest[AST_MAX_EXTENSION];
00087 struct ast_channel *subchan;
00088 struct feature_sub subs[3];
00089 struct ast_channel *owner;
00090 struct feature_pvt *next;
00091 } *features = NULL;
00092
00093 #define SUB_REAL 0
00094 #define SUB_CALLWAIT 1
00095 #define SUB_THREEWAY 2
00096
00097 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause);
00098 static int features_digit(struct ast_channel *ast, char digit);
00099 static int features_call(struct ast_channel *ast, char *dest, int timeout);
00100 static int features_hangup(struct ast_channel *ast);
00101 static int features_answer(struct ast_channel *ast);
00102 static struct ast_frame *features_read(struct ast_channel *ast);
00103 static int features_write(struct ast_channel *ast, struct ast_frame *f);
00104 static int features_indicate(struct ast_channel *ast, int condition);
00105 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00106
00107 static const struct ast_channel_tech features_tech = {
00108 .type = type,
00109 .description = tdesc,
00110 .capabilities = -1,
00111 .requester = features_request,
00112 .send_digit = features_digit,
00113 .call = features_call,
00114 .hangup = features_hangup,
00115 .answer = features_answer,
00116 .read = features_read,
00117 .write = features_write,
00118 .exception = features_read,
00119 .indicate = features_indicate,
00120 .fixup = features_fixup,
00121 };
00122
00123 static inline void init_sub(struct feature_sub *sub)
00124 {
00125 sub->inthreeway = 0;
00126 sub->pfd = -1;
00127 sub->timingfdbackup = -1;
00128 sub->alertpipebackup[0] = sub->alertpipebackup[1] = -1;
00129 }
00130
00131 static inline int indexof(struct feature_pvt *p, struct ast_channel *owner, int nullok)
00132 {
00133 int x;
00134 if (!owner) {
00135 ast_log(LOG_WARNING, "indexof called on NULL owner??\n");
00136 return -1;
00137 }
00138 for (x=0; x<3; x++) {
00139 if (owner == p->subs[x].owner)
00140 return x;
00141 }
00142 return -1;
00143 }
00144
00145 #if 0
00146 static void wakeup_sub(struct feature_pvt *p, int a)
00147 {
00148 struct ast_frame null = { AST_FRAME_NULL, };
00149 for (;;) {
00150 if (p->subs[a].owner) {
00151 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
00152 ast_mutex_unlock(&p->lock);
00153 usleep(1);
00154 ast_mutex_lock(&p->lock);
00155 } else {
00156 ast_queue_frame(p->subs[a].owner, &null);
00157 ast_mutex_unlock(&p->subs[a].owner->lock);
00158 break;
00159 }
00160 } else
00161 break;
00162 }
00163 }
00164 #endif
00165
00166 static void restore_channel(struct feature_pvt *p, int index)
00167 {
00168
00169 p->subs[index].owner->timingfd = p->subs[index].timingfdbackup;
00170 p->subs[index].owner->alertpipe[0] = p->subs[index].alertpipebackup[0];
00171 p->subs[index].owner->alertpipe[1] = p->subs[index].alertpipebackup[1];
00172 p->subs[index].owner->fds[AST_MAX_FDS-1] = p->subs[index].alertpipebackup[0];
00173 p->subs[index].owner->fds[AST_MAX_FDS-2] = p->subs[index].timingfdbackup;
00174 }
00175
00176 static void update_features(struct feature_pvt *p, int index)
00177 {
00178 int x;
00179 if (p->subs[index].owner) {
00180 for (x=0; x<AST_MAX_FDS; x++) {
00181 if (index)
00182 p->subs[index].owner->fds[x] = -1;
00183 else
00184 p->subs[index].owner->fds[x] = p->subchan->fds[x];
00185 }
00186 if (!index) {
00187
00188 p->subs[index].owner->timingfd = p->subchan->timingfd;
00189 p->subs[index].owner->alertpipe[0] = p->subchan->alertpipe[0];
00190 p->subs[index].owner->alertpipe[1] = p->subchan->alertpipe[1];
00191 if (p->subs[index].owner->nativeformats != p->subchan->readformat) {
00192 p->subs[index].owner->nativeformats = p->subchan->readformat;
00193 if (p->subs[index].owner->readformat)
00194 ast_set_read_format(p->subs[index].owner, p->subs[index].owner->readformat);
00195 if (p->subs[index].owner->writeformat)
00196 ast_set_write_format(p->subs[index].owner, p->subs[index].owner->writeformat);
00197 }
00198 } else{
00199 restore_channel(p, index);
00200 }
00201 }
00202 }
00203
00204 #if 0
00205 static void swap_subs(struct feature_pvt *p, int a, int b)
00206 {
00207 int tinthreeway;
00208 struct ast_channel *towner;
00209
00210 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
00211
00212 towner = p->subs[a].owner;
00213 tinthreeway = p->subs[a].inthreeway;
00214
00215 p->subs[a].owner = p->subs[b].owner;
00216 p->subs[a].inthreeway = p->subs[b].inthreeway;
00217
00218 p->subs[b].owner = towner;
00219 p->subs[b].inthreeway = tinthreeway;
00220 update_features(p,a);
00221 update_features(p,b);
00222 wakeup_sub(p, a);
00223 wakeup_sub(p, b);
00224 }
00225 #endif
00226
00227 static int features_answer(struct ast_channel *ast)
00228 {
00229 struct feature_pvt *p = ast->tech_pvt;
00230 int res = -1;
00231 int x;
00232
00233 ast_mutex_lock(&p->lock);
00234 x = indexof(p, ast, 0);
00235 if (!x && p->subchan)
00236 res = ast_answer(p->subchan);
00237 ast_mutex_unlock(&p->lock);
00238 return res;
00239 }
00240
00241 static struct ast_frame *features_read(struct ast_channel *ast)
00242 {
00243 static struct ast_frame null_frame = { AST_FRAME_NULL, };
00244 struct feature_pvt *p = ast->tech_pvt;
00245 struct ast_frame *f;
00246 int x;
00247
00248 f = &null_frame;
00249 ast_mutex_lock(&p->lock);
00250 x = indexof(p, ast, 0);
00251 if (!x && p->subchan) {
00252 update_features(p, x);
00253 f = ast_read(p->subchan);
00254 }
00255 ast_mutex_unlock(&p->lock);
00256 return f;
00257 }
00258
00259 static int features_write(struct ast_channel *ast, struct ast_frame *f)
00260 {
00261 struct feature_pvt *p = ast->tech_pvt;
00262 int res = -1;
00263 int x;
00264
00265 ast_mutex_lock(&p->lock);
00266 x = indexof(p, ast, 0);
00267 if (!x && p->subchan)
00268 res = ast_write(p->subchan, f);
00269 ast_mutex_unlock(&p->lock);
00270 return res;
00271 }
00272
00273 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00274 {
00275 struct feature_pvt *p = newchan->tech_pvt;
00276 int x;
00277
00278 ast_mutex_lock(&p->lock);
00279 if (p->owner == oldchan)
00280 p->owner = newchan;
00281 for (x = 0; x < 3; x++) {
00282 if (p->subs[x].owner == oldchan)
00283 p->subs[x].owner = newchan;
00284 }
00285 ast_mutex_unlock(&p->lock);
00286 return 0;
00287 }
00288
00289 static int features_indicate(struct ast_channel *ast, int condition)
00290 {
00291 struct feature_pvt *p = ast->tech_pvt;
00292 int res = -1;
00293 int x;
00294
00295
00296 ast_mutex_lock(&p->lock);
00297 x = indexof(p, ast, 0);
00298 if (!x && p->subchan)
00299 res = ast_indicate(p->subchan, condition);
00300 ast_mutex_unlock(&p->lock);
00301 return res;
00302 }
00303
00304 static int features_digit(struct ast_channel *ast, char digit)
00305 {
00306 struct feature_pvt *p = ast->tech_pvt;
00307 int res = -1;
00308 int x;
00309
00310
00311 ast_mutex_lock(&p->lock);
00312 x = indexof(p, ast, 0);
00313 if (!x && p->subchan)
00314 res = ast_senddigit(p->subchan, digit);
00315 ast_mutex_unlock(&p->lock);
00316 return res;
00317 }
00318
00319 static int features_call(struct ast_channel *ast, char *dest, int timeout)
00320 {
00321 struct feature_pvt *p = ast->tech_pvt;
00322 int res = -1;
00323 int x;
00324 char *dest2;
00325
00326 dest2 = strchr(dest, '/');
00327 if (dest2) {
00328 ast_mutex_lock(&p->lock);
00329 x = indexof(p, ast, 0);
00330 if (!x && p->subchan) {
00331 p->subchan->cid.cid_name = p->owner->cid.cid_name ?
00332 strdup(p->owner->cid.cid_name) : NULL;
00333 p->subchan->cid.cid_num = p->owner->cid.cid_num ?
00334 strdup(p->owner->cid.cid_num) : NULL;
00335 p->subchan->cid.cid_ani = p->owner->cid.cid_ani ?
00336 strdup(p->owner->cid.cid_ani) : NULL;
00337 p->subchan->cid.cid_rdnis = p->owner->cid.cid_rdnis ?
00338 strdup(p->owner->cid.cid_rdnis) : NULL;
00339
00340 p->subchan->cid.cid_pres = p->owner->cid.cid_pres;
00341 strncpy(p->subchan->language, p->owner->language, sizeof(p->subchan->language) - 1);
00342 strncpy(p->subchan->accountcode, p->owner->accountcode, sizeof(p->subchan->accountcode) - 1);
00343 p->subchan->cdrflags = p->owner->cdrflags;
00344 res = ast_call(p->subchan, dest2, timeout);
00345 update_features(p, x);
00346 } else
00347 ast_log(LOG_NOTICE, "Uhm yah, not quite there with the call waiting...\n");
00348 ast_mutex_unlock(&p->lock);
00349 }
00350 return res;
00351 }
00352
00353 static int features_hangup(struct ast_channel *ast)
00354 {
00355 struct feature_pvt *p = ast->tech_pvt;
00356 struct feature_pvt *cur, *prev=NULL;
00357 int x;
00358
00359 ast_mutex_lock(&p->lock);
00360 x = indexof(p, ast, 0);
00361 if (x > -1) {
00362 restore_channel(p, x);
00363 p->subs[x].owner = NULL;
00364
00365 }
00366 ast->tech_pvt = NULL;
00367
00368
00369 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
00370 ast_mutex_unlock(&p->lock);
00371
00372 ast_mutex_lock(&featurelock);
00373 cur = features;
00374 while(cur) {
00375 if (cur == p) {
00376 if (prev)
00377 prev->next = cur->next;
00378 else
00379 features = cur->next;
00380 break;
00381 }
00382 prev = cur;
00383 cur = cur->next;
00384 }
00385 ast_mutex_unlock(&featurelock);
00386 ast_mutex_lock(&p->lock);
00387
00388 if (p->subchan)
00389 ast_hangup(p->subchan);
00390 ast_mutex_unlock(&p->lock);
00391 ast_mutex_destroy(&p->lock);
00392 free(p);
00393 return 0;
00394 }
00395 ast_mutex_unlock(&p->lock);
00396 return 0;
00397 }
00398
00399 static struct feature_pvt *features_alloc(char *data, int format)
00400 {
00401 struct feature_pvt *tmp;
00402 char *dest=NULL;
00403 char *tech;
00404 int x;
00405 int status;
00406 struct ast_channel *chan;
00407
00408 tech = ast_strdupa(data);
00409 if (tech) {
00410 dest = strchr(tech, '/');
00411 if (dest) {
00412 *dest = '\0';
00413 dest++;
00414 }
00415 }
00416 if (!tech || !dest) {
00417 ast_log(LOG_NOTICE, "Format for feature channel is Feature/Tech/Dest ('%s' not valid)!\n",
00418 data);
00419 return NULL;
00420 }
00421 ast_mutex_lock(&featurelock);
00422 tmp = features;
00423 while(tmp) {
00424 if (!strcasecmp(tmp->tech, tech) && !strcmp(tmp->dest, dest))
00425 break;
00426 tmp = tmp->next;
00427 }
00428 ast_mutex_unlock(&featurelock);
00429 if (!tmp) {
00430 chan = ast_request(tech, format, dest, &status);
00431 if (!chan) {
00432 ast_log(LOG_NOTICE, "Unable to allocate subchannel '%s/%s'\n", tech, dest);
00433 return NULL;
00434 }
00435 tmp = malloc(sizeof(struct feature_pvt));
00436 if (tmp) {
00437 memset(tmp, 0, sizeof(struct feature_pvt));
00438 for (x=0;x<3;x++)
00439 init_sub(tmp->subs + x);
00440 ast_mutex_init(&tmp->lock);
00441 strncpy(tmp->tech, tech, sizeof(tmp->tech) - 1);
00442 strncpy(tmp->dest, dest, sizeof(tmp->dest) - 1);
00443 tmp->subchan = chan;
00444 ast_mutex_lock(&featurelock);
00445 tmp->next = features;
00446 features = tmp;
00447 ast_mutex_unlock(&featurelock);
00448 }
00449 }
00450 return tmp;
00451 }
00452
00453 static struct ast_channel *features_new(struct feature_pvt *p, int state, int index)
00454 {
00455 struct ast_channel *tmp;
00456 int x,y;
00457 if (!p->subchan) {
00458 ast_log(LOG_WARNING, "Called upon channel with no subchan:(\n");
00459 return NULL;
00460 }
00461 if (p->subs[index].owner) {
00462 ast_log(LOG_WARNING, "Called to put index %d already there!\n", index);
00463 return NULL;
00464 }
00465 tmp = ast_channel_alloc(0);
00466 if (!tmp) {
00467 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
00468 return NULL;
00469 }
00470 tmp->tech = &features_tech;
00471 for (x=1;x<4;x++) {
00472 snprintf(tmp->name, sizeof(tmp->name), "Feature/%s/%s-%d", p->tech, p->dest, x);
00473 for (y=0;y<3;y++) {
00474 if (y == index)
00475 continue;
00476 if (p->subs[y].owner && !strcasecmp(p->subs[y].owner->name, tmp->name))
00477 break;
00478 }
00479 if (y >= 3)
00480 break;
00481 }
00482 tmp->type = type;
00483 ast_setstate(tmp, state);
00484 tmp->writeformat = p->subchan->writeformat;
00485 tmp->rawwriteformat = p->subchan->rawwriteformat;
00486 tmp->readformat = p->subchan->readformat;
00487 tmp->rawreadformat = p->subchan->rawreadformat;
00488 tmp->nativeformats = p->subchan->readformat;
00489 tmp->tech_pvt = p;
00490 p->subs[index].owner = tmp;
00491 if (!p->owner)
00492 p->owner = tmp;
00493 ast_mutex_lock(&usecnt_lock);
00494 usecnt++;
00495 ast_mutex_unlock(&usecnt_lock);
00496 ast_update_use_count();
00497 return tmp;
00498 }
00499
00500
00501 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause)
00502 {
00503 struct feature_pvt *p;
00504 struct ast_channel *chan = NULL;
00505
00506 p = features_alloc(data, format);
00507 if (p && !p->subs[SUB_REAL].owner)
00508 chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
00509 if (chan)
00510 update_features(p,SUB_REAL);
00511 return chan;
00512 }
00513
00514 static int features_show(int fd, int argc, char **argv)
00515 {
00516 struct feature_pvt *p;
00517
00518 if (argc != 3)
00519 return RESULT_SHOWUSAGE;
00520 ast_mutex_lock(&featurelock);
00521 p = features;
00522 while(p) {
00523 ast_mutex_lock(&p->lock);
00524 ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
00525 ast_mutex_unlock(&p->lock);
00526 p = p->next;
00527 }
00528 if (!features)
00529 ast_cli(fd, "No feature channels in use\n");
00530 ast_mutex_unlock(&featurelock);
00531 return RESULT_SUCCESS;
00532 }
00533
00534 static char show_features_usage[] =
00535 "Usage: feature show channels\n"
00536 " Provides summary information on feature channels.\n";
00537
00538 static struct ast_cli_entry cli_show_features = {
00539 { "feature", "show", "channels", NULL }, features_show,
00540 "Show status of feature channels", show_features_usage, NULL };
00541
00542 int load_module()
00543 {
00544
00545 if (ast_channel_register(&features_tech)) {
00546 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
00547 return -1;
00548 }
00549 ast_cli_register(&cli_show_features);
00550 return 0;
00551 }
00552
00553 int reload()
00554 {
00555 return 0;
00556 }
00557
00558 int unload_module()
00559 {
00560 struct feature_pvt *p, *prev;
00561
00562 ast_cli_unregister(&cli_show_features);
00563 ast_channel_unregister(&features_tech);
00564 if (!ast_mutex_lock(&featurelock)) {
00565
00566 for (p = features; p; p = p->next) {
00567 prev = p;
00568 if (p->owner)
00569 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
00570 free(prev);
00571 }
00572 features = NULL;
00573 ast_mutex_unlock(&featurelock);
00574 } else {
00575 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
00576 return -1;
00577 }
00578 return 0;
00579 }
00580
00581 int usecount()
00582 {
00583 return usecnt;
00584 }
00585
00586 char *key()
00587 {
00588 return ASTERISK_GPL_KEY;
00589 }
00590
00591 char *description()
00592 {
00593 return (char *) desc;
00594 }
00595