| 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 |
#include "asterisk.h" |
| 26 |
|
| 27 |
#include <pjsip.h> |
| 28 |
#include <pjsip_ua.h> |
| 29 |
|
| 30 |
#include "asterisk/app.h" |
| 31 |
#include "asterisk/cli.h" |
| 32 |
#include "asterisk/config.h" |
| 33 |
#include "asterisk/manager.h" |
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/res_pjsip.h" |
| 37 |
#include "asterisk/res_pjsip_session.h" |
| 38 |
#include "asterisk/sorcery.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
#define CONTENT_TYPE_SIZE 64 |
| 128 |
#define CONTENT_SIZE 512 |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
static const char notify_config[] = "pjsip_notify.conf"; |
| 135 |
|
| 136 |
struct notify_option_item { |
| 137 |
const char *name; |
| 138 |
const char *value; |
| 139 |
char buf[0]; |
| 140 |
}; |
| 141 |
|
| 142 |
struct notify_option { |
| 143 |
|
| 144 |
struct ao2_container *items; |
| 145 |
|
| 146 |
char name[0]; |
| 147 |
}; |
| 148 |
|
| 149 |
static int notify_option_hash(const void *obj, int flags) |
| 150 |
{ |
| 151 |
const struct notify_option *option = obj; |
| 152 |
return ast_str_case_hash(flags & OBJ_KEY ? obj : option->name); |
| 153 |
} |
| 154 |
|
| 155 |
static int notify_option_cmp(void *obj, void *arg, int flags) |
| 156 |
{ |
| 157 |
struct notify_option *option1 = obj; |
| 158 |
struct notify_option *option2 = arg; |
| 159 |
const char *key = flags & OBJ_KEY ? arg : option2->name; |
| 160 |
|
| 161 |
return strcasecmp(option1->name, key) ? 0 : CMP_MATCH; |
| 162 |
} |
| 163 |
|
| 164 |
static void notify_option_destroy(void *obj) |
| 165 |
{ |
| 166 |
struct notify_option *option = obj; |
| 167 |
ao2_cleanup(option->items); |
| 168 |
} |
| 169 |
|
| 170 |
static void *notify_option_alloc(const char *category) |
| 171 |
{ |
| 172 |
int category_size = strlen(category) + 1; |
| 173 |
|
| 174 |
struct notify_option *option = ao2_alloc( |
| 175 |
sizeof(*option) + category_size, notify_option_destroy); |
| 176 |
|
| 177 |
if (!option) { |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
ast_copy_string(option->name, category, category_size); |
| 182 |
|
| 183 |
if (!(option->items = ao2_container_alloc_list( |
| 184 |
AO2_ALLOC_OPT_LOCK_NOLOCK, |
| 185 |
AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW, NULL, NULL))) { |
| 186 |
ao2_cleanup(option); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
return option; |
| 191 |
} |
| 192 |
|
| 193 |
static void *notify_option_find(struct ao2_container *container, const char *category) |
| 194 |
{ |
| 195 |
return ao2_find(container, category, OBJ_KEY); |
| 196 |
} |
| 197 |
|
| 198 |
static int notify_option_handler(const struct aco_option *opt, |
| 199 |
struct ast_variable *var, void *obj) |
| 200 |
{ |
| 201 |
struct notify_option *option = obj; |
| 202 |
|
| 203 |
int name_size = strlen(var->name) + 1; |
| 204 |
int value_size = strlen(var->value) + 1; |
| 205 |
|
| 206 |
RAII_VAR(struct notify_option_item *, item, |
| 207 |
ao2_alloc(sizeof(*item) + name_size + value_size, |
| 208 |
NULL), ao2_cleanup); |
| 209 |
|
| 210 |
item->name = item->buf; |
| 211 |
item->value = item->buf + name_size; |
| 212 |
|
| 213 |
ast_copy_string(item->buf, var->name, name_size); |
| 214 |
ast_copy_string(item->buf + name_size, var->value, value_size); |
| 215 |
|
| 216 |
if (!ao2_link(option->items, item)) { |
| 217 |
return -1; |
| 218 |
} |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
struct notify_cfg { |
| 224 |
struct ao2_container *notify_options; |
| 225 |
}; |
| 226 |
|
| 227 |
static void notify_cfg_destroy(void *obj) |
| 228 |
{ |
| 229 |
struct notify_cfg *cfg = obj; |
| 230 |
ao2_cleanup(cfg->notify_options); |
| 231 |
} |
| 232 |
|
| 233 |
static void *notify_cfg_alloc(void) |
| 234 |
{ |
| 235 |
struct notify_cfg *cfg; |
| 236 |
|
| 237 |
if (!(cfg = ao2_alloc(sizeof(*cfg), notify_cfg_destroy))) { |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
cfg->notify_options = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 242 |
20, notify_option_hash, NULL, notify_option_cmp); |
| 243 |
if (!cfg->notify_options) { |
| 244 |
ao2_cleanup(cfg); |
| 245 |
return NULL; |
| 246 |
} |
| 247 |
|
| 248 |
return cfg; |
| 249 |
} |
| 250 |
|
| 251 |
static struct aco_type notify_option = { |
| 252 |
.type = ACO_ITEM, |
| 253 |
.name = "notify", |
| 254 |
.category_match = ACO_BLACKLIST_EXACT, |
| 255 |
.category = "general", |
| 256 |
.item_offset = offsetof(struct notify_cfg, notify_options), |
| 257 |
.item_alloc = notify_option_alloc, |
| 258 |
.item_find = notify_option_find |
| 259 |
}; |
| 260 |
|
| 261 |
static struct aco_type *notify_options[] = ACO_TYPES(¬ify_option); |
| 262 |
|
| 263 |
static struct aco_file module_conf = { |
| 264 |
.filename = notify_config, |
| 265 |
.types = ACO_TYPES(¬ify_option), |
| 266 |
}; |
| 267 |
|
| 268 |
AO2_GLOBAL_OBJ_STATIC(globals); |
| 269 |
|
| 270 |
CONFIG_INFO_STANDARD(notify_cfg, globals, notify_cfg_alloc, |
| 271 |
.files = ACO_FILES(&module_conf) |
| 272 |
); |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
struct notify_data { |
| 279 |
|
| 280 |
struct ast_sip_endpoint *endpoint; |
| 281 |
|
| 282 |
void *info; |
| 283 |
|
| 284 |
void (*build_notify)(pjsip_tx_data *, void *); |
| 285 |
}; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
static void notify_cli_data_destroy(void *obj) |
| 292 |
{ |
| 293 |
struct notify_data *data = obj; |
| 294 |
|
| 295 |
ao2_cleanup(data->endpoint); |
| 296 |
ao2_cleanup(data->info); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
struct notify_uri_data { |
| 304 |
char *uri; |
| 305 |
void *info; |
| 306 |
void (*build_notify)(pjsip_tx_data *, void *); |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
struct notify_channel_data { |
| 314 |
struct ast_sip_session *session; |
| 315 |
void *info; |
| 316 |
void (*build_notify)(pjsip_tx_data *, void *); |
| 317 |
}; |
| 318 |
|
| 319 |
static void notify_cli_uri_data_destroy(void *obj) |
| 320 |
{ |
| 321 |
struct notify_uri_data *data = obj; |
| 322 |
|
| 323 |
ast_free(data->uri); |
| 324 |
ao2_cleanup(data->info); |
| 325 |
} |
| 326 |
|
| 327 |
static void notify_cli_channel_data_destroy(void *obj) |
| 328 |
{ |
| 329 |
struct notify_channel_data *data = obj; |
| 330 |
|
| 331 |
ao2_cleanup(data->info); |
| 332 |
ao2_cleanup(data->session); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
static void build_cli_notify(pjsip_tx_data *tdata, void *info); |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
static struct notify_data* notify_cli_data_create( |
| 346 |
struct ast_sip_endpoint *endpoint, void *info) |
| 347 |
{ |
| 348 |
struct notify_data *data = ao2_alloc(sizeof(*data), |
| 349 |
notify_cli_data_destroy); |
| 350 |
if (!data) { |
| 351 |
return NULL; |
| 352 |
} |
| 353 |
|
| 354 |
data->endpoint = endpoint; |
| 355 |
ao2_ref(data->endpoint, +1); |
| 356 |
|
| 357 |
data->info = info; |
| 358 |
ao2_ref(data->info, +1); |
| 359 |
|
| 360 |
data->build_notify = build_cli_notify; |
| 361 |
|
| 362 |
return data; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static struct notify_uri_data* notify_cli_uri_data_create( |
| 370 |
const char *uri, void *info) |
| 371 |
{ |
| 372 |
struct notify_uri_data *data = ao2_alloc(sizeof(*data), |
| 373 |
notify_cli_uri_data_destroy); |
| 374 |
|
| 375 |
if (!data) { |
| 376 |
return NULL; |
| 377 |
} |
| 378 |
|
| 379 |
data->uri = ast_strdup(uri); |
| 380 |
if (!data->uri) { |
| 381 |
ao2_ref(data, -1); |
| 382 |
return NULL; |
| 383 |
} |
| 384 |
|
| 385 |
data->info = info; |
| 386 |
ao2_ref(data->info, +1); |
| 387 |
|
| 388 |
data->build_notify = build_cli_notify; |
| 389 |
|
| 390 |
return data; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
static struct notify_channel_data* notify_cli_channel_data_create( |
| 398 |
struct ast_sip_session *session, void *info) |
| 399 |
{ |
| 400 |
struct notify_channel_data *data = ao2_alloc_options(sizeof(*data), |
| 401 |
notify_cli_channel_data_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 402 |
|
| 403 |
if (!data) { |
| 404 |
return NULL; |
| 405 |
} |
| 406 |
|
| 407 |
data->session = session; |
| 408 |
data->info = info; |
| 409 |
ao2_ref(data->info, +1); |
| 410 |
|
| 411 |
data->build_notify = build_cli_notify; |
| 412 |
|
| 413 |
return data; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
static void notify_ami_data_destroy(void *obj) |
| 421 |
{ |
| 422 |
struct notify_data *data = obj; |
| 423 |
struct ast_variable *info = data->info; |
| 424 |
|
| 425 |
ao2_cleanup(data->endpoint); |
| 426 |
ast_variables_destroy(info); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
static void notify_ami_uri_data_destroy(void *obj) |
| 434 |
{ |
| 435 |
struct notify_uri_data *data = obj; |
| 436 |
struct ast_variable *info = data->info; |
| 437 |
|
| 438 |
ast_free(data->uri); |
| 439 |
ast_variables_destroy(info); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
static void notify_ami_channel_data_destroy(void *obj) |
| 447 |
{ |
| 448 |
struct notify_channel_data *data = obj; |
| 449 |
struct ast_variable *info = data->info; |
| 450 |
|
| 451 |
ao2_cleanup(data->session); |
| 452 |
ast_variables_destroy(info); |
| 453 |
} |
| 454 |
|
| 455 |
static void build_ami_notify(pjsip_tx_data *tdata, void *info); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
static struct notify_data* notify_ami_data_create( |
| 462 |
struct ast_sip_endpoint *endpoint, void *info) |
| 463 |
{ |
| 464 |
struct notify_data *data = ao2_alloc(sizeof(*data), |
| 465 |
notify_ami_data_destroy); |
| 466 |
if (!data) { |
| 467 |
return NULL; |
| 468 |
} |
| 469 |
|
| 470 |
data->endpoint = endpoint; |
| 471 |
ao2_ref(data->endpoint, +1); |
| 472 |
|
| 473 |
data->info = info; |
| 474 |
data->build_notify = build_ami_notify; |
| 475 |
|
| 476 |
return data; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
static struct notify_uri_data* notify_ami_uri_data_create( |
| 484 |
const char *uri, void *info) |
| 485 |
{ |
| 486 |
struct notify_uri_data *data = ao2_alloc(sizeof(*data), |
| 487 |
notify_ami_uri_data_destroy); |
| 488 |
if (!data) { |
| 489 |
return NULL; |
| 490 |
} |
| 491 |
|
| 492 |
data->uri = ast_strdup(uri); |
| 493 |
if (!data->uri) { |
| 494 |
ao2_ref(data, -1); |
| 495 |
return NULL; |
| 496 |
} |
| 497 |
|
| 498 |
data->info = info; |
| 499 |
data->build_notify = build_ami_notify; |
| 500 |
|
| 501 |
return data; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
static struct notify_channel_data *notify_ami_channel_data_create( |
| 509 |
struct ast_sip_session *session, void *info) |
| 510 |
{ |
| 511 |
struct notify_channel_data *data; |
| 512 |
|
| 513 |
data = ao2_alloc_options(sizeof(*data), notify_ami_channel_data_destroy, |
| 514 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 515 |
if (!data) { |
| 516 |
return NULL; |
| 517 |
} |
| 518 |
|
| 519 |
data->session = session; |
| 520 |
data->info = info; |
| 521 |
data->build_notify = build_ami_notify; |
| 522 |
|
| 523 |
return data; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
static int not_allowed(const char *name) |
| 535 |
{ |
| 536 |
int i; |
| 537 |
static const char *names[] = { |
| 538 |
"Call-ID", |
| 539 |
"Contact", |
| 540 |
"CSeq", |
| 541 |
"To", |
| 542 |
"From", |
| 543 |
"Record-Route", |
| 544 |
"Route", |
| 545 |
"Request-URI", |
| 546 |
"Via", |
| 547 |
}; |
| 548 |
|
| 549 |
for (i = 0; i < ARRAY_LEN(names); ++i) { |
| 550 |
if (!strcasecmp(name, names[i])) { |
| 551 |
return 1; |
| 552 |
} |
| 553 |
} |
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
static int multiple_headers_allowed(const char *name) |
| 562 |
{ |
| 563 |
|
| 564 |
return strcasecmp("Event", name); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
static void build_notify_body(pjsip_tx_data *tdata, struct ast_str *content_type, |
| 573 |
struct ast_str *content) |
| 574 |
{ |
| 575 |
if (content_type) { |
| 576 |
char *p; |
| 577 |
struct ast_sip_body body; |
| 578 |
|
| 579 |
if (content) { |
| 580 |
body.body_text = ast_str_buffer(content); |
| 581 |
} |
| 582 |
|
| 583 |
body.type = ast_str_buffer(content_type); |
| 584 |
if ((p = strchr((char *)body.type, '/'))) { |
| 585 |
*p++ = '\0'; |
| 586 |
body.subtype = p; |
| 587 |
} |
| 588 |
ast_sip_add_body(tdata, &body); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
static void build_notify(pjsip_tx_data *tdata, const char *name, const char *value, |
| 597 |
struct ast_str **content_type, struct ast_str **content) |
| 598 |
{ |
| 599 |
if (not_allowed(name)) { |
| 600 |
ast_log(LOG_WARNING, "Cannot specify %s header, " |
| 601 |
"ignoring\n", name); |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
if (!strcasecmp(name, "Content-type")) { |
| 606 |
if (!(*content_type)) { |
| 607 |
*content_type = ast_str_create(CONTENT_TYPE_SIZE); |
| 608 |
} |
| 609 |
ast_str_set(content_type, 0,"%s", value); |
| 610 |
} else if (!strcasecmp(name, "Content")) { |
| 611 |
if (!(*content)) { |
| 612 |
*content = ast_str_create(CONTENT_SIZE); |
| 613 |
} |
| 614 |
|
| 615 |
if (ast_str_strlen(*content)) { |
| 616 |
ast_str_append(content, 0, "\r\n"); |
| 617 |
} |
| 618 |
ast_str_append(content, 0, "%s", value); |
| 619 |
} else { |
| 620 |
|
| 621 |
if (!multiple_headers_allowed(name)) { |
| 622 |
pj_str_t hdr_name; |
| 623 |
pj_cstr(&hdr_name, name); |
| 624 |
|
| 625 |
if (pjsip_msg_find_hdr_by_name(tdata->msg, &hdr_name, NULL)) { |
| 626 |
ast_log(LOG_ERROR, "Only one '%s' header can be added to a NOTIFY, " |
| 627 |
"ignoring \"%s: %s\"\n", name, name, value); |
| 628 |
return; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
ast_sip_add_header(tdata, name, value); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
static void build_cli_notify(pjsip_tx_data *tdata, void *info) |
| 642 |
{ |
| 643 |
struct notify_option *option = info; |
| 644 |
RAII_VAR(struct ast_str *, content_type, NULL, ast_free); |
| 645 |
RAII_VAR(struct ast_str *, content, NULL, ast_free); |
| 646 |
|
| 647 |
struct notify_option_item *item; |
| 648 |
struct ao2_iterator i = ao2_iterator_init(option->items, 0); |
| 649 |
|
| 650 |
while ((item = ao2_iterator_next(&i))) { |
| 651 |
build_notify(tdata, item->name, item->value, |
| 652 |
&content_type, &content); |
| 653 |
ao2_cleanup(item); |
| 654 |
} |
| 655 |
ao2_iterator_destroy(&i); |
| 656 |
|
| 657 |
build_notify_body(tdata, content_type, content); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
static void build_ami_notify(pjsip_tx_data *tdata, void *info) |
| 666 |
{ |
| 667 |
struct ast_variable *vars = info; |
| 668 |
RAII_VAR(struct ast_str *, content_type, NULL, ast_free); |
| 669 |
RAII_VAR(struct ast_str *, content, NULL, ast_free); |
| 670 |
struct ast_variable *i; |
| 671 |
|
| 672 |
for (i = vars; i; i = i->next) { |
| 673 |
if (!strcasecmp(i->name, "Content-Length")) { |
| 674 |
ast_log(LOG_NOTICE, "It is not necessary to specify Content-Length, ignoring.\n"); |
| 675 |
continue; |
| 676 |
} |
| 677 |
build_notify(tdata, i->name, i->value, |
| 678 |
&content_type, &content); |
| 679 |
} |
| 680 |
|
| 681 |
build_notify_body(tdata, content_type, content); |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
static int notify_contact(void *obj, void *arg, int flags) |
| 689 |
{ |
| 690 |
struct ast_sip_contact *contact = obj; |
| 691 |
struct notify_data *data = arg; |
| 692 |
pjsip_tx_data *tdata; |
| 693 |
|
| 694 |
if (ast_sip_create_request("NOTIFY", NULL, data->endpoint, |
| 695 |
NULL, contact, &tdata)) { |
| 696 |
ast_log(LOG_WARNING, "SIP NOTIFY - Unable to create request for " |
| 697 |
"contact %s\n", contact->uri); |
| 698 |
return -1; |
| 699 |
} |
| 700 |
|
| 701 |
ast_sip_add_header(tdata, "Subscription-State", "terminated"); |
| 702 |
data->build_notify(tdata, data->info); |
| 703 |
|
| 704 |
if (ast_sip_send_request(tdata, NULL, data->endpoint, NULL, NULL)) { |
| 705 |
ast_log(LOG_ERROR, "SIP NOTIFY - Unable to send request for " |
| 706 |
"contact %s\n", contact->uri); |
| 707 |
return -1; |
| 708 |
} |
| 709 |
|
| 710 |
return 0; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
static int notify_endpoint(void *obj) |
| 721 |
{ |
| 722 |
RAII_VAR(struct notify_data *, data, obj, ao2_cleanup); |
| 723 |
char *aor_name, *aors; |
| 724 |
|
| 725 |
if (ast_strlen_zero(data->endpoint->aors)) { |
| 726 |
ast_log(LOG_WARNING, "Unable to NOTIFY - " |
| 727 |
"endpoint has no configured AORs\n"); |
| 728 |
return -1; |
| 729 |
} |
| 730 |
|
| 731 |
aors = ast_strdupa(data->endpoint->aors); |
| 732 |
|
| 733 |
while ((aor_name = ast_strip(strsep(&aors, ",")))) { |
| 734 |
RAII_VAR(struct ast_sip_aor *, aor, |
| 735 |
ast_sip_location_retrieve_aor(aor_name), ao2_cleanup); |
| 736 |
RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup); |
| 737 |
|
| 738 |
if (!aor || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) { |
| 739 |
continue; |
| 740 |
} |
| 741 |
|
| 742 |
ao2_callback(contacts, OBJ_NODATA, notify_contact, data); |
| 743 |
} |
| 744 |
|
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
static int notify_uri(void *obj) |
| 753 |
{ |
| 754 |
RAII_VAR(struct notify_uri_data *, data, obj, ao2_cleanup); |
| 755 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, |
| 756 |
ast_sip_default_outbound_endpoint(), ao2_cleanup); |
| 757 |
pjsip_tx_data *tdata; |
| 758 |
|
| 759 |
if (!endpoint) { |
| 760 |
ast_log(LOG_WARNING, "No default outbound endpoint set, can not send " |
| 761 |
"NOTIFY requests to arbitrary URIs.\n"); |
| 762 |
return -1; |
| 763 |
} |
| 764 |
|
| 765 |
if (ast_strlen_zero(data->uri)) { |
| 766 |
ast_log(LOG_WARNING, "Unable to NOTIFY - URI is blank.\n"); |
| 767 |
return -1; |
| 768 |
} |
| 769 |
|
| 770 |
if (ast_sip_create_request("NOTIFY", NULL, endpoint, |
| 771 |
data->uri, NULL, &tdata)) { |
| 772 |
ast_log(LOG_WARNING, "SIP NOTIFY - Unable to create request for " |
| 773 |
"uri %s\n", data->uri); |
| 774 |
return -1; |
| 775 |
} |
| 776 |
|
| 777 |
ast_sip_add_header(tdata, "Subscription-State", "terminated"); |
| 778 |
|
| 779 |
data->build_notify(tdata, data->info); |
| 780 |
|
| 781 |
if (ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL)) { |
| 782 |
ast_log(LOG_ERROR, "SIP NOTIFY - Unable to send request for " |
| 783 |
"uri %s\n", data->uri); |
| 784 |
return -1; |
| 785 |
} |
| 786 |
|
| 787 |
return 0; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
static int notify_channel(void *obj) |
| 795 |
{ |
| 796 |
RAII_VAR(struct notify_channel_data *, data, obj, ao2_cleanup); |
| 797 |
pjsip_tx_data *tdata; |
| 798 |
struct pjsip_dialog *dlg; |
| 799 |
|
| 800 |
if (!data->session->channel |
| 801 |
|| !data->session->inv_session |
| 802 |
|| data->session->inv_session->state < PJSIP_INV_STATE_EARLY |
| 803 |
|| data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) { |
| 804 |
return -1; |
| 805 |
} |
| 806 |
|
| 807 |
ast_debug(1, "Sending notify on channel %s\n", ast_channel_name(data->session->channel)); |
| 808 |
|
| 809 |
dlg = data->session->inv_session->dlg; |
| 810 |
|
| 811 |
if (ast_sip_create_request("NOTIFY", dlg, NULL, NULL, NULL, &tdata)) { |
| 812 |
return -1; |
| 813 |
} |
| 814 |
|
| 815 |
ast_sip_add_header(tdata, "Subscription-State", "terminated"); |
| 816 |
data->build_notify(tdata, data->info); |
| 817 |
|
| 818 |
if (ast_sip_send_request(tdata, dlg, NULL, NULL, NULL)) { |
| 819 |
return -1; |
| 820 |
} |
| 821 |
|
| 822 |
return 0; |
| 823 |
} |
| 824 |
|
| 825 |
enum notify_result { |
| 826 |
SUCCESS, |
| 827 |
INVALID_ENDPOINT, |
| 828 |
INVALID_CHANNEL, |
| 829 |
ALLOC_ERROR, |
| 830 |
TASK_PUSH_ERROR |
| 831 |
}; |
| 832 |
|
| 833 |
typedef struct notify_data *(*task_data_create)( |
| 834 |
struct ast_sip_endpoint *, void *info); |
| 835 |
|
| 836 |
typedef struct notify_uri_data *(*task_uri_data_create)( |
| 837 |
const char *uri, void *info); |
| 838 |
|
| 839 |
typedef struct notify_channel_data *(*task_channel_data_create)( |
| 840 |
struct ast_sip_session *session, void *info); |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
static enum notify_result push_notify(const char *endpoint_name, void *info, |
| 847 |
task_data_create data_create) |
| 848 |
{ |
| 849 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 850 |
struct notify_data *data; |
| 851 |
|
| 852 |
if (!(endpoint = ast_sorcery_retrieve_by_id( |
| 853 |
ast_sip_get_sorcery(), "endpoint", endpoint_name))) { |
| 854 |
return INVALID_ENDPOINT; |
| 855 |
} |
| 856 |
|
| 857 |
if (!(data = data_create(endpoint, info))) { |
| 858 |
return ALLOC_ERROR; |
| 859 |
} |
| 860 |
|
| 861 |
if (ast_sip_push_task(NULL, notify_endpoint, data)) { |
| 862 |
ao2_cleanup(data); |
| 863 |
return TASK_PUSH_ERROR; |
| 864 |
} |
| 865 |
|
| 866 |
return SUCCESS; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
static enum notify_result push_notify_uri(const char *uri, void *info, |
| 874 |
task_uri_data_create data_create) |
| 875 |
{ |
| 876 |
struct notify_uri_data *data; |
| 877 |
|
| 878 |
if (!(data = data_create(uri, info))) { |
| 879 |
return ALLOC_ERROR; |
| 880 |
} |
| 881 |
|
| 882 |
if (ast_sip_push_task(NULL, notify_uri, data)) { |
| 883 |
ao2_cleanup(data); |
| 884 |
return TASK_PUSH_ERROR; |
| 885 |
} |
| 886 |
|
| 887 |
return SUCCESS; |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
static enum notify_result push_notify_channel(const char *channel_name, void *info, |
| 895 |
task_channel_data_create data_create) |
| 896 |
{ |
| 897 |
struct notify_channel_data *data; |
| 898 |
struct ast_channel *ch; |
| 899 |
struct ast_sip_session *session; |
| 900 |
struct ast_sip_channel_pvt *ch_pvt; |
| 901 |
|
| 902 |
|
| 903 |
ch = ast_channel_get_by_name(channel_name); |
| 904 |
if (!ch) { |
| 905 |
ast_debug(1, "No channel found with name %s", channel_name); |
| 906 |
return INVALID_CHANNEL; |
| 907 |
} |
| 908 |
|
| 909 |
if (strcmp(ast_channel_tech(ch)->type, "PJSIP")) { |
| 910 |
ast_log(LOG_WARNING, "Channel was a non-PJSIP channel: %s\n", channel_name); |
| 911 |
ast_channel_unref(ch); |
| 912 |
return INVALID_CHANNEL; |
| 913 |
} |
| 914 |
|
| 915 |
ast_channel_lock(ch); |
| 916 |
ch_pvt = ast_channel_tech_pvt(ch); |
| 917 |
session = ch_pvt->session; |
| 918 |
|
| 919 |
if (!session || !session->inv_session |
| 920 |
|| session->inv_session->state < PJSIP_INV_STATE_EARLY |
| 921 |
|| session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) { |
| 922 |
ast_debug(1, "No active session for channel %s\n", channel_name); |
| 923 |
ast_channel_unlock(ch); |
| 924 |
ast_channel_unref(ch); |
| 925 |
return INVALID_CHANNEL; |
| 926 |
} |
| 927 |
|
| 928 |
ao2_ref(session, +1); |
| 929 |
ast_channel_unlock(ch); |
| 930 |
|
| 931 |
|
| 932 |
ast_channel_unref(ch); |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
data = data_create(session, info); |
| 939 |
if (!data) { |
| 940 |
ao2_ref(session, -1); |
| 941 |
return ALLOC_ERROR; |
| 942 |
} |
| 943 |
|
| 944 |
if (ast_sip_push_task(session->serializer, notify_channel, data)) { |
| 945 |
ao2_ref(data, -1); |
| 946 |
return TASK_PUSH_ERROR; |
| 947 |
} |
| 948 |
|
| 949 |
return SUCCESS; |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
static char *cli_complete_endpoint(const char *word) |
| 957 |
{ |
| 958 |
int wordlen = strlen(word); |
| 959 |
struct ao2_container * endpoints; |
| 960 |
struct ast_sip_endpoint *endpoint; |
| 961 |
struct ao2_iterator i; |
| 962 |
|
| 963 |
endpoints = ast_sorcery_retrieve_by_prefix(ast_sip_get_sorcery(), |
| 964 |
"endpoint", word, wordlen); |
| 965 |
if (endpoints == NULL) { |
| 966 |
return NULL; |
| 967 |
} |
| 968 |
|
| 969 |
i = ao2_iterator_init(endpoints, 0); |
| 970 |
while ((endpoint = ao2_iterator_next(&i))) { |
| 971 |
ast_cli_completion_add( |
| 972 |
ast_strdup(ast_sorcery_object_get_id(endpoint))); |
| 973 |
ao2_cleanup(endpoint); |
| 974 |
} |
| 975 |
ao2_iterator_destroy(&i); |
| 976 |
|
| 977 |
ao2_ref(endpoints, -1); |
| 978 |
|
| 979 |
return NULL; |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
static char *cli_complete_notify(struct ast_cli_args *a) |
| 987 |
{ |
| 988 |
char *c = NULL; |
| 989 |
|
| 990 |
if (a->pos == 3) { |
| 991 |
int which = 0; |
| 992 |
int wordlen = strlen(a->word); |
| 993 |
|
| 994 |
RAII_VAR(struct notify_cfg *, cfg, |
| 995 |
ao2_global_obj_ref(globals), ao2_cleanup); |
| 996 |
struct notify_option *option; |
| 997 |
|
| 998 |
|
| 999 |
struct ao2_iterator i = ao2_iterator_init(cfg->notify_options, 0); |
| 1000 |
while ((option = ao2_iterator_next(&i))) { |
| 1001 |
if (!strncasecmp(a->word, option->name, wordlen) && ++which > a->n) { |
| 1002 |
c = ast_strdup(option->name); |
| 1003 |
} |
| 1004 |
|
| 1005 |
ao2_cleanup(option); |
| 1006 |
if (c) { |
| 1007 |
break; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
ao2_iterator_destroy(&i); |
| 1011 |
return c; |
| 1012 |
} |
| 1013 |
|
| 1014 |
if (a->pos == 4) { |
| 1015 |
int wordlen = strlen(a->word); |
| 1016 |
|
| 1017 |
if (ast_strlen_zero(a->word)) { |
| 1018 |
if (a->n == 0) { |
| 1019 |
c = ast_strdup("endpoint"); |
| 1020 |
} else if (a->n == 1) { |
| 1021 |
c = ast_strdup("uri"); |
| 1022 |
} else if (a->n == 2) { |
| 1023 |
c = ast_strdup("channel"); |
| 1024 |
} |
| 1025 |
} else if (a->n == 0) { |
| 1026 |
if (!strncasecmp(a->word, "endpoint", wordlen)) { |
| 1027 |
c = ast_strdup("endpoint"); |
| 1028 |
} else if (!strncasecmp(a->word, "uri", wordlen)) { |
| 1029 |
c = ast_strdup("uri"); |
| 1030 |
} else if (!strncasecmp(a->word, "channel", wordlen)) { |
| 1031 |
c = ast_strdup("channel"); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
return c; |
| 1036 |
} |
| 1037 |
|
| 1038 |
if (a->pos > 4) { |
| 1039 |
if (!strcasecmp(a->argv[4], "endpoint")) { |
| 1040 |
return cli_complete_endpoint(a->word); |
| 1041 |
} else if (!strcasecmp(a->argv[4], "channel")) { |
| 1042 |
return ast_complete_channels(a->line, a->word, a->pos, a->n, 5); |
| 1043 |
} |
| 1044 |
} |
| 1045 |
return NULL; |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
static char *cli_notify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1057 |
{ |
| 1058 |
RAII_VAR(struct notify_cfg *, cfg, NULL, ao2_cleanup); |
| 1059 |
RAII_VAR(struct notify_option *, option, NULL, ao2_cleanup); |
| 1060 |
|
| 1061 |
int i; |
| 1062 |
int using_uri = 0; |
| 1063 |
int using_channel = 0; |
| 1064 |
|
| 1065 |
switch (cmd) { |
| 1066 |
case CLI_INIT: |
| 1067 |
e->command = "pjsip send notify"; |
| 1068 |
e->usage = |
| 1069 |
"Usage: pjsip send notify <type> {endpoint|uri|channel} <peer> [<peer>...]\n" |
| 1070 |
" Send a NOTIFY request to an endpoint\n" |
| 1071 |
" Message types are defined in pjsip_notify.conf\n"; |
| 1072 |
return NULL; |
| 1073 |
case CLI_GENERATE: |
| 1074 |
return cli_complete_notify(a); |
| 1075 |
} |
| 1076 |
|
| 1077 |
if (a->argc < 6) { |
| 1078 |
return CLI_SHOWUSAGE; |
| 1079 |
} |
| 1080 |
|
| 1081 |
if (!strcasecmp(a->argv[4], "uri")) { |
| 1082 |
using_uri = 1; |
| 1083 |
} else if (!strcasecmp(a->argv[4], "channel")) { |
| 1084 |
using_channel = 1; |
| 1085 |
} else if (strcasecmp(a->argv[4], "endpoint")) { |
| 1086 |
return CLI_SHOWUSAGE; |
| 1087 |
} |
| 1088 |
|
| 1089 |
cfg = ao2_global_obj_ref(globals); |
| 1090 |
|
| 1091 |
if (!(option = notify_option_find(cfg->notify_options, a->argv[3]))) |
| 1092 |
{ |
| 1093 |
ast_cli(a->fd, "Unable to find notify type '%s'\n", |
| 1094 |
a->argv[3]); |
| 1095 |
return CLI_FAILURE; |
| 1096 |
} |
| 1097 |
|
| 1098 |
for (i = 5; i < a->argc; ++i) { |
| 1099 |
enum notify_result result; |
| 1100 |
ast_cli(a->fd, "Sending NOTIFY of type '%s' to '%s'\n", |
| 1101 |
a->argv[3], a->argv[i]); |
| 1102 |
|
| 1103 |
if (using_uri) { |
| 1104 |
result = push_notify_uri(a->argv[i], option, notify_cli_uri_data_create); |
| 1105 |
} else if (using_channel) { |
| 1106 |
result = push_notify_channel(a->argv[i], option, notify_cli_channel_data_create); |
| 1107 |
} else { |
| 1108 |
result = push_notify(a->argv[i], option, notify_cli_data_create); |
| 1109 |
} |
| 1110 |
switch(result) { |
| 1111 |
case INVALID_ENDPOINT: |
| 1112 |
ast_cli(a->fd, "Unable to retrieve endpoint %s\n", a->argv[i]); |
| 1113 |
break; |
| 1114 |
case INVALID_CHANNEL: |
| 1115 |
ast_cli(a->fd, "Unable to find channel %s\n", a->argv[i]); |
| 1116 |
break; |
| 1117 |
case ALLOC_ERROR: |
| 1118 |
ast_cli(a->fd, "Unable to allocate NOTIFY task data\n"); |
| 1119 |
return CLI_FAILURE; |
| 1120 |
case TASK_PUSH_ERROR: |
| 1121 |
ast_cli(a->fd, "Unable to push NOTIFY task\n"); |
| 1122 |
return CLI_FAILURE; |
| 1123 |
default: |
| 1124 |
break; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
return CLI_SUCCESS; |
| 1129 |
} |
| 1130 |
|
| 1131 |
static struct ast_cli_entry cli_options[] = { |
| 1132 |
AST_CLI_DEFINE(cli_notify, "Send a NOTIFY request to a SIP endpoint") |
| 1133 |
}; |
| 1134 |
|
| 1135 |
enum notify_type { |
| 1136 |
NOTIFY_ENDPOINT, |
| 1137 |
NOTIFY_URI, |
| 1138 |
NOTIFY_CHANNEL, |
| 1139 |
}; |
| 1140 |
|
| 1141 |
static void manager_send_response(struct mansession *s, const struct message *m, enum notify_type type, enum notify_result res, struct ast_variable *vars, const char *endpoint_name) |
| 1142 |
{ |
| 1143 |
switch (res) { |
| 1144 |
case INVALID_CHANNEL: |
| 1145 |
if (type == NOTIFY_CHANNEL) { |
| 1146 |
ast_variables_destroy(vars); |
| 1147 |
astman_send_error(s, m, "Channel not found"); |
| 1148 |
} else { |
| 1149 |
|
| 1150 |
ast_assert(0); |
| 1151 |
} |
| 1152 |
break; |
| 1153 |
case INVALID_ENDPOINT: |
| 1154 |
if (type == NOTIFY_ENDPOINT) { |
| 1155 |
ast_variables_destroy(vars); |
| 1156 |
astman_send_error_va(s, m, "Unable to retrieve endpoint %s", endpoint_name); |
| 1157 |
} else { |
| 1158 |
|
| 1159 |
ast_assert(0); |
| 1160 |
} |
| 1161 |
break; |
| 1162 |
case ALLOC_ERROR: |
| 1163 |
ast_variables_destroy(vars); |
| 1164 |
astman_send_error(s, m, "Unable to allocate NOTIFY task data"); |
| 1165 |
break; |
| 1166 |
case TASK_PUSH_ERROR: |
| 1167 |
|
| 1168 |
astman_send_error(s, m, "Unable to push Notify task"); |
| 1169 |
break; |
| 1170 |
case SUCCESS: |
| 1171 |
astman_send_ack(s, m, "NOTIFY sent"); |
| 1172 |
break; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
static void manager_notify_endpoint(struct mansession *s, |
| 1181 |
const struct message *m, const char *endpoint_name) |
| 1182 |
{ |
| 1183 |
RAII_VAR(struct notify_cfg *, cfg, NULL, ao2_cleanup); |
| 1184 |
RAII_VAR(struct notify_option *, option, NULL, ao2_cleanup); |
| 1185 |
struct ast_variable *vars = NULL; |
| 1186 |
enum notify_result res; |
| 1187 |
const char *option_name = astman_get_header(m, "Option"); |
| 1188 |
|
| 1189 |
if (!ast_strlen_zero(option_name) && (cfg = ao2_global_obj_ref(globals)) && !(option = notify_option_find(cfg->notify_options, option_name))) { |
| 1190 |
astman_send_error_va(s, m, "Unable to find notify type '%s'\n", option_name); |
| 1191 |
return; |
| 1192 |
} |
| 1193 |
if (!option) { |
| 1194 |
vars = astman_get_variables_order(m, ORDER_NATURAL); |
| 1195 |
} |
| 1196 |
|
| 1197 |
if (!strncasecmp(endpoint_name, "sip/", 4)) { |
| 1198 |
endpoint_name += 4; |
| 1199 |
} |
| 1200 |
|
| 1201 |
if (!strncasecmp(endpoint_name, "pjsip/", 6)) { |
| 1202 |
endpoint_name += 6; |
| 1203 |
} |
| 1204 |
|
| 1205 |
if (option) { |
| 1206 |
res = push_notify(endpoint_name, option, notify_cli_data_create); |
| 1207 |
} else { |
| 1208 |
res = push_notify(endpoint_name, vars, notify_ami_data_create); |
| 1209 |
} |
| 1210 |
|
| 1211 |
manager_send_response(s, m, NOTIFY_ENDPOINT, res, vars, endpoint_name); |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
static void manager_notify_uri(struct mansession *s, |
| 1219 |
const struct message *m, const char *uri) |
| 1220 |
{ |
| 1221 |
RAII_VAR(struct notify_cfg *, cfg, NULL, ao2_cleanup); |
| 1222 |
RAII_VAR(struct notify_option *, option, NULL, ao2_cleanup); |
| 1223 |
enum notify_result res; |
| 1224 |
const char *option_name = astman_get_header(m, "Option"); |
| 1225 |
struct ast_variable *vars = NULL; |
| 1226 |
|
| 1227 |
if (!ast_strlen_zero(option_name) && (cfg = ao2_global_obj_ref(globals)) && !(option = notify_option_find(cfg->notify_options, option_name))) { |
| 1228 |
astman_send_error_va(s, m, "Unable to find notify type '%s'\n", option_name); |
| 1229 |
return; |
| 1230 |
} |
| 1231 |
if (!option) { |
| 1232 |
vars = astman_get_variables_order(m, ORDER_NATURAL); |
| 1233 |
} |
| 1234 |
|
| 1235 |
if (option) { |
| 1236 |
res = push_notify_uri(uri, option, notify_cli_uri_data_create); |
| 1237 |
} else { |
| 1238 |
res = push_notify_uri(uri, vars, notify_ami_uri_data_create); |
| 1239 |
} |
| 1240 |
|
| 1241 |
manager_send_response(s, m, NOTIFY_URI, res, vars, NULL); |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
static void manager_notify_channel(struct mansession *s, |
| 1249 |
const struct message *m, const char *channel) |
| 1250 |
{ |
| 1251 |
enum notify_result res; |
| 1252 |
struct ast_variable *vars = NULL; |
| 1253 |
|
| 1254 |
vars = astman_get_variables_order(m, ORDER_NATURAL); |
| 1255 |
res = push_notify_channel(channel, vars, notify_ami_channel_data_create); |
| 1256 |
|
| 1257 |
manager_send_response(s, m, NOTIFY_CHANNEL, res, vars, NULL); |
| 1258 |
} |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
static int manager_notify(struct mansession *s, const struct message *m) |
| 1265 |
{ |
| 1266 |
const char *endpoint_name = astman_get_header(m, "Endpoint"); |
| 1267 |
const char *uri = astman_get_header(m, "URI"); |
| 1268 |
const char *channel = astman_get_header(m, "Channel"); |
| 1269 |
const char *variables = astman_get_header(m, "Variable"); |
| 1270 |
const char *option = astman_get_header(m, "Option"); |
| 1271 |
int count = 0; |
| 1272 |
|
| 1273 |
if (!ast_strlen_zero(endpoint_name)) { |
| 1274 |
++count; |
| 1275 |
} |
| 1276 |
if (!ast_strlen_zero(uri)) { |
| 1277 |
++count; |
| 1278 |
} |
| 1279 |
if (!ast_strlen_zero(channel)) { |
| 1280 |
++count; |
| 1281 |
} |
| 1282 |
|
| 1283 |
if ((!ast_strlen_zero(option) && !ast_strlen_zero(variables)) || (ast_strlen_zero(option) && ast_strlen_zero(variables))) { |
| 1284 |
astman_send_error(s, m, |
| 1285 |
"PJSIPNotify requires either an Option or Variable(s)." |
| 1286 |
"You must use only one of them."); |
| 1287 |
} else if (1 < count) { |
| 1288 |
astman_send_error(s, m, |
| 1289 |
"PJSIPNotify requires either an endpoint name, a SIP URI, or a channel. " |
| 1290 |
"You must use only one of them."); |
| 1291 |
} else if (!ast_strlen_zero(endpoint_name)) { |
| 1292 |
manager_notify_endpoint(s, m, endpoint_name); |
| 1293 |
} else if (!ast_strlen_zero(uri)) { |
| 1294 |
manager_notify_uri(s, m, uri); |
| 1295 |
} else if (!ast_strlen_zero(channel)) { |
| 1296 |
manager_notify_channel(s, m, channel); |
| 1297 |
} else { |
| 1298 |
astman_send_error(s, m, |
| 1299 |
"PJSIPNotify requires either an endpoint name, a SIP URI, or a channel."); |
| 1300 |
} |
| 1301 |
|
| 1302 |
return 0; |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
static struct ast_variable *headers_to_variables(const char *headers) |
| 1308 |
{ |
| 1309 |
struct ast_variable *varlist = NULL; |
| 1310 |
struct ast_variable *var; |
| 1311 |
char *cur; |
| 1312 |
char *header; |
| 1313 |
|
| 1314 |
cur = (char *)headers; |
| 1315 |
|
| 1316 |
while( (header = strsep(&cur, "&")) ) { |
| 1317 |
char *name; |
| 1318 |
char *value; |
| 1319 |
|
| 1320 |
name = value = header; |
| 1321 |
strsep(&value, "="); |
| 1322 |
|
| 1323 |
if (!value || ast_strlen_zero(name)) { |
| 1324 |
continue; |
| 1325 |
} |
| 1326 |
|
| 1327 |
var = ast_variable_new(name, value, ""); |
| 1328 |
var->next = varlist; |
| 1329 |
varlist = var; |
| 1330 |
} |
| 1331 |
return varlist; |
| 1332 |
} |
| 1333 |
|
| 1334 |
|
| 1335 |
static int app_notify(struct ast_channel *chan, const char *data) |
| 1336 |
{ |
| 1337 |
RAII_VAR(struct notify_cfg *, cfg, NULL, ao2_cleanup); |
| 1338 |
RAII_VAR(struct notify_option *, option, NULL, ao2_cleanup); |
| 1339 |
|
| 1340 |
struct ast_variable *varlist = NULL; |
| 1341 |
char *tmp; |
| 1342 |
int res; |
| 1343 |
AST_DECLARE_APP_ARGS(args, |
| 1344 |
AST_APP_ARG(to); |
| 1345 |
AST_APP_ARG(headers); |
| 1346 |
); |
| 1347 |
|
| 1348 |
|
| 1349 |
if (ast_strlen_zero(data)) { |
| 1350 |
ast_log(LOG_WARNING, "PJSIPNotify requires arguments (to, &header=...)\n"); |
| 1351 |
return -1; |
| 1352 |
} |
| 1353 |
|
| 1354 |
tmp = ast_strdupa(data); |
| 1355 |
AST_STANDARD_APP_ARGS(args, tmp); |
| 1356 |
cfg = ao2_global_obj_ref(globals); |
| 1357 |
|
| 1358 |
if (!(option = notify_option_find(cfg->notify_options, args.headers))) { |
| 1359 |
|
| 1360 |
|
| 1361 |
varlist = headers_to_variables(args.headers); |
| 1362 |
if (ast_strlen_zero(args.to)) { |
| 1363 |
res = push_notify_channel(ast_channel_name(chan), varlist, notify_ami_channel_data_create); |
| 1364 |
} else { |
| 1365 |
res = push_notify_uri(args.to, varlist, notify_ami_uri_data_create); |
| 1366 |
} |
| 1367 |
} else { |
| 1368 |
|
| 1369 |
|
| 1370 |
if (ast_strlen_zero(args.to)) { |
| 1371 |
res = push_notify_channel(ast_channel_name(chan), option, notify_cli_channel_data_create); |
| 1372 |
} else { |
| 1373 |
res = push_notify_uri(args.to, option, notify_cli_uri_data_create); |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
switch (res) { |
| 1378 |
case INVALID_CHANNEL: |
| 1379 |
case INVALID_ENDPOINT: |
| 1380 |
case ALLOC_ERROR: |
| 1381 |
res = -1; |
| 1382 |
ast_variables_destroy(varlist); |
| 1383 |
break; |
| 1384 |
case TASK_PUSH_ERROR: |
| 1385 |
|
| 1386 |
res = -1; |
| 1387 |
break; |
| 1388 |
case SUCCESS: |
| 1389 |
res = 0; |
| 1390 |
break; |
| 1391 |
} |
| 1392 |
|
| 1393 |
return res; |
| 1394 |
} |
| 1395 |
|
| 1396 |
static int load_module(void) |
| 1397 |
{ |
| 1398 |
if (aco_info_init(¬ify_cfg)) { |
| 1399 |
return AST_MODULE_LOAD_DECLINE; |
| 1400 |
} |
| 1401 |
|
| 1402 |
aco_option_register_custom(¬ify_cfg, "", ACO_PREFIX, notify_options, |
| 1403 |
"", notify_option_handler, 0); |
| 1404 |
|
| 1405 |
if (aco_process_config(¬ify_cfg, 0)) { |
| 1406 |
aco_info_destroy(¬ify_cfg); |
| 1407 |
return AST_MODULE_LOAD_DECLINE; |
| 1408 |
} |
| 1409 |
|
| 1410 |
ast_cli_register_multiple(cli_options, ARRAY_LEN(cli_options)); |
| 1411 |
ast_manager_register_xml("PJSIPNotify", EVENT_FLAG_SYSTEM, manager_notify); |
| 1412 |
ast_register_application_xml("PJSIPNotify", app_notify); |
| 1413 |
|
| 1414 |
return AST_MODULE_LOAD_SUCCESS; |
| 1415 |
} |
| 1416 |
|
| 1417 |
static int reload_module(void) |
| 1418 |
{ |
| 1419 |
if (aco_process_config(¬ify_cfg, 1) == ACO_PROCESS_ERROR) { |
| 1420 |
return AST_MODULE_LOAD_DECLINE; |
| 1421 |
} |
| 1422 |
|
| 1423 |
return 0; |
| 1424 |
} |
| 1425 |
|
| 1426 |
static int unload_module(void) |
| 1427 |
{ |
| 1428 |
ast_manager_unregister("PJSIPNotify"); |
| 1429 |
ast_unregister_application("PJSIPNotify"); |
| 1430 |
ast_cli_unregister_multiple(cli_options, ARRAY_LEN(cli_options)); |
| 1431 |
aco_info_destroy(¬ify_cfg); |
| 1432 |
ao2_global_obj_release(globals); |
| 1433 |
|
| 1434 |
return 0; |
| 1435 |
} |
| 1436 |
|
| 1437 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "CLI/AMI PJSIP NOTIFY Support", |
| 1438 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1439 |
.load = load_module, |
| 1440 |
.reload = reload_module, |
| 1441 |
.unload = unload_module, |
| 1442 |
.load_pri = AST_MODPRI_APP_DEPEND, |
| 1443 |
.requires = "res_pjsip", |
| 1444 |
); |