| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include "asterisk/astobj2.h" |
| 34 |
#include "asterisk/json.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/bridge.h" |
| 37 |
#include "asterisk/stasis_bridges.h" |
| 38 |
#include "asterisk/translate.h" |
| 39 |
#include "asterisk/stasis.h" |
| 40 |
#include "asterisk/stasis_channels.h" |
| 41 |
#include "asterisk/dial.h" |
| 42 |
#include "asterisk/linkedlists.h" |
| 43 |
#include "asterisk/utf8.h" |
| 44 |
#include "asterisk/vector.h" |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
#define NUM_MULTI_CHANNEL_BLOB_BUCKETS 7 |
| 139 |
|
| 140 |
static struct stasis_topic *channel_topic_all; |
| 141 |
static struct ao2_container *channel_cache; |
| 142 |
static struct ao2_container *channel_cache_by_name; |
| 143 |
|
| 144 |
struct ao2_container *ast_channel_cache_all(void) |
| 145 |
{ |
| 146 |
return ao2_bump(channel_cache); |
| 147 |
} |
| 148 |
|
| 149 |
struct stasis_topic *ast_channel_topic_all(void) |
| 150 |
{ |
| 151 |
return channel_topic_all; |
| 152 |
} |
| 153 |
|
| 154 |
struct ao2_container *ast_channel_cache_by_name(void) |
| 155 |
{ |
| 156 |
return ao2_bump(channel_cache_by_name); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
static int channel_snapshot_hash_cb(const void *obj, const int flags) |
| 164 |
{ |
| 165 |
const struct ast_channel_snapshot *object = obj; |
| 166 |
const char *key; |
| 167 |
|
| 168 |
switch (flags & OBJ_SEARCH_MASK) { |
| 169 |
case OBJ_SEARCH_KEY: |
| 170 |
key = obj; |
| 171 |
break; |
| 172 |
case OBJ_SEARCH_OBJECT: |
| 173 |
key = object->base->name; |
| 174 |
break; |
| 175 |
default: |
| 176 |
ast_assert(0); |
| 177 |
return 0; |
| 178 |
} |
| 179 |
return ast_str_case_hash(key); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
static int channel_snapshot_cmp_cb(void *obj, void *arg, int flags) |
| 187 |
{ |
| 188 |
const struct ast_channel_snapshot *object_left = obj; |
| 189 |
const struct ast_channel_snapshot *object_right = arg; |
| 190 |
const char *right_key = arg; |
| 191 |
int cmp; |
| 192 |
|
| 193 |
switch (flags & OBJ_SEARCH_MASK) { |
| 194 |
case OBJ_SEARCH_OBJECT: |
| 195 |
right_key = object_right->base->name; |
| 196 |
case OBJ_SEARCH_KEY: |
| 197 |
cmp = strcasecmp(object_left->base->name, right_key); |
| 198 |
break; |
| 199 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 200 |
cmp = strncasecmp(object_left->base->name, right_key, strlen(right_key)); |
| 201 |
break; |
| 202 |
default: |
| 203 |
cmp = 0; |
| 204 |
break; |
| 205 |
} |
| 206 |
if (cmp) { |
| 207 |
return 0; |
| 208 |
} |
| 209 |
return CMP_MATCH; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
static int channel_snapshot_uniqueid_hash_cb(const void *obj, const int flags) |
| 217 |
{ |
| 218 |
const struct ast_channel_snapshot *object = obj; |
| 219 |
const char *key; |
| 220 |
|
| 221 |
switch (flags & OBJ_SEARCH_MASK) { |
| 222 |
case OBJ_SEARCH_KEY: |
| 223 |
key = obj; |
| 224 |
break; |
| 225 |
case OBJ_SEARCH_OBJECT: |
| 226 |
key = object->base->uniqueid; |
| 227 |
break; |
| 228 |
default: |
| 229 |
ast_assert(0); |
| 230 |
return 0; |
| 231 |
} |
| 232 |
return ast_str_case_hash(key); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
static int channel_snapshot_uniqueid_cmp_cb(void *obj, void *arg, int flags) |
| 240 |
{ |
| 241 |
const struct ast_channel_snapshot *object_left = obj; |
| 242 |
const struct ast_channel_snapshot *object_right = arg; |
| 243 |
const char *right_key = arg; |
| 244 |
int cmp; |
| 245 |
|
| 246 |
switch (flags & OBJ_SEARCH_MASK) { |
| 247 |
case OBJ_SEARCH_OBJECT: |
| 248 |
right_key = object_right->base->uniqueid; |
| 249 |
case OBJ_SEARCH_KEY: |
| 250 |
cmp = strcasecmp(object_left->base->uniqueid, right_key); |
| 251 |
break; |
| 252 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 253 |
cmp = strncasecmp(object_left->base->uniqueid, right_key, strlen(right_key)); |
| 254 |
break; |
| 255 |
default: |
| 256 |
cmp = 0; |
| 257 |
break; |
| 258 |
} |
| 259 |
if (cmp) { |
| 260 |
return 0; |
| 261 |
} |
| 262 |
return CMP_MATCH; |
| 263 |
} |
| 264 |
|
| 265 |
static void channel_snapshot_dtor(void *obj) |
| 266 |
{ |
| 267 |
struct ast_channel_snapshot *snapshot = obj; |
| 268 |
|
| 269 |
ao2_cleanup(snapshot->base); |
| 270 |
ao2_cleanup(snapshot->peer); |
| 271 |
ao2_cleanup(snapshot->caller); |
| 272 |
ao2_cleanup(snapshot->connected); |
| 273 |
ao2_cleanup(snapshot->bridge); |
| 274 |
ao2_cleanup(snapshot->dialplan); |
| 275 |
ao2_cleanup(snapshot->hangup); |
| 276 |
ao2_cleanup(snapshot->manager_vars); |
| 277 |
ao2_cleanup(snapshot->ari_vars); |
| 278 |
} |
| 279 |
|
| 280 |
static void channel_snapshot_base_dtor(void *obj) |
| 281 |
{ |
| 282 |
struct ast_channel_snapshot_base *snapshot = obj; |
| 283 |
|
| 284 |
ast_string_field_free_memory(snapshot); |
| 285 |
} |
| 286 |
|
| 287 |
static struct ast_channel_snapshot_base *channel_snapshot_base_create(struct ast_channel *chan) |
| 288 |
{ |
| 289 |
struct ast_channel_snapshot_base *snapshot; |
| 290 |
|
| 291 |
snapshot = ao2_alloc_options(sizeof(*snapshot), channel_snapshot_base_dtor, |
| 292 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 293 |
if (!snapshot) { |
| 294 |
return NULL; |
| 295 |
} |
| 296 |
|
| 297 |
if (ast_string_field_init(snapshot, 256) || ast_string_field_init_extended(snapshot, protocol_id)) { |
| 298 |
ao2_ref(snapshot, -1); |
| 299 |
return NULL; |
| 300 |
} |
| 301 |
|
| 302 |
ast_string_field_set(snapshot, name, ast_channel_name(chan)); |
| 303 |
ast_string_field_set(snapshot, type, ast_channel_tech(chan)->type); |
| 304 |
ast_string_field_set(snapshot, accountcode, ast_channel_accountcode(chan)); |
| 305 |
ast_string_field_set(snapshot, userfield, ast_channel_userfield(chan)); |
| 306 |
ast_string_field_set(snapshot, uniqueid, ast_channel_uniqueid(chan)); |
| 307 |
ast_string_field_set(snapshot, language, ast_channel_language(chan)); |
| 308 |
ast_string_field_set(snapshot, tenantid, ast_channel_tenantid(chan)); |
| 309 |
|
| 310 |
snapshot->creationtime = ast_channel_creationtime(chan); |
| 311 |
snapshot->tech_properties = ast_channel_tech(chan)->properties; |
| 312 |
|
| 313 |
if (ast_channel_tech(chan)->get_pvt_uniqueid) { |
| 314 |
ast_string_field_set(snapshot, protocol_id, ast_channel_tech(chan)->get_pvt_uniqueid(chan)); |
| 315 |
} |
| 316 |
|
| 317 |
return snapshot; |
| 318 |
} |
| 319 |
|
| 320 |
static struct ast_channel_snapshot_peer *channel_snapshot_peer_create(struct ast_channel *chan) |
| 321 |
{ |
| 322 |
const char *linkedid = S_OR(ast_channel_linkedid(chan), ""); |
| 323 |
const char *peeraccount = S_OR(ast_channel_peeraccount(chan), ""); |
| 324 |
size_t linkedid_len = strlen(linkedid) + 1; |
| 325 |
size_t peeraccount_len = strlen(peeraccount) + 1; |
| 326 |
struct ast_channel_snapshot_peer *snapshot; |
| 327 |
|
| 328 |
snapshot = ao2_alloc_options(sizeof(*snapshot) + linkedid_len + peeraccount_len, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 329 |
if (!snapshot) { |
| 330 |
return NULL; |
| 331 |
} |
| 332 |
|
| 333 |
strcpy(snapshot->account, peeraccount); |
| 334 |
snapshot->linkedid = snapshot->account + peeraccount_len; |
| 335 |
ast_copy_string(snapshot->linkedid, linkedid, linkedid_len); |
| 336 |
|
| 337 |
return snapshot; |
| 338 |
} |
| 339 |
|
| 340 |
static void channel_snapshot_caller_dtor(void *obj) |
| 341 |
{ |
| 342 |
struct ast_channel_snapshot_caller *snapshot = obj; |
| 343 |
|
| 344 |
ast_string_field_free_memory(snapshot); |
| 345 |
} |
| 346 |
|
| 347 |
static struct ast_channel_snapshot_caller *channel_snapshot_caller_create(struct ast_channel *chan) |
| 348 |
{ |
| 349 |
struct ast_channel_snapshot_caller *snapshot; |
| 350 |
|
| 351 |
snapshot = ao2_alloc_options(sizeof(*snapshot), channel_snapshot_caller_dtor, |
| 352 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 353 |
if (!snapshot) { |
| 354 |
return NULL; |
| 355 |
} |
| 356 |
|
| 357 |
if (ast_string_field_init(snapshot, 256)) { |
| 358 |
ao2_ref(snapshot, -1); |
| 359 |
return NULL; |
| 360 |
} |
| 361 |
|
| 362 |
ast_string_field_set(snapshot, name, |
| 363 |
S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "")); |
| 364 |
ast_string_field_set(snapshot, number, |
| 365 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "")); |
| 366 |
ast_string_field_set(snapshot, subaddr, |
| 367 |
S_COR(ast_channel_caller(chan)->id.subaddress.valid, ast_channel_caller(chan)->id.subaddress.str, "")); |
| 368 |
ast_string_field_set(snapshot, ani, |
| 369 |
S_COR(ast_channel_caller(chan)->ani.number.valid, ast_channel_caller(chan)->ani.number.str, "")); |
| 370 |
|
| 371 |
ast_string_field_set(snapshot, rdnis, |
| 372 |
S_COR(ast_channel_redirecting(chan)->from.number.valid, ast_channel_redirecting(chan)->from.number.str, "")); |
| 373 |
|
| 374 |
ast_string_field_set(snapshot, dnid, |
| 375 |
S_OR(ast_channel_dialed(chan)->number.str, "")); |
| 376 |
ast_string_field_set(snapshot, dialed_subaddr, |
| 377 |
S_COR(ast_channel_dialed(chan)->subaddress.valid, ast_channel_dialed(chan)->subaddress.str, "")); |
| 378 |
|
| 379 |
snapshot->pres = ast_party_id_presentation(&ast_channel_caller(chan)->id); |
| 380 |
|
| 381 |
return snapshot; |
| 382 |
} |
| 383 |
|
| 384 |
static struct ast_channel_snapshot_connected *channel_snapshot_connected_create(struct ast_channel *chan) |
| 385 |
{ |
| 386 |
const char *name = S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, ""); |
| 387 |
const char *number = S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, ""); |
| 388 |
size_t name_len = strlen(name) + 1; |
| 389 |
size_t number_len = strlen(number) + 1; |
| 390 |
struct ast_channel_snapshot_connected *snapshot; |
| 391 |
|
| 392 |
snapshot = ao2_alloc_options(sizeof(*snapshot) + name_len + number_len, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 393 |
if (!snapshot) { |
| 394 |
return NULL; |
| 395 |
} |
| 396 |
|
| 397 |
strcpy(snapshot->name, name); |
| 398 |
snapshot->number = snapshot->name + name_len; |
| 399 |
ast_copy_string(snapshot->number, number, number_len); |
| 400 |
|
| 401 |
return snapshot; |
| 402 |
} |
| 403 |
|
| 404 |
static struct ast_channel_snapshot_bridge *channel_snapshot_bridge_create(struct ast_channel *chan) |
| 405 |
{ |
| 406 |
const char *uniqueid = ""; |
| 407 |
struct ast_bridge *bridge; |
| 408 |
struct ast_channel_snapshot_bridge *snapshot; |
| 409 |
|
| 410 |
bridge = ast_channel_get_bridge(chan); |
| 411 |
if (bridge && !ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) { |
| 412 |
uniqueid = bridge->uniqueid; |
| 413 |
} |
| 414 |
ao2_cleanup(bridge); |
| 415 |
|
| 416 |
snapshot = ao2_alloc_options(sizeof(*snapshot) + strlen(uniqueid) + 1, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 417 |
if (!snapshot) { |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
|
| 421 |
strcpy(snapshot->id, uniqueid); |
| 422 |
|
| 423 |
return snapshot; |
| 424 |
} |
| 425 |
|
| 426 |
static void channel_snapshot_dialplan_dtor(void *obj) |
| 427 |
{ |
| 428 |
struct ast_channel_snapshot_dialplan *snapshot = obj; |
| 429 |
|
| 430 |
ast_string_field_free_memory(snapshot); |
| 431 |
} |
| 432 |
|
| 433 |
static struct ast_channel_snapshot_dialplan *channel_snapshot_dialplan_create(struct ast_channel *chan) |
| 434 |
{ |
| 435 |
struct ast_channel_snapshot_dialplan *snapshot; |
| 436 |
|
| 437 |
snapshot = ao2_alloc_options(sizeof(*snapshot), channel_snapshot_dialplan_dtor, |
| 438 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 439 |
if (!snapshot) { |
| 440 |
return NULL; |
| 441 |
} |
| 442 |
|
| 443 |
if (ast_string_field_init(snapshot, 256)) { |
| 444 |
ao2_ref(snapshot, -1); |
| 445 |
return NULL; |
| 446 |
} |
| 447 |
|
| 448 |
if (ast_channel_appl(chan)) { |
| 449 |
ast_string_field_set(snapshot, appl, ast_channel_appl(chan)); |
| 450 |
} |
| 451 |
if (ast_channel_data(chan)) { |
| 452 |
ast_string_field_set(snapshot, data, ast_channel_data(chan)); |
| 453 |
} |
| 454 |
ast_string_field_set(snapshot, context, ast_channel_context(chan)); |
| 455 |
ast_string_field_set(snapshot, exten, ast_channel_exten(chan)); |
| 456 |
snapshot->priority = ast_channel_priority(chan); |
| 457 |
|
| 458 |
return snapshot; |
| 459 |
} |
| 460 |
|
| 461 |
static struct ast_channel_snapshot_hangup *channel_snapshot_hangup_create(struct ast_channel *chan) |
| 462 |
{ |
| 463 |
const char *hangupsource = S_OR(ast_channel_hangupsource(chan), ""); |
| 464 |
struct ast_channel_snapshot_hangup *snapshot; |
| 465 |
|
| 466 |
snapshot = ao2_alloc_options(sizeof(*snapshot) + strlen(hangupsource) + 1, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 467 |
if (!snapshot) { |
| 468 |
return NULL; |
| 469 |
} |
| 470 |
|
| 471 |
snapshot->cause = ast_channel_hangupcause(chan); |
| 472 |
snapshot->tech_cause = ast_channel_tech_hangupcause(chan); |
| 473 |
snapshot->source = snapshot->buffer; |
| 474 |
strcpy(snapshot->source, hangupsource); |
| 475 |
|
| 476 |
return snapshot; |
| 477 |
} |
| 478 |
|
| 479 |
struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan) |
| 480 |
{ |
| 481 |
struct ast_channel_snapshot *old_snapshot; |
| 482 |
struct ast_channel_snapshot *snapshot; |
| 483 |
|
| 484 |
|
| 485 |
if (!ast_channel_tech(chan)) { |
| 486 |
return NULL; |
| 487 |
} |
| 488 |
|
| 489 |
snapshot = ao2_alloc_options(sizeof(*snapshot), channel_snapshot_dtor, |
| 490 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 491 |
if (!snapshot) { |
| 492 |
return NULL; |
| 493 |
} |
| 494 |
|
| 495 |
old_snapshot = ast_channel_snapshot(chan); |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
if (ast_test_flag(ast_channel_snapshot_segment_flags(chan), AST_CHANNEL_SNAPSHOT_INVALIDATE_BASE)) { |
| 503 |
|
| 504 |
snapshot->base = channel_snapshot_base_create(chan); |
| 505 |
if (!snapshot->base) { |
| 506 |
ao2_ref(snapshot, -1); |
| 507 |
return NULL; |
| 508 |
} |
| 509 |
} else { |
| 510 |
snapshot->base = ao2_bump(old_snapshot->base); |
| 511 |
} |
| 512 |
|
| 513 |
if (ast_test_flag(ast_channel_snapshot_segment_flags(chan), AST_CHANNEL_SNAPSHOT_INVALIDATE_PEER)) { |
| 514 |
|
| 515 |
snapshot->peer = channel_snapshot_peer_create(chan); |
| 516 |
if (!snapshot->peer) { |
| 517 |
ao2_ref(snapshot, -1); |
| 518 |
return NULL; |
| 519 |
} |
| 520 |
} else { |
| 521 |
snapshot->peer = ao2_bump(old_snapshot->peer); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
snapshot->caller = channel_snapshot_caller_create(chan); |
| 530 |
if (!snapshot->caller) { |
| 531 |
ao2_ref(snapshot, -1); |
| 532 |
return NULL; |
| 533 |
} |
| 534 |
|
| 535 |
snapshot->connected = channel_snapshot_connected_create(chan); |
| 536 |
if (!snapshot->connected) { |
| 537 |
ao2_ref(snapshot, -1); |
| 538 |
return NULL; |
| 539 |
} |
| 540 |
|
| 541 |
if (ast_test_flag(ast_channel_snapshot_segment_flags(chan), AST_CHANNEL_SNAPSHOT_INVALIDATE_BRIDGE)) { |
| 542 |
|
| 543 |
snapshot->bridge = channel_snapshot_bridge_create(chan); |
| 544 |
if (!snapshot->bridge) { |
| 545 |
ao2_ref(snapshot, -1); |
| 546 |
return NULL; |
| 547 |
} |
| 548 |
} else { |
| 549 |
snapshot->bridge = ao2_bump(old_snapshot->bridge); |
| 550 |
} |
| 551 |
|
| 552 |
if (ast_test_flag(ast_channel_snapshot_segment_flags(chan), AST_CHANNEL_SNAPSHOT_INVALIDATE_DIALPLAN)) { |
| 553 |
|
| 554 |
snapshot->dialplan = channel_snapshot_dialplan_create(chan); |
| 555 |
if (!snapshot->dialplan) { |
| 556 |
ao2_ref(snapshot, -1); |
| 557 |
return NULL; |
| 558 |
} |
| 559 |
} else { |
| 560 |
snapshot->dialplan = ao2_bump(old_snapshot->dialplan); |
| 561 |
} |
| 562 |
|
| 563 |
if (ast_test_flag(ast_channel_snapshot_segment_flags(chan), AST_CHANNEL_SNAPSHOT_INVALIDATE_HANGUP)) { |
| 564 |
|
| 565 |
snapshot->hangup = channel_snapshot_hangup_create(chan); |
| 566 |
if (!snapshot->hangup) { |
| 567 |
ao2_ref(snapshot, -1); |
| 568 |
return NULL; |
| 569 |
} |
| 570 |
} else { |
| 571 |
snapshot->hangup = ao2_bump(old_snapshot->hangup); |
| 572 |
} |
| 573 |
|
| 574 |
snapshot->state = ast_channel_state(chan); |
| 575 |
snapshot->amaflags = ast_channel_amaflags(chan); |
| 576 |
ast_copy_flags(&snapshot->flags, ast_channel_flags(chan), 0xFFFFFFFF); |
| 577 |
ast_set_flag(&snapshot->softhangup_flags, ast_channel_softhangup_internal_flag(chan)); |
| 578 |
|
| 579 |
|
| 580 |
snapshot->manager_vars = ast_channel_get_manager_vars(chan); |
| 581 |
snapshot->ari_vars = ast_channel_get_ari_vars(chan); |
| 582 |
|
| 583 |
return snapshot; |
| 584 |
} |
| 585 |
|
| 586 |
static void channel_snapshot_update_dtor(void *obj) |
| 587 |
{ |
| 588 |
struct ast_channel_snapshot_update *update = obj; |
| 589 |
|
| 590 |
ao2_cleanup(update->old_snapshot); |
| 591 |
ao2_cleanup(update->new_snapshot); |
| 592 |
} |
| 593 |
|
| 594 |
static struct ast_channel_snapshot_update *channel_snapshot_update_create(struct ast_channel *chan) |
| 595 |
{ |
| 596 |
struct ast_channel_snapshot_update *update; |
| 597 |
|
| 598 |
update = ao2_alloc_options(sizeof(*update), channel_snapshot_update_dtor, |
| 599 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 600 |
if (!update) { |
| 601 |
return NULL; |
| 602 |
} |
| 603 |
|
| 604 |
update->old_snapshot = ao2_bump(ast_channel_snapshot(chan)); |
| 605 |
update->new_snapshot = ast_channel_snapshot_create(chan); |
| 606 |
if (!update->new_snapshot) { |
| 607 |
ao2_ref(update, -1); |
| 608 |
return NULL; |
| 609 |
} |
| 610 |
|
| 611 |
return update; |
| 612 |
} |
| 613 |
|
| 614 |
static void publish_message_for_channel_topics(struct stasis_message *message, struct ast_channel *chan) |
| 615 |
{ |
| 616 |
if (chan) { |
| 617 |
stasis_publish(ast_channel_topic(chan), message); |
| 618 |
} else { |
| 619 |
stasis_publish(ast_channel_topic_all(), message); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
static void channel_blob_dtor(void *obj) |
| 624 |
{ |
| 625 |
struct ast_channel_blob *event = obj; |
| 626 |
ao2_cleanup(event->snapshot); |
| 627 |
ast_json_unref(event->blob); |
| 628 |
} |
| 629 |
|
| 630 |
static void ast_channel_publish_dial_internal(struct ast_channel *caller, |
| 631 |
struct ast_channel *peer, struct ast_channel *forwarded, const char *dialstring, |
| 632 |
const char *dialstatus, const char *forward) |
| 633 |
{ |
| 634 |
struct ast_multi_channel_blob *payload; |
| 635 |
struct stasis_message *msg; |
| 636 |
struct ast_json *blob; |
| 637 |
struct ast_channel_snapshot *peer_snapshot; |
| 638 |
|
| 639 |
if (!ast_channel_dial_type()) { |
| 640 |
return; |
| 641 |
} |
| 642 |
|
| 643 |
ast_assert(peer != NULL); |
| 644 |
|
| 645 |
blob = ast_json_pack("{s: s, s: s, s: s}", |
| 646 |
"dialstatus", S_OR(dialstatus, ""), |
| 647 |
"forward", S_OR(forward, ""), |
| 648 |
"dialstring", S_OR(dialstring, "")); |
| 649 |
if (!blob) { |
| 650 |
return; |
| 651 |
} |
| 652 |
payload = ast_multi_channel_blob_create(blob); |
| 653 |
ast_json_unref(blob); |
| 654 |
if (!payload) { |
| 655 |
return; |
| 656 |
} |
| 657 |
|
| 658 |
if (caller) { |
| 659 |
struct ast_channel_snapshot *caller_snapshot; |
| 660 |
|
| 661 |
ast_channel_lock(caller); |
| 662 |
if (ast_strlen_zero(dialstatus)) { |
| 663 |
caller_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(caller)); |
| 664 |
} else { |
| 665 |
caller_snapshot = ast_channel_snapshot_create(caller); |
| 666 |
} |
| 667 |
ast_channel_unlock(caller); |
| 668 |
if (!caller_snapshot) { |
| 669 |
ao2_ref(payload, -1); |
| 670 |
return; |
| 671 |
} |
| 672 |
ast_multi_channel_blob_add_channel(payload, "caller", caller_snapshot); |
| 673 |
ao2_ref(caller_snapshot, -1); |
| 674 |
} |
| 675 |
|
| 676 |
ast_channel_lock(peer); |
| 677 |
if (ast_strlen_zero(dialstatus)) { |
| 678 |
peer_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(peer)); |
| 679 |
} else { |
| 680 |
peer_snapshot = ast_channel_snapshot_create(peer); |
| 681 |
} |
| 682 |
ast_channel_unlock(peer); |
| 683 |
if (!peer_snapshot) { |
| 684 |
ao2_ref(payload, -1); |
| 685 |
return; |
| 686 |
} |
| 687 |
ast_multi_channel_blob_add_channel(payload, "peer", peer_snapshot); |
| 688 |
ao2_ref(peer_snapshot, -1); |
| 689 |
|
| 690 |
if (forwarded) { |
| 691 |
struct ast_channel_snapshot *forwarded_snapshot; |
| 692 |
|
| 693 |
ast_channel_lock(forwarded); |
| 694 |
forwarded_snapshot = ast_channel_snapshot_create(forwarded); |
| 695 |
ast_channel_unlock(forwarded); |
| 696 |
if (!forwarded_snapshot) { |
| 697 |
ao2_ref(payload, -1); |
| 698 |
return; |
| 699 |
} |
| 700 |
ast_multi_channel_blob_add_channel(payload, "forwarded", forwarded_snapshot); |
| 701 |
ao2_ref(forwarded_snapshot, -1); |
| 702 |
} |
| 703 |
|
| 704 |
msg = stasis_message_create(ast_channel_dial_type(), payload); |
| 705 |
ao2_ref(payload, -1); |
| 706 |
if (msg) { |
| 707 |
publish_message_for_channel_topics(msg, caller ?: peer); |
| 708 |
ao2_ref(msg, -1); |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
static void remove_dial_masquerade(struct ast_channel *peer); |
| 713 |
static void remove_dial_masquerade_caller(struct ast_channel *caller); |
| 714 |
static int set_dial_masquerade(struct ast_channel *caller, |
| 715 |
struct ast_channel *peer, const char *dialstring); |
| 716 |
|
| 717 |
void ast_channel_publish_dial_forward(struct ast_channel *caller, struct ast_channel *peer, |
| 718 |
struct ast_channel *forwarded, const char *dialstring, const char *dialstatus, |
| 719 |
const char *forward) |
| 720 |
{ |
| 721 |
ast_assert(peer != NULL); |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
if (caller) { |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
for (;; ast_channel_unlock(caller), sched_yield()) { |
| 736 |
ast_channel_lock(caller); |
| 737 |
if (ast_channel_trylock(peer)) { |
| 738 |
continue; |
| 739 |
} |
| 740 |
if (forwarded && ast_channel_trylock(forwarded)) { |
| 741 |
ast_channel_unlock(peer); |
| 742 |
continue; |
| 743 |
} |
| 744 |
break; |
| 745 |
} |
| 746 |
|
| 747 |
if (ast_strlen_zero(dialstatus)) { |
| 748 |
set_dial_masquerade(caller, peer, dialstring); |
| 749 |
} else { |
| 750 |
remove_dial_masquerade(peer); |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
ast_channel_publish_dial_internal(caller, peer, forwarded, dialstring, dialstatus, |
| 755 |
forward); |
| 756 |
|
| 757 |
if (caller) { |
| 758 |
if (forwarded) { |
| 759 |
ast_channel_unlock(forwarded); |
| 760 |
} |
| 761 |
ast_channel_unlock(peer); |
| 762 |
remove_dial_masquerade_caller(caller); |
| 763 |
ast_channel_unlock(caller); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
void ast_channel_publish_dial(struct ast_channel *caller, struct ast_channel *peer, |
| 768 |
const char *dialstring, const char *dialstatus) |
| 769 |
{ |
| 770 |
ast_channel_publish_dial_forward(caller, peer, NULL, dialstring, dialstatus, NULL); |
| 771 |
} |
| 772 |
|
| 773 |
static struct stasis_message *create_channel_blob_message(struct ast_channel_snapshot *snapshot, |
| 774 |
struct stasis_message_type *type, |
| 775 |
struct ast_json *blob) |
| 776 |
{ |
| 777 |
struct stasis_message *msg; |
| 778 |
struct ast_channel_blob *obj; |
| 779 |
|
| 780 |
obj = ao2_alloc(sizeof(*obj), channel_blob_dtor); |
| 781 |
if (!obj) { |
| 782 |
return NULL; |
| 783 |
} |
| 784 |
|
| 785 |
if (snapshot) { |
| 786 |
obj->snapshot = snapshot; |
| 787 |
ao2_ref(obj->snapshot, +1); |
| 788 |
} |
| 789 |
if (!blob) { |
| 790 |
blob = ast_json_null(); |
| 791 |
} |
| 792 |
obj->blob = ast_json_ref(blob); |
| 793 |
|
| 794 |
msg = stasis_message_create(type, obj); |
| 795 |
ao2_cleanup(obj); |
| 796 |
return msg; |
| 797 |
} |
| 798 |
|
| 799 |
struct stasis_message *ast_channel_blob_create_from_cache(const char *channel_id, |
| 800 |
struct stasis_message_type *type, |
| 801 |
struct ast_json *blob) |
| 802 |
{ |
| 803 |
struct ast_channel_snapshot *snapshot; |
| 804 |
struct stasis_message *msg; |
| 805 |
|
| 806 |
if (!type) { |
| 807 |
return NULL; |
| 808 |
} |
| 809 |
|
| 810 |
snapshot = ast_channel_snapshot_get_latest(channel_id); |
| 811 |
msg = create_channel_blob_message(snapshot, type, blob); |
| 812 |
ao2_cleanup(snapshot); |
| 813 |
return msg; |
| 814 |
} |
| 815 |
|
| 816 |
struct stasis_message *ast_channel_blob_create(struct ast_channel *chan, |
| 817 |
struct stasis_message_type *type, struct ast_json *blob) |
| 818 |
{ |
| 819 |
struct ast_channel_snapshot *snapshot; |
| 820 |
struct stasis_message *msg; |
| 821 |
|
| 822 |
if (!type) { |
| 823 |
return NULL; |
| 824 |
} |
| 825 |
|
| 826 |
snapshot = chan ? ao2_bump(ast_channel_snapshot(chan)) : NULL; |
| 827 |
msg = create_channel_blob_message(snapshot, type, blob); |
| 828 |
ao2_cleanup(snapshot); |
| 829 |
return msg; |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
struct channel_role_snapshot { |
| 834 |
struct ast_channel_snapshot *snapshot; |
| 835 |
char role[0]; |
| 836 |
}; |
| 837 |
|
| 838 |
|
| 839 |
struct ast_multi_channel_blob { |
| 840 |
struct ao2_container *channel_snapshots; |
| 841 |
struct ast_json *blob; |
| 842 |
}; |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
static int channel_role_cmp_cb(void *obj, void *arg, int flags) |
| 849 |
{ |
| 850 |
const struct channel_role_snapshot *object_left = obj; |
| 851 |
const struct channel_role_snapshot *object_right = arg; |
| 852 |
const char *right_key = arg; |
| 853 |
int cmp; |
| 854 |
|
| 855 |
switch (flags & OBJ_SEARCH_MASK) { |
| 856 |
case OBJ_SEARCH_OBJECT: |
| 857 |
right_key = object_right->role; |
| 858 |
case OBJ_SEARCH_KEY: |
| 859 |
cmp = strcasecmp(object_left->role, right_key); |
| 860 |
break; |
| 861 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 862 |
cmp = strncasecmp(object_left->role, right_key, strlen(right_key)); |
| 863 |
break; |
| 864 |
default: |
| 865 |
cmp = 0; |
| 866 |
break; |
| 867 |
} |
| 868 |
if (cmp) { |
| 869 |
return 0; |
| 870 |
} |
| 871 |
return CMP_MATCH; |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
static int channel_role_hash_cb(const void *obj, const int flags) |
| 879 |
{ |
| 880 |
const struct channel_role_snapshot *object = obj; |
| 881 |
const char *key; |
| 882 |
|
| 883 |
switch (flags & OBJ_SEARCH_MASK) { |
| 884 |
case OBJ_SEARCH_KEY: |
| 885 |
key = obj; |
| 886 |
break; |
| 887 |
case OBJ_SEARCH_OBJECT: |
| 888 |
key = object->role; |
| 889 |
break; |
| 890 |
default: |
| 891 |
ast_assert(0); |
| 892 |
return 0; |
| 893 |
} |
| 894 |
return ast_str_case_hash(key); |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
static void multi_channel_blob_dtor(void *obj) |
| 902 |
{ |
| 903 |
struct ast_multi_channel_blob *multi_blob = obj; |
| 904 |
|
| 905 |
ao2_cleanup(multi_blob->channel_snapshots); |
| 906 |
ast_json_unref(multi_blob->blob); |
| 907 |
} |
| 908 |
|
| 909 |
struct ast_multi_channel_blob *ast_multi_channel_blob_create(struct ast_json *blob) |
| 910 |
{ |
| 911 |
struct ast_multi_channel_blob *obj; |
| 912 |
|
| 913 |
ast_assert(blob != NULL); |
| 914 |
|
| 915 |
obj = ao2_alloc(sizeof(*obj), multi_channel_blob_dtor); |
| 916 |
if (!obj) { |
| 917 |
return NULL; |
| 918 |
} |
| 919 |
|
| 920 |
obj->channel_snapshots = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 921 |
NUM_MULTI_CHANNEL_BLOB_BUCKETS, channel_role_hash_cb, NULL, channel_role_cmp_cb); |
| 922 |
if (!obj->channel_snapshots) { |
| 923 |
ao2_ref(obj, -1); |
| 924 |
return NULL; |
| 925 |
} |
| 926 |
|
| 927 |
obj->blob = ast_json_ref(blob); |
| 928 |
return obj; |
| 929 |
} |
| 930 |
|
| 931 |
struct ast_channel_snapshot *ast_channel_snapshot_get_latest(const char *uniqueid) |
| 932 |
{ |
| 933 |
ast_assert(!ast_strlen_zero(uniqueid)); |
| 934 |
|
| 935 |
return ao2_find(channel_cache, uniqueid, OBJ_SEARCH_KEY); |
| 936 |
} |
| 937 |
|
| 938 |
struct ast_channel_snapshot *ast_channel_snapshot_get_latest_by_name(const char *name) |
| 939 |
{ |
| 940 |
ast_assert(!ast_strlen_zero(name)); |
| 941 |
|
| 942 |
return ao2_find(channel_cache_by_name, name, OBJ_SEARCH_KEY); |
| 943 |
} |
| 944 |
|
| 945 |
void ast_channel_publish_final_snapshot(struct ast_channel *chan) |
| 946 |
{ |
| 947 |
struct ast_channel_snapshot_update *update; |
| 948 |
struct stasis_message *message; |
| 949 |
|
| 950 |
if (!ast_channel_snapshot_type()) { |
| 951 |
return; |
| 952 |
} |
| 953 |
|
| 954 |
update = channel_snapshot_update_create(chan); |
| 955 |
if (!update) { |
| 956 |
return; |
| 957 |
} |
| 958 |
|
| 959 |
message = stasis_message_create(ast_channel_snapshot_type(), update); |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
ao2_ref(update, -1); |
| 964 |
if (!message) { |
| 965 |
return; |
| 966 |
} |
| 967 |
|
| 968 |
ao2_find(channel_cache, ast_channel_uniqueid(chan), OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA); |
| 969 |
ao2_find(channel_cache_by_name, ast_channel_name(chan), OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA); |
| 970 |
|
| 971 |
ast_channel_snapshot_set(chan, NULL); |
| 972 |
|
| 973 |
stasis_publish(ast_channel_topic(chan), message); |
| 974 |
ao2_ref(message, -1); |
| 975 |
} |
| 976 |
|
| 977 |
static void channel_role_snapshot_dtor(void *obj) |
| 978 |
{ |
| 979 |
struct channel_role_snapshot *role_snapshot = obj; |
| 980 |
|
| 981 |
ao2_cleanup(role_snapshot->snapshot); |
| 982 |
} |
| 983 |
|
| 984 |
void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj, const char *role, struct ast_channel_snapshot *snapshot) |
| 985 |
{ |
| 986 |
struct channel_role_snapshot *role_snapshot; |
| 987 |
int role_len = strlen(role) + 1; |
| 988 |
|
| 989 |
if (!obj || ast_strlen_zero(role) || !snapshot) { |
| 990 |
return; |
| 991 |
} |
| 992 |
|
| 993 |
role_snapshot = ao2_alloc_options(sizeof(*role_snapshot) + role_len, |
| 994 |
channel_role_snapshot_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 995 |
if (!role_snapshot) { |
| 996 |
return; |
| 997 |
} |
| 998 |
ast_copy_string(role_snapshot->role, role, role_len); |
| 999 |
role_snapshot->snapshot = snapshot; |
| 1000 |
ao2_ref(role_snapshot->snapshot, +1); |
| 1001 |
ao2_link(obj->channel_snapshots, role_snapshot); |
| 1002 |
ao2_ref(role_snapshot, -1); |
| 1003 |
} |
| 1004 |
|
| 1005 |
struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(struct ast_multi_channel_blob *obj, const char *role) |
| 1006 |
{ |
| 1007 |
struct channel_role_snapshot *role_snapshot; |
| 1008 |
struct ast_channel_snapshot *snapshot; |
| 1009 |
|
| 1010 |
if (!obj || ast_strlen_zero(role)) { |
| 1011 |
return NULL; |
| 1012 |
} |
| 1013 |
role_snapshot = ao2_find(obj->channel_snapshots, role, OBJ_SEARCH_KEY); |
| 1014 |
|
| 1015 |
if (!role_snapshot) { |
| 1016 |
return NULL; |
| 1017 |
} |
| 1018 |
snapshot = role_snapshot->snapshot; |
| 1019 |
ao2_ref(role_snapshot, -1); |
| 1020 |
return snapshot; |
| 1021 |
} |
| 1022 |
|
| 1023 |
struct ao2_container *ast_multi_channel_blob_get_channels(struct ast_multi_channel_blob *obj, const char *role) |
| 1024 |
{ |
| 1025 |
struct ao2_container *ret_container; |
| 1026 |
struct ao2_iterator *it_role_snapshots; |
| 1027 |
struct channel_role_snapshot *role_snapshot; |
| 1028 |
char *arg; |
| 1029 |
|
| 1030 |
if (!obj || ast_strlen_zero(role)) { |
| 1031 |
return NULL; |
| 1032 |
} |
| 1033 |
|
| 1034 |
ret_container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 1035 |
NUM_MULTI_CHANNEL_BLOB_BUCKETS, |
| 1036 |
channel_snapshot_hash_cb, NULL, channel_snapshot_cmp_cb); |
| 1037 |
if (!ret_container) { |
| 1038 |
return NULL; |
| 1039 |
} |
| 1040 |
|
| 1041 |
arg = ast_strdupa(role); |
| 1042 |
it_role_snapshots = ao2_callback(obj->channel_snapshots, |
| 1043 |
OBJ_MULTIPLE | OBJ_SEARCH_KEY, channel_role_cmp_cb, arg); |
| 1044 |
if (!it_role_snapshots) { |
| 1045 |
ao2_ref(ret_container, -1); |
| 1046 |
return NULL; |
| 1047 |
} |
| 1048 |
|
| 1049 |
while ((role_snapshot = ao2_iterator_next(it_role_snapshots))) { |
| 1050 |
ao2_link(ret_container, role_snapshot->snapshot); |
| 1051 |
ao2_ref(role_snapshot, -1); |
| 1052 |
} |
| 1053 |
ao2_iterator_destroy(it_role_snapshots); |
| 1054 |
|
| 1055 |
return ret_container; |
| 1056 |
} |
| 1057 |
|
| 1058 |
struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj) |
| 1059 |
{ |
| 1060 |
if (!obj) { |
| 1061 |
return NULL; |
| 1062 |
} |
| 1063 |
return obj->blob; |
| 1064 |
} |
| 1065 |
|
| 1066 |
void ast_channel_stage_snapshot(struct ast_channel *chan) |
| 1067 |
{ |
| 1068 |
ast_set_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE); |
| 1069 |
} |
| 1070 |
|
| 1071 |
void ast_channel_stage_snapshot_done(struct ast_channel *chan) |
| 1072 |
{ |
| 1073 |
ast_clear_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE); |
| 1074 |
ast_channel_publish_snapshot(chan); |
| 1075 |
} |
| 1076 |
|
| 1077 |
void ast_channel_snapshot_invalidate_segment(struct ast_channel *chan, |
| 1078 |
enum ast_channel_snapshot_segment_invalidation segment) |
| 1079 |
{ |
| 1080 |
ast_set_flag(ast_channel_snapshot_segment_flags(chan), segment); |
| 1081 |
} |
| 1082 |
|
| 1083 |
void ast_channel_publish_snapshot(struct ast_channel *chan) |
| 1084 |
{ |
| 1085 |
struct ast_channel_snapshot_update *update; |
| 1086 |
struct stasis_message *message; |
| 1087 |
|
| 1088 |
if (!ast_channel_snapshot_type()) { |
| 1089 |
return; |
| 1090 |
} |
| 1091 |
|
| 1092 |
if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE)) { |
| 1093 |
return; |
| 1094 |
} |
| 1095 |
|
| 1096 |
update = channel_snapshot_update_create(chan); |
| 1097 |
if (!update) { |
| 1098 |
return; |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
if (update->old_snapshot && !memcmp(update->old_snapshot, update->new_snapshot, sizeof(struct ast_channel_snapshot))) { |
| 1105 |
ao2_ref(update, -1); |
| 1106 |
return; |
| 1107 |
} |
| 1108 |
|
| 1109 |
message = stasis_message_create(ast_channel_snapshot_type(), update); |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
ao2_ref(update, -1); |
| 1114 |
if (!message) { |
| 1115 |
return; |
| 1116 |
} |
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
ao2_wrlock(channel_cache); |
| 1122 |
ao2_find(channel_cache, ast_channel_uniqueid(chan), OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA | OBJ_NOLOCK); |
| 1123 |
|
| 1124 |
ao2_link_flags(channel_cache, update->new_snapshot, OBJ_NOLOCK); |
| 1125 |
ao2_unlock(channel_cache); |
| 1126 |
|
| 1127 |
|
| 1128 |
ao2_wrlock(channel_cache_by_name); |
| 1129 |
ao2_find(channel_cache_by_name, ast_channel_name(chan), OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA | OBJ_NOLOCK); |
| 1130 |
|
| 1131 |
ao2_link_flags(channel_cache_by_name, update->new_snapshot, OBJ_NOLOCK); |
| 1132 |
ao2_unlock(channel_cache_by_name); |
| 1133 |
|
| 1134 |
ast_channel_snapshot_set(chan, update->new_snapshot); |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
ast_clear_flag(ast_channel_snapshot_segment_flags(chan), AST_FLAGS_ALL); |
| 1140 |
|
| 1141 |
ast_assert(ast_channel_topic(chan) != NULL); |
| 1142 |
stasis_publish(ast_channel_topic(chan), message); |
| 1143 |
ao2_ref(message, -1); |
| 1144 |
} |
| 1145 |
|
| 1146 |
void ast_channel_publish_cached_blob(struct ast_channel *chan, struct stasis_message_type *type, struct ast_json *blob) |
| 1147 |
{ |
| 1148 |
struct stasis_message *message; |
| 1149 |
|
| 1150 |
if (!blob) { |
| 1151 |
blob = ast_json_null(); |
| 1152 |
} |
| 1153 |
|
| 1154 |
message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan), type, blob); |
| 1155 |
if (message) { |
| 1156 |
stasis_publish(ast_channel_topic(chan), message); |
| 1157 |
ao2_ref(message, -1); |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
void ast_channel_publish_blob(struct ast_channel *chan, struct stasis_message_type *type, struct ast_json *blob) |
| 1162 |
{ |
| 1163 |
struct stasis_message *message; |
| 1164 |
|
| 1165 |
if (!blob) { |
| 1166 |
blob = ast_json_null(); |
| 1167 |
} |
| 1168 |
|
| 1169 |
message = ast_channel_blob_create(chan, type, blob); |
| 1170 |
if (message) { |
| 1171 |
stasis_publish(ast_channel_topic(chan), message); |
| 1172 |
ao2_ref(message, -1); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value) |
| 1177 |
{ |
| 1178 |
struct ast_json *blob; |
| 1179 |
enum ast_utf8_replace_result result; |
| 1180 |
char *new_value = NULL; |
| 1181 |
size_t new_value_size = 0; |
| 1182 |
|
| 1183 |
ast_assert(name != NULL); |
| 1184 |
ast_assert(value != NULL); |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
result = ast_utf8_replace_invalid_chars(new_value, &new_value_size, |
| 1191 |
value, strlen(value)); |
| 1192 |
|
| 1193 |
if (result == AST_UTF8_REPLACE_VALID) { |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
new_value = (char *)value; |
| 1199 |
} else { |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
new_value = ast_alloca(new_value_size); |
| 1205 |
|
| 1206 |
result = ast_utf8_replace_invalid_chars(new_value, &new_value_size, |
| 1207 |
value, strlen(value)); |
| 1208 |
|
| 1209 |
ast_log(LOG_WARNING, "%s: The contents of variable '%s' had invalid UTF-8 sequences which were replaced", |
| 1210 |
chan ? ast_channel_name(chan) : "GLOBAL", name); |
| 1211 |
} |
| 1212 |
|
| 1213 |
blob = ast_json_pack("{s: s, s: s}", |
| 1214 |
"variable", name, |
| 1215 |
"value", new_value); |
| 1216 |
if (!blob) { |
| 1217 |
ast_log(LOG_ERROR, "Error creating message\n"); |
| 1218 |
return; |
| 1219 |
} |
| 1220 |
|
| 1221 |
|
| 1222 |
if (chan) { |
| 1223 |
struct varshead *ari_vars = ast_channel_get_ari_vars(chan); |
| 1224 |
if (ast_channel_has_manager_vars() || (ari_vars && !AST_LIST_EMPTY(ari_vars))) { |
| 1225 |
ast_channel_publish_snapshot(chan); |
| 1226 |
} |
| 1227 |
ao2_cleanup(ari_vars); |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
ast_channel_publish_blob(chan, ast_channel_varset_type(), blob); |
| 1232 |
ast_json_unref(blob); |
| 1233 |
} |
| 1234 |
|
| 1235 |
static struct ast_manager_event_blob *varset_to_ami(struct stasis_message *msg) |
| 1236 |
{ |
| 1237 |
struct ast_str *channel_event_string; |
| 1238 |
struct ast_channel_blob *obj = stasis_message_data(msg); |
| 1239 |
const char *variable = |
| 1240 |
ast_json_string_get(ast_json_object_get(obj->blob, "variable")); |
| 1241 |
char *value; |
| 1242 |
struct ast_manager_event_blob *ev; |
| 1243 |
|
| 1244 |
value = ast_escape_c_alloc(ast_json_string_get(ast_json_object_get(obj->blob, |
| 1245 |
"value"))); |
| 1246 |
if (!value) { |
| 1247 |
return NULL; |
| 1248 |
} |
| 1249 |
|
| 1250 |
if (obj->snapshot) { |
| 1251 |
channel_event_string = ast_manager_build_channel_state_string(obj->snapshot); |
| 1252 |
} else { |
| 1253 |
channel_event_string = ast_str_create(35); |
| 1254 |
ast_str_set(&channel_event_string, 0, |
| 1255 |
"Channel: none\r\n" |
| 1256 |
"Uniqueid: none\r\n"); |
| 1257 |
} |
| 1258 |
if (!channel_event_string) { |
| 1259 |
ast_free(value); |
| 1260 |
return NULL; |
| 1261 |
} |
| 1262 |
|
| 1263 |
ev = ast_manager_event_blob_create(EVENT_FLAG_DIALPLAN, "VarSet", |
| 1264 |
"%s" |
| 1265 |
"Variable: %s\r\n" |
| 1266 |
"Value: %s\r\n", |
| 1267 |
ast_str_buffer(channel_event_string), variable, value); |
| 1268 |
ast_free(channel_event_string); |
| 1269 |
ast_free(value); |
| 1270 |
return ev; |
| 1271 |
} |
| 1272 |
|
| 1273 |
static struct ast_manager_event_blob *agent_login_to_ami(struct stasis_message *msg) |
| 1274 |
{ |
| 1275 |
struct ast_str *channel_string; |
| 1276 |
struct ast_channel_blob *obj = stasis_message_data(msg); |
| 1277 |
const char *agent = ast_json_string_get(ast_json_object_get(obj->blob, "agent")); |
| 1278 |
struct ast_manager_event_blob *ev; |
| 1279 |
|
| 1280 |
channel_string = ast_manager_build_channel_state_string(obj->snapshot); |
| 1281 |
if (!channel_string) { |
| 1282 |
return NULL; |
| 1283 |
} |
| 1284 |
|
| 1285 |
ev = ast_manager_event_blob_create(EVENT_FLAG_AGENT, "AgentLogin", |
| 1286 |
"%s" |
| 1287 |
"Agent: %s\r\n", |
| 1288 |
ast_str_buffer(channel_string), agent); |
| 1289 |
ast_free(channel_string); |
| 1290 |
return ev; |
| 1291 |
} |
| 1292 |
|
| 1293 |
static struct ast_manager_event_blob *agent_logoff_to_ami(struct stasis_message *msg) |
| 1294 |
{ |
| 1295 |
struct ast_str *channel_string; |
| 1296 |
struct ast_channel_blob *obj = stasis_message_data(msg); |
| 1297 |
const char *agent = ast_json_string_get(ast_json_object_get(obj->blob, "agent")); |
| 1298 |
long logintime = ast_json_integer_get(ast_json_object_get(obj->blob, "logintime")); |
| 1299 |
struct ast_manager_event_blob *ev; |
| 1300 |
|
| 1301 |
channel_string = ast_manager_build_channel_state_string(obj->snapshot); |
| 1302 |
if (!channel_string) { |
| 1303 |
return NULL; |
| 1304 |
} |
| 1305 |
|
| 1306 |
ev = ast_manager_event_blob_create(EVENT_FLAG_AGENT, "AgentLogoff", |
| 1307 |
"%s" |
| 1308 |
"Agent: %s\r\n" |
| 1309 |
"Logintime: %ld\r\n", |
| 1310 |
ast_str_buffer(channel_string), agent, logintime); |
| 1311 |
ast_free(channel_string); |
| 1312 |
return ev; |
| 1313 |
} |
| 1314 |
|
| 1315 |
struct ast_json *ast_channel_snapshot_to_json( |
| 1316 |
const struct ast_channel_snapshot *snapshot, |
| 1317 |
const struct stasis_message_sanitizer *sanitize) |
| 1318 |
{ |
| 1319 |
struct ast_json *json_chan; |
| 1320 |
|
| 1321 |
if (snapshot == NULL |
| 1322 |
|| (sanitize |
| 1323 |
&& sanitize->channel_snapshot |
| 1324 |
&& sanitize->channel_snapshot(snapshot))) { |
| 1325 |
return NULL; |
| 1326 |
} |
| 1327 |
|
| 1328 |
json_chan = ast_json_pack( |
| 1329 |
|
| 1330 |
"{ s: s, s: s, s: s, s: s," |
| 1331 |
" s: o, s: o, s: s," |
| 1332 |
" s: o, s: o, s: s }", |
| 1333 |
|
| 1334 |
"id", snapshot->base->uniqueid, |
| 1335 |
"name", snapshot->base->name, |
| 1336 |
"state", ast_state2str(snapshot->state), |
| 1337 |
"protocol_id", snapshot->base->protocol_id, |
| 1338 |
|
| 1339 |
"caller", ast_json_name_number( |
| 1340 |
snapshot->caller->name, snapshot->caller->number), |
| 1341 |
"connected", ast_json_name_number( |
| 1342 |
snapshot->connected->name, snapshot->connected->number), |
| 1343 |
"accountcode", snapshot->base->accountcode, |
| 1344 |
|
| 1345 |
"dialplan", ast_json_dialplan_cep_app( |
| 1346 |
snapshot->dialplan->context, snapshot->dialplan->exten, snapshot->dialplan->priority, |
| 1347 |
snapshot->dialplan->appl, snapshot->dialplan->data), |
| 1348 |
"creationtime", ast_json_timeval(snapshot->base->creationtime, NULL), |
| 1349 |
"language", snapshot->base->language); |
| 1350 |
|
| 1351 |
if (!ast_strlen_zero(snapshot->caller->rdnis)) { |
| 1352 |
ast_json_object_set(json_chan, "caller_rdnis", ast_json_string_create(snapshot->caller->rdnis)); |
| 1353 |
} |
| 1354 |
|
| 1355 |
if (snapshot->ari_vars && !AST_LIST_EMPTY(snapshot->ari_vars)) { |
| 1356 |
ast_json_object_set(json_chan, "channelvars", ast_json_channel_vars(snapshot->ari_vars)); |
| 1357 |
} |
| 1358 |
|
| 1359 |
if (!ast_strlen_zero(snapshot->base->tenantid)) { |
| 1360 |
ast_json_object_set(json_chan, "tenantid", ast_json_string_create(snapshot->base->tenantid)); |
| 1361 |
} |
| 1362 |
|
| 1363 |
return json_chan; |
| 1364 |
} |
| 1365 |
|
| 1366 |
int ast_channel_snapshot_cep_equal( |
| 1367 |
const struct ast_channel_snapshot *old_snapshot, |
| 1368 |
const struct ast_channel_snapshot *new_snapshot) |
| 1369 |
{ |
| 1370 |
ast_assert(old_snapshot != NULL); |
| 1371 |
ast_assert(new_snapshot != NULL); |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
if (ast_strlen_zero(old_snapshot->dialplan->appl) && |
| 1378 |
!ast_strlen_zero(new_snapshot->dialplan->appl)) { |
| 1379 |
return 0; |
| 1380 |
} |
| 1381 |
|
| 1382 |
return old_snapshot->dialplan->priority == new_snapshot->dialplan->priority && |
| 1383 |
strcmp(old_snapshot->dialplan->context, new_snapshot->dialplan->context) == 0 && |
| 1384 |
strcmp(old_snapshot->dialplan->exten, new_snapshot->dialplan->exten) == 0; |
| 1385 |
} |
| 1386 |
|
| 1387 |
int ast_channel_snapshot_caller_id_equal( |
| 1388 |
const struct ast_channel_snapshot *old_snapshot, |
| 1389 |
const struct ast_channel_snapshot *new_snapshot) |
| 1390 |
{ |
| 1391 |
ast_assert(old_snapshot != NULL); |
| 1392 |
ast_assert(new_snapshot != NULL); |
| 1393 |
return strcmp(old_snapshot->caller->number, new_snapshot->caller->number) == 0 && |
| 1394 |
strcmp(old_snapshot->caller->name, new_snapshot->caller->name) == 0; |
| 1395 |
} |
| 1396 |
|
| 1397 |
int ast_channel_snapshot_connected_line_equal( |
| 1398 |
const struct ast_channel_snapshot *old_snapshot, |
| 1399 |
const struct ast_channel_snapshot *new_snapshot) |
| 1400 |
{ |
| 1401 |
ast_assert(old_snapshot != NULL); |
| 1402 |
ast_assert(new_snapshot != NULL); |
| 1403 |
return strcmp(old_snapshot->connected->number, new_snapshot->connected->number) == 0 && |
| 1404 |
strcmp(old_snapshot->connected->name, new_snapshot->connected->name) == 0; |
| 1405 |
} |
| 1406 |
|
| 1407 |
static struct ast_json *channel_blob_to_json( |
| 1408 |
struct stasis_message *message, |
| 1409 |
const char *type, |
| 1410 |
const struct stasis_message_sanitizer *sanitize) |
| 1411 |
{ |
| 1412 |
struct ast_json *to_json; |
| 1413 |
struct ast_channel_blob *channel_blob = stasis_message_data(message); |
| 1414 |
struct ast_json *blob = channel_blob->blob; |
| 1415 |
struct ast_channel_snapshot *snapshot = channel_blob->snapshot; |
| 1416 |
const struct timeval *tv = stasis_message_timestamp(message); |
| 1417 |
int res = 0; |
| 1418 |
|
| 1419 |
if (blob == NULL || ast_json_is_null(blob)) { |
| 1420 |
to_json = ast_json_object_create(); |
| 1421 |
} else { |
| 1422 |
|
| 1423 |
to_json = ast_json_copy(blob); |
| 1424 |
} |
| 1425 |
if (!to_json) { |
| 1426 |
return NULL; |
| 1427 |
} |
| 1428 |
|
| 1429 |
res |= ast_json_object_set(to_json, "type", ast_json_string_create(type)); |
| 1430 |
res |= ast_json_object_set(to_json, "timestamp", |
| 1431 |
ast_json_timeval(*tv, NULL)); |
| 1432 |
|
| 1433 |
|
| 1434 |
if (snapshot) { |
| 1435 |
struct ast_json *json_channel; |
| 1436 |
|
| 1437 |
json_channel = ast_channel_snapshot_to_json(snapshot, sanitize); |
| 1438 |
if (!json_channel) { |
| 1439 |
ast_json_unref(to_json); |
| 1440 |
return NULL; |
| 1441 |
} |
| 1442 |
|
| 1443 |
res |= ast_json_object_set(to_json, "channel", json_channel); |
| 1444 |
} |
| 1445 |
|
| 1446 |
if (res != 0) { |
| 1447 |
ast_json_unref(to_json); |
| 1448 |
return NULL; |
| 1449 |
} |
| 1450 |
|
| 1451 |
return to_json; |
| 1452 |
} |
| 1453 |
|
| 1454 |
static struct ast_json *dtmf_end_to_json( |
| 1455 |
struct stasis_message *message, |
| 1456 |
const struct stasis_message_sanitizer *sanitize) |
| 1457 |
{ |
| 1458 |
struct ast_channel_blob *channel_blob = stasis_message_data(message); |
| 1459 |
struct ast_json *blob = channel_blob->blob; |
| 1460 |
struct ast_channel_snapshot *snapshot = channel_blob->snapshot; |
| 1461 |
const char *direction = |
| 1462 |
ast_json_string_get(ast_json_object_get(blob, "direction")); |
| 1463 |
const char *digit = |
| 1464 |
ast_json_string_get(ast_json_object_get(blob, "digit")); |
| 1465 |
long duration_ms = |
| 1466 |
ast_json_integer_get(ast_json_object_get(blob, "duration_ms")); |
| 1467 |
const struct timeval *tv = stasis_message_timestamp(message); |
| 1468 |
struct ast_json *json_channel; |
| 1469 |
|
| 1470 |
|
| 1471 |
if (strcasecmp("Received", direction) != 0) { |
| 1472 |
return NULL; |
| 1473 |
} |
| 1474 |
|
| 1475 |
json_channel = ast_channel_snapshot_to_json(snapshot, sanitize); |
| 1476 |
if (!json_channel) { |
| 1477 |
return NULL; |
| 1478 |
} |
| 1479 |
|
| 1480 |
return ast_json_pack("{s: s, s: o, s: s, s: I, s: o}", |
| 1481 |
"type", "ChannelDtmfReceived", |
| 1482 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 1483 |
"digit", digit, |
| 1484 |
"duration_ms", (ast_json_int_t)duration_ms, |
| 1485 |
"channel", json_channel); |
| 1486 |
} |
| 1487 |
|
| 1488 |
static struct ast_json *varset_to_json( |
| 1489 |
struct stasis_message *message, |
| 1490 |
const struct stasis_message_sanitizer *sanitize) |
| 1491 |
{ |
| 1492 |
return channel_blob_to_json(message, "ChannelVarset", sanitize); |
| 1493 |
} |
| 1494 |
|
| 1495 |
static struct ast_json *hangup_request_to_json( |
| 1496 |
struct stasis_message *message, |
| 1497 |
const struct stasis_message_sanitizer *sanitize) |
| 1498 |
{ |
| 1499 |
return channel_blob_to_json(message, "ChannelHangupRequest", sanitize); |
| 1500 |
} |
| 1501 |
|
| 1502 |
static struct ast_json *dial_to_json( |
| 1503 |
struct stasis_message *message, |
| 1504 |
const struct stasis_message_sanitizer *sanitize) |
| 1505 |
{ |
| 1506 |
struct ast_multi_channel_blob *payload = stasis_message_data(message); |
| 1507 |
struct ast_json *blob = ast_multi_channel_blob_get_json(payload); |
| 1508 |
const char *dialstatus = |
| 1509 |
ast_json_string_get(ast_json_object_get(blob, "dialstatus")); |
| 1510 |
const char *forward = |
| 1511 |
ast_json_string_get(ast_json_object_get(blob, "forward")); |
| 1512 |
const char *dialstring = |
| 1513 |
ast_json_string_get(ast_json_object_get(blob, "dialstring")); |
| 1514 |
struct ast_json *caller_json = ast_channel_snapshot_to_json(ast_multi_channel_blob_get_channel(payload, "caller"), sanitize); |
| 1515 |
struct ast_json *peer_json = ast_channel_snapshot_to_json(ast_multi_channel_blob_get_channel(payload, "peer"), sanitize); |
| 1516 |
struct ast_json *forwarded_json = ast_channel_snapshot_to_json(ast_multi_channel_blob_get_channel(payload, "forwarded"), sanitize); |
| 1517 |
struct ast_json *json; |
| 1518 |
const struct timeval *tv = stasis_message_timestamp(message); |
| 1519 |
int res = 0; |
| 1520 |
|
| 1521 |
json = ast_json_pack("{s: s, s: o, s: s, s: s, s: s}", |
| 1522 |
"type", "Dial", |
| 1523 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 1524 |
"dialstatus", dialstatus, |
| 1525 |
"forward", forward, |
| 1526 |
"dialstring", dialstring); |
| 1527 |
if (!json) { |
| 1528 |
ast_json_unref(caller_json); |
| 1529 |
ast_json_unref(peer_json); |
| 1530 |
ast_json_unref(forwarded_json); |
| 1531 |
return NULL; |
| 1532 |
} |
| 1533 |
|
| 1534 |
if (caller_json) { |
| 1535 |
res |= ast_json_object_set(json, "caller", caller_json); |
| 1536 |
} |
| 1537 |
if (peer_json) { |
| 1538 |
res |= ast_json_object_set(json, "peer", peer_json); |
| 1539 |
} |
| 1540 |
if (forwarded_json) { |
| 1541 |
res |= ast_json_object_set(json, "forwarded", forwarded_json); |
| 1542 |
} |
| 1543 |
|
| 1544 |
if (res) { |
| 1545 |
ast_json_unref(json); |
| 1546 |
return NULL; |
| 1547 |
} |
| 1548 |
|
| 1549 |
return json; |
| 1550 |
} |
| 1551 |
|
| 1552 |
static struct ast_manager_event_blob *talking_start_to_ami(struct stasis_message *msg) |
| 1553 |
{ |
| 1554 |
struct ast_str *channel_string; |
| 1555 |
struct ast_channel_blob *obj = stasis_message_data(msg); |
| 1556 |
struct ast_manager_event_blob *blob; |
| 1557 |
|
| 1558 |
channel_string = ast_manager_build_channel_state_string(obj->snapshot); |
| 1559 |
if (!channel_string) { |
| 1560 |
return NULL; |
| 1561 |
} |
| 1562 |
|
| 1563 |
blob = ast_manager_event_blob_create(EVENT_FLAG_CALL, "ChannelTalkingStart", |
| 1564 |
"%s", ast_str_buffer(channel_string)); |
| 1565 |
ast_free(channel_string); |
| 1566 |
|
| 1567 |
return blob; |
| 1568 |
} |
| 1569 |
|
| 1570 |
static struct ast_json *talking_start_to_json(struct stasis_message *message, |
| 1571 |
const struct stasis_message_sanitizer *sanitize) |
| 1572 |
{ |
| 1573 |
return channel_blob_to_json(message, "ChannelTalkingStarted", sanitize); |
| 1574 |
} |
| 1575 |
|
| 1576 |
static struct ast_manager_event_blob *talking_stop_to_ami(struct stasis_message *msg) |
| 1577 |
{ |
| 1578 |
struct ast_str *channel_string; |
| 1579 |
struct ast_channel_blob *obj = stasis_message_data(msg); |
| 1580 |
int duration = ast_json_integer_get(ast_json_object_get(obj->blob, "duration")); |
| 1581 |
struct ast_manager_event_blob *blob; |
| 1582 |
|
| 1583 |
channel_string = ast_manager_build_channel_state_string(obj->snapshot); |
| 1584 |
if (!channel_string) { |
| 1585 |
return NULL; |
| 1586 |
} |
| 1587 |
|
| 1588 |
blob = ast_manager_event_blob_create(EVENT_FLAG_CALL, "ChannelTalkingStop", |
| 1589 |
"%s" |
| 1590 |
"Duration: %d\r\n", |
| 1591 |
ast_str_buffer(channel_string), |
| 1592 |
duration); |
| 1593 |
ast_free(channel_string); |
| 1594 |
|
| 1595 |
return blob; |
| 1596 |
} |
| 1597 |
|
| 1598 |
static struct ast_json *talking_stop_to_json(struct stasis_message *message, |
| 1599 |
const struct stasis_message_sanitizer *sanitize) |
| 1600 |
{ |
| 1601 |
return channel_blob_to_json(message, "ChannelTalkingFinished", sanitize); |
| 1602 |
} |
| 1603 |
|
| 1604 |
static struct ast_json *hold_to_json(struct stasis_message *message, |
| 1605 |
const struct stasis_message_sanitizer *sanitize) |
| 1606 |
{ |
| 1607 |
struct ast_channel_blob *channel_blob = stasis_message_data(message); |
| 1608 |
struct ast_json *blob = channel_blob->blob; |
| 1609 |
struct ast_channel_snapshot *snapshot = channel_blob->snapshot; |
| 1610 |
const char *musicclass = ast_json_string_get(ast_json_object_get(blob, "musicclass")); |
| 1611 |
const struct timeval *tv = stasis_message_timestamp(message); |
| 1612 |
struct ast_json *json_channel; |
| 1613 |
|
| 1614 |
json_channel = ast_channel_snapshot_to_json(snapshot, sanitize); |
| 1615 |
if (!json_channel) { |
| 1616 |
return NULL; |
| 1617 |
} |
| 1618 |
|
| 1619 |
return ast_json_pack("{s: s, s: o, s: s, s: o}", |
| 1620 |
"type", "ChannelHold", |
| 1621 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 1622 |
"musicclass", S_OR(musicclass, "N/A"), |
| 1623 |
"channel", json_channel); |
| 1624 |
} |
| 1625 |
|
| 1626 |
|
| 1627 |
static struct ast_json *tone_detect_to_json(struct stasis_message *message, |
| 1628 |
const struct stasis_message_sanitizer *sanitize) |
| 1629 |
{ |
| 1630 |
return channel_blob_to_json(message, "ChannelToneDetected", sanitize); |
| 1631 |
} |
| 1632 |
|
| 1633 |
static struct ast_json *unhold_to_json(struct stasis_message *message, |
| 1634 |
const struct stasis_message_sanitizer *sanitize) |
| 1635 |
{ |
| 1636 |
struct ast_channel_blob *channel_blob = stasis_message_data(message); |
| 1637 |
struct ast_channel_snapshot *snapshot = channel_blob->snapshot; |
| 1638 |
const struct timeval *tv = stasis_message_timestamp(message); |
| 1639 |
struct ast_json *json_channel; |
| 1640 |
|
| 1641 |
json_channel = ast_channel_snapshot_to_json(snapshot, sanitize); |
| 1642 |
if (!json_channel) { |
| 1643 |
return NULL; |
| 1644 |
} |
| 1645 |
|
| 1646 |
return ast_json_pack("{s: s, s: o, s: o}", |
| 1647 |
"type", "ChannelUnhold", |
| 1648 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 1649 |
"channel", json_channel); |
| 1650 |
} |
| 1651 |
|
| 1652 |
static const char *state2str(enum ast_control_transfer state) { |
| 1653 |
switch (state) { |
| 1654 |
case AST_TRANSFER_FAILED: |
| 1655 |
return "channel_declined"; |
| 1656 |
case AST_TRANSFER_SUCCESS: |
| 1657 |
return "channel_answered"; |
| 1658 |
case AST_TRANSFER_PROGRESS: |
| 1659 |
return "channel_progress"; |
| 1660 |
case AST_TRANSFER_UNAVAILABLE: |
| 1661 |
return "channel_declined"; |
| 1662 |
default: |
| 1663 |
return "invalid"; |
| 1664 |
} |
| 1665 |
} |
| 1666 |
|
| 1667 |
static struct ast_json *ari_transfer_to_json(struct stasis_message *msg, |
| 1668 |
const struct stasis_message_sanitizer *sanitize) |
| 1669 |
{ |
| 1670 |
struct ast_json *json_channel, *res; |
| 1671 |
struct ast_json *refer_json, *referred_json, *dest_json; |
| 1672 |
const struct timeval *tv = stasis_message_timestamp(msg); |
| 1673 |
struct ast_ari_transfer_message *transfer_msg = stasis_message_data(msg); |
| 1674 |
|
| 1675 |
dest_json = ast_json_pack("{s: s}", "destination", transfer_msg->destination); |
| 1676 |
if (!dest_json) { |
| 1677 |
return NULL; |
| 1678 |
} |
| 1679 |
|
| 1680 |
if (transfer_msg->protocol_id) { |
| 1681 |
ast_json_object_set(dest_json, "protocol_id", |
| 1682 |
ast_json_string_create(transfer_msg->protocol_id)); |
| 1683 |
} |
| 1684 |
|
| 1685 |
if (AST_VECTOR_SIZE(transfer_msg->refer_params) > 0) { |
| 1686 |
struct ast_json *params = ast_json_array_create(); |
| 1687 |
if (!params) { |
| 1688 |
return NULL; |
| 1689 |
} |
| 1690 |
for (int i = 0; i < AST_VECTOR_SIZE(transfer_msg->refer_params); ++i) { |
| 1691 |
struct ast_refer_param param = AST_VECTOR_GET(transfer_msg->refer_params, i); |
| 1692 |
ast_json_array_append(params, ast_json_pack("{s: s, s: s}", |
| 1693 |
"parameter_name", param.param_name, |
| 1694 |
"parameter_value", param.param_value)); |
| 1695 |
} |
| 1696 |
ast_json_object_set(dest_json, "additional_protocol_params", params); |
| 1697 |
} |
| 1698 |
|
| 1699 |
refer_json = ast_json_pack("{s: o}", |
| 1700 |
"requested_destination", dest_json); |
| 1701 |
if (!refer_json) { |
| 1702 |
return NULL; |
| 1703 |
} |
| 1704 |
if (transfer_msg->dest) { |
| 1705 |
struct ast_json *dest_chan_json; |
| 1706 |
|
| 1707 |
dest_chan_json = ast_channel_snapshot_to_json(transfer_msg->dest, sanitize); |
| 1708 |
ast_json_object_set(refer_json, "destination_channel", dest_chan_json); |
| 1709 |
} |
| 1710 |
if (transfer_msg->dest_peer) { |
| 1711 |
struct ast_json *peer_chan_json; |
| 1712 |
|
| 1713 |
peer_chan_json = ast_channel_snapshot_to_json(transfer_msg->dest_peer, sanitize); |
| 1714 |
ast_json_object_set(refer_json, "connected_channel", peer_chan_json); |
| 1715 |
} |
| 1716 |
if (transfer_msg->dest_bridge) { |
| 1717 |
struct ast_json *dest_bridge_json; |
| 1718 |
|
| 1719 |
dest_bridge_json = ast_bridge_snapshot_to_json(transfer_msg->dest_bridge, sanitize); |
| 1720 |
ast_json_object_set(refer_json, "bridge", dest_bridge_json); |
| 1721 |
} |
| 1722 |
|
| 1723 |
json_channel = ast_channel_snapshot_to_json(transfer_msg->source, sanitize); |
| 1724 |
if (!json_channel) { |
| 1725 |
return NULL; |
| 1726 |
} |
| 1727 |
|
| 1728 |
referred_json = ast_json_pack("{s: o}", |
| 1729 |
"source_channel", json_channel); |
| 1730 |
if (!referred_json) { |
| 1731 |
return NULL; |
| 1732 |
} |
| 1733 |
if (transfer_msg->source_peer) { |
| 1734 |
struct ast_json *peer_chan_json; |
| 1735 |
|
| 1736 |
peer_chan_json = ast_channel_snapshot_to_json(transfer_msg->source_peer, sanitize); |
| 1737 |
ast_json_object_set(referred_json, "connected_channel", peer_chan_json); |
| 1738 |
} |
| 1739 |
if (transfer_msg->source_bridge) { |
| 1740 |
struct ast_json *source_bridge_json; |
| 1741 |
|
| 1742 |
source_bridge_json = ast_bridge_snapshot_to_json(transfer_msg->source_bridge, sanitize); |
| 1743 |
ast_json_object_set(referred_json, "bridge", source_bridge_json); |
| 1744 |
} |
| 1745 |
|
| 1746 |
res = ast_json_pack("{s: s, s: o, s: o, s: o}", |
| 1747 |
"type", "ChannelTransfer", |
| 1748 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 1749 |
"refer_to", refer_json, |
| 1750 |
"referred_by", referred_json); |
| 1751 |
if (!res) { |
| 1752 |
return NULL; |
| 1753 |
} |
| 1754 |
|
| 1755 |
if (transfer_msg->state != AST_TRANSFER_INVALID) { |
| 1756 |
ast_json_object_set(res, "state", ast_json_string_create(state2str(transfer_msg->state))); |
| 1757 |
} |
| 1758 |
return res; |
| 1759 |
} |
| 1760 |
|
| 1761 |
static void ari_transfer_dtor(void *obj) |
| 1762 |
{ |
| 1763 |
struct ast_ari_transfer_message *msg = obj; |
| 1764 |
|
| 1765 |
ao2_cleanup(msg->source); |
| 1766 |
ao2_cleanup(msg->source_bridge); |
| 1767 |
ao2_cleanup(msg->source_peer); |
| 1768 |
ao2_cleanup(msg->dest); |
| 1769 |
ao2_cleanup(msg->dest_bridge); |
| 1770 |
ao2_cleanup(msg->dest_peer); |
| 1771 |
ao2_cleanup(msg->refer_params); |
| 1772 |
ast_free(msg->referred_by); |
| 1773 |
ast_free(msg->protocol_id); |
| 1774 |
} |
| 1775 |
|
| 1776 |
struct ast_ari_transfer_message *ast_ari_transfer_message_create(struct ast_channel *originating_chan, const char *referred_by, |
| 1777 |
const char *exten, const char *protocol_id, struct ast_channel *dest, |
| 1778 |
struct ast_refer_params *params, enum ast_control_transfer state) |
| 1779 |
{ |
| 1780 |
struct ast_ari_transfer_message *msg; |
| 1781 |
msg = ao2_alloc(sizeof(*msg), ari_transfer_dtor); |
| 1782 |
if (!msg) { |
| 1783 |
return NULL; |
| 1784 |
} |
| 1785 |
|
| 1786 |
msg->refer_params = params; |
| 1787 |
ao2_ref(msg->refer_params, +1); |
| 1788 |
|
| 1789 |
msg->state = state; |
| 1790 |
|
| 1791 |
ast_channel_lock(originating_chan); |
| 1792 |
msg->source = ao2_bump(ast_channel_snapshot(originating_chan)); |
| 1793 |
ast_channel_unlock(originating_chan); |
| 1794 |
if (!msg->source) { |
| 1795 |
ao2_cleanup(msg); |
| 1796 |
return NULL; |
| 1797 |
} |
| 1798 |
|
| 1799 |
if (dest) { |
| 1800 |
ast_channel_lock(dest); |
| 1801 |
msg->dest = ao2_bump(ast_channel_snapshot(dest)); |
| 1802 |
ast_channel_unlock(dest); |
| 1803 |
if (!msg->dest) { |
| 1804 |
ao2_cleanup(msg); |
| 1805 |
return NULL; |
| 1806 |
} |
| 1807 |
} |
| 1808 |
|
| 1809 |
if (referred_by) { |
| 1810 |
msg->referred_by = ast_strdup(referred_by); |
| 1811 |
if (!msg->referred_by) { |
| 1812 |
ao2_cleanup(msg); |
| 1813 |
return NULL; |
| 1814 |
} |
| 1815 |
} |
| 1816 |
|
| 1817 |
ast_copy_string(msg->destination, exten, sizeof(msg->destination)); |
| 1818 |
|
| 1819 |
if (protocol_id) { |
| 1820 |
msg->protocol_id = ast_strdup(protocol_id); |
| 1821 |
if (!msg->protocol_id) { |
| 1822 |
ao2_cleanup(msg); |
| 1823 |
return NULL; |
| 1824 |
} |
| 1825 |
} |
| 1826 |
|
| 1827 |
return msg; |
| 1828 |
} |
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_snapshot_type); |
| 1834 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_dial_type, |
| 1835 |
.to_json = dial_to_json, |
| 1836 |
); |
| 1837 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_varset_type, |
| 1838 |
.to_ami = varset_to_ami, |
| 1839 |
.to_json = varset_to_json, |
| 1840 |
); |
| 1841 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_hangup_request_type, |
| 1842 |
.to_json = hangup_request_to_json, |
| 1843 |
); |
| 1844 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_masquerade_type); |
| 1845 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_begin_type); |
| 1846 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_end_type, |
| 1847 |
.to_json = dtmf_end_to_json, |
| 1848 |
); |
| 1849 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_hold_type, |
| 1850 |
.to_json = hold_to_json, |
| 1851 |
); |
| 1852 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_unhold_type, |
| 1853 |
.to_json = unhold_to_json, |
| 1854 |
); |
| 1855 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_flash_type); |
| 1856 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_wink_type); |
| 1857 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_chanspy_start_type); |
| 1858 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_chanspy_stop_type); |
| 1859 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_fax_type); |
| 1860 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_hangup_handler_type); |
| 1861 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_moh_start_type); |
| 1862 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_moh_stop_type); |
| 1863 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_mixmonitor_start_type); |
| 1864 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_mixmonitor_stop_type); |
| 1865 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_mixmonitor_mute_type); |
| 1866 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_agent_login_type, |
| 1867 |
.to_ami = agent_login_to_ami, |
| 1868 |
); |
| 1869 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_agent_logoff_type, |
| 1870 |
.to_ami = agent_logoff_to_ami, |
| 1871 |
); |
| 1872 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_talking_start, |
| 1873 |
.to_ami = talking_start_to_ami, |
| 1874 |
.to_json = talking_start_to_json, |
| 1875 |
); |
| 1876 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_talking_stop, |
| 1877 |
.to_ami = talking_stop_to_ami, |
| 1878 |
.to_json = talking_stop_to_json, |
| 1879 |
); |
| 1880 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_tone_detect, |
| 1881 |
.to_json = tone_detect_to_json, |
| 1882 |
); |
| 1883 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_transfer_request_type, |
| 1884 |
.to_json = ari_transfer_to_json, |
| 1885 |
); |
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
static void stasis_channels_cleanup(void) |
| 1890 |
{ |
| 1891 |
ao2_cleanup(channel_topic_all); |
| 1892 |
channel_topic_all = NULL; |
| 1893 |
ao2_cleanup(channel_cache); |
| 1894 |
channel_cache = NULL; |
| 1895 |
ao2_cleanup(channel_cache_by_name); |
| 1896 |
channel_cache_by_name = NULL; |
| 1897 |
|
| 1898 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_snapshot_type); |
| 1899 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dial_type); |
| 1900 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_varset_type); |
| 1901 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hangup_request_type); |
| 1902 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_masquerade_type); |
| 1903 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_begin_type); |
| 1904 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_end_type); |
| 1905 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_flash_type); |
| 1906 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_wink_type); |
| 1907 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hold_type); |
| 1908 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_unhold_type); |
| 1909 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_chanspy_start_type); |
| 1910 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_chanspy_stop_type); |
| 1911 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_fax_type); |
| 1912 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hangup_handler_type); |
| 1913 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_moh_start_type); |
| 1914 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_moh_stop_type); |
| 1915 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_mixmonitor_start_type); |
| 1916 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_mixmonitor_stop_type); |
| 1917 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_mixmonitor_mute_type); |
| 1918 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_agent_login_type); |
| 1919 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_agent_logoff_type); |
| 1920 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_talking_start); |
| 1921 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_talking_stop); |
| 1922 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_tone_detect); |
| 1923 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_transfer_request_type); |
| 1924 |
} |
| 1925 |
|
| 1926 |
int ast_stasis_channels_init(void) |
| 1927 |
{ |
| 1928 |
int res = 0; |
| 1929 |
|
| 1930 |
ast_register_cleanup(stasis_channels_cleanup); |
| 1931 |
|
| 1932 |
channel_topic_all = stasis_topic_create("channel:all"); |
| 1933 |
if (!channel_topic_all) { |
| 1934 |
return -1; |
| 1935 |
} |
| 1936 |
|
| 1937 |
channel_cache = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, |
| 1938 |
0, AST_NUM_CHANNEL_BUCKETS, channel_snapshot_uniqueid_hash_cb, |
| 1939 |
NULL, channel_snapshot_uniqueid_cmp_cb); |
| 1940 |
if (!channel_cache) { |
| 1941 |
return -1; |
| 1942 |
} |
| 1943 |
|
| 1944 |
channel_cache_by_name = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, |
| 1945 |
0, AST_NUM_CHANNEL_BUCKETS, channel_snapshot_hash_cb, |
| 1946 |
NULL, channel_snapshot_cmp_cb); |
| 1947 |
if (!channel_cache_by_name) { |
| 1948 |
return -1; |
| 1949 |
} |
| 1950 |
|
| 1951 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_agent_login_type); |
| 1952 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_agent_logoff_type); |
| 1953 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_snapshot_type); |
| 1954 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dial_type); |
| 1955 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_varset_type); |
| 1956 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_hangup_request_type); |
| 1957 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_masquerade_type); |
| 1958 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_begin_type); |
| 1959 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_end_type); |
| 1960 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_flash_type); |
| 1961 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_wink_type); |
| 1962 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_hold_type); |
| 1963 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_unhold_type); |
| 1964 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_chanspy_start_type); |
| 1965 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_chanspy_stop_type); |
| 1966 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_fax_type); |
| 1967 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_hangup_handler_type); |
| 1968 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_moh_start_type); |
| 1969 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_moh_stop_type); |
| 1970 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_mixmonitor_start_type); |
| 1971 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_mixmonitor_stop_type); |
| 1972 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_mixmonitor_mute_type); |
| 1973 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_talking_start); |
| 1974 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_talking_stop); |
| 1975 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_tone_detect); |
| 1976 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_transfer_request_type); |
| 1977 |
|
| 1978 |
return res; |
| 1979 |
} |
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
struct dial_target { |
| 1986 |
|
| 1987 |
struct ast_channel *peer; |
| 1988 |
|
| 1989 |
char *dialstring; |
| 1990 |
|
| 1991 |
AST_LIST_ENTRY(dial_target) list; |
| 1992 |
}; |
| 1993 |
|
| 1994 |
static void dial_target_free(struct dial_target *doomed) |
| 1995 |
{ |
| 1996 |
if (!doomed) { |
| 1997 |
return; |
| 1998 |
} |
| 1999 |
ast_free(doomed->dialstring); |
| 2000 |
ast_channel_cleanup(doomed->peer); |
| 2001 |
ast_free(doomed); |
| 2002 |
} |
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
struct dial_masquerade_datastore { |
| 2010 |
|
| 2011 |
struct ast_channel *caller; |
| 2012 |
|
| 2013 |
AST_LIST_HEAD_NOLOCK(, dial_target) dialed_peers; |
| 2014 |
}; |
| 2015 |
|
| 2016 |
static void dial_masquerade_datastore_cleanup(struct dial_masquerade_datastore *masq_data) |
| 2017 |
{ |
| 2018 |
struct dial_target *cur; |
| 2019 |
|
| 2020 |
while ((cur = AST_LIST_REMOVE_HEAD(&masq_data->dialed_peers, list))) { |
| 2021 |
dial_target_free(cur); |
| 2022 |
} |
| 2023 |
} |
| 2024 |
|
| 2025 |
static void dial_masquerade_datastore_remove_chan(struct dial_masquerade_datastore *masq_data, struct ast_channel *chan) |
| 2026 |
{ |
| 2027 |
struct dial_target *cur; |
| 2028 |
|
| 2029 |
ao2_lock(masq_data); |
| 2030 |
if (masq_data->caller == chan) { |
| 2031 |
dial_masquerade_datastore_cleanup(masq_data); |
| 2032 |
} else { |
| 2033 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&masq_data->dialed_peers, cur, list) { |
| 2034 |
if (cur->peer == chan) { |
| 2035 |
AST_LIST_REMOVE_CURRENT(list); |
| 2036 |
dial_target_free(cur); |
| 2037 |
break; |
| 2038 |
} |
| 2039 |
} |
| 2040 |
AST_LIST_TRAVERSE_SAFE_END; |
| 2041 |
} |
| 2042 |
ao2_unlock(masq_data); |
| 2043 |
} |
| 2044 |
|
| 2045 |
static void dial_masquerade_datastore_dtor(void *vdoomed) |
| 2046 |
{ |
| 2047 |
dial_masquerade_datastore_cleanup(vdoomed); |
| 2048 |
} |
| 2049 |
|
| 2050 |
static struct dial_masquerade_datastore *dial_masquerade_datastore_alloc(void) |
| 2051 |
{ |
| 2052 |
struct dial_masquerade_datastore *masq_data; |
| 2053 |
|
| 2054 |
masq_data = ao2_alloc(sizeof(struct dial_masquerade_datastore), |
| 2055 |
dial_masquerade_datastore_dtor); |
| 2056 |
if (!masq_data) { |
| 2057 |
return NULL; |
| 2058 |
} |
| 2059 |
AST_LIST_HEAD_INIT_NOLOCK(&masq_data->dialed_peers); |
| 2060 |
return masq_data; |
| 2061 |
} |
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
static void dial_masquerade_datastore_destroy(void *data) |
| 2068 |
{ |
| 2069 |
ao2_ref(data, -1); |
| 2070 |
} |
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
static void dial_masquerade_caller_datastore_destroy(void *data) |
| 2077 |
{ |
| 2078 |
dial_masquerade_datastore_cleanup(data); |
| 2079 |
ao2_ref(data, -1); |
| 2080 |
} |
| 2081 |
|
| 2082 |
static struct ast_datastore *dial_masquerade_datastore_find(struct ast_channel *chan); |
| 2083 |
|
| 2084 |
static void dial_masquerade_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan) |
| 2085 |
{ |
| 2086 |
struct dial_masquerade_datastore *masq_data = data; |
| 2087 |
struct dial_target *cur; |
| 2088 |
struct ast_datastore *datastore; |
| 2089 |
|
| 2090 |
ao2_lock(masq_data); |
| 2091 |
if (!masq_data->caller) { |
| 2092 |
|
| 2093 |
} else if (masq_data->caller == old_chan) { |
| 2094 |
|
| 2095 |
ast_debug(1, "Caller channel %s being masqueraded out to %s (is_empty:%d)\n", |
| 2096 |
ast_channel_name(new_chan), ast_channel_name(old_chan), |
| 2097 |
AST_LIST_EMPTY(&masq_data->dialed_peers)); |
| 2098 |
AST_LIST_TRAVERSE(&masq_data->dialed_peers, cur, list) { |
| 2099 |
ast_channel_publish_dial_internal(new_chan, cur->peer, NULL, |
| 2100 |
cur->dialstring, "NOANSWER", NULL); |
| 2101 |
ast_channel_publish_dial_internal(old_chan, cur->peer, NULL, |
| 2102 |
cur->dialstring, NULL, NULL); |
| 2103 |
} |
| 2104 |
dial_masquerade_datastore_cleanup(masq_data); |
| 2105 |
} else { |
| 2106 |
|
| 2107 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&masq_data->dialed_peers, cur, list) { |
| 2108 |
if (cur->peer == old_chan) { |
| 2109 |
ast_debug(1, "Peer channel %s being masqueraded out to %s\n", |
| 2110 |
ast_channel_name(new_chan), ast_channel_name(old_chan)); |
| 2111 |
ast_channel_publish_dial_internal(masq_data->caller, new_chan, NULL, |
| 2112 |
cur->dialstring, "CANCEL", NULL); |
| 2113 |
ast_channel_publish_dial_internal(masq_data->caller, old_chan, NULL, |
| 2114 |
cur->dialstring, NULL, NULL); |
| 2115 |
|
| 2116 |
AST_LIST_REMOVE_CURRENT(list); |
| 2117 |
dial_target_free(cur); |
| 2118 |
break; |
| 2119 |
} |
| 2120 |
} |
| 2121 |
AST_LIST_TRAVERSE_SAFE_END; |
| 2122 |
} |
| 2123 |
ao2_unlock(masq_data); |
| 2124 |
|
| 2125 |
|
| 2126 |
datastore = dial_masquerade_datastore_find(old_chan); |
| 2127 |
if (!datastore) { |
| 2128 |
return; |
| 2129 |
} |
| 2130 |
ast_channel_datastore_remove(old_chan, datastore); |
| 2131 |
ast_datastore_free(datastore); |
| 2132 |
} |
| 2133 |
|
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
|
| 2143 |
static void dial_masquerade_breakdown(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan) |
| 2144 |
{ |
| 2145 |
struct dial_masquerade_datastore *masq_data = data; |
| 2146 |
struct dial_target *cur; |
| 2147 |
|
| 2148 |
ao2_lock(masq_data); |
| 2149 |
|
| 2150 |
if (!masq_data->caller) { |
| 2151 |
ao2_unlock(masq_data); |
| 2152 |
return; |
| 2153 |
} |
| 2154 |
|
| 2155 |
if (masq_data->caller == new_chan) { |
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
ast_debug(1, "Caller channel %s being masqueraded into by %s (is_empty:%d)\n", |
| 2161 |
ast_channel_name(old_chan), ast_channel_name(new_chan), |
| 2162 |
AST_LIST_EMPTY(&masq_data->dialed_peers)); |
| 2163 |
AST_LIST_TRAVERSE(&masq_data->dialed_peers, cur, list) { |
| 2164 |
ast_channel_publish_dial_internal(old_chan, cur->peer, NULL, |
| 2165 |
cur->dialstring, "NOANSWER", NULL); |
| 2166 |
ast_channel_publish_dial_internal(new_chan, cur->peer, NULL, |
| 2167 |
cur->dialstring, NULL, NULL); |
| 2168 |
} |
| 2169 |
|
| 2170 |
ao2_unlock(masq_data); |
| 2171 |
return; |
| 2172 |
} |
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
|
| 2177 |
|
| 2178 |
AST_LIST_TRAVERSE(&masq_data->dialed_peers, cur, list) { |
| 2179 |
if (cur->peer == new_chan) { |
| 2180 |
ast_debug(1, "Peer channel %s being masqueraded into by %s\n", |
| 2181 |
ast_channel_name(old_chan), ast_channel_name(new_chan)); |
| 2182 |
ast_channel_publish_dial_internal(masq_data->caller, old_chan, NULL, |
| 2183 |
cur->dialstring, "CANCEL", NULL); |
| 2184 |
ast_channel_publish_dial_internal(masq_data->caller, new_chan, NULL, |
| 2185 |
cur->dialstring, NULL, NULL); |
| 2186 |
break; |
| 2187 |
} |
| 2188 |
} |
| 2189 |
|
| 2190 |
ao2_unlock(masq_data); |
| 2191 |
} |
| 2192 |
|
| 2193 |
static const struct ast_datastore_info dial_masquerade_info = { |
| 2194 |
.type = "stasis-chan-dial-masq", |
| 2195 |
.destroy = dial_masquerade_datastore_destroy, |
| 2196 |
.chan_fixup = dial_masquerade_fixup, |
| 2197 |
.chan_breakdown = dial_masquerade_breakdown, |
| 2198 |
}; |
| 2199 |
|
| 2200 |
static const struct ast_datastore_info dial_masquerade_caller_info = { |
| 2201 |
.type = "stasis-chan-dial-masq", |
| 2202 |
.destroy = dial_masquerade_caller_datastore_destroy, |
| 2203 |
.chan_fixup = dial_masquerade_fixup, |
| 2204 |
.chan_breakdown = dial_masquerade_breakdown, |
| 2205 |
}; |
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
|
| 2211 |
|
| 2212 |
|
| 2213 |
|
| 2214 |
|
| 2215 |
static struct ast_datastore *dial_masquerade_datastore_find(struct ast_channel *chan) |
| 2216 |
{ |
| 2217 |
struct ast_datastore *datastore; |
| 2218 |
|
| 2219 |
datastore = ast_channel_datastore_find(chan, &dial_masquerade_info, NULL); |
| 2220 |
if (!datastore) { |
| 2221 |
datastore = ast_channel_datastore_find(chan, &dial_masquerade_caller_info, NULL); |
| 2222 |
} |
| 2223 |
|
| 2224 |
return datastore; |
| 2225 |
} |
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
static struct dial_masquerade_datastore *dial_masquerade_datastore_add( |
| 2239 |
struct ast_channel *chan, struct dial_masquerade_datastore *masq_data) |
| 2240 |
{ |
| 2241 |
struct ast_datastore *datastore; |
| 2242 |
|
| 2243 |
datastore = ast_datastore_alloc(!masq_data ? &dial_masquerade_caller_info : &dial_masquerade_info, NULL); |
| 2244 |
if (!datastore) { |
| 2245 |
return NULL; |
| 2246 |
} |
| 2247 |
|
| 2248 |
if (!masq_data) { |
| 2249 |
masq_data = dial_masquerade_datastore_alloc(); |
| 2250 |
if (!masq_data) { |
| 2251 |
ast_datastore_free(datastore); |
| 2252 |
return NULL; |
| 2253 |
} |
| 2254 |
masq_data->caller = chan; |
| 2255 |
} |
| 2256 |
|
| 2257 |
datastore->data = masq_data; |
| 2258 |
ast_channel_datastore_add(chan, datastore); |
| 2259 |
|
| 2260 |
return masq_data; |
| 2261 |
} |
| 2262 |
|
| 2263 |
static int set_dial_masquerade(struct ast_channel *caller, struct ast_channel *peer, const char *dialstring) |
| 2264 |
{ |
| 2265 |
struct ast_datastore *datastore; |
| 2266 |
struct dial_masquerade_datastore *masq_data; |
| 2267 |
struct dial_target *target; |
| 2268 |
|
| 2269 |
|
| 2270 |
datastore = dial_masquerade_datastore_find(caller); |
| 2271 |
if (!datastore) { |
| 2272 |
masq_data = dial_masquerade_datastore_add(caller, NULL); |
| 2273 |
} else { |
| 2274 |
masq_data = datastore->data; |
| 2275 |
} |
| 2276 |
if (!masq_data) { |
| 2277 |
return -1; |
| 2278 |
} |
| 2279 |
ao2_ref(masq_data, +1); |
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
ast_assert(masq_data->caller == caller); |
| 2287 |
|
| 2288 |
|
| 2289 |
target = ast_calloc(1, sizeof(*target)); |
| 2290 |
if (!target) { |
| 2291 |
ao2_ref(masq_data, -1); |
| 2292 |
return -1; |
| 2293 |
} |
| 2294 |
if (dialstring) { |
| 2295 |
target->dialstring = ast_strdup(dialstring); |
| 2296 |
if (!target->dialstring) { |
| 2297 |
ast_free(target); |
| 2298 |
ao2_ref(masq_data, -1); |
| 2299 |
return -1; |
| 2300 |
} |
| 2301 |
} |
| 2302 |
target->peer = ast_channel_ref(peer); |
| 2303 |
|
| 2304 |
|
| 2305 |
ao2_lock(masq_data); |
| 2306 |
dial_masquerade_datastore_remove_chan(masq_data, peer); |
| 2307 |
AST_LIST_INSERT_HEAD(&masq_data->dialed_peers, target, list); |
| 2308 |
ao2_unlock(masq_data); |
| 2309 |
|
| 2310 |
datastore = dial_masquerade_datastore_find(peer); |
| 2311 |
if (datastore) { |
| 2312 |
if (datastore->data == masq_data) { |
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
ao2_ref(masq_data, -1); |
| 2318 |
return 0; |
| 2319 |
} |
| 2320 |
|
| 2321 |
|
| 2322 |
ast_assert(0); |
| 2323 |
|
| 2324 |
|
| 2325 |
dial_masquerade_datastore_remove_chan(datastore->data, peer); |
| 2326 |
ast_channel_datastore_remove(peer, datastore); |
| 2327 |
ast_datastore_free(datastore); |
| 2328 |
} |
| 2329 |
|
| 2330 |
|
| 2331 |
if (dial_masquerade_datastore_add(peer, masq_data)) { |
| 2332 |
|
| 2333 |
return 0; |
| 2334 |
} |
| 2335 |
|
| 2336 |
|
| 2337 |
dial_masquerade_datastore_remove_chan(masq_data, peer); |
| 2338 |
ao2_ref(masq_data, -1); |
| 2339 |
return -1; |
| 2340 |
} |
| 2341 |
|
| 2342 |
static void remove_dial_masquerade(struct ast_channel *peer) |
| 2343 |
{ |
| 2344 |
struct ast_datastore *datastore; |
| 2345 |
struct dial_masquerade_datastore *masq_data; |
| 2346 |
|
| 2347 |
datastore = dial_masquerade_datastore_find(peer); |
| 2348 |
if (!datastore) { |
| 2349 |
return; |
| 2350 |
} |
| 2351 |
|
| 2352 |
masq_data = datastore->data; |
| 2353 |
if (masq_data) { |
| 2354 |
dial_masquerade_datastore_remove_chan(masq_data, peer); |
| 2355 |
} |
| 2356 |
|
| 2357 |
ast_channel_datastore_remove(peer, datastore); |
| 2358 |
ast_datastore_free(datastore); |
| 2359 |
} |
| 2360 |
|
| 2361 |
static void remove_dial_masquerade_caller(struct ast_channel *caller) |
| 2362 |
{ |
| 2363 |
struct ast_datastore *datastore; |
| 2364 |
struct dial_masquerade_datastore *masq_data; |
| 2365 |
|
| 2366 |
datastore = dial_masquerade_datastore_find(caller); |
| 2367 |
if (!datastore) { |
| 2368 |
return; |
| 2369 |
} |
| 2370 |
|
| 2371 |
masq_data = datastore->data; |
| 2372 |
if (!masq_data || !AST_LIST_EMPTY(&masq_data->dialed_peers)) { |
| 2373 |
return; |
| 2374 |
} |
| 2375 |
|
| 2376 |
dial_masquerade_datastore_remove_chan(masq_data, caller); |
| 2377 |
|
| 2378 |
ast_channel_datastore_remove(caller, datastore); |
| 2379 |
ast_datastore_free(datastore); |
| 2380 |
} |