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 "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 288748 $")
00031
00032 #include <fcntl.h>
00033 #include <sys/signal.h>
00034
00035 #include "asterisk/lock.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/config.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/sched.h"
00041 #include "asterisk/io.h"
00042 #include "asterisk/acl.h"
00043 #include "asterisk/callerid.h"
00044 #include "asterisk/file.h"
00045 #include "asterisk/cli.h"
00046 #include "asterisk/app.h"
00047 #include "asterisk/musiconhold.h"
00048 #include "asterisk/manager.h"
00049 #include "asterisk/stringfields.h"
00050 #include "asterisk/devicestate.h"
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 static const char tdesc[] = "Local Proxy Channel Driver";
00072
00073 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
00074
00075 static struct ast_jb_conf g_jb_conf = {
00076 .flags = 0,
00077 .max_size = -1,
00078 .resync_threshold = -1,
00079 .impl = "",
00080 .target_extra = -1,
00081 };
00082
00083 static struct ast_channel *local_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
00084 static int local_digit_begin(struct ast_channel *ast, char digit);
00085 static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
00086 static int local_call(struct ast_channel *ast, char *dest, int timeout);
00087 static int local_hangup(struct ast_channel *ast);
00088 static int local_answer(struct ast_channel *ast);
00089 static struct ast_frame *local_read(struct ast_channel *ast);
00090 static int local_write(struct ast_channel *ast, struct ast_frame *f);
00091 static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
00092 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00093 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
00094 static int local_sendtext(struct ast_channel *ast, const char *text);
00095 static int local_devicestate(void *data);
00096 static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
00097 static int local_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
00098 static int local_setoption(struct ast_channel *chan, int option, void *data, int datalen);
00099
00100
00101 static const struct ast_channel_tech local_tech = {
00102 .type = "Local",
00103 .description = tdesc,
00104 .capabilities = -1,
00105 .requester = local_request,
00106 .send_digit_begin = local_digit_begin,
00107 .send_digit_end = local_digit_end,
00108 .call = local_call,
00109 .hangup = local_hangup,
00110 .answer = local_answer,
00111 .read = local_read,
00112 .write = local_write,
00113 .write_video = local_write,
00114 .exception = local_read,
00115 .indicate = local_indicate,
00116 .fixup = local_fixup,
00117 .send_html = local_sendhtml,
00118 .send_text = local_sendtext,
00119 .devicestate = local_devicestate,
00120 .bridged_channel = local_bridgedchannel,
00121 .queryoption = local_queryoption,
00122 .setoption = local_setoption,
00123 };
00124
00125
00126
00127
00128
00129
00130
00131
00132 struct local_pvt {
00133 ast_mutex_t lock;
00134 unsigned int flags;
00135 char context[AST_MAX_CONTEXT];
00136 char exten[AST_MAX_EXTENSION];
00137 int reqformat;
00138 struct ast_jb_conf jb_conf;
00139 struct ast_channel *owner;
00140 struct ast_channel *chan;
00141 struct ast_module_user *u_owner;
00142 struct ast_module_user *u_chan;
00143 AST_LIST_ENTRY(local_pvt) list;
00144 };
00145
00146 #define LOCAL_GLARE_DETECT (1 << 0)
00147 #define LOCAL_CANCEL_QUEUE (1 << 1)
00148 #define LOCAL_ALREADY_MASQED (1 << 2)
00149 #define LOCAL_LAUNCHED_PBX (1 << 3)
00150 #define LOCAL_NO_OPTIMIZATION (1 << 4)
00151 #define LOCAL_BRIDGE (1 << 5)
00152 #define LOCAL_MOH_PASSTHRU (1 << 6)
00153
00154 static AST_LIST_HEAD_STATIC(locals, local_pvt);
00155
00156 static int local_setoption(struct ast_channel *chan, int option, void * data, int datalen)
00157 {
00158 int res;
00159 struct local_pvt *p;
00160 struct ast_channel *otherchan;
00161 ast_chan_write_info_t *write_info;
00162
00163 if (option != AST_OPTION_CHANNEL_WRITE) {
00164 return -1;
00165 }
00166
00167 write_info = data;
00168
00169 if (write_info->version != AST_CHAN_WRITE_INFO_T_VERSION) {
00170 ast_log(LOG_ERROR, "The chan_write_info_t type has changed, and this channel hasn't been updated!\n");
00171 return -1;
00172 }
00173
00174
00175 startover:
00176 ast_channel_lock(chan);
00177
00178 p = chan->tech_pvt;
00179 if (!p) {
00180 ast_channel_unlock(chan);
00181 ast_log(LOG_WARNING, "Could not update other side of %s, local_pvt went away.\n", chan->name);
00182 return -1;
00183 }
00184
00185 while (ast_mutex_trylock(&p->lock)) {
00186 ast_channel_unlock(chan);
00187 sched_yield();
00188 ast_channel_lock(chan);
00189 p = chan->tech_pvt;
00190 if (!p) {
00191 ast_channel_unlock(chan);
00192 ast_log(LOG_WARNING, "Could not update other side of %s, local_pvt went away.\n", chan->name);
00193 return -1;
00194 }
00195 }
00196
00197 otherchan = (write_info->chan == p->owner) ? p->chan : p->owner;
00198
00199 if (!otherchan || otherchan == write_info->chan) {
00200 ast_mutex_unlock(&p->lock);
00201 ast_channel_unlock(chan);
00202 ast_log(LOG_WARNING, "Could not update other side of %s, other side went away.\n", chan->name);
00203 return 0;
00204 }
00205
00206 if (ast_channel_trylock(otherchan)) {
00207 ast_mutex_unlock(&p->lock);
00208 ast_channel_unlock(chan);
00209 goto startover;
00210 }
00211
00212 res = write_info->write_fn(otherchan, write_info->function, write_info->data, write_info->value);
00213
00214 ast_channel_unlock(otherchan);
00215 ast_mutex_unlock(&p->lock);
00216 ast_channel_unlock(chan);
00217
00218 return res;
00219 }
00220
00221
00222 static int local_devicestate(void *data)
00223 {
00224 char *exten = ast_strdupa(data);
00225 char *context = NULL, *opts = NULL;
00226 int res;
00227 struct local_pvt *lp;
00228
00229 if (!(context = strchr(exten, '@'))) {
00230 ast_log(LOG_WARNING, "Someone used Local/%s somewhere without a @context. This is bad.\n", exten);
00231 return AST_DEVICE_INVALID;
00232 }
00233
00234 *context++ = '\0';
00235
00236
00237 if ((opts = strchr(context, '/')))
00238 *opts = '\0';
00239
00240 ast_debug(3, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
00241
00242 res = ast_exists_extension(NULL, context, exten, 1, NULL);
00243 if (!res)
00244 return AST_DEVICE_INVALID;
00245
00246 res = AST_DEVICE_NOT_INUSE;
00247 AST_LIST_LOCK(&locals);
00248 AST_LIST_TRAVERSE(&locals, lp, list) {
00249 if (!strcmp(exten, lp->exten) && !strcmp(context, lp->context) && lp->owner) {
00250 res = AST_DEVICE_INUSE;
00251 break;
00252 }
00253 }
00254 AST_LIST_UNLOCK(&locals);
00255
00256 return res;
00257 }
00258
00259
00260
00261
00262 static struct local_pvt *local_pvt_destroy(struct local_pvt *pvt)
00263 {
00264 ast_mutex_destroy(&pvt->lock);
00265 ast_free(pvt);
00266 return NULL;
00267 }
00268
00269
00270 static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge)
00271 {
00272 struct local_pvt *p = bridge->tech_pvt;
00273 struct ast_channel *bridged = bridge;
00274
00275 if (!p) {
00276 ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning <none>\n",
00277 chan->name, bridge->name);
00278 return NULL;
00279 }
00280
00281 ast_mutex_lock(&p->lock);
00282
00283 if (ast_test_flag(p, LOCAL_BRIDGE)) {
00284
00285 bridged = (bridge == p->owner ? p->chan : p->owner);
00286
00287
00288 if (!bridged) {
00289 bridged = bridge;
00290 } else if (bridged->_bridge) {
00291 bridged = bridged->_bridge;
00292 }
00293 }
00294
00295 ast_mutex_unlock(&p->lock);
00296
00297 return bridged;
00298 }
00299
00300 static int local_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
00301 {
00302 struct local_pvt *p = ast->tech_pvt;
00303 struct ast_channel *chan, *bridged;
00304 int res;
00305
00306 if (!p) {
00307 return -1;
00308 }
00309
00310 if (option != AST_OPTION_T38_STATE) {
00311
00312 return -1;
00313 }
00314
00315 ast_mutex_lock(&p->lock);
00316 chan = IS_OUTBOUND(ast, p) ? p->owner : p->chan;
00317
00318 try_again:
00319 if (!chan) {
00320 ast_mutex_unlock(&p->lock);
00321 return -1;
00322 }
00323
00324 if (ast_channel_trylock(chan)) {
00325 DEADLOCK_AVOIDANCE(&p->lock);
00326 chan = IS_OUTBOUND(ast, p) ? p->owner : p->chan;
00327 goto try_again;
00328 }
00329
00330 bridged = ast_bridged_channel(chan);
00331 if (!bridged) {
00332
00333 ast_mutex_unlock(&p->lock);
00334 ast_channel_unlock(chan);
00335 return -1;
00336 }
00337
00338 if (ast_channel_trylock(bridged)) {
00339 ast_channel_unlock(chan);
00340 DEADLOCK_AVOIDANCE(&p->lock);
00341 chan = IS_OUTBOUND(ast, p) ? p->owner : p->chan;
00342 goto try_again;
00343 }
00344
00345 res = ast_channel_queryoption(bridged, option, data, datalen, 0);
00346 ast_mutex_unlock(&p->lock);
00347 ast_channel_unlock(chan);
00348 ast_channel_unlock(bridged);
00349 return res;
00350 }
00351
00352 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f,
00353 struct ast_channel *us, int us_locked)
00354 {
00355 struct ast_channel *other = NULL;
00356
00357
00358 other = isoutbound ? p->owner : p->chan;
00359
00360 if (!other) {
00361 return 0;
00362 }
00363
00364
00365 if (us && us->generator && other->generator) {
00366 return 0;
00367 }
00368
00369
00370 ast_set_flag(p, LOCAL_GLARE_DETECT);
00371
00372
00373 while (other && ast_channel_trylock(other)) {
00374 int res;
00375 if ((res = ast_mutex_unlock(&p->lock))) {
00376 ast_log(LOG_ERROR, "chan_local bug! '&p->lock' was not locked when entering local_queue_frame! (%s)\n", strerror(res));
00377 return -1;
00378 }
00379 if (us && us_locked) {
00380 do {
00381 CHANNEL_DEADLOCK_AVOIDANCE(us);
00382 } while (ast_mutex_trylock(&p->lock));
00383 } else {
00384 usleep(1);
00385 ast_mutex_lock(&p->lock);
00386 }
00387 other = isoutbound ? p->owner : p->chan;
00388 }
00389
00390
00391
00392
00393 if (ast_test_flag(p, LOCAL_CANCEL_QUEUE)) {
00394
00395
00396 ast_mutex_unlock(&p->lock);
00397 p = local_pvt_destroy(p);
00398 if (other) {
00399 ast_channel_unlock(other);
00400 }
00401 return -1;
00402 }
00403
00404 if (other) {
00405 if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_RINGING) {
00406 ast_setstate(other, AST_STATE_RINGING);
00407 }
00408 ast_queue_frame(other, f);
00409 ast_channel_unlock(other);
00410 }
00411
00412 ast_clear_flag(p, LOCAL_GLARE_DETECT);
00413
00414 return 0;
00415 }
00416
00417 static int local_answer(struct ast_channel *ast)
00418 {
00419 struct local_pvt *p = ast->tech_pvt;
00420 int isoutbound;
00421 int res = -1;
00422
00423 if (!p)
00424 return -1;
00425
00426 ast_mutex_lock(&p->lock);
00427 isoutbound = IS_OUTBOUND(ast, p);
00428 if (isoutbound) {
00429
00430 struct ast_frame answer = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
00431 res = local_queue_frame(p, isoutbound, &answer, ast, 1);
00432 } else
00433 ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
00434 if (!res)
00435 ast_mutex_unlock(&p->lock);
00436 return res;
00437 }
00438
00439
00440
00441
00442
00443 static void check_bridge(struct local_pvt *p)
00444 {
00445 struct ast_channel_monitor *tmp;
00446 if (ast_test_flag(p, LOCAL_ALREADY_MASQED) || ast_test_flag(p, LOCAL_NO_OPTIMIZATION) || !p->chan || !p->owner || (p->chan->_bridge != ast_bridged_channel(p->chan)))
00447 return;
00448
00449
00450
00451
00452
00453
00454 if (p->chan->_bridge && AST_LIST_EMPTY(&p->owner->readq)) {
00455
00456
00457
00458
00459 if (!ast_channel_trylock(p->chan->_bridge)) {
00460 if (!ast_check_hangup(p->chan->_bridge)) {
00461 if (!ast_channel_trylock(p->owner)) {
00462 if (!ast_check_hangup(p->owner)) {
00463 if (p->owner->monitor && !p->chan->_bridge->monitor) {
00464
00465
00466
00467
00468
00469 tmp = p->owner->monitor;
00470 p->owner->monitor = p->chan->_bridge->monitor;
00471 p->chan->_bridge->monitor = tmp;
00472 }
00473 if (p->chan->audiohooks) {
00474 struct ast_audiohook_list *audiohooks_swapper;
00475 audiohooks_swapper = p->chan->audiohooks;
00476 p->chan->audiohooks = p->owner->audiohooks;
00477 p->owner->audiohooks = audiohooks_swapper;
00478 }
00479
00480
00481
00482
00483
00484
00485
00486
00487 if (p->owner->caller.id.name.valid || p->owner->caller.id.number.valid
00488 || p->owner->caller.id.subaddress.valid || p->owner->caller.ani.name.valid
00489 || p->owner->caller.ani.number.valid || p->owner->caller.ani.subaddress.valid) {
00490 struct ast_party_caller tmp;
00491 tmp = p->owner->caller;
00492 p->owner->caller = p->chan->_bridge->caller;
00493 p->chan->_bridge->caller = tmp;
00494 }
00495 if (p->owner->redirecting.from.name.valid || p->owner->redirecting.from.number.valid
00496 || p->owner->redirecting.from.subaddress.valid || p->owner->redirecting.to.name.valid
00497 || p->owner->redirecting.to.number.valid || p->owner->redirecting.to.subaddress.valid) {
00498 struct ast_party_redirecting tmp;
00499 tmp = p->owner->redirecting;
00500 p->owner->redirecting = p->chan->_bridge->redirecting;
00501 p->chan->_bridge->redirecting = tmp;
00502 }
00503 if (p->owner->dialed.number.str || p->owner->dialed.subaddress.valid) {
00504 struct ast_party_dialed tmp;
00505 tmp = p->owner->dialed;
00506 p->owner->dialed = p->chan->_bridge->dialed;
00507 p->chan->_bridge->dialed = tmp;
00508 }
00509
00510
00511 ast_app_group_update(p->chan, p->owner);
00512 ast_channel_masquerade(p->owner, p->chan->_bridge);
00513 ast_set_flag(p, LOCAL_ALREADY_MASQED);
00514 }
00515 ast_channel_unlock(p->owner);
00516 }
00517 ast_channel_unlock(p->chan->_bridge);
00518 }
00519 }
00520 }
00521 }
00522
00523 static struct ast_frame *local_read(struct ast_channel *ast)
00524 {
00525 return &ast_null_frame;
00526 }
00527
00528 static int local_write(struct ast_channel *ast, struct ast_frame *f)
00529 {
00530 struct local_pvt *p = ast->tech_pvt;
00531 int res = -1;
00532 int isoutbound;
00533
00534 if (!p)
00535 return -1;
00536
00537
00538 ast_mutex_lock(&p->lock);
00539 isoutbound = IS_OUTBOUND(ast, p);
00540 if (isoutbound && f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO))
00541 check_bridge(p);
00542 if (!ast_test_flag(p, LOCAL_ALREADY_MASQED))
00543 res = local_queue_frame(p, isoutbound, f, ast, 1);
00544 else {
00545 ast_debug(1, "Not posting to queue since already masked on '%s'\n", ast->name);
00546 res = 0;
00547 }
00548 if (!res)
00549 ast_mutex_unlock(&p->lock);
00550 return res;
00551 }
00552
00553 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00554 {
00555 struct local_pvt *p = newchan->tech_pvt;
00556
00557 if (!p)
00558 return -1;
00559
00560 ast_mutex_lock(&p->lock);
00561
00562 if ((p->owner != oldchan) && (p->chan != oldchan)) {
00563 ast_log(LOG_WARNING, "Old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
00564 ast_mutex_unlock(&p->lock);
00565 return -1;
00566 }
00567 if (p->owner == oldchan)
00568 p->owner = newchan;
00569 else
00570 p->chan = newchan;
00571
00572
00573 if (!ast_check_hangup(newchan) && (p->owner->_bridge == p->chan || p->chan->_bridge == p->owner)) {
00574 ast_log(LOG_WARNING, "You can not bridge a Local channel to itself!\n");
00575 ast_mutex_unlock(&p->lock);
00576 ast_queue_hangup(newchan);
00577 return -1;
00578 }
00579
00580 ast_mutex_unlock(&p->lock);
00581 return 0;
00582 }
00583
00584 static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
00585 {
00586 struct local_pvt *p = ast->tech_pvt;
00587 int res = 0;
00588 struct ast_frame f = { AST_FRAME_CONTROL, };
00589 int isoutbound;
00590
00591 if (!p)
00592 return -1;
00593
00594
00595 if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_HOLD) {
00596 ast_moh_start(ast, data, NULL);
00597 } else if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_UNHOLD) {
00598 ast_moh_stop(ast);
00599 } else if (condition == AST_CONTROL_CONNECTED_LINE || condition == AST_CONTROL_REDIRECTING) {
00600 struct ast_channel *this_channel;
00601 struct ast_channel *the_other_channel;
00602
00603
00604
00605
00606
00607
00608
00609 ast_mutex_lock(&p->lock);
00610 isoutbound = IS_OUTBOUND(ast, p);
00611 if (isoutbound) {
00612 this_channel = p->chan;
00613 the_other_channel = p->owner;
00614 } else {
00615 this_channel = p->owner;
00616 the_other_channel = p->chan;
00617 }
00618 if (the_other_channel) {
00619 unsigned char frame_data[1024];
00620 if (condition == AST_CONTROL_CONNECTED_LINE) {
00621 if (isoutbound) {
00622 ast_connected_line_copy_to_caller(&the_other_channel->caller, &this_channel->connected);
00623 }
00624 f.datalen = ast_connected_line_build_data(frame_data, sizeof(frame_data), &this_channel->connected, NULL);
00625 } else {
00626 f.datalen = ast_redirecting_build_data(frame_data, sizeof(frame_data), &this_channel->redirecting, NULL);
00627 }
00628 f.subclass.integer = condition;
00629 f.data.ptr = frame_data;
00630 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 1))) {
00631 ast_mutex_unlock(&p->lock);
00632 }
00633 } else {
00634 ast_mutex_unlock(&p->lock);
00635 }
00636 } else {
00637
00638 ast_mutex_lock(&p->lock);
00639 isoutbound = IS_OUTBOUND(ast, p);
00640 f.subclass.integer = condition;
00641 f.data.ptr = (void*)data;
00642 f.datalen = datalen;
00643 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 1)))
00644 ast_mutex_unlock(&p->lock);
00645 }
00646
00647 return res;
00648 }
00649
00650 static int local_digit_begin(struct ast_channel *ast, char digit)
00651 {
00652 struct local_pvt *p = ast->tech_pvt;
00653 int res = -1;
00654 struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
00655 int isoutbound;
00656
00657 if (!p)
00658 return -1;
00659
00660 ast_mutex_lock(&p->lock);
00661 isoutbound = IS_OUTBOUND(ast, p);
00662 f.subclass.integer = digit;
00663 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00664 ast_mutex_unlock(&p->lock);
00665
00666 return res;
00667 }
00668
00669 static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
00670 {
00671 struct local_pvt *p = ast->tech_pvt;
00672 int res = -1;
00673 struct ast_frame f = { AST_FRAME_DTMF_END, };
00674 int isoutbound;
00675
00676 if (!p)
00677 return -1;
00678
00679 ast_mutex_lock(&p->lock);
00680 isoutbound = IS_OUTBOUND(ast, p);
00681 f.subclass.integer = digit;
00682 f.len = duration;
00683 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00684 ast_mutex_unlock(&p->lock);
00685
00686 return res;
00687 }
00688
00689 static int local_sendtext(struct ast_channel *ast, const char *text)
00690 {
00691 struct local_pvt *p = ast->tech_pvt;
00692 int res = -1;
00693 struct ast_frame f = { AST_FRAME_TEXT, };
00694 int isoutbound;
00695
00696 if (!p)
00697 return -1;
00698
00699 ast_mutex_lock(&p->lock);
00700 isoutbound = IS_OUTBOUND(ast, p);
00701 f.data.ptr = (char *) text;
00702 f.datalen = strlen(text) + 1;
00703 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00704 ast_mutex_unlock(&p->lock);
00705 return res;
00706 }
00707
00708 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
00709 {
00710 struct local_pvt *p = ast->tech_pvt;
00711 int res = -1;
00712 struct ast_frame f = { AST_FRAME_HTML, };
00713 int isoutbound;
00714
00715 if (!p)
00716 return -1;
00717
00718 ast_mutex_lock(&p->lock);
00719 isoutbound = IS_OUTBOUND(ast, p);
00720 f.subclass.integer = subclass;
00721 f.data.ptr = (char *)data;
00722 f.datalen = datalen;
00723 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00724 ast_mutex_unlock(&p->lock);
00725 return res;
00726 }
00727
00728
00729
00730 static int local_call(struct ast_channel *ast, char *dest, int timeout)
00731 {
00732 struct local_pvt *p = ast->tech_pvt;
00733 int res;
00734 struct ast_var_t *varptr = NULL, *new;
00735 size_t len, namelen;
00736 char *reduced_dest = ast_strdupa(dest);
00737 char *slash;
00738
00739 if (!p)
00740 return -1;
00741
00742
00743 start_over:
00744 while (ast_channel_trylock(p->chan)) {
00745 ast_channel_unlock(p->owner);
00746 usleep(1);
00747 ast_channel_lock(p->owner);
00748 }
00749
00750
00751 if (ast_mutex_trylock(&p->lock)) {
00752
00753 ast_channel_unlock(p->chan);
00754 ast_channel_unlock(p->owner);
00755 usleep(1);
00756 ast_channel_lock(p->owner);
00757 goto start_over;
00758 }
00759
00760
00761
00762
00763
00764
00765
00766
00767 ast_party_redirecting_copy(&p->chan->redirecting, &p->owner->redirecting);
00768
00769 ast_party_dialed_copy(&p->chan->dialed, &p->owner->dialed);
00770
00771 ast_connected_line_copy_to_caller(&p->chan->caller, &p->owner->connected);
00772 ast_connected_line_copy_from_caller(&p->chan->connected, &p->owner->caller);
00773
00774 ast_string_field_set(p->chan, language, p->owner->language);
00775 ast_string_field_set(p->chan, accountcode, p->owner->accountcode);
00776 ast_string_field_set(p->chan, musicclass, p->owner->musicclass);
00777 ast_cdr_update(p->chan);
00778
00779 ast_channel_cc_params_init(p->chan, ast_channel_get_cc_config_params(p->owner));
00780
00781 if (!ast_exists_extension(NULL, p->chan->context, p->chan->exten, 1,
00782 S_COR(p->owner->caller.id.number.valid, p->owner->caller.id.number.str, NULL))) {
00783 ast_log(LOG_NOTICE, "No such extension/context %s@%s while calling Local channel\n", p->chan->exten, p->chan->context);
00784 ast_mutex_unlock(&p->lock);
00785 ast_channel_unlock(p->chan);
00786 return -1;
00787 }
00788
00789
00790 if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
00791 ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
00792 }
00793
00794
00795
00796 AST_LIST_TRAVERSE(&p->owner->varshead, varptr, entries) {
00797 namelen = strlen(varptr->name);
00798 len = sizeof(struct ast_var_t) + namelen + strlen(varptr->value) + 2;
00799 if ((new = ast_calloc(1, len))) {
00800 memcpy(new, varptr, len);
00801 new->value = &(new->name[0]) + namelen + 1;
00802 AST_LIST_INSERT_TAIL(&p->chan->varshead, new, entries);
00803 }
00804 }
00805 ast_channel_datastore_inherit(p->owner, p->chan);
00806
00807
00808
00809
00810 if ((slash = strrchr(reduced_dest, '/'))) {
00811 *slash = '\0';
00812 }
00813 ast_set_cc_interfaces_chanvar(p->chan, reduced_dest);
00814
00815
00816 if (!(res = ast_pbx_start(p->chan)))
00817 ast_set_flag(p, LOCAL_LAUNCHED_PBX);
00818
00819 ast_mutex_unlock(&p->lock);
00820 ast_channel_unlock(p->chan);
00821 return res;
00822 }
00823
00824
00825 static int local_hangup(struct ast_channel *ast)
00826 {
00827 struct local_pvt *p = ast->tech_pvt;
00828 int isoutbound;
00829 struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_HANGUP }, .data.uint32 = ast->hangupcause };
00830 struct ast_channel *ochan = NULL;
00831 int glaredetect = 0, res = 0;
00832
00833 if (!p)
00834 return -1;
00835
00836 ast_mutex_lock(&p->lock);
00837
00838 isoutbound = IS_OUTBOUND(ast, p);
00839
00840 if (p->chan && ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
00841 ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
00842 ast_debug(2, "This local call has the ANSWERED_ELSEWHERE flag set.\n");
00843 }
00844
00845 if (isoutbound) {
00846 const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
00847 if ((status) && (p->owner)) {
00848
00849 while (p->owner && ast_channel_trylock(p->owner)) {
00850 ast_mutex_unlock(&p->lock);
00851 if (p->chan) {
00852 ast_channel_unlock(p->chan);
00853 }
00854 usleep(1);
00855 if (p->chan) {
00856 ast_channel_lock(p->chan);
00857 }
00858 ast_mutex_lock(&p->lock);
00859 }
00860 if (p->owner) {
00861 pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
00862 ast_channel_unlock(p->owner);
00863 }
00864 }
00865 p->chan = NULL;
00866 ast_clear_flag(p, LOCAL_LAUNCHED_PBX);
00867 ast_module_user_remove(p->u_chan);
00868 } else {
00869 ast_module_user_remove(p->u_owner);
00870 while (p->chan && ast_channel_trylock(p->chan)) {
00871 ast_mutex_unlock(&p->lock);
00872 if (p->owner) {
00873 ast_channel_unlock(p->owner);
00874 }
00875 usleep(1);
00876 if (p->owner) {
00877 ast_channel_lock(p->owner);
00878 }
00879 ast_mutex_lock(&p->lock);
00880 }
00881
00882 p->owner = NULL;
00883 if (p->chan) {
00884 ast_queue_hangup(p->chan);
00885 ast_channel_unlock(p->chan);
00886 }
00887 }
00888
00889 ast->tech_pvt = NULL;
00890
00891 if (!p->owner && !p->chan) {
00892
00893 glaredetect = ast_test_flag(p, LOCAL_GLARE_DETECT);
00894
00895
00896 if (glaredetect)
00897 ast_set_flag(p, LOCAL_CANCEL_QUEUE);
00898
00899 AST_LIST_LOCK(&locals);
00900 AST_LIST_REMOVE(&locals, p, list);
00901 AST_LIST_UNLOCK(&locals);
00902 ast_mutex_unlock(&p->lock);
00903
00904 if (!glaredetect) {
00905 p = local_pvt_destroy(p);
00906 }
00907 return 0;
00908 }
00909 if (p->chan && !ast_test_flag(p, LOCAL_LAUNCHED_PBX))
00910
00911 ochan = p->chan;
00912 else
00913 res = local_queue_frame(p, isoutbound, &f, NULL, 1);
00914 if (!res)
00915 ast_mutex_unlock(&p->lock);
00916 if (ochan)
00917 ast_hangup(ochan);
00918 return 0;
00919 }
00920
00921
00922 static struct local_pvt *local_alloc(const char *data, int format)
00923 {
00924 struct local_pvt *tmp = NULL;
00925 char *c = NULL, *opts = NULL;
00926
00927 if (!(tmp = ast_calloc(1, sizeof(*tmp))))
00928 return NULL;
00929
00930
00931 ast_mutex_init(&tmp->lock);
00932 ast_copy_string(tmp->exten, data, sizeof(tmp->exten));
00933
00934 memcpy(&tmp->jb_conf, &g_jb_conf, sizeof(tmp->jb_conf));
00935
00936
00937 if ((opts = strchr(tmp->exten, '/'))) {
00938 *opts++ = '\0';
00939 if (strchr(opts, 'n'))
00940 ast_set_flag(tmp, LOCAL_NO_OPTIMIZATION);
00941 if (strchr(opts, 'j')) {
00942 if (ast_test_flag(tmp, LOCAL_NO_OPTIMIZATION))
00943 ast_set_flag(&tmp->jb_conf, AST_JB_ENABLED);
00944 else {
00945 ast_log(LOG_ERROR, "You must use the 'n' option for chan_local "
00946 "to use the 'j' option to enable the jitterbuffer\n");
00947 }
00948 }
00949 if (strchr(opts, 'b')) {
00950 ast_set_flag(tmp, LOCAL_BRIDGE);
00951 }
00952 if (strchr(opts, 'm')) {
00953 ast_set_flag(tmp, LOCAL_MOH_PASSTHRU);
00954 }
00955 }
00956
00957
00958 if ((c = strchr(tmp->exten, '@')))
00959 *c++ = '\0';
00960
00961 ast_copy_string(tmp->context, c ? c : "default", sizeof(tmp->context));
00962
00963 tmp->reqformat = format;
00964
00965 #if 0
00966
00967
00968
00969 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
00970 ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
00971 tmp = local_pvt_destroy(tmp);
00972 } else {
00973 #endif
00974
00975 AST_LIST_LOCK(&locals);
00976 AST_LIST_INSERT_HEAD(&locals, tmp, list);
00977 AST_LIST_UNLOCK(&locals);
00978 #if 0
00979 }
00980 #endif
00981
00982 return tmp;
00983 }
00984
00985
00986 static struct ast_channel *local_new(struct local_pvt *p, int state, const char *linkedid)
00987 {
00988 struct ast_channel *tmp = NULL, *tmp2 = NULL;
00989 int randnum = ast_random() & 0xffff, fmt = 0;
00990 const char *t;
00991 int ama;
00992
00993
00994
00995 if (p->owner && p->owner->accountcode)
00996 t = p->owner->accountcode;
00997 else
00998 t = "";
00999
01000 if (p->owner)
01001 ama = p->owner->amaflags;
01002 else
01003 ama = 0;
01004 if (!(tmp = ast_channel_alloc(1, state, 0, 0, t, p->exten, p->context, linkedid, ama, "Local/%s@%s-%04x;1", p->exten, p->context, randnum))
01005 || !(tmp2 = ast_channel_alloc(1, AST_STATE_RING, 0, 0, t, p->exten, p->context, linkedid, ama, "Local/%s@%s-%04x;2", p->exten, p->context, randnum))) {
01006 if (tmp) {
01007 tmp = ast_channel_release(tmp);
01008 }
01009 ast_log(LOG_WARNING, "Unable to allocate channel structure(s)\n");
01010 return NULL;
01011 }
01012
01013 tmp2->tech = tmp->tech = &local_tech;
01014
01015 tmp->nativeformats = p->reqformat;
01016 tmp2->nativeformats = p->reqformat;
01017
01018
01019 fmt = ast_best_codec(p->reqformat);
01020 tmp->writeformat = fmt;
01021 tmp2->writeformat = fmt;
01022 tmp->rawwriteformat = fmt;
01023 tmp2->rawwriteformat = fmt;
01024 tmp->readformat = fmt;
01025 tmp2->readformat = fmt;
01026 tmp->rawreadformat = fmt;
01027 tmp2->rawreadformat = fmt;
01028
01029 tmp->tech_pvt = p;
01030 tmp2->tech_pvt = p;
01031
01032 p->owner = tmp;
01033 p->chan = tmp2;
01034 p->u_owner = ast_module_user_add(p->owner);
01035 p->u_chan = ast_module_user_add(p->chan);
01036
01037 ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
01038 ast_copy_string(tmp2->context, p->context, sizeof(tmp2->context));
01039 ast_copy_string(tmp2->exten, p->exten, sizeof(tmp->exten));
01040 tmp->priority = 1;
01041 tmp2->priority = 1;
01042
01043 ast_jb_configure(tmp, &p->jb_conf);
01044
01045 return tmp;
01046 }
01047
01048
01049 static struct ast_channel *local_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
01050 {
01051 struct local_pvt *p = NULL;
01052 struct ast_channel *chan = NULL;
01053
01054
01055 if ((p = local_alloc(data, format))) {
01056 if (!(chan = local_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL))) {
01057 AST_LIST_LOCK(&locals);
01058 AST_LIST_REMOVE(&locals, p, list);
01059 AST_LIST_UNLOCK(&locals);
01060 p = local_pvt_destroy(p);
01061 }
01062 if (chan && ast_channel_cc_params_init(chan, requestor ? ast_channel_get_cc_config_params((struct ast_channel *)requestor) : NULL)) {
01063 chan = ast_channel_release(chan);
01064 p = local_pvt_destroy(p);
01065 }
01066 }
01067
01068 return chan;
01069 }
01070
01071
01072 static char *locals_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01073 {
01074 struct local_pvt *p = NULL;
01075
01076 switch (cmd) {
01077 case CLI_INIT:
01078 e->command = "local show channels";
01079 e->usage =
01080 "Usage: local show channels\n"
01081 " Provides summary information on active local proxy channels.\n";
01082 return NULL;
01083 case CLI_GENERATE:
01084 return NULL;
01085 }
01086
01087 if (a->argc != 3)
01088 return CLI_SHOWUSAGE;
01089
01090 AST_LIST_LOCK(&locals);
01091 if (!AST_LIST_EMPTY(&locals)) {
01092 AST_LIST_TRAVERSE(&locals, p, list) {
01093 ast_mutex_lock(&p->lock);
01094 ast_cli(a->fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
01095 ast_mutex_unlock(&p->lock);
01096 }
01097 } else
01098 ast_cli(a->fd, "No local channels in use\n");
01099 AST_LIST_UNLOCK(&locals);
01100
01101 return CLI_SUCCESS;
01102 }
01103
01104 static struct ast_cli_entry cli_local[] = {
01105 AST_CLI_DEFINE(locals_show, "List status of local channels"),
01106 };
01107
01108 static int manager_optimize_away(struct mansession *s, const struct message *m)
01109 {
01110 const char *channel;
01111 struct local_pvt *p, *tmp = NULL;
01112 struct ast_channel *c;
01113 int found = 0;
01114
01115 channel = astman_get_header(m, "Channel");
01116
01117 if (ast_strlen_zero(channel)) {
01118 astman_send_error(s, m, "'Channel' not specified.");
01119 return 0;
01120 }
01121
01122 c = ast_channel_get_by_name(channel);
01123 if (!c) {
01124 astman_send_error(s, m, "Channel does not exist.");
01125 return 0;
01126 }
01127
01128 p = c->tech_pvt;
01129 ast_channel_unref(c);
01130 c = NULL;
01131
01132 if (AST_LIST_LOCK(&locals)) {
01133 astman_send_error(s, m, "Unable to lock the monitor");
01134 return 0;
01135 }
01136
01137
01138 AST_LIST_TRAVERSE(&locals, tmp, list) {
01139 if (tmp == p) {
01140 ast_mutex_lock(&tmp->lock);
01141 found = 1;
01142 ast_clear_flag(tmp, LOCAL_NO_OPTIMIZATION);
01143 ast_mutex_unlock(&tmp->lock);
01144 break;
01145 }
01146 }
01147 AST_LIST_UNLOCK(&locals);
01148
01149 if (found) {
01150 astman_send_ack(s, m, "Queued channel to be optimized away");
01151 } else {
01152 astman_send_error(s, m, "Unable to find channel");
01153 }
01154
01155 return 0;
01156 }
01157
01158
01159
01160 static int load_module(void)
01161 {
01162
01163 if (ast_channel_register(&local_tech)) {
01164 ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
01165 return AST_MODULE_LOAD_FAILURE;
01166 }
01167 ast_cli_register_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
01168 ast_manager_register_xml("LocalOptimizeAway", EVENT_FLAG_SYSTEM|EVENT_FLAG_CALL, manager_optimize_away);
01169
01170 return AST_MODULE_LOAD_SUCCESS;
01171 }
01172
01173
01174 static int unload_module(void)
01175 {
01176 struct local_pvt *p = NULL;
01177
01178
01179 ast_cli_unregister_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
01180 ast_manager_unregister("LocalOptimizeAway");
01181 ast_channel_unregister(&local_tech);
01182 if (!AST_LIST_LOCK(&locals)) {
01183
01184 AST_LIST_TRAVERSE(&locals, p, list) {
01185 if (p->owner)
01186 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
01187 }
01188 AST_LIST_UNLOCK(&locals);
01189 } else {
01190 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
01191 return -1;
01192 }
01193 return 0;
01194 }
01195
01196 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Local Proxy Channel (Note: used internally by other modules)",
01197 .load = load_module,
01198 .unload = unload_module,
01199 .load_pri = AST_MODPRI_CHANNEL_DRIVER,
01200 );