| 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 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include <pjsip.h> |
| 29 |
#include <pjsip_ua.h> |
| 30 |
|
| 31 |
#include "asterisk/res_pjsip.h" |
| 32 |
#include "asterisk/res_pjsip_cli.h" |
| 33 |
#include "asterisk/module.h" |
| 34 |
#include "asterisk/taskprocessor.h" |
| 35 |
#include "asterisk/cli.h" |
| 36 |
#include "asterisk/stasis_system.h" |
| 37 |
#include "asterisk/threadstorage.h" |
| 38 |
#include "asterisk/taskpool.h" |
| 39 |
#include "asterisk/serializer_shutdown_group.h" |
| 40 |
#include "asterisk/statsd.h" |
| 41 |
#include "res_pjsip/include/res_pjsip_private.h" |
| 42 |
#include "asterisk/vector.h" |
| 43 |
#include "asterisk/pbx.h" |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
static int set_outbound_initial_authentication_credentials(pjsip_regc *regc, |
| 441 |
const struct ast_sip_auth_vector *auth_vector); |
| 442 |
|
| 443 |
|
| 444 |
AST_THREADSTORAGE(register_callback_invoked); |
| 445 |
|
| 446 |
|
| 447 |
#define REREGISTER_BUFFER_TIME 10 |
| 448 |
|
| 449 |
|
| 450 |
#define LINE_PARAMETER_SIZE 8 |
| 451 |
|
| 452 |
|
| 453 |
enum sip_outbound_registration_status { |
| 454 |
|
| 455 |
SIP_REGISTRATION_UNREGISTERED = 0, |
| 456 |
|
| 457 |
SIP_REGISTRATION_REGISTERED, |
| 458 |
|
| 459 |
SIP_REGISTRATION_REJECTED_TEMPORARY, |
| 460 |
|
| 461 |
SIP_REGISTRATION_REJECTED_PERMANENT, |
| 462 |
|
| 463 |
SIP_REGISTRATION_STOPPING, |
| 464 |
|
| 465 |
SIP_REGISTRATION_STOPPED, |
| 466 |
}; |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
static const char *sip_outbound_registration_status_str(enum sip_outbound_registration_status state) |
| 478 |
{ |
| 479 |
const char *str; |
| 480 |
|
| 481 |
str = "Unregistered"; |
| 482 |
switch (state) { |
| 483 |
case SIP_REGISTRATION_STOPPING: |
| 484 |
case SIP_REGISTRATION_STOPPED: |
| 485 |
case SIP_REGISTRATION_UNREGISTERED: |
| 486 |
break; |
| 487 |
case SIP_REGISTRATION_REGISTERED: |
| 488 |
str = "Registered"; |
| 489 |
break; |
| 490 |
case SIP_REGISTRATION_REJECTED_TEMPORARY: |
| 491 |
case SIP_REGISTRATION_REJECTED_PERMANENT: |
| 492 |
str = "Rejected"; |
| 493 |
break; |
| 494 |
} |
| 495 |
return str; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
struct sip_outbound_registration { |
| 500 |
|
| 501 |
SORCERY_OBJECT(details); |
| 502 |
|
| 503 |
AST_DECLARE_STRING_FIELDS( |
| 504 |
|
| 505 |
AST_STRING_FIELD(server_uri); |
| 506 |
|
| 507 |
AST_STRING_FIELD(client_uri); |
| 508 |
|
| 509 |
AST_STRING_FIELD(contact_user); |
| 510 |
|
| 511 |
AST_STRING_FIELD(contact_header_params); |
| 512 |
|
| 513 |
AST_STRING_FIELD(transport); |
| 514 |
|
| 515 |
AST_STRING_FIELD(outbound_proxy); |
| 516 |
|
| 517 |
AST_STRING_FIELD(endpoint); |
| 518 |
|
| 519 |
AST_STRING_FIELD(user_agent); |
| 520 |
); |
| 521 |
|
| 522 |
unsigned int expiration; |
| 523 |
|
| 524 |
unsigned int max_random_initial_delay; |
| 525 |
|
| 526 |
unsigned int retry_interval; |
| 527 |
|
| 528 |
unsigned int forbidden_retry_interval; |
| 529 |
|
| 530 |
unsigned int fatal_retry_interval; |
| 531 |
|
| 532 |
unsigned int auth_rejection_permanent; |
| 533 |
|
| 534 |
unsigned int max_retries; |
| 535 |
|
| 536 |
unsigned int line; |
| 537 |
|
| 538 |
enum ast_sip_security_negotiation security_negotiation; |
| 539 |
|
| 540 |
struct ast_sip_security_mechanism_vector security_mechanisms; |
| 541 |
|
| 542 |
struct ast_sip_auth_vector outbound_auths; |
| 543 |
|
| 544 |
unsigned int support_path; |
| 545 |
|
| 546 |
unsigned int support_outbound; |
| 547 |
}; |
| 548 |
|
| 549 |
|
| 550 |
struct sip_outbound_registration_client_state { |
| 551 |
|
| 552 |
enum sip_outbound_registration_status status; |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
pjsip_regc *client; |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
pjsip_tx_data *last_tdata; |
| 568 |
|
| 569 |
pj_timer_entry timer; |
| 570 |
|
| 571 |
char line[LINE_PARAMETER_SIZE]; |
| 572 |
|
| 573 |
unsigned int retries; |
| 574 |
|
| 575 |
unsigned int max_retries; |
| 576 |
|
| 577 |
unsigned int retry_interval; |
| 578 |
|
| 579 |
unsigned int forbidden_retry_interval; |
| 580 |
|
| 581 |
unsigned int fatal_retry_interval; |
| 582 |
|
| 583 |
unsigned int auth_rejection_permanent; |
| 584 |
|
| 585 |
unsigned int support_path; |
| 586 |
|
| 587 |
unsigned int support_outbound; |
| 588 |
|
| 589 |
enum ast_sip_security_negotiation security_negotiation; |
| 590 |
|
| 591 |
struct ast_sip_security_mechanism_vector security_mechanisms; |
| 592 |
|
| 593 |
struct ast_sip_security_mechanism_vector server_security_mechanisms; |
| 594 |
|
| 595 |
unsigned int auth_cseq; |
| 596 |
|
| 597 |
struct ast_taskprocessor *serializer; |
| 598 |
|
| 599 |
struct ast_sip_auth_vector outbound_auths; |
| 600 |
|
| 601 |
unsigned int destroy:1; |
| 602 |
|
| 603 |
unsigned int auth_attempted:1; |
| 604 |
|
| 605 |
int last_status_code; |
| 606 |
|
| 607 |
char *transport_name; |
| 608 |
|
| 609 |
char *registration_name; |
| 610 |
|
| 611 |
unsigned int registration_expires; |
| 612 |
|
| 613 |
char *user_agent; |
| 614 |
}; |
| 615 |
|
| 616 |
|
| 617 |
struct sip_outbound_registration_state { |
| 618 |
|
| 619 |
struct sip_outbound_registration *registration; |
| 620 |
|
| 621 |
struct sip_outbound_registration_client_state *client_state; |
| 622 |
}; |
| 623 |
|
| 624 |
|
| 625 |
#define MAX_UNLOAD_TIMEOUT_TIME 35 |
| 626 |
|
| 627 |
|
| 628 |
static struct ast_serializer_shutdown_group *shutdown_group; |
| 629 |
|
| 630 |
|
| 631 |
#define DEFAULT_STATE_BUCKETS 53 |
| 632 |
static AO2_GLOBAL_OBJ_STATIC(current_states); |
| 633 |
|
| 634 |
|
| 635 |
static struct stasis_subscription *network_change_sub; |
| 636 |
|
| 637 |
|
| 638 |
static int registration_state_hash(const void *obj, const int flags) |
| 639 |
{ |
| 640 |
const struct sip_outbound_registration_state *object; |
| 641 |
const char *key; |
| 642 |
|
| 643 |
switch (flags & OBJ_SEARCH_MASK) { |
| 644 |
case OBJ_SEARCH_KEY: |
| 645 |
key = obj; |
| 646 |
break; |
| 647 |
case OBJ_SEARCH_OBJECT: |
| 648 |
object = obj; |
| 649 |
key = ast_sorcery_object_get_id(object->registration); |
| 650 |
break; |
| 651 |
default: |
| 652 |
ast_assert(0); |
| 653 |
return 0; |
| 654 |
} |
| 655 |
return ast_str_hash(key); |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
static int registration_state_cmp(void *obj, void *arg, int flags) |
| 660 |
{ |
| 661 |
const struct sip_outbound_registration_state *object_left = obj; |
| 662 |
const struct sip_outbound_registration_state *object_right = arg; |
| 663 |
const char *right_key = arg; |
| 664 |
int cmp; |
| 665 |
|
| 666 |
switch (flags & OBJ_SEARCH_MASK) { |
| 667 |
case OBJ_SEARCH_OBJECT: |
| 668 |
right_key = ast_sorcery_object_get_id(object_right->registration); |
| 669 |
|
| 670 |
case OBJ_SEARCH_KEY: |
| 671 |
cmp = strcmp(ast_sorcery_object_get_id(object_left->registration), right_key); |
| 672 |
break; |
| 673 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 674 |
|
| 675 |
ast_assert(0); |
| 676 |
return 0; |
| 677 |
default: |
| 678 |
cmp = 0; |
| 679 |
break; |
| 680 |
} |
| 681 |
if (cmp) { |
| 682 |
return 0; |
| 683 |
} |
| 684 |
return CMP_MATCH; |
| 685 |
} |
| 686 |
|
| 687 |
static struct sip_outbound_registration_state *get_state(const char *id) |
| 688 |
{ |
| 689 |
struct sip_outbound_registration_state *state = NULL; |
| 690 |
struct ao2_container *states; |
| 691 |
|
| 692 |
states = ao2_global_obj_ref(current_states); |
| 693 |
if (states) { |
| 694 |
state = ao2_find(states, id, OBJ_SEARCH_KEY); |
| 695 |
ao2_ref(states, -1); |
| 696 |
} |
| 697 |
return state; |
| 698 |
} |
| 699 |
|
| 700 |
static struct ao2_container *get_registrations(void) |
| 701 |
{ |
| 702 |
struct ao2_container *registrations = ast_sorcery_retrieve_by_fields( |
| 703 |
ast_sip_get_sorcery(), "registration", |
| 704 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 705 |
|
| 706 |
return registrations; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
static int line_identify_relationship(void *obj, void *arg, int flags) |
| 711 |
{ |
| 712 |
struct sip_outbound_registration_state *state = obj; |
| 713 |
pjsip_param *line = arg; |
| 714 |
|
| 715 |
return !pj_strcmp2(&line->value, state->client_state->line) ? CMP_MATCH : 0; |
| 716 |
} |
| 717 |
|
| 718 |
static struct pjsip_param *get_uri_option_line(const void *uri) |
| 719 |
{ |
| 720 |
static const pj_str_t LINE_STR = { "line", 4 }; |
| 721 |
|
| 722 |
return ast_sip_pjsip_uri_get_other_param((pjsip_uri *)uri, &LINE_STR); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
static struct ast_sip_endpoint *line_identify(pjsip_rx_data *rdata) |
| 727 |
{ |
| 728 |
pjsip_param *line; |
| 729 |
RAII_VAR(struct ao2_container *, states, NULL, ao2_cleanup); |
| 730 |
RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup); |
| 731 |
|
| 732 |
if (!(line = get_uri_option_line(rdata->msg_info.to->uri)) |
| 733 |
&& !(line = get_uri_option_line(rdata->msg_info.msg->line.req.uri))) { |
| 734 |
return NULL; |
| 735 |
} |
| 736 |
|
| 737 |
states = ao2_global_obj_ref(current_states); |
| 738 |
if (!states) { |
| 739 |
return NULL; |
| 740 |
} |
| 741 |
|
| 742 |
state = ao2_callback(states, 0, line_identify_relationship, line); |
| 743 |
if (!state || ast_strlen_zero(state->registration->endpoint)) { |
| 744 |
return NULL; |
| 745 |
} |
| 746 |
|
| 747 |
ast_debug(3, "Determined relationship to outbound registration '%s' based on line '%s', using configured endpoint '%s'\n", |
| 748 |
ast_sorcery_object_get_id(state->registration), state->client_state->line, state->registration->endpoint); |
| 749 |
|
| 750 |
return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", state->registration->endpoint); |
| 751 |
} |
| 752 |
|
| 753 |
static struct ast_sip_endpoint_identifier line_identifier = { |
| 754 |
.identify_endpoint = line_identify, |
| 755 |
}; |
| 756 |
|
| 757 |
|
| 758 |
static void cancel_registration(struct sip_outbound_registration_client_state *client_state) |
| 759 |
{ |
| 760 |
if (pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()), |
| 761 |
&client_state->timer, client_state->timer.id)) { |
| 762 |
|
| 763 |
ao2_ref(client_state, -1); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
static pj_str_t PATH_NAME = { "path", 4 }; |
| 768 |
static pj_str_t OUTBOUND_NAME = { "outbound", 8 }; |
| 769 |
|
| 770 |
AST_VECTOR(pjsip_generic_string_hdr_vector, pjsip_generic_string_hdr *); |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
static int contact_has_security_mechanisms(void *obj, void *arg, int flags) |
| 783 |
{ |
| 784 |
struct ast_sip_contact *contact = obj; |
| 785 |
struct ast_sip_contact_status **ret = arg; |
| 786 |
struct ast_sip_contact_status *contact_status = ast_sip_get_contact_status(contact); |
| 787 |
|
| 788 |
if (!contact_status) { |
| 789 |
return 0; |
| 790 |
} |
| 791 |
if (!AST_VECTOR_SIZE(&contact_status->security_mechanisms)) { |
| 792 |
ao2_cleanup(contact_status); |
| 793 |
return 0; |
| 794 |
} |
| 795 |
*ret = contact_status; |
| 796 |
return CMP_MATCH | CMP_STOP; |
| 797 |
} |
| 798 |
|
| 799 |
static int contact_add_security_headers_to_status(void *obj, void *arg, int flags) |
| 800 |
{ |
| 801 |
struct ast_sip_contact *contact = obj; |
| 802 |
struct pjsip_generic_string_hdr_vector *header_vector = arg; |
| 803 |
struct ast_sip_contact_status *contact_status = ast_sip_get_contact_status(contact); |
| 804 |
|
| 805 |
if (!contact_status) { |
| 806 |
return 0; |
| 807 |
} |
| 808 |
if (AST_VECTOR_SIZE(&contact_status->security_mechanisms)) { |
| 809 |
goto out; |
| 810 |
} |
| 811 |
|
| 812 |
ao2_lock(contact_status); |
| 813 |
AST_VECTOR_CALLBACK_VOID(header_vector, ast_sip_header_to_security_mechanism, &contact_status->security_mechanisms); |
| 814 |
ao2_unlock(contact_status); |
| 815 |
|
| 816 |
out: |
| 817 |
ao2_cleanup(contact_status); |
| 818 |
return 0; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
static void add_security_headers(struct sip_outbound_registration_client_state *client_state, |
| 823 |
pjsip_tx_data *tdata) |
| 824 |
{ |
| 825 |
int add_sec_client_header = 0; |
| 826 |
struct sip_outbound_registration *reg = NULL; |
| 827 |
struct ast_sip_endpoint *endpt = NULL; |
| 828 |
struct ao2_container *contact_container; |
| 829 |
struct ast_sip_contact_status *contact_status = NULL; |
| 830 |
struct ast_sip_security_mechanism_vector *sec_mechs = NULL; |
| 831 |
static const pj_str_t security_verify = { "Security-Verify", 15 }; |
| 832 |
static const pj_str_t security_client = { "Security-Client", 15 }; |
| 833 |
|
| 834 |
if (client_state->security_negotiation != AST_SIP_SECURITY_NEG_MEDIASEC) { |
| 835 |
return; |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
if ((reg = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "registration", client_state->registration_name)) |
| 840 |
&& !ast_strlen_zero(reg->endpoint) && (endpt = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", reg->endpoint)) |
| 841 |
&& (contact_container = ast_sip_location_retrieve_contacts_from_aor_list(endpt->aors))) { |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
ao2_callback(contact_container, OBJ_NODATA, contact_has_security_mechanisms, &contact_status); |
| 846 |
if (contact_status) { |
| 847 |
ao2_lock(contact_status); |
| 848 |
sec_mechs = &contact_status->security_mechanisms; |
| 849 |
} |
| 850 |
ao2_cleanup(contact_container); |
| 851 |
} |
| 852 |
|
| 853 |
if (!contact_status && AST_VECTOR_SIZE(&client_state->server_security_mechanisms)) { |
| 854 |
sec_mechs = &client_state->server_security_mechanisms; |
| 855 |
} |
| 856 |
if (client_state->status == SIP_REGISTRATION_REJECTED_TEMPORARY || client_state->auth_attempted) { |
| 857 |
if (sec_mechs != NULL && pjsip_msg_find_hdr_by_name(tdata->msg, &security_verify, NULL) == NULL) { |
| 858 |
ast_sip_add_security_headers(sec_mechs, "Security-Verify", 0, tdata); |
| 859 |
} |
| 860 |
if (client_state->last_status_code == 494) { |
| 861 |
ast_sip_remove_headers_by_name_and_value(tdata->msg, &security_client, NULL); |
| 862 |
} else { |
| 863 |
|
| 864 |
add_sec_client_header = (pjsip_msg_find_hdr_by_name(tdata->msg, &security_client, NULL) == NULL) ? 1 : 0; |
| 865 |
} |
| 866 |
} else { |
| 867 |
ast_sip_add_security_headers(&client_state->security_mechanisms, "Security-Client", 0, tdata); |
| 868 |
} |
| 869 |
|
| 870 |
if (add_sec_client_header) { |
| 871 |
ast_sip_add_security_headers(&client_state->security_mechanisms, "Security-Client", 0, tdata); |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
if (contact_status) { |
| 876 |
ao2_unlock(contact_status); |
| 877 |
ao2_cleanup(contact_status); |
| 878 |
} |
| 879 |
ao2_cleanup(endpt); |
| 880 |
ao2_cleanup(reg); |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
static pj_status_t registration_client_send(struct sip_outbound_registration_client_state *client_state, |
| 885 |
pjsip_tx_data *tdata) |
| 886 |
{ |
| 887 |
pj_status_t status; |
| 888 |
int *callback_invoked; |
| 889 |
pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, }; |
| 890 |
|
| 891 |
callback_invoked = ast_threadstorage_get(®ister_callback_invoked, sizeof(int)); |
| 892 |
if (!callback_invoked) { |
| 893 |
pjsip_tx_data_dec_ref(tdata); |
| 894 |
return PJ_ENOMEM; |
| 895 |
} |
| 896 |
*callback_invoked = 0; |
| 897 |
|
| 898 |
|
| 899 |
ao2_ref(client_state, +1); |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
pjsip_tx_data_add_ref(tdata); |
| 906 |
|
| 907 |
|
| 908 |
add_security_headers(client_state, tdata); |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
if (!ast_strlen_zero(client_state->user_agent)) { |
| 914 |
static const pj_str_t user_agent_str = { "User-Agent", 10 }; |
| 915 |
pjsip_generic_string_hdr *default_user_agent_hdr; |
| 916 |
pjsip_generic_string_hdr *user_agent_hdr; |
| 917 |
pj_str_t user_agent_val; |
| 918 |
default_user_agent_hdr = pjsip_msg_find_hdr_by_name(tdata->msg, &user_agent_str, NULL); |
| 919 |
user_agent_val = pj_str(client_state->user_agent); |
| 920 |
user_agent_hdr = pjsip_generic_string_hdr_create(tdata->pool, &user_agent_str, &user_agent_val); |
| 921 |
if (!user_agent_hdr) { |
| 922 |
ast_log(LOG_ERROR, "Could not add custom User-Agent to outbound registration %s, sending REGISTER request with non-custom header\n", client_state->registration_name); |
| 923 |
} else { |
| 924 |
if (default_user_agent_hdr) { |
| 925 |
pj_list_erase(default_user_agent_hdr); |
| 926 |
} |
| 927 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)user_agent_hdr); |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
ast_sip_set_tpselector_from_transport_name(client_state->transport_name, &selector); |
| 937 |
pjsip_regc_set_transport(client_state->client, &selector); |
| 938 |
ast_sip_tpselector_unref(&selector); |
| 939 |
|
| 940 |
status = pjsip_regc_send(client_state->client, tdata); |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
if ((status != PJ_SUCCESS) && !(*callback_invoked)) { |
| 947 |
pjsip_tx_data_dec_ref(tdata); |
| 948 |
ao2_ref(client_state, -1); |
| 949 |
return status; |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
if (client_state->last_tdata) { |
| 958 |
pjsip_tx_data_dec_ref(client_state->last_tdata); |
| 959 |
} |
| 960 |
client_state->last_tdata = tdata; |
| 961 |
|
| 962 |
return status; |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
static int add_to_supported_header(pjsip_tx_data *tdata, pj_str_t *name) |
| 967 |
{ |
| 968 |
pjsip_supported_hdr *hdr; |
| 969 |
int i; |
| 970 |
|
| 971 |
hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_SUPPORTED, NULL); |
| 972 |
if (!hdr) { |
| 973 |
|
| 974 |
hdr = pjsip_supported_hdr_create(tdata->pool); |
| 975 |
if (!hdr) { |
| 976 |
pjsip_tx_data_dec_ref(tdata); |
| 977 |
return 0; |
| 978 |
} |
| 979 |
|
| 980 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr); |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
for (i = 0; i < hdr->count; ++i) { |
| 985 |
if (pj_stricmp(&hdr->values[i], name) == 0) { |
| 986 |
return 1; |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
if (hdr->count >= PJSIP_GENERIC_ARRAY_MAX_COUNT) { |
| 991 |
return 0; |
| 992 |
} |
| 993 |
|
| 994 |
|
| 995 |
pj_strassign(&hdr->values[hdr->count++], name); |
| 996 |
|
| 997 |
return 1; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
static int add_configured_supported_headers(struct sip_outbound_registration_client_state *client_state, pjsip_tx_data *tdata) |
| 1002 |
{ |
| 1003 |
if (client_state->support_path) { |
| 1004 |
if (!add_to_supported_header(tdata, &PATH_NAME)) { |
| 1005 |
return 0; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
if (client_state->support_outbound) { |
| 1010 |
if (!add_to_supported_header(tdata, &OUTBOUND_NAME)) { |
| 1011 |
return 0; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
return 1; |
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
static int handle_client_registration(void *data) |
| 1020 |
{ |
| 1021 |
RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup); |
| 1022 |
pjsip_tx_data *tdata; |
| 1023 |
|
| 1024 |
if (set_outbound_initial_authentication_credentials(client_state->client, &client_state->outbound_auths)) { |
| 1025 |
ast_log(LOG_WARNING, "Failed to set initial authentication credentials\n"); |
| 1026 |
} |
| 1027 |
|
| 1028 |
if (client_state->status == SIP_REGISTRATION_STOPPED |
| 1029 |
|| pjsip_regc_register(client_state->client, PJ_FALSE, &tdata) != PJ_SUCCESS) { |
| 1030 |
return 0; |
| 1031 |
} |
| 1032 |
|
| 1033 |
if (DEBUG_ATLEAST(1)) { |
| 1034 |
pjsip_regc_info info; |
| 1035 |
|
| 1036 |
pjsip_regc_get_info(client_state->client, &info); |
| 1037 |
ast_log(LOG_DEBUG, "Outbound REGISTER attempt %u to '%.*s' with client '%.*s'\n", |
| 1038 |
client_state->retries + 1, |
| 1039 |
(int) info.server_uri.slen, info.server_uri.ptr, |
| 1040 |
(int) info.client_uri.slen, info.client_uri.ptr); |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (!add_configured_supported_headers(client_state, tdata)) { |
| 1044 |
ast_log(LOG_WARNING, "Failed to set supported headers\n"); |
| 1045 |
return -1; |
| 1046 |
} |
| 1047 |
|
| 1048 |
registration_client_send(client_state, tdata); |
| 1049 |
|
| 1050 |
return 0; |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
static void sip_outbound_registration_timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry) |
| 1055 |
{ |
| 1056 |
struct sip_outbound_registration_client_state *client_state = entry->user_data; |
| 1057 |
|
| 1058 |
entry->id = 0; |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
if (ast_sip_push_task(client_state->serializer, handle_client_registration, client_state)) { |
| 1066 |
ast_log(LOG_WARNING, "Scheduled outbound registration could not be executed.\n"); |
| 1067 |
ao2_ref(client_state, -1); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
static void schedule_registration(struct sip_outbound_registration_client_state *client_state, unsigned int seconds) |
| 1073 |
{ |
| 1074 |
pj_time_val delay = { .sec = seconds, }; |
| 1075 |
pjsip_regc_info info; |
| 1076 |
|
| 1077 |
cancel_registration(client_state); |
| 1078 |
|
| 1079 |
pjsip_regc_get_info(client_state->client, &info); |
| 1080 |
ast_debug(1, "Scheduling outbound registration to server '%.*s' from client '%.*s' in %d seconds\n", |
| 1081 |
(int) info.server_uri.slen, info.server_uri.ptr, |
| 1082 |
(int) info.client_uri.slen, info.client_uri.ptr, |
| 1083 |
seconds); |
| 1084 |
|
| 1085 |
ao2_ref(client_state, +1); |
| 1086 |
if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(), &client_state->timer, &delay) != PJ_SUCCESS) { |
| 1087 |
ast_log(LOG_WARNING, "Failed to schedule registration to server '%.*s' from client '%.*s'\n", |
| 1088 |
(int) info.server_uri.slen, info.server_uri.ptr, |
| 1089 |
(int) info.client_uri.slen, info.client_uri.ptr); |
| 1090 |
ao2_ref(client_state, -1); |
| 1091 |
} |
| 1092 |
client_state->registration_expires = ((int) time(NULL)) + seconds; |
| 1093 |
} |
| 1094 |
|
| 1095 |
static void update_client_state_status(struct sip_outbound_registration_client_state *client_state, enum sip_outbound_registration_status status) |
| 1096 |
{ |
| 1097 |
const char *status_old; |
| 1098 |
const char *status_new; |
| 1099 |
|
| 1100 |
if (client_state->status == status) { |
| 1101 |
|
| 1102 |
return; |
| 1103 |
} |
| 1104 |
|
| 1105 |
status_old = sip_outbound_registration_status_str(client_state->status); |
| 1106 |
status_new = sip_outbound_registration_status_str(status); |
| 1107 |
client_state->status = status; |
| 1108 |
|
| 1109 |
if (!strcmp(status_old, status_new)) { |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
return; |
| 1115 |
} |
| 1116 |
|
| 1117 |
ast_statsd_log_string_va("PJSIP.registrations.state.%s", AST_STATSD_GAUGE, "-1", 1.0, |
| 1118 |
status_old); |
| 1119 |
ast_statsd_log_string_va("PJSIP.registrations.state.%s", AST_STATSD_GAUGE, "+1", 1.0, |
| 1120 |
status_new); |
| 1121 |
} |
| 1122 |
|
| 1123 |
|
| 1124 |
static int handle_client_state_destruction(void *data) |
| 1125 |
{ |
| 1126 |
struct sip_outbound_registration_client_state *client_state = data; |
| 1127 |
|
| 1128 |
cancel_registration(client_state); |
| 1129 |
|
| 1130 |
if (client_state->client) { |
| 1131 |
pjsip_regc_info info; |
| 1132 |
pjsip_tx_data *tdata; |
| 1133 |
|
| 1134 |
pjsip_regc_get_info(client_state->client, &info); |
| 1135 |
|
| 1136 |
if (info.is_busy == PJ_TRUE) { |
| 1137 |
|
| 1138 |
ast_debug(1, |
| 1139 |
"Registration transaction is busy with server '%.*s' from client '%.*s'.\n", |
| 1140 |
(int) info.server_uri.slen, info.server_uri.ptr, |
| 1141 |
(int) info.client_uri.slen, info.client_uri.ptr); |
| 1142 |
client_state->destroy = 1; |
| 1143 |
ao2_ref(client_state, -1); |
| 1144 |
return 0; |
| 1145 |
} |
| 1146 |
|
| 1147 |
switch (client_state->status) { |
| 1148 |
case SIP_REGISTRATION_UNREGISTERED: |
| 1149 |
break; |
| 1150 |
case SIP_REGISTRATION_REGISTERED: |
| 1151 |
ast_debug(1, |
| 1152 |
"Trying to unregister with server '%.*s' from client '%.*s' before destruction.\n", |
| 1153 |
(int) info.server_uri.slen, info.server_uri.ptr, |
| 1154 |
(int) info.client_uri.slen, info.client_uri.ptr); |
| 1155 |
|
| 1156 |
update_client_state_status(client_state, SIP_REGISTRATION_STOPPING); |
| 1157 |
client_state->destroy = 1; |
| 1158 |
if (pjsip_regc_unregister(client_state->client, &tdata) == PJ_SUCCESS |
| 1159 |
&& add_configured_supported_headers(client_state, tdata) |
| 1160 |
&& registration_client_send(client_state, tdata) == PJ_SUCCESS) { |
| 1161 |
ao2_ref(client_state, -1); |
| 1162 |
return 0; |
| 1163 |
} |
| 1164 |
break; |
| 1165 |
case SIP_REGISTRATION_REJECTED_TEMPORARY: |
| 1166 |
case SIP_REGISTRATION_REJECTED_PERMANENT: |
| 1167 |
case SIP_REGISTRATION_STOPPING: |
| 1168 |
case SIP_REGISTRATION_STOPPED: |
| 1169 |
break; |
| 1170 |
} |
| 1171 |
|
| 1172 |
pjsip_regc_destroy(client_state->client); |
| 1173 |
client_state->client = NULL; |
| 1174 |
} |
| 1175 |
|
| 1176 |
update_client_state_status(client_state, SIP_REGISTRATION_STOPPED); |
| 1177 |
ast_sip_auth_vector_destroy(&client_state->outbound_auths); |
| 1178 |
ast_sip_security_mechanisms_vector_destroy(&client_state->security_mechanisms); |
| 1179 |
ast_sip_security_mechanisms_vector_destroy(&client_state->server_security_mechanisms); |
| 1180 |
ao2_ref(client_state, -1); |
| 1181 |
|
| 1182 |
return 0; |
| 1183 |
} |
| 1184 |
|
| 1185 |
|
| 1186 |
struct registration_response { |
| 1187 |
|
| 1188 |
int code; |
| 1189 |
|
| 1190 |
int expiration; |
| 1191 |
|
| 1192 |
int retry_after; |
| 1193 |
|
| 1194 |
struct sip_outbound_registration_client_state *client_state; |
| 1195 |
|
| 1196 |
pjsip_rx_data *rdata; |
| 1197 |
|
| 1198 |
pjsip_tx_data *old_request; |
| 1199 |
|
| 1200 |
char transport_key[IP6ADDR_COLON_PORT_BUFLEN]; |
| 1201 |
}; |
| 1202 |
|
| 1203 |
|
| 1204 |
static void registration_response_destroy(void *obj) |
| 1205 |
{ |
| 1206 |
struct registration_response *response = obj; |
| 1207 |
|
| 1208 |
if (response->rdata) { |
| 1209 |
pjsip_rx_data_free_cloned(response->rdata); |
| 1210 |
} |
| 1211 |
|
| 1212 |
if (response->old_request) { |
| 1213 |
pjsip_tx_data_dec_ref(response->old_request); |
| 1214 |
} |
| 1215 |
|
| 1216 |
ao2_cleanup(response->client_state); |
| 1217 |
} |
| 1218 |
|
| 1219 |
|
| 1220 |
static int sip_outbound_registration_is_temporal(unsigned int code, |
| 1221 |
struct sip_outbound_registration_client_state *client_state) |
| 1222 |
{ |
| 1223 |
|
| 1224 |
if (code == PJSIP_SC_REQUEST_TIMEOUT || |
| 1225 |
code == PJSIP_SC_INTERNAL_SERVER_ERROR || |
| 1226 |
code == PJSIP_SC_BAD_GATEWAY || |
| 1227 |
code == PJSIP_SC_SERVICE_UNAVAILABLE || |
| 1228 |
code == PJSIP_SC_SERVER_TIMEOUT || |
| 1229 |
((code == PJSIP_SC_UNAUTHORIZED || |
| 1230 |
code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED) && |
| 1231 |
!client_state->auth_rejection_permanent) || |
| 1232 |
PJSIP_IS_STATUS_IN_CLASS(code, 600)) { |
| 1233 |
return 1; |
| 1234 |
} else { |
| 1235 |
return 0; |
| 1236 |
} |
| 1237 |
} |
| 1238 |
|
| 1239 |
static void schedule_retry(struct registration_response *response, unsigned int interval, |
| 1240 |
const char *server_uri, const char *client_uri) |
| 1241 |
{ |
| 1242 |
update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_TEMPORARY); |
| 1243 |
schedule_registration(response->client_state, interval); |
| 1244 |
|
| 1245 |
if (response->rdata) { |
| 1246 |
ast_log(LOG_WARNING, "Temporal response '%d' received from '%s' on " |
| 1247 |
"registration attempt to '%s', retrying in '%u'\n", |
| 1248 |
response->code, server_uri, client_uri, interval); |
| 1249 |
} else { |
| 1250 |
ast_log(LOG_WARNING, "No response received from '%s' on " |
| 1251 |
"registration attempt to '%s', retrying in '%u'\n", |
| 1252 |
server_uri, client_uri, interval); |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
static int reregister_immediately_cb(void *obj) |
| 1257 |
{ |
| 1258 |
struct sip_outbound_registration_state *state = obj; |
| 1259 |
|
| 1260 |
if (state->client_state->status != SIP_REGISTRATION_REGISTERED) { |
| 1261 |
ao2_ref(state, -1); |
| 1262 |
return 0; |
| 1263 |
} |
| 1264 |
|
| 1265 |
if (DEBUG_ATLEAST(1)) { |
| 1266 |
pjsip_regc_info info; |
| 1267 |
|
| 1268 |
pjsip_regc_get_info(state->client_state->client, &info); |
| 1269 |
ast_log(LOG_DEBUG, |
| 1270 |
"Outbound registration transport to server '%.*s' from client '%.*s' shutdown\n", |
| 1271 |
(int) info.server_uri.slen, info.server_uri.ptr, |
| 1272 |
(int) info.client_uri.slen, info.client_uri.ptr); |
| 1273 |
} |
| 1274 |
|
| 1275 |
cancel_registration(state->client_state); |
| 1276 |
|
| 1277 |
ao2_ref(state->client_state, +1); |
| 1278 |
handle_client_registration(state->client_state); |
| 1279 |
|
| 1280 |
ao2_ref(state, -1); |
| 1281 |
return 0; |
| 1282 |
} |
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
static void registration_transport_shutdown_cb(void *obj) |
| 1294 |
{ |
| 1295 |
const char *registration_name = obj; |
| 1296 |
struct sip_outbound_registration_state *state; |
| 1297 |
|
| 1298 |
state = get_state(registration_name); |
| 1299 |
if (!state) { |
| 1300 |
|
| 1301 |
return; |
| 1302 |
} |
| 1303 |
if (ast_sip_push_task(state->client_state->serializer, reregister_immediately_cb, state)) { |
| 1304 |
ao2_ref(state, -1); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
static int monitor_matcher(void *a, void *b) |
| 1309 |
{ |
| 1310 |
char *ma = a; |
| 1311 |
char *mb = b; |
| 1312 |
|
| 1313 |
return strcmp(ma, mb) == 0; |
| 1314 |
} |
| 1315 |
|
| 1316 |
static void registration_transport_monitor_setup(const char *transport_key, const char *registration_name) |
| 1317 |
{ |
| 1318 |
char *monitor; |
| 1319 |
enum ast_transport_monitor_reg monitor_res; |
| 1320 |
|
| 1321 |
monitor = ao2_alloc_options(strlen(registration_name) + 1, NULL, |
| 1322 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 1323 |
if (!monitor) { |
| 1324 |
return; |
| 1325 |
} |
| 1326 |
strcpy(monitor, registration_name); |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
if((monitor_res = ast_sip_transport_monitor_register_replace_key(transport_key, registration_transport_shutdown_cb, |
| 1334 |
monitor, monitor_matcher))) { |
| 1335 |
ast_log(LOG_NOTICE, "Failed to register transport monitor for regisration %s: %d\n", registration_name, monitor_res); |
| 1336 |
} |
| 1337 |
ao2_ref(monitor, -1); |
| 1338 |
} |
| 1339 |
|
| 1340 |
static void save_response_fields_to_transport(struct registration_response *response) |
| 1341 |
{ |
| 1342 |
static const pj_str_t associated_uri_str = { "P-Associated-URI", 16 }; |
| 1343 |
static const pj_str_t service_route_str = { "Service-Route", 13 }; |
| 1344 |
pjsip_hdr *header = NULL; |
| 1345 |
pjsip_msg *msg = response->rdata->msg_info.msg; |
| 1346 |
struct ast_sip_service_route_vector *service_routes = NULL; |
| 1347 |
|
| 1348 |
|
| 1349 |
if (ast_strlen_zero(response->client_state->transport_name)) { |
| 1350 |
return; |
| 1351 |
} |
| 1352 |
|
| 1353 |
ast_sip_transport_state_set_transport(response->client_state->transport_name, response->rdata->tp_info.transport); |
| 1354 |
|
| 1355 |
while ((header = pjsip_msg_find_hdr_by_name(msg, &service_route_str, header ? header->next : NULL))) { |
| 1356 |
char *service_route; |
| 1357 |
size_t size; |
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
size = pj_strlen(&((pjsip_generic_string_hdr*)header)->hvalue) + 1; |
| 1364 |
service_route = ast_malloc(size); |
| 1365 |
if (!service_route) { |
| 1366 |
if (service_routes) { |
| 1367 |
ast_sip_service_route_vector_destroy(service_routes); |
| 1368 |
service_routes = NULL; |
| 1369 |
} |
| 1370 |
break; |
| 1371 |
} |
| 1372 |
|
| 1373 |
ast_copy_pj_str(service_route, &((pjsip_generic_string_hdr*)header)->hvalue, size); |
| 1374 |
|
| 1375 |
if (!service_routes) { |
| 1376 |
service_routes = ast_sip_service_route_vector_alloc(); |
| 1377 |
if (!service_routes) { |
| 1378 |
ast_free(service_route); |
| 1379 |
break; |
| 1380 |
} |
| 1381 |
} |
| 1382 |
|
| 1383 |
if (AST_VECTOR_APPEND(service_routes, service_route)) { |
| 1384 |
ast_free(service_route); |
| 1385 |
ast_sip_service_route_vector_destroy(service_routes); |
| 1386 |
service_routes = NULL; |
| 1387 |
break; |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
if (service_routes) { |
| 1393 |
ast_sip_transport_state_set_service_routes(response->client_state->transport_name, service_routes); |
| 1394 |
} |
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
header = pjsip_msg_find_hdr_by_name(msg, &associated_uri_str, NULL); |
| 1400 |
if (header) { |
| 1401 |
char value[pj_strlen(&((pjsip_generic_string_hdr*)header)->hvalue) + 1]; |
| 1402 |
|
| 1403 |
ast_copy_pj_str(value, &((pjsip_generic_string_hdr*)header)->hvalue, sizeof(value)); |
| 1404 |
ast_sip_transport_state_set_preferred_identity(response->client_state->transport_name, value); |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
static int handle_registration_response(void *data) |
| 1411 |
{ |
| 1412 |
struct registration_response *response = data; |
| 1413 |
pjsip_regc_info info; |
| 1414 |
char server_uri[PJSIP_MAX_URL_SIZE]; |
| 1415 |
char client_uri[PJSIP_MAX_URL_SIZE]; |
| 1416 |
|
| 1417 |
if (response->client_state->status == SIP_REGISTRATION_STOPPED) { |
| 1418 |
ao2_ref(response, -1); |
| 1419 |
return 0; |
| 1420 |
} |
| 1421 |
|
| 1422 |
pjsip_regc_get_info(response->client_state->client, &info); |
| 1423 |
ast_copy_pj_str(server_uri, &info.server_uri, sizeof(server_uri)); |
| 1424 |
ast_copy_pj_str(client_uri, &info.client_uri, sizeof(client_uri)); |
| 1425 |
response->client_state->last_status_code = response->code; |
| 1426 |
|
| 1427 |
ast_debug(1, "Processing REGISTER response %d from server '%s' for client '%s'\n", |
| 1428 |
response->code, server_uri, client_uri); |
| 1429 |
|
| 1430 |
if (response->code == 408 || response->code == 503) { |
| 1431 |
if ((ast_sip_failover_request(response->old_request))) { |
| 1432 |
int res = registration_client_send(response->client_state, response->old_request); |
| 1433 |
|
| 1434 |
response->old_request = NULL; |
| 1435 |
if (res == PJ_SUCCESS) { |
| 1436 |
ao2_ref(response, -1); |
| 1437 |
return 0; |
| 1438 |
} |
| 1439 |
} |
| 1440 |
} else if ((response->code == 401 || response->code == 407 || response->code == 494) |
| 1441 |
&& (!response->client_state->auth_attempted |
| 1442 |
|| response->rdata->msg_info.cseq->cseq != response->client_state->auth_cseq)) { |
| 1443 |
int res; |
| 1444 |
pjsip_cseq_hdr *cseq_hdr; |
| 1445 |
pjsip_tx_data *tdata; |
| 1446 |
|
| 1447 |
if (response->client_state->security_negotiation == AST_SIP_SECURITY_NEG_MEDIASEC) { |
| 1448 |
struct sip_outbound_registration *reg = NULL; |
| 1449 |
struct ast_sip_endpoint *endpt = NULL; |
| 1450 |
struct ao2_container *contact_container = NULL; |
| 1451 |
pjsip_generic_string_hdr *header; |
| 1452 |
struct pjsip_generic_string_hdr_vector header_vector; |
| 1453 |
static const pj_str_t security_server = { "Security-Server", 15 }; |
| 1454 |
|
| 1455 |
if ((reg = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "registration", |
| 1456 |
response->client_state->registration_name)) && reg->endpoint && |
| 1457 |
(endpt = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", reg->endpoint))) { |
| 1458 |
|
| 1459 |
contact_container = ast_sip_location_retrieve_contacts_from_aor_list(endpt->aors); |
| 1460 |
} |
| 1461 |
|
| 1462 |
AST_VECTOR_INIT(&header_vector, 1); |
| 1463 |
header = pjsip_msg_find_hdr_by_name(response->rdata->msg_info.msg, &security_server, NULL); |
| 1464 |
for (; header; |
| 1465 |
header = pjsip_msg_find_hdr_by_name(response->rdata->msg_info.msg, &security_server, header->next)) { |
| 1466 |
AST_VECTOR_APPEND(&header_vector, header); |
| 1467 |
ast_sip_header_to_security_mechanism(header, &response->client_state->server_security_mechanisms); |
| 1468 |
} |
| 1469 |
if (contact_container) { |
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
ao2_callback(contact_container, OBJ_NODATA, contact_add_security_headers_to_status, &header_vector); |
| 1474 |
ao2_cleanup(contact_container); |
| 1475 |
} |
| 1476 |
AST_VECTOR_FREE(&header_vector); |
| 1477 |
ao2_cleanup(endpt); |
| 1478 |
ao2_cleanup(reg); |
| 1479 |
} |
| 1480 |
|
| 1481 |
if (response->code == 494) { |
| 1482 |
update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_TEMPORARY); |
| 1483 |
response->client_state->retries++; |
| 1484 |
schedule_registration(response->client_state, 0); |
| 1485 |
ao2_ref(response, -1); |
| 1486 |
return 0; |
| 1487 |
} else if (!ast_sip_create_request_with_auth(&response->client_state->outbound_auths, |
| 1488 |
response->rdata, response->old_request, &tdata)) { |
| 1489 |
response->client_state->auth_attempted = 1; |
| 1490 |
ast_debug(1, "Sending authenticated REGISTER to server '%s' from client '%s'\n", |
| 1491 |
server_uri, client_uri); |
| 1492 |
pjsip_tx_data_add_ref(tdata); |
| 1493 |
|
| 1494 |
res = registration_client_send(response->client_state, tdata); |
| 1495 |
|
| 1496 |
|
| 1497 |
cseq_hdr = (pjsip_cseq_hdr *) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, |
| 1498 |
NULL); |
| 1499 |
response->client_state->auth_cseq = cseq_hdr->cseq; |
| 1500 |
pjsip_tx_data_dec_ref(tdata); |
| 1501 |
if (res == PJ_SUCCESS) { |
| 1502 |
ao2_ref(response, -1); |
| 1503 |
return 0; |
| 1504 |
} |
| 1505 |
} else { |
| 1506 |
ast_log(LOG_WARNING, "Failed to create authenticated REGISTER request to server '%s' from client '%s'\n", |
| 1507 |
server_uri, client_uri); |
| 1508 |
} |
| 1509 |
|
| 1510 |
} |
| 1511 |
|
| 1512 |
response->client_state->auth_attempted = 0; |
| 1513 |
|
| 1514 |
if (PJSIP_IS_STATUS_IN_CLASS(response->code, 200)) { |
| 1515 |
|
| 1516 |
if (response->expiration) { |
| 1517 |
int next_registration_round; |
| 1518 |
|
| 1519 |
|
| 1520 |
ast_debug(1, "Outbound registration to '%s' with client '%s' successful\n", server_uri, client_uri); |
| 1521 |
update_client_state_status(response->client_state, SIP_REGISTRATION_REGISTERED); |
| 1522 |
response->client_state->retries = 0; |
| 1523 |
next_registration_round = response->expiration - REREGISTER_BUFFER_TIME; |
| 1524 |
if (next_registration_round < 0) { |
| 1525 |
|
| 1526 |
next_registration_round = 0; |
| 1527 |
} |
| 1528 |
schedule_registration(response->client_state, next_registration_round); |
| 1529 |
|
| 1530 |
|
| 1531 |
if (PJSIP_TRANSPORT_IS_RELIABLE(response->rdata->tp_info.transport)) { |
| 1532 |
registration_transport_monitor_setup(response->transport_key, |
| 1533 |
response->client_state->registration_name); |
| 1534 |
} |
| 1535 |
} else { |
| 1536 |
ast_debug(1, "Outbound unregistration to '%s' with client '%s' successful\n", server_uri, client_uri); |
| 1537 |
update_client_state_status(response->client_state, SIP_REGISTRATION_UNREGISTERED); |
| 1538 |
if (PJSIP_TRANSPORT_IS_RELIABLE(response->rdata->tp_info.transport)) { |
| 1539 |
ast_sip_transport_monitor_unregister_key(response->transport_key, |
| 1540 |
registration_transport_shutdown_cb, response->client_state->registration_name, |
| 1541 |
monitor_matcher); |
| 1542 |
} |
| 1543 |
} |
| 1544 |
|
| 1545 |
save_response_fields_to_transport(response); |
| 1546 |
} else if (response->client_state->destroy) { |
| 1547 |
|
| 1548 |
} else if (response->retry_after) { |
| 1549 |
|
| 1550 |
schedule_retry(response, response->retry_after, server_uri, client_uri); |
| 1551 |
} else if (response->client_state->retry_interval |
| 1552 |
&& sip_outbound_registration_is_temporal(response->code, response->client_state)) { |
| 1553 |
if (response->client_state->retries == response->client_state->max_retries) { |
| 1554 |
|
| 1555 |
update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_PERMANENT); |
| 1556 |
ast_log(LOG_WARNING, "Maximum retries reached when attempting outbound registration to '%s' with client '%s', stopping registration attempt\n", |
| 1557 |
server_uri, client_uri); |
| 1558 |
} else { |
| 1559 |
|
| 1560 |
response->client_state->retries++; |
| 1561 |
schedule_retry(response, response->client_state->retry_interval, server_uri, client_uri); |
| 1562 |
} |
| 1563 |
} else { |
| 1564 |
if (response->code == 403 |
| 1565 |
&& response->client_state->forbidden_retry_interval |
| 1566 |
&& response->client_state->retries < response->client_state->max_retries) { |
| 1567 |
|
| 1568 |
update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_TEMPORARY); |
| 1569 |
response->client_state->retries++; |
| 1570 |
schedule_registration(response->client_state, response->client_state->forbidden_retry_interval); |
| 1571 |
ast_log(LOG_WARNING, "403 Forbidden fatal response received from '%s' on registration attempt to '%s', retrying in '%u' seconds\n", |
| 1572 |
server_uri, client_uri, response->client_state->forbidden_retry_interval); |
| 1573 |
} else if (response->client_state->fatal_retry_interval |
| 1574 |
&& response->client_state->retries < response->client_state->max_retries) { |
| 1575 |
|
| 1576 |
update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_TEMPORARY); |
| 1577 |
response->client_state->retries++; |
| 1578 |
schedule_registration(response->client_state, response->client_state->fatal_retry_interval); |
| 1579 |
ast_log(LOG_WARNING, "'%d' fatal response received from '%s' on registration attempt to '%s', retrying in '%u' seconds\n", |
| 1580 |
response->code, server_uri, client_uri, response->client_state->fatal_retry_interval); |
| 1581 |
} else { |
| 1582 |
|
| 1583 |
update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_PERMANENT); |
| 1584 |
if (response->rdata) { |
| 1585 |
ast_log(LOG_WARNING, "Fatal response '%d' received from '%s' on registration attempt to '%s', stopping outbound registration\n", |
| 1586 |
response->code, server_uri, client_uri); |
| 1587 |
} else { |
| 1588 |
ast_log(LOG_WARNING, "Fatal registration attempt to '%s', stopping outbound registration\n", client_uri); |
| 1589 |
} |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
ast_system_publish_registry("PJSIP", client_uri, server_uri, |
| 1594 |
sip_outbound_registration_status_str(response->client_state->status), NULL); |
| 1595 |
|
| 1596 |
if (response->client_state->destroy) { |
| 1597 |
|
| 1598 |
ao2_ref(response->client_state, +1); |
| 1599 |
handle_client_state_destruction(response->client_state); |
| 1600 |
} |
| 1601 |
|
| 1602 |
ao2_ref(response, -1); |
| 1603 |
return 0; |
| 1604 |
} |
| 1605 |
|
| 1606 |
|
| 1607 |
static void sip_outbound_registration_response_cb(struct pjsip_regc_cbparam *param) |
| 1608 |
{ |
| 1609 |
struct sip_outbound_registration_client_state *client_state = param->token; |
| 1610 |
struct registration_response *response; |
| 1611 |
int *callback_invoked; |
| 1612 |
|
| 1613 |
callback_invoked = ast_threadstorage_get(®ister_callback_invoked, sizeof(int)); |
| 1614 |
|
| 1615 |
ast_assert(callback_invoked != NULL); |
| 1616 |
ast_assert(client_state != NULL); |
| 1617 |
|
| 1618 |
*callback_invoked = 1; |
| 1619 |
|
| 1620 |
response = ao2_alloc(sizeof(*response), registration_response_destroy); |
| 1621 |
if (!response) { |
| 1622 |
ao2_ref(client_state, -1); |
| 1623 |
return; |
| 1624 |
} |
| 1625 |
response->code = param->code; |
| 1626 |
response->expiration = param->expiration; |
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
response->client_state = client_state; |
| 1633 |
|
| 1634 |
ast_debug(1, "Received REGISTER response %d(%.*s)\n", |
| 1635 |
param->code, (int) param->reason.slen, param->reason.ptr); |
| 1636 |
|
| 1637 |
if (param->rdata) { |
| 1638 |
struct pjsip_retry_after_hdr *retry_after; |
| 1639 |
pjsip_transaction *tsx; |
| 1640 |
|
| 1641 |
retry_after = pjsip_msg_find_hdr(param->rdata->msg_info.msg, PJSIP_H_RETRY_AFTER, |
| 1642 |
NULL); |
| 1643 |
response->retry_after = retry_after ? retry_after->ivalue : 0; |
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
pjsip_tx_data_dec_ref(client_state->last_tdata); |
| 1653 |
|
| 1654 |
tsx = pjsip_rdata_get_tsx(param->rdata); |
| 1655 |
response->old_request = tsx->last_tx; |
| 1656 |
pjsip_tx_data_add_ref(response->old_request); |
| 1657 |
pjsip_rx_data_clone(param->rdata, 0, &response->rdata); |
| 1658 |
AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(param->rdata->tp_info.transport, |
| 1659 |
response->transport_key); |
| 1660 |
|
| 1661 |
} else { |
| 1662 |
|
| 1663 |
response->old_request = client_state->last_tdata; |
| 1664 |
} |
| 1665 |
client_state->last_tdata = NULL; |
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
if (ast_sip_push_task(client_state->serializer, handle_registration_response, response)) { |
| 1673 |
ast_log(LOG_WARNING, "Failed to pass incoming registration response to taskpool\n"); |
| 1674 |
ao2_cleanup(response); |
| 1675 |
} |
| 1676 |
} |
| 1677 |
|
| 1678 |
|
| 1679 |
static void sip_outbound_registration_state_destroy(void *obj) |
| 1680 |
{ |
| 1681 |
struct sip_outbound_registration_state *state = obj; |
| 1682 |
|
| 1683 |
ast_debug(3, "Destroying registration state for registration to server '%s' from client '%s'\n", |
| 1684 |
state->registration ? state->registration->server_uri : "", |
| 1685 |
state->registration ? state->registration->client_uri : ""); |
| 1686 |
ao2_cleanup(state->registration); |
| 1687 |
|
| 1688 |
if (!state->client_state) { |
| 1689 |
|
| 1690 |
} else if (!state->client_state->serializer) { |
| 1691 |
ao2_ref(state->client_state, -1); |
| 1692 |
} else if (ast_sip_push_task(state->client_state->serializer, |
| 1693 |
handle_client_state_destruction, state->client_state)) { |
| 1694 |
ast_log(LOG_WARNING, "Failed to pass outbound registration client destruction to taskpool\n"); |
| 1695 |
ao2_ref(state->client_state, -1); |
| 1696 |
} |
| 1697 |
} |
| 1698 |
|
| 1699 |
|
| 1700 |
static void sip_outbound_registration_client_state_destroy(void *obj) |
| 1701 |
{ |
| 1702 |
struct sip_outbound_registration_client_state *client_state = obj; |
| 1703 |
|
| 1704 |
ast_statsd_log_string("PJSIP.registrations.count", AST_STATSD_GAUGE, "-1", 1.0); |
| 1705 |
ast_statsd_log_string_va("PJSIP.registrations.state.%s", AST_STATSD_GAUGE, "-1", 1.0, |
| 1706 |
sip_outbound_registration_status_str(client_state->status)); |
| 1707 |
|
| 1708 |
ast_taskprocessor_unreference(client_state->serializer); |
| 1709 |
ast_free(client_state->transport_name); |
| 1710 |
ast_free(client_state->registration_name); |
| 1711 |
ast_free(client_state->user_agent); |
| 1712 |
if (client_state->last_tdata) { |
| 1713 |
pjsip_tx_data_dec_ref(client_state->last_tdata); |
| 1714 |
} |
| 1715 |
} |
| 1716 |
|
| 1717 |
|
| 1718 |
static struct sip_outbound_registration_state *sip_outbound_registration_state_alloc(struct sip_outbound_registration *registration) |
| 1719 |
{ |
| 1720 |
struct sip_outbound_registration_state *state; |
| 1721 |
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1]; |
| 1722 |
|
| 1723 |
state = ao2_alloc(sizeof(*state), sip_outbound_registration_state_destroy); |
| 1724 |
if (!state) { |
| 1725 |
return NULL; |
| 1726 |
} |
| 1727 |
state->client_state = ao2_alloc(sizeof(*state->client_state), |
| 1728 |
sip_outbound_registration_client_state_destroy); |
| 1729 |
if (!state->client_state) { |
| 1730 |
ao2_cleanup(state); |
| 1731 |
return NULL; |
| 1732 |
} |
| 1733 |
|
| 1734 |
state->client_state->status = SIP_REGISTRATION_UNREGISTERED; |
| 1735 |
pj_timer_entry_init(&state->client_state->timer, 0, state->client_state, |
| 1736 |
sip_outbound_registration_timer_cb); |
| 1737 |
state->client_state->transport_name = ast_strdup(registration->transport); |
| 1738 |
state->client_state->registration_name = |
| 1739 |
ast_strdup(ast_sorcery_object_get_id(registration)); |
| 1740 |
state->client_state->user_agent = ast_strdup(registration->user_agent); |
| 1741 |
|
| 1742 |
ast_statsd_log_string("PJSIP.registrations.count", AST_STATSD_GAUGE, "+1", 1.0); |
| 1743 |
ast_statsd_log_string_va("PJSIP.registrations.state.%s", AST_STATSD_GAUGE, "+1", 1.0, |
| 1744 |
sip_outbound_registration_status_str(state->client_state->status)); |
| 1745 |
|
| 1746 |
if (!state->client_state->transport_name |
| 1747 |
|| !state->client_state->registration_name) { |
| 1748 |
ao2_cleanup(state); |
| 1749 |
return NULL; |
| 1750 |
} |
| 1751 |
|
| 1752 |
|
| 1753 |
ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/outreg/%s", |
| 1754 |
ast_sorcery_object_get_id(registration)); |
| 1755 |
|
| 1756 |
state->client_state->serializer = ast_sip_create_serializer_group(tps_name, |
| 1757 |
shutdown_group); |
| 1758 |
if (!state->client_state->serializer) { |
| 1759 |
ao2_cleanup(state); |
| 1760 |
return NULL; |
| 1761 |
} |
| 1762 |
|
| 1763 |
state->registration = ao2_bump(registration); |
| 1764 |
return state; |
| 1765 |
} |
| 1766 |
|
| 1767 |
|
| 1768 |
static void sip_outbound_registration_destroy(void *obj) |
| 1769 |
{ |
| 1770 |
struct sip_outbound_registration *registration = obj; |
| 1771 |
|
| 1772 |
ast_sip_auth_vector_destroy(®istration->outbound_auths); |
| 1773 |
ast_sip_security_mechanisms_vector_destroy(®istration->security_mechanisms); |
| 1774 |
|
| 1775 |
ast_string_field_free_memory(registration); |
| 1776 |
} |
| 1777 |
|
| 1778 |
|
| 1779 |
static void *sip_outbound_registration_alloc(const char *name) |
| 1780 |
{ |
| 1781 |
struct sip_outbound_registration *registration; |
| 1782 |
|
| 1783 |
registration = ast_sorcery_generic_alloc(sizeof(*registration), |
| 1784 |
sip_outbound_registration_destroy); |
| 1785 |
if (!registration || ast_string_field_init(registration, 256)) { |
| 1786 |
ao2_cleanup(registration); |
| 1787 |
return NULL; |
| 1788 |
} |
| 1789 |
|
| 1790 |
return registration; |
| 1791 |
} |
| 1792 |
|
| 1793 |
|
| 1794 |
static int sip_dialog_create_contact(pj_pool_t *pool, pj_str_t *contact, const char *user, |
| 1795 |
const pj_str_t *target, pjsip_tpselector *selector, const char *line, const char *header_params) |
| 1796 |
{ |
| 1797 |
pj_str_t tmp, local_addr; |
| 1798 |
pjsip_uri *uri; |
| 1799 |
pjsip_sip_uri *sip_uri; |
| 1800 |
pjsip_transport_type_e type; |
| 1801 |
int local_port; |
| 1802 |
|
| 1803 |
pj_strdup_with_null(pool, &tmp, target); |
| 1804 |
|
| 1805 |
if (!(uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0)) || |
| 1806 |
(!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) { |
| 1807 |
return -1; |
| 1808 |
} |
| 1809 |
|
| 1810 |
sip_uri = pjsip_uri_get_uri(uri); |
| 1811 |
|
| 1812 |
type = pjsip_transport_get_type_from_name(&sip_uri->transport_param); |
| 1813 |
if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) { |
| 1814 |
if (type == PJSIP_TRANSPORT_UNSPECIFIED |
| 1815 |
|| !(pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE)) { |
| 1816 |
type = PJSIP_TRANSPORT_TLS; |
| 1817 |
} |
| 1818 |
} else if (!sip_uri->transport_param.slen) { |
| 1819 |
type = PJSIP_TRANSPORT_UDP; |
| 1820 |
} else if (type == PJSIP_TRANSPORT_UNSPECIFIED) { |
| 1821 |
return -1; |
| 1822 |
} |
| 1823 |
|
| 1824 |
if (pj_strchr(&sip_uri->host, ':')) { |
| 1825 |
type |= PJSIP_TRANSPORT_IPV6; |
| 1826 |
} |
| 1827 |
|
| 1828 |
if (pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), |
| 1829 |
pool, type, selector, &local_addr, &local_port) != PJ_SUCCESS) { |
| 1830 |
return -1; |
| 1831 |
} |
| 1832 |
|
| 1833 |
if (!pj_strchr(&sip_uri->host, ':') && pj_strchr(&local_addr, ':')) { |
| 1834 |
type |= PJSIP_TRANSPORT_IPV6; |
| 1835 |
} |
| 1836 |
|
| 1837 |
contact->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE); |
| 1838 |
contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE, |
| 1839 |
"<%s:%s@%s%.*s%s:%d%s%s%s%s>%s%s", |
| 1840 |
((pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) && PJSIP_URI_SCHEME_IS_SIPS(uri)) ? "sips" : "sip", |
| 1841 |
user, |
| 1842 |
(type & PJSIP_TRANSPORT_IPV6) ? "[" : "", |
| 1843 |
(int)local_addr.slen, |
| 1844 |
local_addr.ptr, |
| 1845 |
(type & PJSIP_TRANSPORT_IPV6) ? "]" : "", |
| 1846 |
local_port, |
| 1847 |
(type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "", |
| 1848 |
(type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "", |
| 1849 |
!ast_strlen_zero(line) ? ";line=" : "", |
| 1850 |
S_OR(line, ""), |
| 1851 |
!ast_strlen_zero(header_params) ? ";" : "", |
| 1852 |
S_OR(header_params, "")); |
| 1853 |
|
| 1854 |
return 0; |
| 1855 |
} |
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
static int can_reuse_registration(struct sip_outbound_registration *existing, |
| 1868 |
struct sip_outbound_registration *applied) |
| 1869 |
{ |
| 1870 |
int rc = 1; |
| 1871 |
struct ast_sorcery *sorcery = ast_sip_get_sorcery(); |
| 1872 |
struct ast_variable *ve = ast_sorcery_objectset_create(sorcery, existing); |
| 1873 |
struct ast_variable *va = ast_sorcery_objectset_create(sorcery, applied); |
| 1874 |
struct ast_variable *vc = NULL; |
| 1875 |
|
| 1876 |
if (ast_sorcery_changeset_create(ve, va, &vc) || vc != NULL) { |
| 1877 |
rc = 0; |
| 1878 |
ast_debug(4, "Registration '%s' changed. Can't re-use.\n", ast_sorcery_object_get_id(existing)); |
| 1879 |
} else { |
| 1880 |
ast_debug(4, "Registration '%s' didn't change. Can re-use\n", ast_sorcery_object_get_id(existing)); |
| 1881 |
} |
| 1882 |
|
| 1883 |
ast_variables_destroy(ve); |
| 1884 |
ast_variables_destroy(va); |
| 1885 |
ast_variables_destroy(vc); |
| 1886 |
|
| 1887 |
return rc; |
| 1888 |
} |
| 1889 |
|
| 1890 |
|
| 1891 |
static const char *fetch_google_access_token(const struct ast_sip_auth *auth) |
| 1892 |
{ |
| 1893 |
char *cmd = NULL; |
| 1894 |
const char *token; |
| 1895 |
const char *url = "https://www.googleapis.com/oauth2/v3/token"; |
| 1896 |
char buf[4096]; |
| 1897 |
int res; |
| 1898 |
struct ast_json_error error; |
| 1899 |
struct ast_json *json; |
| 1900 |
|
| 1901 |
|
| 1902 |
if (ast_func_write(NULL, "CURLOPT(conntimeout)", "10")) { |
| 1903 |
ast_log(LOG_ERROR, "CURL is unavailable. This is required for Google OAuth 2.0 authentication. Please ensure it is loaded.\n"); |
| 1904 |
return NULL; |
| 1905 |
} |
| 1906 |
|
| 1907 |
res = ast_asprintf(&cmd, |
| 1908 |
"CURL(%s,client_id=%s&client_secret=%s&refresh_token=%s&grant_type=refresh_token)", |
| 1909 |
url, auth->oauth_clientid, auth->oauth_secret, auth->refresh_token); |
| 1910 |
if (res < 0) { |
| 1911 |
return NULL; |
| 1912 |
} |
| 1913 |
|
| 1914 |
ast_debug(2, "Performing Google OAuth 2.0 authentication using command: %s\n", cmd); |
| 1915 |
|
| 1916 |
buf[0] = '\0'; |
| 1917 |
res = ast_func_read(NULL, cmd, buf, sizeof(buf)); |
| 1918 |
ast_free(cmd); |
| 1919 |
if (res) { |
| 1920 |
ast_log(LOG_ERROR, "Could not retrieve Google OAuth 2.0 authentication\n"); |
| 1921 |
return NULL; |
| 1922 |
} |
| 1923 |
|
| 1924 |
ast_debug(2, "Google OAuth 2.0 authentication returned: %s\n", buf); |
| 1925 |
|
| 1926 |
json = ast_json_load_string(buf, &error); |
| 1927 |
if (!json) { |
| 1928 |
ast_log(LOG_ERROR, "Could not parse Google OAuth 2.0 authentication: %d(%d) %s: '%s'\n", |
| 1929 |
error.line, error.column, error.text, buf); |
| 1930 |
return NULL; |
| 1931 |
} |
| 1932 |
|
| 1933 |
token = ast_json_string_get(ast_json_object_get(json, "access_token")); |
| 1934 |
if (!token) { |
| 1935 |
ast_log(LOG_ERROR, "Could not find Google OAuth 2.0 access_token in: '%s'\n", |
| 1936 |
buf); |
| 1937 |
} |
| 1938 |
token = ast_strdup(token); |
| 1939 |
ast_json_unref(json); |
| 1940 |
return token; |
| 1941 |
} |
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
static int set_outbound_initial_authentication_credentials(pjsip_regc *regc, |
| 1952 |
const struct ast_sip_auth_vector *auth_vector) |
| 1953 |
{ |
| 1954 |
size_t auth_size = AST_VECTOR_SIZE(auth_vector); |
| 1955 |
struct ast_sip_auth *auths[auth_size]; |
| 1956 |
const char *access_token; |
| 1957 |
pjsip_cred_info auth_creds[1]; |
| 1958 |
pjsip_auth_clt_pref prefs; |
| 1959 |
int res = 0; |
| 1960 |
int idx; |
| 1961 |
|
| 1962 |
memset(auths, 0, sizeof(auths)); |
| 1963 |
if (ast_sip_retrieve_auths(auth_vector, auths)) { |
| 1964 |
res = -1; |
| 1965 |
goto cleanup; |
| 1966 |
} |
| 1967 |
|
| 1968 |
for (idx = 0; idx < auth_size; ++idx) { |
| 1969 |
switch (auths[idx]->type) { |
| 1970 |
case AST_SIP_AUTH_TYPE_GOOGLE_OAUTH: |
| 1971 |
pj_cstr(&auth_creds[0].username, auths[idx]->auth_user); |
| 1972 |
pj_cstr(&auth_creds[0].scheme, "Bearer"); |
| 1973 |
pj_cstr(&auth_creds[0].realm, auths[idx]->realm); |
| 1974 |
ast_debug(2, "Obtaining Google OAuth access token\n"); |
| 1975 |
access_token = fetch_google_access_token(auths[idx]); |
| 1976 |
if (!access_token) { |
| 1977 |
ast_log(LOG_WARNING, "Obtaining Google OAuth access token failed\n"); |
| 1978 |
access_token = auths[idx]->auth_pass; |
| 1979 |
res = -1; |
| 1980 |
} |
| 1981 |
ast_debug(2, "Setting data to '%s'\n", access_token); |
| 1982 |
|
| 1983 |
pj_cstr(&auth_creds[0].data, access_token); |
| 1984 |
auth_creds[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 1985 |
|
| 1986 |
pjsip_regc_set_credentials(regc, 1, auth_creds); |
| 1987 |
|
| 1988 |
|
| 1989 |
prefs.initial_auth = PJ_TRUE; |
| 1990 |
pj_cstr(&prefs.algorithm, "oauth"); |
| 1991 |
pjsip_regc_set_prefs(regc, &prefs); |
| 1992 |
|
| 1993 |
if (access_token != auths[idx]->auth_pass) { |
| 1994 |
ast_free((char *) access_token); |
| 1995 |
} |
| 1996 |
break; |
| 1997 |
default: |
| 1998 |
|
| 1999 |
break; |
| 2000 |
} |
| 2001 |
} |
| 2002 |
|
| 2003 |
cleanup: |
| 2004 |
ast_sip_cleanup_auths(auths, auth_size); |
| 2005 |
return res; |
| 2006 |
} |
| 2007 |
|
| 2008 |
|
| 2009 |
static int sip_outbound_registration_regc_alloc(void *data) |
| 2010 |
{ |
| 2011 |
struct sip_outbound_registration_state *state = data; |
| 2012 |
RAII_VAR(struct sip_outbound_registration *, registration, |
| 2013 |
ao2_bump(state->registration), ao2_cleanup); |
| 2014 |
pj_pool_t *pool; |
| 2015 |
pj_str_t tmp; |
| 2016 |
pjsip_uri *uri; |
| 2017 |
pj_str_t server_uri, client_uri, contact_uri; |
| 2018 |
pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, }; |
| 2019 |
|
| 2020 |
pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "URI Validation", 256, 256); |
| 2021 |
if (!pool) { |
| 2022 |
ast_log(LOG_ERROR, "Could not create pool for URI validation on outbound registration '%s'\n", |
| 2023 |
ast_sorcery_object_get_id(registration)); |
| 2024 |
return -1; |
| 2025 |
} |
| 2026 |
|
| 2027 |
pj_strdup2_with_null(pool, &tmp, registration->server_uri); |
| 2028 |
uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0); |
| 2029 |
if (!uri) { |
| 2030 |
ast_log(LOG_ERROR, "Invalid server URI '%s' specified on outbound registration '%s'\n", |
| 2031 |
registration->server_uri, ast_sorcery_object_get_id(registration)); |
| 2032 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool); |
| 2033 |
return -1; |
| 2034 |
} |
| 2035 |
|
| 2036 |
pj_strdup2_with_null(pool, &tmp, registration->client_uri); |
| 2037 |
uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0); |
| 2038 |
if (!uri) { |
| 2039 |
ast_log(LOG_ERROR, "Invalid client URI '%s' specified on outbound registration '%s'\n", |
| 2040 |
registration->client_uri, ast_sorcery_object_get_id(registration)); |
| 2041 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool); |
| 2042 |
return -1; |
| 2043 |
} |
| 2044 |
|
| 2045 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool); |
| 2046 |
|
| 2047 |
ast_assert(state->client_state->client == NULL); |
| 2048 |
if (pjsip_regc_create(ast_sip_get_pjsip_endpoint(), state->client_state, |
| 2049 |
sip_outbound_registration_response_cb, |
| 2050 |
&state->client_state->client) != PJ_SUCCESS) { |
| 2051 |
return -1; |
| 2052 |
} |
| 2053 |
|
| 2054 |
ast_sip_set_tpselector_from_transport_name(registration->transport, &selector); |
| 2055 |
pjsip_regc_set_transport(state->client_state->client, &selector); |
| 2056 |
|
| 2057 |
if (!ast_strlen_zero(registration->outbound_proxy)) { |
| 2058 |
pjsip_route_hdr route_set, *route; |
| 2059 |
static const pj_str_t ROUTE_HNAME = { "Route", 5 }; |
| 2060 |
pj_str_t tmp; |
| 2061 |
|
| 2062 |
pj_list_init(&route_set); |
| 2063 |
|
| 2064 |
pj_strdup2_with_null(pjsip_regc_get_pool(state->client_state->client), &tmp, |
| 2065 |
registration->outbound_proxy); |
| 2066 |
route = pjsip_parse_hdr(pjsip_regc_get_pool(state->client_state->client), |
| 2067 |
&ROUTE_HNAME, tmp.ptr, tmp.slen, NULL); |
| 2068 |
if (!route) { |
| 2069 |
ast_sip_tpselector_unref(&selector); |
| 2070 |
return -1; |
| 2071 |
} |
| 2072 |
pj_list_insert_nodes_before(&route_set, route); |
| 2073 |
|
| 2074 |
pjsip_regc_set_route_set(state->client_state->client, &route_set); |
| 2075 |
} |
| 2076 |
|
| 2077 |
if (state->registration->line) { |
| 2078 |
ast_generate_random_string(state->client_state->line, sizeof(state->client_state->line)); |
| 2079 |
} |
| 2080 |
|
| 2081 |
pj_cstr(&server_uri, registration->server_uri); |
| 2082 |
|
| 2083 |
if (sip_dialog_create_contact(pjsip_regc_get_pool(state->client_state->client), |
| 2084 |
&contact_uri, S_OR(registration->contact_user, "s"), &server_uri, &selector, |
| 2085 |
state->client_state->line, registration->contact_header_params)) { |
| 2086 |
ast_sip_tpselector_unref(&selector); |
| 2087 |
return -1; |
| 2088 |
} |
| 2089 |
|
| 2090 |
ast_sip_tpselector_unref(&selector); |
| 2091 |
|
| 2092 |
pj_cstr(&client_uri, registration->client_uri); |
| 2093 |
if (pjsip_regc_init(state->client_state->client, &server_uri, &client_uri, |
| 2094 |
&client_uri, 1, &contact_uri, registration->expiration) != PJ_SUCCESS) { |
| 2095 |
return -1; |
| 2096 |
} |
| 2097 |
|
| 2098 |
return 0; |
| 2099 |
} |
| 2100 |
|
| 2101 |
|
| 2102 |
static int sip_outbound_registration_perform(void *data) |
| 2103 |
{ |
| 2104 |
struct sip_outbound_registration_state *state = data; |
| 2105 |
struct sip_outbound_registration *registration = ao2_bump(state->registration); |
| 2106 |
size_t i; |
| 2107 |
int max_delay; |
| 2108 |
pjsip_regc_info info; |
| 2109 |
|
| 2110 |
|
| 2111 |
ast_sip_auth_vector_destroy(&state->client_state->outbound_auths); |
| 2112 |
ast_sip_security_mechanisms_vector_destroy(&state->client_state->security_mechanisms); |
| 2113 |
ast_sip_security_mechanisms_vector_destroy(&state->client_state->server_security_mechanisms); |
| 2114 |
|
| 2115 |
AST_VECTOR_INIT(&state->client_state->outbound_auths, AST_VECTOR_SIZE(®istration->outbound_auths)); |
| 2116 |
for (i = 0; i < AST_VECTOR_SIZE(®istration->outbound_auths); ++i) { |
| 2117 |
char *name = ast_strdup(AST_VECTOR_GET(®istration->outbound_auths, i)); |
| 2118 |
|
| 2119 |
if (name && AST_VECTOR_APPEND(&state->client_state->outbound_auths, name)) { |
| 2120 |
ast_free(name); |
| 2121 |
} |
| 2122 |
} |
| 2123 |
ast_sip_security_mechanisms_vector_copy(&state->client_state->security_mechanisms, |
| 2124 |
®istration->security_mechanisms); |
| 2125 |
state->client_state->retry_interval = registration->retry_interval; |
| 2126 |
state->client_state->forbidden_retry_interval = registration->forbidden_retry_interval; |
| 2127 |
state->client_state->fatal_retry_interval = registration->fatal_retry_interval; |
| 2128 |
state->client_state->max_retries = registration->max_retries; |
| 2129 |
state->client_state->retries = 0; |
| 2130 |
state->client_state->support_path = registration->support_path; |
| 2131 |
state->client_state->support_outbound = registration->support_outbound; |
| 2132 |
state->client_state->security_negotiation = registration->security_negotiation; |
| 2133 |
state->client_state->auth_rejection_permanent = registration->auth_rejection_permanent; |
| 2134 |
max_delay = registration->max_random_initial_delay; |
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
if (pjsip_regc_get_info(state->client_state->client, &info) == PJ_SUCCESS && info.interval != (unsigned) registration->expiration) { |
| 2142 |
pjsip_regc_update_expires(state->client_state->client, registration->expiration); |
| 2143 |
} |
| 2144 |
|
| 2145 |
|
| 2146 |
schedule_registration(state->client_state, (max_delay ? ast_random() % max_delay : 0) + 1); |
| 2147 |
|
| 2148 |
ao2_ref(registration, -1); |
| 2149 |
ao2_ref(state, -1); |
| 2150 |
return 0; |
| 2151 |
} |
| 2152 |
|
| 2153 |
|
| 2154 |
static int sip_outbound_registration_apply(const struct ast_sorcery *sorcery, void *obj) |
| 2155 |
{ |
| 2156 |
RAII_VAR(struct ao2_container *, states, ao2_global_obj_ref(current_states), ao2_cleanup); |
| 2157 |
RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup); |
| 2158 |
RAII_VAR(struct sip_outbound_registration_state *, new_state, NULL, ao2_cleanup); |
| 2159 |
struct sip_outbound_registration *applied = obj; |
| 2160 |
|
| 2161 |
if (!states) { |
| 2162 |
|
| 2163 |
return -1; |
| 2164 |
} |
| 2165 |
state = ao2_find(states, ast_sorcery_object_get_id(applied), OBJ_SEARCH_KEY); |
| 2166 |
|
| 2167 |
ast_debug(4, "Applying configuration to outbound registration '%s'\n", ast_sorcery_object_get_id(applied)); |
| 2168 |
|
| 2169 |
if (ast_strlen_zero(applied->server_uri)) { |
| 2170 |
ast_log(LOG_ERROR, "No server URI specified on outbound registration '%s'\n", |
| 2171 |
ast_sorcery_object_get_id(applied)); |
| 2172 |
return -1; |
| 2173 |
} else if (ast_sip_validate_uri_length(applied->server_uri)) { |
| 2174 |
ast_log(LOG_ERROR, "Server URI or hostname length exceeds pjproject limit or is not a sip(s) uri: '%s'\n", |
| 2175 |
ast_sorcery_object_get_id(applied)); |
| 2176 |
return -1; |
| 2177 |
} else if (ast_strlen_zero(applied->client_uri)) { |
| 2178 |
ast_log(LOG_ERROR, "No client URI specified on outbound registration '%s'\n", |
| 2179 |
ast_sorcery_object_get_id(applied)); |
| 2180 |
return -1; |
| 2181 |
} else if (ast_sip_validate_uri_length(applied->client_uri)) { |
| 2182 |
ast_log(LOG_ERROR, "Client URI or hostname length exceeds pjproject limit or is not a sip(s) uri: '%s'\n", |
| 2183 |
ast_sorcery_object_get_id(applied)); |
| 2184 |
return -1; |
| 2185 |
} else if (applied->line && ast_strlen_zero(applied->endpoint)) { |
| 2186 |
ast_log(LOG_ERROR, "Line support has been enabled on outbound registration '%s' without providing an endpoint\n", |
| 2187 |
ast_sorcery_object_get_id(applied)); |
| 2188 |
return -1; |
| 2189 |
} else if (!ast_strlen_zero(applied->endpoint) && !applied->line) { |
| 2190 |
ast_log(LOG_ERROR, "An endpoint has been specified on outbound registration '%s' without enabling line support\n", |
| 2191 |
ast_sorcery_object_get_id(applied)); |
| 2192 |
return -1; |
| 2193 |
} |
| 2194 |
|
| 2195 |
if (state && can_reuse_registration(state->registration, applied)) { |
| 2196 |
ast_debug(4, |
| 2197 |
"No change between old configuration and new configuration on outbound registration '%s'. Using previous state\n", |
| 2198 |
ast_sorcery_object_get_id(applied)); |
| 2199 |
|
| 2200 |
|
| 2201 |
|
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 2205 |
ao2_lock(states); |
| 2206 |
ao2_replace(state->registration, applied); |
| 2207 |
ao2_unlock(states); |
| 2208 |
return 0; |
| 2209 |
} |
| 2210 |
|
| 2211 |
if (!(new_state = sip_outbound_registration_state_alloc(applied))) { |
| 2212 |
return -1; |
| 2213 |
} |
| 2214 |
|
| 2215 |
if (ast_sip_push_task_wait_serializer(new_state->client_state->serializer, |
| 2216 |
sip_outbound_registration_regc_alloc, new_state)) { |
| 2217 |
return -1; |
| 2218 |
} |
| 2219 |
|
| 2220 |
if (ast_sip_push_task(new_state->client_state->serializer, |
| 2221 |
sip_outbound_registration_perform, ao2_bump(new_state))) { |
| 2222 |
ast_log(LOG_ERROR, "Failed to perform outbound registration on '%s'\n", |
| 2223 |
ast_sorcery_object_get_id(new_state->registration)); |
| 2224 |
ao2_ref(new_state, -1); |
| 2225 |
return -1; |
| 2226 |
} |
| 2227 |
|
| 2228 |
ao2_lock(states); |
| 2229 |
if (state) { |
| 2230 |
ao2_unlink(states, state); |
| 2231 |
} |
| 2232 |
ao2_link(states, new_state); |
| 2233 |
ao2_unlock(states); |
| 2234 |
|
| 2235 |
return 0; |
| 2236 |
} |
| 2237 |
|
| 2238 |
static int security_mechanism_to_str(const void *obj, const intptr_t *args, char **buf) |
| 2239 |
{ |
| 2240 |
const struct sip_outbound_registration *registration = obj; |
| 2241 |
|
| 2242 |
return ast_sip_security_mechanisms_to_str(®istration->security_mechanisms, 0, buf); |
| 2243 |
} |
| 2244 |
|
| 2245 |
static const char *security_negotiation_map[] = { |
| 2246 |
[AST_SIP_SECURITY_NEG_NONE] = "no", |
| 2247 |
[AST_SIP_SECURITY_NEG_MEDIASEC] = "mediasec", |
| 2248 |
}; |
| 2249 |
|
| 2250 |
static int security_negotiation_to_str(const void *obj, const intptr_t *args, char **buf) |
| 2251 |
{ |
| 2252 |
const struct sip_outbound_registration *registration = obj; |
| 2253 |
if (ARRAY_IN_BOUNDS(registration->security_negotiation, security_negotiation_map)) { |
| 2254 |
*buf = ast_strdup(security_negotiation_map[registration->security_negotiation]); |
| 2255 |
} |
| 2256 |
return 0; |
| 2257 |
} |
| 2258 |
|
| 2259 |
static int security_mechanisms_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 2260 |
{ |
| 2261 |
struct sip_outbound_registration *registration = obj; |
| 2262 |
|
| 2263 |
return ast_sip_security_mechanism_vector_init(®istration->security_mechanisms, var->value); |
| 2264 |
} |
| 2265 |
|
| 2266 |
static int security_negotiation_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 2267 |
{ |
| 2268 |
struct sip_outbound_registration *registration = obj; |
| 2269 |
|
| 2270 |
return ast_sip_set_security_negotiation(®istration->security_negotiation, var->value); |
| 2271 |
} |
| 2272 |
|
| 2273 |
static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 2274 |
{ |
| 2275 |
struct sip_outbound_registration *registration = obj; |
| 2276 |
|
| 2277 |
return ast_sip_auth_vector_init(®istration->outbound_auths, var->value); |
| 2278 |
} |
| 2279 |
|
| 2280 |
static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf) |
| 2281 |
{ |
| 2282 |
const struct sip_outbound_registration *registration = obj; |
| 2283 |
|
| 2284 |
return ast_sip_auths_to_str(®istration->outbound_auths, buf); |
| 2285 |
} |
| 2286 |
|
| 2287 |
static int outbound_auths_to_var_list(const void *obj, struct ast_variable **fields) |
| 2288 |
{ |
| 2289 |
const struct sip_outbound_registration *registration = obj; |
| 2290 |
int i; |
| 2291 |
struct ast_variable *head = NULL; |
| 2292 |
|
| 2293 |
for (i = 0; i < AST_VECTOR_SIZE(®istration->outbound_auths) ; i++) { |
| 2294 |
ast_variable_list_append(&head, ast_variable_new("outbound_auth", |
| 2295 |
AST_VECTOR_GET(®istration->outbound_auths, i), "")); |
| 2296 |
} |
| 2297 |
|
| 2298 |
if (head) { |
| 2299 |
*fields = head; |
| 2300 |
} |
| 2301 |
|
| 2302 |
return 0; |
| 2303 |
} |
| 2304 |
|
| 2305 |
static int unregister_task(void *obj) |
| 2306 |
{ |
| 2307 |
struct sip_outbound_registration_state *state = obj; |
| 2308 |
struct pjsip_regc *client = state->client_state->client; |
| 2309 |
pjsip_tx_data *tdata; |
| 2310 |
pjsip_regc_info info; |
| 2311 |
|
| 2312 |
pjsip_regc_get_info(client, &info); |
| 2313 |
ast_debug(1, "Unregistering contacts with server '%s' from client '%s'\n", |
| 2314 |
state->registration->server_uri, state->registration->client_uri); |
| 2315 |
|
| 2316 |
cancel_registration(state->client_state); |
| 2317 |
|
| 2318 |
if (pjsip_regc_unregister(client, &tdata) == PJ_SUCCESS |
| 2319 |
&& add_configured_supported_headers(state->client_state, tdata)) { |
| 2320 |
registration_client_send(state->client_state, tdata); |
| 2321 |
} |
| 2322 |
|
| 2323 |
ao2_ref(state, -1); |
| 2324 |
return 0; |
| 2325 |
} |
| 2326 |
|
| 2327 |
static int queue_unregister(struct sip_outbound_registration_state *state) |
| 2328 |
{ |
| 2329 |
ao2_ref(state, +1); |
| 2330 |
if (ast_sip_push_task(state->client_state->serializer, unregister_task, state)) { |
| 2331 |
ao2_ref(state, -1); |
| 2332 |
return -1; |
| 2333 |
} |
| 2334 |
|
| 2335 |
return 0; |
| 2336 |
} |
| 2337 |
|
| 2338 |
static int queue_register(struct sip_outbound_registration_state *state) |
| 2339 |
{ |
| 2340 |
ao2_ref(state, +1); |
| 2341 |
if (ast_sip_push_task(state->client_state->serializer, sip_outbound_registration_perform, state)) { |
| 2342 |
ao2_ref(state, -1); |
| 2343 |
return -1; |
| 2344 |
} |
| 2345 |
|
| 2346 |
return 0; |
| 2347 |
} |
| 2348 |
|
| 2349 |
static void unregister_all(void) |
| 2350 |
{ |
| 2351 |
struct ao2_container *states; |
| 2352 |
|
| 2353 |
states = ao2_global_obj_ref(current_states); |
| 2354 |
if (!states) { |
| 2355 |
return; |
| 2356 |
} |
| 2357 |
|
| 2358 |
|
| 2359 |
ao2_callback(states, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL); |
| 2360 |
ao2_ref(states, -1); |
| 2361 |
} |
| 2362 |
|
| 2363 |
static void reregister_all(void) |
| 2364 |
{ |
| 2365 |
unregister_all(); |
| 2366 |
ast_sorcery_load_object(ast_sip_get_sorcery(), "registration"); |
| 2367 |
} |
| 2368 |
|
| 2369 |
static char *cli_complete_registration(const char *line, const char *word, |
| 2370 |
int pos, int state) |
| 2371 |
{ |
| 2372 |
char *result = NULL; |
| 2373 |
int wordlen; |
| 2374 |
int which = 0; |
| 2375 |
struct sip_outbound_registration *registration; |
| 2376 |
struct ao2_container *registrations; |
| 2377 |
struct ao2_iterator i; |
| 2378 |
|
| 2379 |
if (pos != 3) { |
| 2380 |
return NULL; |
| 2381 |
} |
| 2382 |
|
| 2383 |
wordlen = strlen(word); |
| 2384 |
if (wordlen == 0 && ++which > state) { |
| 2385 |
return ast_strdup("*all"); |
| 2386 |
} |
| 2387 |
|
| 2388 |
registrations = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration", |
| 2389 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 2390 |
if (!registrations) { |
| 2391 |
return NULL; |
| 2392 |
} |
| 2393 |
|
| 2394 |
i = ao2_iterator_init(registrations, 0); |
| 2395 |
while ((registration = ao2_iterator_next(&i))) { |
| 2396 |
const char *name = ast_sorcery_object_get_id(registration); |
| 2397 |
|
| 2398 |
if (!strncasecmp(word, name, wordlen) && ++which > state) { |
| 2399 |
result = ast_strdup(name); |
| 2400 |
} |
| 2401 |
|
| 2402 |
ao2_ref(registration, -1); |
| 2403 |
if (result) { |
| 2404 |
break; |
| 2405 |
} |
| 2406 |
} |
| 2407 |
ao2_iterator_destroy(&i); |
| 2408 |
|
| 2409 |
ao2_ref(registrations, -1); |
| 2410 |
return result; |
| 2411 |
} |
| 2412 |
|
| 2413 |
static char *cli_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2414 |
{ |
| 2415 |
struct sip_outbound_registration_state *state; |
| 2416 |
const char *registration_name; |
| 2417 |
|
| 2418 |
switch (cmd) { |
| 2419 |
case CLI_INIT: |
| 2420 |
e->command = "pjsip send unregister"; |
| 2421 |
e->usage = |
| 2422 |
"Usage: pjsip send unregister <registration> | *all\n" |
| 2423 |
" Unregisters the specified (or all) outbound registration(s) " |
| 2424 |
"and stops future registration attempts.\n"; |
| 2425 |
return NULL; |
| 2426 |
case CLI_GENERATE: |
| 2427 |
return cli_complete_registration(a->line, a->word, a->pos, a->n); |
| 2428 |
} |
| 2429 |
|
| 2430 |
if (a->argc != 4) { |
| 2431 |
return CLI_SHOWUSAGE; |
| 2432 |
} |
| 2433 |
|
| 2434 |
registration_name = a->argv[3]; |
| 2435 |
|
| 2436 |
if (strcmp(registration_name, "*all") == 0) { |
| 2437 |
unregister_all(); |
| 2438 |
ast_cli(a->fd, "Unregister all queued\n"); |
| 2439 |
return CLI_SUCCESS; |
| 2440 |
} |
| 2441 |
|
| 2442 |
state = get_state(registration_name); |
| 2443 |
if (!state) { |
| 2444 |
ast_cli(a->fd, "Unable to retrieve registration %s\n", registration_name); |
| 2445 |
return CLI_FAILURE; |
| 2446 |
} |
| 2447 |
|
| 2448 |
if (queue_unregister(state)) { |
| 2449 |
ast_cli(a->fd, "Failed to queue unregistration\n"); |
| 2450 |
} |
| 2451 |
|
| 2452 |
ao2_ref(state, -1); |
| 2453 |
return CLI_SUCCESS; |
| 2454 |
} |
| 2455 |
|
| 2456 |
static char *cli_register(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2457 |
{ |
| 2458 |
struct sip_outbound_registration_state *state; |
| 2459 |
const char *registration_name; |
| 2460 |
|
| 2461 |
switch (cmd) { |
| 2462 |
case CLI_INIT: |
| 2463 |
e->command = "pjsip send register"; |
| 2464 |
e->usage = |
| 2465 |
"Usage: pjsip send register <registration> | *all \n" |
| 2466 |
" Unregisters the specified (or all) outbound " |
| 2467 |
"registration(s) then starts registration(s) and schedules re-registrations.\n"; |
| 2468 |
return NULL; |
| 2469 |
case CLI_GENERATE: |
| 2470 |
return cli_complete_registration(a->line, a->word, a->pos, a->n); |
| 2471 |
} |
| 2472 |
|
| 2473 |
if (a->argc != 4) { |
| 2474 |
return CLI_SHOWUSAGE; |
| 2475 |
} |
| 2476 |
|
| 2477 |
registration_name = a->argv[3]; |
| 2478 |
|
| 2479 |
if (strcmp(registration_name, "*all") == 0) { |
| 2480 |
reregister_all(); |
| 2481 |
ast_cli(a->fd, "Re-register all queued\n"); |
| 2482 |
return CLI_SUCCESS; |
| 2483 |
} |
| 2484 |
|
| 2485 |
state = get_state(registration_name); |
| 2486 |
if (!state) { |
| 2487 |
ast_cli(a->fd, "Unable to retrieve registration %s\n", registration_name); |
| 2488 |
return CLI_FAILURE; |
| 2489 |
} |
| 2490 |
|
| 2491 |
|
| 2492 |
|
| 2493 |
|
| 2494 |
if (queue_unregister(state)) { |
| 2495 |
ast_cli(a->fd, "Failed to queue unregistration\n"); |
| 2496 |
} else if (queue_register(state)) { |
| 2497 |
ast_cli(a->fd, "Failed to queue registration\n"); |
| 2498 |
} |
| 2499 |
|
| 2500 |
ao2_ref(state, -1); |
| 2501 |
return CLI_SUCCESS; |
| 2502 |
} |
| 2503 |
|
| 2504 |
static int ami_unregister(struct mansession *s, const struct message *m) |
| 2505 |
{ |
| 2506 |
const char *registration_name = astman_get_header(m, "Registration"); |
| 2507 |
struct sip_outbound_registration_state *state; |
| 2508 |
|
| 2509 |
if (ast_strlen_zero(registration_name)) { |
| 2510 |
astman_send_error(s, m, "Registration parameter missing."); |
| 2511 |
return 0; |
| 2512 |
} |
| 2513 |
|
| 2514 |
if (strcmp(registration_name, "*all") == 0) { |
| 2515 |
unregister_all(); |
| 2516 |
astman_send_ack(s, m, "Unregistrations queued."); |
| 2517 |
return 0; |
| 2518 |
} |
| 2519 |
|
| 2520 |
state = get_state(registration_name); |
| 2521 |
if (!state) { |
| 2522 |
astman_send_error(s, m, "Unable to retrieve registration entry\n"); |
| 2523 |
return 0; |
| 2524 |
} |
| 2525 |
|
| 2526 |
if (queue_unregister(state)) { |
| 2527 |
astman_send_ack(s, m, "Failed to queue unregistration"); |
| 2528 |
} else { |
| 2529 |
astman_send_ack(s, m, "Unregistration sent"); |
| 2530 |
} |
| 2531 |
|
| 2532 |
ao2_ref(state, -1); |
| 2533 |
return 0; |
| 2534 |
} |
| 2535 |
|
| 2536 |
static int ami_register(struct mansession *s, const struct message *m) |
| 2537 |
{ |
| 2538 |
const char *registration_name = astman_get_header(m, "Registration"); |
| 2539 |
struct sip_outbound_registration_state *state; |
| 2540 |
|
| 2541 |
if (ast_strlen_zero(registration_name)) { |
| 2542 |
astman_send_error(s, m, "Registration parameter missing."); |
| 2543 |
return 0; |
| 2544 |
} |
| 2545 |
|
| 2546 |
if (strcmp(registration_name, "*all") == 0) { |
| 2547 |
reregister_all(); |
| 2548 |
astman_send_ack(s, m, "Reregistrations queued."); |
| 2549 |
return 0; |
| 2550 |
} |
| 2551 |
|
| 2552 |
state = get_state(registration_name); |
| 2553 |
if (!state) { |
| 2554 |
astman_send_error(s, m, "Unable to retrieve registration entry\n"); |
| 2555 |
return 0; |
| 2556 |
} |
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
if (queue_unregister(state)) { |
| 2562 |
astman_send_ack(s, m, "Failed to queue unregistration"); |
| 2563 |
} else if (queue_register(state)) { |
| 2564 |
astman_send_ack(s, m, "Failed to queue unregistration"); |
| 2565 |
} else { |
| 2566 |
astman_send_ack(s, m, "Reregistration sent"); |
| 2567 |
} |
| 2568 |
|
| 2569 |
ao2_ref(state, -1); |
| 2570 |
return 0; |
| 2571 |
} |
| 2572 |
|
| 2573 |
struct sip_ami_outbound { |
| 2574 |
struct ast_sip_ami *ami; |
| 2575 |
int registered; |
| 2576 |
int not_registered; |
| 2577 |
struct sip_outbound_registration *registration; |
| 2578 |
}; |
| 2579 |
|
| 2580 |
static int ami_outbound_registration_task(void *obj) |
| 2581 |
{ |
| 2582 |
struct sip_ami_outbound *ami = obj; |
| 2583 |
struct ast_str *buf; |
| 2584 |
struct sip_outbound_registration_state *state; |
| 2585 |
|
| 2586 |
buf = ast_sip_create_ami_event("OutboundRegistrationDetail", ami->ami); |
| 2587 |
if (!buf) { |
| 2588 |
return -1; |
| 2589 |
} |
| 2590 |
|
| 2591 |
ast_sip_sorcery_object_to_ami(ami->registration, &buf); |
| 2592 |
|
| 2593 |
if ((state = get_state(ast_sorcery_object_get_id(ami->registration)))) { |
| 2594 |
pjsip_regc_info info; |
| 2595 |
|
| 2596 |
if (state->client_state->status == SIP_REGISTRATION_REGISTERED) { |
| 2597 |
++ami->registered; |
| 2598 |
} else { |
| 2599 |
++ami->not_registered; |
| 2600 |
} |
| 2601 |
|
| 2602 |
ast_str_append(&buf, 0, "Status: %s\r\n", |
| 2603 |
sip_outbound_registration_status_str(state->client_state->status)); |
| 2604 |
|
| 2605 |
pjsip_regc_get_info(state->client_state->client, &info); |
| 2606 |
ast_str_append(&buf, 0, "NextReg: %d\r\n", info.next_reg); |
| 2607 |
ao2_ref(state, -1); |
| 2608 |
} |
| 2609 |
|
| 2610 |
astman_append(ami->ami->s, "%s\r\n", ast_str_buffer(buf)); |
| 2611 |
ast_free(buf); |
| 2612 |
|
| 2613 |
return ast_sip_format_auths_ami(&ami->registration->outbound_auths, ami->ami); |
| 2614 |
} |
| 2615 |
|
| 2616 |
static int ami_outbound_registration_detail(void *obj, void *arg, int flags) |
| 2617 |
{ |
| 2618 |
struct sip_ami_outbound *ami = arg; |
| 2619 |
|
| 2620 |
ami->registration = obj; |
| 2621 |
return ast_sip_push_task_wait_servant(NULL, ami_outbound_registration_task, ami); |
| 2622 |
} |
| 2623 |
|
| 2624 |
static int ami_show_outbound_registrations(struct mansession *s, |
| 2625 |
const struct message *m) |
| 2626 |
{ |
| 2627 |
struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), }; |
| 2628 |
struct sip_ami_outbound ami_outbound = { .ami = &ami }; |
| 2629 |
struct ao2_container *regs; |
| 2630 |
|
| 2631 |
regs = get_registrations(); |
| 2632 |
if (!regs) { |
| 2633 |
astman_send_error(s, m, "Unable to retrieve " |
| 2634 |
"outbound registrations\n"); |
| 2635 |
return -1; |
| 2636 |
} |
| 2637 |
|
| 2638 |
astman_send_listack(s, m, "Following are Events for each Outbound registration", |
| 2639 |
"start"); |
| 2640 |
|
| 2641 |
ao2_callback(regs, OBJ_NODATA, ami_outbound_registration_detail, &ami_outbound); |
| 2642 |
|
| 2643 |
astman_send_list_complete_start(s, m, "OutboundRegistrationDetailComplete", |
| 2644 |
ami_outbound.registered + ami_outbound.not_registered); |
| 2645 |
astman_append(s, |
| 2646 |
"Registered: %d\r\n" |
| 2647 |
"NotRegistered: %d\r\n", |
| 2648 |
ami_outbound.registered, |
| 2649 |
ami_outbound.not_registered); |
| 2650 |
astman_send_list_complete_end(s); |
| 2651 |
|
| 2652 |
ao2_ref(regs, -1); |
| 2653 |
return 0; |
| 2654 |
} |
| 2655 |
|
| 2656 |
static struct ao2_container *cli_get_container(const char *regex) |
| 2657 |
{ |
| 2658 |
RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup); |
| 2659 |
struct ao2_container *s_container; |
| 2660 |
|
| 2661 |
container = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "registration", regex); |
| 2662 |
if (!container) { |
| 2663 |
return NULL; |
| 2664 |
} |
| 2665 |
|
| 2666 |
s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 2667 |
ast_sorcery_object_id_sort, ast_sorcery_object_id_compare); |
| 2668 |
if (!s_container) { |
| 2669 |
return NULL; |
| 2670 |
} |
| 2671 |
|
| 2672 |
if (ao2_container_dup(s_container, container, 0)) { |
| 2673 |
ao2_ref(s_container, -1); |
| 2674 |
return NULL; |
| 2675 |
} |
| 2676 |
|
| 2677 |
return s_container; |
| 2678 |
} |
| 2679 |
|
| 2680 |
static int cli_iterator(void *container, ao2_callback_fn callback, void *args) |
| 2681 |
{ |
| 2682 |
ao2_callback(container, OBJ_NODATA, callback, args); |
| 2683 |
|
| 2684 |
return 0; |
| 2685 |
} |
| 2686 |
|
| 2687 |
static void *cli_retrieve_by_id(const char *id) |
| 2688 |
{ |
| 2689 |
struct ao2_container *states; |
| 2690 |
void *obj = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "registration", id); |
| 2691 |
|
| 2692 |
if (!obj) { |
| 2693 |
|
| 2694 |
states = ao2_global_obj_ref(current_states); |
| 2695 |
if (states) { |
| 2696 |
ao2_find(states, id, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA); |
| 2697 |
ao2_ref(states, -1); |
| 2698 |
} |
| 2699 |
} |
| 2700 |
|
| 2701 |
return obj; |
| 2702 |
} |
| 2703 |
|
| 2704 |
static int cli_print_header(void *obj, void *arg, int flags) |
| 2705 |
{ |
| 2706 |
struct ast_sip_cli_context *context = arg; |
| 2707 |
|
| 2708 |
ast_assert(context->output_buffer != NULL); |
| 2709 |
|
| 2710 |
ast_str_append(&context->output_buffer, 0, |
| 2711 |
" <Registration/ServerURI..............................> <Auth....................> <Status.......>\n"); |
| 2712 |
|
| 2713 |
return 0; |
| 2714 |
} |
| 2715 |
|
| 2716 |
static int cli_print_body(void *obj, void *arg, int flags) |
| 2717 |
{ |
| 2718 |
struct sip_outbound_registration *registration = obj; |
| 2719 |
struct ast_sip_cli_context *context = arg; |
| 2720 |
const char *id = ast_sorcery_object_get_id(registration); |
| 2721 |
struct sip_outbound_registration_state *state = get_state(id); |
| 2722 |
int expsecs; |
| 2723 |
#define REGISTRATION_URI_FIELD_LEN 53 |
| 2724 |
|
| 2725 |
ast_assert(context->output_buffer != NULL); |
| 2726 |
expsecs = state ? state->client_state->registration_expires - ((int) time(NULL)) : 0; |
| 2727 |
|
| 2728 |
ast_str_append(&context->output_buffer, 0, " %-s/%-*.*s %-26s %-16s %s%d%s\n", |
| 2729 |
id, |
| 2730 |
(int) (REGISTRATION_URI_FIELD_LEN - strlen(id)), |
| 2731 |
(int) (REGISTRATION_URI_FIELD_LEN - strlen(id)), |
| 2732 |
registration->server_uri, |
| 2733 |
AST_VECTOR_SIZE(®istration->outbound_auths) |
| 2734 |
? AST_VECTOR_GET(®istration->outbound_auths, 0) |
| 2735 |
: "n/a", |
| 2736 |
(state ? sip_outbound_registration_status_str(state->client_state->status) : "Unregistered"), |
| 2737 |
state ? " (exp. " : "", abs(expsecs), state ? (expsecs < 0 ? "s ago)" : "s)") : ""); |
| 2738 |
ao2_cleanup(state); |
| 2739 |
|
| 2740 |
if (context->show_details |
| 2741 |
|| (context->show_details_only_level_0 && context->indent_level == 0)) { |
| 2742 |
ast_str_append(&context->output_buffer, 0, "\n"); |
| 2743 |
ast_sip_cli_print_sorcery_objectset(registration, context, 0); |
| 2744 |
} |
| 2745 |
|
| 2746 |
return 0; |
| 2747 |
} |
| 2748 |
|
| 2749 |
|
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
static char *my_cli_traverse_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2755 |
{ |
| 2756 |
return ast_sip_cli_traverse_objects(e, cmd, a); |
| 2757 |
} |
| 2758 |
|
| 2759 |
static struct ast_cli_entry cli_outbound_registration[] = { |
| 2760 |
AST_CLI_DEFINE(cli_unregister, "Unregisters outbound registration target"), |
| 2761 |
AST_CLI_DEFINE(cli_register, "Registers an outbound registration target"), |
| 2762 |
AST_CLI_DEFINE(my_cli_traverse_objects, "List PJSIP Registrations", |
| 2763 |
.command = "pjsip list registrations", |
| 2764 |
.usage = "Usage: pjsip list registrations [ like <pattern> ]\n" |
| 2765 |
" List the configured PJSIP Registrations\n" |
| 2766 |
" Optional regular expression pattern is used to filter the list.\n"), |
| 2767 |
AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Registrations", |
| 2768 |
.command = "pjsip show registrations", |
| 2769 |
.usage = "Usage: pjsip show registrations [ like <pattern> ]\n" |
| 2770 |
" Show the configured PJSIP Registrations\n" |
| 2771 |
" Optional regular expression pattern is used to filter the list.\n"), |
| 2772 |
AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Registration", |
| 2773 |
.command = "pjsip show registration", |
| 2774 |
.usage = "Usage: pjsip show registration <id>\n" |
| 2775 |
" Show the configured PJSIP Registration\n"), |
| 2776 |
}; |
| 2777 |
|
| 2778 |
static struct ast_sip_cli_formatter_entry *cli_formatter; |
| 2779 |
|
| 2780 |
static void auth_observer(const char *type) |
| 2781 |
{ |
| 2782 |
struct sip_outbound_registration *registration; |
| 2783 |
struct sip_outbound_registration_state *state; |
| 2784 |
struct ao2_container *regs; |
| 2785 |
const char *registration_id; |
| 2786 |
struct ao2_iterator i; |
| 2787 |
|
| 2788 |
ast_debug(4, "Auths updated. Checking for any outbound registrations that are in permanent rejected state so they can be retried\n"); |
| 2789 |
|
| 2790 |
regs = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration", |
| 2791 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 2792 |
if (!regs || ao2_container_count(regs) == 0) { |
| 2793 |
ao2_cleanup(regs); |
| 2794 |
return; |
| 2795 |
} |
| 2796 |
|
| 2797 |
i = ao2_iterator_init(regs, 0); |
| 2798 |
for (; (registration = ao2_iterator_next(&i)); ao2_ref(registration, -1)) { |
| 2799 |
registration_id = ast_sorcery_object_get_id(registration); |
| 2800 |
state = get_state(registration_id); |
| 2801 |
if (state && state->client_state->status == SIP_REGISTRATION_REJECTED_PERMANENT) { |
| 2802 |
ast_debug(4, "Trying outbound registration '%s' again\n", registration_id); |
| 2803 |
|
| 2804 |
if (ast_sip_push_task(state->client_state->serializer, |
| 2805 |
sip_outbound_registration_perform, ao2_bump(state))) { |
| 2806 |
ast_log(LOG_ERROR, "Failed to perform outbound registration on '%s'\n", registration_id); |
| 2807 |
ao2_ref(state, -1); |
| 2808 |
} |
| 2809 |
} |
| 2810 |
ao2_cleanup(state); |
| 2811 |
} |
| 2812 |
ao2_iterator_destroy(&i); |
| 2813 |
ao2_cleanup(regs); |
| 2814 |
} |
| 2815 |
|
| 2816 |
static const struct ast_sorcery_observer observer_callbacks_auth = { |
| 2817 |
.loaded = auth_observer, |
| 2818 |
}; |
| 2819 |
|
| 2820 |
static int check_state(void *obj, void *arg, int flags) |
| 2821 |
{ |
| 2822 |
struct sip_outbound_registration_state *state = obj; |
| 2823 |
struct sip_outbound_registration *registration; |
| 2824 |
|
| 2825 |
registration = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "registration", |
| 2826 |
ast_sorcery_object_get_id(state->registration)); |
| 2827 |
if (!registration) { |
| 2828 |
|
| 2829 |
return CMP_MATCH; |
| 2830 |
} |
| 2831 |
|
| 2832 |
ao2_ref(registration, -1); |
| 2833 |
return 0; |
| 2834 |
} |
| 2835 |
|
| 2836 |
|
| 2837 |
|
| 2838 |
|
| 2839 |
|
| 2840 |
|
| 2841 |
|
| 2842 |
|
| 2843 |
|
| 2844 |
|
| 2845 |
static void registration_loaded_observer(const char *name, const struct ast_sorcery *sorcery, const char *object_type, int reloaded) |
| 2846 |
{ |
| 2847 |
struct ao2_container *states; |
| 2848 |
|
| 2849 |
if (strcmp(object_type, "registration")) { |
| 2850 |
|
| 2851 |
return; |
| 2852 |
} |
| 2853 |
|
| 2854 |
states = ao2_global_obj_ref(current_states); |
| 2855 |
if (!states) { |
| 2856 |
|
| 2857 |
return; |
| 2858 |
} |
| 2859 |
|
| 2860 |
|
| 2861 |
|
| 2862 |
|
| 2863 |
|
| 2864 |
|
| 2865 |
ao2_cleanup(get_registrations()); |
| 2866 |
|
| 2867 |
|
| 2868 |
ao2_callback(states, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, check_state, NULL); |
| 2869 |
ao2_ref(states, -1); |
| 2870 |
} |
| 2871 |
|
| 2872 |
static const struct ast_sorcery_instance_observer observer_callbacks_registrations = { |
| 2873 |
.object_type_loaded = registration_loaded_observer, |
| 2874 |
}; |
| 2875 |
|
| 2876 |
static void registration_deleted_observer(const void *obj) |
| 2877 |
{ |
| 2878 |
const struct sip_outbound_registration *registration = obj; |
| 2879 |
struct ao2_container *states; |
| 2880 |
|
| 2881 |
states = ao2_global_obj_ref(current_states); |
| 2882 |
if (!states) { |
| 2883 |
|
| 2884 |
return; |
| 2885 |
} |
| 2886 |
|
| 2887 |
ao2_find(states, ast_sorcery_object_get_id(registration), OBJ_UNLINK | OBJ_NODATA | OBJ_SEARCH_KEY); |
| 2888 |
|
| 2889 |
ao2_ref(states, -1); |
| 2890 |
} |
| 2891 |
|
| 2892 |
static const struct ast_sorcery_observer registration_observer = { |
| 2893 |
.deleted = registration_deleted_observer, |
| 2894 |
}; |
| 2895 |
|
| 2896 |
static void network_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 2897 |
{ |
| 2898 |
|
| 2899 |
if (stasis_message_type(message) != ast_network_change_type()) { |
| 2900 |
return; |
| 2901 |
} |
| 2902 |
ast_debug(3, "Received network change event\n"); |
| 2903 |
|
| 2904 |
reregister_all(); |
| 2905 |
} |
| 2906 |
|
| 2907 |
static int unload_module(void) |
| 2908 |
{ |
| 2909 |
int remaining; |
| 2910 |
|
| 2911 |
network_change_sub = stasis_unsubscribe_and_join(network_change_sub); |
| 2912 |
|
| 2913 |
ast_manager_unregister("PJSIPShowRegistrationsOutbound"); |
| 2914 |
ast_manager_unregister("PJSIPUnregister"); |
| 2915 |
ast_manager_unregister("PJSIPRegister"); |
| 2916 |
|
| 2917 |
ast_cli_unregister_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration)); |
| 2918 |
ast_sip_unregister_cli_formatter(cli_formatter); |
| 2919 |
cli_formatter = NULL; |
| 2920 |
|
| 2921 |
ast_sip_unregister_endpoint_identifier(&line_identifier); |
| 2922 |
|
| 2923 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "auth", &observer_callbacks_auth); |
| 2924 |
ast_sorcery_instance_observer_remove(ast_sip_get_sorcery(), &observer_callbacks_registrations); |
| 2925 |
|
| 2926 |
ast_sorcery_object_unregister(ast_sip_get_sorcery(), "registration"); |
| 2927 |
|
| 2928 |
ao2_global_obj_release(current_states); |
| 2929 |
|
| 2930 |
ast_sip_transport_monitor_unregister_all(registration_transport_shutdown_cb, NULL, NULL); |
| 2931 |
|
| 2932 |
|
| 2933 |
ast_debug(2, "Waiting for registration transactions to complete for unload.\n"); |
| 2934 |
remaining = ast_serializer_shutdown_group_join(shutdown_group, MAX_UNLOAD_TIMEOUT_TIME); |
| 2935 |
if (remaining) { |
| 2936 |
|
| 2937 |
|
| 2938 |
|
| 2939 |
|
| 2940 |
|
| 2941 |
ast_log(LOG_WARNING, "Unload incomplete. Could not stop %d outbound registrations. Try again later.\n", |
| 2942 |
remaining); |
| 2943 |
return -1; |
| 2944 |
} |
| 2945 |
|
| 2946 |
ast_debug(2, "Successful shutdown.\n"); |
| 2947 |
|
| 2948 |
ao2_cleanup(shutdown_group); |
| 2949 |
shutdown_group = NULL; |
| 2950 |
|
| 2951 |
return 0; |
| 2952 |
} |
| 2953 |
|
| 2954 |
static int load_module(void) |
| 2955 |
{ |
| 2956 |
struct ao2_container *new_states; |
| 2957 |
|
| 2958 |
shutdown_group = ast_serializer_shutdown_group_alloc(); |
| 2959 |
if (!shutdown_group) { |
| 2960 |
return AST_MODULE_LOAD_DECLINE; |
| 2961 |
} |
| 2962 |
|
| 2963 |
|
| 2964 |
|
| 2965 |
|
| 2966 |
|
| 2967 |
ast_cli_register_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration)); |
| 2968 |
|
| 2969 |
|
| 2970 |
new_states = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 2971 |
DEFAULT_STATE_BUCKETS, registration_state_hash, NULL, registration_state_cmp); |
| 2972 |
if (!new_states) { |
| 2973 |
ast_log(LOG_ERROR, "Unable to allocate registration states container\n"); |
| 2974 |
unload_module(); |
| 2975 |
return AST_MODULE_LOAD_DECLINE; |
| 2976 |
} |
| 2977 |
ao2_global_obj_replace_unref(current_states, new_states); |
| 2978 |
ao2_ref(new_states, -1); |
| 2979 |
|
| 2980 |
|
| 2981 |
|
| 2982 |
|
| 2983 |
ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_outbound_registration"); |
| 2984 |
ast_sorcery_apply_default(ast_sip_get_sorcery(), "registration", "config", "pjsip.conf,criteria=type=registration"); |
| 2985 |
|
| 2986 |
if (ast_sorcery_object_register(ast_sip_get_sorcery(), "registration", sip_outbound_registration_alloc, NULL, sip_outbound_registration_apply)) { |
| 2987 |
unload_module(); |
| 2988 |
return AST_MODULE_LOAD_DECLINE; |
| 2989 |
} |
| 2990 |
|
| 2991 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "type", "", OPT_NOOP_T, 0, 0); |
| 2992 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "server_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, server_uri)); |
| 2993 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "client_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, client_uri)); |
| 2994 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "contact_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, contact_user)); |
| 2995 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "contact_header_params", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, contact_header_params)); |
| 2996 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, transport)); |
| 2997 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, outbound_proxy)); |
| 2998 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "expiration", "3600", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, expiration)); |
| 2999 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "max_random_initial_delay", "10", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, max_random_initial_delay)); |
| 3000 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "retry_interval", "60", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, retry_interval)); |
| 3001 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "forbidden_retry_interval", "0", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, forbidden_retry_interval)); |
| 3002 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "fatal_retry_interval", "0", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, fatal_retry_interval)); |
| 3003 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "max_retries", "10", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, max_retries)); |
| 3004 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "auth_rejection_permanent", "yes", OPT_BOOL_T, 1, FLDSET(struct sip_outbound_registration, auth_rejection_permanent)); |
| 3005 |
ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "registration", "outbound_auth", "", outbound_auth_handler, outbound_auths_to_str, outbound_auths_to_var_list, 0, 0); |
| 3006 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "support_path", "no", OPT_BOOL_T, 1, FLDSET(struct sip_outbound_registration, support_path)); |
| 3007 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "support_outbound", "no", OPT_YESNO_T, 1, FLDSET(struct sip_outbound_registration, support_outbound)); |
| 3008 |
ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "registration", "security_negotiation", "no", security_negotiation_handler, security_negotiation_to_str, NULL, 0, 0); |
| 3009 |
ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "registration", "security_mechanisms", "", security_mechanisms_handler, security_mechanism_to_str, NULL, 0, 0); |
| 3010 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "line", "no", OPT_BOOL_T, 1, FLDSET(struct sip_outbound_registration, line)); |
| 3011 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "endpoint", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, endpoint)); |
| 3012 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "user_agent", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, user_agent)); |
| 3013 |
|
| 3014 |
|
| 3015 |
|
| 3016 |
|
| 3017 |
if (ast_sorcery_instance_observer_add(ast_sip_get_sorcery(), |
| 3018 |
&observer_callbacks_registrations) |
| 3019 |
|| ast_sorcery_observer_add(ast_sip_get_sorcery(), "auth", |
| 3020 |
&observer_callbacks_auth) |
| 3021 |
|| ast_sorcery_observer_add(ast_sip_get_sorcery(), "registration", |
| 3022 |
®istration_observer)) { |
| 3023 |
ast_log(LOG_ERROR, "Unable to register observers.\n"); |
| 3024 |
unload_module(); |
| 3025 |
return AST_MODULE_LOAD_DECLINE; |
| 3026 |
} |
| 3027 |
|
| 3028 |
|
| 3029 |
ast_sip_register_endpoint_identifier(&line_identifier); |
| 3030 |
|
| 3031 |
|
| 3032 |
cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL); |
| 3033 |
if (!cli_formatter) { |
| 3034 |
ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n"); |
| 3035 |
unload_module(); |
| 3036 |
return AST_MODULE_LOAD_DECLINE; |
| 3037 |
} |
| 3038 |
cli_formatter->name = "registration"; |
| 3039 |
cli_formatter->print_header = cli_print_header; |
| 3040 |
cli_formatter->print_body = cli_print_body; |
| 3041 |
cli_formatter->get_container = cli_get_container; |
| 3042 |
cli_formatter->iterate = cli_iterator; |
| 3043 |
cli_formatter->get_id = ast_sorcery_object_get_id; |
| 3044 |
cli_formatter->retrieve_by_id = cli_retrieve_by_id; |
| 3045 |
ast_sip_register_cli_formatter(cli_formatter); |
| 3046 |
|
| 3047 |
|
| 3048 |
ast_manager_register_xml("PJSIPUnregister", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_unregister); |
| 3049 |
ast_manager_register_xml("PJSIPRegister", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_register); |
| 3050 |
ast_manager_register_xml("PJSIPShowRegistrationsOutbound", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_show_outbound_registrations); |
| 3051 |
|
| 3052 |
|
| 3053 |
ast_statsd_log("PJSIP.registrations.count", AST_STATSD_GAUGE, 0); |
| 3054 |
ast_statsd_log("PJSIP.registrations.state.Registered", AST_STATSD_GAUGE, 0); |
| 3055 |
ast_statsd_log("PJSIP.registrations.state.Unregistered", AST_STATSD_GAUGE, 0); |
| 3056 |
ast_statsd_log("PJSIP.registrations.state.Rejected", AST_STATSD_GAUGE, 0); |
| 3057 |
|
| 3058 |
|
| 3059 |
ast_sorcery_load_object(ast_sip_get_sorcery(), "registration"); |
| 3060 |
|
| 3061 |
network_change_sub = stasis_subscribe(ast_system_topic(), |
| 3062 |
network_change_stasis_cb, NULL); |
| 3063 |
stasis_subscription_accept_message_type(network_change_sub, ast_network_change_type()); |
| 3064 |
stasis_subscription_set_filter(network_change_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 3065 |
|
| 3066 |
return AST_MODULE_LOAD_SUCCESS; |
| 3067 |
} |
| 3068 |
|
| 3069 |
static int reload_module(void) |
| 3070 |
{ |
| 3071 |
ast_sorcery_reload_object(ast_sip_get_sorcery(), "registration"); |
| 3072 |
return 0; |
| 3073 |
} |
| 3074 |
|
| 3075 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Outbound Registration Support", |
| 3076 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 3077 |
.load = load_module, |
| 3078 |
.reload = reload_module, |
| 3079 |
.unload = unload_module, |
| 3080 |
.load_pri = AST_MODPRI_APP_DEPEND, |
| 3081 |
.requires = "res_pjsip", |
| 3082 |
.optional_modules = "res_statsd", |
| 3083 |
); |