| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "asterisk.h" |
| 21 |
|
| 22 |
#include <pjsip.h> |
| 23 |
#include <pjsip_ua.h> |
| 24 |
#include <pjlib.h> |
| 25 |
|
| 26 |
#include "asterisk/res_pjsip.h" |
| 27 |
#include "asterisk/channel.h" |
| 28 |
#include "asterisk/pbx.h" |
| 29 |
#include "asterisk/astobj2.h" |
| 30 |
#include "asterisk/cli.h" |
| 31 |
#include "asterisk/time.h" |
| 32 |
#include "asterisk/test.h" |
| 33 |
#include "asterisk/statsd.h" |
| 34 |
#include "include/res_pjsip_private.h" |
| 35 |
#include "asterisk/taskprocessor.h" |
| 36 |
#include "asterisk/serializer_shutdown_group.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#define DEFAULT_LANGUAGE "en" |
| 97 |
#define DEFAULT_ENCODING "identity" |
| 98 |
|
| 99 |
|
| 100 |
#ifdef LOW_MEMORY |
| 101 |
#define AOR_BUCKETS 61 |
| 102 |
#else |
| 103 |
#define AOR_BUCKETS 1567 |
| 104 |
#endif |
| 105 |
|
| 106 |
|
| 107 |
#ifdef LOW_MEMORY |
| 108 |
#define CONTACT_STATUS_BUCKETS 61 |
| 109 |
#else |
| 110 |
#define CONTACT_STATUS_BUCKETS 1567 |
| 111 |
#endif |
| 112 |
|
| 113 |
|
| 114 |
#define CONTACT_BUCKETS 13 |
| 115 |
|
| 116 |
|
| 117 |
#define ENDPOINT_STATE_COMPOSITOR_BUCKETS 13 |
| 118 |
|
| 119 |
|
| 120 |
#define ENDPOINT_STATE_COMPOSITOR_INITIAL_SIZE 1 |
| 121 |
|
| 122 |
|
| 123 |
#define AOR_STATUS_BUCKETS 3 |
| 124 |
|
| 125 |
|
| 126 |
#define MAX_UNLOAD_TIMEOUT_TIME 10 |
| 127 |
|
| 128 |
|
| 129 |
static struct ast_serializer_shutdown_group *shutdown_group; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
struct sip_options_endpoint_aor_status { |
| 135 |
|
| 136 |
char available; |
| 137 |
|
| 138 |
char name[0]; |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
struct sip_options_endpoint_state_compositor { |
| 145 |
|
| 146 |
struct ao2_container *aor_statuses; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
char active; |
| 157 |
|
| 158 |
char name[0]; |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
struct sip_options_aor { |
| 165 |
|
| 166 |
struct ast_sip_sched_task *sched_task; |
| 167 |
|
| 168 |
struct ast_taskprocessor *serializer; |
| 169 |
|
| 170 |
struct ao2_container *contacts; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
struct ao2_container *dynamic_contacts; |
| 177 |
|
| 178 |
AST_VECTOR(, struct sip_options_endpoint_state_compositor *) compositors; |
| 179 |
|
| 180 |
unsigned int available; |
| 181 |
|
| 182 |
unsigned int qualify_frequency; |
| 183 |
|
| 184 |
int qualify_2xx_only; |
| 185 |
|
| 186 |
int authenticate_qualify; |
| 187 |
|
| 188 |
double qualify_timeout; |
| 189 |
|
| 190 |
char name[0]; |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
static struct ao2_container *sip_options_aors; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
static struct ao2_container *sip_options_contact_statuses; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
static struct ao2_container *sip_options_endpoint_state_compositors; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
static struct ast_taskprocessor *management_serializer; |
| 216 |
|
| 217 |
static pj_status_t send_options_response(pjsip_rx_data *rdata, int code) |
| 218 |
{ |
| 219 |
pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint(); |
| 220 |
pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata); |
| 221 |
pjsip_transaction *trans = pjsip_rdata_get_tsx(rdata); |
| 222 |
pjsip_tx_data *tdata; |
| 223 |
const pjsip_hdr *hdr; |
| 224 |
pj_status_t status; |
| 225 |
|
| 226 |
|
| 227 |
status = ast_sip_create_response(rdata, code, NULL, &tdata); |
| 228 |
if (status != PJ_SUCCESS) { |
| 229 |
ast_log(LOG_ERROR, "Unable to create response (%d)\n", status); |
| 230 |
return status; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_ACCEPT, NULL))) { |
| 235 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr)); |
| 236 |
} |
| 237 |
if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_ALLOW, NULL))) { |
| 238 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr)); |
| 239 |
} |
| 240 |
if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, NULL))) { |
| 241 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr)); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
ast_sip_add_header(tdata, "Accept-Encoding", DEFAULT_ENCODING); |
| 250 |
ast_sip_add_header(tdata, "Accept-Language", DEFAULT_LANGUAGE); |
| 251 |
|
| 252 |
if (dlg && trans) { |
| 253 |
status = pjsip_dlg_send_response(dlg, trans, tdata); |
| 254 |
} else { |
| 255 |
struct ast_sip_endpoint *endpoint; |
| 256 |
|
| 257 |
endpoint = ast_pjsip_rdata_get_endpoint(rdata); |
| 258 |
status = ast_sip_send_stateful_response(rdata, tdata, endpoint); |
| 259 |
ao2_cleanup(endpoint); |
| 260 |
} |
| 261 |
|
| 262 |
if (status != PJ_SUCCESS) { |
| 263 |
ast_log(LOG_ERROR, "Unable to send response (%d)\n", status); |
| 264 |
} |
| 265 |
|
| 266 |
return status; |
| 267 |
} |
| 268 |
|
| 269 |
static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata) |
| 270 |
{ |
| 271 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 272 |
pjsip_uri *ruri; |
| 273 |
char exten[AST_MAX_EXTENSION]; |
| 274 |
|
| 275 |
if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_options_method)) { |
| 276 |
return PJ_FALSE; |
| 277 |
} |
| 278 |
|
| 279 |
if (!(endpoint = ast_pjsip_rdata_get_endpoint(rdata))) { |
| 280 |
return PJ_FALSE; |
| 281 |
} |
| 282 |
|
| 283 |
ruri = rdata->msg_info.msg->line.req.uri; |
| 284 |
if (!ast_sip_is_allowed_uri(ruri)) { |
| 285 |
send_options_response(rdata, 416); |
| 286 |
return PJ_TRUE; |
| 287 |
} |
| 288 |
|
| 289 |
ast_copy_pj_str(exten, ast_sip_pjsip_uri_get_username(ruri), sizeof(exten)); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten); |
| 296 |
|
| 297 |
if (ast_shutting_down()) { |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
send_options_response(rdata, 503); |
| 303 |
} else if (!ast_strlen_zero(exten) |
| 304 |
&& !ast_exists_extension(NULL, endpoint->context, exten, 1, NULL)) { |
| 305 |
send_options_response(rdata, 404); |
| 306 |
} else { |
| 307 |
send_options_response(rdata, 200); |
| 308 |
} |
| 309 |
return PJ_TRUE; |
| 310 |
} |
| 311 |
|
| 312 |
static pjsip_module options_module = { |
| 313 |
.name = {"Options Module", 14}, |
| 314 |
.id = -1, |
| 315 |
.priority = PJSIP_MOD_PRIORITY_APPLICATION, |
| 316 |
.on_rx_request = options_on_rx_request, |
| 317 |
}; |
| 318 |
|
| 319 |
static const char *status_map[] = { |
| 320 |
[UNAVAILABLE] = "Unreachable", |
| 321 |
[AVAILABLE] = "Reachable", |
| 322 |
[UNKNOWN] = "Unknown", |
| 323 |
[CREATED] = "NonQualified", |
| 324 |
[REMOVED] = "Removed", |
| 325 |
}; |
| 326 |
|
| 327 |
static const char *short_status_map[] = { |
| 328 |
[UNAVAILABLE] = "Unavail", |
| 329 |
[AVAILABLE] = "Avail", |
| 330 |
[UNKNOWN] = "Unknown", |
| 331 |
[CREATED] = "NonQual", |
| 332 |
[REMOVED] = "Removed", |
| 333 |
}; |
| 334 |
|
| 335 |
const char *ast_sip_get_contact_status_label(const enum ast_sip_contact_status_type status) |
| 336 |
{ |
| 337 |
ast_assert(0 <= status && status < ARRAY_LEN(status_map)); |
| 338 |
return status_map[status]; |
| 339 |
} |
| 340 |
|
| 341 |
const char *ast_sip_get_contact_short_status_label(const enum ast_sip_contact_status_type status) |
| 342 |
{ |
| 343 |
ast_assert(0 <= status && status < ARRAY_LEN(short_status_map)); |
| 344 |
return short_status_map[status]; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
static void sip_contact_status_dtor(void *obj) |
| 349 |
{ |
| 350 |
struct ast_sip_contact_status *contact_status = obj; |
| 351 |
|
| 352 |
ast_sip_security_mechanisms_vector_destroy(&contact_status->security_mechanisms); |
| 353 |
|
| 354 |
ast_string_field_free_memory(contact_status); |
| 355 |
} |
| 356 |
|
| 357 |
static struct ast_sip_contact_status *sip_contact_status_alloc(const char *name) |
| 358 |
{ |
| 359 |
struct ast_sip_contact_status *contact_status; |
| 360 |
size_t size = sizeof(*contact_status) + strlen(name) + 1; |
| 361 |
|
| 362 |
contact_status = ao2_alloc_options(size, sip_contact_status_dtor, |
| 363 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 364 |
if (!contact_status) { |
| 365 |
return NULL; |
| 366 |
} |
| 367 |
if (ast_string_field_init(contact_status, 256)) { |
| 368 |
ao2_ref(contact_status, -1); |
| 369 |
return NULL; |
| 370 |
} |
| 371 |
AST_VECTOR_INIT(&contact_status->security_mechanisms, 0); |
| 372 |
strcpy(contact_status->name, name); |
| 373 |
return contact_status; |
| 374 |
} |
| 375 |
|
| 376 |
static struct ast_sip_contact_status *sip_contact_status_copy(const struct ast_sip_contact_status *src) |
| 377 |
{ |
| 378 |
struct ast_sip_contact_status *dst; |
| 379 |
|
| 380 |
dst = sip_contact_status_alloc(src->name); |
| 381 |
if (!dst) { |
| 382 |
return NULL; |
| 383 |
} |
| 384 |
|
| 385 |
if (ast_string_fields_copy(dst, src)) { |
| 386 |
ao2_ref(dst, -1); |
| 387 |
return NULL; |
| 388 |
} |
| 389 |
dst->rtt = src->rtt; |
| 390 |
dst->status = src->status; |
| 391 |
dst->last_status = src->last_status; |
| 392 |
|
| 393 |
ast_sip_security_mechanisms_vector_copy(&dst->security_mechanisms, &src->security_mechanisms); |
| 394 |
return dst; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
AO2_STRING_FIELD_HASH_FN(ast_sip_contact_status, name); |
| 399 |
|
| 400 |
|
| 401 |
AO2_STRING_FIELD_SORT_FN(ast_sip_contact_status, name); |
| 402 |
|
| 403 |
|
| 404 |
AO2_STRING_FIELD_CMP_FN(ast_sip_contact_status, name); |
| 405 |
|
| 406 |
|
| 407 |
static struct ao2_container *sip_options_contact_statuses_alloc(void) |
| 408 |
{ |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
return ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, |
| 414 |
AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, CONTACT_STATUS_BUCKETS, |
| 415 |
ast_sip_contact_status_hash_fn, ast_sip_contact_status_sort_fn, |
| 416 |
ast_sip_contact_status_cmp_fn); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
static void sip_options_publish_contact_state(const struct sip_options_aor *aor_options, |
| 421 |
const struct ast_sip_contact_status *contact_status) |
| 422 |
{ |
| 423 |
int i; |
| 424 |
|
| 425 |
for (i = 0; i < AST_VECTOR_SIZE(&aor_options->compositors); ++i) { |
| 426 |
const struct sip_options_endpoint_state_compositor *endpoint_state_compositor; |
| 427 |
|
| 428 |
endpoint_state_compositor = AST_VECTOR_GET(&aor_options->compositors, i); |
| 429 |
ast_sip_persistent_endpoint_publish_contact_state(endpoint_state_compositor->name, |
| 430 |
contact_status); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
static int contact_status_publish_update_task(void *obj) |
| 439 |
{ |
| 440 |
struct ast_sip_contact_status *contact_status = obj; |
| 441 |
struct sip_options_aor *aor_options; |
| 442 |
|
| 443 |
aor_options = ao2_find(sip_options_aors, contact_status->aor, OBJ_SEARCH_KEY); |
| 444 |
if (aor_options) { |
| 445 |
sip_options_publish_contact_state(aor_options, contact_status); |
| 446 |
ao2_ref(aor_options, -1); |
| 447 |
} |
| 448 |
ao2_ref(contact_status, -1); |
| 449 |
|
| 450 |
return 0; |
| 451 |
} |
| 452 |
|
| 453 |
static void sip_options_contact_status_update(struct ast_sip_contact_status *contact_status) |
| 454 |
{ |
| 455 |
struct ast_taskprocessor *mgmt_serializer = management_serializer; |
| 456 |
|
| 457 |
if (mgmt_serializer) { |
| 458 |
ao2_ref(contact_status, +1); |
| 459 |
if (ast_sip_push_task(mgmt_serializer, contact_status_publish_update_task, |
| 460 |
contact_status)) { |
| 461 |
ao2_ref(contact_status, -1); |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
struct ast_sip_contact_status *ast_res_pjsip_find_or_create_contact_status(const struct ast_sip_contact *contact) |
| 467 |
{ |
| 468 |
struct ast_sip_contact_status *contact_status; |
| 469 |
int res; |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
if (!sip_options_contact_statuses) { |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
ast_assert(0); |
| 485 |
return NULL; |
| 486 |
} |
| 487 |
|
| 488 |
ao2_lock(sip_options_contact_statuses); |
| 489 |
|
| 490 |
|
| 491 |
contact_status = ao2_find(sip_options_contact_statuses, |
| 492 |
ast_sorcery_object_get_id(contact), OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 493 |
if (contact_status) { |
| 494 |
ao2_unlock(sip_options_contact_statuses); |
| 495 |
return contact_status; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
contact_status = sip_contact_status_alloc(ast_sorcery_object_get_id(contact)); |
| 500 |
if (!contact_status) { |
| 501 |
ao2_unlock(sip_options_contact_statuses); |
| 502 |
return NULL; |
| 503 |
} |
| 504 |
|
| 505 |
contact_status->rtt = 0; |
| 506 |
contact_status->status = CREATED; |
| 507 |
contact_status->last_status = CREATED; |
| 508 |
res = ast_string_field_set(contact_status, uri, contact->uri); |
| 509 |
res |= ast_string_field_set(contact_status, aor, contact->aor); |
| 510 |
if (res) { |
| 511 |
ao2_unlock(sip_options_contact_statuses); |
| 512 |
ao2_ref(contact_status, -1); |
| 513 |
return NULL; |
| 514 |
} |
| 515 |
|
| 516 |
ao2_link_flags(sip_options_contact_statuses, contact_status, OBJ_NOLOCK); |
| 517 |
ao2_unlock(sip_options_contact_statuses); |
| 518 |
|
| 519 |
ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, |
| 520 |
"+1", 1.0, ast_sip_get_contact_status_label(contact_status->status)); |
| 521 |
|
| 522 |
sip_options_contact_status_update(contact_status); |
| 523 |
|
| 524 |
return contact_status; |
| 525 |
} |
| 526 |
|
| 527 |
struct ast_sip_contact_status *ast_sip_get_contact_status(const struct ast_sip_contact *contact) |
| 528 |
{ |
| 529 |
return ao2_find(sip_options_contact_statuses, ast_sorcery_object_get_id(contact), |
| 530 |
OBJ_SEARCH_KEY); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
AO2_STRING_FIELD_HASH_FN(sip_options_aor, name); |
| 535 |
|
| 536 |
|
| 537 |
AO2_STRING_FIELD_CMP_FN(sip_options_aor, name); |
| 538 |
|
| 539 |
|
| 540 |
AO2_STRING_FIELD_HASH_FN(sip_options_endpoint_state_compositor, name); |
| 541 |
|
| 542 |
|
| 543 |
AO2_STRING_FIELD_CMP_FN(sip_options_endpoint_state_compositor, name); |
| 544 |
|
| 545 |
|
| 546 |
struct sip_options_contact_callback_data { |
| 547 |
|
| 548 |
struct ast_sip_contact *contact; |
| 549 |
|
| 550 |
struct sip_options_aor *aor_options; |
| 551 |
|
| 552 |
struct timeval rtt_start; |
| 553 |
|
| 554 |
enum ast_sip_contact_status_type status; |
| 555 |
}; |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
static enum ast_endpoint_state sip_options_get_endpoint_state_compositor_state( |
| 562 |
const struct sip_options_endpoint_state_compositor *endpoint_state_compositor) |
| 563 |
{ |
| 564 |
struct ao2_iterator it_aor_statuses; |
| 565 |
struct sip_options_endpoint_aor_status *aor_status; |
| 566 |
enum ast_endpoint_state state = AST_ENDPOINT_OFFLINE; |
| 567 |
|
| 568 |
it_aor_statuses = ao2_iterator_init(endpoint_state_compositor->aor_statuses, 0); |
| 569 |
for (; (aor_status = ao2_iterator_next(&it_aor_statuses)); ao2_ref(aor_status, -1)) { |
| 570 |
if (aor_status->available) { |
| 571 |
state = AST_ENDPOINT_ONLINE; |
| 572 |
ao2_ref(aor_status, -1); |
| 573 |
break; |
| 574 |
} |
| 575 |
} |
| 576 |
ao2_iterator_destroy(&it_aor_statuses); |
| 577 |
|
| 578 |
return state; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
static void sip_options_update_endpoint_state_compositor_aor(struct sip_options_endpoint_state_compositor *endpoint_state_compositor, |
| 586 |
const char *name, enum ast_sip_contact_status_type status) |
| 587 |
{ |
| 588 |
struct sip_options_endpoint_aor_status *aor_status; |
| 589 |
enum ast_endpoint_state endpoint_state; |
| 590 |
|
| 591 |
aor_status = ao2_find(endpoint_state_compositor->aor_statuses, name, |
| 592 |
OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 593 |
if (!aor_status) { |
| 594 |
|
| 595 |
if (status == REMOVED) { |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
aor_status = ao2_alloc_options(sizeof(*aor_status) + strlen(name) + 1, NULL, |
| 600 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 601 |
if (!aor_status) { |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
strcpy(aor_status->name, name); |
| 606 |
ao2_link(endpoint_state_compositor->aor_statuses, aor_status); |
| 607 |
} |
| 608 |
|
| 609 |
if (status == REMOVED) { |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
ao2_unlink(endpoint_state_compositor->aor_statuses, aor_status); |
| 615 |
} else { |
| 616 |
aor_status->available = (status == AVAILABLE ? 1 : 0); |
| 617 |
} |
| 618 |
ao2_ref(aor_status, -1); |
| 619 |
|
| 620 |
if (!endpoint_state_compositor->active) { |
| 621 |
return; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
if (status == AVAILABLE) { |
| 626 |
ast_debug(3, "Endpoint state compositor '%s' is online as AOR '%s' is available\n", |
| 627 |
endpoint_state_compositor->name, name); |
| 628 |
endpoint_state = AST_ENDPOINT_ONLINE; |
| 629 |
} else { |
| 630 |
endpoint_state = |
| 631 |
sip_options_get_endpoint_state_compositor_state(endpoint_state_compositor); |
| 632 |
} |
| 633 |
|
| 634 |
ast_sip_persistent_endpoint_update_state(endpoint_state_compositor->name, |
| 635 |
endpoint_state); |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
static void sip_options_notify_endpoint_state_compositors(struct sip_options_aor *aor_options, |
| 640 |
enum ast_sip_contact_status_type status) |
| 641 |
{ |
| 642 |
int i; |
| 643 |
|
| 644 |
|
| 645 |
for (i = 0; i < AST_VECTOR_SIZE(&aor_options->compositors); ++i) { |
| 646 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor; |
| 647 |
|
| 648 |
endpoint_state_compositor = AST_VECTOR_GET(&aor_options->compositors, i); |
| 649 |
|
| 650 |
ao2_lock(endpoint_state_compositor); |
| 651 |
sip_options_update_endpoint_state_compositor_aor(endpoint_state_compositor, |
| 652 |
aor_options->name, status); |
| 653 |
ao2_unlock(endpoint_state_compositor); |
| 654 |
} |
| 655 |
|
| 656 |
if (status == REMOVED) { |
| 657 |
AST_VECTOR_RESET(&aor_options->compositors, ao2_cleanup); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
static int sip_options_contact_status_notify_task(void *obj) |
| 666 |
{ |
| 667 |
struct sip_options_contact_callback_data *contact_callback_data = obj; |
| 668 |
struct ast_sip_contact *contact; |
| 669 |
struct ast_sip_contact_status *cs_old; |
| 670 |
struct ast_sip_contact_status *cs_new; |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
if (!contact_callback_data->aor_options->qualify_frequency) { |
| 680 |
|
| 681 |
ao2_ref(contact_callback_data, -1); |
| 682 |
return 0; |
| 683 |
} |
| 684 |
|
| 685 |
contact = ao2_find(contact_callback_data->aor_options->contacts, |
| 686 |
contact_callback_data->contact, OBJ_SEARCH_OBJECT); |
| 687 |
if (!contact) { |
| 688 |
|
| 689 |
ao2_ref(contact_callback_data, -1); |
| 690 |
return 0; |
| 691 |
} |
| 692 |
ao2_ref(contact, -1); |
| 693 |
|
| 694 |
cs_old = ao2_find(sip_options_contact_statuses, |
| 695 |
ast_sorcery_object_get_id(contact_callback_data->contact), OBJ_SEARCH_KEY); |
| 696 |
if (!cs_old) { |
| 697 |
|
| 698 |
ao2_ref(contact_callback_data, -1); |
| 699 |
return 0; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
cs_new = sip_contact_status_copy(cs_old); |
| 704 |
ao2_ref(cs_old, -1); |
| 705 |
if (!cs_new) { |
| 706 |
ao2_ref(contact_callback_data, -1); |
| 707 |
return 0; |
| 708 |
} |
| 709 |
cs_new->last_status = cs_new->status; |
| 710 |
cs_new->status = contact_callback_data->status; |
| 711 |
cs_new->rtt = |
| 712 |
cs_new->status == AVAILABLE |
| 713 |
? ast_tvdiff_us(ast_tvnow(), contact_callback_data->rtt_start) |
| 714 |
: 0; |
| 715 |
ao2_link(sip_options_contact_statuses, cs_new); |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
if (cs_new->last_status != cs_new->status) { |
| 722 |
if (cs_new->status == AVAILABLE) { |
| 723 |
|
| 724 |
++contact_callback_data->aor_options->available; |
| 725 |
if (contact_callback_data->aor_options->available == 1) { |
| 726 |
sip_options_notify_endpoint_state_compositors( |
| 727 |
contact_callback_data->aor_options, AVAILABLE); |
| 728 |
} |
| 729 |
} else if (cs_new->last_status == AVAILABLE) { |
| 730 |
ast_assert(cs_new->status == UNAVAILABLE); |
| 731 |
|
| 732 |
|
| 733 |
--contact_callback_data->aor_options->available; |
| 734 |
if (!contact_callback_data->aor_options->available) { |
| 735 |
sip_options_notify_endpoint_state_compositors( |
| 736 |
contact_callback_data->aor_options, UNAVAILABLE); |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
ast_verb(3, "Contact %s/%s is now %s. RTT: %.3f msec\n", |
| 741 |
cs_new->aor, |
| 742 |
cs_new->uri, |
| 743 |
ast_sip_get_contact_status_label(cs_new->status), |
| 744 |
cs_new->rtt / 1000.0); |
| 745 |
|
| 746 |
ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, |
| 747 |
"-1", 1.0, ast_sip_get_contact_status_label(cs_new->last_status)); |
| 748 |
ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, |
| 749 |
"+1", 1.0, ast_sip_get_contact_status_label(cs_new->status)); |
| 750 |
|
| 751 |
sip_options_contact_status_update(cs_new); |
| 752 |
|
| 753 |
ast_test_suite_event_notify("AOR_CONTACT_UPDATE", |
| 754 |
"Contact: %s\r\n" |
| 755 |
"Status: %s", |
| 756 |
cs_new->name, |
| 757 |
ast_sip_get_contact_status_label(cs_new->status)); |
| 758 |
} else { |
| 759 |
ast_debug(3, "Contact %s/%s status didn't change: %s, RTT: %.3f msec\n", |
| 760 |
cs_new->aor, |
| 761 |
cs_new->uri, |
| 762 |
ast_sip_get_contact_status_label(cs_new->status), |
| 763 |
cs_new->rtt / 1000.0); |
| 764 |
} |
| 765 |
|
| 766 |
ast_statsd_log_full_va("PJSIP.contacts.%s.rtt", AST_STATSD_TIMER, |
| 767 |
cs_new->status != AVAILABLE ? -1 : cs_new->rtt / 1000, |
| 768 |
1.0, |
| 769 |
cs_new->name); |
| 770 |
|
| 771 |
ast_test_suite_event_notify("AOR_CONTACT_QUALIFY_RESULT", |
| 772 |
"Contact: %s\r\n" |
| 773 |
"Status: %s\r\n" |
| 774 |
"RTT: %" PRId64, |
| 775 |
cs_new->name, |
| 776 |
ast_sip_get_contact_status_label(cs_new->status), |
| 777 |
cs_new->rtt); |
| 778 |
|
| 779 |
ast_debug(3, "AOR '%s' now has %d available contacts\n", |
| 780 |
contact_callback_data->aor_options->name, |
| 781 |
contact_callback_data->aor_options->available); |
| 782 |
|
| 783 |
ao2_ref(cs_new, -1); |
| 784 |
ao2_ref(contact_callback_data, -1); |
| 785 |
|
| 786 |
return 0; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
static void qualify_contact_cb(void *token, pjsip_event *e) |
| 791 |
{ |
| 792 |
struct sip_options_contact_callback_data *contact_callback_data = token; |
| 793 |
enum ast_sip_contact_status_type status; |
| 794 |
|
| 795 |
switch(e->body.tsx_state.type) { |
| 796 |
default: |
| 797 |
ast_log(LOG_ERROR, "Unexpected PJSIP event %u\n", e->body.tsx_state.type); |
| 798 |
|
| 799 |
case PJSIP_EVENT_TRANSPORT_ERROR: |
| 800 |
case PJSIP_EVENT_TIMER: |
| 801 |
status = UNAVAILABLE; |
| 802 |
break; |
| 803 |
case PJSIP_EVENT_RX_MSG: |
| 804 |
if (contact_callback_data->aor_options->qualify_2xx_only && |
| 805 |
(e->body.tsx_state.tsx->status_code < 200 || e->body.tsx_state.tsx->status_code >= 300)) { |
| 806 |
status = UNAVAILABLE; |
| 807 |
} else { |
| 808 |
status = AVAILABLE; |
| 809 |
} |
| 810 |
break; |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
contact_callback_data->status = status; |
| 815 |
|
| 816 |
if (ast_sip_push_task(contact_callback_data->aor_options->serializer, |
| 817 |
sip_options_contact_status_notify_task, contact_callback_data)) { |
| 818 |
ast_log(LOG_WARNING, "Unable to queue contact status update for '%s' on AOR '%s', state will be incorrect\n", |
| 819 |
ast_sorcery_object_get_id(contact_callback_data->contact), |
| 820 |
contact_callback_data->aor_options->name); |
| 821 |
ao2_ref(contact_callback_data, -1); |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
static void sip_options_contact_callback_data_dtor(void *obj) |
| 829 |
{ |
| 830 |
struct sip_options_contact_callback_data *contact_callback_data = obj; |
| 831 |
|
| 832 |
ao2_cleanup(contact_callback_data->contact); |
| 833 |
ao2_cleanup(contact_callback_data->aor_options); |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
static struct sip_options_contact_callback_data *sip_options_contact_callback_data_alloc( |
| 838 |
struct ast_sip_contact *contact, struct sip_options_aor *aor_options) |
| 839 |
{ |
| 840 |
struct sip_options_contact_callback_data *contact_callback_data; |
| 841 |
|
| 842 |
contact_callback_data = ao2_alloc_options(sizeof(*contact_callback_data), |
| 843 |
sip_options_contact_callback_data_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 844 |
if (!contact_callback_data) { |
| 845 |
return NULL; |
| 846 |
} |
| 847 |
|
| 848 |
contact_callback_data->contact = ao2_bump(contact); |
| 849 |
contact_callback_data->aor_options = ao2_bump(aor_options); |
| 850 |
contact_callback_data->rtt_start = ast_tvnow(); |
| 851 |
|
| 852 |
return contact_callback_data; |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
static int sip_options_qualify_contact(void *obj, void *arg, int flags) |
| 857 |
{ |
| 858 |
struct ast_sip_contact *contact = obj; |
| 859 |
struct sip_options_aor *aor_options = arg; |
| 860 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 861 |
pjsip_tx_data *tdata; |
| 862 |
struct ast_sip_contact_status *contact_status; |
| 863 |
struct sip_options_contact_callback_data *contact_callback_data; |
| 864 |
|
| 865 |
ast_debug(3, "Qualifying contact '%s' on AOR '%s'\n", |
| 866 |
ast_sorcery_object_get_id(contact), aor_options->name); |
| 867 |
|
| 868 |
if (!ast_strlen_zero(contact->endpoint_name)) { |
| 869 |
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", |
| 870 |
contact->endpoint_name); |
| 871 |
} |
| 872 |
if (!endpoint && AST_VECTOR_SIZE(&aor_options->compositors)) { |
| 873 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor; |
| 874 |
|
| 875 |
endpoint_state_compositor = AST_VECTOR_GET(&aor_options->compositors, 0); |
| 876 |
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", |
| 877 |
endpoint_state_compositor->name); |
| 878 |
} |
| 879 |
if (!endpoint) { |
| 880 |
ast_debug(3, "Could not find an endpoint to qualify contact '%s' on AOR '%s'\n", |
| 881 |
ast_sorcery_object_get_id(contact), aor_options->name); |
| 882 |
return 0; |
| 883 |
} |
| 884 |
|
| 885 |
if (ast_sip_create_request("OPTIONS", NULL, endpoint, NULL, contact, &tdata)) { |
| 886 |
ast_log(LOG_ERROR, "Unable to create request to qualify contact %s on AOR %s\n", |
| 887 |
contact->uri, aor_options->name); |
| 888 |
return 0; |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
if (!ast_strlen_zero(contact->outbound_proxy) && |
| 893 |
ast_sip_set_outbound_proxy(tdata, contact->outbound_proxy)) { |
| 894 |
ast_log(LOG_ERROR, "Unable to apply outbound proxy on request to qualify contact %s\n", |
| 895 |
contact->uri); |
| 896 |
pjsip_tx_data_dec_ref(tdata); |
| 897 |
return 0; |
| 898 |
} |
| 899 |
|
| 900 |
contact_status = ast_res_pjsip_find_or_create_contact_status(contact); |
| 901 |
if (!contact_status) { |
| 902 |
ast_log(LOG_ERROR, "Unable to retrieve contact status information for contact %s on AOR %s\n", |
| 903 |
contact->uri, aor_options->name); |
| 904 |
pjsip_tx_data_dec_ref(tdata); |
| 905 |
return 0; |
| 906 |
} |
| 907 |
ao2_ref(contact_status, -1); |
| 908 |
|
| 909 |
contact_callback_data = sip_options_contact_callback_data_alloc(contact, aor_options); |
| 910 |
if (!contact_callback_data) { |
| 911 |
ast_log(LOG_ERROR, "Unable to create object to contain callback data for contact %s on AOR %s\n", |
| 912 |
contact->uri, aor_options->name); |
| 913 |
pjsip_tx_data_dec_ref(tdata); |
| 914 |
return 0; |
| 915 |
} |
| 916 |
|
| 917 |
if (ast_sip_send_out_of_dialog_request(tdata, endpoint, |
| 918 |
(int)(aor_options->qualify_timeout * 1000), contact_callback_data, |
| 919 |
qualify_contact_cb)) { |
| 920 |
ast_log(LOG_ERROR, "Unable to send request to qualify contact %s on AOR %s\n", |
| 921 |
contact->uri, aor_options->name); |
| 922 |
ao2_ref(contact_callback_data, -1); |
| 923 |
} |
| 924 |
|
| 925 |
return 0; |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
static int sip_options_qualify_aor(void *obj) |
| 933 |
{ |
| 934 |
struct sip_options_aor *aor_options = obj; |
| 935 |
|
| 936 |
ast_debug(3, "Qualifying all contacts on AOR '%s'\n", aor_options->name); |
| 937 |
|
| 938 |
|
| 939 |
ao2_callback(aor_options->contacts, OBJ_NODATA, sip_options_qualify_contact, |
| 940 |
(struct sip_options_aor *) aor_options); |
| 941 |
|
| 942 |
|
| 943 |
return aor_options->qualify_frequency * 1000; |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
static int sip_options_remove_contact(void *obj, void *arg, int flags); |
| 948 |
|
| 949 |
|
| 950 |
static void sip_options_aor_dtor(void *obj) |
| 951 |
{ |
| 952 |
struct sip_options_aor *aor_options = obj; |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
if (aor_options->contacts) { |
| 959 |
ao2_callback(aor_options->contacts, OBJ_NODATA | OBJ_UNLINK, |
| 960 |
sip_options_remove_contact, aor_options); |
| 961 |
ao2_ref(aor_options->contacts, -1); |
| 962 |
} |
| 963 |
ao2_cleanup(aor_options->dynamic_contacts); |
| 964 |
|
| 965 |
ast_taskprocessor_unreference(aor_options->serializer); |
| 966 |
|
| 967 |
ast_assert(AST_VECTOR_SIZE(&aor_options->compositors) == 0); |
| 968 |
AST_VECTOR_FREE(&aor_options->compositors); |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
static struct sip_options_aor *sip_options_aor_alloc(struct ast_sip_aor *aor) |
| 973 |
{ |
| 974 |
struct sip_options_aor *aor_options; |
| 975 |
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1]; |
| 976 |
|
| 977 |
aor_options = ao2_alloc_options(sizeof(*aor_options) + strlen(ast_sorcery_object_get_id(aor)) + 1, |
| 978 |
sip_options_aor_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 979 |
if (!aor_options) { |
| 980 |
return NULL; |
| 981 |
} |
| 982 |
|
| 983 |
strcpy(aor_options->name, ast_sorcery_object_get_id(aor)); |
| 984 |
|
| 985 |
ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/options/%s", |
| 986 |
ast_sorcery_object_get_id(aor)); |
| 987 |
aor_options->serializer = ast_sip_create_serializer_group(tps_name, |
| 988 |
shutdown_group); |
| 989 |
if (!aor_options->serializer) { |
| 990 |
ao2_ref(aor_options, -1); |
| 991 |
return NULL; |
| 992 |
} |
| 993 |
|
| 994 |
if (AST_VECTOR_INIT(&aor_options->compositors, ENDPOINT_STATE_COMPOSITOR_INITIAL_SIZE)) { |
| 995 |
ao2_ref(aor_options, -1); |
| 996 |
return NULL; |
| 997 |
} |
| 998 |
|
| 999 |
aor_options->contacts = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, |
| 1000 |
AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, CONTACT_BUCKETS, ast_sorcery_object_id_hash, |
| 1001 |
ast_sorcery_object_id_sort, ast_sorcery_object_id_compare); |
| 1002 |
if (!aor_options->contacts) { |
| 1003 |
ao2_ref(aor_options, -1); |
| 1004 |
return NULL; |
| 1005 |
} |
| 1006 |
|
| 1007 |
aor_options->dynamic_contacts = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, |
| 1008 |
AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, CONTACT_BUCKETS, ast_sorcery_object_id_hash, |
| 1009 |
ast_sorcery_object_id_sort, ast_sorcery_object_id_compare); |
| 1010 |
if (!aor_options->dynamic_contacts) { |
| 1011 |
ao2_ref(aor_options, -1); |
| 1012 |
return NULL; |
| 1013 |
} |
| 1014 |
|
| 1015 |
return aor_options; |
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
static void sip_options_remove_contact_status(struct sip_options_aor *aor_options, |
| 1020 |
struct ast_sip_contact *contact) |
| 1021 |
{ |
| 1022 |
struct ast_sip_contact_status *cs_new; |
| 1023 |
struct ast_sip_contact_status *cs_old; |
| 1024 |
|
| 1025 |
cs_old = ao2_find(sip_options_contact_statuses, ast_sorcery_object_get_id(contact), |
| 1026 |
OBJ_SEARCH_KEY | OBJ_UNLINK); |
| 1027 |
if (!cs_old) { |
| 1028 |
ast_debug(3, "Attempted to remove contact status for '%s' but it does not exist\n", |
| 1029 |
ast_sorcery_object_get_id(contact)); |
| 1030 |
return; |
| 1031 |
} |
| 1032 |
|
| 1033 |
ast_verb(2, "Contact %s/%s has been deleted\n", contact->aor, contact->uri); |
| 1034 |
|
| 1035 |
|
| 1036 |
cs_new = sip_contact_status_copy(cs_old); |
| 1037 |
if (!cs_new) { |
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
cs_new = cs_old; |
| 1044 |
} else { |
| 1045 |
ao2_ref(cs_old, -1); |
| 1046 |
} |
| 1047 |
cs_new->last_status = cs_new->status; |
| 1048 |
cs_new->status = REMOVED; |
| 1049 |
cs_new->rtt = 0; |
| 1050 |
|
| 1051 |
ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, |
| 1052 |
"-1", 1.0, ast_sip_get_contact_status_label(cs_new->last_status)); |
| 1053 |
ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, |
| 1054 |
"+1", 1.0, ast_sip_get_contact_status_label(cs_new->status)); |
| 1055 |
|
| 1056 |
sip_options_contact_status_update(cs_new); |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
if (!aor_options->qualify_frequency || cs_new->last_status != AVAILABLE) { |
| 1064 |
ao2_ref(cs_new, -1); |
| 1065 |
return; |
| 1066 |
} |
| 1067 |
|
| 1068 |
--aor_options->available; |
| 1069 |
if (!aor_options->available) { |
| 1070 |
sip_options_notify_endpoint_state_compositors(aor_options, UNAVAILABLE); |
| 1071 |
} |
| 1072 |
|
| 1073 |
ast_debug(3, "AOR '%s' now has %d available contacts\n", aor_options->name, |
| 1074 |
aor_options->available); |
| 1075 |
|
| 1076 |
ao2_ref(cs_new, -1); |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
struct sip_options_synchronize_aor_task_data { |
| 1081 |
|
| 1082 |
struct sip_options_aor *aor_options; |
| 1083 |
|
| 1084 |
struct ast_sip_aor *aor; |
| 1085 |
|
| 1086 |
struct ao2_container *existing; |
| 1087 |
|
| 1088 |
int added; |
| 1089 |
}; |
| 1090 |
|
| 1091 |
|
| 1092 |
static int sip_options_remove_contact(void *obj, void *arg, int flags) |
| 1093 |
{ |
| 1094 |
struct ast_sip_contact *contact = obj; |
| 1095 |
struct sip_options_aor *aor_options = arg; |
| 1096 |
|
| 1097 |
sip_options_remove_contact_status(aor_options, contact); |
| 1098 |
|
| 1099 |
return CMP_MATCH; |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
static int sip_options_determine_initial_qualify_time(int qualify_frequency) |
| 1104 |
{ |
| 1105 |
int initial_interval; |
| 1106 |
int max_time = ast_sip_get_max_initial_qualify_time(); |
| 1107 |
|
| 1108 |
if (max_time && max_time < qualify_frequency) { |
| 1109 |
initial_interval = max_time; |
| 1110 |
} else { |
| 1111 |
initial_interval = qualify_frequency; |
| 1112 |
} |
| 1113 |
|
| 1114 |
initial_interval = (int)((initial_interval * 1000) * ast_random_double()); |
| 1115 |
return 0 < initial_interval ? initial_interval : 1; |
| 1116 |
} |
| 1117 |
|
| 1118 |
|
| 1119 |
static void sip_options_set_contact_status(struct ast_sip_contact_status *contact_status, |
| 1120 |
enum ast_sip_contact_status_type status) |
| 1121 |
{ |
| 1122 |
struct ast_sip_contact_status *cs_new; |
| 1123 |
|
| 1124 |
|
| 1125 |
cs_new = sip_contact_status_copy(contact_status); |
| 1126 |
if (!cs_new) { |
| 1127 |
return; |
| 1128 |
} |
| 1129 |
cs_new->last_status = cs_new->status; |
| 1130 |
cs_new->status = status; |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
cs_new->rtt = 0; |
| 1138 |
|
| 1139 |
ao2_link(sip_options_contact_statuses, cs_new); |
| 1140 |
|
| 1141 |
if (cs_new->status != cs_new->last_status) { |
| 1142 |
ast_verb(3, "Contact %s/%s is now %s.\n", |
| 1143 |
cs_new->aor, cs_new->uri, |
| 1144 |
ast_sip_get_contact_status_label(cs_new->status)); |
| 1145 |
|
| 1146 |
ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, |
| 1147 |
"-1", 1.0, ast_sip_get_contact_status_label(cs_new->last_status)); |
| 1148 |
ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, |
| 1149 |
"+1", 1.0, ast_sip_get_contact_status_label(cs_new->status)); |
| 1150 |
|
| 1151 |
sip_options_contact_status_update(cs_new); |
| 1152 |
|
| 1153 |
ast_test_suite_event_notify("AOR_CONTACT_UPDATE", |
| 1154 |
"Contact: %s\r\n" |
| 1155 |
"Status: %s", |
| 1156 |
cs_new->name, |
| 1157 |
ast_sip_get_contact_status_label(cs_new->status)); |
| 1158 |
} |
| 1159 |
ao2_ref(cs_new, -1); |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
static int sip_options_set_contact_status_unqualified(void *obj, void *arg, int flags) |
| 1164 |
{ |
| 1165 |
struct ast_sip_contact *contact = obj; |
| 1166 |
struct ast_sip_contact_status *contact_status; |
| 1167 |
|
| 1168 |
contact_status = ast_res_pjsip_find_or_create_contact_status(contact); |
| 1169 |
if (!contact_status) { |
| 1170 |
return 0; |
| 1171 |
} |
| 1172 |
|
| 1173 |
switch (contact_status->status) { |
| 1174 |
case AVAILABLE: |
| 1175 |
case UNAVAILABLE: |
| 1176 |
case UNKNOWN: |
| 1177 |
sip_options_set_contact_status(contact_status, CREATED); |
| 1178 |
break; |
| 1179 |
case CREATED: |
| 1180 |
case REMOVED: |
| 1181 |
break; |
| 1182 |
} |
| 1183 |
|
| 1184 |
ao2_ref(contact_status, -1); |
| 1185 |
|
| 1186 |
return 0; |
| 1187 |
} |
| 1188 |
|
| 1189 |
|
| 1190 |
static int sip_options_set_contact_status_qualified(void *obj, void *arg, int flags) |
| 1191 |
{ |
| 1192 |
struct ast_sip_contact *contact = obj; |
| 1193 |
struct ast_sip_contact_status *contact_status; |
| 1194 |
|
| 1195 |
contact_status = ast_res_pjsip_find_or_create_contact_status(contact); |
| 1196 |
if (!contact_status) { |
| 1197 |
return 0; |
| 1198 |
} |
| 1199 |
|
| 1200 |
switch (contact_status->status) { |
| 1201 |
case AVAILABLE: |
| 1202 |
sip_options_set_contact_status(contact_status, UNAVAILABLE); |
| 1203 |
break; |
| 1204 |
case UNAVAILABLE: |
| 1205 |
case UNKNOWN: |
| 1206 |
case CREATED: |
| 1207 |
case REMOVED: |
| 1208 |
break; |
| 1209 |
} |
| 1210 |
|
| 1211 |
ao2_ref(contact_status, -1); |
| 1212 |
|
| 1213 |
return 0; |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
static int sip_options_contact_status_available_count(void *obj, void *arg, int flags) |
| 1218 |
{ |
| 1219 |
struct ast_sip_contact *contact = obj; |
| 1220 |
unsigned int *available = arg; |
| 1221 |
struct ast_sip_contact_status *contact_status; |
| 1222 |
|
| 1223 |
contact_status = ast_res_pjsip_find_or_create_contact_status(contact); |
| 1224 |
if (!contact_status) { |
| 1225 |
return 0; |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
switch (contact_status->status) { |
| 1230 |
case AVAILABLE: |
| 1231 |
++*available; |
| 1232 |
break; |
| 1233 |
case UNAVAILABLE: |
| 1234 |
case UNKNOWN: |
| 1235 |
case CREATED: |
| 1236 |
case REMOVED: |
| 1237 |
break; |
| 1238 |
} |
| 1239 |
|
| 1240 |
ao2_ref(contact_status, -1); |
| 1241 |
|
| 1242 |
return 0; |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
static void sip_options_apply_aor_configuration(struct sip_options_aor *aor_options, |
| 1250 |
struct ast_sip_aor *aor, int is_new) |
| 1251 |
{ |
| 1252 |
struct ao2_container *existing_contacts; |
| 1253 |
struct ast_sip_contact *contact; |
| 1254 |
struct ao2_iterator iter; |
| 1255 |
|
| 1256 |
ast_debug(3, "Configuring AOR '%s' with current state of configuration and world\n", |
| 1257 |
aor_options->name); |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
existing_contacts = ao2_container_clone(aor_options->contacts, 0); |
| 1267 |
if (!existing_contacts) { |
| 1268 |
ast_log(LOG_WARNING, "Synchronization of AOR '%s' failed for qualify, retaining existing state\n", |
| 1269 |
aor_options->name); |
| 1270 |
return; |
| 1271 |
} |
| 1272 |
|
| 1273 |
ao2_callback(aor_options->contacts, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, |
| 1274 |
NULL, NULL); |
| 1275 |
|
| 1276 |
|
| 1277 |
if (aor->permanent_contacts) { |
| 1278 |
iter = ao2_iterator_init(aor->permanent_contacts, 0); |
| 1279 |
for (; (contact = ao2_iterator_next(&iter)); ao2_ref(contact, -1)) { |
| 1280 |
ao2_find(existing_contacts, ast_sorcery_object_get_id(contact), |
| 1281 |
OBJ_NODATA | OBJ_UNLINK | OBJ_SEARCH_KEY); |
| 1282 |
ao2_link(aor_options->contacts, contact); |
| 1283 |
} |
| 1284 |
ao2_iterator_destroy(&iter); |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
if (is_new) { |
| 1294 |
size_t prefix_len = strlen(ast_sorcery_object_get_id(aor)) + sizeof(";@") - 1; |
| 1295 |
char prefix[prefix_len + 1]; |
| 1296 |
struct ao2_container *contacts; |
| 1297 |
|
| 1298 |
sprintf(prefix, "%s;@", ast_sorcery_object_get_id(aor)); |
| 1299 |
contacts = ast_sorcery_retrieve_by_prefix(ast_sip_get_sorcery(), "contact", |
| 1300 |
prefix, prefix_len); |
| 1301 |
if (contacts) { |
| 1302 |
ao2_container_dup(aor_options->dynamic_contacts, contacts, 0); |
| 1303 |
ao2_ref(contacts, -1); |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
iter = ao2_iterator_init(aor_options->dynamic_contacts, 0); |
| 1309 |
for (; (contact = ao2_iterator_next(&iter)); ao2_ref(contact, -1)) { |
| 1310 |
ao2_find(existing_contacts, ast_sorcery_object_get_id(contact), |
| 1311 |
OBJ_NODATA | OBJ_UNLINK | OBJ_SEARCH_KEY); |
| 1312 |
ao2_link(aor_options->contacts, contact); |
| 1313 |
} |
| 1314 |
ao2_iterator_destroy(&iter); |
| 1315 |
|
| 1316 |
|
| 1317 |
ao2_callback(existing_contacts, OBJ_NODATA | OBJ_UNLINK, |
| 1318 |
sip_options_remove_contact, aor_options); |
| 1319 |
ao2_ref(existing_contacts, -1); |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
if (!aor->qualify_frequency) { |
| 1329 |
ao2_callback(aor_options->contacts, OBJ_NODATA, |
| 1330 |
sip_options_set_contact_status_unqualified, NULL); |
| 1331 |
aor_options->available = ao2_container_count(aor_options->contacts); |
| 1332 |
ast_debug(3, "AOR '%s' is unqualified, number of available contacts is therefore '%d'\n", |
| 1333 |
aor_options->name, aor_options->available); |
| 1334 |
} else if (!aor_options->qualify_frequency) { |
| 1335 |
ao2_callback(aor_options->contacts, OBJ_NODATA, |
| 1336 |
sip_options_set_contact_status_qualified, NULL); |
| 1337 |
aor_options->available = 0; |
| 1338 |
ast_debug(3, "AOR '%s' has transitioned from unqualified to qualified, reset available contacts to 0\n", |
| 1339 |
aor_options->name); |
| 1340 |
} else { |
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
aor_options->available = 0; |
| 1346 |
ao2_callback(aor_options->contacts, OBJ_NODATA, |
| 1347 |
sip_options_contact_status_available_count, &aor_options->available); |
| 1348 |
} |
| 1349 |
|
| 1350 |
aor_options->authenticate_qualify = aor->authenticate_qualify; |
| 1351 |
aor_options->qualify_2xx_only = aor->qualify_2xx_only; |
| 1352 |
aor_options->qualify_timeout = aor->qualify_timeout; |
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
if (aor_options->qualify_frequency != aor->qualify_frequency |
| 1362 |
|| (!aor_options->sched_task && ao2_container_count(aor_options->contacts)) |
| 1363 |
|| (aor_options->sched_task && !ao2_container_count(aor_options->contacts))) { |
| 1364 |
if (aor_options->sched_task) { |
| 1365 |
ast_sip_sched_task_cancel(aor_options->sched_task); |
| 1366 |
ao2_ref(aor_options->sched_task, -1); |
| 1367 |
aor_options->sched_task = NULL; |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
aor_options->qualify_frequency = aor->qualify_frequency; |
| 1372 |
if (aor_options->qualify_frequency |
| 1373 |
&& ao2_container_count(aor_options->contacts)) { |
| 1374 |
aor_options->sched_task = ast_sip_schedule_task(aor_options->serializer, |
| 1375 |
sip_options_determine_initial_qualify_time(aor_options->qualify_frequency), |
| 1376 |
sip_options_qualify_aor, ast_taskprocessor_name(aor_options->serializer), |
| 1377 |
aor_options, AST_SIP_SCHED_TASK_VARIABLE | AST_SIP_SCHED_TASK_DATA_AO2); |
| 1378 |
if (!aor_options->sched_task) { |
| 1379 |
ast_log(LOG_ERROR, "Unable to schedule qualify for contacts of AOR '%s'\n", |
| 1380 |
aor_options->name); |
| 1381 |
} |
| 1382 |
} |
| 1383 |
} |
| 1384 |
|
| 1385 |
ast_debug(3, "AOR '%s' now has %d available contacts\n", aor_options->name, |
| 1386 |
aor_options->available); |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
static int sip_options_synchronize_aor_task(void *obj) |
| 1394 |
{ |
| 1395 |
struct sip_options_synchronize_aor_task_data *task_data = obj; |
| 1396 |
int i; |
| 1397 |
|
| 1398 |
ast_debug(3, "Synchronizing AOR '%s' with current state of configuration and world\n", |
| 1399 |
task_data->aor_options->name); |
| 1400 |
|
| 1401 |
sip_options_apply_aor_configuration(task_data->aor_options, task_data->aor, |
| 1402 |
task_data->added); |
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
for (i = 0; i < AST_VECTOR_SIZE(&task_data->aor_options->compositors); ++i) { |
| 1412 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor; |
| 1413 |
|
| 1414 |
endpoint_state_compositor = AST_VECTOR_GET(&task_data->aor_options->compositors, i); |
| 1415 |
|
| 1416 |
ao2_lock(endpoint_state_compositor); |
| 1417 |
endpoint_state_compositor->active = 0; |
| 1418 |
sip_options_update_endpoint_state_compositor_aor(endpoint_state_compositor, |
| 1419 |
task_data->aor_options->name, REMOVED); |
| 1420 |
ao2_unlock(endpoint_state_compositor); |
| 1421 |
} |
| 1422 |
AST_VECTOR_RESET(&task_data->aor_options->compositors, ao2_cleanup); |
| 1423 |
|
| 1424 |
return 0; |
| 1425 |
} |
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
static int sip_options_synchronize_aor(void *obj, void *arg, int flags) |
| 1432 |
{ |
| 1433 |
struct sip_options_synchronize_aor_task_data task_data = { |
| 1434 |
.aor = obj, |
| 1435 |
.existing = arg, |
| 1436 |
}; |
| 1437 |
|
| 1438 |
task_data.aor_options = ao2_find(sip_options_aors, |
| 1439 |
ast_sorcery_object_get_id(task_data.aor), OBJ_SEARCH_KEY); |
| 1440 |
if (!task_data.aor_options) { |
| 1441 |
task_data.aor_options = sip_options_aor_alloc(task_data.aor); |
| 1442 |
if (!task_data.aor_options) { |
| 1443 |
return 0; |
| 1444 |
} |
| 1445 |
|
| 1446 |
task_data.added = 1; |
| 1447 |
|
| 1448 |
|
| 1449 |
sip_options_synchronize_aor_task(&task_data); |
| 1450 |
ao2_link(sip_options_aors, task_data.aor_options); |
| 1451 |
} else { |
| 1452 |
|
| 1453 |
ast_sip_push_task_wait_serializer(task_data.aor_options->serializer, |
| 1454 |
sip_options_synchronize_aor_task, &task_data); |
| 1455 |
} |
| 1456 |
|
| 1457 |
ao2_ref(task_data.aor_options, -1); |
| 1458 |
|
| 1459 |
if (task_data.existing) { |
| 1460 |
ao2_find(task_data.existing, ast_sorcery_object_get_id(task_data.aor), |
| 1461 |
OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA); |
| 1462 |
} |
| 1463 |
|
| 1464 |
return 0; |
| 1465 |
} |
| 1466 |
|
| 1467 |
|
| 1468 |
static void sip_options_endpoint_state_compositor_dtor(void *obj) |
| 1469 |
{ |
| 1470 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor = obj; |
| 1471 |
|
| 1472 |
ao2_cleanup(endpoint_state_compositor->aor_statuses); |
| 1473 |
} |
| 1474 |
|
| 1475 |
|
| 1476 |
AO2_STRING_FIELD_HASH_FN(sip_options_endpoint_aor_status, name); |
| 1477 |
|
| 1478 |
|
| 1479 |
AO2_STRING_FIELD_CMP_FN(sip_options_endpoint_aor_status, name); |
| 1480 |
|
| 1481 |
|
| 1482 |
static struct sip_options_endpoint_state_compositor *sip_options_endpoint_state_compositor_find_or_alloc(const struct ast_sip_endpoint *endpoint) |
| 1483 |
{ |
| 1484 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor; |
| 1485 |
|
| 1486 |
ao2_lock(sip_options_endpoint_state_compositors); |
| 1487 |
endpoint_state_compositor = ao2_find(sip_options_endpoint_state_compositors, |
| 1488 |
ast_sorcery_object_get_id(endpoint), OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 1489 |
if (endpoint_state_compositor) { |
| 1490 |
ao2_unlock(sip_options_endpoint_state_compositors); |
| 1491 |
return endpoint_state_compositor; |
| 1492 |
} |
| 1493 |
|
| 1494 |
endpoint_state_compositor = ao2_alloc(sizeof(*endpoint_state_compositor) |
| 1495 |
+ strlen(ast_sorcery_object_get_id(endpoint)) + 1, |
| 1496 |
sip_options_endpoint_state_compositor_dtor); |
| 1497 |
if (!endpoint_state_compositor) { |
| 1498 |
ao2_unlock(sip_options_endpoint_state_compositors); |
| 1499 |
return NULL; |
| 1500 |
} |
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
endpoint_state_compositor->aor_statuses = ao2_container_alloc_hash( |
| 1507 |
AO2_ALLOC_OPT_LOCK_NOLOCK, 0, AOR_STATUS_BUCKETS, |
| 1508 |
sip_options_endpoint_aor_status_hash_fn, NULL, |
| 1509 |
sip_options_endpoint_aor_status_cmp_fn); |
| 1510 |
if (!endpoint_state_compositor->aor_statuses) { |
| 1511 |
ao2_unlock(sip_options_endpoint_state_compositors); |
| 1512 |
ao2_ref(endpoint_state_compositor, -1); |
| 1513 |
return NULL; |
| 1514 |
} |
| 1515 |
|
| 1516 |
strcpy(endpoint_state_compositor->name, ast_sorcery_object_get_id(endpoint)); |
| 1517 |
|
| 1518 |
ao2_link_flags(sip_options_endpoint_state_compositors, endpoint_state_compositor, |
| 1519 |
OBJ_NOLOCK); |
| 1520 |
ao2_unlock(sip_options_endpoint_state_compositors); |
| 1521 |
|
| 1522 |
return endpoint_state_compositor; |
| 1523 |
} |
| 1524 |
|
| 1525 |
|
| 1526 |
struct sip_options_endpoint_compositor_task_data { |
| 1527 |
|
| 1528 |
struct sip_options_aor *aor_options; |
| 1529 |
|
| 1530 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor; |
| 1531 |
}; |
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
static int sip_options_endpoint_compositor_add_task(void *obj) |
| 1538 |
{ |
| 1539 |
struct sip_options_endpoint_compositor_task_data *task_data = obj; |
| 1540 |
|
| 1541 |
ast_debug(3, "Adding endpoint compositor '%s' to AOR '%s'\n", |
| 1542 |
task_data->endpoint_state_compositor->name, task_data->aor_options->name); |
| 1543 |
|
| 1544 |
ao2_ref(task_data->endpoint_state_compositor, +1); |
| 1545 |
if (AST_VECTOR_APPEND(&task_data->aor_options->compositors, |
| 1546 |
task_data->endpoint_state_compositor)) { |
| 1547 |
|
| 1548 |
ao2_ref(task_data->endpoint_state_compositor, -1); |
| 1549 |
return 0; |
| 1550 |
} |
| 1551 |
|
| 1552 |
ao2_lock(task_data->endpoint_state_compositor); |
| 1553 |
sip_options_update_endpoint_state_compositor_aor(task_data->endpoint_state_compositor, |
| 1554 |
task_data->aor_options->name, |
| 1555 |
task_data->aor_options->available ? AVAILABLE : UNAVAILABLE); |
| 1556 |
ao2_unlock(task_data->endpoint_state_compositor); |
| 1557 |
|
| 1558 |
return 0; |
| 1559 |
} |
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
static int sip_options_endpoint_compositor_remove_task(void *obj) |
| 1566 |
{ |
| 1567 |
struct sip_options_endpoint_compositor_task_data *task_data = obj; |
| 1568 |
int i; |
| 1569 |
|
| 1570 |
ast_debug(3, "Removing endpoint compositor '%s' from AOR '%s'\n", |
| 1571 |
task_data->endpoint_state_compositor->name, |
| 1572 |
task_data->aor_options->name); |
| 1573 |
|
| 1574 |
for (i = 0; i < AST_VECTOR_SIZE(&task_data->aor_options->compositors); ++i) { |
| 1575 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor; |
| 1576 |
|
| 1577 |
endpoint_state_compositor = AST_VECTOR_GET(&task_data->aor_options->compositors, i); |
| 1578 |
if (endpoint_state_compositor != task_data->endpoint_state_compositor) { |
| 1579 |
continue; |
| 1580 |
} |
| 1581 |
|
| 1582 |
AST_VECTOR_REMOVE(&task_data->aor_options->compositors, i, 0); |
| 1583 |
ao2_ref(endpoint_state_compositor, -1); |
| 1584 |
break; |
| 1585 |
} |
| 1586 |
|
| 1587 |
return 0; |
| 1588 |
} |
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
static int sip_options_synchronize_endpoint(void *obj, void *arg, int flags) |
| 1595 |
{ |
| 1596 |
struct ast_sip_endpoint *endpoint = obj; |
| 1597 |
struct ast_sip_aor *aor = arg; |
| 1598 |
char *aors; |
| 1599 |
char *aor_name; |
| 1600 |
struct sip_options_endpoint_compositor_task_data task_data = { NULL, }; |
| 1601 |
|
| 1602 |
if (ast_strlen_zero(endpoint->aors)) { |
| 1603 |
|
| 1604 |
ast_debug(3, "Endpoint '%s' is not interested in any AORs so not creating endpoint state compositor\n", |
| 1605 |
ast_sorcery_object_get_id(endpoint)); |
| 1606 |
return 0; |
| 1607 |
} |
| 1608 |
|
| 1609 |
ast_debug(3, "Synchronizing endpoint '%s' with AORs '%s'\n", |
| 1610 |
ast_sorcery_object_get_id(endpoint), endpoint->aors); |
| 1611 |
|
| 1612 |
aors = ast_strdupa(endpoint->aors); |
| 1613 |
while ((aor_name = ast_strip(strsep(&aors, ",")))) { |
| 1614 |
if (ast_strlen_zero(aor_name)) { |
| 1615 |
continue; |
| 1616 |
} |
| 1617 |
if (aor && strcasecmp(ast_sorcery_object_get_id(aor), aor_name)) { |
| 1618 |
ast_debug(3, "Filtered AOR '%s' on endpoint '%s' as we are looking for '%s'\n", |
| 1619 |
aor_name, ast_sorcery_object_get_id(endpoint), |
| 1620 |
ast_sorcery_object_get_id(aor)); |
| 1621 |
continue; |
| 1622 |
} |
| 1623 |
|
| 1624 |
task_data.aor_options = ao2_find(sip_options_aors, aor_name, OBJ_SEARCH_KEY); |
| 1625 |
if (!task_data.aor_options) { |
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
ast_debug(3, "Endpoint '%s' referenced invalid AOR '%s'\n", |
| 1631 |
ast_sorcery_object_get_id(endpoint), aor_name); |
| 1632 |
continue; |
| 1633 |
} |
| 1634 |
|
| 1635 |
if (!task_data.endpoint_state_compositor) { |
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
task_data.endpoint_state_compositor = |
| 1641 |
sip_options_endpoint_state_compositor_find_or_alloc(endpoint); |
| 1642 |
if (!task_data.endpoint_state_compositor) { |
| 1643 |
ast_log(LOG_WARNING, |
| 1644 |
"Could not create endpoint state compositor for '%s', endpoint state will be incorrect\n", |
| 1645 |
ast_sorcery_object_get_id(endpoint)); |
| 1646 |
ao2_ref(task_data.aor_options, -1); |
| 1647 |
ast_sip_persistent_endpoint_update_state(ast_sorcery_object_get_id(endpoint), |
| 1648 |
AST_ENDPOINT_OFFLINE); |
| 1649 |
return 0; |
| 1650 |
} |
| 1651 |
} |
| 1652 |
|
| 1653 |
|
| 1654 |
ast_sip_push_task_wait_serializer(task_data.aor_options->serializer, |
| 1655 |
sip_options_endpoint_compositor_add_task, &task_data); |
| 1656 |
|
| 1657 |
ao2_ref(task_data.aor_options, -1); |
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
if (aor) { |
| 1664 |
break; |
| 1665 |
} |
| 1666 |
} |
| 1667 |
|
| 1668 |
if (task_data.endpoint_state_compositor) { |
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
ao2_lock(task_data.endpoint_state_compositor); |
| 1674 |
task_data.endpoint_state_compositor->active = 1; |
| 1675 |
ast_sip_persistent_endpoint_update_state(ast_sorcery_object_get_id(endpoint), |
| 1676 |
sip_options_get_endpoint_state_compositor_state(task_data.endpoint_state_compositor)); |
| 1677 |
ao2_unlock(task_data.endpoint_state_compositor); |
| 1678 |
|
| 1679 |
ao2_ref(task_data.endpoint_state_compositor, -1); |
| 1680 |
} else if (!aor) { |
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
ast_debug(3, "Endpoint '%s' has no AORs feeding it, setting it to offline state as default\n", |
| 1686 |
ast_sorcery_object_get_id(endpoint)); |
| 1687 |
ast_sip_persistent_endpoint_update_state(ast_sorcery_object_get_id(endpoint), |
| 1688 |
AST_ENDPOINT_OFFLINE); |
| 1689 |
} |
| 1690 |
|
| 1691 |
return 0; |
| 1692 |
} |
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
static int sip_options_aor_remove_task(void *obj) |
| 1699 |
{ |
| 1700 |
struct sip_options_aor *aor_options = obj; |
| 1701 |
|
| 1702 |
sip_options_notify_endpoint_state_compositors(aor_options, REMOVED); |
| 1703 |
|
| 1704 |
if (aor_options->sched_task) { |
| 1705 |
ast_sip_sched_task_cancel(aor_options->sched_task); |
| 1706 |
ao2_ref(aor_options->sched_task, -1); |
| 1707 |
aor_options->sched_task = NULL; |
| 1708 |
} |
| 1709 |
|
| 1710 |
return 0; |
| 1711 |
} |
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
static int sip_options_unused_aor(void *obj, void *arg, int flags) |
| 1718 |
{ |
| 1719 |
struct sip_options_aor *aor_options = obj; |
| 1720 |
|
| 1721 |
ast_debug(3, "AOR '%s' is no longer configured, removing it\n", aor_options->name); |
| 1722 |
|
| 1723 |
ast_sip_push_task_wait_serializer(aor_options->serializer, sip_options_aor_remove_task, |
| 1724 |
aor_options); |
| 1725 |
ao2_unlink(sip_options_aors, aor_options); |
| 1726 |
|
| 1727 |
return CMP_MATCH; |
| 1728 |
} |
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
static int sip_options_unused_endpoint_state_compositor(void *obj, void *arg, int flags) |
| 1735 |
{ |
| 1736 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor = obj; |
| 1737 |
|
| 1738 |
if (ao2_container_count(endpoint_state_compositor->aor_statuses)) { |
| 1739 |
return 0; |
| 1740 |
} |
| 1741 |
|
| 1742 |
|
| 1743 |
ast_sip_persistent_endpoint_update_state(endpoint_state_compositor->name, |
| 1744 |
AST_ENDPOINT_OFFLINE); |
| 1745 |
|
| 1746 |
return CMP_MATCH; |
| 1747 |
} |
| 1748 |
|
| 1749 |
|
| 1750 |
struct sip_options_synchronize_task_data { |
| 1751 |
|
| 1752 |
int reload; |
| 1753 |
}; |
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
static int sip_options_synchronize_task(void *obj) |
| 1760 |
{ |
| 1761 |
struct sip_options_synchronize_task_data *task_data = obj; |
| 1762 |
struct ao2_container *existing = NULL; |
| 1763 |
struct ao2_container *objects; |
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
if (task_data->reload) { |
| 1770 |
existing = ao2_container_clone(sip_options_aors, 0); |
| 1771 |
if (!existing) { |
| 1772 |
return 0; |
| 1773 |
} |
| 1774 |
} |
| 1775 |
|
| 1776 |
objects = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "aor", |
| 1777 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 1778 |
if (objects) { |
| 1779 |
|
| 1780 |
ao2_callback(objects, OBJ_NODATA, sip_options_synchronize_aor, existing); |
| 1781 |
ao2_ref(objects, -1); |
| 1782 |
} |
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
if (existing) { |
| 1789 |
ao2_callback(existing, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, |
| 1790 |
sip_options_unused_aor, NULL); |
| 1791 |
ao2_ref(existing, -1); |
| 1792 |
} |
| 1793 |
|
| 1794 |
objects = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "endpoint", |
| 1795 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 1796 |
if (objects) { |
| 1797 |
|
| 1798 |
ao2_callback(objects, OBJ_NODATA, sip_options_synchronize_endpoint, NULL); |
| 1799 |
ao2_ref(objects, -1); |
| 1800 |
} |
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
ao2_callback(sip_options_endpoint_state_compositors, |
| 1808 |
OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, |
| 1809 |
sip_options_unused_endpoint_state_compositor, NULL); |
| 1810 |
|
| 1811 |
return 0; |
| 1812 |
} |
| 1813 |
|
| 1814 |
|
| 1815 |
static void sip_options_synchronize(int reload) |
| 1816 |
{ |
| 1817 |
struct sip_options_synchronize_task_data task_data = { |
| 1818 |
.reload = reload, |
| 1819 |
}; |
| 1820 |
|
| 1821 |
ast_sip_push_task_wait_serializer(management_serializer, sip_options_synchronize_task, |
| 1822 |
&task_data); |
| 1823 |
} |
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
static void sip_options_endpoint_unlink_aor_feeders(struct ast_sip_endpoint *endpoint, |
| 1830 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor) |
| 1831 |
{ |
| 1832 |
struct ao2_iterator it_aor_statuses; |
| 1833 |
struct sip_options_endpoint_aor_status *aor_status; |
| 1834 |
struct sip_options_endpoint_compositor_task_data task_data = { |
| 1835 |
.endpoint_state_compositor = endpoint_state_compositor, |
| 1836 |
}; |
| 1837 |
|
| 1838 |
ao2_lock(endpoint_state_compositor); |
| 1839 |
endpoint_state_compositor->active = 0; |
| 1840 |
|
| 1841 |
|
| 1842 |
it_aor_statuses = ao2_iterator_init(endpoint_state_compositor->aor_statuses, 0); |
| 1843 |
for (; (aor_status = ao2_iterator_next(&it_aor_statuses)); ao2_ref(aor_status, -1)) { |
| 1844 |
task_data.aor_options = ao2_find(sip_options_aors, aor_status->name, |
| 1845 |
OBJ_SEARCH_KEY); |
| 1846 |
if (!task_data.aor_options) { |
| 1847 |
continue; |
| 1848 |
} |
| 1849 |
|
| 1850 |
ast_debug(3, "Removing endpoint state compositor '%s' from AOR '%s'\n", |
| 1851 |
ast_sorcery_object_get_id(endpoint), aor_status->name); |
| 1852 |
ao2_unlock(endpoint_state_compositor); |
| 1853 |
ast_sip_push_task_wait_serializer(task_data.aor_options->serializer, |
| 1854 |
sip_options_endpoint_compositor_remove_task, &task_data); |
| 1855 |
ao2_lock(endpoint_state_compositor); |
| 1856 |
ao2_ref(task_data.aor_options, -1); |
| 1857 |
} |
| 1858 |
ao2_iterator_destroy(&it_aor_statuses); |
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
ao2_unlock(endpoint_state_compositor); |
| 1867 |
} |
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
static int sip_options_endpoint_observer_deleted_task(void *obj) |
| 1874 |
{ |
| 1875 |
struct ast_sip_endpoint *endpoint = obj; |
| 1876 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor; |
| 1877 |
|
| 1878 |
endpoint_state_compositor = ao2_find(sip_options_endpoint_state_compositors, |
| 1879 |
ast_sorcery_object_get_id(endpoint), OBJ_SEARCH_KEY | OBJ_UNLINK); |
| 1880 |
if (!endpoint_state_compositor) { |
| 1881 |
return 0; |
| 1882 |
} |
| 1883 |
|
| 1884 |
ast_debug(3, "Endpoint '%s' has been deleted, removing endpoint state compositor from AORs\n", |
| 1885 |
ast_sorcery_object_get_id(endpoint)); |
| 1886 |
sip_options_endpoint_unlink_aor_feeders(endpoint, endpoint_state_compositor); |
| 1887 |
ao2_ref(endpoint_state_compositor, -1); |
| 1888 |
|
| 1889 |
return 0; |
| 1890 |
} |
| 1891 |
|
| 1892 |
|
| 1893 |
static void endpoint_observer_deleted(const void *obj) |
| 1894 |
{ |
| 1895 |
ast_sip_push_task_wait_serializer(management_serializer, |
| 1896 |
sip_options_endpoint_observer_deleted_task, (void *) obj); |
| 1897 |
} |
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
static int sip_options_endpoint_observer_modified_task(void *obj) |
| 1904 |
{ |
| 1905 |
struct ast_sip_endpoint *endpoint = obj; |
| 1906 |
struct sip_options_endpoint_state_compositor *endpoint_state_compositor; |
| 1907 |
|
| 1908 |
ast_debug(3, "Endpoint '%s' has been created or modified, updating state\n", |
| 1909 |
ast_sorcery_object_get_id(endpoint)); |
| 1910 |
|
| 1911 |
endpoint_state_compositor = ao2_find(sip_options_endpoint_state_compositors, |
| 1912 |
ast_sorcery_object_get_id(endpoint), OBJ_SEARCH_KEY | OBJ_UNLINK); |
| 1913 |
if (endpoint_state_compositor) { |
| 1914 |
|
| 1915 |
sip_options_endpoint_unlink_aor_feeders(endpoint, endpoint_state_compositor); |
| 1916 |
ao2_ref(endpoint_state_compositor, -1); |
| 1917 |
} |
| 1918 |
|
| 1919 |
|
| 1920 |
sip_options_synchronize_endpoint(endpoint, NULL, 0); |
| 1921 |
return 0; |
| 1922 |
} |
| 1923 |
|
| 1924 |
|
| 1925 |
static void endpoint_observer_modified(const void *obj) |
| 1926 |
{ |
| 1927 |
ast_sip_push_task_wait_serializer(management_serializer, |
| 1928 |
sip_options_endpoint_observer_modified_task, (void *)obj); |
| 1929 |
} |
| 1930 |
|
| 1931 |
|
| 1932 |
static const struct ast_sorcery_observer endpoint_observer_callbacks = { |
| 1933 |
.created = endpoint_observer_modified, |
| 1934 |
.updated = endpoint_observer_modified, |
| 1935 |
.deleted = endpoint_observer_deleted, |
| 1936 |
}; |
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
static int sip_options_update_aor_task(void *obj) |
| 1943 |
{ |
| 1944 |
struct sip_options_synchronize_aor_task_data *task_data = obj; |
| 1945 |
int available = task_data->aor_options->available; |
| 1946 |
|
| 1947 |
ast_debug(3, "Individually updating AOR '%s' with current state of configuration and world\n", |
| 1948 |
task_data->aor_options->name); |
| 1949 |
|
| 1950 |
sip_options_apply_aor_configuration(task_data->aor_options, task_data->aor, |
| 1951 |
task_data->added); |
| 1952 |
|
| 1953 |
if (!available && task_data->aor_options->available) { |
| 1954 |
ast_debug(3, "After modifying AOR '%s' it has now become available\n", |
| 1955 |
task_data->aor_options->name); |
| 1956 |
sip_options_notify_endpoint_state_compositors(task_data->aor_options, AVAILABLE); |
| 1957 |
} else if (available && !task_data->aor_options->available) { |
| 1958 |
ast_debug(3, "After modifying AOR '%s' it has become unavailable\n", |
| 1959 |
task_data->aor_options->name); |
| 1960 |
sip_options_notify_endpoint_state_compositors(task_data->aor_options, UNAVAILABLE); |
| 1961 |
} |
| 1962 |
|
| 1963 |
return 0; |
| 1964 |
} |
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
static int sip_options_aor_observer_modified_task(void *obj) |
| 1971 |
{ |
| 1972 |
struct ast_sip_aor *aor = obj; |
| 1973 |
struct sip_options_aor *aor_options; |
| 1974 |
|
| 1975 |
aor_options = ao2_find(sip_options_aors, ast_sorcery_object_get_id(aor), |
| 1976 |
OBJ_SEARCH_KEY); |
| 1977 |
if (!aor_options) { |
| 1978 |
struct ao2_container *endpoints; |
| 1979 |
|
| 1980 |
aor_options = sip_options_aor_alloc(aor); |
| 1981 |
if (!aor_options) { |
| 1982 |
return 0; |
| 1983 |
} |
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
sip_options_apply_aor_configuration(aor_options, aor, 1); |
| 1992 |
ao2_link(sip_options_aors, aor_options); |
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
endpoints = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), |
| 1999 |
"endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 2000 |
if (endpoints) { |
| 2001 |
ao2_callback(endpoints, OBJ_NODATA, sip_options_synchronize_endpoint, aor); |
| 2002 |
ao2_ref(endpoints, -1); |
| 2003 |
} |
| 2004 |
} else { |
| 2005 |
struct sip_options_synchronize_aor_task_data task_data = { |
| 2006 |
.aor_options = aor_options, |
| 2007 |
.aor = aor, |
| 2008 |
}; |
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
ast_sip_push_task_wait_serializer(aor_options->serializer, |
| 2016 |
sip_options_update_aor_task, &task_data); |
| 2017 |
} |
| 2018 |
|
| 2019 |
ao2_ref(aor_options, -1); |
| 2020 |
|
| 2021 |
return 0; |
| 2022 |
} |
| 2023 |
|
| 2024 |
|
| 2025 |
static void aor_observer_modified(const void *obj) |
| 2026 |
{ |
| 2027 |
ast_sip_push_task_wait_serializer(management_serializer, |
| 2028 |
sip_options_aor_observer_modified_task, (void *) obj); |
| 2029 |
} |
| 2030 |
|
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
static int sip_options_aor_observer_deleted_task(void *obj) |
| 2036 |
{ |
| 2037 |
struct ast_sip_aor *aor = obj; |
| 2038 |
struct sip_options_aor *aor_options; |
| 2039 |
|
| 2040 |
aor_options = ao2_find(sip_options_aors, ast_sorcery_object_get_id(aor), |
| 2041 |
OBJ_SEARCH_KEY | OBJ_UNLINK); |
| 2042 |
if (!aor_options) { |
| 2043 |
return 0; |
| 2044 |
} |
| 2045 |
|
| 2046 |
ast_debug(3, "AOR '%s' has been deleted, removing it\n", aor_options->name); |
| 2047 |
|
| 2048 |
ast_sip_push_task_wait_serializer(aor_options->serializer, sip_options_aor_remove_task, |
| 2049 |
aor_options); |
| 2050 |
ao2_ref(aor_options, -1); |
| 2051 |
|
| 2052 |
return 0; |
| 2053 |
} |
| 2054 |
|
| 2055 |
|
| 2056 |
static void aor_observer_deleted(const void *obj) |
| 2057 |
{ |
| 2058 |
ast_sip_push_task_wait_serializer(management_serializer, |
| 2059 |
sip_options_aor_observer_deleted_task, (void *) obj); |
| 2060 |
} |
| 2061 |
|
| 2062 |
|
| 2063 |
static const struct ast_sorcery_observer aor_observer_callbacks = { |
| 2064 |
.created = aor_observer_modified, |
| 2065 |
.updated = aor_observer_modified, |
| 2066 |
.deleted = aor_observer_deleted, |
| 2067 |
}; |
| 2068 |
|
| 2069 |
|
| 2070 |
struct sip_options_contact_observer_task_data { |
| 2071 |
|
| 2072 |
struct sip_options_aor *aor_options; |
| 2073 |
|
| 2074 |
struct ast_sip_contact *contact; |
| 2075 |
}; |
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 |
|
| 2080 |
|
| 2081 |
static int has_qualify_changed (const struct ast_sip_contact *contact, const struct sip_options_aor *aor_options) |
| 2082 |
{ |
| 2083 |
if (!contact) { |
| 2084 |
return 0; |
| 2085 |
} |
| 2086 |
|
| 2087 |
if (!aor_options) { |
| 2088 |
if (contact->qualify_frequency) { |
| 2089 |
return 1; |
| 2090 |
} |
| 2091 |
} else if (contact->qualify_frequency != aor_options->qualify_frequency |
| 2092 |
|| contact->authenticate_qualify != aor_options->authenticate_qualify |
| 2093 |
|| contact->qualify_2xx_only != aor_options->qualify_2xx_only |
| 2094 |
|| ((int)(contact->qualify_timeout * 1000)) != ((int)(aor_options->qualify_timeout * 1000))) { |
| 2095 |
return 1; |
| 2096 |
} |
| 2097 |
|
| 2098 |
return 0; |
| 2099 |
} |
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
|
| 2105 |
static int sip_options_contact_add_task(void *obj) |
| 2106 |
{ |
| 2107 |
struct sip_options_contact_observer_task_data *task_data = obj; |
| 2108 |
struct ast_sip_contact_status *contact_status; |
| 2109 |
|
| 2110 |
ao2_link(task_data->aor_options->dynamic_contacts, task_data->contact); |
| 2111 |
ao2_link(task_data->aor_options->contacts, task_data->contact); |
| 2112 |
|
| 2113 |
contact_status = ast_res_pjsip_find_or_create_contact_status(task_data->contact); |
| 2114 |
ao2_cleanup(contact_status); |
| 2115 |
|
| 2116 |
if (task_data->aor_options->qualify_frequency) { |
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
ast_debug(3, "Starting scheduled callback on AOR '%s' for qualifying as there is now a contact on it\n", |
| 2122 |
task_data->aor_options->name); |
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
if (task_data->aor_options->sched_task) { |
| 2130 |
ast_sip_sched_task_cancel(task_data->aor_options->sched_task); |
| 2131 |
ao2_ref(task_data->aor_options->sched_task, -1); |
| 2132 |
task_data->aor_options->sched_task = NULL; |
| 2133 |
} |
| 2134 |
task_data->aor_options->sched_task = ast_sip_schedule_task( |
| 2135 |
task_data->aor_options->serializer, 1, sip_options_qualify_aor, |
| 2136 |
ast_taskprocessor_name(task_data->aor_options->serializer), |
| 2137 |
task_data->aor_options, |
| 2138 |
AST_SIP_SCHED_TASK_VARIABLE | AST_SIP_SCHED_TASK_DATA_AO2); |
| 2139 |
if (!task_data->aor_options->sched_task) { |
| 2140 |
ast_log(LOG_ERROR, "Unable to schedule qualify for contacts of AOR '%s'\n", |
| 2141 |
task_data->aor_options->name); |
| 2142 |
} |
| 2143 |
} else { |
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
task_data->aor_options->available = |
| 2149 |
ao2_container_count(task_data->aor_options->contacts); |
| 2150 |
if (task_data->aor_options->available == 1) { |
| 2151 |
ast_debug(3, "An unqualified contact has been added to AOR '%s' so it is now available\n", |
| 2152 |
task_data->aor_options->name); |
| 2153 |
sip_options_notify_endpoint_state_compositors(task_data->aor_options, |
| 2154 |
AVAILABLE); |
| 2155 |
} |
| 2156 |
} |
| 2157 |
|
| 2158 |
return 0; |
| 2159 |
} |
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
static int sip_options_contact_add_management_task(void *obj) |
| 2166 |
{ |
| 2167 |
struct sip_options_contact_observer_task_data task_data; |
| 2168 |
|
| 2169 |
task_data.contact = obj; |
| 2170 |
task_data.aor_options = ao2_find(sip_options_aors, task_data.contact->aor, |
| 2171 |
OBJ_SEARCH_KEY); |
| 2172 |
|
| 2173 |
if (has_qualify_changed(task_data.contact, task_data.aor_options)) { |
| 2174 |
struct ast_sip_aor *aor; |
| 2175 |
|
| 2176 |
aor = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", |
| 2177 |
task_data.contact->aor); |
| 2178 |
if (aor) { |
| 2179 |
ast_debug(3, "AOR '%s' qualify options have been modified. Synchronize an AOR local state\n", |
| 2180 |
task_data.contact->aor); |
| 2181 |
sip_options_aor_observer_modified_task(aor); |
| 2182 |
ao2_ref(aor, -1); |
| 2183 |
} |
| 2184 |
} |
| 2185 |
|
| 2186 |
if (!task_data.aor_options) { |
| 2187 |
return 0; |
| 2188 |
} |
| 2189 |
|
| 2190 |
ast_sip_push_task_wait_serializer(task_data.aor_options->serializer, |
| 2191 |
sip_options_contact_add_task, &task_data); |
| 2192 |
ao2_ref(task_data.aor_options, -1); |
| 2193 |
|
| 2194 |
return 0; |
| 2195 |
} |
| 2196 |
|
| 2197 |
|
| 2198 |
static void contact_observer_created(const void *obj) |
| 2199 |
{ |
| 2200 |
ast_sip_push_task_wait_serializer(management_serializer, |
| 2201 |
sip_options_contact_add_management_task, (void *) obj); |
| 2202 |
} |
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
static int sip_options_contact_update_task(void *obj) |
| 2209 |
{ |
| 2210 |
struct sip_options_contact_observer_task_data *task_data = obj; |
| 2211 |
struct ast_sip_contact_status *contact_status; |
| 2212 |
|
| 2213 |
contact_status = ast_sip_get_contact_status(task_data->contact); |
| 2214 |
if (contact_status) { |
| 2215 |
switch (contact_status->status) { |
| 2216 |
case CREATED: |
| 2217 |
case UNAVAILABLE: |
| 2218 |
case AVAILABLE: |
| 2219 |
case UNKNOWN: |
| 2220 |
|
| 2221 |
sip_options_contact_status_update(contact_status); |
| 2222 |
break; |
| 2223 |
case REMOVED: |
| 2224 |
break; |
| 2225 |
} |
| 2226 |
ao2_ref(contact_status, -1); |
| 2227 |
} |
| 2228 |
|
| 2229 |
ao2_ref(task_data->contact, -1); |
| 2230 |
ao2_ref(task_data->aor_options, -1); |
| 2231 |
ast_free(task_data); |
| 2232 |
return 0; |
| 2233 |
} |
| 2234 |
|
| 2235 |
|
| 2236 |
static void contact_observer_updated(const void *obj) |
| 2237 |
{ |
| 2238 |
const struct ast_sip_contact *contact = obj; |
| 2239 |
struct sip_options_aor *aor_options = ao2_find(sip_options_aors, contact->aor, OBJ_SEARCH_KEY); |
| 2240 |
|
| 2241 |
if (has_qualify_changed(contact, aor_options)) { |
| 2242 |
struct ast_sip_aor *aor; |
| 2243 |
|
| 2244 |
aor = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", |
| 2245 |
contact->aor); |
| 2246 |
if (aor) { |
| 2247 |
ast_debug(3, "AOR '%s' qualify options have been modified. Synchronize an AOR local state\n", |
| 2248 |
contact->aor); |
| 2249 |
ast_sip_push_task_wait_serializer(management_serializer, |
| 2250 |
sip_options_aor_observer_modified_task, aor); |
| 2251 |
ao2_ref(aor, -1); |
| 2252 |
} |
| 2253 |
} |
| 2254 |
|
| 2255 |
if (aor_options && ast_sip_get_send_contact_status_on_update_registration()) { |
| 2256 |
struct sip_options_contact_observer_task_data *task_data; |
| 2257 |
|
| 2258 |
task_data = ast_malloc(sizeof(*task_data)); |
| 2259 |
if (!task_data) { |
| 2260 |
ao2_ref(aor_options, -1); |
| 2261 |
return; |
| 2262 |
} |
| 2263 |
|
| 2264 |
task_data->contact = (struct ast_sip_contact *) contact; |
| 2265 |
|
| 2266 |
task_data->aor_options = aor_options; |
| 2267 |
|
| 2268 |
ao2_ref(task_data->contact, +1); |
| 2269 |
if (ast_sip_push_task(task_data->aor_options->serializer, |
| 2270 |
sip_options_contact_update_task, task_data)) { |
| 2271 |
ao2_ref(task_data->contact, -1); |
| 2272 |
ao2_ref(task_data->aor_options, -1); |
| 2273 |
ast_free(task_data); |
| 2274 |
} |
| 2275 |
} else { |
| 2276 |
ao2_cleanup(aor_options); |
| 2277 |
} |
| 2278 |
} |
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
static int sip_options_contact_delete_task(void *obj) |
| 2285 |
{ |
| 2286 |
struct sip_options_contact_observer_task_data *task_data = obj; |
| 2287 |
|
| 2288 |
ao2_find(task_data->aor_options->dynamic_contacts, task_data->contact, |
| 2289 |
OBJ_NODATA | OBJ_UNLINK | OBJ_SEARCH_OBJECT); |
| 2290 |
ao2_find(task_data->aor_options->contacts, task_data->contact, |
| 2291 |
OBJ_NODATA | OBJ_UNLINK | OBJ_SEARCH_OBJECT); |
| 2292 |
|
| 2293 |
sip_options_remove_contact_status(task_data->aor_options, task_data->contact); |
| 2294 |
|
| 2295 |
if (task_data->aor_options->qualify_frequency) { |
| 2296 |
|
| 2297 |
if (!ao2_container_count(task_data->aor_options->contacts)) { |
| 2298 |
ast_debug(3, "Terminating scheduled callback on AOR '%s' as there are no contacts to qualify\n", |
| 2299 |
task_data->aor_options->name); |
| 2300 |
if (task_data->aor_options->sched_task) { |
| 2301 |
ast_sip_sched_task_cancel(task_data->aor_options->sched_task); |
| 2302 |
ao2_ref(task_data->aor_options->sched_task, -1); |
| 2303 |
task_data->aor_options->sched_task = NULL; |
| 2304 |
} |
| 2305 |
} |
| 2306 |
} else { |
| 2307 |
task_data->aor_options->available = |
| 2308 |
ao2_container_count(task_data->aor_options->contacts); |
| 2309 |
if (!task_data->aor_options->available) { |
| 2310 |
ast_debug(3, "An unqualified contact has been removed from AOR '%s' leaving no remaining contacts\n", |
| 2311 |
task_data->aor_options->name); |
| 2312 |
sip_options_notify_endpoint_state_compositors(task_data->aor_options, |
| 2313 |
UNAVAILABLE); |
| 2314 |
} |
| 2315 |
} |
| 2316 |
|
| 2317 |
return 0; |
| 2318 |
} |
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
static int sip_options_contact_delete_management_task(void *obj) |
| 2325 |
{ |
| 2326 |
struct sip_options_contact_observer_task_data task_data; |
| 2327 |
|
| 2328 |
task_data.contact = obj; |
| 2329 |
task_data.aor_options = ao2_find(sip_options_aors, task_data.contact->aor, |
| 2330 |
OBJ_SEARCH_KEY); |
| 2331 |
if (!task_data.aor_options) { |
| 2332 |
|
| 2333 |
return 0; |
| 2334 |
} |
| 2335 |
|
| 2336 |
ast_sip_push_task_wait_serializer(task_data.aor_options->serializer, |
| 2337 |
sip_options_contact_delete_task, &task_data); |
| 2338 |
ao2_ref(task_data.aor_options, -1); |
| 2339 |
|
| 2340 |
return 0; |
| 2341 |
} |
| 2342 |
|
| 2343 |
|
| 2344 |
static void contact_observer_deleted(const void *obj) |
| 2345 |
{ |
| 2346 |
ast_sip_push_task_wait_serializer(management_serializer, |
| 2347 |
sip_options_contact_delete_management_task, (void *) obj); |
| 2348 |
} |
| 2349 |
|
| 2350 |
|
| 2351 |
static const struct ast_sorcery_observer contact_observer_callbacks = { |
| 2352 |
.created = contact_observer_created, |
| 2353 |
.updated = contact_observer_updated, |
| 2354 |
.deleted = contact_observer_deleted, |
| 2355 |
}; |
| 2356 |
|
| 2357 |
static char *cli_qualify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2358 |
{ |
| 2359 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 2360 |
const char *endpoint_name; |
| 2361 |
char *aors; |
| 2362 |
char *aor_name; |
| 2363 |
|
| 2364 |
switch (cmd) { |
| 2365 |
case CLI_INIT: |
| 2366 |
e->command = "pjsip qualify"; |
| 2367 |
e->usage = |
| 2368 |
"Usage: pjsip qualify <endpoint>\n" |
| 2369 |
" Send a SIP OPTIONS request to all contacts on the endpoint.\n"; |
| 2370 |
return NULL; |
| 2371 |
case CLI_GENERATE: |
| 2372 |
return NULL; |
| 2373 |
} |
| 2374 |
|
| 2375 |
if (a->argc != 3) { |
| 2376 |
return CLI_SHOWUSAGE; |
| 2377 |
} |
| 2378 |
|
| 2379 |
endpoint_name = a->argv[2]; |
| 2380 |
|
| 2381 |
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", |
| 2382 |
endpoint_name); |
| 2383 |
if (!endpoint) { |
| 2384 |
ast_cli(a->fd, "Unable to retrieve endpoint %s\n", endpoint_name); |
| 2385 |
return CLI_FAILURE; |
| 2386 |
} |
| 2387 |
|
| 2388 |
if (ast_strlen_zero(endpoint->aors)) { |
| 2389 |
ast_cli(a->fd, "No AORs configured for endpoint '%s'\n", endpoint_name); |
| 2390 |
return CLI_FAILURE; |
| 2391 |
} |
| 2392 |
|
| 2393 |
aors = ast_strdupa(endpoint->aors); |
| 2394 |
while ((aor_name = ast_strip(strsep(&aors, ",")))) { |
| 2395 |
struct sip_options_aor *aor_options; |
| 2396 |
|
| 2397 |
aor_options = ao2_find(sip_options_aors, aor_name, OBJ_SEARCH_KEY); |
| 2398 |
if (!aor_options) { |
| 2399 |
continue; |
| 2400 |
} |
| 2401 |
|
| 2402 |
ast_cli(a->fd, "Qualifying AOR '%s' on endpoint '%s'\n", aor_name, endpoint_name); |
| 2403 |
ast_sip_push_task_wait_serializer(aor_options->serializer, sip_options_qualify_aor, |
| 2404 |
aor_options); |
| 2405 |
ao2_ref(aor_options, -1); |
| 2406 |
} |
| 2407 |
|
| 2408 |
return CLI_SUCCESS; |
| 2409 |
} |
| 2410 |
|
| 2411 |
static struct ao2_container *get_all_contacts(void) |
| 2412 |
{ |
| 2413 |
struct ao2_container *contacts; |
| 2414 |
|
| 2415 |
contacts = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "contact", |
| 2416 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 2417 |
|
| 2418 |
return contacts; |
| 2419 |
} |
| 2420 |
|
| 2421 |
static int sip_contact_to_ami(const struct ast_sip_contact *contact, |
| 2422 |
struct ast_str **buf) |
| 2423 |
{ |
| 2424 |
return ast_sip_sorcery_object_to_ami(contact, buf); |
| 2425 |
} |
| 2426 |
|
| 2427 |
static int format_ami_contactlist_handler(void *obj, void *arg, int flags) |
| 2428 |
{ |
| 2429 |
struct ast_sip_contact *contact = obj; |
| 2430 |
struct ast_sip_ami *ami = arg; |
| 2431 |
struct ast_str *buf; |
| 2432 |
struct ast_sip_contact_status *status; |
| 2433 |
|
| 2434 |
buf = ast_sip_create_ami_event("ContactList", ami); |
| 2435 |
if (!buf) { |
| 2436 |
return CMP_STOP; |
| 2437 |
} |
| 2438 |
|
| 2439 |
if (sip_contact_to_ami(contact, &buf)) { |
| 2440 |
ast_free(buf); |
| 2441 |
return CMP_STOP; |
| 2442 |
} |
| 2443 |
|
| 2444 |
|
| 2445 |
status = ast_sip_get_contact_status(contact); |
| 2446 |
ast_str_append(&buf, 0, "Status: %s\r\n", |
| 2447 |
ast_sip_get_contact_status_label(status ? status->status : UNKNOWN)); |
| 2448 |
if (!status || status->status != AVAILABLE) { |
| 2449 |
ast_str_append(&buf, 0, "RoundtripUsec: N/A\r\n"); |
| 2450 |
} else { |
| 2451 |
ast_str_append(&buf, 0, "RoundtripUsec: %" PRId64 "\r\n", status->rtt); |
| 2452 |
} |
| 2453 |
ao2_cleanup(status); |
| 2454 |
|
| 2455 |
astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); |
| 2456 |
|
| 2457 |
ami->count++; |
| 2458 |
|
| 2459 |
ast_free(buf); |
| 2460 |
|
| 2461 |
return 0; |
| 2462 |
} |
| 2463 |
|
| 2464 |
static int ami_show_contacts(struct mansession *s, const struct message *m) |
| 2465 |
{ |
| 2466 |
struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), }; |
| 2467 |
struct ao2_container *contacts; |
| 2468 |
|
| 2469 |
contacts = get_all_contacts(); |
| 2470 |
if (!contacts) { |
| 2471 |
astman_send_error(s, m, "Could not get Contacts\n"); |
| 2472 |
return 0; |
| 2473 |
} |
| 2474 |
|
| 2475 |
if (!ao2_container_count(contacts)) { |
| 2476 |
astman_send_error(s, m, "No Contacts found\n"); |
| 2477 |
ao2_ref(contacts, -1); |
| 2478 |
return 0; |
| 2479 |
} |
| 2480 |
|
| 2481 |
astman_send_listack(s, m, "A listing of Contacts follows, presented as ContactList events", |
| 2482 |
"start"); |
| 2483 |
|
| 2484 |
ao2_callback(contacts, OBJ_NODATA, format_ami_contactlist_handler, &ami); |
| 2485 |
|
| 2486 |
astman_send_list_complete_start(s, m, "ContactListComplete", ami.count); |
| 2487 |
astman_send_list_complete_end(s); |
| 2488 |
|
| 2489 |
ao2_ref(contacts, -1); |
| 2490 |
|
| 2491 |
return 0; |
| 2492 |
} |
| 2493 |
|
| 2494 |
static char *cli_show_qualify_endpoint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2495 |
{ |
| 2496 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 2497 |
const char *endpoint_name; |
| 2498 |
char *aors; |
| 2499 |
char *aor_name; |
| 2500 |
|
| 2501 |
switch (cmd) { |
| 2502 |
case CLI_INIT: |
| 2503 |
e->command = "pjsip show qualify endpoint"; |
| 2504 |
e->usage = |
| 2505 |
"Usage: pjsip show qualify endpoint <id>\n" |
| 2506 |
" Show the current qualify options for all Aors on the PJSIP endpoint.\n"; |
| 2507 |
return NULL; |
| 2508 |
case CLI_GENERATE: |
| 2509 |
return NULL; |
| 2510 |
} |
| 2511 |
|
| 2512 |
if (a->argc != 5) { |
| 2513 |
return CLI_SHOWUSAGE; |
| 2514 |
} |
| 2515 |
|
| 2516 |
endpoint_name = a->argv[4]; |
| 2517 |
|
| 2518 |
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", |
| 2519 |
endpoint_name); |
| 2520 |
if (!endpoint) { |
| 2521 |
ast_cli(a->fd, "Unable to retrieve endpoint %s\n", endpoint_name); |
| 2522 |
return CLI_FAILURE; |
| 2523 |
} |
| 2524 |
|
| 2525 |
if (ast_strlen_zero(endpoint->aors)) { |
| 2526 |
ast_cli(a->fd, "No AORs configured for endpoint '%s'\n", endpoint_name); |
| 2527 |
return CLI_FAILURE; |
| 2528 |
} |
| 2529 |
|
| 2530 |
aors = ast_strdupa(endpoint->aors); |
| 2531 |
while ((aor_name = ast_strip(strsep(&aors, ",")))) { |
| 2532 |
struct sip_options_aor *aor_options; |
| 2533 |
|
| 2534 |
aor_options = ao2_find(sip_options_aors, aor_name, OBJ_SEARCH_KEY); |
| 2535 |
if (!aor_options) { |
| 2536 |
continue; |
| 2537 |
} |
| 2538 |
|
| 2539 |
ast_cli(a->fd, " * AOR '%s' on endpoint '%s'\n", aor_name, endpoint_name); |
| 2540 |
ast_cli(a->fd, " Qualify frequency : %d sec\n", aor_options->qualify_frequency); |
| 2541 |
ast_cli(a->fd, " Qualify timeout : %d ms\n", (int)(aor_options->qualify_timeout / 1000)); |
| 2542 |
ast_cli(a->fd, " Qualify 2xx only : %s\n", aor_options->qualify_2xx_only ? "yes" : "no"); |
| 2543 |
ast_cli(a->fd, " Authenticate qualify : %s\n", aor_options->authenticate_qualify?"yes":"no"); |
| 2544 |
ast_cli(a->fd, "\n"); |
| 2545 |
ao2_ref(aor_options, -1); |
| 2546 |
} |
| 2547 |
|
| 2548 |
return CLI_SUCCESS; |
| 2549 |
} |
| 2550 |
|
| 2551 |
static char *cli_show_qualify_aor(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2552 |
{ |
| 2553 |
struct sip_options_aor *aor_options; |
| 2554 |
const char *aor_name; |
| 2555 |
|
| 2556 |
switch (cmd) { |
| 2557 |
case CLI_INIT: |
| 2558 |
e->command = "pjsip show qualify aor"; |
| 2559 |
e->usage = |
| 2560 |
"Usage: pjsip show qualify aor <id>\n" |
| 2561 |
" Show the PJSIP Aor current qualify options.\n"; |
| 2562 |
return NULL; |
| 2563 |
case CLI_GENERATE: |
| 2564 |
return NULL; |
| 2565 |
} |
| 2566 |
|
| 2567 |
if (a->argc != 5) { |
| 2568 |
return CLI_SHOWUSAGE; |
| 2569 |
} |
| 2570 |
|
| 2571 |
aor_name = a->argv[4]; |
| 2572 |
|
| 2573 |
aor_options = ao2_find(sip_options_aors, aor_name, OBJ_SEARCH_KEY); |
| 2574 |
if (!aor_options) { |
| 2575 |
ast_cli(a->fd, "Unable to retrieve aor '%s' qualify options\n", aor_name); |
| 2576 |
return CLI_FAILURE; |
| 2577 |
} |
| 2578 |
|
| 2579 |
ast_cli(a->fd, " * AOR '%s'\n", aor_name); |
| 2580 |
ast_cli(a->fd, " Qualify frequency : %d sec\n", aor_options->qualify_frequency); |
| 2581 |
ast_cli(a->fd, " Qualify timeout : %d ms\n", (int)(aor_options->qualify_timeout / 1000)); |
| 2582 |
ast_cli(a->fd, " Qualify 2xx only : %s\n", aor_options->qualify_2xx_only ? "yes" : "no"); |
| 2583 |
ast_cli(a->fd, " Authenticate qualify : %s\n", aor_options->authenticate_qualify?"yes":"no"); |
| 2584 |
ao2_ref(aor_options, -1); |
| 2585 |
|
| 2586 |
return CLI_SUCCESS; |
| 2587 |
} |
| 2588 |
|
| 2589 |
static char *cli_reload_qualify_endpoint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2590 |
{ |
| 2591 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 2592 |
const char *endpoint_name; |
| 2593 |
char *aors; |
| 2594 |
char *aor_name; |
| 2595 |
|
| 2596 |
switch (cmd) { |
| 2597 |
case CLI_INIT: |
| 2598 |
e->command = "pjsip reload qualify endpoint"; |
| 2599 |
e->usage = |
| 2600 |
"Usage: pjsip reload qualify endpoint <id>\n" |
| 2601 |
" Synchronize the qualify options for all Aors on the PJSIP endpoint.\n"; |
| 2602 |
return NULL; |
| 2603 |
case CLI_GENERATE: |
| 2604 |
return NULL; |
| 2605 |
} |
| 2606 |
|
| 2607 |
if (a->argc != 5) { |
| 2608 |
return CLI_SHOWUSAGE; |
| 2609 |
} |
| 2610 |
|
| 2611 |
endpoint_name = a->argv[4]; |
| 2612 |
|
| 2613 |
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", |
| 2614 |
endpoint_name); |
| 2615 |
if (!endpoint) { |
| 2616 |
ast_cli(a->fd, "Unable to retrieve endpoint %s\n", endpoint_name); |
| 2617 |
return CLI_FAILURE; |
| 2618 |
} |
| 2619 |
|
| 2620 |
if (ast_strlen_zero(endpoint->aors)) { |
| 2621 |
ast_cli(a->fd, "No AORs configured for endpoint '%s'\n", endpoint_name); |
| 2622 |
return CLI_FAILURE; |
| 2623 |
} |
| 2624 |
|
| 2625 |
aors = ast_strdupa(endpoint->aors); |
| 2626 |
while ((aor_name = ast_strip(strsep(&aors, ",")))) { |
| 2627 |
struct ast_sip_aor *aor; |
| 2628 |
|
| 2629 |
aor = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", aor_name); |
| 2630 |
if (!aor) { |
| 2631 |
continue; |
| 2632 |
} |
| 2633 |
|
| 2634 |
ast_cli(a->fd, "Synchronizing AOR '%s' on endpoint '%s'\n", aor_name, endpoint_name); |
| 2635 |
ast_sip_push_task_wait_serializer(management_serializer, |
| 2636 |
sip_options_aor_observer_modified_task, aor); |
| 2637 |
ao2_ref(aor, -1); |
| 2638 |
} |
| 2639 |
|
| 2640 |
return CLI_SUCCESS; |
| 2641 |
} |
| 2642 |
|
| 2643 |
static char *cli_reload_qualify_aor(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2644 |
{ |
| 2645 |
struct ast_sip_aor *aor; |
| 2646 |
const char *aor_name; |
| 2647 |
|
| 2648 |
switch (cmd) { |
| 2649 |
case CLI_INIT: |
| 2650 |
e->command = "pjsip reload qualify aor"; |
| 2651 |
e->usage = |
| 2652 |
"Usage: pjsip reload qualify aor <id>\n" |
| 2653 |
" Synchronize the PJSIP Aor qualify options.\n"; |
| 2654 |
return NULL; |
| 2655 |
case CLI_GENERATE: |
| 2656 |
return NULL; |
| 2657 |
} |
| 2658 |
|
| 2659 |
if (a->argc != 5) { |
| 2660 |
return CLI_SHOWUSAGE; |
| 2661 |
} |
| 2662 |
|
| 2663 |
aor_name = a->argv[4]; |
| 2664 |
|
| 2665 |
aor = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", aor_name); |
| 2666 |
if (!aor) { |
| 2667 |
ast_cli(a->fd, "Unable to retrieve aor '%s'\n", aor_name); |
| 2668 |
return CLI_FAILURE; |
| 2669 |
} |
| 2670 |
|
| 2671 |
ast_cli(a->fd, "Synchronizing AOR '%s'\n", aor_name); |
| 2672 |
ast_sip_push_task_wait_serializer(management_serializer, |
| 2673 |
sip_options_aor_observer_modified_task, aor); |
| 2674 |
ao2_ref(aor, -1); |
| 2675 |
|
| 2676 |
return CLI_SUCCESS; |
| 2677 |
} |
| 2678 |
|
| 2679 |
static int ami_sip_qualify(struct mansession *s, const struct message *m) |
| 2680 |
{ |
| 2681 |
const char *endpoint_name = astman_get_header(m, "Endpoint"); |
| 2682 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 2683 |
char *aors; |
| 2684 |
char *aor_name; |
| 2685 |
|
| 2686 |
if (ast_strlen_zero(endpoint_name)) { |
| 2687 |
astman_send_error(s, m, "Endpoint parameter missing."); |
| 2688 |
return 0; |
| 2689 |
} |
| 2690 |
|
| 2691 |
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", |
| 2692 |
endpoint_name); |
| 2693 |
if (!endpoint) { |
| 2694 |
astman_send_error(s, m, "Unable to retrieve endpoint\n"); |
| 2695 |
return 0; |
| 2696 |
} |
| 2697 |
|
| 2698 |
|
| 2699 |
if (ast_strlen_zero(endpoint->aors)) { |
| 2700 |
astman_send_error(s, m, "No AoRs configured for endpoint\n"); |
| 2701 |
return 0; |
| 2702 |
} |
| 2703 |
|
| 2704 |
aors = ast_strdupa(endpoint->aors); |
| 2705 |
while ((aor_name = ast_strip(strsep(&aors, ",")))) { |
| 2706 |
struct sip_options_aor *aor_options; |
| 2707 |
|
| 2708 |
aor_options = ao2_find(sip_options_aors, aor_name, OBJ_SEARCH_KEY); |
| 2709 |
if (!aor_options) { |
| 2710 |
continue; |
| 2711 |
} |
| 2712 |
|
| 2713 |
ast_sip_push_task_wait_serializer(aor_options->serializer, sip_options_qualify_aor, |
| 2714 |
aor_options); |
| 2715 |
ao2_ref(aor_options, -1); |
| 2716 |
} |
| 2717 |
|
| 2718 |
astman_send_ack(s, m, "Endpoint found, will qualify"); |
| 2719 |
return 0; |
| 2720 |
} |
| 2721 |
|
| 2722 |
static struct ast_cli_entry cli_options[] = { |
| 2723 |
AST_CLI_DEFINE(cli_qualify, "Send an OPTIONS request to a PJSIP endpoint"), |
| 2724 |
AST_CLI_DEFINE(cli_show_qualify_endpoint, "Show the current qualify options for all Aors on the PJSIP endpoint"), |
| 2725 |
AST_CLI_DEFINE(cli_show_qualify_aor, "Show the PJSIP Aor current qualify options"), |
| 2726 |
AST_CLI_DEFINE(cli_reload_qualify_endpoint, "Synchronize the qualify options for all Aors on the PJSIP endpoint"), |
| 2727 |
AST_CLI_DEFINE(cli_reload_qualify_aor, "Synchronize the PJSIP Aor qualify options"), |
| 2728 |
}; |
| 2729 |
|
| 2730 |
int ast_sip_format_contact_ami(void *obj, void *arg, int flags) |
| 2731 |
{ |
| 2732 |
struct ast_sip_contact_wrapper *wrapper = obj; |
| 2733 |
struct ast_sip_contact *contact = wrapper->contact; |
| 2734 |
struct ast_sip_ami *ami = arg; |
| 2735 |
struct ast_sip_contact_status *status; |
| 2736 |
struct ast_str *buf; |
| 2737 |
const struct ast_sip_endpoint *endpoint = ami->arg; |
| 2738 |
char secs[AST_TIME_T_LEN]; |
| 2739 |
|
| 2740 |
buf = ast_sip_create_ami_event("ContactStatusDetail", ami); |
| 2741 |
if (!buf) { |
| 2742 |
return -1; |
| 2743 |
} |
| 2744 |
|
| 2745 |
status = ast_sip_get_contact_status(contact); |
| 2746 |
|
| 2747 |
ast_str_append(&buf, 0, "AOR: %s\r\n", wrapper->aor_id); |
| 2748 |
ast_str_append(&buf, 0, "URI: %s\r\n", contact->uri); |
| 2749 |
ast_str_append(&buf, 0, "UserAgent: %s\r\n", contact->user_agent); |
| 2750 |
ast_time_t_to_string(contact->expiration_time.tv_sec, secs, sizeof(secs)); |
| 2751 |
ast_str_append(&buf, 0, "RegExpire: %s\r\n", secs); |
| 2752 |
if (!ast_strlen_zero(contact->via_addr)) { |
| 2753 |
ast_str_append(&buf, 0, "ViaAddress: %s", contact->via_addr); |
| 2754 |
if (contact->via_port) { |
| 2755 |
ast_str_append(&buf, 0, ":%d", contact->via_port); |
| 2756 |
} |
| 2757 |
ast_str_append(&buf, 0, "\r\n"); |
| 2758 |
} |
| 2759 |
if (!ast_strlen_zero(contact->call_id)) { |
| 2760 |
ast_str_append(&buf, 0, "CallID: %s\r\n", contact->call_id); |
| 2761 |
} |
| 2762 |
ast_str_append(&buf, 0, "Status: %s\r\n", |
| 2763 |
ast_sip_get_contact_status_label(status ? status->status : UNKNOWN)); |
| 2764 |
if (!status || status->status != AVAILABLE) { |
| 2765 |
ast_str_append(&buf, 0, "RoundtripUsec: N/A\r\n"); |
| 2766 |
} else { |
| 2767 |
ast_str_append(&buf, 0, "RoundtripUsec: %" PRId64 "\r\n", status->rtt); |
| 2768 |
} |
| 2769 |
ast_str_append(&buf, 0, "EndpointName: %s\r\n", |
| 2770 |
endpoint ? ast_sorcery_object_get_id(endpoint) : S_OR(contact->endpoint_name, "")); |
| 2771 |
|
| 2772 |
ast_str_append(&buf, 0, "ID: %s\r\n", ast_sorcery_object_get_id(contact)); |
| 2773 |
ast_str_append(&buf, 0, "AuthenticateQualify: %d\r\n", contact->authenticate_qualify); |
| 2774 |
ast_str_append(&buf, 0, "OutboundProxy: %s\r\n", contact->outbound_proxy); |
| 2775 |
ast_str_append(&buf, 0, "Path: %s\r\n", contact->path); |
| 2776 |
ast_str_append(&buf, 0, "QualifyFrequency: %u\r\n", contact->qualify_frequency); |
| 2777 |
ast_str_append(&buf, 0, "QualifyTimeout: %.3f\r\n", contact->qualify_timeout); |
| 2778 |
ast_str_append(&buf, 0, "Qualify2xxOnly: %d\r\n", contact->qualify_2xx_only); |
| 2779 |
|
| 2780 |
astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); |
| 2781 |
ami->count++; |
| 2782 |
|
| 2783 |
ast_free(buf); |
| 2784 |
ao2_cleanup(status); |
| 2785 |
return 0; |
| 2786 |
} |
| 2787 |
|
| 2788 |
static int format_contact_status_for_aor(void *obj, void *arg, int flags) |
| 2789 |
{ |
| 2790 |
struct ast_sip_aor *aor = obj; |
| 2791 |
|
| 2792 |
return ast_sip_for_each_contact(aor, ast_sip_format_contact_ami, arg); |
| 2793 |
} |
| 2794 |
|
| 2795 |
static int format_ami_contact_status(const struct ast_sip_endpoint *endpoint, |
| 2796 |
struct ast_sip_ami *ami) |
| 2797 |
{ |
| 2798 |
ami->arg = (void *)endpoint; |
| 2799 |
return ast_sip_for_each_aor(endpoint->aors, format_contact_status_for_aor, ami); |
| 2800 |
} |
| 2801 |
|
| 2802 |
static struct ast_sip_endpoint_formatter contact_status_formatter = { |
| 2803 |
.format_ami = format_ami_contact_status |
| 2804 |
}; |
| 2805 |
|
| 2806 |
|
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
static int sip_options_cleanup_aor_task(void *obj) |
| 2811 |
{ |
| 2812 |
struct sip_options_aor *aor_options = obj; |
| 2813 |
|
| 2814 |
ast_debug(2, "Cleaning up AOR '%s' for shutdown\n", aor_options->name); |
| 2815 |
|
| 2816 |
aor_options->qualify_frequency = 0; |
| 2817 |
if (aor_options->sched_task) { |
| 2818 |
ast_sip_sched_task_cancel(aor_options->sched_task); |
| 2819 |
ao2_ref(aor_options->sched_task, -1); |
| 2820 |
aor_options->sched_task = NULL; |
| 2821 |
} |
| 2822 |
AST_VECTOR_RESET(&aor_options->compositors, ao2_cleanup); |
| 2823 |
|
| 2824 |
return 0; |
| 2825 |
} |
| 2826 |
|
| 2827 |
|
| 2828 |
|
| 2829 |
|
| 2830 |
|
| 2831 |
static int sip_options_cleanup_task(void *obj) |
| 2832 |
{ |
| 2833 |
struct ao2_iterator it_aor; |
| 2834 |
struct sip_options_aor *aor_options; |
| 2835 |
|
| 2836 |
if (!sip_options_aors) { |
| 2837 |
|
| 2838 |
return 0; |
| 2839 |
} |
| 2840 |
|
| 2841 |
it_aor = ao2_iterator_init(sip_options_aors, AO2_ITERATOR_UNLINK); |
| 2842 |
for (; (aor_options = ao2_iterator_next(&it_aor)); ao2_ref(aor_options, -1)) { |
| 2843 |
ast_sip_push_task_wait_serializer(aor_options->serializer, |
| 2844 |
sip_options_cleanup_aor_task, aor_options); |
| 2845 |
} |
| 2846 |
ao2_iterator_destroy(&it_aor); |
| 2847 |
|
| 2848 |
return 0; |
| 2849 |
} |
| 2850 |
|
| 2851 |
void ast_res_pjsip_cleanup_options_handling(void) |
| 2852 |
{ |
| 2853 |
int remaining; |
| 2854 |
struct ast_taskprocessor *mgmt_serializer; |
| 2855 |
|
| 2856 |
ast_cli_unregister_multiple(cli_options, ARRAY_LEN(cli_options)); |
| 2857 |
ast_manager_unregister("PJSIPQualify"); |
| 2858 |
ast_manager_unregister("PJSIPShowContacts"); |
| 2859 |
ast_sip_unregister_endpoint_formatter(&contact_status_formatter); |
| 2860 |
|
| 2861 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "contact", |
| 2862 |
&contact_observer_callbacks); |
| 2863 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "aor", |
| 2864 |
&aor_observer_callbacks); |
| 2865 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "endpoint", |
| 2866 |
&endpoint_observer_callbacks); |
| 2867 |
|
| 2868 |
mgmt_serializer = management_serializer; |
| 2869 |
management_serializer = NULL; |
| 2870 |
if (mgmt_serializer) { |
| 2871 |
ast_sip_push_task_wait_serializer(mgmt_serializer, sip_options_cleanup_task, NULL); |
| 2872 |
} |
| 2873 |
|
| 2874 |
remaining = ast_serializer_shutdown_group_join(shutdown_group, |
| 2875 |
MAX_UNLOAD_TIMEOUT_TIME); |
| 2876 |
if (remaining) { |
| 2877 |
ast_log(LOG_WARNING, "Cleanup incomplete. Could not stop %d AORs.\n", |
| 2878 |
remaining); |
| 2879 |
} |
| 2880 |
ao2_cleanup(shutdown_group); |
| 2881 |
shutdown_group = NULL; |
| 2882 |
|
| 2883 |
if (mgmt_serializer) { |
| 2884 |
ast_taskprocessor_unreference(mgmt_serializer); |
| 2885 |
} |
| 2886 |
|
| 2887 |
ao2_cleanup(sip_options_aors); |
| 2888 |
sip_options_aors = NULL; |
| 2889 |
ao2_cleanup(sip_options_contact_statuses); |
| 2890 |
sip_options_contact_statuses = NULL; |
| 2891 |
ao2_cleanup(sip_options_endpoint_state_compositors); |
| 2892 |
sip_options_endpoint_state_compositors = NULL; |
| 2893 |
|
| 2894 |
pjsip_endpt_unregister_module(ast_sip_get_pjsip_endpoint(), &options_module); |
| 2895 |
} |
| 2896 |
|
| 2897 |
|
| 2898 |
|
| 2899 |
|
| 2900 |
|
| 2901 |
static int sip_options_init_task(void *mgmt_serializer) |
| 2902 |
{ |
| 2903 |
management_serializer = mgmt_serializer; |
| 2904 |
|
| 2905 |
shutdown_group = ast_serializer_shutdown_group_alloc(); |
| 2906 |
if (!shutdown_group) { |
| 2907 |
return -1; |
| 2908 |
} |
| 2909 |
|
| 2910 |
if (ast_sorcery_observer_add(ast_sip_get_sorcery(), "endpoint", |
| 2911 |
&endpoint_observer_callbacks)) { |
| 2912 |
return -1; |
| 2913 |
} |
| 2914 |
if (ast_sorcery_observer_add(ast_sip_get_sorcery(), "aor", |
| 2915 |
&aor_observer_callbacks)) { |
| 2916 |
return -1; |
| 2917 |
} |
| 2918 |
if (ast_sorcery_observer_add(ast_sip_get_sorcery(), "contact", |
| 2919 |
&contact_observer_callbacks)) { |
| 2920 |
return -1; |
| 2921 |
} |
| 2922 |
|
| 2923 |
sip_options_synchronize(0); |
| 2924 |
|
| 2925 |
return 0; |
| 2926 |
} |
| 2927 |
|
| 2928 |
int ast_res_pjsip_preinit_options_handling(void) |
| 2929 |
{ |
| 2930 |
sip_options_contact_statuses = sip_options_contact_statuses_alloc(); |
| 2931 |
return sip_options_contact_statuses ? 0 : -1; |
| 2932 |
} |
| 2933 |
|
| 2934 |
int ast_res_pjsip_init_options_handling(int reload) |
| 2935 |
{ |
| 2936 |
struct ast_taskprocessor *mgmt_serializer; |
| 2937 |
|
| 2938 |
static const pj_str_t STR_OPTIONS = { "OPTIONS", 7 }; |
| 2939 |
|
| 2940 |
if (reload) { |
| 2941 |
sip_options_synchronize(1); |
| 2942 |
return 0; |
| 2943 |
} |
| 2944 |
|
| 2945 |
if (pjsip_endpt_register_module(ast_sip_get_pjsip_endpoint(), &options_module) |
| 2946 |
!= PJ_SUCCESS) { |
| 2947 |
return -1; |
| 2948 |
} |
| 2949 |
|
| 2950 |
if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), NULL, PJSIP_H_ALLOW, |
| 2951 |
NULL, 1, &STR_OPTIONS) != PJ_SUCCESS) { |
| 2952 |
ast_res_pjsip_cleanup_options_handling(); |
| 2953 |
return -1; |
| 2954 |
} |
| 2955 |
|
| 2956 |
sip_options_aors = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, AOR_BUCKETS, |
| 2957 |
sip_options_aor_hash_fn, NULL, sip_options_aor_cmp_fn); |
| 2958 |
if (!sip_options_aors) { |
| 2959 |
ast_res_pjsip_cleanup_options_handling(); |
| 2960 |
return -1; |
| 2961 |
} |
| 2962 |
sip_options_endpoint_state_compositors = |
| 2963 |
ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, |
| 2964 |
ENDPOINT_STATE_COMPOSITOR_BUCKETS, |
| 2965 |
sip_options_endpoint_state_compositor_hash_fn, NULL, |
| 2966 |
sip_options_endpoint_state_compositor_cmp_fn); |
| 2967 |
if (!sip_options_endpoint_state_compositors) { |
| 2968 |
ast_res_pjsip_cleanup_options_handling(); |
| 2969 |
return -1; |
| 2970 |
} |
| 2971 |
|
| 2972 |
mgmt_serializer = ast_sip_create_serializer("pjsip/options/manage"); |
| 2973 |
if (!mgmt_serializer) { |
| 2974 |
ast_res_pjsip_cleanup_options_handling(); |
| 2975 |
return -1; |
| 2976 |
} |
| 2977 |
|
| 2978 |
|
| 2979 |
|
| 2980 |
|
| 2981 |
|
| 2982 |
|
| 2983 |
ast_taskprocessor_alert_set_levels(mgmt_serializer, -1, |
| 2984 |
10 * AST_TASKPROCESSOR_HIGH_WATER_LEVEL); |
| 2985 |
|
| 2986 |
|
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
|
| 2991 |
if (ast_sip_push_task_wait_serializer(mgmt_serializer, sip_options_init_task, |
| 2992 |
mgmt_serializer)) { |
| 2993 |
|
| 2994 |
management_serializer = mgmt_serializer; |
| 2995 |
ast_res_pjsip_cleanup_options_handling(); |
| 2996 |
return -1; |
| 2997 |
} |
| 2998 |
|
| 2999 |
ast_sip_register_endpoint_formatter(&contact_status_formatter); |
| 3000 |
ast_manager_register_xml("PJSIPQualify", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, |
| 3001 |
ami_sip_qualify); |
| 3002 |
ast_manager_register_xml("PJSIPShowContacts", EVENT_FLAG_SYSTEM, ami_show_contacts); |
| 3003 |
ast_cli_register_multiple(cli_options, ARRAY_LEN(cli_options)); |
| 3004 |
|
| 3005 |
return 0; |
| 3006 |
} |