| 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 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include <errno.h> |
| 35 |
#include <regex.h> |
| 36 |
|
| 37 |
#include "asterisk/astobj2.h" |
| 38 |
#include "asterisk/channel.h" |
| 39 |
#include "asterisk/http.h" |
| 40 |
#include "asterisk/json.h" |
| 41 |
#include "asterisk/lock.h" |
| 42 |
#include "asterisk/module.h" |
| 43 |
#include "asterisk/pbx.h" |
| 44 |
#include "asterisk/stasis_app.h" |
| 45 |
#include "asterisk/stasis_app_impl.h" |
| 46 |
#include "asterisk/stasis_channels.h" |
| 47 |
#include "asterisk/time.h" |
| 48 |
#include "asterisk/utils.h" |
| 49 |
|
| 50 |
#define AST_API_MODULE |
| 51 |
#include "asterisk/stasis_app_broadcast.h" |
| 52 |
|
| 53 |
#define BROADCAST_BUCKETS 37 |
| 54 |
|
| 55 |
|
| 56 |
#define MAX_REGEX_LENGTH 256 |
| 57 |
|
| 58 |
|
| 59 |
#define MAX_GROUP_DEPTH 10 |
| 60 |
|
| 61 |
|
| 62 |
#define MAX_NESTED_QUANTIFIERS 3 |
| 63 |
|
| 64 |
|
| 65 |
#define MAX_QUANTIFIER_BOUND 100 |
| 66 |
|
| 67 |
|
| 68 |
#define MAX_ALTERNATIONS 20 |
| 69 |
|
| 70 |
|
| 71 |
#define ALTERNATION_DEPTH_THRESHOLD 2 |
| 72 |
|
| 73 |
|
| 74 |
#define MAX_BROADCAST_TIMEOUT_MS (24 * 60 * 60 * 1000) |
| 75 |
|
| 76 |
|
| 77 |
#define BROADCAST_POLL_INTERVAL_MS 200 |
| 78 |
|
| 79 |
|
| 80 |
struct stasis_broadcast_ctx { |
| 81 |
|
| 82 |
char channel_id[AST_MAX_PUBLIC_UNIQUEID]; |
| 83 |
|
| 84 |
char *winner_app; |
| 85 |
|
| 86 |
char app_filter[MAX_REGEX_LENGTH + 1]; |
| 87 |
|
| 88 |
regex_t compiled_filter; |
| 89 |
|
| 90 |
unsigned int claimed:1; |
| 91 |
|
| 92 |
unsigned int filter_compiled:1; |
| 93 |
|
| 94 |
unsigned int finished:1; |
| 95 |
|
| 96 |
unsigned int flags; |
| 97 |
|
| 98 |
struct ao2_container *container; |
| 99 |
|
| 100 |
ast_cond_t cond; |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
static struct ao2_container *broadcast_contexts; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
static void broadcast_datastore_destroy(void *data) |
| 113 |
{ |
| 114 |
struct stasis_broadcast_ctx *ctx = data; |
| 115 |
|
| 116 |
if (ctx->container) { |
| 117 |
ao2_unlink(ctx->container, ctx); |
| 118 |
} |
| 119 |
ao2_cleanup(ctx); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
static const struct ast_datastore_info broadcast_datastore_info = { |
| 124 |
.type = "stasis_broadcast_context", |
| 125 |
.destroy = broadcast_datastore_destroy, |
| 126 |
}; |
| 127 |
|
| 128 |
AO2_STRING_FIELD_HASH_FN(stasis_broadcast_ctx, channel_id) |
| 129 |
AO2_STRING_FIELD_CMP_FN(stasis_broadcast_ctx, channel_id) |
| 130 |
|
| 131 |
|
| 132 |
static void broadcast_ctx_destructor(void *obj) |
| 133 |
{ |
| 134 |
struct stasis_broadcast_ctx *ctx = obj; |
| 135 |
ast_free(ctx->winner_app); |
| 136 |
if (ctx->filter_compiled) { |
| 137 |
regfree(&ctx->compiled_filter); |
| 138 |
} |
| 139 |
ao2_cleanup(ctx->container); |
| 140 |
ast_cond_destroy(&ctx->cond); |
| 141 |
} |
| 142 |
|
| 143 |
static int validate_regex_pattern(const char *pattern); |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static struct stasis_broadcast_ctx *broadcast_ctx_create( |
| 152 |
const char *channel_id, const char *app_filter, unsigned int flags) |
| 153 |
{ |
| 154 |
struct stasis_broadcast_ctx *ctx; |
| 155 |
|
| 156 |
ctx = ao2_alloc(sizeof(*ctx), broadcast_ctx_destructor); |
| 157 |
if (!ctx) { |
| 158 |
return NULL; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
ast_copy_string(ctx->channel_id, channel_id, sizeof(ctx->channel_id)); |
| 163 |
ctx->flags = flags; |
| 164 |
ctx->container = ao2_bump(broadcast_contexts); |
| 165 |
ast_cond_init(&ctx->cond, NULL); |
| 166 |
|
| 167 |
|
| 168 |
if (!ast_strlen_zero(app_filter)) { |
| 169 |
ast_copy_string(ctx->app_filter, app_filter, sizeof(ctx->app_filter)); |
| 170 |
if (validate_regex_pattern(app_filter) != 0) { |
| 171 |
ast_log(LOG_WARNING, |
| 172 |
"Channel %s: rejecting app_filter regex as potentially dangerous: %s\n", |
| 173 |
channel_id, app_filter); |
| 174 |
} else if (regcomp(&ctx->compiled_filter, app_filter, |
| 175 |
REG_EXTENDED | REG_NOSUB) != 0) { |
| 176 |
ast_log(LOG_WARNING, |
| 177 |
"Channel %s: failed to compile app_filter regex '%s'\n", |
| 178 |
channel_id, app_filter); |
| 179 |
} else { |
| 180 |
ctx->filter_compiled = 1; |
| 181 |
} |
| 182 |
|
| 183 |
if (!ctx->filter_compiled) { |
| 184 |
ast_log(LOG_WARNING, |
| 185 |
"Channel %s: proceeding without application filtering due to invalid regex\n", |
| 186 |
channel_id); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
ast_debug(1, "Created broadcast context for channel %s (filter: %s, flags: 0x%x)\n", |
| 191 |
ctx->channel_id, |
| 192 |
ctx->filter_compiled ? ctx->app_filter : "none", |
| 193 |
ctx->flags); |
| 194 |
|
| 195 |
return ctx; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
static int validate_regex_pattern(const char *pattern) |
| 208 |
{ |
| 209 |
size_t len; |
| 210 |
int group_depth = 0; |
| 211 |
int quantified_groups = 0; |
| 212 |
int in_class = 0; |
| 213 |
|
| 214 |
int alternations_per_depth[MAX_GROUP_DEPTH + 1] = { 0 }; |
| 215 |
const char *p; |
| 216 |
|
| 217 |
if (ast_strlen_zero(pattern)) { |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
len = strlen(pattern); |
| 223 |
if (len > MAX_REGEX_LENGTH) { |
| 224 |
ast_debug(3, "Regex pattern exceeds maximum length of %d characters (got %zu)\n", |
| 225 |
MAX_REGEX_LENGTH, len); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
for (p = pattern; *p; p++) { |
| 240 |
|
| 241 |
if (!in_class && *p == '[' && (p == pattern || *(p - 1) != '\\')) { |
| 242 |
in_class = 1; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if (*(p + 1) == '^') { |
| 248 |
p++; |
| 249 |
} |
| 250 |
if (*(p + 1) == ']') { |
| 251 |
p++; |
| 252 |
} |
| 253 |
continue; |
| 254 |
} else if (in_class) { |
| 255 |
if (*p == '\\') { |
| 256 |
|
| 257 |
if (*(p + 1)) { |
| 258 |
p++; |
| 259 |
} |
| 260 |
continue; |
| 261 |
} |
| 262 |
if (*p == ']') { |
| 263 |
in_class = 0; |
| 264 |
} |
| 265 |
|
| 266 |
continue; |
| 267 |
} |
| 268 |
switch (*p) { |
| 269 |
case '(': |
| 270 |
group_depth++; |
| 271 |
if (group_depth > MAX_GROUP_DEPTH) { |
| 272 |
ast_debug(3, "Regex pattern has too many nested groups (max %d)\n", |
| 273 |
MAX_GROUP_DEPTH); |
| 274 |
return -1; |
| 275 |
} |
| 276 |
|
| 277 |
alternations_per_depth[group_depth] = 0; |
| 278 |
break; |
| 279 |
case ')': |
| 280 |
if (group_depth > 0) { |
| 281 |
|
| 282 |
alternations_per_depth[group_depth] = 0; |
| 283 |
group_depth--; |
| 284 |
} |
| 285 |
break; |
| 286 |
case '+': |
| 287 |
case '*': |
| 288 |
case '?': |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
if (p > pattern && *(p - 1) == ')') { |
| 294 |
quantified_groups++; |
| 295 |
} |
| 296 |
break; |
| 297 |
case '{': { |
| 298 |
|
| 299 |
const char *q = p + 1; |
| 300 |
long m = 0, n = -1; |
| 301 |
int valid = 0; |
| 302 |
int digit; |
| 303 |
int overflow = 0; |
| 304 |
|
| 305 |
if (*q >= '0' && *q <= '9') { |
| 306 |
|
| 307 |
while (*q >= '0' && *q <= '9') { |
| 308 |
digit = (*q - '0'); |
| 309 |
if (m > (LONG_MAX - digit) / 10) { |
| 310 |
overflow = 1; |
| 311 |
break; |
| 312 |
} |
| 313 |
m = (m * 10) + digit; |
| 314 |
if (m > MAX_QUANTIFIER_BOUND) { |
| 315 |
overflow = 1; |
| 316 |
break; |
| 317 |
} |
| 318 |
q++; |
| 319 |
} |
| 320 |
if (!overflow && *q == ',') { |
| 321 |
q++; |
| 322 |
if (*q >= '0' && *q <= '9') { |
| 323 |
long nn = 0; |
| 324 |
while (*q >= '0' && *q <= '9') { |
| 325 |
digit = (*q - '0'); |
| 326 |
if (nn > (LONG_MAX - digit) / 10) { |
| 327 |
overflow = 1; |
| 328 |
break; |
| 329 |
} |
| 330 |
nn = (nn * 10) + digit; |
| 331 |
if (nn > MAX_QUANTIFIER_BOUND) { |
| 332 |
overflow = 1; |
| 333 |
break; |
| 334 |
} |
| 335 |
q++; |
| 336 |
} |
| 337 |
n = nn; |
| 338 |
} else { |
| 339 |
n = -1; |
| 340 |
} |
| 341 |
} else if (!overflow) { |
| 342 |
n = m; |
| 343 |
} |
| 344 |
if (!overflow && *q == '}') { |
| 345 |
valid = 1; |
| 346 |
} |
| 347 |
} |
| 348 |
if (overflow) { |
| 349 |
ast_debug(3, "Regex quantifier overflow or exceeds max bound (max %d)\n", MAX_QUANTIFIER_BOUND); |
| 350 |
return -1; |
| 351 |
} |
| 352 |
if (valid) { |
| 353 |
|
| 354 |
if (m > MAX_QUANTIFIER_BOUND || (n != -1 && n > MAX_QUANTIFIER_BOUND)) { |
| 355 |
ast_debug(3, "Regex quantifier bounds too large (max %d)\n", MAX_QUANTIFIER_BOUND); |
| 356 |
return -1; |
| 357 |
} |
| 358 |
if (p > pattern && *(p - 1) == ')') { |
| 359 |
quantified_groups++; |
| 360 |
} |
| 361 |
p = q; |
| 362 |
} |
| 363 |
break; |
| 364 |
} |
| 365 |
case '|': |
| 366 |
if (group_depth > 0) { |
| 367 |
alternations_per_depth[group_depth]++; |
| 368 |
if (group_depth > ALTERNATION_DEPTH_THRESHOLD && |
| 369 |
alternations_per_depth[group_depth] > MAX_ALTERNATIONS) { |
| 370 |
ast_debug(3, |
| 371 |
"Regex has too many alternations in deep group (depth %d, count %d, max %d)\n", |
| 372 |
group_depth, |
| 373 |
alternations_per_depth[group_depth], |
| 374 |
MAX_ALTERNATIONS); |
| 375 |
return -1; |
| 376 |
} |
| 377 |
} |
| 378 |
break; |
| 379 |
case '\\': |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
if (*(p + 1)) { |
| 387 |
p++; |
| 388 |
} |
| 389 |
|
| 390 |
continue; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
if (quantified_groups > MAX_NESTED_QUANTIFIERS) { |
| 400 |
ast_debug(3, "Regex pattern has too many quantified groups (max %d)\n", |
| 401 |
MAX_NESTED_QUANTIFIERS); |
| 402 |
return -1; |
| 403 |
} |
| 404 |
|
| 405 |
return 0; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
static int send_broadcast_event(struct ast_channel *chan, |
| 413 |
struct stasis_broadcast_ctx *ctx) |
| 414 |
{ |
| 415 |
RAII_VAR(struct ast_json *, event, NULL, ast_json_unref); |
| 416 |
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup); |
| 417 |
RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup); |
| 418 |
struct ao2_iterator iter; |
| 419 |
char *app_name; |
| 420 |
const char *caller = NULL; |
| 421 |
const char *called = NULL; |
| 422 |
|
| 423 |
|
| 424 |
ast_channel_lock(chan); |
| 425 |
snapshot = ao2_bump(ast_channel_snapshot(chan)); |
| 426 |
caller = ast_strdupa(S_OR(ast_channel_caller(chan)->id.number.str, "")); |
| 427 |
called = ast_strdupa(S_OR(ast_channel_exten(chan), "")); |
| 428 |
ast_channel_unlock(chan); |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
event = ast_json_pack("{s: s, s: o, s: o, s: s?, s: s?}", |
| 434 |
"type", "CallBroadcast", |
| 435 |
"timestamp", ast_json_timeval(ast_tvnow(), NULL), |
| 436 |
"channel", ast_channel_snapshot_to_json(snapshot, NULL), |
| 437 |
"caller", caller, |
| 438 |
"called", called); |
| 439 |
|
| 440 |
if (!event) { |
| 441 |
ast_log(LOG_ERROR, "Channel %s: failed to create broadcast event\n", |
| 442 |
ast_channel_name(chan)); |
| 443 |
return -1; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
apps = stasis_app_get_all(); |
| 448 |
if (!apps) { |
| 449 |
ast_log(LOG_ERROR, "Channel %s: failed to get stasis applications\n", |
| 450 |
ast_channel_name(chan)); |
| 451 |
return -1; |
| 452 |
} |
| 453 |
|
| 454 |
ast_debug(2, "Broadcasting to %d registered Stasis applications\n", |
| 455 |
ao2_container_count(apps)); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
he |
| 465 |
|
| 466 |
|
| 467 |
{ |
| 468 |
int app_count; |
| 469 |
char **matching_arr; |
| 470 |
int n = 0; |
| 471 |
int i; |
| 472 |
|
| 473 |
app_count = ao2_container_count(apps); |
| 474 |
if (app_count == 0) { |
| 475 |
ast_debug(2, "Channel %s: no Stasis applications registered\n", |
| 476 |
ast_channel_uniqueid(chan)); |
| 477 |
return 0; |
| 478 |
} |
| 479 |
|
| 480 |
matching_arr = ast_malloc(app_count * sizeof(*matching_arr)); |
| 481 |
if (!matching_arr) { |
| 482 |
ast_log(LOG_ERROR, "Channel %s: failed to allocate matching apps array\n", |
| 483 |
ast_channel_name(chan)); |
| 484 |
return -1; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
iter = ao2_iterator_init(apps, 0); |
| 489 |
while ((app_name = ao2_iterator_next(&iter)) && n < app_count) { |
| 490 |
if (ctx->filter_compiled && |
| 491 |
regexec(&ctx->compiled_filter, app_name, 0, NULL, 0) == REG_NOMATCH) { |
| 492 |
ast_debug(3, "App '%s' does not match filter, skipping\n", app_name); |
| 493 |
ao2_ref(app_name, -1); |
| 494 |
continue; |
| 495 |
} |
| 496 |
matching_arr[n++] = app_name; |
| 497 |
} |
| 498 |
ao2_iterator_destroy(&iter); |
| 499 |
|
| 500 |
ast_debug(2, "Broadcasting channel %s to %d matching applications\n", |
| 501 |
ast_channel_uniqueid(chan), n); |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
for (i = n - 1; i > 0; i--) { |
| 506 |
int j = ast_random() % (i + 1); |
| 507 |
char *tmp = matching_arr[i]; |
| 508 |
matching_arr[i] = matching_arr[j]; |
| 509 |
matching_arr[j] = tmp; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
for (i = 0; i < n; i++) { |
| 518 |
char *match_name = matching_arr[i]; |
| 519 |
struct ast_json *event_copy; |
| 520 |
|
| 521 |
ast_debug(3, "Sending broadcast to app '%s'\n", match_name); |
| 522 |
|
| 523 |
event_copy = ast_json_deep_copy(event); |
| 524 |
if (!event_copy) { |
| 525 |
ast_log(LOG_ERROR, |
| 526 |
"Channel %s: failed to deep-copy event for app '%s'\n", |
| 527 |
ast_channel_uniqueid(chan), match_name); |
| 528 |
ao2_ref(match_name, -1); |
| 529 |
continue; |
| 530 |
} |
| 531 |
|
| 532 |
stasis_app_send(match_name, event_copy); |
| 533 |
ast_json_unref(event_copy); |
| 534 |
ao2_ref(match_name, -1); |
| 535 |
} |
| 536 |
|
| 537 |
ast_free(matching_arr); |
| 538 |
} |
| 539 |
|
| 540 |
return 0; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
int AST_OPTIONAL_API_NAME(stasis_app_broadcast_channel)(struct ast_channel *chan, int timeout_ms, |
| 551 |
const char *app_filter, unsigned int flags) |
| 552 |
{ |
| 553 |
RAII_VAR(struct stasis_broadcast_ctx *, ctx, NULL, ao2_cleanup); |
| 554 |
struct ast_datastore *datastore; |
| 555 |
|
| 556 |
if (!chan) { |
| 557 |
return -1; |
| 558 |
} |
| 559 |
|
| 560 |
if (!broadcast_contexts) { |
| 561 |
return -1; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
{ |
| 569 |
struct ast_datastore *old_ds; |
| 570 |
ast_channel_lock(chan); |
| 571 |
old_ds = ast_channel_datastore_find(chan, &broadcast_datastore_info, NULL); |
| 572 |
if (old_ds) { |
| 573 |
ast_channel_datastore_remove(chan, old_ds); |
| 574 |
} |
| 575 |
ast_channel_unlock(chan); |
| 576 |
if (old_ds) { |
| 577 |
ast_datastore_free(old_ds); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
ctx = broadcast_ctx_create(ast_channel_uniqueid(chan), app_filter, flags); |
| 583 |
if (!ctx) { |
| 584 |
ast_log(LOG_ERROR, "Channel %s: failed to create broadcast context\n", |
| 585 |
ast_channel_uniqueid(chan)); |
| 586 |
return -1; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
ao2_link(broadcast_contexts, ctx); |
| 591 |
|
| 592 |
|
| 593 |
datastore = ast_datastore_alloc(&broadcast_datastore_info, ast_channel_uniqueid(chan)); |
| 594 |
if (!datastore) { |
| 595 |
ast_log(LOG_ERROR, "Channel %s: failed to allocate broadcast datastore\n", |
| 596 |
ast_channel_uniqueid(chan)); |
| 597 |
ao2_unlink(broadcast_contexts, ctx); |
| 598 |
return -1; |
| 599 |
} |
| 600 |
|
| 601 |
datastore->data = ao2_bump(ctx); |
| 602 |
ast_channel_lock(chan); |
| 603 |
if (ast_channel_datastore_add(chan, datastore)) { |
| 604 |
ast_channel_unlock(chan); |
| 605 |
ast_log(LOG_ERROR, "Channel %s: failed to attach broadcast datastore\n", |
| 606 |
ast_channel_uniqueid(chan)); |
| 607 |
ast_datastore_free(datastore); |
| 608 |
ao2_unlink(broadcast_contexts, ctx); |
| 609 |
return -1; |
| 610 |
} |
| 611 |
ast_channel_unlock(chan); |
| 612 |
|
| 613 |
ast_debug(1, "Starting broadcast for channel %s (timeout: %dms, filter: %s)\n", |
| 614 |
ast_channel_uniqueid(chan), timeout_ms, app_filter ? app_filter : "none"); |
| 615 |
|
| 616 |
|
| 617 |
if (send_broadcast_event(chan, ctx) != 0) { |
| 618 |
ast_log(LOG_ERROR, "Channel %s: failed to send broadcast event\n", |
| 619 |
ast_channel_uniqueid(chan)); |
| 620 |
ast_channel_lock(chan); |
| 621 |
ast_channel_datastore_remove(chan, datastore); |
| 622 |
ast_channel_unlock(chan); |
| 623 |
ast_datastore_free(datastore); |
| 624 |
ao2_unlink(broadcast_contexts, ctx); |
| 625 |
return -1; |
| 626 |
} |
| 627 |
|
| 628 |
return 0; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
int AST_OPTIONAL_API_NAME(stasis_app_claim_channel)(const char *channel_id, const char *app_name) |
| 638 |
{ |
| 639 |
RAII_VAR(struct stasis_broadcast_ctx *, ctx, NULL, ao2_cleanup); |
| 640 |
|
| 641 |
if (ast_strlen_zero(channel_id) || ast_strlen_zero(app_name)) { |
| 642 |
return -1; |
| 643 |
} |
| 644 |
|
| 645 |
if (!broadcast_contexts) { |
| 646 |
return -1; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
ctx = ao2_find(broadcast_contexts, channel_id, OBJ_SEARCH_KEY); |
| 651 |
if (!ctx) { |
| 652 |
ast_debug(1, "No broadcast context found for channel %s\n", channel_id); |
| 653 |
return -1; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
ao2_lock(ctx); |
| 661 |
if (ctx->claimed) { |
| 662 |
ast_debug(1, "Channel %s already claimed by %s (attempt by %s denied)\n", |
| 663 |
channel_id, ctx->winner_app ? ctx->winner_app : "(unknown)", app_name); |
| 664 |
ao2_unlock(ctx); |
| 665 |
return -2; |
| 666 |
} |
| 667 |
if (ctx->finished) { |
| 668 |
ast_debug(1, "Channel %s broadcast already finished (late claim by %s rejected)\n", |
| 669 |
channel_id, app_name); |
| 670 |
ao2_unlock(ctx); |
| 671 |
return -1; |
| 672 |
} |
| 673 |
ctx->winner_app = ast_strdup(app_name); |
| 674 |
if (!ctx->winner_app) { |
| 675 |
ast_log(LOG_ERROR, |
| 676 |
"Failed to allocate winner app name for channel %s\n", |
| 677 |
channel_id); |
| 678 |
ao2_unlock(ctx); |
| 679 |
return -1; |
| 680 |
} |
| 681 |
ctx->claimed = 1; |
| 682 |
ast_verb(3, "Channel %s claimed by application %s\n", |
| 683 |
channel_id, app_name); |
| 684 |
|
| 685 |
ast_cond_signal(&ctx->cond); |
| 686 |
ao2_unlock(ctx); |
| 687 |
|
| 688 |
|
| 689 |
if (!(ctx->flags & STASIS_BROADCAST_FLAG_SUPPRESS_CLAIMED)) { |
| 690 |
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup); |
| 691 |
RAII_VAR(struct ast_json *, event, NULL, ast_json_unref); |
| 692 |
RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup); |
| 693 |
struct ao2_iterator iter; |
| 694 |
char *app_name_iter; |
| 695 |
|
| 696 |
snapshot = ast_channel_snapshot_get_latest(channel_id); |
| 697 |
if (snapshot) { |
| 698 |
event = ast_json_pack("{s: s, s: o, s: o, s: s}", |
| 699 |
"type", "CallClaimed", |
| 700 |
"timestamp", ast_json_timeval(ast_tvnow(), NULL), |
| 701 |
"channel", ast_channel_snapshot_to_json(snapshot, NULL), |
| 702 |
"winner_app", app_name); |
| 703 |
} |
| 704 |
if (event) { |
| 705 |
apps = stasis_app_get_all(); |
| 706 |
} |
| 707 |
if (apps) { |
| 708 |
iter = ao2_iterator_init(apps, 0); |
| 709 |
while ((app_name_iter = ao2_iterator_next(&iter))) { |
| 710 |
struct ast_json *event_copy; |
| 711 |
|
| 712 |
|
| 713 |
if (ctx->filter_compiled && |
| 714 |
regexec(&ctx->compiled_filter, app_name_iter, |
| 715 |
0, NULL, 0) == REG_NOMATCH) { |
| 716 |
ao2_ref(app_name_iter, -1); |
| 717 |
continue; |
| 718 |
} |
| 719 |
|
| 720 |
event_copy = ast_json_deep_copy(event); |
| 721 |
if (!event_copy) { |
| 722 |
ao2_ref(app_name_iter, -1); |
| 723 |
continue; |
| 724 |
} |
| 725 |
|
| 726 |
stasis_app_send(app_name_iter, event_copy); |
| 727 |
ast_json_unref(event_copy); |
| 728 |
ao2_ref(app_name_iter, -1); |
| 729 |
} |
| 730 |
ao2_iterator_destroy(&iter); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
return 0; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
char *AST_OPTIONAL_API_NAME(stasis_app_broadcast_winner)(const char *channel_id) |
| 744 |
{ |
| 745 |
RAII_VAR(struct stasis_broadcast_ctx *, ctx, NULL, ao2_cleanup); |
| 746 |
char *winner = NULL; |
| 747 |
|
| 748 |
if (ast_strlen_zero(channel_id)) { |
| 749 |
return NULL; |
| 750 |
} |
| 751 |
|
| 752 |
ctx = ao2_find(broadcast_contexts, channel_id, OBJ_SEARCH_KEY); |
| 753 |
if (!ctx) { |
| 754 |
return NULL; |
| 755 |
} |
| 756 |
|
| 757 |
ao2_lock(ctx); |
| 758 |
if (ctx->claimed) { |
| 759 |
winner = ast_strdup(ctx->winner_app); |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
ctx->finished = 1; |
| 765 |
ao2_unlock(ctx); |
| 766 |
|
| 767 |
return winner; |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
int AST_OPTIONAL_API_NAME(stasis_app_broadcast_wait)(struct ast_channel *chan, int timeout_ms) |
| 783 |
{ |
| 784 |
RAII_VAR(struct stasis_broadcast_ctx *, ctx, NULL, ao2_cleanup); |
| 785 |
const char *channel_id; |
| 786 |
struct timeval deadline; |
| 787 |
int result = -1; |
| 788 |
|
| 789 |
if (!chan) { |
| 790 |
return -1; |
| 791 |
} |
| 792 |
|
| 793 |
channel_id = ast_channel_uniqueid(chan); |
| 794 |
|
| 795 |
if (!broadcast_contexts) { |
| 796 |
return -1; |
| 797 |
} |
| 798 |
|
| 799 |
ctx = ao2_find(broadcast_contexts, channel_id, OBJ_SEARCH_KEY); |
| 800 |
if (!ctx) { |
| 801 |
ast_log(LOG_WARNING, "No broadcast context for channel %s\n", channel_id); |
| 802 |
return -1; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
if (timeout_ms < 0) { |
| 807 |
timeout_ms = 0; |
| 808 |
} else if (timeout_ms > MAX_BROADCAST_TIMEOUT_MS) { |
| 809 |
timeout_ms = MAX_BROADCAST_TIMEOUT_MS; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
deadline = ast_tvadd(ast_tvnow(), |
| 814 |
ast_tv(timeout_ms / 1000, (timeout_ms % 1000) * 1000)); |
| 815 |
|
| 816 |
ao2_lock(ctx); |
| 817 |
while (!ctx->claimed) { |
| 818 |
struct timeval now; |
| 819 |
struct timespec poll_spec; |
| 820 |
long remaining_ms; |
| 821 |
long poll_ms; |
| 822 |
int wait_result; |
| 823 |
|
| 824 |
|
| 825 |
if (ast_check_hangup(chan)) { |
| 826 |
ast_debug(3, "Channel %s hung up during broadcast wait\n", |
| 827 |
channel_id); |
| 828 |
break; |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
now = ast_tvnow(); |
| 833 |
remaining_ms = ast_tvdiff_ms(deadline, now); |
| 834 |
if (remaining_ms <= 0) { |
| 835 |
ast_debug(3, "Broadcast timeout for channel %s after %dms\n", |
| 836 |
channel_id, timeout_ms); |
| 837 |
break; |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
poll_ms = remaining_ms; |
| 842 |
if (poll_ms > BROADCAST_POLL_INTERVAL_MS) { |
| 843 |
poll_ms = BROADCAST_POLL_INTERVAL_MS; |
| 844 |
} |
| 845 |
|
| 846 |
poll_spec.tv_sec = now.tv_sec + (poll_ms / 1000); |
| 847 |
poll_spec.tv_nsec = (long)(now.tv_usec) * 1000L |
| 848 |
+ (long)(poll_ms % 1000) * 1000000L; |
| 849 |
while (poll_spec.tv_nsec >= 1000000000) { |
| 850 |
poll_spec.tv_sec++; |
| 851 |
poll_spec.tv_nsec -= 1000000000; |
| 852 |
} |
| 853 |
|
| 854 |
wait_result = ast_cond_timedwait(&ctx->cond, ao2_object_get_lockaddr(ctx), &poll_spec); |
| 855 |
if (wait_result != 0 && wait_result != ETIMEDOUT) { |
| 856 |
ast_log(LOG_WARNING, |
| 857 |
"Channel %s: unexpected error waiting for claim: %s (%d)\n", |
| 858 |
channel_id, strerror(wait_result), wait_result); |
| 859 |
break; |
| 860 |
} |
| 861 |
|
| 862 |
} |
| 863 |
|
| 864 |
if (ctx->claimed) { |
| 865 |
ast_debug(1, "Channel %s claimed by %s\n", |
| 866 |
channel_id, ctx->winner_app); |
| 867 |
result = 0; |
| 868 |
} |
| 869 |
ao2_unlock(ctx); |
| 870 |
|
| 871 |
return result; |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
void AST_OPTIONAL_API_NAME(stasis_app_broadcast_cleanup)(const char *channel_id) |
| 886 |
{ |
| 887 |
RAII_VAR(struct stasis_broadcast_ctx *, ctx, NULL, ao2_cleanup); |
| 888 |
|
| 889 |
if (ast_strlen_zero(channel_id) || !broadcast_contexts) { |
| 890 |
return; |
| 891 |
} |
| 892 |
|
| 893 |
ctx = ao2_find(broadcast_contexts, channel_id, OBJ_SEARCH_KEY | OBJ_UNLINK); |
| 894 |
if (ctx) { |
| 895 |
ast_debug(3, "Cleaning up broadcast context for %s\n", channel_id); |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
static int load_module(void) |
| 900 |
{ |
| 901 |
broadcast_contexts = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 902 |
BROADCAST_BUCKETS, stasis_broadcast_ctx_hash_fn, NULL, stasis_broadcast_ctx_cmp_fn); |
| 903 |
|
| 904 |
if (!broadcast_contexts) { |
| 905 |
return AST_MODULE_LOAD_DECLINE; |
| 906 |
} |
| 907 |
|
| 908 |
ast_debug(1, "Stasis broadcast module loaded\n"); |
| 909 |
return AST_MODULE_LOAD_SUCCESS; |
| 910 |
} |
| 911 |
|
| 912 |
static int unload_module(void) |
| 913 |
{ |
| 914 |
|
| 915 |
|
| 916 |
{ |
| 917 |
struct ao2_container *old_contexts = broadcast_contexts; |
| 918 |
broadcast_contexts = NULL; |
| 919 |
ao2_cleanup(old_contexts); |
| 920 |
} |
| 921 |
|
| 922 |
ast_debug(1, "Stasis broadcast module unloaded\n"); |
| 923 |
return 0; |
| 924 |
} |
| 925 |
|
| 926 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, |
| 927 |
"Stasis application broadcast", |
| 928 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 929 |
.load = load_module, |
| 930 |
.unload = unload_module, |
| 931 |
.requires = "res_stasis,res_ari,http", |
| 932 |
.load_pri = AST_MODPRI_APP_DEPEND - 1, |
| 933 |
); |