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