| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/_private.h" |
| 33 |
|
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/datastore.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/manager.h" |
| 38 |
#include "asterisk/stasis_bridges.h" |
| 39 |
#include "asterisk/stasis_channels.h" |
| 40 |
#include "asterisk/strings.h" |
| 41 |
#include "asterisk/astobj2.h" |
| 42 |
#include "asterisk/vector.h" |
| 43 |
#include "asterisk/app.h" |
| 44 |
#include "asterisk/taskprocessor.h" |
| 45 |
#include "asterisk/refer.h" |
| 46 |
|
| 47 |
struct refer_data { |
| 48 |
|
| 49 |
char *name; |
| 50 |
|
| 51 |
char *value; |
| 52 |
|
| 53 |
char stuff[0]; |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
struct ast_refer { |
| 60 |
AST_DECLARE_STRING_FIELDS( |
| 61 |
|
| 62 |
AST_STRING_FIELD(to); |
| 63 |
|
| 64 |
AST_STRING_FIELD(from); |
| 65 |
|
| 66 |
AST_STRING_FIELD(refer_to); |
| 67 |
|
| 68 |
AST_STRING_FIELD(endpoint); |
| 69 |
|
| 70 |
AST_STRING_FIELD(tech); |
| 71 |
); |
| 72 |
|
| 73 |
int to_self; |
| 74 |
|
| 75 |
struct ao2_container *vars; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
static ast_rwlock_t refer_techs_lock; |
| 80 |
|
| 81 |
|
| 82 |
AST_VECTOR(, const struct ast_refer_tech *) refer_techs; |
| 83 |
|
| 84 |
static int refer_data_cmp_fn(void *obj, void *arg, int flags) |
| 85 |
{ |
| 86 |
const struct refer_data *object_left = obj; |
| 87 |
const struct refer_data *object_right = arg; |
| 88 |
const char *right_key = arg; |
| 89 |
int cmp; |
| 90 |
|
| 91 |
switch (flags & OBJ_SEARCH_MASK) { |
| 92 |
case OBJ_SEARCH_OBJECT: |
| 93 |
right_key = object_right->name; |
| 94 |
case OBJ_SEARCH_KEY: |
| 95 |
cmp = strcasecmp(object_left->name, right_key); |
| 96 |
break; |
| 97 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 98 |
cmp = strncasecmp(object_left->name, right_key, strlen(right_key)); |
| 99 |
break; |
| 100 |
default: |
| 101 |
cmp = 0; |
| 102 |
break; |
| 103 |
} |
| 104 |
if (cmp) { |
| 105 |
return 0; |
| 106 |
} |
| 107 |
return CMP_MATCH; |
| 108 |
} |
| 109 |
|
| 110 |
static void refer_data_destructor(void *obj) |
| 111 |
{ |
| 112 |
struct refer_data *data = obj; |
| 113 |
ast_free(data->value); |
| 114 |
} |
| 115 |
|
| 116 |
static void refer_destructor(void *obj) |
| 117 |
{ |
| 118 |
struct ast_refer *refer = obj; |
| 119 |
|
| 120 |
ast_string_field_free_memory(refer); |
| 121 |
ao2_cleanup(refer->vars); |
| 122 |
} |
| 123 |
|
| 124 |
struct ast_refer *ast_refer_alloc(void) |
| 125 |
{ |
| 126 |
struct ast_refer *refer; |
| 127 |
|
| 128 |
if (!(refer = ao2_alloc_options(sizeof(*refer), refer_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK))) { |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
|
| 132 |
if (ast_string_field_init(refer, 128)) { |
| 133 |
ao2_ref(refer, -1); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
refer->vars = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 138 |
NULL, refer_data_cmp_fn); |
| 139 |
if (!refer->vars) { |
| 140 |
ao2_ref(refer, -1); |
| 141 |
return NULL; |
| 142 |
} |
| 143 |
refer->to_self = 0; |
| 144 |
|
| 145 |
return refer; |
| 146 |
} |
| 147 |
|
| 148 |
struct ast_refer *ast_refer_ref(struct ast_refer *refer) |
| 149 |
{ |
| 150 |
ao2_ref(refer, 1); |
| 151 |
return refer; |
| 152 |
} |
| 153 |
|
| 154 |
struct ast_refer *ast_refer_destroy(struct ast_refer *refer) |
| 155 |
{ |
| 156 |
ao2_ref(refer, -1); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
int ast_refer_set_to(struct ast_refer *refer, const char *fmt, ...) |
| 161 |
{ |
| 162 |
va_list ap; |
| 163 |
|
| 164 |
va_start(ap, fmt); |
| 165 |
ast_string_field_build_va(refer, to, fmt, ap); |
| 166 |
va_end(ap); |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
int ast_refer_set_from(struct ast_refer *refer, const char *fmt, ...) |
| 172 |
{ |
| 173 |
va_list ap; |
| 174 |
|
| 175 |
va_start(ap, fmt); |
| 176 |
ast_string_field_build_va(refer, from, fmt, ap); |
| 177 |
va_end(ap); |
| 178 |
|
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
int ast_refer_set_refer_to(struct ast_refer *refer, const char *fmt, ...) |
| 183 |
{ |
| 184 |
va_list ap; |
| 185 |
|
| 186 |
va_start(ap, fmt); |
| 187 |
ast_string_field_build_va(refer, refer_to, fmt, ap); |
| 188 |
va_end(ap); |
| 189 |
|
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
int ast_refer_set_to_self(struct ast_refer *refer, int val) |
| 194 |
{ |
| 195 |
refer->to_self = val; |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
int ast_refer_set_tech(struct ast_refer *refer, const char *fmt, ...) |
| 200 |
{ |
| 201 |
va_list ap; |
| 202 |
|
| 203 |
va_start(ap, fmt); |
| 204 |
ast_string_field_build_va(refer, tech, fmt, ap); |
| 205 |
va_end(ap); |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
int ast_refer_set_endpoint(struct ast_refer *refer, const char *fmt, ...) |
| 211 |
{ |
| 212 |
va_list ap; |
| 213 |
|
| 214 |
va_start(ap, fmt); |
| 215 |
ast_string_field_build_va(refer, endpoint, fmt, ap); |
| 216 |
va_end(ap); |
| 217 |
|
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
const char *ast_refer_get_refer_to(const struct ast_refer *refer) |
| 222 |
{ |
| 223 |
return refer->refer_to; |
| 224 |
} |
| 225 |
|
| 226 |
const char *ast_refer_get_from(const struct ast_refer *refer) |
| 227 |
{ |
| 228 |
return refer->from; |
| 229 |
} |
| 230 |
|
| 231 |
const char *ast_refer_get_to(const struct ast_refer *refer) |
| 232 |
{ |
| 233 |
return refer->to; |
| 234 |
} |
| 235 |
|
| 236 |
int ast_refer_get_to_self(const struct ast_refer *refer) |
| 237 |
{ |
| 238 |
return refer->to_self; |
| 239 |
} |
| 240 |
|
| 241 |
const char *ast_refer_get_tech(const struct ast_refer *refer) |
| 242 |
{ |
| 243 |
return refer->tech; |
| 244 |
} |
| 245 |
|
| 246 |
const char *ast_refer_get_endpoint(const struct ast_refer *refer) |
| 247 |
{ |
| 248 |
return refer->endpoint; |
| 249 |
} |
| 250 |
|
| 251 |
static struct refer_data *refer_data_new(const char *name) |
| 252 |
{ |
| 253 |
struct refer_data *data; |
| 254 |
int name_len = strlen(name) + 1; |
| 255 |
|
| 256 |
if ((data = ao2_alloc_options(name_len + sizeof(*data), refer_data_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK))) { |
| 257 |
data->name = data->stuff; |
| 258 |
strcpy(data->name, name); |
| 259 |
} |
| 260 |
|
| 261 |
return data; |
| 262 |
} |
| 263 |
|
| 264 |
static struct refer_data *refer_data_find(struct ao2_container *vars, const char *name) |
| 265 |
{ |
| 266 |
return ao2_find(vars, name, OBJ_SEARCH_KEY); |
| 267 |
} |
| 268 |
|
| 269 |
char *ast_refer_get_var_and_unlink(struct ast_refer *refer, const char *name) |
| 270 |
{ |
| 271 |
struct refer_data *data; |
| 272 |
char *val = NULL; |
| 273 |
|
| 274 |
if (!(data = ao2_find(refer->vars, name, OBJ_SEARCH_KEY | OBJ_UNLINK))) { |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
|
| 278 |
val = ast_strdup(data->value); |
| 279 |
ao2_ref(data, -1); |
| 280 |
|
| 281 |
return val; |
| 282 |
} |
| 283 |
|
| 284 |
static int refer_set_var_full(struct ast_refer *refer, const char *name, const char *value) |
| 285 |
{ |
| 286 |
struct refer_data *data; |
| 287 |
|
| 288 |
if (!(data = refer_data_find(refer->vars, name))) { |
| 289 |
if (ast_strlen_zero(value)) { |
| 290 |
return 0; |
| 291 |
} |
| 292 |
if (!(data = refer_data_new(name))) { |
| 293 |
return -1; |
| 294 |
}; |
| 295 |
data->value = ast_strdup(value); |
| 296 |
|
| 297 |
ao2_link(refer->vars, data); |
| 298 |
} else { |
| 299 |
if (ast_strlen_zero(value)) { |
| 300 |
ao2_unlink(refer->vars, data); |
| 301 |
} else { |
| 302 |
ast_free(data->value); |
| 303 |
data->value = ast_strdup(value); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
ao2_ref(data, -1); |
| 308 |
|
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
int ast_refer_set_var_outbound(struct ast_refer *refer, const char *name, const char *value) |
| 313 |
{ |
| 314 |
return refer_set_var_full(refer, name, value); |
| 315 |
} |
| 316 |
|
| 317 |
const char *ast_refer_get_var(struct ast_refer *refer, const char *name) |
| 318 |
{ |
| 319 |
struct refer_data *data; |
| 320 |
const char *val = NULL; |
| 321 |
|
| 322 |
if (!(data = refer_data_find(refer->vars, name))) { |
| 323 |
return NULL; |
| 324 |
} |
| 325 |
|
| 326 |
val = data->value; |
| 327 |
ao2_ref(data, -1); |
| 328 |
|
| 329 |
return val; |
| 330 |
} |
| 331 |
|
| 332 |
struct ast_refer_var_iterator { |
| 333 |
struct ao2_iterator iter; |
| 334 |
struct refer_data *current_used; |
| 335 |
}; |
| 336 |
|
| 337 |
struct ast_refer_var_iterator *ast_refer_var_iterator_init(const struct ast_refer *refer) |
| 338 |
{ |
| 339 |
struct ast_refer_var_iterator *iter; |
| 340 |
|
| 341 |
iter = ast_calloc(1, sizeof(*iter)); |
| 342 |
if (!iter) { |
| 343 |
return NULL; |
| 344 |
} |
| 345 |
|
| 346 |
iter->iter = ao2_iterator_init(refer->vars, 0); |
| 347 |
|
| 348 |
return iter; |
| 349 |
} |
| 350 |
|
| 351 |
int ast_refer_var_iterator_next(struct ast_refer_var_iterator *iter, const char **name, const char **value) |
| 352 |
{ |
| 353 |
struct refer_data *data; |
| 354 |
|
| 355 |
if (!iter) { |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
data = ao2_iterator_next(&iter->iter); |
| 360 |
if (!data) { |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
*name = data->name; |
| 365 |
*value = data->value; |
| 366 |
|
| 367 |
iter->current_used = data; |
| 368 |
|
| 369 |
return 1; |
| 370 |
} |
| 371 |
|
| 372 |
void ast_refer_var_unref_current(struct ast_refer_var_iterator *iter) |
| 373 |
{ |
| 374 |
ao2_cleanup(iter->current_used); |
| 375 |
iter->current_used = NULL; |
| 376 |
} |
| 377 |
|
| 378 |
void ast_refer_var_iterator_destroy(struct ast_refer_var_iterator *iter) |
| 379 |
{ |
| 380 |
if (iter) { |
| 381 |
ao2_iterator_destroy(&iter->iter); |
| 382 |
ast_refer_var_unref_current(iter); |
| 383 |
ast_free(iter); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
static const struct ast_refer_tech *refer_find_by_tech_name(const char *tech_name) |
| 399 |
{ |
| 400 |
const struct ast_refer_tech *current; |
| 401 |
int i; |
| 402 |
|
| 403 |
for (i = 0; i < AST_VECTOR_SIZE(&refer_techs); i++) { |
| 404 |
current = AST_VECTOR_GET(&refer_techs, i); |
| 405 |
if (!strcmp(current->name, tech_name)) { |
| 406 |
return current; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
return NULL; |
| 411 |
} |
| 412 |
|
| 413 |
int ast_refer_send(struct ast_refer *refer) |
| 414 |
{ |
| 415 |
char *tech_name = NULL; |
| 416 |
const struct ast_refer_tech *refer_tech; |
| 417 |
int res = -1; |
| 418 |
|
| 419 |
if (ast_strlen_zero(refer->to)) { |
| 420 |
ao2_ref(refer, -1); |
| 421 |
return -1; |
| 422 |
} |
| 423 |
|
| 424 |
tech_name = ast_strdupa(refer->to); |
| 425 |
tech_name = strsep(&tech_name, ":"); |
| 426 |
|
| 427 |
ast_rwlock_rdlock(&refer_techs_lock); |
| 428 |
refer_tech = refer_find_by_tech_name(tech_name); |
| 429 |
|
| 430 |
if (!refer_tech) { |
| 431 |
ast_log(LOG_ERROR, "Unknown refer tech: %s\n", tech_name); |
| 432 |
ast_rwlock_unlock(&refer_techs_lock); |
| 433 |
ao2_ref(refer, -1); |
| 434 |
return -1; |
| 435 |
} |
| 436 |
|
| 437 |
ao2_lock(refer); |
| 438 |
res = refer_tech->refer_send(refer); |
| 439 |
ao2_unlock(refer); |
| 440 |
|
| 441 |
ast_rwlock_unlock(&refer_techs_lock); |
| 442 |
|
| 443 |
ao2_ref(refer, -1); |
| 444 |
|
| 445 |
return res; |
| 446 |
} |
| 447 |
|
| 448 |
int ast_refer_tech_register(const struct ast_refer_tech *tech) |
| 449 |
{ |
| 450 |
const struct ast_refer_tech *match; |
| 451 |
|
| 452 |
ast_rwlock_wrlock(&refer_techs_lock); |
| 453 |
|
| 454 |
match = refer_find_by_tech_name(tech->name); |
| 455 |
if (match) { |
| 456 |
ast_log(LOG_ERROR, "Refer technology already registered for '%s'\n", |
| 457 |
tech->name); |
| 458 |
ast_rwlock_unlock(&refer_techs_lock); |
| 459 |
return -1; |
| 460 |
} |
| 461 |
|
| 462 |
if (AST_VECTOR_APPEND(&refer_techs, tech)) { |
| 463 |
ast_log(LOG_ERROR, "Failed to register refer technology for '%s'\n", |
| 464 |
tech->name); |
| 465 |
ast_rwlock_unlock(&refer_techs_lock); |
| 466 |
return -1; |
| 467 |
} |
| 468 |
ast_verb(5, "Refer technology '%s' registered.\n", tech->name); |
| 469 |
|
| 470 |
ast_rwlock_unlock(&refer_techs_lock); |
| 471 |
|
| 472 |
return 0; |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
static int refer_tech_cmp(const struct ast_refer_tech *vec_elem, const struct ast_refer_tech *srch) |
| 485 |
{ |
| 486 |
if (!vec_elem->name || !srch->name) { |
| 487 |
return (vec_elem->name == srch->name) ? 1 : 0; |
| 488 |
} |
| 489 |
return !strcmp(vec_elem->name, srch->name); |
| 490 |
} |
| 491 |
|
| 492 |
int ast_refer_tech_unregister(const struct ast_refer_tech *tech) |
| 493 |
{ |
| 494 |
int match; |
| 495 |
|
| 496 |
ast_rwlock_wrlock(&refer_techs_lock); |
| 497 |
match = AST_VECTOR_REMOVE_CMP_UNORDERED(&refer_techs, tech, refer_tech_cmp, |
| 498 |
AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 499 |
ast_rwlock_unlock(&refer_techs_lock); |
| 500 |
|
| 501 |
if (match) { |
| 502 |
ast_log(LOG_ERROR, "No '%s' refer technology found.\n", tech->name); |
| 503 |
return -1; |
| 504 |
} |
| 505 |
|
| 506 |
ast_verb(5, "Refer technology '%s' unregistered.\n", tech->name); |
| 507 |
|
| 508 |
return 0; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
static void refer_shutdown(void) |
| 516 |
{ |
| 517 |
AST_VECTOR_FREE(&refer_techs); |
| 518 |
ast_rwlock_destroy(&refer_techs_lock); |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
int ast_refer_init(void) |
| 532 |
{ |
| 533 |
ast_rwlock_init(&refer_techs_lock); |
| 534 |
if (AST_VECTOR_INIT(&refer_techs, 8)) { |
| 535 |
return -1; |
| 536 |
} |
| 537 |
ast_register_cleanup(refer_shutdown); |
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
int ast_refer_notify_transfer_request(struct ast_channel *source, const char *referred_by, |
| 542 |
const char *exten, const char *protocol_id, |
| 543 |
struct ast_channel *dest, struct ast_refer_params *params, |
| 544 |
enum ast_control_transfer state) |
| 545 |
{ |
| 546 |
RAII_VAR(struct ast_ari_transfer_message *, transfer_message, NULL, ao2_cleanup); |
| 547 |
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); |
| 548 |
RAII_VAR(struct ast_bridge *, source_bridge, NULL, ao2_cleanup); |
| 549 |
RAII_VAR(struct ast_bridge *, dest_bridge, NULL, ao2_cleanup); |
| 550 |
|
| 551 |
transfer_message = ast_ari_transfer_message_create(source, referred_by, exten, protocol_id, dest, params, state); |
| 552 |
if (!transfer_message) { |
| 553 |
return -1; |
| 554 |
} |
| 555 |
source_bridge = ast_bridge_transfer_acquire_bridge(source); |
| 556 |
if (source_bridge) { |
| 557 |
RAII_VAR(struct ast_channel *, peer, NULL, ast_channel_cleanup); |
| 558 |
|
| 559 |
ast_bridge_lock(source_bridge); |
| 560 |
transfer_message->source_bridge = ast_bridge_get_snapshot(source_bridge); |
| 561 |
peer = ast_bridge_peer_nolock(source_bridge, source); |
| 562 |
if (peer) { |
| 563 |
ast_channel_lock(peer); |
| 564 |
transfer_message->source_peer = ao2_bump(ast_channel_snapshot(peer)); |
| 565 |
ast_channel_unlock(peer); |
| 566 |
} |
| 567 |
ast_bridge_unlock(source_bridge); |
| 568 |
} |
| 569 |
|
| 570 |
if (dest) { |
| 571 |
dest_bridge = ast_bridge_transfer_acquire_bridge(dest); |
| 572 |
if (dest_bridge) { |
| 573 |
RAII_VAR(struct ast_channel *, peer, NULL, ast_channel_cleanup); |
| 574 |
|
| 575 |
ast_bridge_lock(dest_bridge); |
| 576 |
transfer_message->dest_bridge = ast_bridge_get_snapshot(dest_bridge); |
| 577 |
peer = ast_bridge_peer_nolock(dest_bridge, dest); |
| 578 |
if (peer) { |
| 579 |
ast_channel_lock(peer); |
| 580 |
transfer_message->dest_peer = ao2_bump(ast_channel_snapshot(peer)); |
| 581 |
ast_channel_unlock(peer); |
| 582 |
} |
| 583 |
ast_bridge_unlock(dest_bridge); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
msg = stasis_message_create(ast_channel_transfer_request_type(), transfer_message); |
| 588 |
if (msg) { |
| 589 |
ast_channel_lock(source); |
| 590 |
stasis_publish(ast_channel_topic(source), msg); |
| 591 |
ast_channel_unlock(source); |
| 592 |
} |
| 593 |
|
| 594 |
return 0; |
| 595 |
} |