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: 273831 $")
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/rtp.h"
00043 #include "asterisk/acl.h"
00044 #include "asterisk/callerid.h"
00045 #include "asterisk/file.h"
00046 #include "asterisk/cli.h"
00047 #include "asterisk/app.h"
00048 #include "asterisk/musiconhold.h"
00049 #include "asterisk/manager.h"
00050 #include "asterisk/stringfields.h"
00051 #include "asterisk/devicestate.h"
00052
00053 static const char tdesc[] = "Local Proxy Channel Driver";
00054
00055 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
00056
00057 static struct ast_jb_conf g_jb_conf = {
00058 .flags = 0,
00059 .max_size = -1,
00060 .resync_threshold = -1,
00061 .impl = "",
00062 .target_extra = -1,
00063 };
00064
00065 static struct ast_channel *local_request(const char *type, int format, void *data, int *cause);
00066 static int local_digit_begin(struct ast_channel *ast, char digit);
00067 static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
00068 static int local_call(struct ast_channel *ast, char *dest, int timeout);
00069 static int local_hangup(struct ast_channel *ast);
00070 static int local_answer(struct ast_channel *ast);
00071 static struct ast_frame *local_read(struct ast_channel *ast);
00072 static int local_write(struct ast_channel *ast, struct ast_frame *f);
00073 static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
00074 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00075 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
00076 static int local_sendtext(struct ast_channel *ast, const char *text);
00077 static int local_devicestate(void *data);
00078 static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
00079
00080
00081 static const struct ast_channel_tech local_tech = {
00082 .type = "Local",
00083 .description = tdesc,
00084 .capabilities = -1,
00085 .requester = local_request,
00086 .send_digit_begin = local_digit_begin,
00087 .send_digit_end = local_digit_end,
00088 .call = local_call,
00089 .hangup = local_hangup,
00090 .answer = local_answer,
00091 .read = local_read,
00092 .write = local_write,
00093 .write_video = local_write,
00094 .exception = local_read,
00095 .indicate = local_indicate,
00096 .fixup = local_fixup,
00097 .send_html = local_sendhtml,
00098 .send_text = local_sendtext,
00099 .devicestate = local_devicestate,
00100 .bridged_channel = local_bridgedchannel,
00101 };
00102
00103 struct local_pvt {
00104 ast_mutex_t lock;
00105 unsigned int flags;
00106 char context[AST_MAX_CONTEXT];
00107 char exten[AST_MAX_EXTENSION];
00108 int reqformat;
00109 struct ast_jb_conf jb_conf;
00110 struct ast_channel *owner;
00111 struct ast_channel *chan;
00112 struct ast_module_user *u_owner;
00113 struct ast_module_user *u_chan;
00114 AST_LIST_ENTRY(local_pvt) list;
00115 };
00116
00117 #define LOCAL_GLARE_DETECT (1 << 0)
00118 #define LOCAL_CANCEL_QUEUE (1 << 1)
00119 #define LOCAL_ALREADY_MASQED (1 << 2)
00120 #define LOCAL_LAUNCHED_PBX (1 << 3)
00121 #define LOCAL_NO_OPTIMIZATION (1 << 4)
00122 #define LOCAL_BRIDGE (1 << 5)
00123 #define LOCAL_MOH_PASSTHRU (1 << 6)
00124
00125 static AST_LIST_HEAD_STATIC(locals, local_pvt);
00126
00127
00128 static int local_devicestate(void *data)
00129 {
00130 char *exten = ast_strdupa(data);
00131 char *context = NULL, *opts = NULL;
00132 int res;
00133 struct local_pvt *lp;
00134
00135 if (!(context = strchr(exten, '@'))) {
00136 ast_log(LOG_WARNING, "Someone used Local/%s somewhere without a @context. This is bad.\n", exten);
00137 return AST_DEVICE_INVALID;
00138 }
00139
00140 *context++ = '\0';
00141
00142
00143 if ((opts = strchr(context, '/')))
00144 *opts = '\0';
00145
00146 ast_debug(3, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
00147
00148 res = ast_exists_extension(NULL, context, exten, 1, NULL);
00149 if (!res)
00150 return AST_DEVICE_INVALID;
00151
00152 res = AST_DEVICE_NOT_INUSE;
00153 AST_LIST_LOCK(&locals);
00154 AST_LIST_TRAVERSE(&locals, lp, list) {
00155 if (!strcmp(exten, lp->exten) && !strcmp(context, lp->context) && lp->owner) {
00156 res = AST_DEVICE_INUSE;
00157 break;
00158 }
00159 }
00160 AST_LIST_UNLOCK(&locals);
00161
00162 return res;
00163 }
00164
00165
00166
00167
00168 static struct local_pvt *local_pvt_destroy(struct local_pvt *pvt)
00169 {
00170 ast_mutex_destroy(&pvt->lock);
00171 ast_free(pvt);
00172 return NULL;
00173 }
00174
00175
00176 static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge)
00177 {
00178 struct local_pvt *p = bridge->tech_pvt;
00179 struct ast_channel *bridged = bridge;
00180
00181 if (!p) {
00182 ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning <none>\n",
00183 chan->name, bridge->name);
00184 return NULL;
00185 }
00186
00187 ast_mutex_lock(&p->lock);
00188
00189 if (ast_test_flag(p, LOCAL_BRIDGE)) {
00190
00191 bridged = (bridge == p->owner ? p->chan : p->owner);
00192
00193
00194 if (!bridged) {
00195 bridged = bridge;
00196 } else if (bridged->_bridge) {
00197 bridged = bridged->_bridge;
00198 }
00199 }
00200
00201 ast_mutex_unlock(&p->lock);
00202
00203 return bridged;
00204 }
00205
00206 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f,
00207 struct ast_channel *us, int us_locked)
00208 {
00209 struct ast_channel *other = NULL;
00210
00211
00212 other = isoutbound ? p->owner : p->chan;
00213
00214 if (!other) {
00215 return 0;
00216 }
00217
00218
00219 if (us && us->generator && other->generator) {
00220 return 0;
00221 }
00222
00223
00224 ast_set_flag(p, LOCAL_GLARE_DETECT);
00225
00226
00227 while (other && ast_channel_trylock(other)) {
00228 int res;
00229 if ((res = ast_mutex_unlock(&p->lock))) {
00230 ast_log(LOG_ERROR, "chan_local bug! '&p->lock' was not locked when entering local_queue_frame! (%s)\n", strerror(res));
00231 return -1;
00232 }
00233 if (us && us_locked) {
00234 do {
00235 CHANNEL_DEADLOCK_AVOIDANCE(us);
00236 } while (ast_mutex_trylock(&p->lock));
00237 } else {
00238 usleep(1);
00239 ast_mutex_lock(&p->lock);
00240 }
00241 other = isoutbound ? p->owner : p->chan;
00242 }
00243
00244
00245
00246
00247 if (ast_test_flag(p, LOCAL_CANCEL_QUEUE)) {
00248
00249
00250 ast_mutex_unlock(&p->lock);
00251 p = local_pvt_destroy(p);
00252 if (other) {
00253 ast_channel_unlock(other);
00254 }
00255 return -1;
00256 }
00257
00258 if (other) {
00259 if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_RINGING) {
00260 ast_setstate(other, AST_STATE_RINGING);
00261 }
00262 ast_queue_frame(other, f);
00263 ast_channel_unlock(other);
00264 }
00265
00266 ast_clear_flag(p, LOCAL_GLARE_DETECT);
00267
00268 return 0;
00269 }
00270
00271 static int local_answer(struct ast_channel *ast)
00272 {
00273 struct local_pvt *p = ast->tech_pvt;
00274 int isoutbound;
00275 int res = -1;
00276
00277 if (!p)
00278 return -1;
00279
00280 ast_mutex_lock(&p->lock);
00281 isoutbound = IS_OUTBOUND(ast, p);
00282 if (isoutbound) {
00283
00284 struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
00285 res = local_queue_frame(p, isoutbound, &answer, ast, 1);
00286 } else
00287 ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
00288 if (!res)
00289 ast_mutex_unlock(&p->lock);
00290 return res;
00291 }
00292
00293
00294
00295
00296
00297 static void check_bridge(struct local_pvt *p)
00298 {
00299 struct ast_channel_monitor *tmp;
00300 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)))
00301 return;
00302
00303
00304
00305
00306
00307
00308 if (p->chan->_bridge && AST_LIST_EMPTY(&p->owner->readq)) {
00309
00310
00311
00312
00313 if (!ast_channel_trylock(p->chan->_bridge)) {
00314 if (!ast_check_hangup(p->chan->_bridge)) {
00315 if (!ast_channel_trylock(p->owner)) {
00316 if (!ast_check_hangup(p->owner)) {
00317 if (p->owner->monitor && !p->chan->_bridge->monitor) {
00318
00319
00320
00321
00322
00323 tmp = p->owner->monitor;
00324 p->owner->monitor = p->chan->_bridge->monitor;
00325 p->chan->_bridge->monitor = tmp;
00326 }
00327 if (p->chan->audiohooks) {
00328 struct ast_audiohook_list *audiohooks_swapper;
00329 audiohooks_swapper = p->chan->audiohooks;
00330 p->chan->audiohooks = p->owner->audiohooks;
00331 p->owner->audiohooks = audiohooks_swapper;
00332 }
00333 ast_app_group_update(p->chan, p->owner);
00334 ast_channel_masquerade(p->owner, p->chan->_bridge);
00335 ast_set_flag(p, LOCAL_ALREADY_MASQED);
00336 }
00337 ast_channel_unlock(p->owner);
00338 }
00339 ast_channel_unlock(p->chan->_bridge);
00340 }
00341 }
00342 }
00343 }
00344
00345 static struct ast_frame *local_read(struct ast_channel *ast)
00346 {
00347 return &ast_null_frame;
00348 }
00349
00350 static int local_write(struct ast_channel *ast, struct ast_frame *f)
00351 {
00352 struct local_pvt *p = ast->tech_pvt;
00353 int res = -1;
00354 int isoutbound;
00355
00356 if (!p)
00357 return -1;
00358
00359
00360 ast_mutex_lock(&p->lock);
00361 isoutbound = IS_OUTBOUND(ast, p);
00362 if (isoutbound && f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO))
00363 check_bridge(p);
00364 if (!ast_test_flag(p, LOCAL_ALREADY_MASQED))
00365 res = local_queue_frame(p, isoutbound, f, ast, 1);
00366 else {
00367 ast_debug(1, "Not posting to queue since already masked on '%s'\n", ast->name);
00368 res = 0;
00369 }
00370 if (!res)
00371 ast_mutex_unlock(&p->lock);
00372 return res;
00373 }
00374
00375 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00376 {
00377 struct local_pvt *p = newchan->tech_pvt;
00378
00379 if (!p)
00380 return -1;
00381
00382 ast_mutex_lock(&p->lock);
00383
00384 if ((p->owner != oldchan) && (p->chan != oldchan)) {
00385 ast_log(LOG_WARNING, "Old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
00386 ast_mutex_unlock(&p->lock);
00387 return -1;
00388 }
00389 if (p->owner == oldchan)
00390 p->owner = newchan;
00391 else
00392 p->chan = newchan;
00393 ast_mutex_unlock(&p->lock);
00394 return 0;
00395 }
00396
00397 static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
00398 {
00399 struct local_pvt *p = ast->tech_pvt;
00400 int res = 0;
00401 struct ast_frame f = { AST_FRAME_CONTROL, };
00402 int isoutbound;
00403
00404 if (!p)
00405 return -1;
00406
00407
00408 if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_HOLD) {
00409 ast_moh_start(ast, data, NULL);
00410 } else if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_UNHOLD) {
00411 ast_moh_stop(ast);
00412 } else {
00413
00414 ast_mutex_lock(&p->lock);
00415 isoutbound = IS_OUTBOUND(ast, p);
00416 f.subclass = condition;
00417 f.data.ptr = (void*)data;
00418 f.datalen = datalen;
00419 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 1)))
00420 ast_mutex_unlock(&p->lock);
00421 }
00422
00423 return res;
00424 }
00425
00426 static int local_digit_begin(struct ast_channel *ast, char digit)
00427 {
00428 struct local_pvt *p = ast->tech_pvt;
00429 int res = -1;
00430 struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
00431 int isoutbound;
00432
00433 if (!p)
00434 return -1;
00435
00436 ast_mutex_lock(&p->lock);
00437 isoutbound = IS_OUTBOUND(ast, p);
00438 f.subclass = digit;
00439 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00440 ast_mutex_unlock(&p->lock);
00441
00442 return res;
00443 }
00444
00445 static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
00446 {
00447 struct local_pvt *p = ast->tech_pvt;
00448 int res = -1;
00449 struct ast_frame f = { AST_FRAME_DTMF_END, };
00450 int isoutbound;
00451
00452 if (!p)
00453 return -1;
00454
00455 ast_mutex_lock(&p->lock);
00456 isoutbound = IS_OUTBOUND(ast, p);
00457 f.subclass = digit;
00458 f.len = duration;
00459 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00460 ast_mutex_unlock(&p->lock);
00461
00462 return res;
00463 }
00464
00465 static int local_sendtext(struct ast_channel *ast, const char *text)
00466 {
00467 struct local_pvt *p = ast->tech_pvt;
00468 int res = -1;
00469 struct ast_frame f = { AST_FRAME_TEXT, };
00470 int isoutbound;
00471
00472 if (!p)
00473 return -1;
00474
00475 ast_mutex_lock(&p->lock);
00476 isoutbound = IS_OUTBOUND(ast, p);
00477 f.data.ptr = (char *) text;
00478 f.datalen = strlen(text) + 1;
00479 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00480 ast_mutex_unlock(&p->lock);
00481 return res;
00482 }
00483
00484 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
00485 {
00486 struct local_pvt *p = ast->tech_pvt;
00487 int res = -1;
00488 struct ast_frame f = { AST_FRAME_HTML, };
00489 int isoutbound;
00490
00491 if (!p)
00492 return -1;
00493
00494 ast_mutex_lock(&p->lock);
00495 isoutbound = IS_OUTBOUND(ast, p);
00496 f.subclass = subclass;
00497 f.data.ptr = (char *)data;
00498 f.datalen = datalen;
00499 if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00500 ast_mutex_unlock(&p->lock);
00501 return res;
00502 }
00503
00504
00505
00506 static int local_call(struct ast_channel *ast, char *dest, int timeout)
00507 {
00508 struct local_pvt *p = ast->tech_pvt;
00509 int res;
00510 struct ast_var_t *varptr = NULL, *new;
00511 size_t len, namelen;
00512
00513 if (!p)
00514 return -1;
00515
00516 ast_mutex_lock(&p->lock);
00517
00518
00519
00520
00521
00522 p->chan->cid.cid_dnid = ast_strdup(p->owner->cid.cid_dnid);
00523 p->chan->cid.cid_num = ast_strdup(p->owner->cid.cid_num);
00524 p->chan->cid.cid_name = ast_strdup(p->owner->cid.cid_name);
00525 p->chan->cid.cid_rdnis = ast_strdup(p->owner->cid.cid_rdnis);
00526 p->chan->cid.cid_ani = ast_strdup(p->owner->cid.cid_ani);
00527 p->chan->cid.cid_pres = p->owner->cid.cid_pres;
00528 p->chan->cid.cid_ani2 = p->owner->cid.cid_ani2;
00529 p->chan->cid.cid_ton = p->owner->cid.cid_ton;
00530 p->chan->cid.cid_tns = p->owner->cid.cid_tns;
00531 ast_string_field_set(p->chan, language, p->owner->language);
00532 ast_string_field_set(p->chan, accountcode, p->owner->accountcode);
00533 ast_string_field_set(p->chan, musicclass, p->owner->musicclass);
00534 ast_cdr_update(p->chan);
00535 p->chan->cdrflags = p->owner->cdrflags;
00536
00537 if (!ast_exists_extension(NULL, p->chan->context, p->chan->exten, 1, p->owner->cid.cid_num)) {
00538 ast_log(LOG_NOTICE, "No such extension/context %s@%s while calling Local channel\n", p->chan->exten, p->chan->context);
00539 ast_mutex_unlock(&p->lock);
00540 return -1;
00541 }
00542
00543
00544 if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
00545 ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
00546 }
00547
00548
00549
00550 AST_LIST_TRAVERSE(&p->owner->varshead, varptr, entries) {
00551 namelen = strlen(varptr->name);
00552 len = sizeof(struct ast_var_t) + namelen + strlen(varptr->value) + 2;
00553 if ((new = ast_calloc(1, len))) {
00554 memcpy(new, varptr, len);
00555 new->value = &(new->name[0]) + namelen + 1;
00556 AST_LIST_INSERT_TAIL(&p->chan->varshead, new, entries);
00557 }
00558 }
00559 ast_channel_datastore_inherit(p->owner, p->chan);
00560
00561
00562 if (!(res = ast_pbx_start(p->chan)))
00563 ast_set_flag(p, LOCAL_LAUNCHED_PBX);
00564
00565 ast_mutex_unlock(&p->lock);
00566 return res;
00567 }
00568
00569
00570 static int local_hangup(struct ast_channel *ast)
00571 {
00572 struct local_pvt *p = ast->tech_pvt;
00573 int isoutbound;
00574 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP, .data.uint32 = ast->hangupcause };
00575 struct ast_channel *ochan = NULL;
00576 int glaredetect = 0, res = 0;
00577
00578 if (!p)
00579 return -1;
00580
00581 ast_mutex_lock(&p->lock);
00582
00583 isoutbound = IS_OUTBOUND(ast, p);
00584
00585 if (p->chan && ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
00586 ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
00587 ast_debug(2, "This local call has the ANSWERED_ELSEWHERE flag set.\n");
00588 }
00589
00590 if (isoutbound) {
00591 const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
00592 if ((status) && (p->owner)) {
00593
00594 while (p->owner && ast_channel_trylock(p->owner)) {
00595 ast_mutex_unlock(&p->lock);
00596 if (p->chan) {
00597 ast_channel_unlock(p->chan);
00598 }
00599 usleep(1);
00600 if (p->chan) {
00601 ast_channel_lock(p->chan);
00602 }
00603 ast_mutex_lock(&p->lock);
00604 }
00605 if (p->owner) {
00606 pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
00607 ast_channel_unlock(p->owner);
00608 }
00609 }
00610 p->chan = NULL;
00611 ast_clear_flag(p, LOCAL_LAUNCHED_PBX);
00612 ast_module_user_remove(p->u_chan);
00613 } else {
00614 ast_module_user_remove(p->u_owner);
00615 while (p->chan && ast_channel_trylock(p->chan)) {
00616 ast_mutex_unlock(&p->lock);
00617 if (p->owner) {
00618 ast_channel_unlock(p->owner);
00619 }
00620 usleep(1);
00621 if (p->owner) {
00622 ast_channel_lock(p->owner);
00623 }
00624 ast_mutex_lock(&p->lock);
00625 }
00626
00627 p->owner = NULL;
00628 if (p->chan) {
00629 ast_queue_hangup(p->chan);
00630 ast_channel_unlock(p->chan);
00631 }
00632 }
00633
00634 ast->tech_pvt = NULL;
00635
00636 if (!p->owner && !p->chan) {
00637
00638 glaredetect = ast_test_flag(p, LOCAL_GLARE_DETECT);
00639
00640
00641 if (glaredetect)
00642 ast_set_flag(p, LOCAL_CANCEL_QUEUE);
00643
00644 AST_LIST_LOCK(&locals);
00645 AST_LIST_REMOVE(&locals, p, list);
00646 AST_LIST_UNLOCK(&locals);
00647 ast_mutex_unlock(&p->lock);
00648
00649 if (!glaredetect) {
00650 p = local_pvt_destroy(p);
00651 }
00652 return 0;
00653 }
00654 if (p->chan && !ast_test_flag(p, LOCAL_LAUNCHED_PBX))
00655
00656 ochan = p->chan;
00657 else
00658 res = local_queue_frame(p, isoutbound, &f, NULL, 1);
00659 if (!res)
00660 ast_mutex_unlock(&p->lock);
00661 if (ochan)
00662 ast_hangup(ochan);
00663 return 0;
00664 }
00665
00666
00667 static struct local_pvt *local_alloc(const char *data, int format)
00668 {
00669 struct local_pvt *tmp = NULL;
00670 char *c = NULL, *opts = NULL;
00671
00672 if (!(tmp = ast_calloc(1, sizeof(*tmp))))
00673 return NULL;
00674
00675
00676 ast_mutex_init(&tmp->lock);
00677 ast_copy_string(tmp->exten, data, sizeof(tmp->exten));
00678
00679 memcpy(&tmp->jb_conf, &g_jb_conf, sizeof(tmp->jb_conf));
00680
00681
00682 if ((opts = strchr(tmp->exten, '/'))) {
00683 *opts++ = '\0';
00684 if (strchr(opts, 'n'))
00685 ast_set_flag(tmp, LOCAL_NO_OPTIMIZATION);
00686 if (strchr(opts, 'j')) {
00687 if (ast_test_flag(tmp, LOCAL_NO_OPTIMIZATION))
00688 ast_set_flag(&tmp->jb_conf, AST_JB_ENABLED);
00689 else {
00690 ast_log(LOG_ERROR, "You must use the 'n' option for chan_local "
00691 "to use the 'j' option to enable the jitterbuffer\n");
00692 }
00693 }
00694 if (strchr(opts, 'b')) {
00695 ast_set_flag(tmp, LOCAL_BRIDGE);
00696 }
00697 if (strchr(opts, 'm')) {
00698 ast_set_flag(tmp, LOCAL_MOH_PASSTHRU);
00699 }
00700 }
00701
00702
00703 if ((c = strchr(tmp->exten, '@')))
00704 *c++ = '\0';
00705
00706 ast_copy_string(tmp->context, c ? c : "default", sizeof(tmp->context));
00707
00708 tmp->reqformat = format;
00709
00710 #if 0
00711
00712
00713
00714 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
00715 ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
00716 tmp = local_pvt_destroy(tmp);
00717 } else {
00718 #endif
00719
00720 AST_LIST_LOCK(&locals);
00721 AST_LIST_INSERT_HEAD(&locals, tmp, list);
00722 AST_LIST_UNLOCK(&locals);
00723 #if 0
00724 }
00725 #endif
00726
00727 return tmp;
00728 }
00729
00730
00731 static struct ast_channel *local_new(struct local_pvt *p, int state)
00732 {
00733 struct ast_channel *tmp = NULL, *tmp2 = NULL;
00734 int randnum = ast_random() & 0xffff, fmt = 0;
00735 const char *t;
00736 int ama;
00737
00738
00739
00740 if (p->owner && p->owner->accountcode)
00741 t = p->owner->accountcode;
00742 else
00743 t = "";
00744
00745 if (p->owner)
00746 ama = p->owner->amaflags;
00747 else
00748 ama = 0;
00749 if (!(tmp = ast_channel_alloc(1, state, 0, 0, t, p->exten, p->context, ama, "Local/%s@%s-%04x;1", p->exten, p->context, randnum))
00750 || !(tmp2 = ast_channel_alloc(1, AST_STATE_RING, 0, 0, t, p->exten, p->context, ama, "Local/%s@%s-%04x;2", p->exten, p->context, randnum))) {
00751 if (tmp)
00752 ast_channel_free(tmp);
00753 if (tmp2)
00754 ast_channel_free(tmp2);
00755 ast_log(LOG_WARNING, "Unable to allocate channel structure(s)\n");
00756 return NULL;
00757 }
00758
00759 tmp2->tech = tmp->tech = &local_tech;
00760
00761 tmp->nativeformats = p->reqformat;
00762 tmp2->nativeformats = p->reqformat;
00763
00764
00765 fmt = ast_best_codec(p->reqformat);
00766 tmp->writeformat = fmt;
00767 tmp2->writeformat = fmt;
00768 tmp->rawwriteformat = fmt;
00769 tmp2->rawwriteformat = fmt;
00770 tmp->readformat = fmt;
00771 tmp2->readformat = fmt;
00772 tmp->rawreadformat = fmt;
00773 tmp2->rawreadformat = fmt;
00774
00775 tmp->tech_pvt = p;
00776 tmp2->tech_pvt = p;
00777
00778 p->owner = tmp;
00779 p->chan = tmp2;
00780 p->u_owner = ast_module_user_add(p->owner);
00781 p->u_chan = ast_module_user_add(p->chan);
00782
00783 ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
00784 ast_copy_string(tmp2->context, p->context, sizeof(tmp2->context));
00785 ast_copy_string(tmp2->exten, p->exten, sizeof(tmp->exten));
00786 tmp->priority = 1;
00787 tmp2->priority = 1;
00788
00789 ast_jb_configure(tmp, &p->jb_conf);
00790
00791 return tmp;
00792 }
00793
00794
00795 static struct ast_channel *local_request(const char *type, int format, void *data, int *cause)
00796 {
00797 struct local_pvt *p = NULL;
00798 struct ast_channel *chan = NULL;
00799
00800
00801 if ((p = local_alloc(data, format))) {
00802 if (!(chan = local_new(p, AST_STATE_DOWN))) {
00803 AST_LIST_LOCK(&locals);
00804 AST_LIST_REMOVE(&locals, p, list);
00805 AST_LIST_UNLOCK(&locals);
00806 p = local_pvt_destroy(p);
00807 }
00808 }
00809
00810 return chan;
00811 }
00812
00813
00814 static char *locals_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00815 {
00816 struct local_pvt *p = NULL;
00817
00818 switch (cmd) {
00819 case CLI_INIT:
00820 e->command = "local show channels";
00821 e->usage =
00822 "Usage: local show channels\n"
00823 " Provides summary information on active local proxy channels.\n";
00824 return NULL;
00825 case CLI_GENERATE:
00826 return NULL;
00827 }
00828
00829 if (a->argc != 3)
00830 return CLI_SHOWUSAGE;
00831
00832 AST_LIST_LOCK(&locals);
00833 if (!AST_LIST_EMPTY(&locals)) {
00834 AST_LIST_TRAVERSE(&locals, p, list) {
00835 ast_mutex_lock(&p->lock);
00836 ast_cli(a->fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
00837 ast_mutex_unlock(&p->lock);
00838 }
00839 } else
00840 ast_cli(a->fd, "No local channels in use\n");
00841 AST_LIST_UNLOCK(&locals);
00842
00843 return CLI_SUCCESS;
00844 }
00845
00846 static struct ast_cli_entry cli_local[] = {
00847 AST_CLI_DEFINE(locals_show, "List status of local channels"),
00848 };
00849
00850
00851 static int load_module(void)
00852 {
00853
00854 if (ast_channel_register(&local_tech)) {
00855 ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
00856 return AST_MODULE_LOAD_FAILURE;
00857 }
00858 ast_cli_register_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
00859 return AST_MODULE_LOAD_SUCCESS;
00860 }
00861
00862
00863 static int unload_module(void)
00864 {
00865 struct local_pvt *p = NULL;
00866
00867
00868 ast_cli_unregister_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
00869 ast_channel_unregister(&local_tech);
00870 if (!AST_LIST_LOCK(&locals)) {
00871
00872 AST_LIST_TRAVERSE(&locals, p, list) {
00873 if (p->owner)
00874 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
00875 }
00876 AST_LIST_UNLOCK(&locals);
00877 } else {
00878 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
00879 return -1;
00880 }
00881 return 0;
00882 }
00883
00884 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Local Proxy Channel (Note: used internally by other modules)");