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