| 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 <signal.h> |
| 29 |
#include <pjsip.h> |
| 30 |
#include <pjsip_ua.h> |
| 31 |
|
| 32 |
#include "asterisk/res_pjsip.h" |
| 33 |
#include "asterisk/module.h" |
| 34 |
#include "asterisk/paths.h" |
| 35 |
#include "asterisk/test.h" |
| 36 |
#include "asterisk/taskprocessor.h" |
| 37 |
#include "asterisk/manager.h" |
| 38 |
#include "asterisk/named_locks.h" |
| 39 |
#include "asterisk/res_pjproject.h" |
| 40 |
#include "res_pjsip/include/res_pjsip_private.h" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
static int pj_max_hostname = PJ_MAX_HOSTNAME; |
| 153 |
static int pjsip_max_url_size = PJSIP_MAX_URL_SIZE; |
| 154 |
|
| 155 |
|
| 156 |
static unsigned int registrar_get_expiration(const struct ast_sip_aor *aor, const pjsip_contact_hdr *contact, const pjsip_rx_data *rdata) |
| 157 |
{ |
| 158 |
pjsip_expires_hdr *expires; |
| 159 |
unsigned int expiration = aor->default_expiration; |
| 160 |
|
| 161 |
if (contact && contact->expires != PJSIP_EXPIRES_NOT_SPECIFIED) { |
| 162 |
|
| 163 |
expiration = contact->expires; |
| 164 |
} else if ((expires = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL))) { |
| 165 |
|
| 166 |
expiration = expires->ivalue; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
if (!expiration) { |
| 171 |
return expiration; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
if (expiration < aor->minimum_expiration) { |
| 176 |
expiration = aor->minimum_expiration; |
| 177 |
} else if (expiration > aor->maximum_expiration) { |
| 178 |
expiration = aor->maximum_expiration; |
| 179 |
} |
| 180 |
|
| 181 |
return expiration; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
struct registrar_contact_details { |
| 186 |
|
| 187 |
pj_pool_t *pool; |
| 188 |
|
| 189 |
pjsip_sip_uri *uri; |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
static int registrar_find_contact(void *obj, void *arg, int flags) |
| 194 |
{ |
| 195 |
struct ast_sip_contact *contact = obj; |
| 196 |
const struct registrar_contact_details *details = arg; |
| 197 |
pjsip_uri *contact_uri; |
| 198 |
|
| 199 |
if (ast_tvzero(contact->expiration_time)) { |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
contact_uri = pjsip_parse_uri(details->pool, (char*)contact->uri, strlen(contact->uri), 0); |
| 204 |
if (!contact_uri) { |
| 205 |
ast_log(LOG_WARNING, "Unable to parse contact URI from '%s'.\n", contact->uri); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
return (pjsip_uri_cmp(PJSIP_URI_IN_CONTACT_HDR, details->uri, contact_uri) == PJ_SUCCESS) ? CMP_MATCH : 0; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
static int registrar_validate_contacts(const pjsip_rx_data *rdata, pj_pool_t *pool, struct ao2_container *contacts, |
| 214 |
struct ast_sip_aor *aor, int permanent, int *added, int *updated, int *deleted) |
| 215 |
{ |
| 216 |
pjsip_contact_hdr *previous = NULL; |
| 217 |
pjsip_contact_hdr *contact = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr; |
| 218 |
struct registrar_contact_details details = { |
| 219 |
.pool = pool, |
| 220 |
}; |
| 221 |
|
| 222 |
for (; (contact = (pjsip_contact_hdr *) pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next)); pj_pool_reset(pool)) { |
| 223 |
unsigned int expiration = registrar_get_expiration(aor, contact, rdata); |
| 224 |
struct ast_sip_contact *existing; |
| 225 |
char contact_uri[pjsip_max_url_size]; |
| 226 |
|
| 227 |
if (contact->star) { |
| 228 |
|
| 229 |
if (expiration != 0 || previous) { |
| 230 |
return -1; |
| 231 |
} |
| 232 |
|
| 233 |
*deleted = ao2_container_count(contacts) - permanent; |
| 234 |
previous = contact; |
| 235 |
continue; |
| 236 |
} else if (previous && previous->star) { |
| 237 |
|
| 238 |
return -1; |
| 239 |
} |
| 240 |
previous = contact; |
| 241 |
|
| 242 |
if (!PJSIP_URI_SCHEME_IS_SIP(contact->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact->uri)) { |
| 243 |
continue; |
| 244 |
} |
| 245 |
|
| 246 |
details.uri = pjsip_uri_get_uri(contact->uri); |
| 247 |
|
| 248 |
|
| 249 |
if (pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, details.uri, contact_uri, sizeof(contact_uri)) < 0) { |
| 250 |
|
| 251 |
return -1; |
| 252 |
} |
| 253 |
|
| 254 |
if (details.uri->host.slen >= pj_max_hostname) { |
| 255 |
|
| 256 |
return -1; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
existing = ao2_callback(contacts, 0, registrar_find_contact, &details); |
| 261 |
ao2_cleanup(existing); |
| 262 |
if (!existing) { |
| 263 |
if (expiration) { |
| 264 |
++*added; |
| 265 |
} |
| 266 |
} else if (expiration) { |
| 267 |
++*updated; |
| 268 |
} else { |
| 269 |
++*deleted; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
enum contact_delete_type { |
| 277 |
CONTACT_DELETE_ERROR, |
| 278 |
CONTACT_DELETE_EXISTING, |
| 279 |
CONTACT_DELETE_UNAVAILABLE, |
| 280 |
CONTACT_DELETE_EXPIRE, |
| 281 |
CONTACT_DELETE_REQUEST, |
| 282 |
CONTACT_DELETE_SHUTDOWN, |
| 283 |
}; |
| 284 |
|
| 285 |
static int registrar_contact_delete(enum contact_delete_type type, pjsip_transport *transport, |
| 286 |
struct ast_sip_contact *contact, const char *aor_name); |
| 287 |
|
| 288 |
|
| 289 |
static int registrar_delete_contact(void *obj, void *arg, int flags) |
| 290 |
{ |
| 291 |
return registrar_contact_delete( |
| 292 |
CONTACT_DELETE_REQUEST, NULL, obj, arg) ? 0 : CMP_MATCH; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
static int registrar_add_contact(void *obj, void *arg, int flags) |
| 297 |
{ |
| 298 |
struct ast_sip_contact *contact = obj; |
| 299 |
pjsip_tx_data *tdata = arg; |
| 300 |
pj_str_t uri; |
| 301 |
pjsip_uri *parsed; |
| 302 |
|
| 303 |
pj_strdup2_with_null(tdata->pool, &uri, contact->uri); |
| 304 |
parsed = pjsip_parse_uri(tdata->pool, uri.ptr, uri.slen, PJSIP_PARSE_URI_AS_NAMEADDR); |
| 305 |
|
| 306 |
if (parsed && (PJSIP_URI_SCHEME_IS_SIP(parsed) || PJSIP_URI_SCHEME_IS_SIPS(parsed))) { |
| 307 |
pjsip_contact_hdr *hdr = pjsip_contact_hdr_create(tdata->pool); |
| 308 |
hdr->uri = parsed; |
| 309 |
if (!ast_tvzero(contact->expiration_time)) { |
| 310 |
hdr->expires = ast_tvdiff_ms(contact->expiration_time, ast_tvnow()) / 1000; |
| 311 |
} else { |
| 312 |
hdr->expires = PJSIP_EXPIRES_NOT_SPECIFIED; |
| 313 |
} |
| 314 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) hdr); |
| 315 |
} else { |
| 316 |
ast_log(LOG_WARNING, "Skipping invalid Contact URI \"%.*s\" for AOR %s\n", |
| 317 |
(int) uri.slen, uri.ptr, contact->aor); |
| 318 |
} |
| 319 |
|
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
static const pj_str_t path_hdr_name = { "Path", 4 }; |
| 324 |
|
| 325 |
static int build_path_data(pjsip_rx_data *rdata, struct ast_str **path_str) |
| 326 |
{ |
| 327 |
pjsip_generic_string_hdr *path_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &path_hdr_name, NULL); |
| 328 |
|
| 329 |
if (!path_hdr) { |
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
*path_str = ast_str_create(64); |
| 334 |
if (!*path_str) { |
| 335 |
return -1; |
| 336 |
} |
| 337 |
|
| 338 |
ast_str_set(path_str, 0, "%.*s", (int)path_hdr->hvalue.slen, path_hdr->hvalue.ptr); |
| 339 |
|
| 340 |
while ((path_hdr = (pjsip_generic_string_hdr *) pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &path_hdr_name, path_hdr->next))) { |
| 341 |
ast_str_append(path_str, 0, ",%.*s", (int)path_hdr->hvalue.slen, path_hdr->hvalue.ptr); |
| 342 |
} |
| 343 |
|
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
static int registrar_validate_path(pjsip_rx_data *rdata, struct ast_sip_aor *aor, struct ast_str **path_str) |
| 348 |
{ |
| 349 |
const pj_str_t path_supported_name = { "path", 4 }; |
| 350 |
pjsip_supported_hdr *supported_hdr; |
| 351 |
int i; |
| 352 |
|
| 353 |
if (!aor->support_path) { |
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
if (build_path_data(rdata, path_str)) { |
| 358 |
return -1; |
| 359 |
} |
| 360 |
|
| 361 |
if (!*path_str) { |
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
supported_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_SUPPORTED, NULL); |
| 366 |
if (!supported_hdr) { |
| 367 |
return -1; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
for (i = 0; i < supported_hdr->count; i++) { |
| 372 |
if (!pj_stricmp(&supported_hdr->values[i], &path_supported_name)) { |
| 373 |
return 0; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
return -1; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
struct contact_transport_monitor { |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
char *contact_name; |
| 388 |
|
| 389 |
int removing; |
| 390 |
|
| 391 |
char aor_name[0]; |
| 392 |
}; |
| 393 |
|
| 394 |
static int contact_transport_monitor_matcher(void *a, void *b) |
| 395 |
{ |
| 396 |
struct contact_transport_monitor *ma = a; |
| 397 |
struct contact_transport_monitor *mb = b; |
| 398 |
|
| 399 |
return strcmp(ma->aor_name, mb->aor_name) == 0 |
| 400 |
&& strcmp(ma->contact_name, mb->contact_name) == 0; |
| 401 |
} |
| 402 |
|
| 403 |
static int register_contact_transport_remove_cb(void *data) |
| 404 |
{ |
| 405 |
struct contact_transport_monitor *monitor = data; |
| 406 |
struct ast_sip_contact *contact; |
| 407 |
struct ast_sip_aor *aor; |
| 408 |
|
| 409 |
aor = ast_sip_location_retrieve_aor(monitor->aor_name); |
| 410 |
if (!aor) { |
| 411 |
ao2_lock(monitor); |
| 412 |
monitor->removing = 0; |
| 413 |
ao2_unlock(monitor); |
| 414 |
ao2_ref(monitor, -1); |
| 415 |
return 0; |
| 416 |
} |
| 417 |
|
| 418 |
ao2_lock(aor); |
| 419 |
|
| 420 |
contact = ast_sip_location_retrieve_contact(monitor->contact_name); |
| 421 |
if (contact) { |
| 422 |
registrar_contact_delete(CONTACT_DELETE_SHUTDOWN, NULL, contact, monitor->aor_name); |
| 423 |
ao2_ref(contact, -1); |
| 424 |
} |
| 425 |
ao2_unlock(aor); |
| 426 |
ao2_ref(aor, -1); |
| 427 |
|
| 428 |
ao2_ref(monitor, -1); |
| 429 |
return 0; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
static void register_contact_transport_shutdown_cb(void *data) |
| 441 |
{ |
| 442 |
struct contact_transport_monitor *monitor = data; |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
ao2_lock(monitor); |
| 451 |
if (monitor->removing) { |
| 452 |
ao2_unlock(monitor); |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
monitor->removing = 1; |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
ao2_ref(monitor, +1); |
| 465 |
if (ast_sip_push_task(NULL, register_contact_transport_remove_cb, monitor)) { |
| 466 |
monitor->removing = 0; |
| 467 |
ao2_ref(monitor, -1); |
| 468 |
} |
| 469 |
|
| 470 |
ao2_unlock(monitor); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
static int registrar_contact_delete(enum contact_delete_type type, pjsip_transport *transport, |
| 475 |
struct ast_sip_contact *contact, const char *aor_name) |
| 476 |
{ |
| 477 |
int aor_size; |
| 478 |
|
| 479 |
|
| 480 |
if (ast_tvzero(contact->expiration_time)) { |
| 481 |
return -1; |
| 482 |
} |
| 483 |
|
| 484 |
aor_size = aor_name ? strlen(aor_name) : 0; |
| 485 |
if (contact->prune_on_boot && type != CONTACT_DELETE_SHUTDOWN && aor_size) { |
| 486 |
const char *contact_name = ast_sorcery_object_get_id(contact); |
| 487 |
size_t contact_name_len = strlen(contact_name) + 1; |
| 488 |
struct contact_transport_monitor *monitor = ast_alloca( |
| 489 |
sizeof(*monitor) + 1 + aor_size + contact_name_len); |
| 490 |
|
| 491 |
strcpy(monitor->aor_name, aor_name); |
| 492 |
monitor->contact_name = monitor->aor_name + aor_size + 1; |
| 493 |
ast_copy_string(monitor->contact_name, contact_name, contact_name_len); |
| 494 |
|
| 495 |
if (transport) { |
| 496 |
ast_sip_transport_monitor_unregister(transport, |
| 497 |
register_contact_transport_shutdown_cb, monitor, |
| 498 |
contact_transport_monitor_matcher); |
| 499 |
} else { |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
ast_sip_transport_monitor_unregister_all(register_contact_transport_shutdown_cb, |
| 505 |
monitor, contact_transport_monitor_matcher); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
ast_sip_location_delete_contact(contact); |
| 510 |
|
| 511 |
if (aor_size) { |
| 512 |
if (VERBOSITY_ATLEAST(3)) { |
| 513 |
const char *reason = "none"; |
| 514 |
|
| 515 |
switch (type) { |
| 516 |
case CONTACT_DELETE_ERROR: |
| 517 |
reason = "registration failure"; |
| 518 |
break; |
| 519 |
case CONTACT_DELETE_EXISTING: |
| 520 |
reason = "remove existing"; |
| 521 |
break; |
| 522 |
case CONTACT_DELETE_UNAVAILABLE: |
| 523 |
reason = "remove unavailable"; |
| 524 |
break; |
| 525 |
case CONTACT_DELETE_EXPIRE: |
| 526 |
reason = "expiration"; |
| 527 |
break; |
| 528 |
case CONTACT_DELETE_REQUEST: |
| 529 |
reason = "request"; |
| 530 |
break; |
| 531 |
case CONTACT_DELETE_SHUTDOWN: |
| 532 |
reason = "shutdown"; |
| 533 |
break; |
| 534 |
} |
| 535 |
|
| 536 |
ast_verb(3, "Removed contact '%s' from AOR '%s' due to %s\n", |
| 537 |
contact->uri, aor_name, reason); |
| 538 |
} |
| 539 |
|
| 540 |
ast_test_suite_event_notify("AOR_CONTACT_REMOVED", |
| 541 |
"Contact: %s\r\n" |
| 542 |
"AOR: %s\r\n" |
| 543 |
"UserAgent: %s", |
| 544 |
contact->uri, |
| 545 |
aor_name, |
| 546 |
contact->user_agent); |
| 547 |
} |
| 548 |
|
| 549 |
return 0; |
| 550 |
} |
| 551 |
|
| 552 |
AST_VECTOR(excess_contact_vector, struct ast_sip_contact *); |
| 553 |
|
| 554 |
static int vec_contact_cmp(struct ast_sip_contact *left, struct ast_sip_contact *right) |
| 555 |
{ |
| 556 |
struct ast_sip_contact *left_contact = left; |
| 557 |
struct ast_sip_contact *right_contact = right; |
| 558 |
|
| 559 |
|
| 560 |
int time_sorted = ast_tvcmp(left_contact->expiration_time, right_contact->expiration_time); |
| 561 |
|
| 562 |
struct ast_sip_aor *aor = ast_sip_location_retrieve_aor(left_contact->aor); |
| 563 |
struct ast_sip_contact_status *left_status; |
| 564 |
struct ast_sip_contact_status *right_status; |
| 565 |
int remove_unavailable = 0; |
| 566 |
int left_unreachable; |
| 567 |
int right_unreachable; |
| 568 |
|
| 569 |
if (aor) { |
| 570 |
remove_unavailable = aor->remove_unavailable; |
| 571 |
ao2_ref(aor, -1); |
| 572 |
} |
| 573 |
|
| 574 |
if (!remove_unavailable) { |
| 575 |
return time_sorted; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
left_status = ast_sip_get_contact_status(left_contact); |
| 580 |
if (!left_status) { |
| 581 |
return time_sorted; |
| 582 |
} |
| 583 |
|
| 584 |
right_status = ast_sip_get_contact_status(right_contact); |
| 585 |
if (!right_status) { |
| 586 |
ao2_ref(left_status, -1); |
| 587 |
return time_sorted; |
| 588 |
} |
| 589 |
|
| 590 |
left_unreachable = (left_status->status == UNAVAILABLE); |
| 591 |
right_unreachable = (right_status->status == UNAVAILABLE); |
| 592 |
ao2_ref(left_status, -1); |
| 593 |
ao2_ref(right_status, -1); |
| 594 |
if (left_unreachable != right_unreachable) { |
| 595 |
|
| 596 |
if (left_unreachable) return -1; |
| 597 |
if (right_unreachable) return 1; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
return time_sorted; |
| 602 |
} |
| 603 |
|
| 604 |
static int vec_contact_add(void *obj, void *arg, int flags) |
| 605 |
{ |
| 606 |
struct ast_sip_contact *contact = obj; |
| 607 |
struct excess_contact_vector *contact_vec = arg; |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
AST_VECTOR_ADD_SORTED(contact_vec, contact, vec_contact_cmp); |
| 615 |
if (AST_VECTOR_SIZE(contact_vec) == AST_VECTOR_MAX_SIZE(contact_vec)) { |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
--AST_VECTOR_SIZE(contact_vec); |
| 624 |
} |
| 625 |
return 0; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
static void remove_excess_contacts(struct ao2_container *contacts, struct ao2_container *response_contacts, |
| 638 |
unsigned int to_remove, unsigned int remove_existing) |
| 639 |
{ |
| 640 |
struct excess_contact_vector contact_vec; |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
if (AST_VECTOR_INIT(&contact_vec, to_remove + 1)) { |
| 649 |
return; |
| 650 |
} |
| 651 |
ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE, vec_contact_add, &contact_vec); |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
ast_assert(AST_VECTOR_SIZE(&contact_vec) == to_remove); |
| 660 |
to_remove = AST_VECTOR_SIZE(&contact_vec); |
| 661 |
|
| 662 |
|
| 663 |
while (to_remove--) { |
| 664 |
struct ast_sip_contact *contact; |
| 665 |
|
| 666 |
contact = AST_VECTOR_GET(&contact_vec, to_remove); |
| 667 |
|
| 668 |
if (!remove_existing) { |
| 669 |
registrar_contact_delete(CONTACT_DELETE_UNAVAILABLE, NULL, contact, contact->aor); |
| 670 |
} else { |
| 671 |
registrar_contact_delete(CONTACT_DELETE_EXISTING, NULL, contact, contact->aor); |
| 672 |
} |
| 673 |
|
| 674 |
ao2_unlink(response_contacts, contact); |
| 675 |
} |
| 676 |
|
| 677 |
AST_VECTOR_FREE(&contact_vec); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
static int registrar_add_non_permanent(void *obj, void *arg, int flags) |
| 682 |
{ |
| 683 |
struct ast_sip_contact *contact = obj; |
| 684 |
struct ao2_container *container = arg; |
| 685 |
|
| 686 |
if (ast_tvzero(contact->expiration_time)) { |
| 687 |
return 0; |
| 688 |
} |
| 689 |
|
| 690 |
ao2_link(container, contact); |
| 691 |
|
| 692 |
return 0; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
static int registrar_add_unreachable(void *obj, void *arg, int flags) |
| 697 |
{ |
| 698 |
struct ast_sip_contact *contact = obj; |
| 699 |
struct ao2_container *container = arg; |
| 700 |
struct ast_sip_contact_status *status; |
| 701 |
int unreachable; |
| 702 |
|
| 703 |
status = ast_sip_get_contact_status(contact); |
| 704 |
if (!status) { |
| 705 |
return 0; |
| 706 |
} |
| 707 |
|
| 708 |
unreachable = (status->status == UNAVAILABLE); |
| 709 |
ao2_ref(status, -1); |
| 710 |
|
| 711 |
if (unreachable) { |
| 712 |
ao2_link(container, contact); |
| 713 |
} |
| 714 |
|
| 715 |
return 0; |
| 716 |
} |
| 717 |
|
| 718 |
struct aor_core_response { |
| 719 |
|
| 720 |
pjsip_tx_data *tdata; |
| 721 |
|
| 722 |
int code; |
| 723 |
}; |
| 724 |
|
| 725 |
static void register_aor_core(pjsip_rx_data *rdata, |
| 726 |
struct ast_sip_endpoint *endpoint, |
| 727 |
struct ast_sip_aor *aor, |
| 728 |
const char *aor_name, |
| 729 |
struct ao2_container *contacts, |
| 730 |
struct aor_core_response *response) |
| 731 |
{ |
| 732 |
static const pj_str_t USER_AGENT = { "User-Agent", 10 }; |
| 733 |
|
| 734 |
int added = 0; |
| 735 |
int updated = 0; |
| 736 |
int deleted = 0; |
| 737 |
int permanent = 0; |
| 738 |
int contact_count; |
| 739 |
struct ao2_container *existing_contacts = NULL; |
| 740 |
struct ao2_container *unavail_contacts = NULL; |
| 741 |
pjsip_contact_hdr *contact_hdr = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr; |
| 742 |
struct registrar_contact_details details = { 0, }; |
| 743 |
pjsip_tx_data *tdata; |
| 744 |
RAII_VAR(struct ast_str *, path_str, NULL, ast_free); |
| 745 |
struct ast_sip_contact *response_contact; |
| 746 |
char *user_agent = NULL; |
| 747 |
pjsip_user_agent_hdr *user_agent_hdr; |
| 748 |
pjsip_expires_hdr *expires_hdr; |
| 749 |
pjsip_via_hdr *via_hdr; |
| 750 |
pjsip_via_hdr *via_hdr_last; |
| 751 |
char *via_addr = NULL; |
| 752 |
int via_port = 0; |
| 753 |
pjsip_cid_hdr *call_id_hdr; |
| 754 |
char *call_id = NULL; |
| 755 |
size_t alloc_size; |
| 756 |
|
| 757 |
|
| 758 |
details.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), |
| 759 |
"Contact Comparison", 1024, 256); |
| 760 |
if (!details.pool) { |
| 761 |
response->code = 500; |
| 762 |
return; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
if (aor->permanent_contacts) { |
| 769 |
permanent = ao2_container_count(aor->permanent_contacts); |
| 770 |
} |
| 771 |
|
| 772 |
if (registrar_validate_contacts(rdata, details.pool, contacts, aor, permanent, &added, &updated, &deleted)) { |
| 773 |
|
| 774 |
ast_sip_report_failed_acl(endpoint, rdata, "registrar_invalid_contacts_provided"); |
| 775 |
ast_log(LOG_WARNING, "Failed to validate contacts in REGISTER request from '%s'\n", |
| 776 |
ast_sorcery_object_get_id(endpoint)); |
| 777 |
response->code = 400; |
| 778 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool); |
| 779 |
return; |
| 780 |
} |
| 781 |
|
| 782 |
if (registrar_validate_path(rdata, aor, &path_str)) { |
| 783 |
|
| 784 |
ast_log(LOG_WARNING, "Invalid modifications made to REGISTER request from '%s' by intervening proxy\n", |
| 785 |
ast_sorcery_object_get_id(endpoint)); |
| 786 |
response->code = 420; |
| 787 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool); |
| 788 |
return; |
| 789 |
} |
| 790 |
|
| 791 |
if (aor->remove_existing) { |
| 792 |
|
| 793 |
contact_count = MAX(updated + added - deleted, 0); |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
existing_contacts = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 799 |
NULL, ast_sorcery_object_id_compare); |
| 800 |
if (!existing_contacts) { |
| 801 |
response->code = 500; |
| 802 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool); |
| 803 |
return; |
| 804 |
} |
| 805 |
|
| 806 |
ao2_callback(contacts, OBJ_NODATA, registrar_add_non_permanent, existing_contacts); |
| 807 |
} else { |
| 808 |
|
| 809 |
contact_count = ao2_container_count(contacts) - permanent + added - deleted; |
| 810 |
} |
| 811 |
|
| 812 |
if (contact_count > aor->max_contacts && aor->remove_unavailable) { |
| 813 |
|
| 814 |
int unavail_count = 0; |
| 815 |
|
| 816 |
unavail_contacts = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 817 |
NULL, ast_sorcery_object_id_compare); |
| 818 |
if (!unavail_contacts) { |
| 819 |
response->code = 500; |
| 820 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool); |
| 821 |
return; |
| 822 |
} |
| 823 |
ao2_callback(contacts, OBJ_NODATA, registrar_add_unreachable, unavail_contacts); |
| 824 |
if (unavail_contacts) { |
| 825 |
unavail_count = ao2_container_count(unavail_contacts); |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
if (contact_count - unavail_count <= aor->max_contacts) { |
| 830 |
|
| 831 |
remove_excess_contacts(unavail_contacts, contacts, contact_count - aor->max_contacts, aor->remove_existing); |
| 832 |
ao2_cleanup(unavail_contacts); |
| 833 |
|
| 834 |
contact_count = ao2_container_count(contacts) - permanent + added - deleted; |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
if (contact_count > aor->max_contacts) { |
| 839 |
|
| 840 |
ast_sip_report_failed_acl(endpoint, rdata, "registrar_attempt_exceeds_maximum_configured_contacts"); |
| 841 |
ast_log(LOG_WARNING, "Registration attempt from endpoint '%s' (%s:%d) to AOR '%s' will exceed max contacts of %u\n", |
| 842 |
ast_sorcery_object_get_id(endpoint), rdata->pkt_info.src_name, rdata->pkt_info.src_port, |
| 843 |
aor_name, aor->max_contacts); |
| 844 |
response->code = 403; |
| 845 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool); |
| 846 |
ao2_cleanup(existing_contacts); |
| 847 |
return; |
| 848 |
} |
| 849 |
|
| 850 |
user_agent_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &USER_AGENT, NULL); |
| 851 |
if (user_agent_hdr) { |
| 852 |
alloc_size = pj_strlen(&user_agent_hdr->hvalue) + 1; |
| 853 |
user_agent = ast_alloca(alloc_size); |
| 854 |
ast_copy_pj_str(user_agent, &user_agent_hdr->hvalue, alloc_size); |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
via_hdr = via_hdr_last = (pjsip_via_hdr*) pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_VIA, NULL); |
| 859 |
if (via_hdr) { |
| 860 |
|
| 861 |
while ( (via_hdr = (pjsip_via_hdr*) pjsip_msg_find_hdr(rdata->msg_info.msg, |
| 862 |
PJSIP_H_VIA, via_hdr->next)) != NULL) { |
| 863 |
via_hdr_last = via_hdr; |
| 864 |
} |
| 865 |
alloc_size = pj_strlen(&via_hdr_last->sent_by.host) + 1; |
| 866 |
via_addr = ast_alloca(alloc_size); |
| 867 |
ast_copy_pj_str(via_addr, &via_hdr_last->sent_by.host, alloc_size); |
| 868 |
via_port=via_hdr_last->sent_by.port; |
| 869 |
} |
| 870 |
|
| 871 |
call_id_hdr = (pjsip_cid_hdr*) pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CALL_ID, NULL); |
| 872 |
if (call_id_hdr) { |
| 873 |
alloc_size = pj_strlen(&call_id_hdr->id) + 1; |
| 874 |
call_id = ast_alloca(alloc_size); |
| 875 |
ast_copy_pj_str(call_id, &call_id_hdr->id, alloc_size); |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
for (; (contact_hdr = (pjsip_contact_hdr *) pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, contact_hdr->next)); pj_pool_reset(details.pool)) { |
| 880 |
int expiration; |
| 881 |
char contact_uri[pjsip_max_url_size]; |
| 882 |
RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup); |
| 883 |
|
| 884 |
if (contact_hdr->star) { |
| 885 |
|
| 886 |
ao2_callback(contacts, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, |
| 887 |
registrar_delete_contact, (void *)aor_name); |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
if (existing_contacts) { |
| 892 |
ao2_ref(existing_contacts, -1); |
| 893 |
existing_contacts = NULL; |
| 894 |
} |
| 895 |
break; |
| 896 |
} |
| 897 |
|
| 898 |
if (!PJSIP_URI_SCHEME_IS_SIP(contact_hdr->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact_hdr->uri)) { |
| 899 |
|
| 900 |
continue; |
| 901 |
} |
| 902 |
|
| 903 |
expiration = registrar_get_expiration(aor, contact_hdr, rdata); |
| 904 |
details.uri = pjsip_uri_get_uri(contact_hdr->uri); |
| 905 |
pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, details.uri, contact_uri, sizeof(contact_uri)); |
| 906 |
|
| 907 |
contact = ao2_callback(contacts, OBJ_UNLINK, registrar_find_contact, &details); |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
if (contact && existing_contacts) { |
| 913 |
ao2_unlink(existing_contacts, contact); |
| 914 |
} |
| 915 |
|
| 916 |
if (!contact) { |
| 917 |
int prune_on_boot; |
| 918 |
|
| 919 |
|
| 920 |
if (!expiration) { |
| 921 |
ast_verb(3, "Attempted to remove non-existent contact '%s' from AOR '%s' by request\n", |
| 922 |
contact_uri, aor_name); |
| 923 |
continue; |
| 924 |
} |
| 925 |
|
| 926 |
prune_on_boot = !ast_sip_will_uri_survive_restart(details.uri, endpoint, rdata); |
| 927 |
|
| 928 |
contact = ast_sip_location_create_contact(aor, contact_uri, |
| 929 |
ast_tvadd(ast_tvnow(), ast_samp2tv(expiration, 1)), |
| 930 |
path_str ? ast_str_buffer(path_str) : NULL, |
| 931 |
user_agent, via_addr, via_port, call_id, prune_on_boot, endpoint); |
| 932 |
if (!contact) { |
| 933 |
ast_log(LOG_ERROR, "Unable to bind contact '%s' to AOR '%s'\n", |
| 934 |
contact_uri, aor_name); |
| 935 |
continue; |
| 936 |
} |
| 937 |
|
| 938 |
if (prune_on_boot) { |
| 939 |
size_t contact_name_len; |
| 940 |
const char *contact_name; |
| 941 |
struct contact_transport_monitor *monitor; |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
contact_name = ast_sorcery_object_get_id(contact); |
| 948 |
contact_name_len = strlen(contact_name) + 1; |
| 949 |
monitor = ao2_alloc(sizeof(*monitor) + 1 + strlen(aor_name) |
| 950 |
+ contact_name_len, NULL); |
| 951 |
if (monitor) { |
| 952 |
strcpy(monitor->aor_name, aor_name); |
| 953 |
monitor->contact_name = monitor->aor_name + strlen(aor_name) + 1; |
| 954 |
ast_copy_string(monitor->contact_name, contact_name, contact_name_len); |
| 955 |
|
| 956 |
ast_sip_transport_monitor_register_replace(rdata->tp_info.transport, |
| 957 |
register_contact_transport_shutdown_cb, monitor, contact_transport_monitor_matcher); |
| 958 |
ao2_ref(monitor, -1); |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
ast_verb(3, "Added contact '%s' to AOR '%s' with expiration of %d seconds\n", |
| 963 |
contact_uri, aor_name, expiration); |
| 964 |
ast_test_suite_event_notify("AOR_CONTACT_ADDED", |
| 965 |
"Contact: %s\r\n" |
| 966 |
"AOR: %s\r\n" |
| 967 |
"Expiration: %d\r\n" |
| 968 |
"UserAgent: %s", |
| 969 |
contact_uri, |
| 970 |
aor_name, |
| 971 |
expiration, |
| 972 |
user_agent); |
| 973 |
|
| 974 |
ao2_link(contacts, contact); |
| 975 |
} else if (expiration) { |
| 976 |
struct ast_sip_contact *contact_update; |
| 977 |
|
| 978 |
contact_update = ast_sorcery_copy(ast_sip_get_sorcery(), contact); |
| 979 |
if (!contact_update) { |
| 980 |
ast_log(LOG_ERROR, "Failed to update contact '%s' expiration time to %d seconds.\n", |
| 981 |
contact->uri, expiration); |
| 982 |
continue; |
| 983 |
} |
| 984 |
|
| 985 |
contact_update->expiration_time = ast_tvadd(ast_tvnow(), ast_samp2tv(expiration, 1)); |
| 986 |
contact_update->qualify_frequency = aor->qualify_frequency; |
| 987 |
contact_update->authenticate_qualify = aor->authenticate_qualify; |
| 988 |
contact_update->qualify_2xx_only = aor->qualify_2xx_only; |
| 989 |
if (path_str) { |
| 990 |
ast_string_field_set(contact_update, path, ast_str_buffer(path_str)); |
| 991 |
} |
| 992 |
if (user_agent) { |
| 993 |
ast_string_field_set(contact_update, user_agent, user_agent); |
| 994 |
} |
| 995 |
if (!ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) { |
| 996 |
ast_string_field_set(contact_update, reg_server, ast_config_AST_SYSTEM_NAME); |
| 997 |
} |
| 998 |
|
| 999 |
if (ast_sip_location_update_contact(contact_update)) { |
| 1000 |
ast_log(LOG_ERROR, "Failed to update contact '%s' expiration time to %d seconds.\n", |
| 1001 |
contact->uri, expiration); |
| 1002 |
registrar_contact_delete(CONTACT_DELETE_ERROR, rdata->tp_info.transport, |
| 1003 |
contact, aor_name); |
| 1004 |
continue; |
| 1005 |
} |
| 1006 |
ast_debug(3, "Refreshed contact '%s' on AOR '%s' with new expiration of %d seconds\n", |
| 1007 |
contact_uri, aor_name, expiration); |
| 1008 |
ast_test_suite_event_notify("AOR_CONTACT_REFRESHED", |
| 1009 |
"Contact: %s\r\n" |
| 1010 |
"AOR: %s\r\n" |
| 1011 |
"Expiration: %d\r\n" |
| 1012 |
"UserAgent: %s", |
| 1013 |
contact_uri, |
| 1014 |
aor_name, |
| 1015 |
expiration, |
| 1016 |
contact_update->user_agent); |
| 1017 |
ao2_link(contacts, contact_update); |
| 1018 |
ao2_cleanup(contact_update); |
| 1019 |
} else { |
| 1020 |
registrar_contact_delete(CONTACT_DELETE_REQUEST, rdata->tp_info.transport, |
| 1021 |
contact, aor_name); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool); |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
if (aor->remove_existing && existing_contacts) { |
| 1037 |
|
| 1038 |
contact_count = ao2_container_count(existing_contacts) + updated + added; |
| 1039 |
if (contact_count > aor->max_contacts) { |
| 1040 |
|
| 1041 |
remove_excess_contacts(existing_contacts, contacts, contact_count - aor->max_contacts, |
| 1042 |
aor->remove_existing); |
| 1043 |
} |
| 1044 |
ao2_ref(existing_contacts, -1); |
| 1045 |
} |
| 1046 |
|
| 1047 |
response_contact = ao2_callback(contacts, 0, NULL, NULL); |
| 1048 |
|
| 1049 |
|
| 1050 |
if (ast_sip_create_response(rdata, 200, response_contact, &tdata) != PJ_SUCCESS) { |
| 1051 |
ao2_cleanup(response_contact); |
| 1052 |
ao2_cleanup(contacts); |
| 1053 |
response->code = 500; |
| 1054 |
return; |
| 1055 |
} |
| 1056 |
ao2_cleanup(response_contact); |
| 1057 |
|
| 1058 |
|
| 1059 |
ast_sip_add_date_header(tdata); |
| 1060 |
|
| 1061 |
ao2_callback(contacts, 0, registrar_add_contact, tdata); |
| 1062 |
|
| 1063 |
if ((expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL))) { |
| 1064 |
expires_hdr = pjsip_expires_hdr_create(tdata->pool, registrar_get_expiration(aor, NULL, rdata)); |
| 1065 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)expires_hdr); |
| 1066 |
} |
| 1067 |
|
| 1068 |
response->tdata = tdata; |
| 1069 |
} |
| 1070 |
|
| 1071 |
static int register_aor(pjsip_rx_data *rdata, |
| 1072 |
struct ast_sip_endpoint *endpoint, |
| 1073 |
struct ast_sip_aor *aor, |
| 1074 |
const char *aor_name) |
| 1075 |
{ |
| 1076 |
struct aor_core_response response = { |
| 1077 |
.code = 500, |
| 1078 |
}; |
| 1079 |
struct ao2_container *contacts = NULL; |
| 1080 |
|
| 1081 |
ao2_lock(aor); |
| 1082 |
contacts = ast_sip_location_retrieve_aor_contacts_nolock(aor); |
| 1083 |
if (!contacts) { |
| 1084 |
ao2_unlock(aor); |
| 1085 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), |
| 1086 |
rdata, response.code, NULL, NULL, NULL); |
| 1087 |
return PJ_TRUE; |
| 1088 |
} |
| 1089 |
|
| 1090 |
register_aor_core(rdata, endpoint, aor, aor_name, contacts, &response); |
| 1091 |
ao2_cleanup(contacts); |
| 1092 |
ao2_unlock(aor); |
| 1093 |
|
| 1094 |
|
| 1095 |
if (response.tdata) { |
| 1096 |
ast_sip_send_stateful_response(rdata, response.tdata, endpoint); |
| 1097 |
} else { |
| 1098 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), |
| 1099 |
rdata, response.code, NULL, NULL, NULL); |
| 1100 |
} |
| 1101 |
return PJ_TRUE; |
| 1102 |
} |
| 1103 |
|
| 1104 |
static int match_aor(const char *aor_name, const char *id) |
| 1105 |
{ |
| 1106 |
if (ast_strlen_zero(aor_name)) { |
| 1107 |
return 0; |
| 1108 |
} |
| 1109 |
|
| 1110 |
if (!strcmp(aor_name, id)) { |
| 1111 |
ast_debug(3, "Matched id '%s' to aor '%s'\n", id, aor_name); |
| 1112 |
return 1; |
| 1113 |
} |
| 1114 |
|
| 1115 |
return 0; |
| 1116 |
} |
| 1117 |
|
| 1118 |
static char *find_aor_name(const pj_str_t *pj_username, const pj_str_t *pj_domain, const char *aors) |
| 1119 |
{ |
| 1120 |
char *configured_aors; |
| 1121 |
char *aors_buf; |
| 1122 |
char *aor_name; |
| 1123 |
char *id_domain; |
| 1124 |
char *username, *domain; |
| 1125 |
struct ast_sip_domain_alias *alias; |
| 1126 |
|
| 1127 |
|
| 1128 |
username = ast_alloca(pj_strlen(pj_username) + 1); |
| 1129 |
ast_copy_pj_str(username, pj_username, pj_strlen(pj_username) + 1); |
| 1130 |
domain = ast_alloca(pj_strlen(pj_domain) + 1); |
| 1131 |
ast_copy_pj_str(domain, pj_domain, pj_strlen(pj_domain) + 1); |
| 1132 |
|
| 1133 |
id_domain = ast_alloca(strlen(username) + strlen(domain) + 2); |
| 1134 |
sprintf(id_domain, "%s@%s", username, domain); |
| 1135 |
|
| 1136 |
aors_buf = ast_strdupa(aors); |
| 1137 |
|
| 1138 |
|
| 1139 |
configured_aors = aors_buf; |
| 1140 |
while ((aor_name = ast_strip(strsep(&configured_aors, ",")))) { |
| 1141 |
if (match_aor(aor_name, id_domain)) { |
| 1142 |
return ast_strdup(aor_name); |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain); |
| 1148 |
if (alias) { |
| 1149 |
char *id_domain_alias = ast_alloca(strlen(username) + strlen(alias->domain) + 2); |
| 1150 |
|
| 1151 |
sprintf(id_domain_alias, "%s@%s", username, alias->domain); |
| 1152 |
ao2_cleanup(alias); |
| 1153 |
|
| 1154 |
configured_aors = strcpy(aors_buf, aors); |
| 1155 |
while ((aor_name = ast_strip(strsep(&configured_aors, ",")))) { |
| 1156 |
if (match_aor(aor_name, id_domain_alias)) { |
| 1157 |
return ast_strdup(aor_name); |
| 1158 |
} |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
if (ast_strlen_zero(username)) { |
| 1163 |
|
| 1164 |
return NULL; |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
configured_aors = strcpy(aors_buf, aors); |
| 1169 |
while ((aor_name = ast_strip(strsep(&configured_aors, ",")))) { |
| 1170 |
if (match_aor(aor_name, username)) { |
| 1171 |
return ast_strdup(aor_name); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
return NULL; |
| 1176 |
} |
| 1177 |
|
| 1178 |
static struct ast_sip_aor *find_registrar_aor(struct pjsip_rx_data *rdata, struct ast_sip_endpoint *endpoint) |
| 1179 |
{ |
| 1180 |
struct ast_sip_aor *aor = NULL; |
| 1181 |
char *aor_name = NULL; |
| 1182 |
int i; |
| 1183 |
|
| 1184 |
for (i = 0; i < AST_VECTOR_SIZE(&endpoint->ident_method_order); ++i) { |
| 1185 |
pj_str_t username; |
| 1186 |
pjsip_sip_uri *uri; |
| 1187 |
pjsip_authorization_hdr *header = NULL; |
| 1188 |
|
| 1189 |
switch (AST_VECTOR_GET(&endpoint->ident_method_order, i)) { |
| 1190 |
case AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME: |
| 1191 |
uri = pjsip_uri_get_uri(rdata->msg_info.to->uri); |
| 1192 |
|
| 1193 |
pj_strassign(&username, &uri->user); |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
if (ast_sip_get_ignore_uri_user_options()) { |
| 1202 |
pj_ssize_t semi = pj_strcspn2(&username, ";"); |
| 1203 |
if (semi < pj_strlen(&username)) { |
| 1204 |
username.slen = semi; |
| 1205 |
} |
| 1206 |
} |
| 1207 |
|
| 1208 |
aor_name = find_aor_name(&username, &uri->host, endpoint->aors); |
| 1209 |
if (aor_name) { |
| 1210 |
ast_debug(3, "Matched aor '%s' by To username\n", aor_name); |
| 1211 |
} |
| 1212 |
break; |
| 1213 |
case AST_SIP_ENDPOINT_IDENTIFY_BY_AUTH_USERNAME: |
| 1214 |
while ((header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_AUTHORIZATION, |
| 1215 |
header ? header->next : NULL))) { |
| 1216 |
if (header && !pj_stricmp2(&header->scheme, "digest")) { |
| 1217 |
aor_name = find_aor_name(&header->credential.digest.username, |
| 1218 |
&header->credential.digest.realm, endpoint->aors); |
| 1219 |
if (aor_name) { |
| 1220 |
ast_debug(3, "Matched aor '%s' by Authentication username\n", aor_name); |
| 1221 |
break; |
| 1222 |
} |
| 1223 |
} |
| 1224 |
} |
| 1225 |
break; |
| 1226 |
default: |
| 1227 |
continue; |
| 1228 |
} |
| 1229 |
|
| 1230 |
if (aor_name) { |
| 1231 |
break; |
| 1232 |
} |
| 1233 |
} |
| 1234 |
|
| 1235 |
if (ast_strlen_zero(aor_name) || !(aor = ast_sip_location_retrieve_aor(aor_name))) { |
| 1236 |
|
| 1237 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL); |
| 1238 |
ast_sip_report_req_no_support(endpoint, rdata, "registrar_requested_aor_not_found"); |
| 1239 |
ast_log(LOG_WARNING, "AOR '%s' not found for endpoint '%s' (%s:%d)\n", |
| 1240 |
aor_name ?: "", ast_sorcery_object_get_id(endpoint), |
| 1241 |
rdata->pkt_info.src_name, rdata->pkt_info.src_port); |
| 1242 |
} |
| 1243 |
ast_free(aor_name); |
| 1244 |
return aor; |
| 1245 |
} |
| 1246 |
|
| 1247 |
static pj_bool_t registrar_on_rx_request(struct pjsip_rx_data *rdata) |
| 1248 |
{ |
| 1249 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, |
| 1250 |
ast_pjsip_rdata_get_endpoint(rdata), ao2_cleanup); |
| 1251 |
struct ast_sip_aor *aor; |
| 1252 |
const char *aor_name; |
| 1253 |
|
| 1254 |
if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_register_method) || !endpoint) { |
| 1255 |
return PJ_FALSE; |
| 1256 |
} |
| 1257 |
|
| 1258 |
if (ast_strlen_zero(endpoint->aors)) { |
| 1259 |
|
| 1260 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL); |
| 1261 |
ast_sip_report_failed_acl(endpoint, rdata, "registrar_attempt_without_configured_aors"); |
| 1262 |
ast_log(LOG_WARNING, "Endpoint '%s' (%s:%d) has no configured AORs\n", ast_sorcery_object_get_id(endpoint), |
| 1263 |
rdata->pkt_info.src_name, rdata->pkt_info.src_port); |
| 1264 |
return PJ_TRUE; |
| 1265 |
} |
| 1266 |
|
| 1267 |
if (!PJSIP_URI_SCHEME_IS_SIP(rdata->msg_info.to->uri) && !PJSIP_URI_SCHEME_IS_SIPS(rdata->msg_info.to->uri)) { |
| 1268 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 416, NULL, NULL, NULL); |
| 1269 |
ast_sip_report_failed_acl(endpoint, rdata, "registrar_invalid_uri_in_to_received"); |
| 1270 |
ast_log(LOG_WARNING, "Endpoint '%s' (%s:%d) attempted to register to an AOR with a non-SIP URI\n", ast_sorcery_object_get_id(endpoint), |
| 1271 |
rdata->pkt_info.src_name, rdata->pkt_info.src_port); |
| 1272 |
return PJ_TRUE; |
| 1273 |
} |
| 1274 |
|
| 1275 |
aor = find_registrar_aor(rdata, endpoint); |
| 1276 |
if (!aor) { |
| 1277 |
|
| 1278 |
return PJ_TRUE; |
| 1279 |
} |
| 1280 |
|
| 1281 |
aor_name = ast_sorcery_object_get_id(aor); |
| 1282 |
|
| 1283 |
if (!aor->max_contacts) { |
| 1284 |
|
| 1285 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL); |
| 1286 |
ast_sip_report_req_no_support(endpoint, rdata, "registrar_attempt_without_registration_permitted"); |
| 1287 |
ast_log(LOG_WARNING, "AOR '%s' has no configured max_contacts. Endpoint '%s' (%s:%d) unable to register\n", |
| 1288 |
aor_name, ast_sorcery_object_get_id(endpoint), |
| 1289 |
rdata->pkt_info.src_name, rdata->pkt_info.src_port); |
| 1290 |
} else { |
| 1291 |
register_aor(rdata, endpoint, aor, aor_name); |
| 1292 |
} |
| 1293 |
ao2_ref(aor, -1); |
| 1294 |
return PJ_TRUE; |
| 1295 |
} |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
static int sip_contact_to_str(void *acp, void *arg, int flags) |
| 1300 |
{ |
| 1301 |
return ast_sip_contact_to_str(acp, arg, flags); |
| 1302 |
} |
| 1303 |
|
| 1304 |
static int ami_registrations_aor(void *obj, void *arg, int flags) |
| 1305 |
{ |
| 1306 |
struct ast_sip_aor *aor = obj; |
| 1307 |
struct ast_sip_ami *ami = arg; |
| 1308 |
int *count = ami->arg; |
| 1309 |
RAII_VAR(struct ast_str *, buf, |
| 1310 |
ast_sip_create_ami_event("InboundRegistrationDetail", ami), ast_free); |
| 1311 |
|
| 1312 |
if (!buf) { |
| 1313 |
return -1; |
| 1314 |
} |
| 1315 |
|
| 1316 |
ast_sip_sorcery_object_to_ami(aor, &buf); |
| 1317 |
ast_str_append(&buf, 0, "Contacts: "); |
| 1318 |
ast_sip_for_each_contact(aor, sip_contact_to_str, &buf); |
| 1319 |
ast_str_append(&buf, 0, "\r\n"); |
| 1320 |
|
| 1321 |
astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); |
| 1322 |
(*count)++; |
| 1323 |
return 0; |
| 1324 |
} |
| 1325 |
|
| 1326 |
static int ami_registrations_endpoint(void *obj, void *arg, int flags) |
| 1327 |
{ |
| 1328 |
struct ast_sip_endpoint *endpoint = obj; |
| 1329 |
return ast_sip_for_each_aor( |
| 1330 |
endpoint->aors, ami_registrations_aor, arg); |
| 1331 |
} |
| 1332 |
|
| 1333 |
static int ami_registrations_endpoints(void *arg) |
| 1334 |
{ |
| 1335 |
RAII_VAR(struct ao2_container *, endpoints, |
| 1336 |
ast_sip_get_endpoints(), ao2_cleanup); |
| 1337 |
|
| 1338 |
if (!endpoints) { |
| 1339 |
return 0; |
| 1340 |
} |
| 1341 |
|
| 1342 |
ao2_callback(endpoints, OBJ_NODATA, ami_registrations_endpoint, arg); |
| 1343 |
return 0; |
| 1344 |
} |
| 1345 |
|
| 1346 |
static int ami_show_registrations(struct mansession *s, const struct message *m) |
| 1347 |
{ |
| 1348 |
int count = 0; |
| 1349 |
struct ast_sip_ami ami = { .s = s, .m = m, .arg = &count, .action_id = astman_get_header(m, "ActionID"), }; |
| 1350 |
|
| 1351 |
astman_send_listack(s, m, "Following are Events for each Inbound registration", |
| 1352 |
"start"); |
| 1353 |
|
| 1354 |
ami_registrations_endpoints(&ami); |
| 1355 |
|
| 1356 |
astman_send_list_complete_start(s, m, "InboundRegistrationDetailComplete", count); |
| 1357 |
astman_send_list_complete_end(s); |
| 1358 |
return 0; |
| 1359 |
} |
| 1360 |
|
| 1361 |
static int ami_show_registration_contact_statuses(struct mansession *s, const struct message *m) |
| 1362 |
{ |
| 1363 |
int count = 0; |
| 1364 |
struct ast_sip_ami ami = { .s = s, .m = m, .arg = NULL, .action_id = astman_get_header(m, "ActionID"), }; |
| 1365 |
struct ao2_container *contacts = ast_sorcery_retrieve_by_fields( |
| 1366 |
ast_sip_get_sorcery(), "contact", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 1367 |
struct ao2_iterator i; |
| 1368 |
struct ast_sip_contact *contact; |
| 1369 |
|
| 1370 |
astman_send_listack(s, m, "Following are ContactStatusEvents for each Inbound " |
| 1371 |
"registration", "start"); |
| 1372 |
|
| 1373 |
if (contacts) { |
| 1374 |
i = ao2_iterator_init(contacts, 0); |
| 1375 |
while ((contact = ao2_iterator_next(&i))) { |
| 1376 |
struct ast_sip_contact_wrapper wrapper; |
| 1377 |
|
| 1378 |
wrapper.aor_id = (char *)contact->aor; |
| 1379 |
wrapper.contact = contact; |
| 1380 |
wrapper.contact_id = (char *)ast_sorcery_object_get_id(contact); |
| 1381 |
|
| 1382 |
ast_sip_format_contact_ami(&wrapper, &ami, 0); |
| 1383 |
count++; |
| 1384 |
|
| 1385 |
ao2_ref(contact, -1); |
| 1386 |
} |
| 1387 |
ao2_iterator_destroy(&i); |
| 1388 |
ao2_ref(contacts, -1); |
| 1389 |
} |
| 1390 |
|
| 1391 |
astman_send_list_complete_start(s, m, "ContactStatusDetailComplete", count); |
| 1392 |
astman_send_list_complete_end(s); |
| 1393 |
return 0; |
| 1394 |
} |
| 1395 |
|
| 1396 |
#define AMI_SHOW_REGISTRATION_CONTACT_STATUSES "PJSIPShowRegistrationInboundContactStatuses" |
| 1397 |
#define AMI_SHOW_REGISTRATIONS "PJSIPShowRegistrationsInbound" |
| 1398 |
|
| 1399 |
static pjsip_module registrar_module = { |
| 1400 |
.name = { "Registrar", 9 }, |
| 1401 |
.id = -1, |
| 1402 |
.priority = PJSIP_MOD_PRIORITY_APPLICATION, |
| 1403 |
.on_rx_request = registrar_on_rx_request, |
| 1404 |
}; |
| 1405 |
|
| 1406 |
|
| 1407 |
static pthread_t check_thread = AST_PTHREADT_NULL; |
| 1408 |
|
| 1409 |
|
| 1410 |
static unsigned int check_interval; |
| 1411 |
|
| 1412 |
|
| 1413 |
static int expire_contact(void *obj, void *arg, int flags) |
| 1414 |
{ |
| 1415 |
struct ast_sip_contact *contact = obj; |
| 1416 |
struct ast_named_lock *lock; |
| 1417 |
|
| 1418 |
lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "aor", contact->aor); |
| 1419 |
if (!lock) { |
| 1420 |
return 0; |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
ao2_lock(lock); |
| 1428 |
if (ast_tvdiff_ms(ast_tvnow(), contact->expiration_time) > 0) { |
| 1429 |
registrar_contact_delete(CONTACT_DELETE_EXPIRE, NULL, contact, contact->aor); |
| 1430 |
} |
| 1431 |
ao2_unlock(lock); |
| 1432 |
ast_named_lock_put(lock); |
| 1433 |
|
| 1434 |
return 0; |
| 1435 |
} |
| 1436 |
|
| 1437 |
static void *check_expiration_thread(void *data) |
| 1438 |
{ |
| 1439 |
struct ao2_container *contacts; |
| 1440 |
struct ast_variable *var; |
| 1441 |
char time[AST_TIME_T_LEN]; |
| 1442 |
|
| 1443 |
while (check_interval) { |
| 1444 |
sleep(check_interval); |
| 1445 |
|
| 1446 |
ast_time_t_to_string(ast_tvnow().tv_sec, time, sizeof(time)); |
| 1447 |
|
| 1448 |
var = ast_variable_new("expiration_time <=", time, ""); |
| 1449 |
|
| 1450 |
ast_debug(4, "Woke up at %s Interval: %d\n", time, check_interval); |
| 1451 |
|
| 1452 |
contacts = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "contact", |
| 1453 |
AST_RETRIEVE_FLAG_MULTIPLE, var); |
| 1454 |
|
| 1455 |
ast_variables_destroy(var); |
| 1456 |
if (contacts) { |
| 1457 |
ast_debug(3, "Expiring %d contacts\n", ao2_container_count(contacts)); |
| 1458 |
ao2_callback(contacts, OBJ_NODATA, expire_contact, NULL); |
| 1459 |
ao2_ref(contacts, -1); |
| 1460 |
} |
| 1461 |
} |
| 1462 |
|
| 1463 |
return NULL; |
| 1464 |
} |
| 1465 |
|
| 1466 |
static void expiration_global_loaded(const char *object_type) |
| 1467 |
{ |
| 1468 |
check_interval = ast_sip_get_contact_expiration_check_interval(); |
| 1469 |
|
| 1470 |
|
| 1471 |
if (check_interval) { |
| 1472 |
if (check_thread == AST_PTHREADT_NULL) { |
| 1473 |
if (ast_pthread_create_background(&check_thread, NULL, check_expiration_thread, NULL)) { |
| 1474 |
ast_log(LOG_ERROR, "Could not create thread for checking contact expiration.\n"); |
| 1475 |
return; |
| 1476 |
} |
| 1477 |
ast_debug(3, "Interval = %d, starting thread\n", check_interval); |
| 1478 |
} |
| 1479 |
} else { |
| 1480 |
if (check_thread != AST_PTHREADT_NULL) { |
| 1481 |
pthread_kill(check_thread, SIGURG); |
| 1482 |
pthread_join(check_thread, NULL); |
| 1483 |
check_thread = AST_PTHREADT_NULL; |
| 1484 |
ast_debug(3, "Interval = 0, shutting thread down\n"); |
| 1485 |
} |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
|
| 1490 |
static struct ast_sorcery_observer expiration_global_observer = { |
| 1491 |
.loaded = expiration_global_loaded, |
| 1492 |
}; |
| 1493 |
|
| 1494 |
static int load_module(void) |
| 1495 |
{ |
| 1496 |
const pj_str_t STR_REGISTER = { "REGISTER", 8 }; |
| 1497 |
|
| 1498 |
ast_pjproject_get_buildopt("PJ_MAX_HOSTNAME", "%d", &pj_max_hostname); |
| 1499 |
|
| 1500 |
ast_pjproject_get_buildopt("PJSIP_MAX_URL_SIZE", "%d", &pjsip_max_url_size); |
| 1501 |
|
| 1502 |
if (ast_sip_register_service(®istrar_module)) { |
| 1503 |
return AST_MODULE_LOAD_DECLINE; |
| 1504 |
} |
| 1505 |
|
| 1506 |
if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), NULL, PJSIP_H_ALLOW, NULL, 1, &STR_REGISTER) != PJ_SUCCESS) { |
| 1507 |
ast_sip_unregister_service(®istrar_module); |
| 1508 |
return AST_MODULE_LOAD_DECLINE; |
| 1509 |
} |
| 1510 |
|
| 1511 |
ast_manager_register_xml(AMI_SHOW_REGISTRATIONS, EVENT_FLAG_SYSTEM, |
| 1512 |
ami_show_registrations); |
| 1513 |
ast_manager_register_xml(AMI_SHOW_REGISTRATION_CONTACT_STATUSES, EVENT_FLAG_SYSTEM, |
| 1514 |
ami_show_registration_contact_statuses); |
| 1515 |
|
| 1516 |
ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &expiration_global_observer); |
| 1517 |
ast_sorcery_reload_object(ast_sip_get_sorcery(), "global"); |
| 1518 |
|
| 1519 |
return AST_MODULE_LOAD_SUCCESS; |
| 1520 |
} |
| 1521 |
|
| 1522 |
static int unload_module(void) |
| 1523 |
{ |
| 1524 |
if (check_thread != AST_PTHREADT_NULL) { |
| 1525 |
check_interval = 0; |
| 1526 |
pthread_kill(check_thread, SIGURG); |
| 1527 |
pthread_join(check_thread, NULL); |
| 1528 |
|
| 1529 |
check_thread = AST_PTHREADT_NULL; |
| 1530 |
} |
| 1531 |
|
| 1532 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "global", &expiration_global_observer); |
| 1533 |
|
| 1534 |
ast_manager_unregister(AMI_SHOW_REGISTRATIONS); |
| 1535 |
ast_manager_unregister(AMI_SHOW_REGISTRATION_CONTACT_STATUSES); |
| 1536 |
ast_sip_unregister_service(®istrar_module); |
| 1537 |
ast_sip_transport_monitor_unregister_all(register_contact_transport_shutdown_cb, NULL, NULL); |
| 1538 |
return 0; |
| 1539 |
} |
| 1540 |
|
| 1541 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Registrar Support", |
| 1542 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1543 |
.load = load_module, |
| 1544 |
.unload = unload_module, |
| 1545 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND - 3, |
| 1546 |
.requires = "res_pjproject,res_pjsip", |
| 1547 |
); |