| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "asterisk.h" |
| 20 |
|
| 21 |
#include <pjsip.h> |
| 22 |
|
| 23 |
#include "asterisk/res_pjsip.h" |
| 24 |
#include "asterisk/acl.h" |
| 25 |
#include "include/res_pjsip_private.h" |
| 26 |
#include "asterisk/taskprocessor.h" |
| 27 |
#include "asterisk/taskpool.h" |
| 28 |
#include "asterisk/res_pjsip_cli.h" |
| 29 |
|
| 30 |
static int distribute(void *data); |
| 31 |
static pj_bool_t distributor(pjsip_rx_data *rdata); |
| 32 |
static pj_status_t record_serializer(pjsip_tx_data *tdata); |
| 33 |
|
| 34 |
static pjsip_module distributor_mod = { |
| 35 |
.name = {"Request Distributor", 19}, |
| 36 |
.priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 6, |
| 37 |
.on_tx_request = record_serializer, |
| 38 |
.on_rx_request = distributor, |
| 39 |
.on_rx_response = distributor, |
| 40 |
}; |
| 41 |
|
| 42 |
struct ast_sched_context *prune_context; |
| 43 |
|
| 44 |
#define DEFAULT_SUSPECTS_BUCKETS 53 |
| 45 |
|
| 46 |
static struct ao2_container *unidentified_requests; |
| 47 |
static unsigned int unidentified_count; |
| 48 |
static unsigned int unidentified_period; |
| 49 |
static unsigned int unidentified_prune_interval; |
| 50 |
static int using_auth_username; |
| 51 |
static enum ast_sip_taskprocessor_overload_trigger overload_trigger; |
| 52 |
|
| 53 |
struct unidentified_request{ |
| 54 |
struct timeval first_seen; |
| 55 |
int count; |
| 56 |
char src_name[]; |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
#define DISTRIBUTOR_POOL_SIZE 31 |
| 61 |
|
| 62 |
|
| 63 |
static struct ast_taskprocessor *distributor_pool[DISTRIBUTOR_POOL_SIZE]; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static pj_status_t record_serializer(pjsip_tx_data *tdata) |
| 75 |
{ |
| 76 |
struct ast_taskprocessor *serializer; |
| 77 |
|
| 78 |
serializer = ast_taskpool_serializer_get_current(); |
| 79 |
if (serializer) { |
| 80 |
const char *name; |
| 81 |
|
| 82 |
name = ast_taskprocessor_name(serializer); |
| 83 |
if (!ast_strlen_zero(name) |
| 84 |
&& (!tdata->mod_data[distributor_mod.id] |
| 85 |
|| strcmp(tdata->mod_data[distributor_mod.id], name))) { |
| 86 |
char *tdata_name; |
| 87 |
|
| 88 |
|
| 89 |
tdata_name = pj_pool_alloc(tdata->pool, strlen(name) + 1); |
| 90 |
strcpy(tdata_name, name); |
| 91 |
|
| 92 |
tdata->mod_data[distributor_mod.id] = tdata_name; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return PJ_SUCCESS; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static struct ast_taskprocessor *find_request_serializer(pjsip_rx_data *rdata) |
| 110 |
{ |
| 111 |
struct ast_taskprocessor *serializer = NULL; |
| 112 |
pj_str_t tsx_key; |
| 113 |
pjsip_transaction *tsx; |
| 114 |
|
| 115 |
pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAC, |
| 116 |
&rdata->msg_info.cseq->method, rdata); |
| 117 |
|
| 118 |
tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE); |
| 119 |
if (!tsx) { |
| 120 |
ast_debug(1, "Could not find transaction for %s.\n", |
| 121 |
pjsip_rx_data_get_info(rdata)); |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
ast_debug(3, "Found transaction %s for %s.\n", |
| 125 |
tsx->obj_name, pjsip_rx_data_get_info(rdata)); |
| 126 |
|
| 127 |
if (tsx->last_tx) { |
| 128 |
const char *serializer_name; |
| 129 |
|
| 130 |
serializer_name = tsx->last_tx->mod_data[distributor_mod.id]; |
| 131 |
if (!ast_strlen_zero(serializer_name)) { |
| 132 |
serializer = ast_taskprocessor_get(serializer_name, TPS_REF_IF_EXISTS); |
| 133 |
if (serializer) { |
| 134 |
ast_debug(3, "Found serializer %s on transaction %s\n", |
| 135 |
serializer_name, tsx->obj_name); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
#ifdef HAVE_PJ_TRANSACTION_GRP_LOCK |
| 141 |
pj_grp_lock_release(tsx->grp_lock); |
| 142 |
#else |
| 143 |
pj_mutex_unlock(tsx->mutex); |
| 144 |
#endif |
| 145 |
|
| 146 |
return serializer; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
struct distributor_dialog_data { |
| 151 |
|
| 152 |
pjsip_dialog *dlg; |
| 153 |
|
| 154 |
struct ast_taskprocessor *serializer; |
| 155 |
|
| 156 |
struct ast_sip_endpoint *endpoint; |
| 157 |
}; |
| 158 |
|
| 159 |
#define DIALOG_ASSOCIATIONS_BUCKETS 251 |
| 160 |
|
| 161 |
static struct ao2_container *dialog_associations; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static int buf_hash_add(const char *pos, size_t len, int hash) |
| 182 |
{ |
| 183 |
while (len--) { |
| 184 |
hash = hash * 33 ^ *pos++; |
| 185 |
} |
| 186 |
|
| 187 |
return hash; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
static int buf_hash(const char *pos, size_t len) |
| 208 |
{ |
| 209 |
return buf_hash_add(pos, len, 5381); |
| 210 |
} |
| 211 |
|
| 212 |
static int dialog_associations_hash(const void *obj, int flags) |
| 213 |
{ |
| 214 |
const struct distributor_dialog_data *object; |
| 215 |
union { |
| 216 |
const pjsip_dialog *dlg; |
| 217 |
const char buf[sizeof(pjsip_dialog *)]; |
| 218 |
} key; |
| 219 |
|
| 220 |
switch (flags & OBJ_SEARCH_MASK) { |
| 221 |
case OBJ_SEARCH_KEY: |
| 222 |
key.dlg = obj; |
| 223 |
break; |
| 224 |
case OBJ_SEARCH_OBJECT: |
| 225 |
object = obj; |
| 226 |
key.dlg = object->dlg; |
| 227 |
break; |
| 228 |
default: |
| 229 |
|
| 230 |
ast_assert(0); |
| 231 |
return 0; |
| 232 |
} |
| 233 |
return ast_str_hash_restrict(buf_hash(key.buf, sizeof(key.buf))); |
| 234 |
} |
| 235 |
|
| 236 |
static int dialog_associations_cmp(void *obj, void *arg, int flags) |
| 237 |
{ |
| 238 |
const struct distributor_dialog_data *object_left = obj; |
| 239 |
const struct distributor_dialog_data *object_right = arg; |
| 240 |
const pjsip_dialog *right_key = arg; |
| 241 |
int cmp = 0; |
| 242 |
|
| 243 |
switch (flags & OBJ_SEARCH_MASK) { |
| 244 |
case OBJ_SEARCH_OBJECT: |
| 245 |
right_key = object_right->dlg; |
| 246 |
|
| 247 |
case OBJ_SEARCH_KEY: |
| 248 |
if (object_left->dlg == right_key) { |
| 249 |
cmp = CMP_MATCH; |
| 250 |
} |
| 251 |
break; |
| 252 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 253 |
|
| 254 |
ast_assert(0); |
| 255 |
break; |
| 256 |
default: |
| 257 |
cmp = 0; |
| 258 |
break; |
| 259 |
} |
| 260 |
return cmp; |
| 261 |
} |
| 262 |
|
| 263 |
void ast_sip_dialog_set_serializer(pjsip_dialog *dlg, struct ast_taskprocessor *serializer) |
| 264 |
{ |
| 265 |
struct distributor_dialog_data *dist; |
| 266 |
|
| 267 |
ao2_wrlock(dialog_associations); |
| 268 |
dist = ao2_find(dialog_associations, dlg, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 269 |
if (!dist) { |
| 270 |
if (serializer) { |
| 271 |
dist = ao2_alloc(sizeof(*dist), NULL); |
| 272 |
if (dist) { |
| 273 |
dist->dlg = dlg; |
| 274 |
dist->serializer = serializer; |
| 275 |
ao2_link_flags(dialog_associations, dist, OBJ_NOLOCK); |
| 276 |
ao2_ref(dist, -1); |
| 277 |
} |
| 278 |
} |
| 279 |
} else { |
| 280 |
ao2_lock(dist); |
| 281 |
dist->serializer = serializer; |
| 282 |
if (!dist->serializer && !dist->endpoint) { |
| 283 |
ao2_unlink_flags(dialog_associations, dist, OBJ_NOLOCK); |
| 284 |
} |
| 285 |
ao2_unlock(dist); |
| 286 |
ao2_ref(dist, -1); |
| 287 |
} |
| 288 |
ao2_unlock(dialog_associations); |
| 289 |
} |
| 290 |
|
| 291 |
void ast_sip_dialog_set_endpoint(pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint) |
| 292 |
{ |
| 293 |
struct distributor_dialog_data *dist; |
| 294 |
|
| 295 |
ao2_wrlock(dialog_associations); |
| 296 |
dist = ao2_find(dialog_associations, dlg, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 297 |
if (!dist) { |
| 298 |
if (endpoint) { |
| 299 |
dist = ao2_alloc(sizeof(*dist), NULL); |
| 300 |
if (dist) { |
| 301 |
dist->dlg = dlg; |
| 302 |
dist->endpoint = endpoint; |
| 303 |
ao2_link_flags(dialog_associations, dist, OBJ_NOLOCK); |
| 304 |
ao2_ref(dist, -1); |
| 305 |
} |
| 306 |
} |
| 307 |
} else { |
| 308 |
ao2_lock(dist); |
| 309 |
dist->endpoint = endpoint; |
| 310 |
if (!dist->serializer && !dist->endpoint) { |
| 311 |
ao2_unlink_flags(dialog_associations, dist, OBJ_NOLOCK); |
| 312 |
} |
| 313 |
ao2_unlock(dist); |
| 314 |
ao2_ref(dist, -1); |
| 315 |
} |
| 316 |
ao2_unlock(dialog_associations); |
| 317 |
} |
| 318 |
|
| 319 |
struct ast_sip_endpoint *ast_sip_dialog_get_endpoint(pjsip_dialog *dlg) |
| 320 |
{ |
| 321 |
struct distributor_dialog_data *dist; |
| 322 |
struct ast_sip_endpoint *endpoint; |
| 323 |
|
| 324 |
dist = ao2_find(dialog_associations, dlg, OBJ_SEARCH_KEY); |
| 325 |
if (dist) { |
| 326 |
ao2_lock(dist); |
| 327 |
endpoint = ao2_bump(dist->endpoint); |
| 328 |
ao2_unlock(dist); |
| 329 |
ao2_ref(dist, -1); |
| 330 |
} else { |
| 331 |
endpoint = NULL; |
| 332 |
} |
| 333 |
return endpoint; |
| 334 |
} |
| 335 |
|
| 336 |
static pjsip_dialog *find_dialog(pjsip_rx_data *rdata) |
| 337 |
{ |
| 338 |
pj_str_t tsx_key; |
| 339 |
pjsip_transaction *tsx; |
| 340 |
pjsip_dialog *dlg; |
| 341 |
pj_str_t *local_tag; |
| 342 |
pj_str_t *remote_tag; |
| 343 |
|
| 344 |
if (!rdata->msg_info.msg) { |
| 345 |
return NULL; |
| 346 |
} |
| 347 |
|
| 348 |
if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) { |
| 349 |
local_tag = &rdata->msg_info.to->tag; |
| 350 |
remote_tag = &rdata->msg_info.from->tag; |
| 351 |
} else { |
| 352 |
local_tag = &rdata->msg_info.from->tag; |
| 353 |
remote_tag = &rdata->msg_info.to->tag; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
if (rdata->msg_info.msg->type == PJSIP_RESPONSE_MSG || |
| 362 |
pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method) || |
| 363 |
rdata->msg_info.to->tag.slen != 0) { |
| 364 |
dlg = pjsip_ua_find_dialog(&rdata->msg_info.cid->id, local_tag, |
| 365 |
remote_tag, PJ_FALSE); |
| 366 |
if (dlg) { |
| 367 |
return dlg; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) { |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAS, |
| 382 |
pjsip_get_invite_method(), rdata); |
| 383 |
} else { |
| 384 |
pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAC, |
| 385 |
&rdata->msg_info.cseq->method, rdata); |
| 386 |
} |
| 387 |
|
| 388 |
tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE); |
| 389 |
if (!tsx) { |
| 390 |
ast_debug(3, "Could not find matching transaction for %s\n", |
| 391 |
pjsip_rx_data_get_info(rdata)); |
| 392 |
return NULL; |
| 393 |
} |
| 394 |
|
| 395 |
dlg = pjsip_tsx_get_dlg(tsx); |
| 396 |
|
| 397 |
#ifdef HAVE_PJ_TRANSACTION_GRP_LOCK |
| 398 |
pj_grp_lock_release(tsx->grp_lock); |
| 399 |
#else |
| 400 |
pj_mutex_unlock(tsx->mutex); |
| 401 |
#endif |
| 402 |
|
| 403 |
return dlg; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
static int pjstr_hash_add(pj_str_t *str, int hash) |
| 424 |
{ |
| 425 |
return buf_hash_add(pj_strbuf(str), pj_strlen(str), hash); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
static int pjstr_hash(pj_str_t *str) |
| 441 |
{ |
| 442 |
return pjstr_hash_add(str, 5381); |
| 443 |
} |
| 444 |
|
| 445 |
struct ast_taskprocessor *ast_sip_get_distributor_serializer(pjsip_rx_data *rdata) |
| 446 |
{ |
| 447 |
int hash; |
| 448 |
pj_str_t *remote_tag; |
| 449 |
struct ast_taskprocessor *serializer; |
| 450 |
|
| 451 |
if (!rdata->msg_info.msg) { |
| 452 |
return NULL; |
| 453 |
} |
| 454 |
|
| 455 |
if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) { |
| 456 |
remote_tag = &rdata->msg_info.from->tag; |
| 457 |
} else { |
| 458 |
remote_tag = &rdata->msg_info.to->tag; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
hash = pjstr_hash(&rdata->msg_info.cid->id); |
| 463 |
hash = pjstr_hash_add(remote_tag, hash); |
| 464 |
hash = ast_str_hash_restrict(hash); |
| 465 |
|
| 466 |
serializer = ao2_bump(distributor_pool[hash % ARRAY_LEN(distributor_pool)]); |
| 467 |
if (serializer) { |
| 468 |
ast_debug(3, "Calculated serializer %s to use for %s\n", |
| 469 |
ast_taskprocessor_name(serializer), pjsip_rx_data_get_info(rdata)); |
| 470 |
} |
| 471 |
return serializer; |
| 472 |
} |
| 473 |
|
| 474 |
static pj_bool_t endpoint_lookup(pjsip_rx_data *rdata); |
| 475 |
|
| 476 |
static pjsip_module endpoint_mod = { |
| 477 |
.name = {"Endpoint Identifier", 19}, |
| 478 |
.priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 3, |
| 479 |
.on_rx_request = endpoint_lookup, |
| 480 |
}; |
| 481 |
|
| 482 |
static pj_bool_t distributor(pjsip_rx_data *rdata) |
| 483 |
{ |
| 484 |
pjsip_dialog *dlg; |
| 485 |
struct distributor_dialog_data *dist = NULL; |
| 486 |
struct ast_taskprocessor *serializer = NULL; |
| 487 |
pjsip_rx_data *clone; |
| 488 |
|
| 489 |
if (!ast_fully_booted) { |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
return PJ_TRUE; |
| 495 |
} |
| 496 |
|
| 497 |
dlg = find_dialog(rdata); |
| 498 |
if (dlg) { |
| 499 |
ast_debug(3, "Searching for serializer associated with dialog %s for %s\n", |
| 500 |
dlg->obj_name, pjsip_rx_data_get_info(rdata)); |
| 501 |
dist = ao2_find(dialog_associations, dlg, OBJ_SEARCH_KEY); |
| 502 |
if (dist) { |
| 503 |
ao2_lock(dist); |
| 504 |
serializer = ao2_bump(dist->serializer); |
| 505 |
ao2_unlock(dist); |
| 506 |
if (serializer) { |
| 507 |
ast_debug(3, "Found serializer %s associated with dialog %s\n", |
| 508 |
ast_taskprocessor_name(serializer), dlg->obj_name); |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
if (serializer) { |
| 514 |
|
| 515 |
} else if (rdata->msg_info.msg->type == PJSIP_RESPONSE_MSG) { |
| 516 |
ast_debug(3, "No dialog serializer for %s. Using request transaction as basis.\n", |
| 517 |
pjsip_rx_data_get_info(rdata)); |
| 518 |
serializer = find_request_serializer(rdata); |
| 519 |
if (!serializer) { |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
serializer = ast_sip_get_distributor_serializer(rdata); |
| 526 |
} |
| 527 |
} else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method) |
| 528 |
|| !pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method)) { |
| 529 |
|
| 530 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, |
| 531 |
PJSIP_SC_CALL_TSX_DOES_NOT_EXIST, NULL, NULL, NULL); |
| 532 |
ao2_cleanup(dist); |
| 533 |
return PJ_TRUE; |
| 534 |
} else { |
| 535 |
if ((overload_trigger == TASKPROCESSOR_OVERLOAD_TRIGGER_GLOBAL && |
| 536 |
ast_taskprocessor_alert_get()) |
| 537 |
|| (overload_trigger == TASKPROCESSOR_OVERLOAD_TRIGGER_PJSIP_ONLY && |
| 538 |
ast_taskprocessor_get_subsystem_alert("pjsip"))) { |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
switch (rdata->tp_info.transport->key.type) { |
| 548 |
case PJSIP_TRANSPORT_UDP6: |
| 549 |
case PJSIP_TRANSPORT_UDP: |
| 550 |
ast_debug(3, "Taskprocessor overload alert: Ignoring '%s'.\n", |
| 551 |
pjsip_rx_data_get_info(rdata)); |
| 552 |
break; |
| 553 |
default: |
| 554 |
ast_debug(3, "Taskprocessor overload on non-udp transport. Received:'%s'. " |
| 555 |
"Responding with a 503.\n", pjsip_rx_data_get_info(rdata)); |
| 556 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, |
| 557 |
PJSIP_SC_SERVICE_UNAVAILABLE, NULL, NULL, NULL); |
| 558 |
break; |
| 559 |
} |
| 560 |
ao2_cleanup(dist); |
| 561 |
return PJ_TRUE; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
serializer = ast_sip_get_distributor_serializer(rdata); |
| 566 |
} |
| 567 |
|
| 568 |
if (pjsip_rx_data_clone(rdata, 0, &clone) != PJ_SUCCESS) { |
| 569 |
ast_taskprocessor_unreference(serializer); |
| 570 |
ao2_cleanup(dist); |
| 571 |
return PJ_TRUE; |
| 572 |
} |
| 573 |
|
| 574 |
if (dist) { |
| 575 |
ao2_lock(dist); |
| 576 |
clone->endpt_info.mod_data[endpoint_mod.id] = ao2_bump(dist->endpoint); |
| 577 |
ao2_unlock(dist); |
| 578 |
ao2_cleanup(dist); |
| 579 |
} |
| 580 |
|
| 581 |
if (ast_sip_push_task(serializer, distribute, clone)) { |
| 582 |
ao2_cleanup(clone->endpt_info.mod_data[endpoint_mod.id]); |
| 583 |
pjsip_rx_data_free_cloned(clone); |
| 584 |
} |
| 585 |
|
| 586 |
ast_taskprocessor_unreference(serializer); |
| 587 |
|
| 588 |
return PJ_TRUE; |
| 589 |
} |
| 590 |
|
| 591 |
static struct ast_sip_auth *alloc_artificial_auth(char *default_realm, |
| 592 |
char *default_algos_uac, char *default_algos_uas) |
| 593 |
{ |
| 594 |
struct ast_sip_auth *fake_auth; |
| 595 |
|
| 596 |
fake_auth = ast_sorcery_alloc(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, |
| 597 |
"artificial"); |
| 598 |
if (!fake_auth) { |
| 599 |
return NULL; |
| 600 |
} |
| 601 |
|
| 602 |
ast_string_field_set(fake_auth, realm, default_realm); |
| 603 |
ast_string_field_set(fake_auth, auth_user, ""); |
| 604 |
ast_string_field_set(fake_auth, auth_pass, ""); |
| 605 |
|
| 606 |
ast_sip_auth_digest_algorithms_vector_init("artificial", |
| 607 |
&fake_auth->supported_algorithms_uac, "UAC", default_algos_uac); |
| 608 |
|
| 609 |
ast_sip_auth_digest_algorithms_vector_init("artificial", |
| 610 |
&fake_auth->supported_algorithms_uas, "UAS", default_algos_uas); |
| 611 |
|
| 612 |
fake_auth->type = AST_SIP_AUTH_TYPE_ARTIFICIAL; |
| 613 |
|
| 614 |
return fake_auth; |
| 615 |
} |
| 616 |
|
| 617 |
static AO2_GLOBAL_OBJ_STATIC(artificial_auth); |
| 618 |
|
| 619 |
static int create_artificial_auth(void) |
| 620 |
{ |
| 621 |
char default_realm[AST_SIP_AUTH_MAX_REALM_LENGTH + 1]; |
| 622 |
struct ast_sip_auth *fake_auth; |
| 623 |
char default_algos_uac[AST_SIP_AUTH_MAX_SUPPORTED_ALGORITHMS_LENGTH + 1]; |
| 624 |
char default_algos_uas[AST_SIP_AUTH_MAX_SUPPORTED_ALGORITHMS_LENGTH + 1]; |
| 625 |
|
| 626 |
ast_sip_get_default_realm(default_realm, sizeof(default_realm)); |
| 627 |
ast_sip_get_default_auth_algorithms_uac(default_algos_uac, |
| 628 |
sizeof(default_algos_uac)); |
| 629 |
ast_sip_get_default_auth_algorithms_uas(default_algos_uas, |
| 630 |
sizeof(default_algos_uas)); |
| 631 |
|
| 632 |
fake_auth = alloc_artificial_auth(default_realm, default_algos_uac, |
| 633 |
default_algos_uas); |
| 634 |
if (!fake_auth) { |
| 635 |
ast_log(LOG_ERROR, "Unable to create artificial auth\n"); |
| 636 |
return -1; |
| 637 |
} |
| 638 |
ao2_global_obj_replace_unref(artificial_auth, fake_auth); |
| 639 |
ao2_cleanup(fake_auth); |
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
struct ast_sip_auth *ast_sip_get_artificial_auth(void) |
| 644 |
{ |
| 645 |
return ao2_global_obj_ref(artificial_auth); |
| 646 |
} |
| 647 |
|
| 648 |
static struct ast_sip_endpoint *artificial_endpoint = NULL; |
| 649 |
|
| 650 |
static int create_artificial_endpoint(void) |
| 651 |
{ |
| 652 |
artificial_endpoint = ast_sorcery_alloc(ast_sip_get_sorcery(), "endpoint", NULL); |
| 653 |
if (!artificial_endpoint) { |
| 654 |
return -1; |
| 655 |
} |
| 656 |
|
| 657 |
AST_VECTOR_INIT(&artificial_endpoint->inbound_auths, 1); |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
AST_VECTOR_APPEND(&artificial_endpoint->inbound_auths, ast_strdup("artificial-auth")); |
| 663 |
return 0; |
| 664 |
} |
| 665 |
|
| 666 |
struct ast_sip_endpoint *ast_sip_get_artificial_endpoint(void) |
| 667 |
{ |
| 668 |
ao2_ref(artificial_endpoint, +1); |
| 669 |
return artificial_endpoint; |
| 670 |
} |
| 671 |
|
| 672 |
static void log_failed_request(pjsip_rx_data *rdata, char *msg, unsigned int count, unsigned int period) |
| 673 |
{ |
| 674 |
char from_buf[PJSIP_MAX_URL_SIZE]; |
| 675 |
char callid_buf[PJSIP_MAX_URL_SIZE]; |
| 676 |
char method_buf[PJSIP_MAX_URL_SIZE]; |
| 677 |
char src_addr_buf[AST_SOCKADDR_BUFLEN]; |
| 678 |
pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, rdata->msg_info.from->uri, from_buf, PJSIP_MAX_URL_SIZE); |
| 679 |
ast_copy_pj_str(callid_buf, &rdata->msg_info.cid->id, PJSIP_MAX_URL_SIZE); |
| 680 |
ast_copy_pj_str(method_buf, &rdata->msg_info.msg->line.req.method.name, PJSIP_MAX_URL_SIZE); |
| 681 |
if (count) { |
| 682 |
ast_log(LOG_NOTICE, "Request '%s' from '%s' failed for '%s' (callid: %s) - %s" |
| 683 |
" after %u tries in %.3f ms\n", |
| 684 |
method_buf, from_buf, |
| 685 |
pj_sockaddr_print(&rdata->pkt_info.src_addr, src_addr_buf, sizeof(src_addr_buf), 3), |
| 686 |
callid_buf, msg, count, period / 1000.0); |
| 687 |
} else { |
| 688 |
ast_log(LOG_NOTICE, "Request '%s' from '%s' failed for '%s' (callid: %s) - %s\n", |
| 689 |
method_buf, from_buf, |
| 690 |
pj_sockaddr_print(&rdata->pkt_info.src_addr, src_addr_buf, sizeof(src_addr_buf), 3), |
| 691 |
callid_buf, msg); |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
static void check_endpoint(pjsip_rx_data *rdata, struct unidentified_request *unid, |
| 696 |
const char *name) |
| 697 |
{ |
| 698 |
int64_t ms = ast_tvdiff_ms(ast_tvnow(), unid->first_seen); |
| 699 |
|
| 700 |
ao2_wrlock(unid); |
| 701 |
unid->count++; |
| 702 |
|
| 703 |
if (ms < (unidentified_period * 1000) && unid->count >= unidentified_count) { |
| 704 |
log_failed_request(rdata, "No matching endpoint found", unid->count, ms); |
| 705 |
ast_sip_report_invalid_endpoint(name, rdata); |
| 706 |
} |
| 707 |
ao2_unlock(unid); |
| 708 |
} |
| 709 |
|
| 710 |
static int apply_endpoint_acl(pjsip_rx_data *rdata, struct ast_sip_endpoint *endpoint); |
| 711 |
static int apply_endpoint_contact_acl(pjsip_rx_data *rdata, struct ast_sip_endpoint *endpoint); |
| 712 |
|
| 713 |
static void apply_acls(pjsip_rx_data *rdata) |
| 714 |
{ |
| 715 |
struct ast_sip_endpoint *endpoint; |
| 716 |
|
| 717 |
|
| 718 |
endpoint = rdata->endpt_info.mod_data[endpoint_mod.id]; |
| 719 |
if (endpoint != artificial_endpoint |
| 720 |
&& (apply_endpoint_acl(rdata, endpoint) |
| 721 |
|| apply_endpoint_contact_acl(rdata, endpoint))) { |
| 722 |
ast_debug(1, "Endpoint '%s' not allowed by ACL\n", |
| 723 |
ast_sorcery_object_get_id(endpoint)); |
| 724 |
|
| 725 |
|
| 726 |
ao2_replace(rdata->endpt_info.mod_data[endpoint_mod.id], artificial_endpoint); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
static pj_bool_t endpoint_lookup(pjsip_rx_data *rdata) |
| 731 |
{ |
| 732 |
struct ast_sip_endpoint *endpoint; |
| 733 |
struct unidentified_request *unid; |
| 734 |
int is_ack = rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD; |
| 735 |
|
| 736 |
endpoint = rdata->endpt_info.mod_data[endpoint_mod.id]; |
| 737 |
if (endpoint) { |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
unid = ao2_find(unidentified_requests, rdata->pkt_info.src_name, OBJ_SEARCH_KEY); |
| 745 |
if (unid) { |
| 746 |
ao2_unlink(unidentified_requests, unid); |
| 747 |
ao2_ref(unid, -1); |
| 748 |
} |
| 749 |
apply_acls(rdata); |
| 750 |
return PJ_FALSE; |
| 751 |
} |
| 752 |
|
| 753 |
endpoint = ast_sip_identify_endpoint(rdata); |
| 754 |
if (endpoint) { |
| 755 |
unid = ao2_find(unidentified_requests, rdata->pkt_info.src_name, OBJ_SEARCH_KEY); |
| 756 |
if (unid) { |
| 757 |
ao2_unlink(unidentified_requests, unid); |
| 758 |
ao2_ref(unid, -1); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
if (!endpoint) { |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
endpoint = ast_sip_get_artificial_endpoint(); |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
rdata->endpt_info.mod_data[endpoint_mod.id] = endpoint; |
| 773 |
|
| 774 |
if (endpoint == artificial_endpoint && !is_ack) { |
| 775 |
char name[AST_UUID_STR_LEN] = ""; |
| 776 |
pjsip_uri *from = rdata->msg_info.from->uri; |
| 777 |
|
| 778 |
if (ast_sip_is_allowed_uri(from)) { |
| 779 |
ast_copy_pj_str(name, ast_sip_pjsip_uri_get_username(from), sizeof(name)); |
| 780 |
} |
| 781 |
|
| 782 |
unid = ao2_find(unidentified_requests, rdata->pkt_info.src_name, OBJ_SEARCH_KEY); |
| 783 |
if (unid) { |
| 784 |
check_endpoint(rdata, unid, name); |
| 785 |
ao2_ref(unid, -1); |
| 786 |
} else if (using_auth_username) { |
| 787 |
ao2_wrlock(unidentified_requests); |
| 788 |
|
| 789 |
unid = ao2_find(unidentified_requests, rdata->pkt_info.src_name, |
| 790 |
OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 791 |
if (unid) { |
| 792 |
check_endpoint(rdata, unid, name); |
| 793 |
} else { |
| 794 |
unid = ao2_alloc_options(sizeof(*unid) + strlen(rdata->pkt_info.src_name) + 1, |
| 795 |
NULL, AO2_ALLOC_OPT_LOCK_RWLOCK); |
| 796 |
if (!unid) { |
| 797 |
ao2_unlock(unidentified_requests); |
| 798 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL); |
| 799 |
return PJ_TRUE; |
| 800 |
} |
| 801 |
strcpy(unid->src_name, rdata->pkt_info.src_name); |
| 802 |
unid->first_seen = ast_tvnow(); |
| 803 |
unid->count = 1; |
| 804 |
ao2_link_flags(unidentified_requests, unid, OBJ_NOLOCK); |
| 805 |
} |
| 806 |
ao2_ref(unid, -1); |
| 807 |
ao2_unlock(unidentified_requests); |
| 808 |
} else { |
| 809 |
log_failed_request(rdata, "No matching endpoint found", 0, 0); |
| 810 |
ast_sip_report_invalid_endpoint(name, rdata); |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
apply_acls(rdata); |
| 815 |
return PJ_FALSE; |
| 816 |
} |
| 817 |
|
| 818 |
static int apply_endpoint_acl(pjsip_rx_data *rdata, struct ast_sip_endpoint *endpoint) |
| 819 |
{ |
| 820 |
struct ast_sockaddr addr; |
| 821 |
|
| 822 |
if (ast_acl_list_is_empty(endpoint->acl)) { |
| 823 |
return 0; |
| 824 |
} |
| 825 |
|
| 826 |
memset(&addr, 0, sizeof(addr)); |
| 827 |
ast_sockaddr_parse(&addr, rdata->pkt_info.src_name, PARSE_PORT_FORBID); |
| 828 |
ast_sockaddr_set_port(&addr, rdata->pkt_info.src_port); |
| 829 |
|
| 830 |
if (ast_apply_acl(endpoint->acl, &addr, "SIP ACL: ") != AST_SENSE_ALLOW) { |
| 831 |
log_failed_request(rdata, "Not match Endpoint ACL", 0, 0); |
| 832 |
ast_sip_report_failed_acl(endpoint, rdata, "not_match_endpoint_acl"); |
| 833 |
return 1; |
| 834 |
} |
| 835 |
return 0; |
| 836 |
} |
| 837 |
|
| 838 |
static int extract_contact_addr(pjsip_contact_hdr *contact, struct ast_sockaddr **addrs) |
| 839 |
{ |
| 840 |
pjsip_sip_uri *sip_uri; |
| 841 |
char host[256]; |
| 842 |
|
| 843 |
if (!contact || contact->star) { |
| 844 |
*addrs = NULL; |
| 845 |
return 0; |
| 846 |
} |
| 847 |
|
| 848 |
if (!PJSIP_URI_SCHEME_IS_SIP(contact->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact->uri)) { |
| 849 |
*addrs = NULL; |
| 850 |
return 0; |
| 851 |
} |
| 852 |
sip_uri = pjsip_uri_get_uri(contact->uri); |
| 853 |
ast_copy_pj_str(host, &sip_uri->host, sizeof(host)); |
| 854 |
return ast_sockaddr_resolve(addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC); |
| 855 |
} |
| 856 |
|
| 857 |
static int apply_endpoint_contact_acl(pjsip_rx_data *rdata, struct ast_sip_endpoint *endpoint) |
| 858 |
{ |
| 859 |
int num_contact_addrs; |
| 860 |
int forbidden = 0; |
| 861 |
struct ast_sockaddr *contact_addrs; |
| 862 |
int i; |
| 863 |
pjsip_contact_hdr *contact = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr; |
| 864 |
|
| 865 |
if (ast_acl_list_is_empty(endpoint->contact_acl)) { |
| 866 |
return 0; |
| 867 |
} |
| 868 |
|
| 869 |
while ((contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next))) { |
| 870 |
num_contact_addrs = extract_contact_addr(contact, &contact_addrs); |
| 871 |
if (num_contact_addrs <= 0) { |
| 872 |
continue; |
| 873 |
} |
| 874 |
for (i = 0; i < num_contact_addrs; ++i) { |
| 875 |
if (ast_apply_acl(endpoint->contact_acl, &contact_addrs[i], "SIP Contact ACL: ") != AST_SENSE_ALLOW) { |
| 876 |
log_failed_request(rdata, "Not match Endpoint Contact ACL", 0, 0); |
| 877 |
ast_sip_report_failed_acl(endpoint, rdata, "not_match_endpoint_contact_acl"); |
| 878 |
forbidden = 1; |
| 879 |
break; |
| 880 |
} |
| 881 |
} |
| 882 |
ast_free(contact_addrs); |
| 883 |
if (forbidden) { |
| 884 |
|
| 885 |
break; |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
return forbidden; |
| 890 |
} |
| 891 |
|
| 892 |
static pj_bool_t authenticate(pjsip_rx_data *rdata) |
| 893 |
{ |
| 894 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_pjsip_rdata_get_endpoint(rdata), ao2_cleanup); |
| 895 |
int is_ack = rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD; |
| 896 |
|
| 897 |
ast_assert(endpoint != NULL); |
| 898 |
|
| 899 |
if (is_ack) { |
| 900 |
return PJ_FALSE; |
| 901 |
} |
| 902 |
|
| 903 |
if (ast_sip_requires_authentication(endpoint, rdata)) { |
| 904 |
pjsip_tx_data *tdata; |
| 905 |
struct unidentified_request *unid; |
| 906 |
|
| 907 |
pjsip_endpt_create_response(ast_sip_get_pjsip_endpoint(), rdata, 401, NULL, &tdata); |
| 908 |
switch (ast_sip_check_authentication(endpoint, rdata, tdata)) { |
| 909 |
case AST_SIP_AUTHENTICATION_CHALLENGE: |
| 910 |
|
| 911 |
ast_sip_report_auth_challenge_sent(endpoint, rdata, tdata); |
| 912 |
if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) { |
| 913 |
pjsip_tx_data_dec_ref(tdata); |
| 914 |
} |
| 915 |
return PJ_TRUE; |
| 916 |
case AST_SIP_AUTHENTICATION_SUCCESS: |
| 917 |
|
| 918 |
unid = ao2_find(unidentified_requests, rdata->pkt_info.src_name, OBJ_SEARCH_KEY); |
| 919 |
if (unid) { |
| 920 |
ao2_unlink(unidentified_requests, unid); |
| 921 |
ao2_ref(unid, -1); |
| 922 |
} |
| 923 |
ast_sip_report_auth_success(endpoint, rdata); |
| 924 |
break; |
| 925 |
case AST_SIP_AUTHENTICATION_FAILED: |
| 926 |
log_failed_request(rdata, "Failed to authenticate", 0, 0); |
| 927 |
ast_sip_report_auth_failed_challenge_response(endpoint, rdata); |
| 928 |
if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) { |
| 929 |
pjsip_tx_data_dec_ref(tdata); |
| 930 |
} |
| 931 |
return PJ_TRUE; |
| 932 |
case AST_SIP_AUTHENTICATION_ERROR: |
| 933 |
log_failed_request(rdata, "Error to authenticate", 0, 0); |
| 934 |
ast_sip_report_auth_failed_challenge_response(endpoint, rdata); |
| 935 |
pjsip_tx_data_dec_ref(tdata); |
| 936 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL); |
| 937 |
return PJ_TRUE; |
| 938 |
} |
| 939 |
pjsip_tx_data_dec_ref(tdata); |
| 940 |
} else if (endpoint == artificial_endpoint) { |
| 941 |
|
| 942 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL); |
| 943 |
return PJ_TRUE; |
| 944 |
} |
| 945 |
|
| 946 |
return PJ_FALSE; |
| 947 |
} |
| 948 |
|
| 949 |
static pjsip_module auth_mod = { |
| 950 |
.name = {"Request Authenticator", 21}, |
| 951 |
.priority = PJSIP_MOD_PRIORITY_APPLICATION - 2, |
| 952 |
.on_rx_request = authenticate, |
| 953 |
}; |
| 954 |
|
| 955 |
static int distribute(void *data) |
| 956 |
{ |
| 957 |
static pjsip_process_rdata_param param = { |
| 958 |
.start_mod = &distributor_mod, |
| 959 |
.idx_after_start = 1, |
| 960 |
}; |
| 961 |
pj_bool_t handled = PJ_FALSE; |
| 962 |
pjsip_rx_data *rdata = data; |
| 963 |
int is_request = rdata->msg_info.msg->type == PJSIP_REQUEST_MSG; |
| 964 |
int is_ack = is_request ? rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD : 0; |
| 965 |
struct ast_sip_endpoint *endpoint; |
| 966 |
|
| 967 |
pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(), rdata, ¶m, &handled); |
| 968 |
if (!handled && is_request && !is_ack) { |
| 969 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 501, NULL, NULL, NULL); |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
endpoint = rdata->endpt_info.mod_data[endpoint_mod.id]; |
| 976 |
ao2_cleanup(endpoint); |
| 977 |
pjsip_rx_data_free_cloned(rdata); |
| 978 |
return 0; |
| 979 |
} |
| 980 |
|
| 981 |
struct ast_sip_endpoint *ast_pjsip_rdata_get_endpoint(pjsip_rx_data *rdata) |
| 982 |
{ |
| 983 |
struct ast_sip_endpoint *endpoint = rdata->endpt_info.mod_data[endpoint_mod.id]; |
| 984 |
if (endpoint) { |
| 985 |
ao2_ref(endpoint, +1); |
| 986 |
} |
| 987 |
return endpoint; |
| 988 |
} |
| 989 |
|
| 990 |
static int suspects_sort(const void *obj, const void *arg, int flags) |
| 991 |
{ |
| 992 |
const struct unidentified_request *object_left = obj; |
| 993 |
const struct unidentified_request *object_right = arg; |
| 994 |
const char *right_key = arg; |
| 995 |
int cmp; |
| 996 |
|
| 997 |
switch (flags & OBJ_SEARCH_MASK) { |
| 998 |
case OBJ_SEARCH_OBJECT: |
| 999 |
right_key = object_right->src_name; |
| 1000 |
|
| 1001 |
case OBJ_SEARCH_KEY: |
| 1002 |
cmp = strcmp(object_left->src_name, right_key); |
| 1003 |
break; |
| 1004 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 1005 |
cmp = strncmp(object_left->src_name, right_key, strlen(right_key)); |
| 1006 |
break; |
| 1007 |
default: |
| 1008 |
cmp = 0; |
| 1009 |
break; |
| 1010 |
} |
| 1011 |
return cmp; |
| 1012 |
} |
| 1013 |
|
| 1014 |
static int suspects_compare(void *obj, void *arg, int flags) |
| 1015 |
{ |
| 1016 |
const struct unidentified_request *object_left = obj; |
| 1017 |
const struct unidentified_request *object_right = arg; |
| 1018 |
const char *right_key = arg; |
| 1019 |
int cmp = 0; |
| 1020 |
|
| 1021 |
switch (flags & OBJ_SEARCH_MASK) { |
| 1022 |
case OBJ_SEARCH_OBJECT: |
| 1023 |
right_key = object_right->src_name; |
| 1024 |
|
| 1025 |
case OBJ_SEARCH_KEY: |
| 1026 |
if (strcmp(object_left->src_name, right_key) == 0) { |
| 1027 |
cmp = CMP_MATCH; |
| 1028 |
} |
| 1029 |
break; |
| 1030 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 1031 |
if (strncmp(object_left->src_name, right_key, strlen(right_key)) == 0) { |
| 1032 |
cmp = CMP_MATCH; |
| 1033 |
} |
| 1034 |
break; |
| 1035 |
default: |
| 1036 |
cmp = 0; |
| 1037 |
break; |
| 1038 |
} |
| 1039 |
return cmp; |
| 1040 |
} |
| 1041 |
|
| 1042 |
static int suspects_hash(const void *obj, int flags) |
| 1043 |
{ |
| 1044 |
const struct unidentified_request *object; |
| 1045 |
const char *key; |
| 1046 |
|
| 1047 |
switch (flags & OBJ_SEARCH_MASK) { |
| 1048 |
case OBJ_SEARCH_KEY: |
| 1049 |
key = obj; |
| 1050 |
break; |
| 1051 |
case OBJ_SEARCH_OBJECT: |
| 1052 |
object = obj; |
| 1053 |
key = object->src_name; |
| 1054 |
break; |
| 1055 |
default: |
| 1056 |
|
| 1057 |
ast_assert(0); |
| 1058 |
return 0; |
| 1059 |
} |
| 1060 |
return ast_str_hash(key); |
| 1061 |
} |
| 1062 |
|
| 1063 |
static struct ao2_container *cli_unid_get_container(const char *regex) |
| 1064 |
{ |
| 1065 |
struct ao2_container *s_container; |
| 1066 |
|
| 1067 |
s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 1068 |
suspects_sort, suspects_compare); |
| 1069 |
if (!s_container) { |
| 1070 |
return NULL; |
| 1071 |
} |
| 1072 |
|
| 1073 |
if (ao2_container_dup(s_container, unidentified_requests, 0)) { |
| 1074 |
ao2_ref(s_container, -1); |
| 1075 |
return NULL; |
| 1076 |
} |
| 1077 |
|
| 1078 |
return s_container; |
| 1079 |
} |
| 1080 |
|
| 1081 |
static int cli_unid_iterate(void *container, ao2_callback_fn callback, void *args) |
| 1082 |
{ |
| 1083 |
ao2_callback(container, 0, callback, args); |
| 1084 |
|
| 1085 |
return 0; |
| 1086 |
} |
| 1087 |
|
| 1088 |
static void *cli_unid_retrieve_by_id(const char *id) |
| 1089 |
{ |
| 1090 |
return ao2_find(unidentified_requests, id, OBJ_SEARCH_KEY); |
| 1091 |
} |
| 1092 |
|
| 1093 |
static const char *cli_unid_get_id(const void *obj) |
| 1094 |
{ |
| 1095 |
const struct unidentified_request *unid = obj; |
| 1096 |
|
| 1097 |
return unid->src_name; |
| 1098 |
} |
| 1099 |
|
| 1100 |
static int cli_unid_print_header(void *obj, void *arg, int flags) |
| 1101 |
{ |
| 1102 |
struct ast_sip_cli_context *context = arg; |
| 1103 |
RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup); |
| 1104 |
|
| 1105 |
int indent = CLI_INDENT_TO_SPACES(context->indent_level); |
| 1106 |
int filler = CLI_LAST_TABSTOP - indent - 7; |
| 1107 |
|
| 1108 |
ast_assert(context->output_buffer != NULL); |
| 1109 |
|
| 1110 |
ast_str_append(&context->output_buffer, 0, |
| 1111 |
"%*s: <IP Address%*.*s> <Count> <Age(sec)>\n", |
| 1112 |
indent, "Request", filler, filler, CLI_HEADER_FILLER); |
| 1113 |
|
| 1114 |
return 0; |
| 1115 |
} |
| 1116 |
|
| 1117 |
static int cli_unid_print_body(void *obj, void *arg, int flags) |
| 1118 |
{ |
| 1119 |
struct unidentified_request *unid = obj; |
| 1120 |
struct ast_sip_cli_context *context = arg; |
| 1121 |
int indent; |
| 1122 |
int flexwidth; |
| 1123 |
int64_t ms = ast_tvdiff_ms(ast_tvnow(), unid->first_seen); |
| 1124 |
|
| 1125 |
ast_assert(context->output_buffer != NULL); |
| 1126 |
|
| 1127 |
indent = CLI_INDENT_TO_SPACES(context->indent_level); |
| 1128 |
flexwidth = CLI_LAST_TABSTOP - 4; |
| 1129 |
|
| 1130 |
ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %7d %10.3f\n", |
| 1131 |
indent, |
| 1132 |
"Request", |
| 1133 |
flexwidth, flexwidth, |
| 1134 |
unid->src_name, unid->count, ms / 1000.0); |
| 1135 |
|
| 1136 |
return 0; |
| 1137 |
} |
| 1138 |
|
| 1139 |
static struct ast_cli_entry cli_commands[] = { |
| 1140 |
AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Unidentified Requests", |
| 1141 |
.command = "pjsip show unidentified_requests", |
| 1142 |
.usage = "Usage: pjsip show unidentified_requests\n" |
| 1143 |
" Show the PJSIP Unidentified Requests\n"), |
| 1144 |
}; |
| 1145 |
|
| 1146 |
struct ast_sip_cli_formatter_entry *unid_formatter; |
| 1147 |
|
| 1148 |
static int expire_requests(void *object, void *arg, int flags) |
| 1149 |
{ |
| 1150 |
struct unidentified_request *unid = object; |
| 1151 |
int *maxage = arg; |
| 1152 |
int64_t ms = ast_tvdiff_ms(ast_tvnow(), unid->first_seen); |
| 1153 |
|
| 1154 |
if (ms > (*maxage) * 2 * 1000) { |
| 1155 |
return CMP_MATCH; |
| 1156 |
} |
| 1157 |
|
| 1158 |
return 0; |
| 1159 |
} |
| 1160 |
|
| 1161 |
static int prune_task(const void *data) |
| 1162 |
{ |
| 1163 |
unsigned int maxage; |
| 1164 |
|
| 1165 |
ast_sip_get_unidentified_request_thresholds(&unidentified_count, &unidentified_period, &unidentified_prune_interval); |
| 1166 |
maxage = unidentified_period * 2; |
| 1167 |
ao2_callback(unidentified_requests, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK, expire_requests, &maxage); |
| 1168 |
|
| 1169 |
return unidentified_prune_interval * 1000; |
| 1170 |
} |
| 1171 |
|
| 1172 |
static int clean_task(const void *data) |
| 1173 |
{ |
| 1174 |
return 0; |
| 1175 |
} |
| 1176 |
|
| 1177 |
static void global_loaded(const char *object_type) |
| 1178 |
{ |
| 1179 |
char *identifier_order; |
| 1180 |
|
| 1181 |
|
| 1182 |
identifier_order = ast_sip_get_endpoint_identifier_order(); |
| 1183 |
if (identifier_order) { |
| 1184 |
char *identify_method; |
| 1185 |
char *io_copy = ast_strdupa(identifier_order); |
| 1186 |
int new_using = 0; |
| 1187 |
|
| 1188 |
ast_free(identifier_order); |
| 1189 |
while ((identify_method = ast_strip(strsep(&io_copy, ",")))) { |
| 1190 |
if (!strcmp(identify_method, "auth_username")) { |
| 1191 |
new_using = 1; |
| 1192 |
break; |
| 1193 |
} |
| 1194 |
} |
| 1195 |
using_auth_username = new_using; |
| 1196 |
} |
| 1197 |
|
| 1198 |
create_artificial_auth(); |
| 1199 |
|
| 1200 |
ast_sip_get_unidentified_request_thresholds(&unidentified_count, &unidentified_period, &unidentified_prune_interval); |
| 1201 |
|
| 1202 |
overload_trigger = ast_sip_get_taskprocessor_overload_trigger(); |
| 1203 |
|
| 1204 |
|
| 1205 |
ast_sched_clean_by_callback(prune_context, prune_task, clean_task); |
| 1206 |
|
| 1207 |
if (ast_sched_add_variable(prune_context, unidentified_prune_interval * 1000, prune_task, NULL, 1) < 0) { |
| 1208 |
return; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
|
| 1213 |
static struct ast_sorcery_observer global_observer = { |
| 1214 |
.loaded = global_loaded, |
| 1215 |
}; |
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
static void distributor_pool_shutdown(void) |
| 1223 |
{ |
| 1224 |
int idx; |
| 1225 |
|
| 1226 |
for (idx = 0; idx < ARRAY_LEN(distributor_pool); ++idx) { |
| 1227 |
ast_taskprocessor_unreference(distributor_pool[idx]); |
| 1228 |
distributor_pool[idx] = NULL; |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
static int distributor_pool_setup(void) |
| 1241 |
{ |
| 1242 |
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1]; |
| 1243 |
int idx; |
| 1244 |
|
| 1245 |
for (idx = 0; idx < ARRAY_LEN(distributor_pool); ++idx) { |
| 1246 |
|
| 1247 |
ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/distributor"); |
| 1248 |
|
| 1249 |
distributor_pool[idx] = ast_sip_create_serializer(tps_name); |
| 1250 |
if (!distributor_pool[idx]) { |
| 1251 |
return -1; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
return 0; |
| 1255 |
} |
| 1256 |
|
| 1257 |
int ast_sip_initialize_distributor(void) |
| 1258 |
{ |
| 1259 |
unidentified_requests = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, |
| 1260 |
DEFAULT_SUSPECTS_BUCKETS, suspects_hash, NULL, suspects_compare); |
| 1261 |
if (!unidentified_requests) { |
| 1262 |
return -1; |
| 1263 |
} |
| 1264 |
|
| 1265 |
dialog_associations = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, |
| 1266 |
DIALOG_ASSOCIATIONS_BUCKETS, dialog_associations_hash, NULL, |
| 1267 |
dialog_associations_cmp); |
| 1268 |
if (!dialog_associations) { |
| 1269 |
ast_sip_destroy_distributor(); |
| 1270 |
return -1; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if (distributor_pool_setup()) { |
| 1274 |
ast_sip_destroy_distributor(); |
| 1275 |
return -1; |
| 1276 |
} |
| 1277 |
|
| 1278 |
prune_context = ast_sched_context_create(); |
| 1279 |
if (!prune_context) { |
| 1280 |
ast_sip_destroy_distributor(); |
| 1281 |
return -1; |
| 1282 |
} |
| 1283 |
|
| 1284 |
if (ast_sched_start_thread(prune_context)) { |
| 1285 |
ast_sip_destroy_distributor(); |
| 1286 |
return -1; |
| 1287 |
} |
| 1288 |
|
| 1289 |
ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &global_observer); |
| 1290 |
ast_sorcery_reload_object(ast_sip_get_sorcery(), "global"); |
| 1291 |
|
| 1292 |
if (create_artificial_endpoint() || create_artificial_auth()) { |
| 1293 |
ast_sip_destroy_distributor(); |
| 1294 |
return -1; |
| 1295 |
} |
| 1296 |
|
| 1297 |
if (ast_sip_register_service(&distributor_mod)) { |
| 1298 |
ast_sip_destroy_distributor(); |
| 1299 |
return -1; |
| 1300 |
} |
| 1301 |
if (ast_sip_register_service(&endpoint_mod)) { |
| 1302 |
ast_sip_destroy_distributor(); |
| 1303 |
return -1; |
| 1304 |
} |
| 1305 |
if (ast_sip_register_service(&auth_mod)) { |
| 1306 |
ast_sip_destroy_distributor(); |
| 1307 |
return -1; |
| 1308 |
} |
| 1309 |
|
| 1310 |
unid_formatter = ao2_alloc_options(sizeof(struct ast_sip_cli_formatter_entry), NULL, |
| 1311 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 1312 |
if (!unid_formatter) { |
| 1313 |
ast_sip_destroy_distributor(); |
| 1314 |
ast_log(LOG_ERROR, "Unable to allocate memory for unid_formatter\n"); |
| 1315 |
return -1; |
| 1316 |
} |
| 1317 |
unid_formatter->name = "unidentified_request"; |
| 1318 |
unid_formatter->print_header = cli_unid_print_header; |
| 1319 |
unid_formatter->print_body = cli_unid_print_body; |
| 1320 |
unid_formatter->get_container = cli_unid_get_container; |
| 1321 |
unid_formatter->iterate = cli_unid_iterate; |
| 1322 |
unid_formatter->get_id = cli_unid_get_id; |
| 1323 |
unid_formatter->retrieve_by_id = cli_unid_retrieve_by_id; |
| 1324 |
ast_sip_register_cli_formatter(unid_formatter); |
| 1325 |
|
| 1326 |
ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands)); |
| 1327 |
|
| 1328 |
return 0; |
| 1329 |
} |
| 1330 |
|
| 1331 |
void ast_sip_destroy_distributor(void) |
| 1332 |
{ |
| 1333 |
ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands)); |
| 1334 |
ast_sip_unregister_cli_formatter(unid_formatter); |
| 1335 |
|
| 1336 |
ast_sip_unregister_service(&auth_mod); |
| 1337 |
ast_sip_unregister_service(&endpoint_mod); |
| 1338 |
ast_sip_unregister_service(&distributor_mod); |
| 1339 |
|
| 1340 |
ao2_global_obj_release(artificial_auth); |
| 1341 |
ao2_cleanup(artificial_endpoint); |
| 1342 |
|
| 1343 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "global", &global_observer); |
| 1344 |
|
| 1345 |
if (prune_context) { |
| 1346 |
ast_sched_context_destroy(prune_context); |
| 1347 |
} |
| 1348 |
|
| 1349 |
distributor_pool_shutdown(); |
| 1350 |
|
| 1351 |
ao2_cleanup(dialog_associations); |
| 1352 |
ao2_cleanup(unidentified_requests); |
| 1353 |
} |