| 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 |
#include "asterisk.h" |
| 29 |
|
| 30 |
#include <pjsip.h> |
| 31 |
#include <pjsip_simple.h> |
| 32 |
#include <pjlib.h> |
| 33 |
|
| 34 |
#include "asterisk/mwi.h" |
| 35 |
#include "asterisk/res_pjsip_pubsub.h" |
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/linkedlists.h" |
| 38 |
#include "asterisk/astobj2.h" |
| 39 |
#include "asterisk/datastore.h" |
| 40 |
#include "asterisk/uuid.h" |
| 41 |
#include "asterisk/taskprocessor.h" |
| 42 |
#include "asterisk/sched.h" |
| 43 |
#include "asterisk/res_pjsip.h" |
| 44 |
#include "asterisk/callerid.h" |
| 45 |
#include "asterisk/manager.h" |
| 46 |
#include "asterisk/cli.h" |
| 47 |
#include "asterisk/test.h" |
| 48 |
#include "res_pjsip/include/res_pjsip_private.h" |
| 49 |
#include "asterisk/res_pjsip_presence_xml.h" |
| 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 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
static pj_bool_t pubsub_on_rx_request(pjsip_rx_data *rdata); |
| 444 |
|
| 445 |
static struct pjsip_module pubsub_module = { |
| 446 |
.name = { "PubSub Module", 13 }, |
| 447 |
.priority = PJSIP_MOD_PRIORITY_APPLICATION, |
| 448 |
.on_rx_request = pubsub_on_rx_request, |
| 449 |
}; |
| 450 |
|
| 451 |
#define MOD_DATA_PERSISTENCE "sub_persistence" |
| 452 |
#define MOD_DATA_MSG "sub_msg" |
| 453 |
|
| 454 |
static const pj_str_t str_event_name = { "Event", 5 }; |
| 455 |
|
| 456 |
|
| 457 |
static struct ast_sched_context *sched; |
| 458 |
|
| 459 |
|
| 460 |
#define PUBLICATIONS_BUCKETS 37 |
| 461 |
|
| 462 |
|
| 463 |
#define DEFAULT_PUBLISH_EXPIRES 3600 |
| 464 |
|
| 465 |
|
| 466 |
#define DATASTORE_BUCKETS 53 |
| 467 |
|
| 468 |
|
| 469 |
#define DEFAULT_EXPIRES 3600 |
| 470 |
|
| 471 |
|
| 472 |
const pjsip_method pjsip_publish_method = |
| 473 |
{ |
| 474 |
PJSIP_OTHER_METHOD, |
| 475 |
{ "PUBLISH", 7 } |
| 476 |
}; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
enum sip_publish_type { |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
SIP_PUBLISH_UNKNOWN, |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
SIP_PUBLISH_INITIAL, |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
SIP_PUBLISH_REFRESH, |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
SIP_PUBLISH_MODIFY, |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
SIP_PUBLISH_REMOVE, |
| 531 |
}; |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
AST_VECTOR(resources, const char *); |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
struct resource_list { |
| 542 |
SORCERY_OBJECT(details); |
| 543 |
|
| 544 |
char event[32]; |
| 545 |
|
| 546 |
struct resources items; |
| 547 |
|
| 548 |
unsigned int full_state; |
| 549 |
|
| 550 |
unsigned int notification_batch_interval; |
| 551 |
|
| 552 |
unsigned int resource_display_name; |
| 553 |
}; |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
static int esc_etag_counter; |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
struct ast_sip_publication { |
| 564 |
|
| 565 |
struct ao2_container *datastores; |
| 566 |
|
| 567 |
int entity_tag; |
| 568 |
|
| 569 |
struct ast_sip_publish_handler *handler; |
| 570 |
|
| 571 |
struct ast_sip_endpoint *endpoint; |
| 572 |
|
| 573 |
unsigned int expires; |
| 574 |
|
| 575 |
int sched_id; |
| 576 |
|
| 577 |
char *resource; |
| 578 |
|
| 579 |
char *event_configuration_name; |
| 580 |
|
| 581 |
char data[0]; |
| 582 |
}; |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
struct subscription_persistence { |
| 589 |
|
| 590 |
SORCERY_OBJECT(details); |
| 591 |
|
| 592 |
char *endpoint; |
| 593 |
|
| 594 |
char packet[PJSIP_MAX_PKT_LEN]; |
| 595 |
|
| 596 |
char src_name[PJ_INET6_ADDRSTRLEN]; |
| 597 |
|
| 598 |
int src_port; |
| 599 |
|
| 600 |
char transport_type[32]; |
| 601 |
|
| 602 |
char local_name[PJ_INET6_ADDRSTRLEN]; |
| 603 |
|
| 604 |
int local_port; |
| 605 |
|
| 606 |
unsigned int cseq; |
| 607 |
|
| 608 |
char *tag; |
| 609 |
|
| 610 |
struct timeval expires; |
| 611 |
|
| 612 |
char contact_uri[PJSIP_MAX_URL_SIZE]; |
| 613 |
|
| 614 |
int prune_on_boot; |
| 615 |
|
| 616 |
struct ast_json *generator_data; |
| 617 |
}; |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
enum sip_subscription_tree_state { |
| 623 |
|
| 624 |
SIP_SUB_TREE_NORMAL = 0, |
| 625 |
|
| 626 |
SIP_SUB_TREE_TERMINATE_PENDING, |
| 627 |
|
| 628 |
SIP_SUB_TREE_TERMINATE_IN_PROGRESS, |
| 629 |
|
| 630 |
SIP_SUB_TREE_TERMINATED, |
| 631 |
}; |
| 632 |
|
| 633 |
static char *sub_tree_state_description[] = { |
| 634 |
"Normal", |
| 635 |
"TerminatePending", |
| 636 |
"TerminateInProgress", |
| 637 |
"Terminated" |
| 638 |
}; |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
struct sip_subscription_tree { |
| 650 |
|
| 651 |
struct ast_sip_endpoint *endpoint; |
| 652 |
|
| 653 |
struct ast_taskprocessor *serializer; |
| 654 |
|
| 655 |
enum ast_sip_subscription_role role; |
| 656 |
|
| 657 |
struct subscription_persistence *persistence; |
| 658 |
|
| 659 |
pjsip_evsub *evsub; |
| 660 |
|
| 661 |
pjsip_dialog *dlg; |
| 662 |
|
| 663 |
unsigned int notification_batch_interval; |
| 664 |
|
| 665 |
int notify_sched_id; |
| 666 |
|
| 667 |
unsigned int send_scheduled_notify; |
| 668 |
|
| 669 |
struct ast_sip_subscription *root; |
| 670 |
|
| 671 |
int is_list; |
| 672 |
|
| 673 |
AST_LIST_ENTRY(sip_subscription_tree) next; |
| 674 |
|
| 675 |
enum sip_subscription_tree_state state; |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
struct ast_sip_sched_task *expiration_task; |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
char transport_key[IP6ADDR_COLON_PORT_BUFLEN]; |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
unsigned int generate_initial_notify; |
| 689 |
}; |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
struct ast_sip_subscription { |
| 700 |
|
| 701 |
struct ao2_container *datastores; |
| 702 |
|
| 703 |
const struct ast_sip_subscription_handler *handler; |
| 704 |
|
| 705 |
struct sip_subscription_tree *tree; |
| 706 |
|
| 707 |
struct ast_sip_pubsub_body_generator *body_generator; |
| 708 |
|
| 709 |
AST_VECTOR(, struct ast_sip_subscription *) children; |
| 710 |
|
| 711 |
struct ast_str *body_text; |
| 712 |
|
| 713 |
int body_changed; |
| 714 |
|
| 715 |
pjsip_evsub_state subscription_state; |
| 716 |
|
| 717 |
unsigned int version; |
| 718 |
|
| 719 |
unsigned int full_state; |
| 720 |
|
| 721 |
pjsip_sip_uri *uri; |
| 722 |
|
| 723 |
struct ast_json *persistence_data; |
| 724 |
|
| 725 |
char *display_name; |
| 726 |
|
| 727 |
char resource[0]; |
| 728 |
}; |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
struct ast_sip_publication_resource { |
| 734 |
|
| 735 |
SORCERY_OBJECT(details); |
| 736 |
|
| 737 |
char *endpoint; |
| 738 |
|
| 739 |
struct ast_variable *events; |
| 740 |
}; |
| 741 |
|
| 742 |
static const char *sip_subscription_roles_map[] = { |
| 743 |
[AST_SIP_SUBSCRIBER] = "Subscriber", |
| 744 |
[AST_SIP_NOTIFIER] = "Notifier" |
| 745 |
}; |
| 746 |
|
| 747 |
enum sip_persistence_update_type { |
| 748 |
|
| 749 |
SUBSCRIPTION_PERSISTENCE_SEND_REQUEST = 0, |
| 750 |
|
| 751 |
SUBSCRIPTION_PERSISTENCE_CREATED, |
| 752 |
|
| 753 |
SUBSCRIPTION_PERSISTENCE_RECREATED, |
| 754 |
|
| 755 |
SUBSCRIPTION_PERSISTENCE_REFRESHED, |
| 756 |
}; |
| 757 |
|
| 758 |
AST_RWLIST_HEAD_STATIC(subscriptions, sip_subscription_tree); |
| 759 |
|
| 760 |
AST_RWLIST_HEAD_STATIC(body_generators, ast_sip_pubsub_body_generator); |
| 761 |
AST_RWLIST_HEAD_STATIC(body_supplements, ast_sip_pubsub_body_supplement); |
| 762 |
|
| 763 |
static pjsip_media_type rlmi_media_type; |
| 764 |
|
| 765 |
static void pubsub_on_evsub_state(pjsip_evsub *sub, pjsip_event *event); |
| 766 |
static void pubsub_on_rx_refresh(pjsip_evsub *sub, pjsip_rx_data *rdata, |
| 767 |
int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body); |
| 768 |
static void pubsub_on_rx_notify(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code, |
| 769 |
pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body); |
| 770 |
static void pubsub_on_client_refresh(pjsip_evsub *sub); |
| 771 |
static void pubsub_on_server_timeout(pjsip_evsub *sub); |
| 772 |
|
| 773 |
static pjsip_evsub_user pubsub_cb = { |
| 774 |
.on_evsub_state = pubsub_on_evsub_state, |
| 775 |
.on_rx_refresh = pubsub_on_rx_refresh, |
| 776 |
.on_rx_notify = pubsub_on_rx_notify, |
| 777 |
.on_client_refresh = pubsub_on_client_refresh, |
| 778 |
.on_server_timeout = pubsub_on_server_timeout, |
| 779 |
}; |
| 780 |
|
| 781 |
|
| 782 |
static void publication_resource_destroy(void *obj) |
| 783 |
{ |
| 784 |
struct ast_sip_publication_resource *resource = obj; |
| 785 |
|
| 786 |
ast_free(resource->endpoint); |
| 787 |
ast_variables_destroy(resource->events); |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
static void *publication_resource_alloc(const char *name) |
| 792 |
{ |
| 793 |
return ast_sorcery_generic_alloc(sizeof(struct ast_sip_publication_resource), publication_resource_destroy); |
| 794 |
} |
| 795 |
|
| 796 |
static int sub_tree_subscription_terminate_cb(void *data) |
| 797 |
{ |
| 798 |
struct sip_subscription_tree *sub_tree = data; |
| 799 |
|
| 800 |
if (!sub_tree->evsub) { |
| 801 |
|
| 802 |
ao2_ref(sub_tree, -1); |
| 803 |
return 0; |
| 804 |
} |
| 805 |
|
| 806 |
ast_debug(3, "Transport destroyed. Removing subscription '%s->%s' prune on boot: %d\n", |
| 807 |
sub_tree->persistence->endpoint, sub_tree->root->resource, |
| 808 |
sub_tree->persistence->prune_on_boot); |
| 809 |
|
| 810 |
sub_tree->state = SIP_SUB_TREE_TERMINATE_IN_PROGRESS; |
| 811 |
pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE); |
| 812 |
|
| 813 |
ao2_ref(sub_tree, -1); |
| 814 |
return 0; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
static void sub_tree_transport_cb(void *data) |
| 826 |
{ |
| 827 |
struct sip_subscription_tree *sub_tree = data; |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
ao2_ref(sub_tree, +1); |
| 836 |
if (ast_sip_push_task(sub_tree->serializer, sub_tree_subscription_terminate_cb, |
| 837 |
sub_tree)) { |
| 838 |
ao2_ref(sub_tree, -1); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
static void subscription_persistence_destroy(void *obj) |
| 844 |
{ |
| 845 |
struct subscription_persistence *persistence = obj; |
| 846 |
|
| 847 |
ast_free(persistence->endpoint); |
| 848 |
ast_free(persistence->tag); |
| 849 |
ast_json_unref(persistence->generator_data); |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
static void *subscription_persistence_alloc(const char *name) |
| 854 |
{ |
| 855 |
return ast_sorcery_generic_alloc(sizeof(struct subscription_persistence), subscription_persistence_destroy); |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
static struct subscription_persistence *subscription_persistence_create(struct sip_subscription_tree *sub_tree) |
| 860 |
{ |
| 861 |
char tag[PJ_GUID_STRING_LENGTH + 1]; |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
struct subscription_persistence *persistence = ast_sorcery_alloc(ast_sip_get_sorcery(), |
| 867 |
"subscription_persistence", NULL); |
| 868 |
|
| 869 |
pjsip_dialog *dlg = sub_tree->dlg; |
| 870 |
|
| 871 |
if (!persistence) { |
| 872 |
return NULL; |
| 873 |
} |
| 874 |
|
| 875 |
persistence->endpoint = ast_strdup(ast_sorcery_object_get_id(sub_tree->endpoint)); |
| 876 |
ast_copy_pj_str(tag, &dlg->local.info->tag, sizeof(tag)); |
| 877 |
persistence->tag = ast_strdup(tag); |
| 878 |
|
| 879 |
ast_sorcery_create(ast_sip_get_sorcery(), persistence); |
| 880 |
return persistence; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
static void subscription_persistence_update(struct sip_subscription_tree *sub_tree, |
| 885 |
pjsip_rx_data *rdata, enum sip_persistence_update_type type) |
| 886 |
{ |
| 887 |
pjsip_dialog *dlg; |
| 888 |
|
| 889 |
if (!sub_tree->persistence) { |
| 890 |
return; |
| 891 |
} |
| 892 |
|
| 893 |
ast_debug(3, "Updating persistence for '%s->%s' prune on boot: %s\n", |
| 894 |
sub_tree->persistence->endpoint, sub_tree->root->resource, |
| 895 |
sub_tree->persistence->prune_on_boot ? "yes" : "no"); |
| 896 |
|
| 897 |
dlg = sub_tree->dlg; |
| 898 |
sub_tree->persistence->cseq = dlg->local.cseq; |
| 899 |
|
| 900 |
if (rdata) { |
| 901 |
unsigned int expires; |
| 902 |
pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL); |
| 903 |
pjsip_contact_hdr *contact_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL); |
| 904 |
|
| 905 |
expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES; |
| 906 |
sub_tree->persistence->expires = ast_tvadd(ast_tvnow(), ast_samp2tv(expires, 1)); |
| 907 |
|
| 908 |
if (contact_hdr) { |
| 909 |
if (contact_hdr) { |
| 910 |
if (type == SUBSCRIPTION_PERSISTENCE_CREATED) { |
| 911 |
sub_tree->persistence->prune_on_boot = |
| 912 |
!ast_sip_will_uri_survive_restart( |
| 913 |
(pjsip_sip_uri *)pjsip_uri_get_uri(contact_hdr->uri), |
| 914 |
sub_tree->endpoint, rdata); |
| 915 |
|
| 916 |
if (sub_tree->persistence->prune_on_boot) { |
| 917 |
ast_debug(3, "adding transport monitor on %s for '%s->%s' prune on boot: %d\n", |
| 918 |
rdata->tp_info.transport->obj_name, |
| 919 |
sub_tree->persistence->endpoint, sub_tree->root->resource, |
| 920 |
sub_tree->persistence->prune_on_boot); |
| 921 |
AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(rdata->tp_info.transport, |
| 922 |
sub_tree->transport_key); |
| 923 |
ast_sip_transport_monitor_register_key(sub_tree->transport_key, |
| 924 |
sub_tree_transport_cb, sub_tree); |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
} |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, contact_hdr->uri, |
| 933 |
sub_tree->persistence->contact_uri, sizeof(sub_tree->persistence->contact_uri)); |
| 934 |
} else { |
| 935 |
ast_log(LOG_WARNING, "Contact not updated due to missing contact header\n"); |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
if (type == SUBSCRIPTION_PERSISTENCE_CREATED |
| 945 |
|| type == SUBSCRIPTION_PERSISTENCE_RECREATED) { |
| 946 |
if (rdata->msg_info.msg_buf) { |
| 947 |
ast_copy_string(sub_tree->persistence->packet, rdata->msg_info.msg_buf, |
| 948 |
MIN(sizeof(sub_tree->persistence->packet), rdata->msg_info.len + 1)); |
| 949 |
} else { |
| 950 |
ast_copy_string(sub_tree->persistence->packet, rdata->pkt_info.packet, |
| 951 |
sizeof(sub_tree->persistence->packet)); |
| 952 |
} |
| 953 |
} |
| 954 |
ast_copy_string(sub_tree->persistence->src_name, rdata->pkt_info.src_name, |
| 955 |
sizeof(sub_tree->persistence->src_name)); |
| 956 |
sub_tree->persistence->src_port = rdata->pkt_info.src_port; |
| 957 |
ast_copy_string(sub_tree->persistence->transport_type, rdata->tp_info.transport->type_name, |
| 958 |
sizeof(sub_tree->persistence->transport_type)); |
| 959 |
ast_copy_pj_str(sub_tree->persistence->local_name, &rdata->tp_info.transport->local_name.host, |
| 960 |
sizeof(sub_tree->persistence->local_name)); |
| 961 |
sub_tree->persistence->local_port = rdata->tp_info.transport->local_name.port; |
| 962 |
} |
| 963 |
|
| 964 |
ast_sorcery_update(ast_sip_get_sorcery(), sub_tree->persistence); |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
static void subscription_persistence_remove(struct sip_subscription_tree *sub_tree) |
| 969 |
{ |
| 970 |
if (!sub_tree->persistence) { |
| 971 |
return; |
| 972 |
} |
| 973 |
|
| 974 |
if (sub_tree->persistence->prune_on_boot && !ast_strlen_zero(sub_tree->transport_key)) { |
| 975 |
ast_debug(3, "Unregistering transport monitor on %s '%s->%s'\n", |
| 976 |
sub_tree->transport_key, |
| 977 |
sub_tree->endpoint ? ast_sorcery_object_get_id(sub_tree->endpoint) : "Unknown", |
| 978 |
sub_tree->root ? sub_tree->root->resource : "Unknown"); |
| 979 |
ast_sip_transport_monitor_unregister_key(sub_tree->transport_key, |
| 980 |
sub_tree_transport_cb, sub_tree, NULL); |
| 981 |
} |
| 982 |
|
| 983 |
ast_sorcery_delete(ast_sip_get_sorcery(), sub_tree->persistence); |
| 984 |
ao2_ref(sub_tree->persistence, -1); |
| 985 |
sub_tree->persistence = NULL; |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
static struct ast_sip_subscription_handler *find_sub_handler_for_event_name(const char *event_name); |
| 990 |
static struct ast_sip_pubsub_body_generator *find_body_generator(char accept[AST_SIP_MAX_ACCEPT][64], |
| 991 |
size_t num_accept, const char *body_type); |
| 992 |
|
| 993 |
|
| 994 |
static struct ast_sip_subscription_handler *subscription_get_handler_from_rdata(pjsip_rx_data *rdata, const char *endpoint) |
| 995 |
{ |
| 996 |
pjsip_event_hdr *event_header; |
| 997 |
char event[32]; |
| 998 |
struct ast_sip_subscription_handler *handler; |
| 999 |
|
| 1000 |
event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_event_name, rdata->msg_info.msg->hdr.next); |
| 1001 |
if (!event_header) { |
| 1002 |
ast_log(LOG_WARNING, "Incoming SUBSCRIBE request from %s with no Event header\n", |
| 1003 |
endpoint ? endpoint : "Unknown"); |
| 1004 |
return NULL; |
| 1005 |
} |
| 1006 |
ast_copy_pj_str(event, &event_header->event_type, sizeof(event)); |
| 1007 |
|
| 1008 |
handler = find_sub_handler_for_event_name(event); |
| 1009 |
if (!handler) { |
| 1010 |
ast_log(LOG_WARNING, "No registered subscribe handler for event %s from %s\n", event, |
| 1011 |
endpoint ? endpoint : "Unknown"); |
| 1012 |
} |
| 1013 |
|
| 1014 |
return handler; |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
const char *accept_exceptions[] = { |
| 1030 |
"multipart/related", |
| 1031 |
"application/rlmi+xml", |
| 1032 |
}; |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
static int exceptional_accept(const pj_str_t *accept) |
| 1041 |
{ |
| 1042 |
int i; |
| 1043 |
|
| 1044 |
for (i = 0; i < ARRAY_LEN(accept_exceptions); ++i) { |
| 1045 |
if (!pj_strcmp2(accept, accept_exceptions[i])) { |
| 1046 |
return 1; |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
return 0; |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
static struct ast_sip_pubsub_body_generator *subscription_get_generator_from_rdata(pjsip_rx_data *rdata, |
| 1055 |
const struct ast_sip_subscription_handler *handler) |
| 1056 |
{ |
| 1057 |
pjsip_accept_hdr *accept_header = (pjsip_accept_hdr *) &rdata->msg_info.msg->hdr; |
| 1058 |
char accept[AST_SIP_MAX_ACCEPT][64]; |
| 1059 |
size_t num_accept_headers = 0; |
| 1060 |
|
| 1061 |
while ((accept_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, accept_header->next)) && |
| 1062 |
(num_accept_headers < AST_SIP_MAX_ACCEPT)) { |
| 1063 |
int i; |
| 1064 |
|
| 1065 |
for (i = 0; i < accept_header->count && num_accept_headers < AST_SIP_MAX_ACCEPT; ++i) { |
| 1066 |
if (!exceptional_accept(&accept_header->values[i])) { |
| 1067 |
ast_copy_pj_str(accept[num_accept_headers], &accept_header->values[i], sizeof(accept[num_accept_headers])); |
| 1068 |
++num_accept_headers; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
if (num_accept_headers == 0) { |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
ast_copy_string(accept[0], handler->notifier->default_accept, sizeof(accept[0])); |
| 1078 |
num_accept_headers = 1; |
| 1079 |
} |
| 1080 |
|
| 1081 |
return find_body_generator(accept, num_accept_headers, handler->body_type); |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
static int ast_sip_pubsub_has_eventlist_support(pjsip_rx_data *rdata) |
| 1090 |
{ |
| 1091 |
pjsip_supported_hdr *supported_header = (pjsip_supported_hdr *) &rdata->msg_info.msg->hdr; |
| 1092 |
|
| 1093 |
while ((supported_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_SUPPORTED, supported_header->next))) { |
| 1094 |
int i; |
| 1095 |
|
| 1096 |
for (i = 0; i < supported_header->count; i++) { |
| 1097 |
if (!pj_stricmp2(&supported_header->values[i], "eventlist")) { |
| 1098 |
return 1; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
return 0; |
| 1104 |
} |
| 1105 |
|
| 1106 |
struct resource_tree; |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
struct tree_node { |
| 1112 |
AST_VECTOR(, struct tree_node *) children; |
| 1113 |
unsigned int full_state; |
| 1114 |
char *display_name; |
| 1115 |
char resource[0]; |
| 1116 |
}; |
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
static struct resource_list *retrieve_resource_list(const char *resource, const char *event) |
| 1127 |
{ |
| 1128 |
struct resource_list *list; |
| 1129 |
|
| 1130 |
list = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "resource_list", resource); |
| 1131 |
if (!list) { |
| 1132 |
return NULL; |
| 1133 |
} |
| 1134 |
|
| 1135 |
if (strcmp(list->event, event)) { |
| 1136 |
ast_log(LOG_WARNING, "Found resource list %s, but its event type (%s) does not match SUBSCRIBE's (%s)\n", |
| 1137 |
resource, list->event, event); |
| 1138 |
ao2_cleanup(list); |
| 1139 |
return NULL; |
| 1140 |
} |
| 1141 |
|
| 1142 |
return list; |
| 1143 |
} |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
static struct tree_node *tree_node_alloc(const char *resource, struct resources *visited, unsigned int full_state, const char *display_name) |
| 1161 |
{ |
| 1162 |
struct tree_node *node; |
| 1163 |
|
| 1164 |
node = ast_calloc(1, sizeof(*node) + strlen(resource) + 1); |
| 1165 |
if (!node) { |
| 1166 |
return NULL; |
| 1167 |
} |
| 1168 |
|
| 1169 |
strcpy(node->resource, resource); |
| 1170 |
if (AST_VECTOR_INIT(&node->children, 4)) { |
| 1171 |
ast_free(node); |
| 1172 |
return NULL; |
| 1173 |
} |
| 1174 |
node->full_state = full_state; |
| 1175 |
node->display_name = ast_strdup(display_name); |
| 1176 |
|
| 1177 |
if (visited) { |
| 1178 |
AST_VECTOR_APPEND(visited, resource); |
| 1179 |
} |
| 1180 |
return node; |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
static void tree_node_destroy(struct tree_node *node) |
| 1193 |
{ |
| 1194 |
int i; |
| 1195 |
if (!node) { |
| 1196 |
return; |
| 1197 |
} |
| 1198 |
|
| 1199 |
for (i = 0; i < AST_VECTOR_SIZE(&node->children); ++i) { |
| 1200 |
tree_node_destroy(AST_VECTOR_GET(&node->children, i)); |
| 1201 |
} |
| 1202 |
AST_VECTOR_FREE(&node->children); |
| 1203 |
ast_free(node->display_name); |
| 1204 |
ast_free(node); |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
static int have_visited(const char *resource, struct resources *visited) |
| 1216 |
{ |
| 1217 |
int i; |
| 1218 |
|
| 1219 |
for (i = 0; i < AST_VECTOR_SIZE(visited); ++i) { |
| 1220 |
if (!strcmp(resource, AST_VECTOR_GET(visited, i))) { |
| 1221 |
return 1; |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
return 0; |
| 1226 |
} |
| 1227 |
|
| 1228 |
#define NEW_SUBSCRIBE(notifier, endpoint, resource, rdata) notifier->new_subscribe_with_rdata ? notifier->new_subscribe_with_rdata(endpoint, resource, rdata) : notifier->new_subscribe(endpoint, resource) |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
static void build_node_children(struct ast_sip_endpoint *endpoint, const struct ast_sip_subscription_handler *handler, |
| 1252 |
struct resource_list *list, struct tree_node *parent, struct resources *visited, pjsip_rx_data *rdata) |
| 1253 |
{ |
| 1254 |
int i; |
| 1255 |
|
| 1256 |
for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) { |
| 1257 |
struct tree_node *current; |
| 1258 |
struct resource_list *child_list; |
| 1259 |
const char *resource = AST_VECTOR_GET(&list->items, i); |
| 1260 |
|
| 1261 |
if (have_visited(resource, visited)) { |
| 1262 |
ast_debug(1, "Already visited resource %s. Avoiding duplicate resource or potential loop.\n", resource); |
| 1263 |
continue; |
| 1264 |
} |
| 1265 |
|
| 1266 |
child_list = retrieve_resource_list(resource, list->event); |
| 1267 |
if (!child_list) { |
| 1268 |
int resp = NEW_SUBSCRIBE(handler->notifier, endpoint, resource, rdata); |
| 1269 |
if (PJSIP_IS_STATUS_IN_CLASS(resp, 200)) { |
| 1270 |
char display_name[AST_MAX_EXTENSION] = ""; |
| 1271 |
if (list->resource_display_name && handler->notifier->get_resource_display_name) { |
| 1272 |
handler->notifier->get_resource_display_name(endpoint, resource, display_name, sizeof(display_name)); |
| 1273 |
} |
| 1274 |
current = tree_node_alloc(resource, visited, 0, ast_strlen_zero(display_name) ? NULL : display_name); |
| 1275 |
if (!current) { |
| 1276 |
ast_debug(1, |
| 1277 |
"Subscription to leaf resource %s was successful, but encountered allocation error afterwards\n", |
| 1278 |
resource); |
| 1279 |
continue; |
| 1280 |
} |
| 1281 |
ast_debug(2, "Subscription to leaf resource %s resulted in success. Adding to parent %s\n", |
| 1282 |
resource, parent->resource); |
| 1283 |
if (AST_VECTOR_APPEND(&parent->children, current)) { |
| 1284 |
tree_node_destroy(current); |
| 1285 |
} |
| 1286 |
} else { |
| 1287 |
ast_debug(2, "Subscription to leaf resource %s resulted in error response %d\n", |
| 1288 |
resource, resp); |
| 1289 |
} |
| 1290 |
} else { |
| 1291 |
ast_debug(2, "Resource %s (child of %s) is a list\n", resource, parent->resource); |
| 1292 |
current = tree_node_alloc(resource, visited, child_list->full_state, NULL); |
| 1293 |
if (!current) { |
| 1294 |
ast_debug(1, "Cannot build children of resource %s due to allocation failure\n", resource); |
| 1295 |
continue; |
| 1296 |
} |
| 1297 |
build_node_children(endpoint, handler, child_list, current, visited, rdata); |
| 1298 |
if (AST_VECTOR_SIZE(¤t->children) > 0) { |
| 1299 |
ast_debug(1, "List %s had no successful children.\n", resource); |
| 1300 |
if (AST_VECTOR_APPEND(&parent->children, current)) { |
| 1301 |
tree_node_destroy(current); |
| 1302 |
} |
| 1303 |
} else { |
| 1304 |
ast_debug(2, "List %s had successful children. Adding to parent %s\n", |
| 1305 |
resource, parent->resource); |
| 1306 |
tree_node_destroy(current); |
| 1307 |
} |
| 1308 |
ao2_cleanup(child_list); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
struct resource_tree { |
| 1330 |
struct tree_node *root; |
| 1331 |
unsigned int notification_batch_interval; |
| 1332 |
}; |
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
static void resource_tree_destroy(struct resource_tree *tree) |
| 1344 |
{ |
| 1345 |
if (tree) { |
| 1346 |
tree_node_destroy(tree->root); |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
static int build_resource_tree(struct ast_sip_endpoint *endpoint, const struct ast_sip_subscription_handler *handler, |
| 1370 |
const char *resource, struct resource_tree *tree, int has_eventlist_support, pjsip_rx_data *rdata) |
| 1371 |
{ |
| 1372 |
RAII_VAR(struct resource_list *, list, NULL, ao2_cleanup); |
| 1373 |
struct resources visited; |
| 1374 |
|
| 1375 |
int not_eventlist_but_needs_children = !strcmp(handler->body_type, AST_SIP_DEVICE_FEATURE_SYNC_DATA); |
| 1376 |
|
| 1377 |
if ((!has_eventlist_support && !not_eventlist_but_needs_children) || !(list = retrieve_resource_list(resource, handler->event_name))) { |
| 1378 |
ast_debug(2, "Subscription '%s->%s' is not to a list\n", |
| 1379 |
ast_sorcery_object_get_id(endpoint), resource); |
| 1380 |
tree->root = tree_node_alloc(resource, NULL, 0, NULL); |
| 1381 |
if (!tree->root) { |
| 1382 |
return 500; |
| 1383 |
} |
| 1384 |
return NEW_SUBSCRIBE(handler->notifier, endpoint, resource, rdata); |
| 1385 |
} |
| 1386 |
|
| 1387 |
ast_debug(2, "Subscription '%s->%s' is a list\n", |
| 1388 |
ast_sorcery_object_get_id(endpoint), resource); |
| 1389 |
if (AST_VECTOR_INIT(&visited, AST_VECTOR_SIZE(&list->items))) { |
| 1390 |
return 500; |
| 1391 |
} |
| 1392 |
|
| 1393 |
tree->root = tree_node_alloc(resource, &visited, list->full_state, NULL); |
| 1394 |
if (!tree->root) { |
| 1395 |
AST_VECTOR_FREE(&visited); |
| 1396 |
return 500; |
| 1397 |
} |
| 1398 |
|
| 1399 |
tree->notification_batch_interval = list->notification_batch_interval; |
| 1400 |
|
| 1401 |
build_node_children(endpoint, handler, list, tree->root, &visited, rdata); |
| 1402 |
AST_VECTOR_FREE(&visited); |
| 1403 |
|
| 1404 |
if (AST_VECTOR_SIZE(&tree->root->children) > 0) { |
| 1405 |
return 200; |
| 1406 |
} else { |
| 1407 |
return 500; |
| 1408 |
} |
| 1409 |
} |
| 1410 |
|
| 1411 |
static void add_subscription(struct sip_subscription_tree *obj) |
| 1412 |
{ |
| 1413 |
AST_RWLIST_WRLOCK(&subscriptions); |
| 1414 |
AST_RWLIST_INSERT_TAIL(&subscriptions, obj, next); |
| 1415 |
AST_RWLIST_UNLOCK(&subscriptions); |
| 1416 |
} |
| 1417 |
|
| 1418 |
static void remove_subscription(struct sip_subscription_tree *obj) |
| 1419 |
{ |
| 1420 |
struct sip_subscription_tree *i; |
| 1421 |
|
| 1422 |
AST_RWLIST_WRLOCK(&subscriptions); |
| 1423 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&subscriptions, i, next) { |
| 1424 |
if (i == obj) { |
| 1425 |
AST_RWLIST_REMOVE_CURRENT(next); |
| 1426 |
if (i->root) { |
| 1427 |
ast_debug(2, "Removing subscription '%s->%s' from list of subscriptions\n", |
| 1428 |
ast_sorcery_object_get_id(i->endpoint), ast_sip_subscription_get_resource_name(i->root)); |
| 1429 |
} |
| 1430 |
break; |
| 1431 |
} |
| 1432 |
} |
| 1433 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 1434 |
AST_RWLIST_UNLOCK(&subscriptions); |
| 1435 |
} |
| 1436 |
|
| 1437 |
static void destroy_subscription(struct ast_sip_subscription *sub) |
| 1438 |
{ |
| 1439 |
ast_debug(3, "Destroying SIP subscription from '%s->%s'\n", |
| 1440 |
sub->tree && sub->tree->endpoint ? ast_sorcery_object_get_id(sub->tree->endpoint) : "Unknown", |
| 1441 |
sub->resource); |
| 1442 |
|
| 1443 |
ast_free(sub->body_text); |
| 1444 |
|
| 1445 |
AST_VECTOR_FREE(&sub->children); |
| 1446 |
ao2_cleanup(sub->datastores); |
| 1447 |
ast_json_unref(sub->persistence_data); |
| 1448 |
ast_free(sub->display_name); |
| 1449 |
if (sub->tree) { |
| 1450 |
|
| 1451 |
struct sip_subscription_tree *tree = sub->tree; |
| 1452 |
sub->tree=NULL; |
| 1453 |
ao2_cleanup(tree); |
| 1454 |
} |
| 1455 |
ast_free(sub); |
| 1456 |
} |
| 1457 |
|
| 1458 |
static void destroy_subscriptions(struct ast_sip_subscription *root) |
| 1459 |
{ |
| 1460 |
int i; |
| 1461 |
|
| 1462 |
if (!root) { |
| 1463 |
return; |
| 1464 |
} |
| 1465 |
|
| 1466 |
for (i = 0; i < AST_VECTOR_SIZE(&root->children); ++i) { |
| 1467 |
struct ast_sip_subscription *child; |
| 1468 |
|
| 1469 |
child = AST_VECTOR_GET(&root->children, i); |
| 1470 |
destroy_subscriptions(child); |
| 1471 |
} |
| 1472 |
|
| 1473 |
destroy_subscription(root); |
| 1474 |
} |
| 1475 |
|
| 1476 |
static struct ast_sip_subscription *allocate_subscription(const struct ast_sip_subscription_handler *handler, |
| 1477 |
const char *resource, const char *display_name, struct sip_subscription_tree *tree) |
| 1478 |
{ |
| 1479 |
struct ast_sip_subscription *sub; |
| 1480 |
pjsip_msg *msg; |
| 1481 |
pjsip_sip_uri *request_uri; |
| 1482 |
|
| 1483 |
msg = ast_sip_mod_data_get(tree->dlg->mod_data, pubsub_module.id, MOD_DATA_MSG); |
| 1484 |
if (!msg) { |
| 1485 |
ast_log(LOG_ERROR, "No dialog message saved for SIP subscription. Cannot allocate subscription for resource %s\n", resource); |
| 1486 |
return NULL; |
| 1487 |
} |
| 1488 |
|
| 1489 |
sub = ast_calloc(1, sizeof(*sub) + strlen(resource) + 1); |
| 1490 |
if (!sub) { |
| 1491 |
return NULL; |
| 1492 |
} |
| 1493 |
strcpy(sub->resource, resource); |
| 1494 |
|
| 1495 |
sub->display_name = ast_strdup(display_name); |
| 1496 |
|
| 1497 |
sub->datastores = ast_datastores_alloc(); |
| 1498 |
if (!sub->datastores) { |
| 1499 |
destroy_subscription(sub); |
| 1500 |
return NULL; |
| 1501 |
} |
| 1502 |
|
| 1503 |
sub->body_text = ast_str_create(128); |
| 1504 |
if (!sub->body_text) { |
| 1505 |
destroy_subscription(sub); |
| 1506 |
return NULL; |
| 1507 |
} |
| 1508 |
|
| 1509 |
sub->uri = pjsip_sip_uri_create(tree->dlg->pool, PJ_FALSE); |
| 1510 |
request_uri = pjsip_uri_get_uri(msg->line.req.uri); |
| 1511 |
pjsip_sip_uri_assign(tree->dlg->pool, sub->uri, request_uri); |
| 1512 |
pj_strdup2(tree->dlg->pool, &sub->uri->user, resource); |
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
if (tree->persistence && tree->persistence->generator_data) { |
| 1519 |
sub->persistence_data = ast_json_ref(ast_json_object_get(tree->persistence->generator_data, resource)); |
| 1520 |
} |
| 1521 |
|
| 1522 |
sub->handler = handler; |
| 1523 |
sub->subscription_state = PJSIP_EVSUB_STATE_ACTIVE; |
| 1524 |
sub->tree = ao2_bump(tree); |
| 1525 |
|
| 1526 |
return sub; |
| 1527 |
} |
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
static struct ast_sip_subscription *create_virtual_subscriptions(const struct ast_sip_subscription_handler *handler, |
| 1539 |
const char *resource, struct ast_sip_pubsub_body_generator *generator, |
| 1540 |
struct sip_subscription_tree *tree, struct tree_node *current) |
| 1541 |
{ |
| 1542 |
int i; |
| 1543 |
struct ast_sip_subscription *sub; |
| 1544 |
|
| 1545 |
sub = allocate_subscription(handler, resource, current->display_name, tree); |
| 1546 |
if (!sub) { |
| 1547 |
return NULL; |
| 1548 |
} |
| 1549 |
|
| 1550 |
sub->full_state = current->full_state; |
| 1551 |
sub->body_generator = generator; |
| 1552 |
AST_VECTOR_INIT(&sub->children, AST_VECTOR_SIZE(¤t->children)); |
| 1553 |
|
| 1554 |
for (i = 0; i < AST_VECTOR_SIZE(¤t->children); ++i) { |
| 1555 |
struct ast_sip_subscription *child; |
| 1556 |
struct tree_node *child_node = AST_VECTOR_GET(¤t->children, i); |
| 1557 |
|
| 1558 |
child = create_virtual_subscriptions(handler, child_node->resource, generator, |
| 1559 |
tree, child_node); |
| 1560 |
|
| 1561 |
if (!child) { |
| 1562 |
ast_debug(1, "Child subscription to resource %s could not be created\n", |
| 1563 |
child_node->resource); |
| 1564 |
continue; |
| 1565 |
} |
| 1566 |
|
| 1567 |
if (AST_VECTOR_APPEND(&sub->children, child)) { |
| 1568 |
ast_debug(1, "Child subscription to resource %s could not be appended\n", |
| 1569 |
child_node->resource); |
| 1570 |
destroy_subscriptions(child); |
| 1571 |
} |
| 1572 |
} |
| 1573 |
|
| 1574 |
return sub; |
| 1575 |
} |
| 1576 |
|
| 1577 |
static void shutdown_subscriptions(struct ast_sip_subscription *sub) |
| 1578 |
{ |
| 1579 |
int i; |
| 1580 |
|
| 1581 |
if (!sub) { |
| 1582 |
return; |
| 1583 |
} |
| 1584 |
|
| 1585 |
if (AST_VECTOR_SIZE(&sub->children) > 0) { |
| 1586 |
for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) { |
| 1587 |
shutdown_subscriptions(AST_VECTOR_GET(&sub->children, i)); |
| 1588 |
} |
| 1589 |
return; |
| 1590 |
} |
| 1591 |
|
| 1592 |
|
| 1593 |
if (sub->handler->subscription_shutdown) { |
| 1594 |
sub->handler->subscription_shutdown(sub); |
| 1595 |
} |
| 1596 |
} |
| 1597 |
|
| 1598 |
static int subscription_unreference_dialog(void *obj) |
| 1599 |
{ |
| 1600 |
struct sip_subscription_tree *sub_tree = obj; |
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
pjsip_dlg_dec_session(sub_tree->dlg, &pubsub_module); |
| 1612 |
sub_tree->dlg = NULL; |
| 1613 |
|
| 1614 |
return 0; |
| 1615 |
} |
| 1616 |
|
| 1617 |
static void subscription_tree_destructor(void *obj) |
| 1618 |
{ |
| 1619 |
struct sip_subscription_tree *sub_tree = obj; |
| 1620 |
|
| 1621 |
ast_debug(3, "Destroying subscription tree %p '%s->%s'\n", |
| 1622 |
sub_tree, |
| 1623 |
sub_tree->endpoint ? ast_sorcery_object_get_id(sub_tree->endpoint) : "Unknown", |
| 1624 |
sub_tree->root ? sub_tree->root->resource : "Unknown"); |
| 1625 |
|
| 1626 |
destroy_subscriptions(sub_tree->root); |
| 1627 |
|
| 1628 |
if (sub_tree->dlg) { |
| 1629 |
ast_sip_push_task_wait_servant(sub_tree->serializer, |
| 1630 |
subscription_unreference_dialog, sub_tree); |
| 1631 |
} |
| 1632 |
|
| 1633 |
ao2_cleanup(sub_tree->endpoint); |
| 1634 |
|
| 1635 |
ast_taskprocessor_unreference(sub_tree->serializer); |
| 1636 |
ast_module_unref(ast_module_info->self); |
| 1637 |
} |
| 1638 |
|
| 1639 |
void ast_sip_subscription_destroy(struct ast_sip_subscription *sub) |
| 1640 |
{ |
| 1641 |
ast_debug(3, "Removing subscription %p '%s->%s' reference to subscription tree %p\n", |
| 1642 |
sub, ast_sorcery_object_get_id(sub->tree->endpoint), sub->resource, sub->tree); |
| 1643 |
if (sub->tree) { |
| 1644 |
|
| 1645 |
struct sip_subscription_tree *tree = sub->tree; |
| 1646 |
sub->tree = NULL; |
| 1647 |
ao2_cleanup(tree); |
| 1648 |
} |
| 1649 |
} |
| 1650 |
|
| 1651 |
static void subscription_setup_dialog(struct sip_subscription_tree *sub_tree, pjsip_dialog *dlg) |
| 1652 |
{ |
| 1653 |
sub_tree->dlg = dlg; |
| 1654 |
ast_sip_dialog_set_serializer(dlg, sub_tree->serializer); |
| 1655 |
ast_sip_dialog_set_endpoint(dlg, sub_tree->endpoint); |
| 1656 |
pjsip_evsub_set_mod_data(sub_tree->evsub, pubsub_module.id, sub_tree); |
| 1657 |
pjsip_dlg_inc_session(dlg, &pubsub_module); |
| 1658 |
} |
| 1659 |
|
| 1660 |
static struct sip_subscription_tree *allocate_subscription_tree(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata) |
| 1661 |
{ |
| 1662 |
struct sip_subscription_tree *sub_tree; |
| 1663 |
|
| 1664 |
sub_tree = ao2_alloc(sizeof *sub_tree, subscription_tree_destructor); |
| 1665 |
if (!sub_tree) { |
| 1666 |
return NULL; |
| 1667 |
} |
| 1668 |
|
| 1669 |
ast_module_ref(ast_module_info->self); |
| 1670 |
|
| 1671 |
if (rdata) { |
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
sub_tree->serializer = ast_sip_get_distributor_serializer(rdata); |
| 1680 |
} else { |
| 1681 |
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1]; |
| 1682 |
|
| 1683 |
|
| 1684 |
ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/pubsub/%s", |
| 1685 |
ast_sorcery_object_get_id(endpoint)); |
| 1686 |
|
| 1687 |
sub_tree->serializer = ast_sip_create_serializer(tps_name); |
| 1688 |
} |
| 1689 |
if (!sub_tree->serializer) { |
| 1690 |
ao2_ref(sub_tree, -1); |
| 1691 |
return NULL; |
| 1692 |
} |
| 1693 |
|
| 1694 |
sub_tree->endpoint = ao2_bump(endpoint); |
| 1695 |
sub_tree->notify_sched_id = -1; |
| 1696 |
|
| 1697 |
return sub_tree; |
| 1698 |
} |
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
static struct sip_subscription_tree *create_subscription_tree(const struct ast_sip_subscription_handler *handler, |
| 1722 |
struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *resource, |
| 1723 |
struct ast_sip_pubsub_body_generator *generator, struct resource_tree *tree, |
| 1724 |
pj_status_t *dlg_status, struct subscription_persistence *persistence) |
| 1725 |
{ |
| 1726 |
struct sip_subscription_tree *sub_tree; |
| 1727 |
pjsip_dialog *dlg; |
| 1728 |
|
| 1729 |
sub_tree = allocate_subscription_tree(endpoint, rdata); |
| 1730 |
if (!sub_tree) { |
| 1731 |
*dlg_status = PJ_ENOMEM; |
| 1732 |
return NULL; |
| 1733 |
} |
| 1734 |
sub_tree->role = AST_SIP_NOTIFIER; |
| 1735 |
|
| 1736 |
dlg = ast_sip_create_dialog_uas_locked(endpoint, rdata, dlg_status); |
| 1737 |
if (!dlg) { |
| 1738 |
if (*dlg_status != PJ_EEXISTS) { |
| 1739 |
ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n"); |
| 1740 |
} |
| 1741 |
ao2_ref(sub_tree, -1); |
| 1742 |
return NULL; |
| 1743 |
} |
| 1744 |
|
| 1745 |
persistence = ast_sip_mod_data_get(rdata->endpt_info.mod_data, |
| 1746 |
pubsub_module.id, MOD_DATA_PERSISTENCE); |
| 1747 |
if (persistence) { |
| 1748 |
|
| 1749 |
pjsip_ua_unregister_dlg(pjsip_ua_instance(), dlg); |
| 1750 |
pj_strdup2(dlg->pool, &dlg->local.info->tag, persistence->tag); |
| 1751 |
dlg->local.tag_hval = pj_hash_calc_tolower(0, NULL, &dlg->local.info->tag); |
| 1752 |
pjsip_ua_register_dlg(pjsip_ua_instance(), dlg); |
| 1753 |
dlg->local.cseq = persistence->cseq; |
| 1754 |
} |
| 1755 |
|
| 1756 |
pjsip_evsub_create_uas(dlg, &pubsub_cb, rdata, 0, &sub_tree->evsub); |
| 1757 |
|
| 1758 |
subscription_setup_dialog(sub_tree, dlg); |
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
pjsip_dlg_dec_lock(dlg); |
| 1766 |
|
| 1767 |
#ifdef HAVE_PJSIP_EVSUB_GRP_LOCK |
| 1768 |
pjsip_evsub_add_ref(sub_tree->evsub); |
| 1769 |
#endif |
| 1770 |
|
| 1771 |
ast_sip_mod_data_set(dlg->pool, dlg->mod_data, pubsub_module.id, MOD_DATA_MSG, |
| 1772 |
pjsip_msg_clone(dlg->pool, rdata->msg_info.msg)); |
| 1773 |
|
| 1774 |
sub_tree->notification_batch_interval = tree->notification_batch_interval; |
| 1775 |
|
| 1776 |
|
| 1777 |
sub_tree->persistence = ao2_bump(persistence); |
| 1778 |
|
| 1779 |
sub_tree->root = create_virtual_subscriptions(handler, resource, generator, sub_tree, tree->root); |
| 1780 |
if (AST_VECTOR_SIZE(&sub_tree->root->children) > 0) { |
| 1781 |
sub_tree->is_list = 1; |
| 1782 |
} |
| 1783 |
|
| 1784 |
add_subscription(sub_tree); |
| 1785 |
|
| 1786 |
return sub_tree; |
| 1787 |
} |
| 1788 |
|
| 1789 |
|
| 1790 |
struct initial_notify_data { |
| 1791 |
struct sip_subscription_tree *sub_tree; |
| 1792 |
unsigned int expires; |
| 1793 |
}; |
| 1794 |
|
| 1795 |
static int initial_notify_task(void *obj); |
| 1796 |
static int send_notify(struct sip_subscription_tree *sub_tree, unsigned int force_full_state); |
| 1797 |
|
| 1798 |
|
| 1799 |
struct persistence_recreate_data { |
| 1800 |
struct subscription_persistence *persistence; |
| 1801 |
pjsip_rx_data *rdata; |
| 1802 |
}; |
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
static int sub_persistence_recreate(void *obj) |
| 1813 |
{ |
| 1814 |
struct persistence_recreate_data *recreate_data = obj; |
| 1815 |
struct subscription_persistence *persistence = recreate_data->persistence; |
| 1816 |
pjsip_rx_data *rdata = recreate_data->rdata; |
| 1817 |
struct ast_sip_endpoint *endpoint; |
| 1818 |
struct sip_subscription_tree *sub_tree; |
| 1819 |
struct ast_sip_pubsub_body_generator *generator; |
| 1820 |
struct ast_sip_subscription_handler *handler; |
| 1821 |
char *resource; |
| 1822 |
size_t resource_size; |
| 1823 |
int resp; |
| 1824 |
struct resource_tree tree; |
| 1825 |
pjsip_expires_hdr *expires_header; |
| 1826 |
int64_t expires; |
| 1827 |
const pj_str_t *user; |
| 1828 |
|
| 1829 |
user = ast_sip_pjsip_uri_get_username(rdata->msg_info.msg->line.req.uri); |
| 1830 |
resource_size = pj_strlen(user) + 1; |
| 1831 |
resource = ast_alloca(resource_size); |
| 1832 |
ast_copy_pj_str(resource, user, resource_size); |
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(resource); |
| 1839 |
|
| 1840 |
handler = subscription_get_handler_from_rdata(rdata, persistence->endpoint); |
| 1841 |
if (!handler || !handler->notifier) { |
| 1842 |
ast_log(LOG_WARNING, "Failed recreating '%s' subscription: Could not get subscription handler.\n", |
| 1843 |
persistence->endpoint); |
| 1844 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1845 |
return 0; |
| 1846 |
} |
| 1847 |
|
| 1848 |
generator = subscription_get_generator_from_rdata(rdata, handler); |
| 1849 |
if (!generator) { |
| 1850 |
ast_log(LOG_WARNING, "Failed recreating '%s' subscription: Body generator not available.\n", |
| 1851 |
persistence->endpoint); |
| 1852 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1853 |
return 0; |
| 1854 |
} |
| 1855 |
|
| 1856 |
ast_sip_mod_data_set(rdata->tp_info.pool, rdata->endpt_info.mod_data, |
| 1857 |
pubsub_module.id, MOD_DATA_PERSISTENCE, persistence); |
| 1858 |
|
| 1859 |
|
| 1860 |
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", |
| 1861 |
persistence->endpoint); |
| 1862 |
if (!endpoint) { |
| 1863 |
ast_log(LOG_WARNING, "Failed recreating '%s' subscription: The endpoint was not found\n", |
| 1864 |
persistence->endpoint); |
| 1865 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1866 |
return 0; |
| 1867 |
} |
| 1868 |
|
| 1869 |
|
| 1870 |
expires_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, |
| 1871 |
rdata->msg_info.msg->hdr.next); |
| 1872 |
if (!expires_header) { |
| 1873 |
expires_header = pjsip_expires_hdr_create(rdata->tp_info.pool, 0); |
| 1874 |
if (!expires_header) { |
| 1875 |
ast_log(LOG_WARNING, "Failed recreating '%s' subscription: Could not update expires header.\n", |
| 1876 |
persistence->endpoint); |
| 1877 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1878 |
ao2_ref(endpoint, -1); |
| 1879 |
return 0; |
| 1880 |
} |
| 1881 |
pjsip_msg_add_hdr(rdata->msg_info.msg, (pjsip_hdr *) expires_header); |
| 1882 |
} |
| 1883 |
|
| 1884 |
expires = (ast_tvdiff_ms(persistence->expires, ast_tvnow()) / 1000); |
| 1885 |
if (expires <= 0) { |
| 1886 |
|
| 1887 |
ast_debug(3, "Expired subscription retrived from persistent store '%s' %s\n", |
| 1888 |
persistence->endpoint, persistence->tag); |
| 1889 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1890 |
ao2_ref(endpoint, -1); |
| 1891 |
return 0; |
| 1892 |
} |
| 1893 |
expires_header->ivalue = expires; |
| 1894 |
|
| 1895 |
memset(&tree, 0, sizeof(tree)); |
| 1896 |
resp = build_resource_tree(endpoint, handler, resource, &tree, |
| 1897 |
ast_sip_pubsub_has_eventlist_support(rdata), rdata); |
| 1898 |
if (PJSIP_IS_STATUS_IN_CLASS(resp, 200)) { |
| 1899 |
pj_status_t dlg_status; |
| 1900 |
|
| 1901 |
sub_tree = create_subscription_tree(handler, endpoint, rdata, resource, generator, |
| 1902 |
&tree, &dlg_status, persistence); |
| 1903 |
if (!sub_tree) { |
| 1904 |
if (dlg_status != PJ_EEXISTS) { |
| 1905 |
ast_log(LOG_WARNING, "Failed recreating '%s' subscription: Could not create subscription tree.\n", |
| 1906 |
persistence->endpoint); |
| 1907 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1908 |
} |
| 1909 |
} else { |
| 1910 |
struct initial_notify_data *ind = ast_malloc(sizeof(*ind)); |
| 1911 |
|
| 1912 |
if (!ind) { |
| 1913 |
pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE); |
| 1914 |
goto error; |
| 1915 |
} |
| 1916 |
|
| 1917 |
ind->sub_tree = ao2_bump(sub_tree); |
| 1918 |
ind->expires = expires_header->ivalue; |
| 1919 |
|
| 1920 |
subscription_persistence_update(sub_tree, rdata, SUBSCRIPTION_PERSISTENCE_RECREATED); |
| 1921 |
if (ast_sip_push_task(sub_tree->serializer, initial_notify_task, ind)) { |
| 1922 |
|
| 1923 |
pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE); |
| 1924 |
ao2_ref(sub_tree, -1); |
| 1925 |
ast_free(ind); |
| 1926 |
} |
| 1927 |
} |
| 1928 |
} else { |
| 1929 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1930 |
} |
| 1931 |
|
| 1932 |
error: |
| 1933 |
resource_tree_destroy(&tree); |
| 1934 |
ao2_ref(endpoint, -1); |
| 1935 |
|
| 1936 |
return 0; |
| 1937 |
} |
| 1938 |
|
| 1939 |
|
| 1940 |
static int subscription_persistence_recreate(void *obj, void *arg, int flags) |
| 1941 |
{ |
| 1942 |
struct subscription_persistence *persistence = obj; |
| 1943 |
pj_pool_t *pool = arg; |
| 1944 |
struct ast_taskprocessor *serializer; |
| 1945 |
pjsip_rx_data rdata; |
| 1946 |
struct persistence_recreate_data recreate_data; |
| 1947 |
|
| 1948 |
|
| 1949 |
if (persistence->prune_on_boot) { |
| 1950 |
ast_debug(3, "Deleting subscription marked as 'prune' from persistent store '%s' %s\n", |
| 1951 |
persistence->endpoint, persistence->tag); |
| 1952 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1953 |
return 0; |
| 1954 |
} |
| 1955 |
|
| 1956 |
|
| 1957 |
if (ast_tvdiff_ms(persistence->expires, ast_tvnow()) <= 0) { |
| 1958 |
ast_debug(3, "Expired subscription retrived from persistent store '%s' %s\n", |
| 1959 |
persistence->endpoint, persistence->tag); |
| 1960 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1961 |
return 0; |
| 1962 |
} |
| 1963 |
|
| 1964 |
memset(&rdata, 0, sizeof(rdata)); |
| 1965 |
pj_pool_reset(pool); |
| 1966 |
rdata.tp_info.pool = pool; |
| 1967 |
|
| 1968 |
if (ast_sip_create_rdata_with_contact(&rdata, persistence->packet, persistence->src_name, |
| 1969 |
persistence->src_port, persistence->transport_type, persistence->local_name, |
| 1970 |
persistence->local_port, persistence->contact_uri)) { |
| 1971 |
ast_log(LOG_WARNING, "Failed recreating '%s' subscription: The message could not be parsed\n", |
| 1972 |
persistence->endpoint); |
| 1973 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1974 |
return 0; |
| 1975 |
} |
| 1976 |
|
| 1977 |
if (rdata.msg_info.msg->type != PJSIP_REQUEST_MSG) { |
| 1978 |
ast_log(LOG_NOTICE, "Failed recreating '%s' subscription: Stored a SIP response instead of a request.\n", |
| 1979 |
persistence->endpoint); |
| 1980 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1981 |
return 0; |
| 1982 |
} |
| 1983 |
|
| 1984 |
|
| 1985 |
serializer = ast_sip_get_distributor_serializer(&rdata); |
| 1986 |
if (!serializer) { |
| 1987 |
ast_log(LOG_WARNING, "Failed recreating '%s' subscription: Could not get distributor serializer.\n", |
| 1988 |
persistence->endpoint); |
| 1989 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1990 |
return 0; |
| 1991 |
} |
| 1992 |
recreate_data.persistence = persistence; |
| 1993 |
recreate_data.rdata = &rdata; |
| 1994 |
if (ast_sip_push_task_wait_serializer(serializer, sub_persistence_recreate, |
| 1995 |
&recreate_data)) { |
| 1996 |
ast_log(LOG_WARNING, "Failed recreating '%s' subscription: Could not continue under distributor serializer.\n", |
| 1997 |
persistence->endpoint); |
| 1998 |
ast_sorcery_delete(ast_sip_get_sorcery(), persistence); |
| 1999 |
} |
| 2000 |
ast_taskprocessor_unreference(serializer); |
| 2001 |
|
| 2002 |
return 0; |
| 2003 |
} |
| 2004 |
|
| 2005 |
|
| 2006 |
static int subscription_persistence_load(void *data) |
| 2007 |
{ |
| 2008 |
struct ao2_container *persisted_subscriptions = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), |
| 2009 |
"subscription_persistence", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 2010 |
pj_pool_t *pool; |
| 2011 |
|
| 2012 |
pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "rtd%p", PJSIP_POOL_RDATA_LEN, |
| 2013 |
PJSIP_POOL_RDATA_INC); |
| 2014 |
if (!pool) { |
| 2015 |
ast_log(LOG_WARNING, "Could not create a memory pool for recreating SIP subscriptions\n"); |
| 2016 |
return 0; |
| 2017 |
} |
| 2018 |
|
| 2019 |
ao2_callback(persisted_subscriptions, OBJ_NODATA, subscription_persistence_recreate, pool); |
| 2020 |
|
| 2021 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool); |
| 2022 |
|
| 2023 |
ao2_ref(persisted_subscriptions, -1); |
| 2024 |
return 0; |
| 2025 |
} |
| 2026 |
|
| 2027 |
|
| 2028 |
static void subscription_persistence_event_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 2029 |
{ |
| 2030 |
struct ast_json_payload *payload; |
| 2031 |
const char *type; |
| 2032 |
|
| 2033 |
if (stasis_message_type(message) != ast_manager_get_generic_type()) { |
| 2034 |
return; |
| 2035 |
} |
| 2036 |
|
| 2037 |
payload = stasis_message_data(message); |
| 2038 |
type = ast_json_string_get(ast_json_object_get(payload->json, "type")); |
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
if (strcmp(type, "FullyBooted")) { |
| 2044 |
return; |
| 2045 |
} |
| 2046 |
|
| 2047 |
|
| 2048 |
ast_sip_push_task(NULL, subscription_persistence_load, NULL); |
| 2049 |
|
| 2050 |
|
| 2051 |
stasis_unsubscribe(sub); |
| 2052 |
} |
| 2053 |
|
| 2054 |
typedef int (*on_subscription_t)(struct sip_subscription_tree *sub, void *arg); |
| 2055 |
|
| 2056 |
static int for_each_subscription(on_subscription_t on_subscription, void *arg) |
| 2057 |
{ |
| 2058 |
int num = 0; |
| 2059 |
struct sip_subscription_tree *i; |
| 2060 |
|
| 2061 |
if (!on_subscription) { |
| 2062 |
return num; |
| 2063 |
} |
| 2064 |
|
| 2065 |
AST_RWLIST_RDLOCK(&subscriptions); |
| 2066 |
AST_RWLIST_TRAVERSE(&subscriptions, i, next) { |
| 2067 |
if (on_subscription(i, arg)) { |
| 2068 |
break; |
| 2069 |
} |
| 2070 |
++num; |
| 2071 |
} |
| 2072 |
AST_RWLIST_UNLOCK(&subscriptions); |
| 2073 |
return num; |
| 2074 |
} |
| 2075 |
|
| 2076 |
static void sip_subscription_to_ami(struct sip_subscription_tree *sub_tree, |
| 2077 |
struct ast_str **buf) |
| 2078 |
{ |
| 2079 |
char str[256]; |
| 2080 |
struct ast_sip_endpoint_id_configuration *id = &sub_tree->endpoint->id; |
| 2081 |
|
| 2082 |
ast_str_append(buf, 0, "Role: %s\r\n", |
| 2083 |
sip_subscription_roles_map[sub_tree->role]); |
| 2084 |
ast_str_append(buf, 0, "Endpoint: %s\r\n", |
| 2085 |
ast_sorcery_object_get_id(sub_tree->endpoint)); |
| 2086 |
|
| 2087 |
if (sub_tree->dlg) { |
| 2088 |
ast_copy_pj_str(str, &sub_tree->dlg->call_id->id, sizeof(str)); |
| 2089 |
} else { |
| 2090 |
ast_copy_string(str, "<unknown>", sizeof(str)); |
| 2091 |
} |
| 2092 |
ast_str_append(buf, 0, "Callid: %s\r\n", str); |
| 2093 |
|
| 2094 |
ast_str_append(buf, 0, "State: %s\r\n", pjsip_evsub_get_state_name(sub_tree->evsub)); |
| 2095 |
|
| 2096 |
ast_callerid_merge(str, sizeof(str), |
| 2097 |
S_COR(id->self.name.valid, id->self.name.str, NULL), |
| 2098 |
S_COR(id->self.number.valid, id->self.number.str, NULL), |
| 2099 |
"Unknown"); |
| 2100 |
|
| 2101 |
ast_str_append(buf, 0, "Callerid: %s\r\n", str); |
| 2102 |
|
| 2103 |
|
| 2104 |
if (sub_tree->root->handler->to_ami) { |
| 2105 |
sub_tree->root->handler->to_ami(sub_tree->root, buf); |
| 2106 |
} |
| 2107 |
} |
| 2108 |
|
| 2109 |
|
| 2110 |
void *ast_sip_subscription_get_header(const struct ast_sip_subscription *sub, const char *header) |
| 2111 |
{ |
| 2112 |
pjsip_dialog *dlg; |
| 2113 |
pjsip_msg *msg; |
| 2114 |
pj_str_t name; |
| 2115 |
|
| 2116 |
dlg = sub->tree->dlg; |
| 2117 |
msg = ast_sip_mod_data_get(dlg->mod_data, pubsub_module.id, MOD_DATA_MSG); |
| 2118 |
pj_cstr(&name, header); |
| 2119 |
|
| 2120 |
return pjsip_msg_find_hdr_by_name(msg, &name, NULL); |
| 2121 |
} |
| 2122 |
|
| 2123 |
|
| 2124 |
struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler, |
| 2125 |
struct ast_sip_endpoint *endpoint, const char *resource) |
| 2126 |
{ |
| 2127 |
struct ast_sip_subscription *sub; |
| 2128 |
pjsip_dialog *dlg; |
| 2129 |
struct ast_sip_contact *contact; |
| 2130 |
pj_str_t event; |
| 2131 |
pjsip_tx_data *tdata; |
| 2132 |
pjsip_evsub *evsub; |
| 2133 |
struct sip_subscription_tree *sub_tree = NULL; |
| 2134 |
|
| 2135 |
sub_tree = allocate_subscription_tree(endpoint, NULL); |
| 2136 |
if (!sub_tree) { |
| 2137 |
return NULL; |
| 2138 |
} |
| 2139 |
|
| 2140 |
sub = allocate_subscription(handler, resource, NULL, sub_tree); |
| 2141 |
if (!sub) { |
| 2142 |
ao2_cleanup(sub_tree); |
| 2143 |
return NULL; |
| 2144 |
} |
| 2145 |
|
| 2146 |
contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors); |
| 2147 |
if (!contact || ast_strlen_zero(contact->uri)) { |
| 2148 |
ast_log(LOG_WARNING, "No contacts configured for endpoint %s. Unable to create SIP subsription\n", |
| 2149 |
ast_sorcery_object_get_id(endpoint)); |
| 2150 |
ao2_ref(sub_tree, -1); |
| 2151 |
ao2_cleanup(contact); |
| 2152 |
return NULL; |
| 2153 |
} |
| 2154 |
|
| 2155 |
dlg = ast_sip_create_dialog_uac(endpoint, contact->uri, NULL); |
| 2156 |
ao2_cleanup(contact); |
| 2157 |
if (!dlg) { |
| 2158 |
ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n"); |
| 2159 |
ao2_ref(sub_tree, -1); |
| 2160 |
return NULL; |
| 2161 |
} |
| 2162 |
|
| 2163 |
pj_cstr(&event, handler->event_name); |
| 2164 |
pjsip_evsub_create_uac(dlg, &pubsub_cb, &event, 0, &sub_tree->evsub); |
| 2165 |
subscription_setup_dialog(sub_tree, dlg); |
| 2166 |
|
| 2167 |
evsub = sub_tree->evsub; |
| 2168 |
|
| 2169 |
if (pjsip_evsub_initiate(evsub, NULL, -1, &tdata) == PJ_SUCCESS) { |
| 2170 |
pjsip_evsub_send_request(sub_tree->evsub, tdata); |
| 2171 |
} else { |
| 2172 |
|
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
pjsip_evsub_terminate(evsub, PJ_TRUE); |
| 2177 |
ao2_ref(sub_tree, -1); |
| 2178 |
return NULL; |
| 2179 |
} |
| 2180 |
|
| 2181 |
add_subscription(sub_tree); |
| 2182 |
|
| 2183 |
return sub; |
| 2184 |
} |
| 2185 |
|
| 2186 |
pjsip_dialog *ast_sip_subscription_get_dialog(struct ast_sip_subscription *sub) |
| 2187 |
{ |
| 2188 |
ast_assert(sub->tree->dlg != NULL); |
| 2189 |
return sub->tree->dlg; |
| 2190 |
} |
| 2191 |
|
| 2192 |
struct ast_sip_endpoint *ast_sip_subscription_get_endpoint(struct ast_sip_subscription *sub) |
| 2193 |
{ |
| 2194 |
ast_assert(sub->tree->endpoint != NULL); |
| 2195 |
return ao2_bump(sub->tree->endpoint); |
| 2196 |
} |
| 2197 |
|
| 2198 |
struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub) |
| 2199 |
{ |
| 2200 |
ast_assert(sub->tree->serializer != NULL); |
| 2201 |
return sub->tree->serializer; |
| 2202 |
} |
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
|
| 2211 |
|
| 2212 |
|
| 2213 |
|
| 2214 |
|
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
static int allocate_tdata_buffer(pjsip_tx_data *tdata) |
| 2224 |
{ |
| 2225 |
int buf_size; |
| 2226 |
int size = -1; |
| 2227 |
char *buf; |
| 2228 |
|
| 2229 |
for (buf_size = PJSIP_MAX_PKT_LEN; size == -1 && buf_size < (PJSIP_MAX_PKT_LEN * 2); buf_size *= 2) { |
| 2230 |
buf = pj_pool_alloc(tdata->pool, buf_size); |
| 2231 |
size = pjsip_msg_print(tdata->msg, buf, buf_size); |
| 2232 |
} |
| 2233 |
|
| 2234 |
if (size == -1) { |
| 2235 |
return -1; |
| 2236 |
} |
| 2237 |
|
| 2238 |
tdata->buf.start = buf; |
| 2239 |
tdata->buf.cur = tdata->buf.start; |
| 2240 |
tdata->buf.end = tdata->buf.start + buf_size; |
| 2241 |
|
| 2242 |
return 0; |
| 2243 |
} |
| 2244 |
|
| 2245 |
static int sip_subscription_send_request(struct sip_subscription_tree *sub_tree, pjsip_tx_data *tdata) |
| 2246 |
{ |
| 2247 |
#ifdef TEST_FRAMEWORK |
| 2248 |
struct ast_sip_endpoint *endpoint = sub_tree->endpoint; |
| 2249 |
pjsip_evsub *evsub = sub_tree->evsub; |
| 2250 |
#endif |
| 2251 |
int res; |
| 2252 |
|
| 2253 |
if (allocate_tdata_buffer(tdata)) { |
| 2254 |
ast_log(LOG_ERROR, "SIP request %s is too large to send.\n", tdata->info); |
| 2255 |
pjsip_tx_data_dec_ref(tdata); |
| 2256 |
return -1; |
| 2257 |
} |
| 2258 |
|
| 2259 |
res = pjsip_evsub_send_request(sub_tree->evsub, tdata); |
| 2260 |
|
| 2261 |
subscription_persistence_update(sub_tree, NULL, SUBSCRIPTION_PERSISTENCE_SEND_REQUEST); |
| 2262 |
|
| 2263 |
ast_test_suite_event_notify("SUBSCRIPTION_STATE_SET", |
| 2264 |
"StateText: %s\r\n" |
| 2265 |
"Endpoint: %s\r\n", |
| 2266 |
pjsip_evsub_get_state_name(evsub), |
| 2267 |
ast_sorcery_object_get_id(endpoint)); |
| 2268 |
|
| 2269 |
return (res == PJ_SUCCESS ? 0 : -1); |
| 2270 |
} |
| 2271 |
|
| 2272 |
|
| 2273 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
static void add_rlmi_resource(pj_pool_t *pool, pj_xml_node *rlmi, const pjsip_generic_string_hdr *cid, |
| 2287 |
const char *resource_name, const pjsip_sip_uri *resource_uri, pjsip_evsub_state state) |
| 2288 |
{ |
| 2289 |
static pj_str_t cid_name = { "cid", 3 }; |
| 2290 |
pj_xml_node *resource; |
| 2291 |
pj_xml_node *name; |
| 2292 |
pj_xml_node *instance; |
| 2293 |
pj_xml_attr *cid_attr; |
| 2294 |
char id[6]; |
| 2295 |
char uri[PJSIP_MAX_URL_SIZE]; |
| 2296 |
char name_sanitized[PJSIP_MAX_URL_SIZE]; |
| 2297 |
|
| 2298 |
|
| 2299 |
const pj_str_t cid_stripped = { |
| 2300 |
.ptr = cid->hvalue.ptr + 1, |
| 2301 |
.slen = cid->hvalue.slen - 2, |
| 2302 |
}; |
| 2303 |
|
| 2304 |
resource = ast_sip_presence_xml_create_node(pool, rlmi, "resource"); |
| 2305 |
name = ast_sip_presence_xml_create_node(pool, resource, "name"); |
| 2306 |
instance = ast_sip_presence_xml_create_node(pool, resource, "instance"); |
| 2307 |
|
| 2308 |
pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, resource_uri, uri, sizeof(uri)); |
| 2309 |
ast_sip_presence_xml_create_attr(pool, resource, "uri", uri); |
| 2310 |
|
| 2311 |
ast_sip_sanitize_xml(resource_name, name_sanitized, sizeof(name_sanitized)); |
| 2312 |
pj_strdup2(pool, &name->content, name_sanitized); |
| 2313 |
|
| 2314 |
ast_generate_random_string(id, sizeof(id)); |
| 2315 |
|
| 2316 |
ast_sip_presence_xml_create_attr(pool, instance, "id", id); |
| 2317 |
ast_sip_presence_xml_create_attr(pool, instance, "state", |
| 2318 |
state == PJSIP_EVSUB_STATE_TERMINATED ? "terminated" : "active"); |
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
cid_attr = pj_xml_attr_new(pool, &cid_name, &cid_stripped); |
| 2325 |
pj_xml_add_attr(instance, cid_attr); |
| 2326 |
} |
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
|
| 2331 |
|
| 2332 |
|
| 2333 |
|
| 2334 |
|
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
|
| 2339 |
struct body_part { |
| 2340 |
|
| 2341 |
pjsip_generic_string_hdr *cid; |
| 2342 |
|
| 2343 |
const char *resource; |
| 2344 |
|
| 2345 |
pjsip_sip_uri *uri; |
| 2346 |
|
| 2347 |
pjsip_evsub_state state; |
| 2348 |
|
| 2349 |
pjsip_multipart_part *part; |
| 2350 |
|
| 2351 |
const char *display_name; |
| 2352 |
}; |
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
AST_VECTOR(body_part_list, struct body_part *); |
| 2358 |
|
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
|
| 2368 |
static pjsip_generic_string_hdr *generate_content_id_hdr(pj_pool_t *pool, |
| 2369 |
const struct ast_sip_subscription *sub) |
| 2370 |
{ |
| 2371 |
static const pj_str_t cid_name = { "Content-ID", 10 }; |
| 2372 |
pjsip_generic_string_hdr *cid; |
| 2373 |
char id[6]; |
| 2374 |
size_t alloc_size; |
| 2375 |
pj_str_t cid_value; |
| 2376 |
|
| 2377 |
|
| 2378 |
alloc_size = sizeof(id) + pj_strlen(&sub->uri->host) + 3; |
| 2379 |
cid_value.ptr = pj_pool_alloc(pool, alloc_size); |
| 2380 |
cid_value.slen = sprintf(cid_value.ptr, "<%s@%.*s>", |
| 2381 |
ast_generate_random_string(id, sizeof(id)), |
| 2382 |
(int) pj_strlen(&sub->uri->host), pj_strbuf(&sub->uri->host)); |
| 2383 |
cid = pjsip_generic_string_hdr_create(pool, &cid_name, &cid_value); |
| 2384 |
|
| 2385 |
return cid; |
| 2386 |
} |
| 2387 |
|
| 2388 |
static int rlmi_print_body(struct pjsip_msg_body *msg_body, char *buf, pj_size_t size) |
| 2389 |
{ |
| 2390 |
int num_printed; |
| 2391 |
pj_xml_node *rlmi = msg_body->data; |
| 2392 |
|
| 2393 |
num_printed = pj_xml_print(rlmi, buf, size, PJ_TRUE); |
| 2394 |
if (num_printed <= AST_PJSIP_XML_PROLOG_LEN) { |
| 2395 |
return -1; |
| 2396 |
} |
| 2397 |
|
| 2398 |
return num_printed; |
| 2399 |
} |
| 2400 |
|
| 2401 |
static void *rlmi_clone_data(pj_pool_t *pool, const void *data, unsigned len) |
| 2402 |
{ |
| 2403 |
const pj_xml_node *rlmi = data; |
| 2404 |
|
| 2405 |
return pj_xml_clone(pool, rlmi); |
| 2406 |
} |
| 2407 |
|
| 2408 |
|
| 2409 |
|
| 2410 |
|
| 2411 |
|
| 2412 |
|
| 2413 |
|
| 2414 |
|
| 2415 |
|
| 2416 |
|
| 2417 |
|
| 2418 |
|
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
static pjsip_multipart_part *build_rlmi_body(pj_pool_t *pool, struct ast_sip_subscription *sub, |
| 2424 |
struct body_part_list *body_parts, unsigned int full_state) |
| 2425 |
{ |
| 2426 |
pj_xml_node *rlmi; |
| 2427 |
pj_xml_node *name; |
| 2428 |
pjsip_multipart_part *rlmi_part; |
| 2429 |
char version_str[32]; |
| 2430 |
char uri[PJSIP_MAX_URL_SIZE]; |
| 2431 |
pjsip_generic_string_hdr *cid; |
| 2432 |
int i; |
| 2433 |
|
| 2434 |
rlmi = ast_sip_presence_xml_create_node(pool, NULL, "list"); |
| 2435 |
ast_sip_presence_xml_create_attr(pool, rlmi, "xmlns", "urn:ietf:params:xml:ns:rlmi"); |
| 2436 |
|
| 2437 |
ast_sip_subscription_get_local_uri(sub, uri, sizeof(uri)); |
| 2438 |
ast_sip_presence_xml_create_attr(pool, rlmi, "uri", uri); |
| 2439 |
|
| 2440 |
snprintf(version_str, sizeof(version_str), "%u", sub->version++); |
| 2441 |
ast_sip_presence_xml_create_attr(pool, rlmi, "version", version_str); |
| 2442 |
ast_sip_presence_xml_create_attr(pool, rlmi, "fullState", full_state ? "true" : "false"); |
| 2443 |
|
| 2444 |
name = ast_sip_presence_xml_create_node(pool, rlmi, "name"); |
| 2445 |
pj_strdup2(pool, &name->content, ast_sip_subscription_get_resource_name(sub)); |
| 2446 |
|
| 2447 |
for (i = 0; i < AST_VECTOR_SIZE(body_parts); ++i) { |
| 2448 |
const struct body_part *part = AST_VECTOR_GET(body_parts, i); |
| 2449 |
|
| 2450 |
add_rlmi_resource(pool, rlmi, part->cid, S_OR(part->display_name, part->resource), part->uri, part->state); |
| 2451 |
} |
| 2452 |
|
| 2453 |
rlmi_part = pjsip_multipart_create_part(pool); |
| 2454 |
|
| 2455 |
rlmi_part->body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body); |
| 2456 |
pjsip_media_type_cp(pool, &rlmi_part->body->content_type, &rlmi_media_type); |
| 2457 |
|
| 2458 |
rlmi_part->body->data = pj_xml_clone(pool, rlmi); |
| 2459 |
rlmi_part->body->clone_data = rlmi_clone_data; |
| 2460 |
rlmi_part->body->print_body = rlmi_print_body; |
| 2461 |
|
| 2462 |
cid = generate_content_id_hdr(pool, sub); |
| 2463 |
pj_list_insert_before(&rlmi_part->hdr, cid); |
| 2464 |
|
| 2465 |
return rlmi_part; |
| 2466 |
} |
| 2467 |
|
| 2468 |
static pjsip_msg_body *generate_notify_body(pj_pool_t *pool, struct ast_sip_subscription *root, |
| 2469 |
unsigned int force_full_state); |
| 2470 |
|
| 2471 |
|
| 2472 |
|
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
static void free_body_parts(struct body_part_list *parts) |
| 2477 |
{ |
| 2478 |
int i; |
| 2479 |
|
| 2480 |
for (i = 0; i < AST_VECTOR_SIZE(parts); ++i) { |
| 2481 |
struct body_part *part = AST_VECTOR_GET(parts, i); |
| 2482 |
ast_free(part); |
| 2483 |
} |
| 2484 |
|
| 2485 |
AST_VECTOR_FREE(parts); |
| 2486 |
} |
| 2487 |
|
| 2488 |
|
| 2489 |
|
| 2490 |
|
| 2491 |
|
| 2492 |
|
| 2493 |
|
| 2494 |
static struct body_part *allocate_body_part(pj_pool_t *pool, const struct ast_sip_subscription *sub) |
| 2495 |
{ |
| 2496 |
struct body_part *bp; |
| 2497 |
|
| 2498 |
bp = ast_calloc(1, sizeof(*bp)); |
| 2499 |
if (!bp) { |
| 2500 |
return NULL; |
| 2501 |
} |
| 2502 |
|
| 2503 |
bp->cid = generate_content_id_hdr(pool, sub); |
| 2504 |
bp->resource = sub->resource; |
| 2505 |
bp->state = sub->subscription_state; |
| 2506 |
bp->uri = sub->uri; |
| 2507 |
bp->display_name = sub->display_name; |
| 2508 |
|
| 2509 |
return bp; |
| 2510 |
} |
| 2511 |
|
| 2512 |
|
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
static void build_body_part(pj_pool_t *pool, struct ast_sip_subscription *sub, |
| 2521 |
struct body_part_list *parts, unsigned int use_full_state) |
| 2522 |
{ |
| 2523 |
struct body_part *bp; |
| 2524 |
pjsip_msg_body *body; |
| 2525 |
|
| 2526 |
bp = allocate_body_part(pool, sub); |
| 2527 |
if (!bp) { |
| 2528 |
return; |
| 2529 |
} |
| 2530 |
|
| 2531 |
body = generate_notify_body(pool, sub, use_full_state); |
| 2532 |
if (!body) { |
| 2533 |
|
| 2534 |
ast_free(bp); |
| 2535 |
return; |
| 2536 |
} |
| 2537 |
|
| 2538 |
bp->part = pjsip_multipart_create_part(pool); |
| 2539 |
bp->part->body = body; |
| 2540 |
pj_list_insert_before(&bp->part->hdr, bp->cid); |
| 2541 |
|
| 2542 |
if (AST_VECTOR_APPEND(parts, bp)) { |
| 2543 |
ast_free(bp); |
| 2544 |
} |
| 2545 |
} |
| 2546 |
|
| 2547 |
|
| 2548 |
|
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
static pjsip_msg_body *create_multipart_body(pj_pool_t *pool) |
| 2554 |
{ |
| 2555 |
pjsip_media_type media_type; |
| 2556 |
pjsip_param *media_type_param; |
| 2557 |
char boundary[6]; |
| 2558 |
pj_str_t pj_boundary; |
| 2559 |
|
| 2560 |
pjsip_media_type_init2(&media_type, "multipart", "related"); |
| 2561 |
|
| 2562 |
media_type_param = pj_pool_alloc(pool, sizeof(*media_type_param)); |
| 2563 |
pj_list_init(media_type_param); |
| 2564 |
|
| 2565 |
pj_strdup2(pool, &media_type_param->name, "type"); |
| 2566 |
pj_strdup2(pool, &media_type_param->value, "\"application/rlmi+xml\""); |
| 2567 |
|
| 2568 |
pj_list_insert_before(&media_type.param, media_type_param); |
| 2569 |
|
| 2570 |
pj_cstr(&pj_boundary, ast_generate_random_string(boundary, sizeof(boundary))); |
| 2571 |
return pjsip_multipart_create(pool, &media_type, &pj_boundary); |
| 2572 |
} |
| 2573 |
|
| 2574 |
|
| 2575 |
|
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
|
| 2580 |
|
| 2581 |
|
| 2582 |
|
| 2583 |
|
| 2584 |
|
| 2585 |
|
| 2586 |
static pjsip_msg_body *generate_list_body(pj_pool_t *pool, struct ast_sip_subscription *sub, |
| 2587 |
unsigned int force_full_state) |
| 2588 |
{ |
| 2589 |
int i; |
| 2590 |
pjsip_multipart_part *rlmi_part; |
| 2591 |
pjsip_msg_body *multipart; |
| 2592 |
struct body_part_list body_parts; |
| 2593 |
unsigned int use_full_state = force_full_state ? 1 : sub->full_state; |
| 2594 |
|
| 2595 |
if (AST_VECTOR_INIT(&body_parts, AST_VECTOR_SIZE(&sub->children))) { |
| 2596 |
return NULL; |
| 2597 |
} |
| 2598 |
|
| 2599 |
for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) { |
| 2600 |
build_body_part(pool, AST_VECTOR_GET(&sub->children, i), &body_parts, use_full_state); |
| 2601 |
} |
| 2602 |
|
| 2603 |
|
| 2604 |
if (AST_VECTOR_SIZE(&body_parts) == 0) { |
| 2605 |
free_body_parts(&body_parts); |
| 2606 |
return NULL; |
| 2607 |
} |
| 2608 |
|
| 2609 |
multipart = create_multipart_body(pool); |
| 2610 |
|
| 2611 |
rlmi_part = build_rlmi_body(pool, sub, &body_parts, use_full_state); |
| 2612 |
if (!rlmi_part) { |
| 2613 |
free_body_parts(&body_parts); |
| 2614 |
return NULL; |
| 2615 |
} |
| 2616 |
pjsip_multipart_add_part(pool, multipart, rlmi_part); |
| 2617 |
|
| 2618 |
for (i = 0; i < AST_VECTOR_SIZE(&body_parts); ++i) { |
| 2619 |
pjsip_multipart_add_part(pool, multipart, AST_VECTOR_GET(&body_parts, i)->part); |
| 2620 |
} |
| 2621 |
|
| 2622 |
free_body_parts(&body_parts); |
| 2623 |
return multipart; |
| 2624 |
} |
| 2625 |
|
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
static pjsip_msg_body *generate_notify_body(pj_pool_t *pool, struct ast_sip_subscription *root, |
| 2634 |
unsigned int force_full_state) |
| 2635 |
{ |
| 2636 |
pjsip_msg_body *body; |
| 2637 |
|
| 2638 |
if (AST_VECTOR_SIZE(&root->children) == 0) { |
| 2639 |
if (force_full_state || root->body_changed) { |
| 2640 |
|
| 2641 |
|
| 2642 |
|
| 2643 |
pj_str_t type; |
| 2644 |
pj_str_t subtype; |
| 2645 |
pj_str_t text; |
| 2646 |
|
| 2647 |
pj_cstr(&type, ast_sip_subscription_get_body_type(root)); |
| 2648 |
pj_cstr(&subtype, ast_sip_subscription_get_body_subtype(root)); |
| 2649 |
pj_cstr(&text, ast_str_buffer(root->body_text)); |
| 2650 |
|
| 2651 |
body = pjsip_msg_body_create(pool, &type, &subtype, &text); |
| 2652 |
root->body_changed = 0; |
| 2653 |
} else { |
| 2654 |
body = NULL; |
| 2655 |
} |
| 2656 |
} else { |
| 2657 |
body = generate_list_body(pool, root, force_full_state); |
| 2658 |
} |
| 2659 |
|
| 2660 |
return body; |
| 2661 |
} |
| 2662 |
|
| 2663 |
|
| 2664 |
|
| 2665 |
|
| 2666 |
static pjsip_require_hdr *create_require_eventlist(pj_pool_t *pool) |
| 2667 |
{ |
| 2668 |
pjsip_require_hdr *require; |
| 2669 |
|
| 2670 |
require = pjsip_require_hdr_create(pool); |
| 2671 |
pj_strdup2(pool, &require->values[0], "eventlist"); |
| 2672 |
require->count = 1; |
| 2673 |
|
| 2674 |
return require; |
| 2675 |
} |
| 2676 |
|
| 2677 |
static void set_state_terminated(struct ast_sip_subscription *sub) |
| 2678 |
{ |
| 2679 |
int i; |
| 2680 |
|
| 2681 |
sub->subscription_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 2682 |
for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) { |
| 2683 |
set_state_terminated(AST_VECTOR_GET(&sub->children, i)); |
| 2684 |
} |
| 2685 |
} |
| 2686 |
|
| 2687 |
|
| 2688 |
|
| 2689 |
|
| 2690 |
|
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
|
| 2695 |
|
| 2696 |
|
| 2697 |
static int send_notify(struct sip_subscription_tree *sub_tree, unsigned int force_full_state) |
| 2698 |
{ |
| 2699 |
pjsip_evsub *evsub = sub_tree->evsub; |
| 2700 |
pjsip_tx_data *tdata; |
| 2701 |
|
| 2702 |
if (ast_shutdown_final() |
| 2703 |
&& sub_tree->root->subscription_state == PJSIP_EVSUB_STATE_TERMINATED |
| 2704 |
&& sub_tree->persistence) { |
| 2705 |
return 0; |
| 2706 |
} |
| 2707 |
|
| 2708 |
if (pjsip_evsub_notify(evsub, sub_tree->root->subscription_state, |
| 2709 |
NULL, NULL, &tdata) != PJ_SUCCESS) { |
| 2710 |
return -1; |
| 2711 |
} |
| 2712 |
|
| 2713 |
tdata->msg->body = generate_notify_body(tdata->pool, sub_tree->root, force_full_state); |
| 2714 |
if (!tdata->msg->body) { |
| 2715 |
pjsip_tx_data_dec_ref(tdata); |
| 2716 |
return -1; |
| 2717 |
} |
| 2718 |
|
| 2719 |
if (sub_tree->is_list) { |
| 2720 |
pjsip_require_hdr *require = create_require_eventlist(tdata->pool); |
| 2721 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) require); |
| 2722 |
} |
| 2723 |
|
| 2724 |
if (sub_tree->root->handler->notifier->notify_created) { |
| 2725 |
|
| 2726 |
|
| 2727 |
sub_tree->root->handler->notifier->notify_created(sub_tree->root, tdata); |
| 2728 |
} |
| 2729 |
|
| 2730 |
if (sip_subscription_send_request(sub_tree, tdata)) { |
| 2731 |
|
| 2732 |
return -1; |
| 2733 |
} |
| 2734 |
|
| 2735 |
sub_tree->send_scheduled_notify = 0; |
| 2736 |
|
| 2737 |
return 0; |
| 2738 |
} |
| 2739 |
|
| 2740 |
static int serialized_send_notify(void *userdata) |
| 2741 |
{ |
| 2742 |
struct sip_subscription_tree *sub_tree = userdata; |
| 2743 |
pjsip_dialog *dlg = sub_tree->dlg; |
| 2744 |
|
| 2745 |
pjsip_dlg_inc_lock(dlg); |
| 2746 |
|
| 2747 |
sub_tree->notify_sched_id = -1; |
| 2748 |
|
| 2749 |
|
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
|
| 2755 |
|
| 2756 |
if (sub_tree->state >= SIP_SUB_TREE_TERMINATE_IN_PROGRESS |
| 2757 |
|| !sub_tree->send_scheduled_notify) { |
| 2758 |
pjsip_dlg_dec_lock(dlg); |
| 2759 |
ao2_cleanup(sub_tree); |
| 2760 |
return 0; |
| 2761 |
} |
| 2762 |
|
| 2763 |
if (sub_tree->root->subscription_state == PJSIP_EVSUB_STATE_TERMINATED) { |
| 2764 |
sub_tree->state = SIP_SUB_TREE_TERMINATE_IN_PROGRESS; |
| 2765 |
} |
| 2766 |
|
| 2767 |
send_notify(sub_tree, 0); |
| 2768 |
|
| 2769 |
ast_test_suite_event_notify( |
| 2770 |
sub_tree->state == SIP_SUB_TREE_TERMINATED |
| 2771 |
? "SUBSCRIPTION_TERMINATED" : "SUBSCRIPTION_STATE_CHANGED", |
| 2772 |
"Resource: %s", sub_tree->root->resource); |
| 2773 |
|
| 2774 |
pjsip_dlg_dec_lock(dlg); |
| 2775 |
ao2_cleanup(sub_tree); |
| 2776 |
return 0; |
| 2777 |
} |
| 2778 |
|
| 2779 |
static int sched_cb(const void *data) |
| 2780 |
{ |
| 2781 |
struct sip_subscription_tree *sub_tree = (struct sip_subscription_tree *) data; |
| 2782 |
|
| 2783 |
|
| 2784 |
if (ast_sip_push_task(sub_tree->serializer, serialized_send_notify, sub_tree)) { |
| 2785 |
ao2_cleanup(sub_tree); |
| 2786 |
} |
| 2787 |
|
| 2788 |
return 0; |
| 2789 |
} |
| 2790 |
|
| 2791 |
static int schedule_notification(struct sip_subscription_tree *sub_tree) |
| 2792 |
{ |
| 2793 |
|
| 2794 |
if (sub_tree->notify_sched_id > -1) { |
| 2795 |
return 0; |
| 2796 |
} |
| 2797 |
|
| 2798 |
sub_tree->send_scheduled_notify = 1; |
| 2799 |
sub_tree->notify_sched_id = ast_sched_add(sched, sub_tree->notification_batch_interval, sched_cb, ao2_bump(sub_tree)); |
| 2800 |
if (sub_tree->notify_sched_id < 0) { |
| 2801 |
ao2_cleanup(sub_tree); |
| 2802 |
return -1; |
| 2803 |
} |
| 2804 |
|
| 2805 |
return 0; |
| 2806 |
} |
| 2807 |
|
| 2808 |
int ast_sip_subscription_notify(struct ast_sip_subscription *sub, struct ast_sip_body_data *notify_data, |
| 2809 |
int terminate) |
| 2810 |
{ |
| 2811 |
int res; |
| 2812 |
pjsip_dialog *dlg = sub->tree->dlg; |
| 2813 |
|
| 2814 |
pjsip_dlg_inc_lock(dlg); |
| 2815 |
|
| 2816 |
if (sub->tree->state != SIP_SUB_TREE_NORMAL) { |
| 2817 |
pjsip_dlg_dec_lock(dlg); |
| 2818 |
return 0; |
| 2819 |
} |
| 2820 |
|
| 2821 |
if (ast_sip_pubsub_generate_body_content(ast_sip_subscription_get_body_type(sub), |
| 2822 |
ast_sip_subscription_get_body_subtype(sub), notify_data, &sub->body_text)) { |
| 2823 |
pjsip_dlg_dec_lock(dlg); |
| 2824 |
return -1; |
| 2825 |
} |
| 2826 |
|
| 2827 |
sub->body_changed = 1; |
| 2828 |
if (terminate) { |
| 2829 |
sub->subscription_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 2830 |
sub->tree->state = SIP_SUB_TREE_TERMINATE_PENDING; |
| 2831 |
} |
| 2832 |
|
| 2833 |
if (sub->tree->notification_batch_interval) { |
| 2834 |
res = schedule_notification(sub->tree); |
| 2835 |
} else { |
| 2836 |
|
| 2837 |
ao2_ref(sub->tree, +1); |
| 2838 |
if (terminate) { |
| 2839 |
sub->tree->state = SIP_SUB_TREE_TERMINATE_IN_PROGRESS; |
| 2840 |
} |
| 2841 |
res = send_notify(sub->tree, 0); |
| 2842 |
ast_test_suite_event_notify(terminate ? "SUBSCRIPTION_TERMINATED" : "SUBSCRIPTION_STATE_CHANGED", |
| 2843 |
"Resource: %s", |
| 2844 |
sub->tree->root->resource); |
| 2845 |
ao2_ref(sub->tree, -1); |
| 2846 |
} |
| 2847 |
|
| 2848 |
pjsip_dlg_dec_lock(dlg); |
| 2849 |
return res; |
| 2850 |
} |
| 2851 |
|
| 2852 |
pjsip_sip_uri *ast_sip_subscription_get_sip_uri(struct ast_sip_subscription *sub) |
| 2853 |
{ |
| 2854 |
return sub->uri; |
| 2855 |
} |
| 2856 |
|
| 2857 |
void ast_sip_subscription_get_local_uri(struct ast_sip_subscription *sub, char *buf, size_t size) |
| 2858 |
{ |
| 2859 |
pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, sub->uri, buf, size); |
| 2860 |
} |
| 2861 |
|
| 2862 |
void ast_sip_subscription_get_remote_uri(struct ast_sip_subscription *sub, char *buf, size_t size) |
| 2863 |
{ |
| 2864 |
pjsip_dialog *dlg; |
| 2865 |
pjsip_sip_uri *uri; |
| 2866 |
|
| 2867 |
dlg = sub->tree->dlg; |
| 2868 |
uri = pjsip_uri_get_uri(dlg->remote.info->uri); |
| 2869 |
|
| 2870 |
if (pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, uri, buf, size) < 0) { |
| 2871 |
*buf = '\0'; |
| 2872 |
} |
| 2873 |
} |
| 2874 |
|
| 2875 |
const char *ast_sip_subscription_get_resource_name(struct ast_sip_subscription *sub) |
| 2876 |
{ |
| 2877 |
return sub->resource; |
| 2878 |
} |
| 2879 |
|
| 2880 |
int ast_sip_subscription_is_terminated(const struct ast_sip_subscription *sub) |
| 2881 |
{ |
| 2882 |
return sub->subscription_state == PJSIP_EVSUB_STATE_TERMINATED ? 1 : 0; |
| 2883 |
} |
| 2884 |
|
| 2885 |
static int sip_subscription_accept(struct sip_subscription_tree *sub_tree, pjsip_rx_data *rdata, int response) |
| 2886 |
{ |
| 2887 |
pjsip_hdr res_hdr; |
| 2888 |
|
| 2889 |
|
| 2890 |
if (ast_sip_mod_data_get(rdata->endpt_info.mod_data, pubsub_module.id, MOD_DATA_PERSISTENCE)) { |
| 2891 |
return 0; |
| 2892 |
} |
| 2893 |
|
| 2894 |
pj_list_init(&res_hdr); |
| 2895 |
if (sub_tree->is_list) { |
| 2896 |
|
| 2897 |
pj_list_insert_before(&res_hdr, create_require_eventlist(rdata->tp_info.pool)); |
| 2898 |
} |
| 2899 |
|
| 2900 |
return pjsip_evsub_accept(sub_tree->evsub, rdata, response, &res_hdr) == PJ_SUCCESS ? 0 : -1; |
| 2901 |
} |
| 2902 |
|
| 2903 |
struct ast_datastore *ast_sip_subscription_alloc_datastore(const struct ast_datastore_info *info, const char *uid) |
| 2904 |
{ |
| 2905 |
return ast_datastores_alloc_datastore(info, uid); |
| 2906 |
} |
| 2907 |
|
| 2908 |
int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore) |
| 2909 |
{ |
| 2910 |
return ast_datastores_add(subscription->datastores, datastore); |
| 2911 |
} |
| 2912 |
|
| 2913 |
struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name) |
| 2914 |
{ |
| 2915 |
return ast_datastores_find(subscription->datastores, name); |
| 2916 |
} |
| 2917 |
|
| 2918 |
void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name) |
| 2919 |
{ |
| 2920 |
ast_datastores_remove(subscription->datastores, name); |
| 2921 |
} |
| 2922 |
|
| 2923 |
struct ao2_container *ast_sip_subscription_get_datastores(const struct ast_sip_subscription *subscription) |
| 2924 |
{ |
| 2925 |
return subscription->datastores; |
| 2926 |
} |
| 2927 |
|
| 2928 |
int ast_sip_publication_add_datastore(struct ast_sip_publication *publication, struct ast_datastore *datastore) |
| 2929 |
{ |
| 2930 |
return ast_datastores_add(publication->datastores, datastore); |
| 2931 |
} |
| 2932 |
|
| 2933 |
struct ast_datastore *ast_sip_publication_get_datastore(struct ast_sip_publication *publication, const char *name) |
| 2934 |
{ |
| 2935 |
return ast_datastores_find(publication->datastores, name); |
| 2936 |
} |
| 2937 |
|
| 2938 |
void ast_sip_publication_remove_datastore(struct ast_sip_publication *publication, const char *name) |
| 2939 |
{ |
| 2940 |
ast_datastores_remove(publication->datastores, name); |
| 2941 |
} |
| 2942 |
|
| 2943 |
struct ao2_container *ast_sip_publication_get_datastores(const struct ast_sip_publication *publication) |
| 2944 |
{ |
| 2945 |
return publication->datastores; |
| 2946 |
} |
| 2947 |
|
| 2948 |
void ast_sip_subscription_set_persistence_data(struct ast_sip_subscription *subscription, struct ast_json *persistence_data) |
| 2949 |
{ |
| 2950 |
ast_json_unref(subscription->persistence_data); |
| 2951 |
subscription->persistence_data = persistence_data; |
| 2952 |
|
| 2953 |
if (subscription->tree->persistence) { |
| 2954 |
if (!subscription->tree->persistence->generator_data) { |
| 2955 |
subscription->tree->persistence->generator_data = ast_json_object_create(); |
| 2956 |
if (!subscription->tree->persistence->generator_data) { |
| 2957 |
return; |
| 2958 |
} |
| 2959 |
} |
| 2960 |
ast_json_object_set(subscription->tree->persistence->generator_data, subscription->resource, |
| 2961 |
ast_json_ref(persistence_data)); |
| 2962 |
} |
| 2963 |
} |
| 2964 |
|
| 2965 |
const struct ast_json *ast_sip_subscription_get_persistence_data(const struct ast_sip_subscription *subscription) |
| 2966 |
{ |
| 2967 |
return subscription->persistence_data; |
| 2968 |
} |
| 2969 |
|
| 2970 |
AST_RWLIST_HEAD_STATIC(publish_handlers, ast_sip_publish_handler); |
| 2971 |
|
| 2972 |
static int publication_hash_fn(const void *obj, const int flags) |
| 2973 |
{ |
| 2974 |
const struct ast_sip_publication *publication = obj; |
| 2975 |
const int *entity_tag = obj; |
| 2976 |
|
| 2977 |
return flags & OBJ_KEY ? *entity_tag : publication->entity_tag; |
| 2978 |
} |
| 2979 |
|
| 2980 |
static int publication_cmp_fn(void *obj, void *arg, int flags) |
| 2981 |
{ |
| 2982 |
const struct ast_sip_publication *publication1 = obj; |
| 2983 |
const struct ast_sip_publication *publication2 = arg; |
| 2984 |
const int *entity_tag = arg; |
| 2985 |
|
| 2986 |
return (publication1->entity_tag == (flags & OBJ_KEY ? *entity_tag : publication2->entity_tag) ? |
| 2987 |
CMP_MATCH | CMP_STOP : 0); |
| 2988 |
} |
| 2989 |
|
| 2990 |
static void publish_add_handler(struct ast_sip_publish_handler *handler) |
| 2991 |
{ |
| 2992 |
AST_RWLIST_WRLOCK(&publish_handlers); |
| 2993 |
AST_RWLIST_INSERT_TAIL(&publish_handlers, handler, next); |
| 2994 |
AST_RWLIST_UNLOCK(&publish_handlers); |
| 2995 |
} |
| 2996 |
|
| 2997 |
int ast_sip_register_publish_handler(struct ast_sip_publish_handler *handler) |
| 2998 |
{ |
| 2999 |
if (ast_strlen_zero(handler->event_name)) { |
| 3000 |
ast_log(LOG_ERROR, "No event package specified for publish handler. Cannot register\n"); |
| 3001 |
return -1; |
| 3002 |
} |
| 3003 |
|
| 3004 |
handler->publications = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 3005 |
PUBLICATIONS_BUCKETS, publication_hash_fn, NULL, publication_cmp_fn); |
| 3006 |
if (!handler->publications) { |
| 3007 |
ast_log(LOG_ERROR, "Could not allocate publications container for event '%s'\n", |
| 3008 |
handler->event_name); |
| 3009 |
return -1; |
| 3010 |
} |
| 3011 |
|
| 3012 |
publish_add_handler(handler); |
| 3013 |
|
| 3014 |
return 0; |
| 3015 |
} |
| 3016 |
|
| 3017 |
void ast_sip_unregister_publish_handler(struct ast_sip_publish_handler *handler) |
| 3018 |
{ |
| 3019 |
struct ast_sip_publish_handler *iter; |
| 3020 |
|
| 3021 |
AST_RWLIST_WRLOCK(&publish_handlers); |
| 3022 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&publish_handlers, iter, next) { |
| 3023 |
if (handler == iter) { |
| 3024 |
AST_RWLIST_REMOVE_CURRENT(next); |
| 3025 |
ao2_cleanup(handler->publications); |
| 3026 |
break; |
| 3027 |
} |
| 3028 |
} |
| 3029 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 3030 |
AST_RWLIST_UNLOCK(&publish_handlers); |
| 3031 |
} |
| 3032 |
|
| 3033 |
AST_RWLIST_HEAD_STATIC(subscription_handlers, ast_sip_subscription_handler); |
| 3034 |
|
| 3035 |
static void sub_add_handler(struct ast_sip_subscription_handler *handler) |
| 3036 |
{ |
| 3037 |
AST_RWLIST_WRLOCK(&subscription_handlers); |
| 3038 |
AST_RWLIST_INSERT_TAIL(&subscription_handlers, handler, next); |
| 3039 |
AST_RWLIST_UNLOCK(&subscription_handlers); |
| 3040 |
} |
| 3041 |
|
| 3042 |
static struct ast_sip_subscription_handler *find_sub_handler_for_event_name(const char *event_name) |
| 3043 |
{ |
| 3044 |
struct ast_sip_subscription_handler *iter; |
| 3045 |
|
| 3046 |
AST_RWLIST_RDLOCK(&subscription_handlers); |
| 3047 |
AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) { |
| 3048 |
if (!strcmp(iter->event_name, event_name)) { |
| 3049 |
break; |
| 3050 |
} |
| 3051 |
} |
| 3052 |
AST_RWLIST_UNLOCK(&subscription_handlers); |
| 3053 |
return iter; |
| 3054 |
} |
| 3055 |
|
| 3056 |
int ast_sip_register_subscription_handler(struct ast_sip_subscription_handler *handler) |
| 3057 |
{ |
| 3058 |
pj_str_t event; |
| 3059 |
pj_str_t accept[AST_SIP_MAX_ACCEPT] = { {0, }, }; |
| 3060 |
struct ast_sip_subscription_handler *existing; |
| 3061 |
int i = 0; |
| 3062 |
|
| 3063 |
if (ast_strlen_zero(handler->event_name)) { |
| 3064 |
ast_log(LOG_ERROR, "No event package specified for subscription handler. Cannot register\n"); |
| 3065 |
return -1; |
| 3066 |
} |
| 3067 |
|
| 3068 |
existing = find_sub_handler_for_event_name(handler->event_name); |
| 3069 |
if (existing) { |
| 3070 |
ast_log(LOG_ERROR, |
| 3071 |
"Unable to register subscription handler for event %s. A handler is already registered\n", |
| 3072 |
handler->event_name); |
| 3073 |
return -1; |
| 3074 |
} |
| 3075 |
|
| 3076 |
for (i = 0; i < AST_SIP_MAX_ACCEPT && !ast_strlen_zero(handler->accept[i]); ++i) { |
| 3077 |
pj_cstr(&accept[i], handler->accept[i]); |
| 3078 |
} |
| 3079 |
|
| 3080 |
pj_cstr(&event, handler->event_name); |
| 3081 |
|
| 3082 |
pjsip_evsub_register_pkg(&pubsub_module, &event, DEFAULT_EXPIRES, i, accept); |
| 3083 |
|
| 3084 |
sub_add_handler(handler); |
| 3085 |
|
| 3086 |
return 0; |
| 3087 |
} |
| 3088 |
|
| 3089 |
void ast_sip_unregister_subscription_handler(struct ast_sip_subscription_handler *handler) |
| 3090 |
{ |
| 3091 |
struct ast_sip_subscription_handler *iter; |
| 3092 |
|
| 3093 |
AST_RWLIST_WRLOCK(&subscription_handlers); |
| 3094 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&subscription_handlers, iter, next) { |
| 3095 |
if (handler == iter) { |
| 3096 |
AST_RWLIST_REMOVE_CURRENT(next); |
| 3097 |
break; |
| 3098 |
} |
| 3099 |
} |
| 3100 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 3101 |
AST_RWLIST_UNLOCK(&subscription_handlers); |
| 3102 |
} |
| 3103 |
|
| 3104 |
static struct ast_sip_pubsub_body_generator *find_body_generator_type_subtype_nolock(const char *type, const char *subtype) |
| 3105 |
{ |
| 3106 |
struct ast_sip_pubsub_body_generator *gen; |
| 3107 |
|
| 3108 |
AST_LIST_TRAVERSE(&body_generators, gen, list) { |
| 3109 |
if (!strcmp(gen->type, type) |
| 3110 |
&& !strcmp(gen->subtype, subtype)) { |
| 3111 |
break; |
| 3112 |
} |
| 3113 |
} |
| 3114 |
|
| 3115 |
return gen; |
| 3116 |
} |
| 3117 |
|
| 3118 |
static struct ast_sip_pubsub_body_generator *find_body_generator_type_subtype(const char *type, const char *subtype) |
| 3119 |
{ |
| 3120 |
struct ast_sip_pubsub_body_generator *gen; |
| 3121 |
|
| 3122 |
AST_RWLIST_RDLOCK(&body_generators); |
| 3123 |
gen = find_body_generator_type_subtype_nolock(type, subtype); |
| 3124 |
AST_RWLIST_UNLOCK(&body_generators); |
| 3125 |
return gen; |
| 3126 |
} |
| 3127 |
|
| 3128 |
static struct ast_sip_pubsub_body_generator *find_body_generator_accept(const char *accept) |
| 3129 |
{ |
| 3130 |
char *accept_copy = ast_strdupa(accept); |
| 3131 |
char *subtype = accept_copy; |
| 3132 |
char *type = strsep(&subtype, "/"); |
| 3133 |
|
| 3134 |
if (ast_strlen_zero(type) || ast_strlen_zero(subtype)) { |
| 3135 |
return NULL; |
| 3136 |
} |
| 3137 |
|
| 3138 |
return find_body_generator_type_subtype(type, subtype); |
| 3139 |
} |
| 3140 |
|
| 3141 |
static struct ast_sip_pubsub_body_generator *find_body_generator(char accept[AST_SIP_MAX_ACCEPT][64], |
| 3142 |
size_t num_accept, const char *body_type) |
| 3143 |
{ |
| 3144 |
int i; |
| 3145 |
struct ast_sip_pubsub_body_generator *generator = NULL; |
| 3146 |
|
| 3147 |
for (i = 0; i < num_accept; ++i) { |
| 3148 |
generator = find_body_generator_accept(accept[i]); |
| 3149 |
if (generator) { |
| 3150 |
ast_debug(3, "Body generator %p found for accept type %s\n", generator, accept[i]); |
| 3151 |
if (strcmp(generator->body_type, body_type)) { |
| 3152 |
ast_log(LOG_WARNING, "Body generator '%s/%s'(%p) does not accept the type of data this event generates\n", |
| 3153 |
generator->type, generator->subtype, generator); |
| 3154 |
generator = NULL; |
| 3155 |
continue; |
| 3156 |
} |
| 3157 |
break; |
| 3158 |
} else { |
| 3159 |
ast_debug(3, "No body generator found for accept type %s\n", accept[i]); |
| 3160 |
} |
| 3161 |
} |
| 3162 |
|
| 3163 |
return generator; |
| 3164 |
} |
| 3165 |
|
| 3166 |
static int generate_initial_notify(struct ast_sip_subscription *sub) |
| 3167 |
{ |
| 3168 |
void *notify_data; |
| 3169 |
int res; |
| 3170 |
struct ast_sip_body_data data = { |
| 3171 |
.body_type = sub->handler->body_type, |
| 3172 |
}; |
| 3173 |
|
| 3174 |
if (AST_VECTOR_SIZE(&sub->children) > 0) { |
| 3175 |
int i; |
| 3176 |
|
| 3177 |
for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) { |
| 3178 |
if (generate_initial_notify(AST_VECTOR_GET(&sub->children, i))) { |
| 3179 |
return -1; |
| 3180 |
} |
| 3181 |
} |
| 3182 |
|
| 3183 |
return 0; |
| 3184 |
} |
| 3185 |
|
| 3186 |
|
| 3187 |
if (sub->handler->notifier->subscription_established(sub)) { |
| 3188 |
return -1; |
| 3189 |
} |
| 3190 |
|
| 3191 |
notify_data = sub->handler->notifier->get_notify_data(sub); |
| 3192 |
if (!notify_data) { |
| 3193 |
ast_debug(3, "No notify data, not generating any body content\n"); |
| 3194 |
return -1; |
| 3195 |
} |
| 3196 |
|
| 3197 |
data.body_data = notify_data; |
| 3198 |
|
| 3199 |
res = ast_sip_pubsub_generate_body_content(ast_sip_subscription_get_body_type(sub), |
| 3200 |
ast_sip_subscription_get_body_subtype(sub), &data, &sub->body_text); |
| 3201 |
|
| 3202 |
ao2_cleanup(notify_data); |
| 3203 |
|
| 3204 |
return res; |
| 3205 |
} |
| 3206 |
|
| 3207 |
static int pubsub_on_refresh_timeout(void *userdata); |
| 3208 |
|
| 3209 |
static int initial_notify_task(void * obj) |
| 3210 |
{ |
| 3211 |
struct initial_notify_data *ind = obj; |
| 3212 |
|
| 3213 |
if (generate_initial_notify(ind->sub_tree->root)) { |
| 3214 |
pjsip_evsub_terminate(ind->sub_tree->evsub, PJ_TRUE); |
| 3215 |
} else { |
| 3216 |
send_notify(ind->sub_tree, 1); |
| 3217 |
ast_test_suite_event_notify("SUBSCRIPTION_ESTABLISHED", |
| 3218 |
"Resource: %s", |
| 3219 |
ind->sub_tree->root->resource); |
| 3220 |
} |
| 3221 |
|
| 3222 |
if (ind->expires != PJSIP_EXPIRES_NOT_SPECIFIED) { |
| 3223 |
char *name = ast_alloca(strlen("->/ ") + |
| 3224 |
strlen(ind->sub_tree->persistence->endpoint) + |
| 3225 |
strlen(ind->sub_tree->root->resource) + |
| 3226 |
strlen(ind->sub_tree->root->handler->event_name) + |
| 3227 |
ind->sub_tree->dlg->call_id->id.slen + 1); |
| 3228 |
|
| 3229 |
sprintf(name, "%s->%s/%s %.*s", ind->sub_tree->persistence->endpoint, |
| 3230 |
ind->sub_tree->root->resource, ind->sub_tree->root->handler->event_name, |
| 3231 |
(int)ind->sub_tree->dlg->call_id->id.slen, ind->sub_tree->dlg->call_id->id.ptr); |
| 3232 |
|
| 3233 |
ast_debug(3, "Scheduling timer: %s\n", name); |
| 3234 |
ind->sub_tree->expiration_task = ast_sip_schedule_task(ind->sub_tree->serializer, |
| 3235 |
ind->expires * 1000, pubsub_on_refresh_timeout, name, |
| 3236 |
ind->sub_tree, AST_SIP_SCHED_TASK_FIXED | AST_SIP_SCHED_TASK_DATA_AO2); |
| 3237 |
if (!ind->sub_tree->expiration_task) { |
| 3238 |
ast_log(LOG_ERROR, "Unable to create expiration timer of %d seconds for %s\n", |
| 3239 |
ind->expires, name); |
| 3240 |
} |
| 3241 |
} |
| 3242 |
|
| 3243 |
ao2_ref(ind->sub_tree, -1); |
| 3244 |
ast_free(ind); |
| 3245 |
|
| 3246 |
return 0; |
| 3247 |
} |
| 3248 |
|
| 3249 |
static pj_bool_t pubsub_on_rx_subscribe_request(pjsip_rx_data *rdata) |
| 3250 |
{ |
| 3251 |
pjsip_expires_hdr *expires_header; |
| 3252 |
struct ast_sip_subscription_handler *handler; |
| 3253 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 3254 |
struct sip_subscription_tree *sub_tree; |
| 3255 |
struct ast_sip_pubsub_body_generator *generator; |
| 3256 |
char *resource; |
| 3257 |
pjsip_uri *request_uri; |
| 3258 |
size_t resource_size; |
| 3259 |
int resp; |
| 3260 |
struct resource_tree tree; |
| 3261 |
pj_status_t dlg_status; |
| 3262 |
const pj_str_t *user; |
| 3263 |
|
| 3264 |
endpoint = ast_pjsip_rdata_get_endpoint(rdata); |
| 3265 |
ast_assert(endpoint != NULL); |
| 3266 |
|
| 3267 |
if (!endpoint->subscription.allow) { |
| 3268 |
ast_log(LOG_WARNING, "Subscriptions not permitted for endpoint %s.\n", ast_sorcery_object_get_id(endpoint)); |
| 3269 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 603, NULL, NULL, NULL); |
| 3270 |
return PJ_TRUE; |
| 3271 |
} |
| 3272 |
|
| 3273 |
request_uri = rdata->msg_info.msg->line.req.uri; |
| 3274 |
|
| 3275 |
if (!ast_sip_is_uri_sip_sips(request_uri)) { |
| 3276 |
char uri_str[PJSIP_MAX_URL_SIZE]; |
| 3277 |
|
| 3278 |
pjsip_uri_print(PJSIP_URI_IN_REQ_URI, request_uri, uri_str, sizeof(uri_str)); |
| 3279 |
ast_log(LOG_WARNING, "Request URI '%s' is not a sip: or sips: URI.\n", uri_str); |
| 3280 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 416, NULL, NULL, NULL); |
| 3281 |
return PJ_TRUE; |
| 3282 |
} |
| 3283 |
|
| 3284 |
user = ast_sip_pjsip_uri_get_username(request_uri); |
| 3285 |
resource_size = pj_strlen(user) + 1; |
| 3286 |
resource = ast_alloca(resource_size); |
| 3287 |
ast_copy_pj_str(resource, user, resource_size); |
| 3288 |
|
| 3289 |
|
| 3290 |
|
| 3291 |
|
| 3292 |
|
| 3293 |
AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(resource); |
| 3294 |
|
| 3295 |
expires_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, rdata->msg_info.msg->hdr.next); |
| 3296 |
if (expires_header) { |
| 3297 |
if (expires_header->ivalue == 0) { |
| 3298 |
ast_debug(1, "Subscription request from endpoint %s rejected. Expiration of 0 is invalid\n", |
| 3299 |
ast_sorcery_object_get_id(endpoint)); |
| 3300 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400, NULL, NULL, NULL); |
| 3301 |
return PJ_TRUE; |
| 3302 |
} |
| 3303 |
if (expires_header->ivalue < endpoint->subscription.minexpiry) { |
| 3304 |
ast_log(LOG_WARNING, "Subscription expiration %d is too brief for endpoint %s. Minimum is %u\n", |
| 3305 |
expires_header->ivalue, ast_sorcery_object_get_id(endpoint), endpoint->subscription.minexpiry); |
| 3306 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 423, NULL, NULL, NULL); |
| 3307 |
return PJ_TRUE; |
| 3308 |
} |
| 3309 |
} |
| 3310 |
|
| 3311 |
handler = subscription_get_handler_from_rdata(rdata, ast_sorcery_object_get_id(endpoint)); |
| 3312 |
if (!handler) { |
| 3313 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL); |
| 3314 |
return PJ_TRUE; |
| 3315 |
} |
| 3316 |
|
| 3317 |
generator = subscription_get_generator_from_rdata(rdata, handler); |
| 3318 |
if (!generator) { |
| 3319 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL); |
| 3320 |
return PJ_TRUE; |
| 3321 |
} |
| 3322 |
|
| 3323 |
memset(&tree, 0, sizeof(tree)); |
| 3324 |
resp = build_resource_tree(endpoint, handler, resource, &tree, |
| 3325 |
ast_sip_pubsub_has_eventlist_support(rdata), rdata); |
| 3326 |
if (!PJSIP_IS_STATUS_IN_CLASS(resp, 200)) { |
| 3327 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, resp, NULL, NULL, NULL); |
| 3328 |
resource_tree_destroy(&tree); |
| 3329 |
return PJ_TRUE; |
| 3330 |
} |
| 3331 |
|
| 3332 |
sub_tree = create_subscription_tree(handler, endpoint, rdata, resource, generator, &tree, &dlg_status, NULL); |
| 3333 |
if (!sub_tree) { |
| 3334 |
if (dlg_status != PJ_EEXISTS) { |
| 3335 |
ast_debug(3, "No dialog exists, rejecting\n"); |
| 3336 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL); |
| 3337 |
} |
| 3338 |
} else { |
| 3339 |
struct initial_notify_data *ind = ast_malloc(sizeof(*ind)); |
| 3340 |
|
| 3341 |
if (!ind) { |
| 3342 |
pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE); |
| 3343 |
resource_tree_destroy(&tree); |
| 3344 |
return PJ_TRUE; |
| 3345 |
} |
| 3346 |
|
| 3347 |
ind->sub_tree = ao2_bump(sub_tree); |
| 3348 |
|
| 3349 |
ind->expires = PJSIP_EXPIRES_NOT_SPECIFIED; |
| 3350 |
|
| 3351 |
sub_tree->persistence = subscription_persistence_create(sub_tree); |
| 3352 |
subscription_persistence_update(sub_tree, rdata, SUBSCRIPTION_PERSISTENCE_CREATED); |
| 3353 |
sip_subscription_accept(sub_tree, rdata, resp); |
| 3354 |
if (ast_sip_push_task(sub_tree->serializer, initial_notify_task, ind)) { |
| 3355 |
pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE); |
| 3356 |
ao2_ref(sub_tree, -1); |
| 3357 |
ast_free(ind); |
| 3358 |
} |
| 3359 |
} |
| 3360 |
|
| 3361 |
resource_tree_destroy(&tree); |
| 3362 |
return PJ_TRUE; |
| 3363 |
} |
| 3364 |
|
| 3365 |
static struct ast_sip_publish_handler *find_pub_handler(const char *event) |
| 3366 |
{ |
| 3367 |
struct ast_sip_publish_handler *iter = NULL; |
| 3368 |
|
| 3369 |
AST_RWLIST_RDLOCK(&publish_handlers); |
| 3370 |
AST_RWLIST_TRAVERSE(&publish_handlers, iter, next) { |
| 3371 |
if (strcmp(event, iter->event_name)) { |
| 3372 |
ast_debug(3, "Event %s does not match %s\n", event, iter->event_name); |
| 3373 |
continue; |
| 3374 |
} |
| 3375 |
ast_debug(3, "Event name match: %s = %s\n", event, iter->event_name); |
| 3376 |
break; |
| 3377 |
} |
| 3378 |
AST_RWLIST_UNLOCK(&publish_handlers); |
| 3379 |
|
| 3380 |
return iter; |
| 3381 |
} |
| 3382 |
|
| 3383 |
static enum sip_publish_type determine_sip_publish_type(pjsip_rx_data *rdata, |
| 3384 |
pjsip_generic_string_hdr *etag_hdr, unsigned int *expires, int *entity_id) |
| 3385 |
{ |
| 3386 |
pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL); |
| 3387 |
|
| 3388 |
if (etag_hdr) { |
| 3389 |
char etag[pj_strlen(&etag_hdr->hvalue) + 1]; |
| 3390 |
|
| 3391 |
ast_copy_pj_str(etag, &etag_hdr->hvalue, sizeof(etag)); |
| 3392 |
|
| 3393 |
if (sscanf(etag, "%30d", entity_id) != 1) { |
| 3394 |
return SIP_PUBLISH_UNKNOWN; |
| 3395 |
} |
| 3396 |
} |
| 3397 |
|
| 3398 |
*expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES; |
| 3399 |
|
| 3400 |
if (!(*expires)) { |
| 3401 |
return SIP_PUBLISH_REMOVE; |
| 3402 |
} else if (!etag_hdr && rdata->msg_info.msg->body) { |
| 3403 |
return SIP_PUBLISH_INITIAL; |
| 3404 |
} else if (etag_hdr && !rdata->msg_info.msg->body) { |
| 3405 |
return SIP_PUBLISH_REFRESH; |
| 3406 |
} else if (etag_hdr && rdata->msg_info.msg->body) { |
| 3407 |
return SIP_PUBLISH_MODIFY; |
| 3408 |
} |
| 3409 |
|
| 3410 |
return SIP_PUBLISH_UNKNOWN; |
| 3411 |
} |
| 3412 |
|
| 3413 |
|
| 3414 |
static void publication_destroy_fn(void *obj) |
| 3415 |
{ |
| 3416 |
struct ast_sip_publication *publication = obj; |
| 3417 |
|
| 3418 |
ast_debug(3, "Destroying SIP publication\n"); |
| 3419 |
|
| 3420 |
ao2_cleanup(publication->datastores); |
| 3421 |
ao2_cleanup(publication->endpoint); |
| 3422 |
|
| 3423 |
ast_module_unref(ast_module_info->self); |
| 3424 |
} |
| 3425 |
|
| 3426 |
static struct ast_sip_publication *sip_create_publication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, |
| 3427 |
const char *resource, const char *event_configuration_name) |
| 3428 |
{ |
| 3429 |
struct ast_sip_publication *publication; |
| 3430 |
pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL); |
| 3431 |
size_t resource_len = strlen(resource) + 1, event_configuration_name_len = strlen(event_configuration_name) + 1; |
| 3432 |
char *dst; |
| 3433 |
|
| 3434 |
ast_assert(endpoint != NULL); |
| 3435 |
|
| 3436 |
if (!(publication = ao2_alloc(sizeof(*publication) + resource_len + event_configuration_name_len, publication_destroy_fn))) { |
| 3437 |
return NULL; |
| 3438 |
} |
| 3439 |
|
| 3440 |
ast_module_ref(ast_module_info->self); |
| 3441 |
|
| 3442 |
if (!(publication->datastores = ast_datastores_alloc())) { |
| 3443 |
ao2_ref(publication, -1); |
| 3444 |
return NULL; |
| 3445 |
} |
| 3446 |
|
| 3447 |
publication->entity_tag = ast_atomic_fetchadd_int(&esc_etag_counter, +1); |
| 3448 |
ao2_ref(endpoint, +1); |
| 3449 |
publication->endpoint = endpoint; |
| 3450 |
publication->expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES; |
| 3451 |
publication->sched_id = -1; |
| 3452 |
dst = publication->data; |
| 3453 |
publication->resource = strcpy(dst, resource); |
| 3454 |
dst += resource_len; |
| 3455 |
publication->event_configuration_name = strcpy(dst, event_configuration_name); |
| 3456 |
|
| 3457 |
return publication; |
| 3458 |
} |
| 3459 |
|
| 3460 |
static int sip_publication_respond(struct ast_sip_publication *pub, int status_code, |
| 3461 |
pjsip_rx_data *rdata) |
| 3462 |
{ |
| 3463 |
pjsip_tx_data *tdata; |
| 3464 |
pjsip_transaction *tsx; |
| 3465 |
|
| 3466 |
if (pjsip_endpt_create_response(ast_sip_get_pjsip_endpoint(), rdata, status_code, NULL, &tdata) != PJ_SUCCESS) { |
| 3467 |
return -1; |
| 3468 |
} |
| 3469 |
|
| 3470 |
if (PJSIP_IS_STATUS_IN_CLASS(status_code, 200)) { |
| 3471 |
char buf[30]; |
| 3472 |
|
| 3473 |
snprintf(buf, sizeof(buf), "%d", pub->entity_tag); |
| 3474 |
ast_sip_add_header(tdata, "SIP-ETag", buf); |
| 3475 |
|
| 3476 |
snprintf(buf, sizeof(buf), "%d", pub->expires); |
| 3477 |
ast_sip_add_header(tdata, "Expires", buf); |
| 3478 |
} |
| 3479 |
|
| 3480 |
if (pjsip_tsx_create_uas(&pubsub_module, rdata, &tsx) != PJ_SUCCESS) { |
| 3481 |
pjsip_tx_data_dec_ref(tdata); |
| 3482 |
return -1; |
| 3483 |
} |
| 3484 |
|
| 3485 |
pjsip_tsx_recv_msg(tsx, rdata); |
| 3486 |
|
| 3487 |
if (pjsip_tsx_send_msg(tsx, tdata) != PJ_SUCCESS) { |
| 3488 |
pjsip_tx_data_dec_ref(tdata); |
| 3489 |
return -1; |
| 3490 |
} |
| 3491 |
|
| 3492 |
return 0; |
| 3493 |
} |
| 3494 |
|
| 3495 |
static struct ast_sip_publication *publish_request_initial(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, |
| 3496 |
struct ast_sip_publish_handler *handler) |
| 3497 |
{ |
| 3498 |
struct ast_sip_publication *publication; |
| 3499 |
char *resource_name; |
| 3500 |
size_t resource_size; |
| 3501 |
RAII_VAR(struct ast_sip_publication_resource *, resource, NULL, ao2_cleanup); |
| 3502 |
struct ast_variable *event_configuration_name = NULL; |
| 3503 |
pjsip_uri *request_uri; |
| 3504 |
int resp; |
| 3505 |
const pj_str_t *user; |
| 3506 |
|
| 3507 |
request_uri = rdata->msg_info.msg->line.req.uri; |
| 3508 |
|
| 3509 |
if (!ast_sip_is_uri_sip_sips(request_uri)) { |
| 3510 |
char uri_str[PJSIP_MAX_URL_SIZE]; |
| 3511 |
|
| 3512 |
pjsip_uri_print(PJSIP_URI_IN_REQ_URI, request_uri, uri_str, sizeof(uri_str)); |
| 3513 |
ast_log(LOG_WARNING, "Request URI '%s' is not a sip: or sips: URI.\n", uri_str); |
| 3514 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 416, NULL, NULL, NULL); |
| 3515 |
return NULL; |
| 3516 |
} |
| 3517 |
|
| 3518 |
user = ast_sip_pjsip_uri_get_username(request_uri); |
| 3519 |
resource_size = pj_strlen(user) + 1; |
| 3520 |
resource_name = ast_alloca(resource_size); |
| 3521 |
ast_copy_pj_str(resource_name, user, resource_size); |
| 3522 |
|
| 3523 |
|
| 3524 |
|
| 3525 |
|
| 3526 |
|
| 3527 |
AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(resource_name); |
| 3528 |
|
| 3529 |
resource = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "inbound-publication", resource_name); |
| 3530 |
if (!resource) { |
| 3531 |
ast_debug(1, "No 'inbound-publication' defined for resource '%s'\n", resource_name); |
| 3532 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL); |
| 3533 |
return NULL; |
| 3534 |
} |
| 3535 |
|
| 3536 |
if (!ast_strlen_zero(resource->endpoint) && strcmp(resource->endpoint, ast_sorcery_object_get_id(endpoint))) { |
| 3537 |
ast_debug(1, "Resource %s has a defined endpoint '%s', but does not match endpoint '%s' that received the request\n", |
| 3538 |
resource_name, resource->endpoint, ast_sorcery_object_get_id(endpoint)); |
| 3539 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL); |
| 3540 |
return NULL; |
| 3541 |
} |
| 3542 |
|
| 3543 |
for (event_configuration_name = resource->events; event_configuration_name; event_configuration_name = event_configuration_name->next) { |
| 3544 |
if (!strcmp(event_configuration_name->name, handler->event_name)) { |
| 3545 |
break; |
| 3546 |
} |
| 3547 |
} |
| 3548 |
|
| 3549 |
if (!event_configuration_name) { |
| 3550 |
ast_debug(1, "Event '%s' is not configured for '%s'\n", handler->event_name, resource_name); |
| 3551 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL); |
| 3552 |
return NULL; |
| 3553 |
} |
| 3554 |
|
| 3555 |
resp = handler->new_publication(endpoint, resource_name, event_configuration_name->value); |
| 3556 |
|
| 3557 |
if (!PJSIP_IS_STATUS_IN_CLASS(resp, 200)) { |
| 3558 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, resp, NULL, NULL, NULL); |
| 3559 |
return NULL; |
| 3560 |
} |
| 3561 |
|
| 3562 |
publication = sip_create_publication(endpoint, rdata, S_OR(resource_name, ""), event_configuration_name->value); |
| 3563 |
|
| 3564 |
if (!publication) { |
| 3565 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 503, NULL, NULL, NULL); |
| 3566 |
return NULL; |
| 3567 |
} |
| 3568 |
|
| 3569 |
publication->handler = handler; |
| 3570 |
if (publication->handler->publication_state_change(publication, rdata->msg_info.msg->body, |
| 3571 |
AST_SIP_PUBLISH_STATE_INITIALIZED)) { |
| 3572 |
ast_debug(3, "Publication state change failed\n"); |
| 3573 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL); |
| 3574 |
ao2_cleanup(publication); |
| 3575 |
return NULL; |
| 3576 |
} |
| 3577 |
|
| 3578 |
sip_publication_respond(publication, resp, rdata); |
| 3579 |
|
| 3580 |
return publication; |
| 3581 |
} |
| 3582 |
|
| 3583 |
static int publish_expire_callback(void *data) |
| 3584 |
{ |
| 3585 |
RAII_VAR(struct ast_sip_publication *, publication, data, ao2_cleanup); |
| 3586 |
|
| 3587 |
if (publication->handler->publish_expire) { |
| 3588 |
publication->handler->publish_expire(publication); |
| 3589 |
} |
| 3590 |
|
| 3591 |
return 0; |
| 3592 |
} |
| 3593 |
|
| 3594 |
static int publish_expire(const void *data) |
| 3595 |
{ |
| 3596 |
struct ast_sip_publication *publication = (struct ast_sip_publication*)data; |
| 3597 |
|
| 3598 |
ao2_unlink(publication->handler->publications, publication); |
| 3599 |
publication->sched_id = -1; |
| 3600 |
|
| 3601 |
if (ast_sip_push_task(NULL, publish_expire_callback, publication)) { |
| 3602 |
ao2_cleanup(publication); |
| 3603 |
} |
| 3604 |
|
| 3605 |
return 0; |
| 3606 |
} |
| 3607 |
|
| 3608 |
static pj_bool_t pubsub_on_rx_publish_request(pjsip_rx_data *rdata) |
| 3609 |
{ |
| 3610 |
pjsip_event_hdr *event_header; |
| 3611 |
struct ast_sip_publish_handler *handler; |
| 3612 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 3613 |
char event[32]; |
| 3614 |
static const pj_str_t str_sip_if_match = { "SIP-If-Match", 12 }; |
| 3615 |
pjsip_generic_string_hdr *etag_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_sip_if_match, NULL); |
| 3616 |
enum sip_publish_type publish_type; |
| 3617 |
RAII_VAR(struct ast_sip_publication *, publication, NULL, ao2_cleanup); |
| 3618 |
unsigned int expires = 0; |
| 3619 |
int entity_id, response = 0; |
| 3620 |
|
| 3621 |
endpoint = ast_pjsip_rdata_get_endpoint(rdata); |
| 3622 |
ast_assert(endpoint != NULL); |
| 3623 |
|
| 3624 |
event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_event_name, rdata->msg_info.msg->hdr.next); |
| 3625 |
if (!event_header) { |
| 3626 |
ast_log(LOG_WARNING, "Incoming PUBLISH request from %s with no Event header\n", |
| 3627 |
ast_sorcery_object_get_id(endpoint)); |
| 3628 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL); |
| 3629 |
return PJ_TRUE; |
| 3630 |
} |
| 3631 |
ast_copy_pj_str(event, &event_header->event_type, sizeof(event)); |
| 3632 |
|
| 3633 |
handler = find_pub_handler(event); |
| 3634 |
if (!handler) { |
| 3635 |
ast_log(LOG_WARNING, "No registered publish handler for event %s from %s\n", event, |
| 3636 |
ast_sorcery_object_get_id(endpoint)); |
| 3637 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL); |
| 3638 |
return PJ_TRUE; |
| 3639 |
} |
| 3640 |
|
| 3641 |
publish_type = determine_sip_publish_type(rdata, etag_hdr, &expires, &entity_id); |
| 3642 |
|
| 3643 |
|
| 3644 |
if ((publish_type != SIP_PUBLISH_INITIAL) && (publish_type != SIP_PUBLISH_UNKNOWN)) { |
| 3645 |
if (!(publication = ao2_find(handler->publications, &entity_id, OBJ_KEY | OBJ_UNLINK))) { |
| 3646 |
static const pj_str_t str_conditional_request_failed = { "Conditional Request Failed", 26 }; |
| 3647 |
|
| 3648 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 412, &str_conditional_request_failed, |
| 3649 |
NULL, NULL); |
| 3650 |
return PJ_TRUE; |
| 3651 |
} |
| 3652 |
|
| 3653 |
|
| 3654 |
publication->entity_tag = ast_atomic_fetchadd_int(&esc_etag_counter, +1); |
| 3655 |
|
| 3656 |
|
| 3657 |
publication->expires = expires; |
| 3658 |
} |
| 3659 |
|
| 3660 |
switch (publish_type) { |
| 3661 |
case SIP_PUBLISH_INITIAL: |
| 3662 |
publication = publish_request_initial(endpoint, rdata, handler); |
| 3663 |
break; |
| 3664 |
case SIP_PUBLISH_REFRESH: |
| 3665 |
case SIP_PUBLISH_MODIFY: |
| 3666 |
if (handler->publication_state_change(publication, rdata->msg_info.msg->body, |
| 3667 |
AST_SIP_PUBLISH_STATE_ACTIVE)) { |
| 3668 |
|
| 3669 |
expires = 0; |
| 3670 |
} |
| 3671 |
response = 200; |
| 3672 |
break; |
| 3673 |
case SIP_PUBLISH_REMOVE: |
| 3674 |
handler->publication_state_change(publication, rdata->msg_info.msg->body, |
| 3675 |
AST_SIP_PUBLISH_STATE_TERMINATED); |
| 3676 |
response = 200; |
| 3677 |
break; |
| 3678 |
case SIP_PUBLISH_UNKNOWN: |
| 3679 |
default: |
| 3680 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400, NULL, NULL, NULL); |
| 3681 |
break; |
| 3682 |
} |
| 3683 |
|
| 3684 |
if (publication) { |
| 3685 |
if (expires) { |
| 3686 |
ao2_link(handler->publications, publication); |
| 3687 |
|
| 3688 |
AST_SCHED_REPLACE_UNREF(publication->sched_id, sched, expires * 1000, publish_expire, publication, |
| 3689 |
ao2_ref(_data, -1), ao2_ref(publication, -1), ao2_ref(publication, +1)); |
| 3690 |
} else { |
| 3691 |
AST_SCHED_DEL_UNREF(sched, publication->sched_id, ao2_ref(publication, -1)); |
| 3692 |
} |
| 3693 |
} |
| 3694 |
|
| 3695 |
if (response) { |
| 3696 |
sip_publication_respond(publication, response, rdata); |
| 3697 |
} |
| 3698 |
|
| 3699 |
return PJ_TRUE; |
| 3700 |
} |
| 3701 |
|
| 3702 |
struct ast_sip_endpoint *ast_sip_publication_get_endpoint(struct ast_sip_publication *pub) |
| 3703 |
{ |
| 3704 |
return pub->endpoint; |
| 3705 |
} |
| 3706 |
|
| 3707 |
const char *ast_sip_publication_get_resource(const struct ast_sip_publication *pub) |
| 3708 |
{ |
| 3709 |
return pub->resource; |
| 3710 |
} |
| 3711 |
|
| 3712 |
const char *ast_sip_publication_get_event_configuration(const struct ast_sip_publication *pub) |
| 3713 |
{ |
| 3714 |
return pub->event_configuration_name; |
| 3715 |
} |
| 3716 |
|
| 3717 |
int ast_sip_pubsub_is_body_generator_registered(const char *type, const char *subtype) |
| 3718 |
{ |
| 3719 |
return !!find_body_generator_type_subtype(type, subtype); |
| 3720 |
} |
| 3721 |
|
| 3722 |
int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator) |
| 3723 |
{ |
| 3724 |
struct ast_sip_pubsub_body_generator *existing; |
| 3725 |
pj_str_t accept; |
| 3726 |
pj_size_t accept_len; |
| 3727 |
|
| 3728 |
AST_RWLIST_WRLOCK(&body_generators); |
| 3729 |
existing = find_body_generator_type_subtype_nolock(generator->type, generator->subtype); |
| 3730 |
if (existing) { |
| 3731 |
AST_RWLIST_UNLOCK(&body_generators); |
| 3732 |
ast_log(LOG_WARNING, "A body generator for %s/%s is already registered.\n", |
| 3733 |
generator->type, generator->subtype); |
| 3734 |
return -1; |
| 3735 |
} |
| 3736 |
AST_LIST_INSERT_HEAD(&body_generators, generator, list); |
| 3737 |
AST_RWLIST_UNLOCK(&body_generators); |
| 3738 |
|
| 3739 |
|
| 3740 |
accept_len = strlen(generator->type) + strlen(generator->subtype) + 1; |
| 3741 |
|
| 3742 |
|
| 3743 |
pj_strset(&accept, ast_alloca(accept_len + 1), accept_len); |
| 3744 |
sprintf((char *) pj_strbuf(&accept), "%s/%s", generator->type, generator->subtype); |
| 3745 |
|
| 3746 |
pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), &pubsub_module, |
| 3747 |
PJSIP_H_ACCEPT, NULL, 1, &accept); |
| 3748 |
|
| 3749 |
return 0; |
| 3750 |
} |
| 3751 |
|
| 3752 |
void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator) |
| 3753 |
{ |
| 3754 |
struct ast_sip_pubsub_body_generator *iter; |
| 3755 |
|
| 3756 |
AST_RWLIST_WRLOCK(&body_generators); |
| 3757 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&body_generators, iter, list) { |
| 3758 |
if (iter == generator) { |
| 3759 |
AST_LIST_REMOVE_CURRENT(list); |
| 3760 |
break; |
| 3761 |
} |
| 3762 |
} |
| 3763 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 3764 |
AST_RWLIST_UNLOCK(&body_generators); |
| 3765 |
} |
| 3766 |
|
| 3767 |
int ast_sip_pubsub_register_body_supplement(struct ast_sip_pubsub_body_supplement *supplement) |
| 3768 |
{ |
| 3769 |
AST_RWLIST_WRLOCK(&body_supplements); |
| 3770 |
AST_RWLIST_INSERT_TAIL(&body_supplements, supplement, list); |
| 3771 |
AST_RWLIST_UNLOCK(&body_supplements); |
| 3772 |
|
| 3773 |
return 0; |
| 3774 |
} |
| 3775 |
|
| 3776 |
void ast_sip_pubsub_unregister_body_supplement(struct ast_sip_pubsub_body_supplement *supplement) |
| 3777 |
{ |
| 3778 |
struct ast_sip_pubsub_body_supplement *iter; |
| 3779 |
|
| 3780 |
AST_RWLIST_WRLOCK(&body_supplements); |
| 3781 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&body_supplements, iter, list) { |
| 3782 |
if (iter == supplement) { |
| 3783 |
AST_LIST_REMOVE_CURRENT(list); |
| 3784 |
break; |
| 3785 |
} |
| 3786 |
} |
| 3787 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 3788 |
AST_RWLIST_UNLOCK(&body_supplements); |
| 3789 |
} |
| 3790 |
|
| 3791 |
const char *ast_sip_subscription_get_body_type(struct ast_sip_subscription *sub) |
| 3792 |
{ |
| 3793 |
return sub->body_generator->type; |
| 3794 |
} |
| 3795 |
|
| 3796 |
const char *ast_sip_subscription_get_body_subtype(struct ast_sip_subscription *sub) |
| 3797 |
{ |
| 3798 |
return sub->body_generator->subtype; |
| 3799 |
} |
| 3800 |
|
| 3801 |
int ast_sip_pubsub_generate_body_content(const char *type, const char *subtype, |
| 3802 |
struct ast_sip_body_data *data, struct ast_str **str) |
| 3803 |
{ |
| 3804 |
struct ast_sip_pubsub_body_supplement *supplement; |
| 3805 |
struct ast_sip_pubsub_body_generator *generator; |
| 3806 |
int res = 0; |
| 3807 |
void *body; |
| 3808 |
|
| 3809 |
generator = find_body_generator_type_subtype(type, subtype); |
| 3810 |
if (!generator) { |
| 3811 |
ast_log(LOG_WARNING, "Unable to find a body generator for %s/%s\n", |
| 3812 |
type, subtype); |
| 3813 |
return -1; |
| 3814 |
} |
| 3815 |
|
| 3816 |
if (strcmp(data->body_type, generator->body_type)) { |
| 3817 |
ast_log(LOG_WARNING, "%s/%s body generator does not accept the type of data provided\n", |
| 3818 |
type, subtype); |
| 3819 |
return -1; |
| 3820 |
} |
| 3821 |
|
| 3822 |
body = generator->allocate_body(data->body_data); |
| 3823 |
if (!body) { |
| 3824 |
ast_log(LOG_WARNING, "%s/%s body generator could not to allocate a body\n", |
| 3825 |
type, subtype); |
| 3826 |
return -1; |
| 3827 |
} |
| 3828 |
|
| 3829 |
if (generator->generate_body_content(body, data->body_data)) { |
| 3830 |
res = -1; |
| 3831 |
goto end; |
| 3832 |
} |
| 3833 |
|
| 3834 |
AST_RWLIST_RDLOCK(&body_supplements); |
| 3835 |
AST_RWLIST_TRAVERSE(&body_supplements, supplement, list) { |
| 3836 |
if (!strcmp(generator->type, supplement->type) && |
| 3837 |
!strcmp(generator->subtype, supplement->subtype)) { |
| 3838 |
res = supplement->supplement_body(body, data->body_data); |
| 3839 |
if (res) { |
| 3840 |
break; |
| 3841 |
} |
| 3842 |
} |
| 3843 |
} |
| 3844 |
AST_RWLIST_UNLOCK(&body_supplements); |
| 3845 |
|
| 3846 |
if (!res) { |
| 3847 |
generator->to_string(body, str); |
| 3848 |
} |
| 3849 |
|
| 3850 |
end: |
| 3851 |
if (generator->destroy_body) { |
| 3852 |
generator->destroy_body(body); |
| 3853 |
} |
| 3854 |
|
| 3855 |
return res; |
| 3856 |
} |
| 3857 |
|
| 3858 |
struct simple_message_summary { |
| 3859 |
int messages_waiting; |
| 3860 |
int voice_messages_new; |
| 3861 |
int voice_messages_old; |
| 3862 |
int voice_messages_urgent_new; |
| 3863 |
int voice_messages_urgent_old; |
| 3864 |
char message_account[PJSIP_MAX_URL_SIZE]; |
| 3865 |
}; |
| 3866 |
|
| 3867 |
static int parse_simple_message_summary(char *body, |
| 3868 |
struct simple_message_summary *summary) |
| 3869 |
{ |
| 3870 |
char *line; |
| 3871 |
char *buffer; |
| 3872 |
int found_counts = 0; |
| 3873 |
|
| 3874 |
if (ast_strlen_zero(body) || !summary) { |
| 3875 |
return -1; |
| 3876 |
} |
| 3877 |
|
| 3878 |
buffer = ast_strdupa(body); |
| 3879 |
memset(summary, 0, sizeof(*summary)); |
| 3880 |
|
| 3881 |
while ((line = ast_read_line_from_buffer(&buffer))) { |
| 3882 |
line = ast_str_to_lower(line); |
| 3883 |
|
| 3884 |
if (sscanf(line, "voice-message: %d/%d (%d/%d)", |
| 3885 |
&summary->voice_messages_new, &summary->voice_messages_old, |
| 3886 |
&summary->voice_messages_urgent_new, &summary->voice_messages_urgent_old)) { |
| 3887 |
found_counts = 1; |
| 3888 |
} else { |
| 3889 |
sscanf(line, "message-account: %511s", summary->message_account); |
| 3890 |
} |
| 3891 |
} |
| 3892 |
|
| 3893 |
return !found_counts; |
| 3894 |
} |
| 3895 |
|
| 3896 |
static pj_bool_t pubsub_on_rx_mwi_notify_request(pjsip_rx_data *rdata) |
| 3897 |
{ |
| 3898 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 3899 |
struct simple_message_summary summary; |
| 3900 |
const char *endpoint_name; |
| 3901 |
char *atsign; |
| 3902 |
char *context; |
| 3903 |
char *body; |
| 3904 |
char *mailbox; |
| 3905 |
int body_len; |
| 3906 |
int rc; |
| 3907 |
|
| 3908 |
endpoint = ast_pjsip_rdata_get_endpoint(rdata); |
| 3909 |
if (!endpoint) { |
| 3910 |
ast_debug(1, "Incoming MWI: Endpoint not found in rdata (%p)\n", rdata); |
| 3911 |
rc = 404; |
| 3912 |
goto error; |
| 3913 |
} |
| 3914 |
|
| 3915 |
endpoint_name = ast_sorcery_object_get_id(endpoint); |
| 3916 |
ast_debug(1, "Incoming MWI: Found endpoint: %s\n", endpoint_name); |
| 3917 |
if (ast_strlen_zero(endpoint->incoming_mwi_mailbox)) { |
| 3918 |
ast_debug(1, "Incoming MWI: No incoming mailbox specified for endpoint '%s'\n", endpoint_name); |
| 3919 |
ast_test_suite_event_notify("PUBSUB_NO_INCOMING_MWI_MAILBOX", |
| 3920 |
"Endpoint: %s", endpoint_name); |
| 3921 |
rc = 404; |
| 3922 |
goto error; |
| 3923 |
} |
| 3924 |
|
| 3925 |
mailbox = ast_strdupa(endpoint->incoming_mwi_mailbox); |
| 3926 |
atsign = strchr(mailbox, '@'); |
| 3927 |
if (!atsign) { |
| 3928 |
ast_debug(1, "Incoming MWI: No '@' found in endpoint %s's incoming mailbox '%s'. Can't parse context\n", |
| 3929 |
endpoint_name, endpoint->incoming_mwi_mailbox); |
| 3930 |
rc = 404; |
| 3931 |
goto error; |
| 3932 |
} |
| 3933 |
|
| 3934 |
*atsign = '\0'; |
| 3935 |
context = atsign + 1; |
| 3936 |
|
| 3937 |
body = ast_alloca(rdata->msg_info.msg->body->len + 1); |
| 3938 |
body_len = rdata->msg_info.msg->body->print_body(rdata->msg_info.msg->body, body, |
| 3939 |
rdata->msg_info.msg->body->len + 1); |
| 3940 |
|
| 3941 |
if (body_len < 0 || body_len > rdata->msg_info.msg->body->len) { |
| 3942 |
ast_debug(1, "Incoming MWI: Endpoint: '%s' Unable to print request body\n", endpoint_name); |
| 3943 |
rc = 404; |
| 3944 |
goto error; |
| 3945 |
} |
| 3946 |
body[body_len] = '\0'; |
| 3947 |
|
| 3948 |
if (parse_simple_message_summary(body, &summary) != 0) { |
| 3949 |
ast_debug(1, "Incoming MWI: Endpoint: '%s' There was an issue getting message info from body '%s'\n", |
| 3950 |
ast_sorcery_object_get_id(endpoint), body); |
| 3951 |
rc = 404; |
| 3952 |
goto error; |
| 3953 |
} |
| 3954 |
|
| 3955 |
if (ast_publish_mwi_state(mailbox, context, |
| 3956 |
summary.voice_messages_new, summary.voice_messages_old)) { |
| 3957 |
ast_log(LOG_ERROR, "Incoming MWI: Endpoint: '%s' Could not publish MWI to stasis. " |
| 3958 |
"Mailbox: %s Message-Account: %s Voice-Messages: %d/%d (%d/%d)\n", |
| 3959 |
endpoint_name, endpoint->incoming_mwi_mailbox, summary.message_account, |
| 3960 |
summary.voice_messages_new, summary.voice_messages_old, |
| 3961 |
summary.voice_messages_urgent_new, summary.voice_messages_urgent_old); |
| 3962 |
rc = 404; |
| 3963 |
} else { |
| 3964 |
ast_debug(1, "Incoming MWI: Endpoint: '%s' Mailbox: %s Message-Account: %s Voice-Messages: %d/%d (%d/%d)\n", |
| 3965 |
endpoint_name, endpoint->incoming_mwi_mailbox, summary.message_account, |
| 3966 |
summary.voice_messages_new, summary.voice_messages_old, |
| 3967 |
summary.voice_messages_urgent_new, summary.voice_messages_urgent_old); |
| 3968 |
ast_test_suite_event_notify("PUBSUB_INCOMING_MWI_PUBLISH", |
| 3969 |
"Endpoint: %s\r\n" |
| 3970 |
"Mailbox: %s\r\n" |
| 3971 |
"MessageAccount: %s\r\n" |
| 3972 |
"VoiceMessagesNew: %d\r\n" |
| 3973 |
"VoiceMessagesOld: %d\r\n" |
| 3974 |
"VoiceMessagesUrgentNew: %d\r\n" |
| 3975 |
"VoiceMessagesUrgentOld: %d", |
| 3976 |
endpoint_name, endpoint->incoming_mwi_mailbox, summary.message_account, |
| 3977 |
summary.voice_messages_new, summary.voice_messages_old, |
| 3978 |
summary.voice_messages_urgent_new, summary.voice_messages_urgent_old); |
| 3979 |
rc = 200; |
| 3980 |
} |
| 3981 |
|
| 3982 |
error: |
| 3983 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, rc, NULL, NULL, NULL); |
| 3984 |
return PJ_TRUE; |
| 3985 |
} |
| 3986 |
|
| 3987 |
static pj_bool_t pubsub_on_rx_notify_request(pjsip_rx_data *rdata) |
| 3988 |
{ |
| 3989 |
if (rdata->msg_info.msg->body && |
| 3990 |
ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type, |
| 3991 |
"application", "simple-message-summary")) { |
| 3992 |
return pubsub_on_rx_mwi_notify_request(rdata); |
| 3993 |
} |
| 3994 |
return PJ_FALSE; |
| 3995 |
} |
| 3996 |
|
| 3997 |
static pj_bool_t pubsub_on_rx_request(pjsip_rx_data *rdata) |
| 3998 |
{ |
| 3999 |
if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_subscribe_method())) { |
| 4000 |
return pubsub_on_rx_subscribe_request(rdata); |
| 4001 |
} else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_publish_method)) { |
| 4002 |
return pubsub_on_rx_publish_request(rdata); |
| 4003 |
} else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_notify_method)) { |
| 4004 |
return pubsub_on_rx_notify_request(rdata); |
| 4005 |
} |
| 4006 |
|
| 4007 |
return PJ_FALSE; |
| 4008 |
} |
| 4009 |
|
| 4010 |
|
| 4011 |
|
| 4012 |
|
| 4013 |
|
| 4014 |
|
| 4015 |
|
| 4016 |
|
| 4017 |
|
| 4018 |
|
| 4019 |
|
| 4020 |
|
| 4021 |
|
| 4022 |
|
| 4023 |
|
| 4024 |
|
| 4025 |
|
| 4026 |
|
| 4027 |
|
| 4028 |
|
| 4029 |
|
| 4030 |
|
| 4031 |
|
| 4032 |
|
| 4033 |
|
| 4034 |
|
| 4035 |
|
| 4036 |
|
| 4037 |
|
| 4038 |
|
| 4039 |
|
| 4040 |
|
| 4041 |
|
| 4042 |
|
| 4043 |
|
| 4044 |
|
| 4045 |
|
| 4046 |
|
| 4047 |
|
| 4048 |
|
| 4049 |
|
| 4050 |
|
| 4051 |
|
| 4052 |
|
| 4053 |
|
| 4054 |
|
| 4055 |
|
| 4056 |
|
| 4057 |
|
| 4058 |
|
| 4059 |
|
| 4060 |
|
| 4061 |
|
| 4062 |
|
| 4063 |
|
| 4064 |
|
| 4065 |
|
| 4066 |
|
| 4067 |
|
| 4068 |
|
| 4069 |
|
| 4070 |
|
| 4071 |
|
| 4072 |
|
| 4073 |
|
| 4074 |
|
| 4075 |
|
| 4076 |
|
| 4077 |
|
| 4078 |
|
| 4079 |
|
| 4080 |
|
| 4081 |
|
| 4082 |
|
| 4083 |
|
| 4084 |
|
| 4085 |
|
| 4086 |
|
| 4087 |
|
| 4088 |
|
| 4089 |
|
| 4090 |
|
| 4091 |
|
| 4092 |
static void clean_sub_tree(pjsip_evsub *evsub) |
| 4093 |
{ |
| 4094 |
|
| 4095 |
struct sip_subscription_tree *sub_tree; |
| 4096 |
sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id); |
| 4097 |
|
| 4098 |
ast_debug(3, "Cleaning subscription %p\n", evsub); |
| 4099 |
|
| 4100 |
if (sub_tree->expiration_task) { |
| 4101 |
char task_name[256]; |
| 4102 |
|
| 4103 |
ast_sip_sched_task_get_name(sub_tree->expiration_task, task_name, sizeof(task_name)); |
| 4104 |
ast_debug(3, "Cancelling timer: %s\n", task_name); |
| 4105 |
ast_sip_sched_task_cancel(sub_tree->expiration_task); |
| 4106 |
ao2_cleanup(sub_tree->expiration_task); |
| 4107 |
sub_tree->expiration_task = NULL; |
| 4108 |
} |
| 4109 |
|
| 4110 |
remove_subscription(sub_tree); |
| 4111 |
|
| 4112 |
pjsip_evsub_set_mod_data(evsub, pubsub_module.id, NULL); |
| 4113 |
|
| 4114 |
#ifdef HAVE_PJSIP_EVSUB_GRP_LOCK |
| 4115 |
pjsip_evsub_dec_ref(sub_tree->evsub); |
| 4116 |
#endif |
| 4117 |
|
| 4118 |
sub_tree->evsub = NULL; |
| 4119 |
|
| 4120 |
ast_sip_dialog_set_serializer(sub_tree->dlg, NULL); |
| 4121 |
ast_sip_dialog_set_endpoint(sub_tree->dlg, NULL); |
| 4122 |
|
| 4123 |
subscription_persistence_remove(sub_tree); |
| 4124 |
shutdown_subscriptions(sub_tree->root); |
| 4125 |
|
| 4126 |
sub_tree->state = SIP_SUB_TREE_TERMINATED; |
| 4127 |
|
| 4128 |
ao2_ref(sub_tree, -1); |
| 4129 |
} |
| 4130 |
|
| 4131 |
|
| 4132 |
#if PJ_VERSION_NUM >= 0x020D0000 |
| 4133 |
# define HAVE_PJSIP_EVSUB_PENDING_NOTIFY 1 |
| 4134 |
#endif |
| 4135 |
|
| 4136 |
|
| 4137 |
|
| 4138 |
|
| 4139 |
|
| 4140 |
|
| 4141 |
|
| 4142 |
|
| 4143 |
|
| 4144 |
|
| 4145 |
static void pubsub_on_evsub_state(pjsip_evsub *evsub, pjsip_event *event) |
| 4146 |
{ |
| 4147 |
struct sip_subscription_tree *sub_tree = |
| 4148 |
pjsip_evsub_get_mod_data(evsub, pubsub_module.id); |
| 4149 |
|
| 4150 |
ast_debug(3, "evsub %p state %s event %s sub_tree %p sub_tree state %s\n", evsub, |
| 4151 |
pjsip_evsub_get_state_name(evsub), pjsip_event_str(event->type), sub_tree, |
| 4152 |
(sub_tree ? sub_tree_state_description[sub_tree->state] : "UNKNOWN")); |
| 4153 |
|
| 4154 |
if (!sub_tree || pjsip_evsub_get_state(evsub) != PJSIP_EVSUB_STATE_TERMINATED) { |
| 4155 |
return; |
| 4156 |
} |
| 4157 |
|
| 4158 |
|
| 4159 |
if (!(sub_tree->state == SIP_SUB_TREE_TERMINATE_IN_PROGRESS |
| 4160 |
|| (event->type == PJSIP_EVENT_TSX_STATE && sub_tree->state == SIP_SUB_TREE_NORMAL) |
| 4161 |
)) { |
| 4162 |
ast_debug(3, "Do nothing.\n"); |
| 4163 |
return; |
| 4164 |
} |
| 4165 |
|
| 4166 |
#ifdef HAVE_PJSIP_EVSUB_PENDING_NOTIFY |
| 4167 |
|
| 4168 |
|
| 4169 |
|
| 4170 |
|
| 4171 |
if (event->body.tsx_state.type == PJSIP_EVENT_RX_MSG && |
| 4172 |
!pjsip_method_cmp(&event->body.tsx_state.tsx->method, &pjsip_subscribe_method) && |
| 4173 |
pjsip_evsub_get_expires(evsub) == 0) { |
| 4174 |
ast_debug(3, "Subscription ending, do nothing.\n"); |
| 4175 |
return; |
| 4176 |
} |
| 4177 |
#endif |
| 4178 |
|
| 4179 |
|
| 4180 |
clean_sub_tree(evsub); |
| 4181 |
} |
| 4182 |
|
| 4183 |
static int pubsub_on_refresh_timeout(void *userdata) |
| 4184 |
{ |
| 4185 |
struct sip_subscription_tree *sub_tree = userdata; |
| 4186 |
pjsip_dialog *dlg = sub_tree->dlg; |
| 4187 |
|
| 4188 |
ast_debug(3, "sub_tree %p sub_tree state %s\n", sub_tree, |
| 4189 |
(sub_tree ? sub_tree_state_description[sub_tree->state] : "UNKNOWN")); |
| 4190 |
|
| 4191 |
pjsip_dlg_inc_lock(dlg); |
| 4192 |
if (sub_tree->state >= SIP_SUB_TREE_TERMINATE_IN_PROGRESS) { |
| 4193 |
pjsip_dlg_dec_lock(dlg); |
| 4194 |
return 0; |
| 4195 |
} |
| 4196 |
|
| 4197 |
if (sub_tree->state == SIP_SUB_TREE_TERMINATE_PENDING) { |
| 4198 |
sub_tree->state = SIP_SUB_TREE_TERMINATE_IN_PROGRESS; |
| 4199 |
set_state_terminated(sub_tree->root); |
| 4200 |
} |
| 4201 |
|
| 4202 |
if (sub_tree->generate_initial_notify) { |
| 4203 |
sub_tree->generate_initial_notify = 0; |
| 4204 |
if (generate_initial_notify(sub_tree->root)) { |
| 4205 |
pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE); |
| 4206 |
pjsip_dlg_dec_lock(dlg); |
| 4207 |
return 0; |
| 4208 |
} |
| 4209 |
} |
| 4210 |
|
| 4211 |
send_notify(sub_tree, 1); |
| 4212 |
|
| 4213 |
ast_test_suite_event_notify(sub_tree->root->subscription_state == PJSIP_EVSUB_STATE_TERMINATED ? |
| 4214 |
"SUBSCRIPTION_TERMINATED" : "SUBSCRIPTION_REFRESHED", |
| 4215 |
"Resource: %s", sub_tree->root->resource); |
| 4216 |
|
| 4217 |
pjsip_dlg_dec_lock(dlg); |
| 4218 |
|
| 4219 |
return 0; |
| 4220 |
} |
| 4221 |
|
| 4222 |
static int serialized_pubsub_on_refresh_timeout(void *userdata) |
| 4223 |
{ |
| 4224 |
struct sip_subscription_tree *sub_tree = userdata; |
| 4225 |
|
| 4226 |
ast_debug(3, "sub_tree %p sub_tree state %s\n", sub_tree, |
| 4227 |
(sub_tree ? sub_tree_state_description[sub_tree->state] : "UNKNOWN")); |
| 4228 |
|
| 4229 |
pubsub_on_refresh_timeout(userdata); |
| 4230 |
ao2_cleanup(sub_tree); |
| 4231 |
|
| 4232 |
return 0; |
| 4233 |
} |
| 4234 |
|
| 4235 |
|
| 4236 |
|
| 4237 |
|
| 4238 |
|
| 4239 |
|
| 4240 |
|
| 4241 |
|
| 4242 |
|
| 4243 |
|
| 4244 |
static int cmp_strings(char *s1, char *s2) |
| 4245 |
{ |
| 4246 |
if (!ast_strlen_zero(s1) && !ast_strlen_zero(s2)) { |
| 4247 |
return strcmp(s1, s2); |
| 4248 |
} |
| 4249 |
|
| 4250 |
return ast_strlen_zero(s1) == ast_strlen_zero(s2) ? 0 : 1; |
| 4251 |
} |
| 4252 |
|
| 4253 |
|
| 4254 |
|
| 4255 |
|
| 4256 |
|
| 4257 |
|
| 4258 |
|
| 4259 |
static int cmp_subscription_childrens(struct ast_sip_subscription *s1, struct ast_sip_subscription *s2) |
| 4260 |
{ |
| 4261 |
int i; |
| 4262 |
|
| 4263 |
if (AST_VECTOR_SIZE(&s1->children) != AST_VECTOR_SIZE(&s2->children)) { |
| 4264 |
return 1; |
| 4265 |
} |
| 4266 |
|
| 4267 |
for (i = 0; i < AST_VECTOR_SIZE(&s1->children); ++i) { |
| 4268 |
struct ast_sip_subscription *c1 = AST_VECTOR_GET(&s1->children, i); |
| 4269 |
struct ast_sip_subscription *c2 = AST_VECTOR_GET(&s2->children, i); |
| 4270 |
|
| 4271 |
if (cmp_strings(c1->resource, c2->resource) |
| 4272 |
|| cmp_strings(c1->display_name, c2->display_name)) { |
| 4273 |
|
| 4274 |
return 1; |
| 4275 |
} |
| 4276 |
} |
| 4277 |
|
| 4278 |
return 0; |
| 4279 |
} |
| 4280 |
|
| 4281 |
static int destroy_subscriptions_task(void *obj) |
| 4282 |
{ |
| 4283 |
struct ast_sip_subscription *sub = (struct ast_sip_subscription *) obj; |
| 4284 |
|
| 4285 |
destroy_subscriptions(sub); |
| 4286 |
|
| 4287 |
return 0; |
| 4288 |
} |
| 4289 |
|
| 4290 |
|
| 4291 |
|
| 4292 |
|
| 4293 |
|
| 4294 |
|
| 4295 |
|
| 4296 |
|
| 4297 |
|
| 4298 |
static void pubsub_on_rx_refresh(pjsip_evsub *evsub, pjsip_rx_data *rdata, |
| 4299 |
int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body) |
| 4300 |
{ |
| 4301 |
struct sip_subscription_tree *sub_tree; |
| 4302 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 4303 |
|
| 4304 |
sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id); |
| 4305 |
ast_debug(3, "evsub %p sub_tree %p sub_tree state %s\n", evsub, sub_tree, |
| 4306 |
(sub_tree ? sub_tree_state_description[sub_tree->state] : "UNKNOWN")); |
| 4307 |
|
| 4308 |
if (!sub_tree || sub_tree->state != SIP_SUB_TREE_NORMAL) { |
| 4309 |
return; |
| 4310 |
} |
| 4311 |
|
| 4312 |
if (sub_tree->expiration_task) { |
| 4313 |
char task_name[256]; |
| 4314 |
|
| 4315 |
ast_sip_sched_task_get_name(sub_tree->expiration_task, task_name, sizeof(task_name)); |
| 4316 |
ast_debug(3, "Cancelling timer: %s\n", task_name); |
| 4317 |
ast_sip_sched_task_cancel(sub_tree->expiration_task); |
| 4318 |
ao2_cleanup(sub_tree->expiration_task); |
| 4319 |
sub_tree->expiration_task = NULL; |
| 4320 |
} |
| 4321 |
|
| 4322 |
|
| 4323 |
|
| 4324 |
|
| 4325 |
|
| 4326 |
if (pjsip_evsub_get_state(sub_tree->evsub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 4327 |
sub_tree->state = SIP_SUB_TREE_TERMINATE_PENDING; |
| 4328 |
} |
| 4329 |
|
| 4330 |
endpoint = ast_pjsip_rdata_get_endpoint(rdata); |
| 4331 |
|
| 4332 |
|
| 4333 |
if (sub_tree->state == SIP_SUB_TREE_NORMAL && sub_tree->root->handler->notifier->refresh_subscribe) { |
| 4334 |
if (!sub_tree->root->handler->notifier->refresh_subscribe(sub_tree->root, rdata)) { |
| 4335 |
return; |
| 4336 |
} |
| 4337 |
} |
| 4338 |
|
| 4339 |
if (sub_tree->state == SIP_SUB_TREE_NORMAL && sub_tree->is_list) { |
| 4340 |
|
| 4341 |
const char *resource = sub_tree->root->resource; |
| 4342 |
struct ast_sip_subscription *old_root = sub_tree->root; |
| 4343 |
struct ast_sip_subscription *new_root = NULL; |
| 4344 |
|
| 4345 |
struct ast_sip_pubsub_body_generator *generator = NULL; |
| 4346 |
|
| 4347 |
if (endpoint && (generator = subscription_get_generator_from_rdata(rdata, sub_tree->root->handler))) { |
| 4348 |
|
| 4349 |
struct resource_tree tree; |
| 4350 |
int resp; |
| 4351 |
|
| 4352 |
memset(&tree, 0, sizeof(tree)); |
| 4353 |
resp = build_resource_tree(endpoint, sub_tree->root->handler, resource, &tree, |
| 4354 |
ast_sip_pubsub_has_eventlist_support(rdata), rdata); |
| 4355 |
if (PJSIP_IS_STATUS_IN_CLASS(resp, 200)) { |
| 4356 |
new_root = create_virtual_subscriptions(sub_tree->root->handler, resource, generator, sub_tree, tree.root); |
| 4357 |
if (new_root) { |
| 4358 |
if (cmp_subscription_childrens(old_root, new_root)) { |
| 4359 |
ast_debug(1, "RLS '%s->%s' was modified, regenerate it\n", ast_sorcery_object_get_id(endpoint), old_root->resource); |
| 4360 |
new_root->version = old_root->version; |
| 4361 |
sub_tree->root = new_root; |
| 4362 |
sub_tree->generate_initial_notify = 1; |
| 4363 |
|
| 4364 |
|
| 4365 |
if (sub_tree->notify_sched_id > -1) { |
| 4366 |
AST_SCHED_DEL_UNREF(sched, sub_tree->notify_sched_id, ao2_ref(sub_tree, -1)); |
| 4367 |
sub_tree->send_scheduled_notify = 0; |
| 4368 |
} |
| 4369 |
|
| 4370 |
|
| 4371 |
set_state_terminated(old_root); |
| 4372 |
|
| 4373 |
|
| 4374 |
|
| 4375 |
shutdown_subscriptions(old_root); |
| 4376 |
|
| 4377 |
|
| 4378 |
if (ast_sip_push_task(sub_tree->serializer, destroy_subscriptions_task, old_root)) { |
| 4379 |
ast_log(LOG_ERROR, "Failed to push task to destroy old subscriptions for RLS '%s->%s'.\n", |
| 4380 |
ast_sorcery_object_get_id(endpoint), old_root->resource); |
| 4381 |
} |
| 4382 |
} else { |
| 4383 |
destroy_subscriptions(new_root); |
| 4384 |
} |
| 4385 |
} |
| 4386 |
} else { |
| 4387 |
sub_tree->state = SIP_SUB_TREE_TERMINATE_PENDING; |
| 4388 |
pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE); |
| 4389 |
} |
| 4390 |
|
| 4391 |
resource_tree_destroy(&tree); |
| 4392 |
} |
| 4393 |
} |
| 4394 |
|
| 4395 |
subscription_persistence_update(sub_tree, rdata, SUBSCRIPTION_PERSISTENCE_REFRESHED); |
| 4396 |
|
| 4397 |
#ifdef HAVE_PJSIP_EVSUB_PENDING_NOTIFY |
| 4398 |
|
| 4399 |
|
| 4400 |
|
| 4401 |
|
| 4402 |
|
| 4403 |
pubsub_on_refresh_timeout(sub_tree); |
| 4404 |
#else |
| 4405 |
if (ast_sip_push_task(sub_tree->serializer, serialized_pubsub_on_refresh_timeout, ao2_bump(sub_tree))) { |
| 4406 |
|
| 4407 |
ast_log(LOG_ERROR, "Failed to push task to send NOTIFY.\n"); |
| 4408 |
sub_tree->state = SIP_SUB_TREE_NORMAL; |
| 4409 |
ao2_ref(sub_tree, -1); |
| 4410 |
} |
| 4411 |
#endif |
| 4412 |
|
| 4413 |
if (sub_tree->is_list) { |
| 4414 |
pj_list_insert_before(res_hdr, create_require_eventlist(rdata->tp_info.pool)); |
| 4415 |
} |
| 4416 |
} |
| 4417 |
|
| 4418 |
static void pubsub_on_rx_notify(pjsip_evsub *evsub, pjsip_rx_data *rdata, int *p_st_code, |
| 4419 |
pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body) |
| 4420 |
{ |
| 4421 |
struct ast_sip_subscription *sub; |
| 4422 |
|
| 4423 |
if (!(sub = pjsip_evsub_get_mod_data(evsub, pubsub_module.id))) { |
| 4424 |
return; |
| 4425 |
} |
| 4426 |
|
| 4427 |
sub->handler->subscriber->state_change(sub, rdata->msg_info.msg->body, |
| 4428 |
pjsip_evsub_get_state(evsub)); |
| 4429 |
} |
| 4430 |
|
| 4431 |
static int serialized_pubsub_on_client_refresh(void *userdata) |
| 4432 |
{ |
| 4433 |
struct sip_subscription_tree *sub_tree = userdata; |
| 4434 |
pjsip_tx_data *tdata; |
| 4435 |
|
| 4436 |
if (!sub_tree->evsub) { |
| 4437 |
ao2_cleanup(sub_tree); |
| 4438 |
return 0; |
| 4439 |
} |
| 4440 |
|
| 4441 |
if (pjsip_evsub_initiate(sub_tree->evsub, NULL, -1, &tdata) == PJ_SUCCESS) { |
| 4442 |
pjsip_evsub_send_request(sub_tree->evsub, tdata); |
| 4443 |
} else { |
| 4444 |
pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE); |
| 4445 |
} |
| 4446 |
|
| 4447 |
ao2_cleanup(sub_tree); |
| 4448 |
return 0; |
| 4449 |
} |
| 4450 |
|
| 4451 |
static void pubsub_on_client_refresh(pjsip_evsub *evsub) |
| 4452 |
{ |
| 4453 |
struct sip_subscription_tree *sub_tree; |
| 4454 |
|
| 4455 |
if (!(sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id))) { |
| 4456 |
return; |
| 4457 |
} |
| 4458 |
|
| 4459 |
if (ast_sip_push_task(sub_tree->serializer, serialized_pubsub_on_client_refresh, ao2_bump(sub_tree))) { |
| 4460 |
ao2_cleanup(sub_tree); |
| 4461 |
} |
| 4462 |
} |
| 4463 |
|
| 4464 |
static void pubsub_on_server_timeout(pjsip_evsub *evsub) |
| 4465 |
{ |
| 4466 |
struct sip_subscription_tree *sub_tree; |
| 4467 |
|
| 4468 |
|
| 4469 |
|
| 4470 |
|
| 4471 |
|
| 4472 |
|
| 4473 |
|
| 4474 |
|
| 4475 |
|
| 4476 |
|
| 4477 |
|
| 4478 |
|
| 4479 |
|
| 4480 |
|
| 4481 |
sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id); |
| 4482 |
if (!sub_tree || sub_tree->state != SIP_SUB_TREE_NORMAL) { |
| 4483 |
return; |
| 4484 |
} |
| 4485 |
|
| 4486 |
sub_tree->state = SIP_SUB_TREE_TERMINATE_PENDING; |
| 4487 |
if (ast_sip_push_task(sub_tree->serializer, serialized_pubsub_on_refresh_timeout, ao2_bump(sub_tree))) { |
| 4488 |
sub_tree->state = SIP_SUB_TREE_NORMAL; |
| 4489 |
ao2_cleanup(sub_tree); |
| 4490 |
} |
| 4491 |
} |
| 4492 |
|
| 4493 |
static int ami_subscription_detail(struct sip_subscription_tree *sub_tree, |
| 4494 |
struct ast_sip_ami *ami, |
| 4495 |
const char *event) |
| 4496 |
{ |
| 4497 |
struct ast_str *buf; |
| 4498 |
|
| 4499 |
buf = ast_sip_create_ami_event(event, ami); |
| 4500 |
if (!buf) { |
| 4501 |
return -1; |
| 4502 |
} |
| 4503 |
|
| 4504 |
sip_subscription_to_ami(sub_tree, &buf); |
| 4505 |
astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); |
| 4506 |
ast_free(buf); |
| 4507 |
|
| 4508 |
++ami->count; |
| 4509 |
return 0; |
| 4510 |
} |
| 4511 |
|
| 4512 |
static int ami_subscription_detail_inbound(struct sip_subscription_tree *sub_tree, void *arg) |
| 4513 |
{ |
| 4514 |
return sub_tree->role == AST_SIP_NOTIFIER ? ami_subscription_detail( |
| 4515 |
sub_tree, arg, "InboundSubscriptionDetail") : 0; |
| 4516 |
} |
| 4517 |
|
| 4518 |
static int ami_subscription_detail_outbound(struct sip_subscription_tree *sub_tree, void *arg) |
| 4519 |
{ |
| 4520 |
return sub_tree->role == AST_SIP_SUBSCRIBER ? ami_subscription_detail( |
| 4521 |
sub_tree, arg, "OutboundSubscriptionDetail") : 0; |
| 4522 |
} |
| 4523 |
|
| 4524 |
static int ami_show_subscriptions_inbound(struct mansession *s, const struct message *m) |
| 4525 |
{ |
| 4526 |
struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), }; |
| 4527 |
|
| 4528 |
astman_send_listack(s, m, "Following are Events for each inbound Subscription", |
| 4529 |
"start"); |
| 4530 |
|
| 4531 |
for_each_subscription(ami_subscription_detail_inbound, &ami); |
| 4532 |
|
| 4533 |
astman_send_list_complete_start(s, m, "InboundSubscriptionDetailComplete", ami.count); |
| 4534 |
astman_send_list_complete_end(s); |
| 4535 |
return 0; |
| 4536 |
} |
| 4537 |
|
| 4538 |
static int ami_show_subscriptions_outbound(struct mansession *s, const struct message *m) |
| 4539 |
{ |
| 4540 |
struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), }; |
| 4541 |
|
| 4542 |
astman_send_listack(s, m, "Following are Events for each outbound Subscription", |
| 4543 |
"start"); |
| 4544 |
|
| 4545 |
for_each_subscription(ami_subscription_detail_outbound, &ami); |
| 4546 |
|
| 4547 |
astman_send_list_complete_start(s, m, "OutboundSubscriptionDetailComplete", ami.count); |
| 4548 |
astman_send_list_complete_end(s); |
| 4549 |
return 0; |
| 4550 |
} |
| 4551 |
|
| 4552 |
static int format_ami_resource_lists(void *obj, void *arg, int flags) |
| 4553 |
{ |
| 4554 |
struct resource_list *list = obj; |
| 4555 |
struct ast_sip_ami *ami = arg; |
| 4556 |
struct ast_str *buf; |
| 4557 |
|
| 4558 |
buf = ast_sip_create_ami_event("ResourceListDetail", ami); |
| 4559 |
if (!buf) { |
| 4560 |
return CMP_STOP; |
| 4561 |
} |
| 4562 |
|
| 4563 |
if (ast_sip_sorcery_object_to_ami(list, &buf)) { |
| 4564 |
ast_free(buf); |
| 4565 |
return CMP_STOP; |
| 4566 |
} |
| 4567 |
astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); |
| 4568 |
ast_free(buf); |
| 4569 |
|
| 4570 |
++ami->count; |
| 4571 |
return 0; |
| 4572 |
} |
| 4573 |
|
| 4574 |
static int ami_show_resource_lists(struct mansession *s, const struct message *m) |
| 4575 |
{ |
| 4576 |
struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), }; |
| 4577 |
struct ao2_container *lists; |
| 4578 |
|
| 4579 |
lists = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "resource_list", |
| 4580 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 4581 |
|
| 4582 |
if (!lists || !ao2_container_count(lists)) { |
| 4583 |
astman_send_error(s, m, "No resource lists found\n"); |
| 4584 |
return 0; |
| 4585 |
} |
| 4586 |
|
| 4587 |
astman_send_listack(s, m, "A listing of resource lists follows, presented as ResourceListDetail events", |
| 4588 |
"start"); |
| 4589 |
|
| 4590 |
ao2_callback(lists, OBJ_NODATA, format_ami_resource_lists, &ami); |
| 4591 |
|
| 4592 |
astman_send_list_complete_start(s, m, "ResourceListDetailComplete", ami.count); |
| 4593 |
astman_send_list_complete_end(s); |
| 4594 |
return 0; |
| 4595 |
} |
| 4596 |
|
| 4597 |
#define AMI_SHOW_SUBSCRIPTIONS_INBOUND "PJSIPShowSubscriptionsInbound" |
| 4598 |
#define AMI_SHOW_SUBSCRIPTIONS_OUTBOUND "PJSIPShowSubscriptionsOutbound" |
| 4599 |
|
| 4600 |
#define MAX_REGEX_ERROR_LEN 128 |
| 4601 |
|
| 4602 |
struct cli_sub_parms { |
| 4603 |
|
| 4604 |
struct ast_cli_entry *e; |
| 4605 |
|
| 4606 |
struct ast_cli_args *a; |
| 4607 |
|
| 4608 |
struct ast_str *buf; |
| 4609 |
|
| 4610 |
regex_t *like; |
| 4611 |
int count; |
| 4612 |
}; |
| 4613 |
|
| 4614 |
struct cli_sub_complete_parms { |
| 4615 |
struct ast_cli_args *a; |
| 4616 |
|
| 4617 |
char *callid; |
| 4618 |
int wordlen; |
| 4619 |
int which; |
| 4620 |
}; |
| 4621 |
|
| 4622 |
static int cli_complete_subscription_common(struct sip_subscription_tree *sub_tree, struct cli_sub_complete_parms *cli) |
| 4623 |
{ |
| 4624 |
pj_str_t *callid; |
| 4625 |
|
| 4626 |
if (!sub_tree->dlg) { |
| 4627 |
return 0; |
| 4628 |
} |
| 4629 |
|
| 4630 |
callid = &sub_tree->dlg->call_id->id; |
| 4631 |
if (cli->wordlen <= pj_strlen(callid) |
| 4632 |
&& !strncasecmp(cli->a->word, pj_strbuf(callid), cli->wordlen) |
| 4633 |
&& (++cli->which > cli->a->n)) { |
| 4634 |
cli->callid = ast_malloc(pj_strlen(callid) + 1); |
| 4635 |
if (cli->callid) { |
| 4636 |
ast_copy_pj_str(cli->callid, callid, pj_strlen(callid) + 1); |
| 4637 |
} |
| 4638 |
return -1; |
| 4639 |
} |
| 4640 |
return 0; |
| 4641 |
} |
| 4642 |
|
| 4643 |
static int cli_complete_subscription_inbound(struct sip_subscription_tree *sub_tree, void *arg) |
| 4644 |
{ |
| 4645 |
return sub_tree->role == AST_SIP_NOTIFIER |
| 4646 |
? cli_complete_subscription_common(sub_tree, arg) : 0; |
| 4647 |
} |
| 4648 |
|
| 4649 |
static int cli_complete_subscription_outbound(struct sip_subscription_tree *sub_tree, void *arg) |
| 4650 |
{ |
| 4651 |
return sub_tree->role == AST_SIP_SUBSCRIBER |
| 4652 |
? cli_complete_subscription_common(sub_tree, arg) : 0; |
| 4653 |
} |
| 4654 |
|
| 4655 |
static char *cli_complete_subscription_callid(struct ast_cli_args *a) |
| 4656 |
{ |
| 4657 |
struct cli_sub_complete_parms cli; |
| 4658 |
on_subscription_t on_subscription; |
| 4659 |
|
| 4660 |
if (a->pos != 4) { |
| 4661 |
return NULL; |
| 4662 |
} |
| 4663 |
|
| 4664 |
if (!strcasecmp(a->argv[3], "inbound")) { |
| 4665 |
on_subscription = cli_complete_subscription_inbound; |
| 4666 |
} else if (!strcasecmp(a->argv[3], "outbound")) { |
| 4667 |
on_subscription = cli_complete_subscription_outbound; |
| 4668 |
} else { |
| 4669 |
|
| 4670 |
ast_assert(0); |
| 4671 |
return NULL; |
| 4672 |
} |
| 4673 |
|
| 4674 |
cli.a = a; |
| 4675 |
cli.callid = NULL; |
| 4676 |
cli.wordlen = strlen(a->word); |
| 4677 |
cli.which = 0; |
| 4678 |
for_each_subscription(on_subscription, &cli); |
| 4679 |
|
| 4680 |
return cli.callid; |
| 4681 |
} |
| 4682 |
|
| 4683 |
static unsigned int cli_subscription_expiry(struct sip_subscription_tree *sub_tree) |
| 4684 |
{ |
| 4685 |
int expiry; |
| 4686 |
|
| 4687 |
expiry = sub_tree->persistence |
| 4688 |
? ast_tvdiff_ms(sub_tree->persistence->expires, ast_tvnow()) / 1000 |
| 4689 |
: 0; |
| 4690 |
if (expiry < 0) { |
| 4691 |
|
| 4692 |
expiry = 0; |
| 4693 |
} |
| 4694 |
return expiry; |
| 4695 |
} |
| 4696 |
|
| 4697 |
static int cli_show_subscription_common(struct sip_subscription_tree *sub_tree, struct cli_sub_parms *cli) |
| 4698 |
{ |
| 4699 |
const char *callid = (const char *) cli->buf; |
| 4700 |
pj_str_t *sub_callid; |
| 4701 |
struct ast_str *buf; |
| 4702 |
char *src; |
| 4703 |
char *dest; |
| 4704 |
char *key; |
| 4705 |
char *value; |
| 4706 |
char *value_end; |
| 4707 |
int key_len; |
| 4708 |
int key_filler_width; |
| 4709 |
int value_len; |
| 4710 |
|
| 4711 |
if (!sub_tree->dlg) { |
| 4712 |
return 0; |
| 4713 |
} |
| 4714 |
sub_callid = &sub_tree->dlg->call_id->id; |
| 4715 |
if (pj_strcmp2(sub_callid, callid)) { |
| 4716 |
return 0; |
| 4717 |
} |
| 4718 |
|
| 4719 |
buf = ast_str_create(512); |
| 4720 |
if (!buf) { |
| 4721 |
return -1; |
| 4722 |
} |
| 4723 |
|
| 4724 |
ast_cli(cli->a->fd, |
| 4725 |
"%-20s: %s\n" |
| 4726 |
"===========================================================================\n", |
| 4727 |
"ParameterName", "ParameterValue"); |
| 4728 |
|
| 4729 |
ast_str_append(&buf, 0, "Resource: %s\n", sub_tree->root->resource); |
| 4730 |
ast_str_append(&buf, 0, "Event: %s\n", sub_tree->root->handler->event_name); |
| 4731 |
ast_str_append(&buf, 0, "Expiry: %u\n", cli_subscription_expiry(sub_tree)); |
| 4732 |
|
| 4733 |
sip_subscription_to_ami(sub_tree, &buf); |
| 4734 |
|
| 4735 |
|
| 4736 |
src = strchr(ast_str_buffer(buf), '\r'); |
| 4737 |
if (src) { |
| 4738 |
dest = src; |
| 4739 |
++src; |
| 4740 |
while (*src) { |
| 4741 |
if (*src == '\r') { |
| 4742 |
++src; |
| 4743 |
continue; |
| 4744 |
} |
| 4745 |
*dest++ = *src++; |
| 4746 |
} |
| 4747 |
*dest = '\0'; |
| 4748 |
ast_str_update(buf); |
| 4749 |
} |
| 4750 |
|
| 4751 |
|
| 4752 |
key = ast_str_buffer(buf); |
| 4753 |
do { |
| 4754 |
value = strchr(key, ':'); |
| 4755 |
if (!value) { |
| 4756 |
break; |
| 4757 |
} |
| 4758 |
value_end = strchr(value, '\n'); |
| 4759 |
if (!value_end) { |
| 4760 |
break; |
| 4761 |
} |
| 4762 |
|
| 4763 |
|
| 4764 |
key_len = value - key; |
| 4765 |
key_filler_width = 20 - key_len; |
| 4766 |
if (key_filler_width < 0) { |
| 4767 |
key_filler_width = 0; |
| 4768 |
} |
| 4769 |
value_len = value_end - value; |
| 4770 |
|
| 4771 |
ast_cli(cli->a->fd, "%.*s%*s%.*s\n", |
| 4772 |
key_len, key, key_filler_width, "", |
| 4773 |
value_len, value); |
| 4774 |
|
| 4775 |
key = value_end + 1; |
| 4776 |
} while (*key); |
| 4777 |
ast_cli(cli->a->fd, "\n"); |
| 4778 |
|
| 4779 |
ast_free(buf); |
| 4780 |
|
| 4781 |
return -1; |
| 4782 |
} |
| 4783 |
|
| 4784 |
static int cli_show_subscription_inbound(struct sip_subscription_tree *sub_tree, void *arg) |
| 4785 |
{ |
| 4786 |
return sub_tree->role == AST_SIP_NOTIFIER |
| 4787 |
? cli_show_subscription_common(sub_tree, arg) : 0; |
| 4788 |
} |
| 4789 |
|
| 4790 |
static int cli_show_subscription_outbound(struct sip_subscription_tree *sub_tree, void *arg) |
| 4791 |
{ |
| 4792 |
return sub_tree->role == AST_SIP_SUBSCRIBER |
| 4793 |
? cli_show_subscription_common(sub_tree, arg) : 0; |
| 4794 |
} |
| 4795 |
|
| 4796 |
static char *cli_show_subscription_inout(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 4797 |
{ |
| 4798 |
on_subscription_t on_subscription; |
| 4799 |
struct cli_sub_parms cli; |
| 4800 |
|
| 4801 |
switch (cmd) { |
| 4802 |
case CLI_INIT: |
| 4803 |
e->command = "pjsip show subscription {inbound|outbound}"; |
| 4804 |
e->usage = "Usage:\n" |
| 4805 |
" pjsip show subscription inbound <call-id>\n" |
| 4806 |
" pjsip show subscription outbound <call-id>\n" |
| 4807 |
" Show active subscription with the dialog call-id\n"; |
| 4808 |
return NULL; |
| 4809 |
case CLI_GENERATE: |
| 4810 |
return cli_complete_subscription_callid(a); |
| 4811 |
} |
| 4812 |
|
| 4813 |
if (a->argc != 5) { |
| 4814 |
return CLI_SHOWUSAGE; |
| 4815 |
} |
| 4816 |
|
| 4817 |
if (!strcasecmp(a->argv[3], "inbound")) { |
| 4818 |
on_subscription = cli_show_subscription_inbound; |
| 4819 |
} else if (!strcasecmp(a->argv[3], "outbound")) { |
| 4820 |
on_subscription = cli_show_subscription_outbound; |
| 4821 |
} else { |
| 4822 |
|
| 4823 |
ast_assert(0); |
| 4824 |
return NULL; |
| 4825 |
} |
| 4826 |
|
| 4827 |
|
| 4828 |
cli.a = a; |
| 4829 |
cli.e = e; |
| 4830 |
cli.buf = (void *) a->argv[4]; |
| 4831 |
for_each_subscription(on_subscription, &cli); |
| 4832 |
|
| 4833 |
return CLI_SUCCESS; |
| 4834 |
} |
| 4835 |
|
| 4836 |
#define CLI_SHOW_SUB_FORMAT_HEADER \ |
| 4837 |
"Endpoint: <Endpoint/Caller-ID.............................................>\n" \ |
| 4838 |
"Resource: <Resource/Event.................................................>\n" \ |
| 4839 |
" Expiry: <Expiry> <Call-id..............................................>\n" \ |
| 4840 |
"===========================================================================\n\n" |
| 4841 |
#define CLI_SHOW_SUB_FORMAT_ENTRY \ |
| 4842 |
"Endpoint: %s/%s\n" \ |
| 4843 |
"Resource: %s/%s\n" \ |
| 4844 |
" Expiry: %8d %s\n\n" |
| 4845 |
|
| 4846 |
static int cli_show_subscriptions_detail(struct sip_subscription_tree *sub_tree, struct cli_sub_parms *cli) |
| 4847 |
{ |
| 4848 |
char caller_id[256]; |
| 4849 |
char callid[256]; |
| 4850 |
|
| 4851 |
ast_callerid_merge(caller_id, sizeof(caller_id), |
| 4852 |
S_COR(sub_tree->endpoint->id.self.name.valid, |
| 4853 |
sub_tree->endpoint->id.self.name.str, NULL), |
| 4854 |
S_COR(sub_tree->endpoint->id.self.number.valid, |
| 4855 |
sub_tree->endpoint->id.self.number.str, NULL), |
| 4856 |
"<none>"); |
| 4857 |
|
| 4858 |
|
| 4859 |
if (sub_tree->dlg) { |
| 4860 |
ast_copy_pj_str(callid, &sub_tree->dlg->call_id->id, sizeof(callid)); |
| 4861 |
} else { |
| 4862 |
ast_copy_string(callid, "<unknown>", sizeof(callid)); |
| 4863 |
} |
| 4864 |
|
| 4865 |
ast_str_set(&cli->buf, 0, CLI_SHOW_SUB_FORMAT_ENTRY, |
| 4866 |
ast_sorcery_object_get_id(sub_tree->endpoint), caller_id, |
| 4867 |
sub_tree->root->resource, sub_tree->root->handler->event_name, |
| 4868 |
cli_subscription_expiry(sub_tree), callid); |
| 4869 |
|
| 4870 |
if (cli->like) { |
| 4871 |
if (regexec(cli->like, ast_str_buffer(cli->buf), 0, NULL, 0)) { |
| 4872 |
|
| 4873 |
return 0; |
| 4874 |
} |
| 4875 |
} |
| 4876 |
|
| 4877 |
ast_cli(cli->a->fd, "%s", ast_str_buffer(cli->buf)); |
| 4878 |
++cli->count; |
| 4879 |
|
| 4880 |
return 0; |
| 4881 |
} |
| 4882 |
|
| 4883 |
static int cli_show_subscriptions_inbound(struct sip_subscription_tree *sub_tree, void *arg) |
| 4884 |
{ |
| 4885 |
return sub_tree->role == AST_SIP_NOTIFIER |
| 4886 |
? cli_show_subscriptions_detail(sub_tree, arg) : 0; |
| 4887 |
} |
| 4888 |
|
| 4889 |
static int cli_show_subscriptions_outbound(struct sip_subscription_tree *sub_tree, void *arg) |
| 4890 |
{ |
| 4891 |
return sub_tree->role == AST_SIP_SUBSCRIBER |
| 4892 |
? cli_show_subscriptions_detail(sub_tree, arg) : 0; |
| 4893 |
} |
| 4894 |
|
| 4895 |
static char *cli_show_subscriptions_inout(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 4896 |
{ |
| 4897 |
on_subscription_t on_subscription; |
| 4898 |
struct cli_sub_parms cli; |
| 4899 |
regex_t like; |
| 4900 |
const char *regex; |
| 4901 |
|
| 4902 |
switch (cmd) { |
| 4903 |
case CLI_INIT: |
| 4904 |
e->command = "pjsip show subscriptions {inbound|outbound} [like]"; |
| 4905 |
e->usage = "Usage:\n" |
| 4906 |
" pjsip show subscriptions inbound [like <regex>]\n" |
| 4907 |
" Show active inbound subscriptions\n" |
| 4908 |
" pjsip show subscriptions outbound [like <regex>]\n" |
| 4909 |
" Show active outbound subscriptions\n" |
| 4910 |
"\n" |
| 4911 |
" The regex selects a subscriptions output that matches.\n" |
| 4912 |
" i.e., All output lines for a subscription are checked\n" |
| 4913 |
" as a block by the regex.\n"; |
| 4914 |
return NULL; |
| 4915 |
case CLI_GENERATE: |
| 4916 |
return NULL; |
| 4917 |
} |
| 4918 |
|
| 4919 |
if (a->argc != 4 && a->argc != 6) { |
| 4920 |
return CLI_SHOWUSAGE; |
| 4921 |
} |
| 4922 |
if (!strcasecmp(a->argv[3], "inbound")) { |
| 4923 |
on_subscription = cli_show_subscriptions_inbound; |
| 4924 |
} else if (!strcasecmp(a->argv[3], "outbound")) { |
| 4925 |
on_subscription = cli_show_subscriptions_outbound; |
| 4926 |
} else { |
| 4927 |
|
| 4928 |
ast_assert(0); |
| 4929 |
return CLI_SHOWUSAGE; |
| 4930 |
} |
| 4931 |
if (a->argc == 6) { |
| 4932 |
int rc; |
| 4933 |
|
| 4934 |
if (strcasecmp(a->argv[4], "like")) { |
| 4935 |
return CLI_SHOWUSAGE; |
| 4936 |
} |
| 4937 |
|
| 4938 |
|
| 4939 |
memset(&like, 0, sizeof(like)); |
| 4940 |
cli.like = &like; |
| 4941 |
regex = a->argv[5]; |
| 4942 |
rc = regcomp(cli.like, regex, REG_EXTENDED | REG_NOSUB); |
| 4943 |
if (rc) { |
| 4944 |
char *regerr = ast_alloca(MAX_REGEX_ERROR_LEN); |
| 4945 |
|
| 4946 |
regerror(rc, cli.like, regerr, MAX_REGEX_ERROR_LEN); |
| 4947 |
ast_cli(a->fd, "Regular expression '%s' failed to compile: %s\n", |
| 4948 |
regex, regerr); |
| 4949 |
return CLI_FAILURE; |
| 4950 |
} |
| 4951 |
} else { |
| 4952 |
cli.like = NULL; |
| 4953 |
regex = NULL; |
| 4954 |
} |
| 4955 |
|
| 4956 |
cli.a = a; |
| 4957 |
cli.e = e; |
| 4958 |
cli.count = 0; |
| 4959 |
cli.buf = ast_str_create(256); |
| 4960 |
if (!cli.buf) { |
| 4961 |
if (cli.like) { |
| 4962 |
regfree(cli.like); |
| 4963 |
} |
| 4964 |
return CLI_FAILURE; |
| 4965 |
} |
| 4966 |
|
| 4967 |
ast_cli(a->fd, CLI_SHOW_SUB_FORMAT_HEADER); |
| 4968 |
for_each_subscription(on_subscription, &cli); |
| 4969 |
ast_cli(a->fd, "%d active subscriptions%s%s%s\n", |
| 4970 |
cli.count, |
| 4971 |
regex ? " matched \"" : "", |
| 4972 |
regex ?: "", |
| 4973 |
regex ? "\"" : ""); |
| 4974 |
|
| 4975 |
ast_free(cli.buf); |
| 4976 |
if (cli.like) { |
| 4977 |
regfree(cli.like); |
| 4978 |
} |
| 4979 |
|
| 4980 |
return CLI_SUCCESS; |
| 4981 |
} |
| 4982 |
|
| 4983 |
#define CLI_LIST_SUB_FORMAT_HEADER "%-30.30s %-30.30s %6.6s %s\n" |
| 4984 |
#define CLI_LIST_SUB_FORMAT_ENTRY "%-30.30s %-30.30s %6d %s\n" |
| 4985 |
|
| 4986 |
static int cli_list_subscriptions_detail(struct sip_subscription_tree *sub_tree, struct cli_sub_parms *cli) |
| 4987 |
{ |
| 4988 |
char ep_cid_buf[50]; |
| 4989 |
char res_evt_buf[50]; |
| 4990 |
char callid[256]; |
| 4991 |
|
| 4992 |
|
| 4993 |
snprintf(ep_cid_buf, sizeof(ep_cid_buf), "%s/%s", |
| 4994 |
ast_sorcery_object_get_id(sub_tree->endpoint), |
| 4995 |
S_COR(sub_tree->endpoint->id.self.name.valid, sub_tree->endpoint->id.self.name.str, |
| 4996 |
S_COR(sub_tree->endpoint->id.self.number.valid, |
| 4997 |
sub_tree->endpoint->id.self.number.str, "<none>"))); |
| 4998 |
|
| 4999 |
|
| 5000 |
snprintf(res_evt_buf, sizeof(res_evt_buf), "%s/%s", |
| 5001 |
sub_tree->root->resource, |
| 5002 |
sub_tree->root->handler->event_name); |
| 5003 |
|
| 5004 |
|
| 5005 |
if (sub_tree->dlg) { |
| 5006 |
ast_copy_pj_str(callid, &sub_tree->dlg->call_id->id, sizeof(callid)); |
| 5007 |
} else { |
| 5008 |
ast_copy_string(callid, "<unknown>", sizeof(callid)); |
| 5009 |
} |
| 5010 |
|
| 5011 |
ast_str_set(&cli->buf, 0, CLI_LIST_SUB_FORMAT_ENTRY, |
| 5012 |
ep_cid_buf, |
| 5013 |
res_evt_buf, |
| 5014 |
cli_subscription_expiry(sub_tree), |
| 5015 |
callid); |
| 5016 |
|
| 5017 |
if (cli->like) { |
| 5018 |
if (regexec(cli->like, ast_str_buffer(cli->buf), 0, NULL, 0)) { |
| 5019 |
|
| 5020 |
return 0; |
| 5021 |
} |
| 5022 |
} |
| 5023 |
|
| 5024 |
ast_cli(cli->a->fd, "%s", ast_str_buffer(cli->buf)); |
| 5025 |
++cli->count; |
| 5026 |
|
| 5027 |
return 0; |
| 5028 |
} |
| 5029 |
|
| 5030 |
static int cli_list_subscriptions_inbound(struct sip_subscription_tree *sub_tree, void *arg) |
| 5031 |
{ |
| 5032 |
return sub_tree->role == AST_SIP_NOTIFIER |
| 5033 |
? cli_list_subscriptions_detail(sub_tree, arg) : 0; |
| 5034 |
} |
| 5035 |
|
| 5036 |
static int cli_list_subscriptions_outbound(struct sip_subscription_tree *sub_tree, void *arg) |
| 5037 |
{ |
| 5038 |
return sub_tree->role == AST_SIP_SUBSCRIBER |
| 5039 |
? cli_list_subscriptions_detail(sub_tree, arg) : 0; |
| 5040 |
} |
| 5041 |
|
| 5042 |
static char *cli_list_subscriptions_inout(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 5043 |
{ |
| 5044 |
on_subscription_t on_subscription; |
| 5045 |
struct cli_sub_parms cli; |
| 5046 |
regex_t like; |
| 5047 |
const char *regex; |
| 5048 |
|
| 5049 |
switch (cmd) { |
| 5050 |
case CLI_INIT: |
| 5051 |
e->command = "pjsip list subscriptions {inbound|outbound} [like]"; |
| 5052 |
e->usage = "Usage:\n" |
| 5053 |
" pjsip list subscriptions inbound [like <regex>]\n" |
| 5054 |
" List active inbound subscriptions\n" |
| 5055 |
" pjsip list subscriptions outbound [like <regex>]\n" |
| 5056 |
" List active outbound subscriptions\n" |
| 5057 |
"\n" |
| 5058 |
" The regex selects output lines that match.\n"; |
| 5059 |
return NULL; |
| 5060 |
case CLI_GENERATE: |
| 5061 |
return NULL; |
| 5062 |
} |
| 5063 |
|
| 5064 |
if (a->argc != 4 && a->argc != 6) { |
| 5065 |
return CLI_SHOWUSAGE; |
| 5066 |
} |
| 5067 |
if (!strcasecmp(a->argv[3], "inbound")) { |
| 5068 |
on_subscription = cli_list_subscriptions_inbound; |
| 5069 |
} else if (!strcasecmp(a->argv[3], "outbound")) { |
| 5070 |
on_subscription = cli_list_subscriptions_outbound; |
| 5071 |
} else { |
| 5072 |
|
| 5073 |
ast_assert(0); |
| 5074 |
return CLI_SHOWUSAGE; |
| 5075 |
} |
| 5076 |
if (a->argc == 6) { |
| 5077 |
int rc; |
| 5078 |
|
| 5079 |
if (strcasecmp(a->argv[4], "like")) { |
| 5080 |
return CLI_SHOWUSAGE; |
| 5081 |
} |
| 5082 |
|
| 5083 |
|
| 5084 |
memset(&like, 0, sizeof(like)); |
| 5085 |
cli.like = &like; |
| 5086 |
regex = a->argv[5]; |
| 5087 |
rc = regcomp(cli.like, regex, REG_EXTENDED | REG_NOSUB); |
| 5088 |
if (rc) { |
| 5089 |
char *regerr = ast_alloca(MAX_REGEX_ERROR_LEN); |
| 5090 |
|
| 5091 |
regerror(rc, cli.like, regerr, MAX_REGEX_ERROR_LEN); |
| 5092 |
ast_cli(a->fd, "Regular expression '%s' failed to compile: %s\n", |
| 5093 |
regex, regerr); |
| 5094 |
return CLI_FAILURE; |
| 5095 |
} |
| 5096 |
} else { |
| 5097 |
cli.like = NULL; |
| 5098 |
regex = NULL; |
| 5099 |
} |
| 5100 |
|
| 5101 |
cli.a = a; |
| 5102 |
cli.e = e; |
| 5103 |
cli.count = 0; |
| 5104 |
cli.buf = ast_str_create(256); |
| 5105 |
if (!cli.buf) { |
| 5106 |
if (cli.like) { |
| 5107 |
regfree(cli.like); |
| 5108 |
} |
| 5109 |
return CLI_FAILURE; |
| 5110 |
} |
| 5111 |
|
| 5112 |
ast_cli(a->fd, CLI_LIST_SUB_FORMAT_HEADER, |
| 5113 |
"Endpoint/CLI", "Resource/Event", "Expiry", "Call-id"); |
| 5114 |
for_each_subscription(on_subscription, &cli); |
| 5115 |
ast_cli(a->fd, "\n%d active subscriptions%s%s%s\n", |
| 5116 |
cli.count, |
| 5117 |
regex ? " matched \"" : "", |
| 5118 |
regex ?: "", |
| 5119 |
regex ? "\"" : ""); |
| 5120 |
|
| 5121 |
ast_free(cli.buf); |
| 5122 |
if (cli.like) { |
| 5123 |
regfree(cli.like); |
| 5124 |
} |
| 5125 |
|
| 5126 |
return CLI_SUCCESS; |
| 5127 |
} |
| 5128 |
|
| 5129 |
static struct ast_cli_entry cli_commands[] = { |
| 5130 |
AST_CLI_DEFINE(cli_list_subscriptions_inout, "List active inbound/outbound subscriptions"), |
| 5131 |
AST_CLI_DEFINE(cli_show_subscription_inout, "Show active subscription details"), |
| 5132 |
AST_CLI_DEFINE(cli_show_subscriptions_inout, "Show active inbound/outbound subscriptions"), |
| 5133 |
}; |
| 5134 |
|
| 5135 |
static int persistence_endpoint_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 5136 |
{ |
| 5137 |
struct subscription_persistence *persistence = obj; |
| 5138 |
|
| 5139 |
persistence->endpoint = ast_strdup(var->value); |
| 5140 |
return 0; |
| 5141 |
} |
| 5142 |
|
| 5143 |
static int persistence_endpoint_struct2str(const void *obj, const intptr_t *args, char **buf) |
| 5144 |
{ |
| 5145 |
const struct subscription_persistence *persistence = obj; |
| 5146 |
|
| 5147 |
*buf = ast_strdup(persistence->endpoint); |
| 5148 |
return 0; |
| 5149 |
} |
| 5150 |
|
| 5151 |
static int persistence_tag_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 5152 |
{ |
| 5153 |
struct subscription_persistence *persistence = obj; |
| 5154 |
|
| 5155 |
persistence->tag = ast_strdup(var->value); |
| 5156 |
return 0; |
| 5157 |
} |
| 5158 |
|
| 5159 |
static int persistence_tag_struct2str(const void *obj, const intptr_t *args, char **buf) |
| 5160 |
{ |
| 5161 |
const struct subscription_persistence *persistence = obj; |
| 5162 |
|
| 5163 |
*buf = ast_strdup(persistence->tag); |
| 5164 |
return 0; |
| 5165 |
} |
| 5166 |
|
| 5167 |
static int persistence_generator_data_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 5168 |
{ |
| 5169 |
struct subscription_persistence *persistence = obj; |
| 5170 |
struct ast_json_error error; |
| 5171 |
|
| 5172 |
|
| 5173 |
|
| 5174 |
|
| 5175 |
persistence->generator_data = ast_json_load_string(var->value, &error); |
| 5176 |
|
| 5177 |
return 0; |
| 5178 |
} |
| 5179 |
|
| 5180 |
static int persistence_generator_data_struct2str(const void *obj, const intptr_t *args, char **buf) |
| 5181 |
{ |
| 5182 |
const struct subscription_persistence *persistence = obj; |
| 5183 |
char *value; |
| 5184 |
|
| 5185 |
if (!persistence->generator_data) { |
| 5186 |
return 0; |
| 5187 |
} |
| 5188 |
|
| 5189 |
value = ast_json_dump_string(persistence->generator_data); |
| 5190 |
if (!value) { |
| 5191 |
return -1; |
| 5192 |
} |
| 5193 |
|
| 5194 |
*buf = ast_strdup(value); |
| 5195 |
ast_json_free(value); |
| 5196 |
|
| 5197 |
return 0; |
| 5198 |
} |
| 5199 |
|
| 5200 |
static int persistence_expires_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 5201 |
{ |
| 5202 |
struct subscription_persistence *persistence = obj; |
| 5203 |
return ast_get_timeval(var->value, &persistence->expires, ast_tv(0, 0), NULL); |
| 5204 |
} |
| 5205 |
|
| 5206 |
static int persistence_expires_struct2str(const void *obj, const intptr_t *args, char **buf) |
| 5207 |
{ |
| 5208 |
const struct subscription_persistence *persistence = obj; |
| 5209 |
char secs[AST_TIME_T_LEN]; |
| 5210 |
|
| 5211 |
ast_time_t_to_string(persistence->expires.tv_sec, secs, sizeof(secs)); |
| 5212 |
|
| 5213 |
return (ast_asprintf(buf, "%s", secs) < 0) ? -1 : 0; |
| 5214 |
} |
| 5215 |
|
| 5216 |
#define RESOURCE_LIST_INIT_SIZE 4 |
| 5217 |
|
| 5218 |
static void resource_list_destructor(void *obj) |
| 5219 |
{ |
| 5220 |
struct resource_list *list = obj; |
| 5221 |
int i; |
| 5222 |
|
| 5223 |
for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) { |
| 5224 |
ast_free((char *) AST_VECTOR_GET(&list->items, i)); |
| 5225 |
} |
| 5226 |
|
| 5227 |
AST_VECTOR_FREE(&list->items); |
| 5228 |
} |
| 5229 |
|
| 5230 |
static void *resource_list_alloc(const char *name) |
| 5231 |
{ |
| 5232 |
struct resource_list *list; |
| 5233 |
|
| 5234 |
list = ast_sorcery_generic_alloc(sizeof(*list), resource_list_destructor); |
| 5235 |
if (!list) { |
| 5236 |
return NULL; |
| 5237 |
} |
| 5238 |
|
| 5239 |
if (AST_VECTOR_INIT(&list->items, RESOURCE_LIST_INIT_SIZE)) { |
| 5240 |
ao2_cleanup(list); |
| 5241 |
return NULL; |
| 5242 |
} |
| 5243 |
|
| 5244 |
return list; |
| 5245 |
} |
| 5246 |
|
| 5247 |
static int item_in_vector(const struct resource_list *list, const char *item) |
| 5248 |
{ |
| 5249 |
int i; |
| 5250 |
|
| 5251 |
for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) { |
| 5252 |
if (!strcmp(item, AST_VECTOR_GET(&list->items, i))) { |
| 5253 |
return 1; |
| 5254 |
} |
| 5255 |
} |
| 5256 |
|
| 5257 |
return 0; |
| 5258 |
} |
| 5259 |
|
| 5260 |
static int list_item_handler(const struct aco_option *opt, |
| 5261 |
struct ast_variable *var, void *obj) |
| 5262 |
{ |
| 5263 |
struct resource_list *list = obj; |
| 5264 |
char *items = ast_strdupa(var->value); |
| 5265 |
char *item; |
| 5266 |
|
| 5267 |
while ((item = ast_strip(strsep(&items, ",")))) { |
| 5268 |
if (ast_strlen_zero(item)) { |
| 5269 |
continue; |
| 5270 |
} |
| 5271 |
|
| 5272 |
if (item_in_vector(list, item)) { |
| 5273 |
ast_log(LOG_WARNING, "Ignoring duplicated list item '%s'\n", item); |
| 5274 |
continue; |
| 5275 |
} |
| 5276 |
|
| 5277 |
item = ast_strdup(item); |
| 5278 |
if (!item || AST_VECTOR_APPEND(&list->items, item)) { |
| 5279 |
ast_free(item); |
| 5280 |
return -1; |
| 5281 |
} |
| 5282 |
} |
| 5283 |
|
| 5284 |
return 0; |
| 5285 |
} |
| 5286 |
|
| 5287 |
static int list_item_to_str(const void *obj, const intptr_t *args, char **buf) |
| 5288 |
{ |
| 5289 |
const struct resource_list *list = obj; |
| 5290 |
int i; |
| 5291 |
struct ast_str *str = ast_str_create(32); |
| 5292 |
|
| 5293 |
for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) { |
| 5294 |
ast_str_append(&str, 0, "%s,", AST_VECTOR_GET(&list->items, i)); |
| 5295 |
} |
| 5296 |
|
| 5297 |
|
| 5298 |
ast_str_truncate(str, -1); |
| 5299 |
*buf = ast_strdup(ast_str_buffer(str)); |
| 5300 |
ast_free(str); |
| 5301 |
return 0; |
| 5302 |
} |
| 5303 |
|
| 5304 |
static int resource_list_apply_handler(const struct ast_sorcery *sorcery, void *obj) |
| 5305 |
{ |
| 5306 |
struct resource_list *list = obj; |
| 5307 |
|
| 5308 |
if (ast_strlen_zero(list->event)) { |
| 5309 |
ast_log(LOG_WARNING, "Resource list '%s' has no event set\n", |
| 5310 |
ast_sorcery_object_get_id(list)); |
| 5311 |
return -1; |
| 5312 |
} |
| 5313 |
|
| 5314 |
if (AST_VECTOR_SIZE(&list->items) == 0) { |
| 5315 |
ast_log(LOG_WARNING, "Resource list '%s' has no list items\n", |
| 5316 |
ast_sorcery_object_get_id(list)); |
| 5317 |
return -1; |
| 5318 |
} |
| 5319 |
|
| 5320 |
return 0; |
| 5321 |
} |
| 5322 |
|
| 5323 |
static int apply_list_configuration(struct ast_sorcery *sorcery) |
| 5324 |
{ |
| 5325 |
ast_sorcery_apply_default(sorcery, "resource_list", "config", |
| 5326 |
"pjsip.conf,criteria=type=resource_list"); |
| 5327 |
if (ast_sorcery_object_register(sorcery, "resource_list", resource_list_alloc, |
| 5328 |
NULL, resource_list_apply_handler)) { |
| 5329 |
return -1; |
| 5330 |
} |
| 5331 |
|
| 5332 |
ast_sorcery_object_field_register(sorcery, "resource_list", "type", "", |
| 5333 |
OPT_NOOP_T, 0, 0); |
| 5334 |
ast_sorcery_object_field_register(sorcery, "resource_list", "event", "", |
| 5335 |
OPT_CHAR_ARRAY_T, 1, CHARFLDSET(struct resource_list, event)); |
| 5336 |
ast_sorcery_object_field_register(sorcery, "resource_list", "full_state", "no", |
| 5337 |
OPT_BOOL_T, 1, FLDSET(struct resource_list, full_state)); |
| 5338 |
ast_sorcery_object_field_register(sorcery, "resource_list", "notification_batch_interval", |
| 5339 |
"0", OPT_UINT_T, 0, FLDSET(struct resource_list, notification_batch_interval)); |
| 5340 |
ast_sorcery_object_field_register_custom(sorcery, "resource_list", "list_item", |
| 5341 |
"", list_item_handler, list_item_to_str, NULL, 0, 0); |
| 5342 |
ast_sorcery_object_field_register(sorcery, "resource_list", "resource_display_name", "no", |
| 5343 |
OPT_BOOL_T, 1, FLDSET(struct resource_list, resource_display_name)); |
| 5344 |
|
| 5345 |
ast_sorcery_reload_object(sorcery, "resource_list"); |
| 5346 |
|
| 5347 |
return 0; |
| 5348 |
} |
| 5349 |
|
| 5350 |
#ifdef TEST_FRAMEWORK |
| 5351 |
|
| 5352 |
|
| 5353 |
|
| 5354 |
|
| 5355 |
|
| 5356 |
|
| 5357 |
const char *bad_resources[] = { |
| 5358 |
"coconut", |
| 5359 |
"cilantro", |
| 5360 |
"olive", |
| 5361 |
"cheese", |
| 5362 |
}; |
| 5363 |
|
| 5364 |
|
| 5365 |
|
| 5366 |
|
| 5367 |
|
| 5368 |
|
| 5369 |
static int test_new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource) |
| 5370 |
{ |
| 5371 |
int i; |
| 5372 |
|
| 5373 |
for (i = 0; i < ARRAY_LEN(bad_resources); ++i) { |
| 5374 |
if (!strcmp(resource, bad_resources[i])) { |
| 5375 |
return 400; |
| 5376 |
} |
| 5377 |
} |
| 5378 |
|
| 5379 |
return 200; |
| 5380 |
} |
| 5381 |
|
| 5382 |
|
| 5383 |
|
| 5384 |
|
| 5385 |
|
| 5386 |
|
| 5387 |
|
| 5388 |
struct ast_sip_notifier test_notifier = { |
| 5389 |
.new_subscribe = test_new_subscribe, |
| 5390 |
}; |
| 5391 |
|
| 5392 |
|
| 5393 |
|
| 5394 |
|
| 5395 |
struct ast_sip_subscription_handler test_handler = { |
| 5396 |
.event_name = "test", |
| 5397 |
.body_type = "", |
| 5398 |
.notifier = &test_notifier, |
| 5399 |
}; |
| 5400 |
|
| 5401 |
|
| 5402 |
|
| 5403 |
|
| 5404 |
|
| 5405 |
|
| 5406 |
|
| 5407 |
|
| 5408 |
|
| 5409 |
|
| 5410 |
|
| 5411 |
static int populate_list(struct resource_list *list, const char *event, const char **resources, size_t num_resources) |
| 5412 |
{ |
| 5413 |
int i; |
| 5414 |
|
| 5415 |
ast_copy_string(list->event, event, sizeof(list->event)); |
| 5416 |
|
| 5417 |
for (i = 0; i < num_resources; ++i) { |
| 5418 |
char *resource = ast_strdup(resources[i]); |
| 5419 |
|
| 5420 |
if (!resource || AST_VECTOR_APPEND(&list->items, resource)) { |
| 5421 |
ast_free(resource); |
| 5422 |
return -1; |
| 5423 |
} |
| 5424 |
} |
| 5425 |
return 0; |
| 5426 |
} |
| 5427 |
|
| 5428 |
|
| 5429 |
|
| 5430 |
|
| 5431 |
static void cleanup_resource_list(struct resource_list *list) |
| 5432 |
{ |
| 5433 |
if (!list) { |
| 5434 |
return; |
| 5435 |
} |
| 5436 |
|
| 5437 |
ast_sorcery_delete(ast_sip_get_sorcery(), list); |
| 5438 |
ao2_cleanup(list); |
| 5439 |
} |
| 5440 |
|
| 5441 |
|
| 5442 |
|
| 5443 |
|
| 5444 |
|
| 5445 |
|
| 5446 |
|
| 5447 |
|
| 5448 |
|
| 5449 |
|
| 5450 |
|
| 5451 |
|
| 5452 |
static struct resource_list *create_resource_list(struct ast_test *test, |
| 5453 |
const char *list_name, const char *event, const char **resources, size_t num_resources) |
| 5454 |
{ |
| 5455 |
struct resource_list *list; |
| 5456 |
|
| 5457 |
list = ast_sorcery_alloc(ast_sip_get_sorcery(), "resource_list", list_name); |
| 5458 |
if (!list) { |
| 5459 |
ast_test_status_update(test, "Could not allocate resource list in sorcery\n"); |
| 5460 |
return NULL; |
| 5461 |
} |
| 5462 |
|
| 5463 |
if (ast_sorcery_create(ast_sip_get_sorcery(), list)) { |
| 5464 |
ast_test_status_update(test, "Could not store the resource list in sorcery\n"); |
| 5465 |
ao2_cleanup(list); |
| 5466 |
return NULL; |
| 5467 |
} |
| 5468 |
|
| 5469 |
if (populate_list(list, event, resources, num_resources)) { |
| 5470 |
ast_test_status_update(test, "Could not add resources to the resource list\n"); |
| 5471 |
cleanup_resource_list(list); |
| 5472 |
return NULL; |
| 5473 |
} |
| 5474 |
|
| 5475 |
return list; |
| 5476 |
} |
| 5477 |
|
| 5478 |
|
| 5479 |
|
| 5480 |
|
| 5481 |
|
| 5482 |
|
| 5483 |
|
| 5484 |
|
| 5485 |
|
| 5486 |
|
| 5487 |
|
| 5488 |
|
| 5489 |
|
| 5490 |
|
| 5491 |
static int check_node(struct ast_test *test, struct tree_node *node, |
| 5492 |
const char **resources, size_t num_resources) |
| 5493 |
{ |
| 5494 |
int i; |
| 5495 |
|
| 5496 |
if (AST_VECTOR_SIZE(&node->children) != num_resources) { |
| 5497 |
ast_test_status_update(test, "Unexpected number of resources in tree. Expected %zu, got %zu\n", |
| 5498 |
num_resources, AST_VECTOR_SIZE(&node->children)); |
| 5499 |
return -1; |
| 5500 |
} |
| 5501 |
|
| 5502 |
for (i = 0; i < num_resources; ++i) { |
| 5503 |
if (strcmp(resources[i], AST_VECTOR_GET(&node->children, i)->resource)) { |
| 5504 |
ast_test_status_update(test, "Mismatched resources. Expected '%s' but got '%s'\n", |
| 5505 |
resources[i], AST_VECTOR_GET(&node->children, i)->resource); |
| 5506 |
return -1; |
| 5507 |
} |
| 5508 |
} |
| 5509 |
|
| 5510 |
return 0; |
| 5511 |
} |
| 5512 |
|
| 5513 |
|
| 5514 |
|
| 5515 |
|
| 5516 |
static void test_resource_tree_destroy(struct resource_tree *tree) |
| 5517 |
{ |
| 5518 |
resource_tree_destroy(tree); |
| 5519 |
ast_free(tree); |
| 5520 |
} |
| 5521 |
|
| 5522 |
static int ineligible_configuration(void) |
| 5523 |
{ |
| 5524 |
struct ast_config *config; |
| 5525 |
struct ast_flags flags = {0,}; |
| 5526 |
const char *value; |
| 5527 |
|
| 5528 |
config = ast_config_load("sorcery.conf", flags); |
| 5529 |
if (!config) { |
| 5530 |
return 1; |
| 5531 |
} |
| 5532 |
|
| 5533 |
value = ast_variable_retrieve(config, "res_pjsip_pubsub", "resource_list"); |
| 5534 |
if (ast_strlen_zero(value)) { |
| 5535 |
ast_config_destroy(config); |
| 5536 |
return 1; |
| 5537 |
} |
| 5538 |
|
| 5539 |
if (strcasecmp(value, "memory") && strcasecmp(value, "astdb")) { |
| 5540 |
ast_config_destroy(config); |
| 5541 |
return 1; |
| 5542 |
} |
| 5543 |
|
| 5544 |
return 0; |
| 5545 |
} |
| 5546 |
|
| 5547 |
AST_TEST_DEFINE(resource_tree) |
| 5548 |
{ |
| 5549 |
RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list); |
| 5550 |
RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy); |
| 5551 |
const char *resources[] = { |
| 5552 |
"huey", |
| 5553 |
"dewey", |
| 5554 |
"louie", |
| 5555 |
}; |
| 5556 |
int resp; |
| 5557 |
|
| 5558 |
switch (cmd) { |
| 5559 |
case TEST_INIT: |
| 5560 |
info->name = "resource_tree"; |
| 5561 |
info->category = "/res/res_pjsip_pubsub/"; |
| 5562 |
info->summary = "Basic resource tree integrity check"; |
| 5563 |
info->description = |
| 5564 |
"Create a resource list and ensure that our attempt to build a tree works as expected."; |
| 5565 |
return AST_TEST_NOT_RUN; |
| 5566 |
case TEST_EXECUTE: |
| 5567 |
break; |
| 5568 |
} |
| 5569 |
|
| 5570 |
if (ineligible_configuration()) { |
| 5571 |
ast_test_status_update(test, "Ineligible configuration for this test. Please add a " |
| 5572 |
"'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n"); |
| 5573 |
return AST_TEST_NOT_RUN; |
| 5574 |
} |
| 5575 |
|
| 5576 |
list = create_resource_list(test, "foo", "test", resources, ARRAY_LEN(resources)); |
| 5577 |
if (!list) { |
| 5578 |
return AST_TEST_FAIL; |
| 5579 |
} |
| 5580 |
|
| 5581 |
tree = ast_calloc(1, sizeof(*tree)); |
| 5582 |
resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1, NULL); |
| 5583 |
if (resp != 200) { |
| 5584 |
ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp); |
| 5585 |
return AST_TEST_FAIL; |
| 5586 |
} |
| 5587 |
|
| 5588 |
if (!tree->root) { |
| 5589 |
ast_test_status_update(test, "Resource tree has no root\n"); |
| 5590 |
return AST_TEST_FAIL; |
| 5591 |
} |
| 5592 |
|
| 5593 |
if (check_node(test, tree->root, resources, ARRAY_LEN(resources))) { |
| 5594 |
return AST_TEST_FAIL; |
| 5595 |
} |
| 5596 |
|
| 5597 |
return AST_TEST_PASS; |
| 5598 |
} |
| 5599 |
|
| 5600 |
AST_TEST_DEFINE(complex_resource_tree) |
| 5601 |
{ |
| 5602 |
RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list); |
| 5603 |
RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list); |
| 5604 |
RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy); |
| 5605 |
const char *resources_1[] = { |
| 5606 |
"huey", |
| 5607 |
"dewey", |
| 5608 |
"louie", |
| 5609 |
"dwarves", |
| 5610 |
}; |
| 5611 |
const char *resources_2[] = { |
| 5612 |
"happy", |
| 5613 |
"grumpy", |
| 5614 |
"doc", |
| 5615 |
"bashful", |
| 5616 |
"dopey", |
| 5617 |
"sneezy", |
| 5618 |
"sleepy", |
| 5619 |
}; |
| 5620 |
int resp; |
| 5621 |
struct tree_node *node; |
| 5622 |
|
| 5623 |
switch (cmd) { |
| 5624 |
case TEST_INIT: |
| 5625 |
info->name = "complex_resource_tree"; |
| 5626 |
info->category = "/res/res_pjsip_pubsub/"; |
| 5627 |
info->summary = "Complex resource tree integrity check"; |
| 5628 |
info->description = |
| 5629 |
"Create a complex resource list and ensure that our attempt to build a tree works as expected."; |
| 5630 |
return AST_TEST_NOT_RUN; |
| 5631 |
case TEST_EXECUTE: |
| 5632 |
break; |
| 5633 |
} |
| 5634 |
|
| 5635 |
if (ineligible_configuration()) { |
| 5636 |
ast_test_status_update(test, "Ineligible configuration for this test. Please add a " |
| 5637 |
"'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n"); |
| 5638 |
return AST_TEST_NOT_RUN; |
| 5639 |
} |
| 5640 |
|
| 5641 |
list_1 = create_resource_list(test, "foo", "test", resources_1, ARRAY_LEN(resources_1)); |
| 5642 |
if (!list_1) { |
| 5643 |
return AST_TEST_FAIL; |
| 5644 |
} |
| 5645 |
|
| 5646 |
list_2 = create_resource_list(test, "dwarves", "test", resources_2, ARRAY_LEN(resources_2)); |
| 5647 |
if (!list_2) { |
| 5648 |
return AST_TEST_FAIL; |
| 5649 |
} |
| 5650 |
|
| 5651 |
tree = ast_calloc(1, sizeof(*tree)); |
| 5652 |
resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1, NULL); |
| 5653 |
if (resp != 200) { |
| 5654 |
ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp); |
| 5655 |
return AST_TEST_FAIL; |
| 5656 |
} |
| 5657 |
|
| 5658 |
if (!tree->root) { |
| 5659 |
ast_test_status_update(test, "Resource tree has no root\n"); |
| 5660 |
return AST_TEST_FAIL; |
| 5661 |
} |
| 5662 |
|
| 5663 |
node = tree->root; |
| 5664 |
if (check_node(test, node, resources_1, ARRAY_LEN(resources_1))) { |
| 5665 |
return AST_TEST_FAIL; |
| 5666 |
} |
| 5667 |
|
| 5668 |
|
| 5669 |
node = AST_VECTOR_GET(&node->children, 3); |
| 5670 |
if (check_node(test, node, resources_2, ARRAY_LEN(resources_2))) { |
| 5671 |
return AST_TEST_FAIL; |
| 5672 |
} |
| 5673 |
|
| 5674 |
return AST_TEST_PASS; |
| 5675 |
} |
| 5676 |
|
| 5677 |
AST_TEST_DEFINE(bad_resource) |
| 5678 |
{ |
| 5679 |
RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list); |
| 5680 |
RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy); |
| 5681 |
const char *resources[] = { |
| 5682 |
"huey", |
| 5683 |
"dewey", |
| 5684 |
"louie", |
| 5685 |
"coconut", |
| 5686 |
}; |
| 5687 |
int resp; |
| 5688 |
|
| 5689 |
switch (cmd) { |
| 5690 |
case TEST_INIT: |
| 5691 |
info->name = "bad_resource"; |
| 5692 |
info->category = "/res/res_pjsip_pubsub/"; |
| 5693 |
info->summary = "Ensure bad resources do not end up in the tree"; |
| 5694 |
info->description = |
| 5695 |
"Create a resource list with a single bad resource. Ensure the bad resource does not end up in the tree."; |
| 5696 |
return AST_TEST_NOT_RUN; |
| 5697 |
case TEST_EXECUTE: |
| 5698 |
break; |
| 5699 |
} |
| 5700 |
|
| 5701 |
if (ineligible_configuration()) { |
| 5702 |
ast_test_status_update(test, "Ineligible configuration for this test. Please add a " |
| 5703 |
"'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n"); |
| 5704 |
return AST_TEST_NOT_RUN; |
| 5705 |
} |
| 5706 |
|
| 5707 |
list = create_resource_list(test, "foo", "test", resources, ARRAY_LEN(resources)); |
| 5708 |
if (!list) { |
| 5709 |
return AST_TEST_FAIL; |
| 5710 |
} |
| 5711 |
|
| 5712 |
tree = ast_calloc(1, sizeof(*tree)); |
| 5713 |
resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1, NULL); |
| 5714 |
if (resp != 200) { |
| 5715 |
ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp); |
| 5716 |
return AST_TEST_FAIL; |
| 5717 |
} |
| 5718 |
|
| 5719 |
if (!tree->root) { |
| 5720 |
ast_test_status_update(test, "Resource tree has no root\n"); |
| 5721 |
return AST_TEST_FAIL; |
| 5722 |
} |
| 5723 |
|
| 5724 |
|
| 5725 |
if (check_node(test, tree->root, resources, ARRAY_LEN(resources) - 1)) { |
| 5726 |
return AST_TEST_FAIL; |
| 5727 |
} |
| 5728 |
|
| 5729 |
return AST_TEST_PASS; |
| 5730 |
|
| 5731 |
} |
| 5732 |
|
| 5733 |
AST_TEST_DEFINE(bad_branch) |
| 5734 |
{ |
| 5735 |
RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list); |
| 5736 |
RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list); |
| 5737 |
RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy); |
| 5738 |
const char *resources_1[] = { |
| 5739 |
"huey", |
| 5740 |
"dewey", |
| 5741 |
"louie", |
| 5742 |
"gross", |
| 5743 |
}; |
| 5744 |
|
| 5745 |
const char *resources_2[] = { |
| 5746 |
"coconut", |
| 5747 |
"cilantro", |
| 5748 |
"olive", |
| 5749 |
"cheese", |
| 5750 |
}; |
| 5751 |
int resp; |
| 5752 |
|
| 5753 |
switch (cmd) { |
| 5754 |
case TEST_INIT: |
| 5755 |
info->name = "bad_branch"; |
| 5756 |
info->category = "/res/res_pjsip_pubsub/"; |
| 5757 |
info->summary = "Ensure bad branches are pruned from the tree"; |
| 5758 |
info->description = |
| 5759 |
"Create a resource list that makes a tree with an entire branch of bad resources.\n" |
| 5760 |
"Ensure the bad branch is pruned from the tree."; |
| 5761 |
return AST_TEST_NOT_RUN; |
| 5762 |
case TEST_EXECUTE: |
| 5763 |
break; |
| 5764 |
} |
| 5765 |
|
| 5766 |
if (ineligible_configuration()) { |
| 5767 |
ast_test_status_update(test, "Ineligible configuration for this test. Please add a " |
| 5768 |
"'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n"); |
| 5769 |
return AST_TEST_NOT_RUN; |
| 5770 |
} |
| 5771 |
|
| 5772 |
list_1 = create_resource_list(test, "foo", "test", resources_1, ARRAY_LEN(resources_1)); |
| 5773 |
if (!list_1) { |
| 5774 |
return AST_TEST_FAIL; |
| 5775 |
} |
| 5776 |
list_2 = create_resource_list(test, "gross", "test", resources_2, ARRAY_LEN(resources_2)); |
| 5777 |
if (!list_2) { |
| 5778 |
return AST_TEST_FAIL; |
| 5779 |
} |
| 5780 |
|
| 5781 |
tree = ast_calloc(1, sizeof(*tree)); |
| 5782 |
resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1, NULL); |
| 5783 |
if (resp != 200) { |
| 5784 |
ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp); |
| 5785 |
return AST_TEST_FAIL; |
| 5786 |
} |
| 5787 |
|
| 5788 |
if (!tree->root) { |
| 5789 |
ast_test_status_update(test, "Resource tree has no root\n"); |
| 5790 |
return AST_TEST_FAIL; |
| 5791 |
} |
| 5792 |
|
| 5793 |
|
| 5794 |
|
| 5795 |
|
| 5796 |
if (check_node(test, tree->root, resources_1, ARRAY_LEN(resources_1) - 1)) { |
| 5797 |
return AST_TEST_FAIL; |
| 5798 |
} |
| 5799 |
|
| 5800 |
return AST_TEST_PASS; |
| 5801 |
|
| 5802 |
} |
| 5803 |
|
| 5804 |
AST_TEST_DEFINE(duplicate_resource) |
| 5805 |
{ |
| 5806 |
RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list); |
| 5807 |
RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list); |
| 5808 |
RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy); |
| 5809 |
const char *resources_1[] = { |
| 5810 |
"huey", |
| 5811 |
"ducks", |
| 5812 |
"dewey", |
| 5813 |
"louie", |
| 5814 |
}; |
| 5815 |
const char *resources_2[] = { |
| 5816 |
"donald", |
| 5817 |
"daisy", |
| 5818 |
"scrooge", |
| 5819 |
"dewey", |
| 5820 |
"louie", |
| 5821 |
"huey", |
| 5822 |
}; |
| 5823 |
int resp; |
| 5824 |
struct tree_node *node; |
| 5825 |
|
| 5826 |
switch (cmd) { |
| 5827 |
case TEST_INIT: |
| 5828 |
info->name = "duplicate_resource"; |
| 5829 |
info->category = "/res/res_pjsip_pubsub/"; |
| 5830 |
info->summary = "Ensure duplicated resources do not end up in the tree"; |
| 5831 |
info->description = |
| 5832 |
"Create a resource list with a single duplicated resource. Ensure the duplicated resource does not end up in the tree."; |
| 5833 |
return AST_TEST_NOT_RUN; |
| 5834 |
case TEST_EXECUTE: |
| 5835 |
break; |
| 5836 |
} |
| 5837 |
|
| 5838 |
if (ineligible_configuration()) { |
| 5839 |
ast_test_status_update(test, "Ineligible configuration for this test. Please add a " |
| 5840 |
"'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n"); |
| 5841 |
return AST_TEST_NOT_RUN; |
| 5842 |
} |
| 5843 |
|
| 5844 |
list_1 = create_resource_list(test, "foo", "test", resources_1, ARRAY_LEN(resources_1)); |
| 5845 |
if (!list_1) { |
| 5846 |
return AST_TEST_FAIL; |
| 5847 |
} |
| 5848 |
|
| 5849 |
list_2 = create_resource_list(test, "ducks", "test", resources_2, ARRAY_LEN(resources_2)); |
| 5850 |
if (!list_2) { |
| 5851 |
return AST_TEST_FAIL; |
| 5852 |
} |
| 5853 |
|
| 5854 |
tree = ast_calloc(1, sizeof(*tree)); |
| 5855 |
resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1, NULL); |
| 5856 |
if (resp != 200) { |
| 5857 |
ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp); |
| 5858 |
return AST_TEST_FAIL; |
| 5859 |
} |
| 5860 |
|
| 5861 |
if (!tree->root) { |
| 5862 |
ast_test_status_update(test, "Resource tree has no root\n"); |
| 5863 |
return AST_TEST_FAIL; |
| 5864 |
} |
| 5865 |
|
| 5866 |
node = tree->root; |
| 5867 |
|
| 5868 |
|
| 5869 |
|
| 5870 |
if (check_node(test, node, resources_1, ARRAY_LEN(resources_1) - 2)) { |
| 5871 |
return AST_TEST_FAIL; |
| 5872 |
} |
| 5873 |
|
| 5874 |
|
| 5875 |
|
| 5876 |
|
| 5877 |
node = AST_VECTOR_GET(&node->children, 1); |
| 5878 |
if (check_node(test, node, resources_2, ARRAY_LEN(resources_2) - 1)) { |
| 5879 |
return AST_TEST_FAIL; |
| 5880 |
} |
| 5881 |
|
| 5882 |
return AST_TEST_PASS; |
| 5883 |
} |
| 5884 |
|
| 5885 |
AST_TEST_DEFINE(loop) |
| 5886 |
{ |
| 5887 |
RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list); |
| 5888 |
RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list); |
| 5889 |
RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy); |
| 5890 |
const char *resources_1[] = { |
| 5891 |
"derp", |
| 5892 |
}; |
| 5893 |
const char *resources_2[] = { |
| 5894 |
"herp", |
| 5895 |
}; |
| 5896 |
int resp; |
| 5897 |
|
| 5898 |
switch (cmd) { |
| 5899 |
case TEST_INIT: |
| 5900 |
info->name = "loop"; |
| 5901 |
info->category = "/res/res_pjsip_pubsub/"; |
| 5902 |
info->summary = "Test that loops are properly detected."; |
| 5903 |
info->description = |
| 5904 |
"Create two resource lists that refer to each other. Ensure that attempting to build a tree\n" |
| 5905 |
"results in an empty tree."; |
| 5906 |
return AST_TEST_NOT_RUN; |
| 5907 |
case TEST_EXECUTE: |
| 5908 |
break; |
| 5909 |
} |
| 5910 |
|
| 5911 |
if (ineligible_configuration()) { |
| 5912 |
ast_test_status_update(test, "Ineligible configuration for this test. Please add a " |
| 5913 |
"'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n"); |
| 5914 |
return AST_TEST_NOT_RUN; |
| 5915 |
} |
| 5916 |
|
| 5917 |
list_1 = create_resource_list(test, "herp", "test", resources_1, ARRAY_LEN(resources_1)); |
| 5918 |
if (!list_1) { |
| 5919 |
return AST_TEST_FAIL; |
| 5920 |
} |
| 5921 |
list_2 = create_resource_list(test, "derp", "test", resources_2, ARRAY_LEN(resources_2)); |
| 5922 |
if (!list_2) { |
| 5923 |
return AST_TEST_FAIL; |
| 5924 |
} |
| 5925 |
|
| 5926 |
tree = ast_calloc(1, sizeof(*tree)); |
| 5927 |
resp = build_resource_tree(NULL, &test_handler, "herp", tree, 1, NULL); |
| 5928 |
if (resp == 200) { |
| 5929 |
ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp); |
| 5930 |
return AST_TEST_FAIL; |
| 5931 |
} |
| 5932 |
|
| 5933 |
return AST_TEST_PASS; |
| 5934 |
} |
| 5935 |
|
| 5936 |
AST_TEST_DEFINE(bad_event) |
| 5937 |
{ |
| 5938 |
RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list); |
| 5939 |
RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy); |
| 5940 |
const char *resources[] = { |
| 5941 |
"huey", |
| 5942 |
"dewey", |
| 5943 |
"louie", |
| 5944 |
}; |
| 5945 |
int resp; |
| 5946 |
|
| 5947 |
switch (cmd) { |
| 5948 |
case TEST_INIT: |
| 5949 |
info->name = "bad_event"; |
| 5950 |
info->category = "/res/res_pjsip_pubsub/"; |
| 5951 |
info->summary = "Ensure that list with wrong event specified is not retrieved"; |
| 5952 |
info->description = |
| 5953 |
"Create a simple resource list for event 'tsetse'. Ensure that trying to retrieve the list for event 'test' fails."; |
| 5954 |
return AST_TEST_NOT_RUN; |
| 5955 |
case TEST_EXECUTE: |
| 5956 |
break; |
| 5957 |
} |
| 5958 |
|
| 5959 |
if (ineligible_configuration()) { |
| 5960 |
ast_test_status_update(test, "Ineligible configuration for this test. Please add a " |
| 5961 |
"'res_pjsip_pubsub' section to sorcery.conf, and set 'resource_list=memory'\n"); |
| 5962 |
return AST_TEST_NOT_RUN; |
| 5963 |
} |
| 5964 |
|
| 5965 |
list = create_resource_list(test, "foo", "tsetse", resources, ARRAY_LEN(resources)); |
| 5966 |
if (!list) { |
| 5967 |
return AST_TEST_FAIL; |
| 5968 |
} |
| 5969 |
|
| 5970 |
tree = ast_calloc(1, sizeof(*tree)); |
| 5971 |
|
| 5972 |
|
| 5973 |
|
| 5974 |
resp = build_resource_tree(NULL, &test_handler, "foo", tree, 1, NULL); |
| 5975 |
if (resp != 200) { |
| 5976 |
ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp); |
| 5977 |
return AST_TEST_FAIL; |
| 5978 |
} |
| 5979 |
|
| 5980 |
if (!tree->root) { |
| 5981 |
ast_test_status_update(test, "Resource tree has no root\n"); |
| 5982 |
return AST_TEST_FAIL; |
| 5983 |
} |
| 5984 |
|
| 5985 |
if (strcmp(tree->root->resource, "foo")) { |
| 5986 |
ast_test_status_update(test, "Unexpected resource %s found in tree\n", tree->root->resource); |
| 5987 |
return AST_TEST_FAIL; |
| 5988 |
} |
| 5989 |
|
| 5990 |
return AST_TEST_PASS; |
| 5991 |
} |
| 5992 |
|
| 5993 |
#endif |
| 5994 |
|
| 5995 |
static int resource_endpoint_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 5996 |
{ |
| 5997 |
struct ast_sip_publication_resource *resource = obj; |
| 5998 |
|
| 5999 |
ast_free(resource->endpoint); |
| 6000 |
resource->endpoint = ast_strdup(var->value); |
| 6001 |
|
| 6002 |
return 0; |
| 6003 |
} |
| 6004 |
|
| 6005 |
static int resource_event_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 6006 |
{ |
| 6007 |
struct ast_sip_publication_resource *resource = obj; |
| 6008 |
|
| 6009 |
const char *event = var->name + 6; |
| 6010 |
struct ast_variable *item; |
| 6011 |
|
| 6012 |
if (ast_strlen_zero(event) || ast_strlen_zero(var->value)) { |
| 6013 |
return -1; |
| 6014 |
} |
| 6015 |
|
| 6016 |
item = ast_variable_new(event, var->value, ""); |
| 6017 |
if (!item) { |
| 6018 |
return -1; |
| 6019 |
} |
| 6020 |
|
| 6021 |
if (resource->events) { |
| 6022 |
item->next = resource->events; |
| 6023 |
} |
| 6024 |
resource->events = item; |
| 6025 |
|
| 6026 |
return 0; |
| 6027 |
} |
| 6028 |
|
| 6029 |
static int load_module(void) |
| 6030 |
{ |
| 6031 |
static const pj_str_t str_PUBLISH = { "PUBLISH", 7 }; |
| 6032 |
struct ast_sorcery *sorcery; |
| 6033 |
|
| 6034 |
sorcery = ast_sip_get_sorcery(); |
| 6035 |
|
| 6036 |
if (!(sched = ast_sched_context_create())) { |
| 6037 |
ast_log(LOG_ERROR, "Could not create scheduler for publication expiration\n"); |
| 6038 |
return AST_MODULE_LOAD_DECLINE; |
| 6039 |
} |
| 6040 |
|
| 6041 |
if (ast_sched_start_thread(sched)) { |
| 6042 |
ast_log(LOG_ERROR, "Could not start scheduler thread for publication expiration\n"); |
| 6043 |
ast_sched_context_destroy(sched); |
| 6044 |
return AST_MODULE_LOAD_DECLINE; |
| 6045 |
} |
| 6046 |
|
| 6047 |
ast_sorcery_apply_config(sorcery, "res_pjsip_pubsub"); |
| 6048 |
ast_sorcery_apply_default(sorcery, "subscription_persistence", "astdb", "subscription_persistence"); |
| 6049 |
if (ast_sorcery_object_register(sorcery, "subscription_persistence", subscription_persistence_alloc, |
| 6050 |
NULL, NULL)) { |
| 6051 |
ast_log(LOG_ERROR, "Could not register subscription persistence object support\n"); |
| 6052 |
ast_sched_context_destroy(sched); |
| 6053 |
return AST_MODULE_LOAD_DECLINE; |
| 6054 |
} |
| 6055 |
ast_sorcery_object_field_register(sorcery, "subscription_persistence", "packet", "", OPT_CHAR_ARRAY_T, 0, |
| 6056 |
CHARFLDSET(struct subscription_persistence, packet)); |
| 6057 |
ast_sorcery_object_field_register(sorcery, "subscription_persistence", "src_name", "", OPT_CHAR_ARRAY_T, 0, |
| 6058 |
CHARFLDSET(struct subscription_persistence, src_name)); |
| 6059 |
ast_sorcery_object_field_register(sorcery, "subscription_persistence", "src_port", "0", OPT_UINT_T, 0, |
| 6060 |
FLDSET(struct subscription_persistence, src_port)); |
| 6061 |
ast_sorcery_object_field_register(sorcery, "subscription_persistence", "transport_key", "0", OPT_CHAR_ARRAY_T, 0, |
| 6062 |
CHARFLDSET(struct subscription_persistence, transport_type)); |
| 6063 |
ast_sorcery_object_field_register(sorcery, "subscription_persistence", "local_name", "", OPT_CHAR_ARRAY_T, 0, |
| 6064 |
CHARFLDSET(struct subscription_persistence, local_name)); |
| 6065 |
ast_sorcery_object_field_register(sorcery, "subscription_persistence", "local_port", "0", OPT_UINT_T, 0, |
| 6066 |
FLDSET(struct subscription_persistence, local_port)); |
| 6067 |
ast_sorcery_object_field_register(sorcery, "subscription_persistence", "cseq", "0", OPT_UINT_T, 0, |
| 6068 |
FLDSET(struct subscription_persistence, cseq)); |
| 6069 |
ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "endpoint", "", |
| 6070 |
persistence_endpoint_str2struct, persistence_endpoint_struct2str, NULL, 0, 0); |
| 6071 |
ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "tag", "", |
| 6072 |
persistence_tag_str2struct, persistence_tag_struct2str, NULL, 0, 0); |
| 6073 |
ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "expires", "", |
| 6074 |
persistence_expires_str2struct, persistence_expires_struct2str, NULL, 0, 0); |
| 6075 |
ast_sorcery_object_field_register(sorcery, "subscription_persistence", "contact_uri", "", OPT_CHAR_ARRAY_T, 0, |
| 6076 |
CHARFLDSET(struct subscription_persistence, contact_uri)); |
| 6077 |
ast_sorcery_object_field_register(sorcery, "subscription_persistence", "prune_on_boot", "no", OPT_YESNO_T, 1, |
| 6078 |
FLDSET(struct subscription_persistence, prune_on_boot)); |
| 6079 |
ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "generator_data", "", |
| 6080 |
persistence_generator_data_str2struct, persistence_generator_data_struct2str, NULL, 0, 0); |
| 6081 |
|
| 6082 |
if (apply_list_configuration(sorcery)) { |
| 6083 |
ast_sched_context_destroy(sched); |
| 6084 |
return AST_MODULE_LOAD_DECLINE; |
| 6085 |
} |
| 6086 |
|
| 6087 |
ast_sorcery_apply_default(sorcery, "inbound-publication", "config", "pjsip.conf,criteria=type=inbound-publication"); |
| 6088 |
if (ast_sorcery_object_register(sorcery, "inbound-publication", publication_resource_alloc, |
| 6089 |
NULL, NULL)) { |
| 6090 |
ast_log(LOG_ERROR, "Could not register subscription persistence object support\n"); |
| 6091 |
ast_sched_context_destroy(sched); |
| 6092 |
return AST_MODULE_LOAD_DECLINE; |
| 6093 |
} |
| 6094 |
ast_sorcery_object_field_register(sorcery, "inbound-publication", "type", "", OPT_NOOP_T, 0, 0); |
| 6095 |
ast_sorcery_object_field_register_custom(sorcery, "inbound-publication", "endpoint", "", |
| 6096 |
resource_endpoint_handler, NULL, NULL, 0, 0); |
| 6097 |
ast_sorcery_object_fields_register(sorcery, "inbound-publication", "^event_", resource_event_handler, NULL); |
| 6098 |
ast_sorcery_reload_object(sorcery, "inbound-publication"); |
| 6099 |
|
| 6100 |
if (ast_sip_register_service(&pubsub_module)) { |
| 6101 |
ast_log(LOG_ERROR, "Could not register pubsub service\n"); |
| 6102 |
ast_sched_context_destroy(sched); |
| 6103 |
return AST_MODULE_LOAD_DECLINE; |
| 6104 |
} |
| 6105 |
|
| 6106 |
if (pjsip_evsub_init_module(ast_sip_get_pjsip_endpoint()) != PJ_SUCCESS) { |
| 6107 |
ast_log(LOG_ERROR, "Could not initialize pjsip evsub module.\n"); |
| 6108 |
ast_sip_unregister_service(&pubsub_module); |
| 6109 |
ast_sched_context_destroy(sched); |
| 6110 |
return AST_MODULE_LOAD_DECLINE; |
| 6111 |
} |
| 6112 |
|
| 6113 |
|
| 6114 |
|
| 6115 |
ast_module_shutdown_ref(ast_module_info->self); |
| 6116 |
|
| 6117 |
pjsip_media_type_init2(&rlmi_media_type, "application", "rlmi+xml"); |
| 6118 |
|
| 6119 |
pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), NULL, PJSIP_H_ALLOW, NULL, 1, &str_PUBLISH); |
| 6120 |
|
| 6121 |
if (ast_fully_booted) { |
| 6122 |
ast_sip_push_task(NULL, subscription_persistence_load, NULL); |
| 6123 |
} else { |
| 6124 |
struct stasis_subscription *sub; |
| 6125 |
|
| 6126 |
sub = stasis_subscribe_pool(ast_manager_get_topic(), subscription_persistence_event_cb, NULL); |
| 6127 |
stasis_subscription_accept_message_type(sub, ast_manager_get_generic_type()); |
| 6128 |
stasis_subscription_set_filter(sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 6129 |
} |
| 6130 |
|
| 6131 |
ast_manager_register_xml(AMI_SHOW_SUBSCRIPTIONS_INBOUND, EVENT_FLAG_SYSTEM, |
| 6132 |
ami_show_subscriptions_inbound); |
| 6133 |
ast_manager_register_xml(AMI_SHOW_SUBSCRIPTIONS_OUTBOUND, EVENT_FLAG_SYSTEM, |
| 6134 |
ami_show_subscriptions_outbound); |
| 6135 |
ast_manager_register_xml("PJSIPShowResourceLists", EVENT_FLAG_SYSTEM, |
| 6136 |
ami_show_resource_lists); |
| 6137 |
|
| 6138 |
ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands)); |
| 6139 |
|
| 6140 |
AST_TEST_REGISTER(resource_tree); |
| 6141 |
AST_TEST_REGISTER(complex_resource_tree); |
| 6142 |
AST_TEST_REGISTER(bad_resource); |
| 6143 |
AST_TEST_REGISTER(bad_branch); |
| 6144 |
AST_TEST_REGISTER(duplicate_resource); |
| 6145 |
AST_TEST_REGISTER(loop); |
| 6146 |
AST_TEST_REGISTER(bad_event); |
| 6147 |
|
| 6148 |
return AST_MODULE_LOAD_SUCCESS; |
| 6149 |
} |
| 6150 |
|
| 6151 |
static int unload_module(void) |
| 6152 |
{ |
| 6153 |
AST_TEST_UNREGISTER(resource_tree); |
| 6154 |
AST_TEST_UNREGISTER(complex_resource_tree); |
| 6155 |
AST_TEST_UNREGISTER(bad_resource); |
| 6156 |
AST_TEST_UNREGISTER(bad_branch); |
| 6157 |
AST_TEST_UNREGISTER(duplicate_resource); |
| 6158 |
AST_TEST_UNREGISTER(loop); |
| 6159 |
AST_TEST_UNREGISTER(bad_event); |
| 6160 |
|
| 6161 |
ast_sip_transport_monitor_unregister_all(sub_tree_transport_cb, NULL, NULL); |
| 6162 |
|
| 6163 |
ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands)); |
| 6164 |
|
| 6165 |
ast_manager_unregister(AMI_SHOW_SUBSCRIPTIONS_OUTBOUND); |
| 6166 |
ast_manager_unregister(AMI_SHOW_SUBSCRIPTIONS_INBOUND); |
| 6167 |
ast_manager_unregister("PJSIPShowResourceLists"); |
| 6168 |
|
| 6169 |
ast_sip_unregister_service(&pubsub_module); |
| 6170 |
if (sched) { |
| 6171 |
ast_sched_context_destroy(sched); |
| 6172 |
} |
| 6173 |
|
| 6174 |
return 0; |
| 6175 |
} |
| 6176 |
|
| 6177 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PJSIP event resource", |
| 6178 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 6179 |
.load = load_module, |
| 6180 |
.unload = unload_module, |
| 6181 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND, |
| 6182 |
.requires = "res_pjsip", |
| 6183 |
); |