| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/res_pjsip.h" |
| 33 |
#include "asterisk/res_pjsip_cli.h" |
| 34 |
#include "include/res_pjsip_private.h" |
| 35 |
#include "asterisk/linkedlists.h" |
| 36 |
#include "asterisk/vector.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#define ACTIVE_TRANSPORTS_BUCKETS 127 |
| 42 |
|
| 43 |
|
| 44 |
struct transport_monitor_notifier { |
| 45 |
|
| 46 |
ast_transport_monitor_shutdown_cb cb; |
| 47 |
|
| 48 |
void *data; |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
struct transport_monitor { |
| 53 |
|
| 54 |
char key[IP6ADDR_COLON_PORT_BUFLEN]; |
| 55 |
|
| 56 |
pjsip_transport *transport; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
char *transport_obj_name; |
| 61 |
|
| 62 |
AST_VECTOR(, struct transport_monitor_notifier) monitors; |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
static AO2_GLOBAL_OBJ_STATIC(active_transports); |
| 67 |
|
| 68 |
|
| 69 |
static pjsip_tp_state_callback tpmgr_state_callback; |
| 70 |
|
| 71 |
|
| 72 |
static AST_RWLIST_HEAD(, ast_sip_tpmgr_state_callback) transport_state_list; |
| 73 |
|
| 74 |
|
| 75 |
AO2_STRING_FIELD_HASH_FN(transport_monitor, key); |
| 76 |
|
| 77 |
|
| 78 |
AO2_STRING_FIELD_CMP_FN(transport_monitor, key); |
| 79 |
|
| 80 |
|
| 81 |
AO2_STRING_FIELD_SORT_FN(transport_monitor, key); |
| 82 |
|
| 83 |
static const char *transport_state2str(pjsip_transport_state state) |
| 84 |
{ |
| 85 |
const char *name; |
| 86 |
|
| 87 |
switch (state) { |
| 88 |
case PJSIP_TP_STATE_CONNECTED: |
| 89 |
name = "CONNECTED"; |
| 90 |
break; |
| 91 |
case PJSIP_TP_STATE_DISCONNECTED: |
| 92 |
name = "DISCONNECTED"; |
| 93 |
break; |
| 94 |
case PJSIP_TP_STATE_SHUTDOWN: |
| 95 |
name = "SHUTDOWN"; |
| 96 |
break; |
| 97 |
case PJSIP_TP_STATE_DESTROY: |
| 98 |
name = "DESTROY"; |
| 99 |
break; |
| 100 |
default: |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
ast_assert(0); |
| 106 |
name = "<unknown>"; |
| 107 |
break; |
| 108 |
} |
| 109 |
return name; |
| 110 |
} |
| 111 |
|
| 112 |
static void transport_monitor_dtor(void *vdoomed) |
| 113 |
{ |
| 114 |
struct transport_monitor *monitored = vdoomed; |
| 115 |
int idx; |
| 116 |
|
| 117 |
for (idx = AST_VECTOR_SIZE(&monitored->monitors); idx--;) { |
| 118 |
struct transport_monitor_notifier *notifier; |
| 119 |
|
| 120 |
notifier = AST_VECTOR_GET_ADDR(&monitored->monitors, idx); |
| 121 |
ao2_cleanup(notifier->data); |
| 122 |
} |
| 123 |
AST_VECTOR_FREE(&monitored->monitors); |
| 124 |
ast_debug(3, "Transport %s(%s,%s) RefCnt: %ld : state:MONITOR_DESTROYED\n", |
| 125 |
monitored->key, monitored->transport->obj_name, |
| 126 |
monitored->transport->type_name,pj_atomic_get(monitored->transport->ref_cnt)); |
| 127 |
ast_free(monitored->transport_obj_name); |
| 128 |
pjsip_transport_dec_ref(monitored->transport); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
static void transport_state_do_reg_callbacks(struct ao2_container *transports, pjsip_transport *transport) |
| 140 |
{ |
| 141 |
struct ao2_iterator *monitor_iter; |
| 142 |
struct transport_monitor *monitored; |
| 143 |
char key[IP6ADDR_COLON_PORT_BUFLEN]; |
| 144 |
int idx; |
| 145 |
|
| 146 |
AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(transport, key); |
| 147 |
|
| 148 |
monitor_iter = ao2_find(transports, key, OBJ_SEARCH_KEY | OBJ_MULTIPLE); |
| 149 |
while ((monitored = ao2_iterator_next(monitor_iter))) { |
| 150 |
if (monitored->transport == transport) { |
| 151 |
ao2_unlink(transports, monitored); |
| 152 |
for (idx = AST_VECTOR_SIZE(&monitored->monitors); idx--;) { |
| 153 |
struct transport_monitor_notifier *notifier; |
| 154 |
|
| 155 |
notifier = AST_VECTOR_GET_ADDR(&monitored->monitors, idx); |
| 156 |
ast_debug(3, "Transport %s(%s,%s) RefCnt: %ld : running callback %p(%p)\n", |
| 157 |
monitored->key, monitored->transport->obj_name, |
| 158 |
monitored->transport->type_name, |
| 159 |
pj_atomic_get(monitored->transport->ref_cnt), notifier->cb, notifier->data); |
| 160 |
notifier->cb(notifier->data); |
| 161 |
} |
| 162 |
} else { |
| 163 |
ast_debug(3, "Transport %s(%s,%s) RefCnt: %ld : ignored not matching %s\n", |
| 164 |
monitored->key, monitored->transport->obj_name, |
| 165 |
monitored->transport->type_name, |
| 166 |
pj_atomic_get(monitored->transport->ref_cnt), transport->obj_name); |
| 167 |
} |
| 168 |
ao2_ref(monitored, -1); |
| 169 |
} |
| 170 |
ao2_iterator_destroy(monitor_iter); |
| 171 |
} |
| 172 |
|
| 173 |
static void verify_log_result(int log_level, const pjsip_transport *transport, |
| 174 |
pj_uint32_t verify_status) |
| 175 |
{ |
| 176 |
char transport_remote_ipaddr_port[IP6ADDR_COLON_PORT_BUFLEN]; |
| 177 |
const char *status[32]; |
| 178 |
unsigned int count; |
| 179 |
unsigned int i; |
| 180 |
|
| 181 |
count = ARRAY_LEN(status); |
| 182 |
|
| 183 |
if (pj_ssl_cert_get_verify_status_strings(verify_status, status, &count) != PJ_SUCCESS) { |
| 184 |
ast_log(LOG_ERROR, "Error retrieving certificate verification result(s)\n"); |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(transport, transport_remote_ipaddr_port); |
| 189 |
for (i = 0; i < count; ++i) { |
| 190 |
ast_log(log_level, _A_, "Transport '%s' to remote '%.*s' - %s - %s\n", transport->factory->info, |
| 191 |
(int)pj_strlen(&transport->remote_name.host), pj_strbuf(&transport->remote_name.host), |
| 192 |
transport_remote_ipaddr_port, |
| 193 |
status[i]); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
static int verify_cert_name(const pj_str_t *local, const pj_str_t *remote) |
| 198 |
{ |
| 199 |
const char *p; |
| 200 |
pj_ssize_t size; |
| 201 |
|
| 202 |
ast_debug(3, "Verify certificate name: local = %.*s, remote = %.*s\n", |
| 203 |
(unsigned int)pj_strlen(local), pj_strbuf(local), |
| 204 |
(unsigned int)pj_strlen(remote), pj_strbuf(remote)); |
| 205 |
|
| 206 |
if (!pj_stricmp(remote, local)) { |
| 207 |
return 1; |
| 208 |
} |
| 209 |
|
| 210 |
if (pj_strnicmp2(remote, "*.", 2)) { |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
p = pj_strchr(local, '.'); |
| 215 |
if (!p || p == pj_strbuf(local)) { |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
size = pj_strbuf(local) + pj_strlen(local) - ++p; |
| 220 |
|
| 221 |
return size == pj_strlen(remote) - 2 ? |
| 222 |
!pj_memcmp(pj_strbuf(remote) + 2, p, size) : 0; |
| 223 |
} |
| 224 |
|
| 225 |
static int verify_cert_names(const pj_str_t *host, const pj_ssl_cert_info *remote) |
| 226 |
{ |
| 227 |
unsigned int i; |
| 228 |
|
| 229 |
for (i = 0; i < remote->subj_alt_name.cnt; ++i) { |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
if (remote->subj_alt_name.entry[i].type == PJ_SSL_CERT_NAME_DNS |
| 235 |
&& verify_cert_name(host, &remote->subj_alt_name.entry[i].name)) { |
| 236 |
return 1; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return verify_cert_name(host, &remote->subject.cn); |
| 241 |
} |
| 242 |
|
| 243 |
static int transport_tls_verify(const pjsip_transport *transport, |
| 244 |
const pjsip_tls_state_info *state_info) |
| 245 |
{ |
| 246 |
pj_uint32_t verify_status; |
| 247 |
const struct ast_sip_transport_state *state; |
| 248 |
|
| 249 |
if (transport->dir == PJSIP_TP_DIR_INCOMING) { |
| 250 |
return 1; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
ast_assert(!ast_strlen_zero(transport->factory->info)); |
| 255 |
|
| 256 |
state = ast_sip_get_transport_state(transport->factory->info); |
| 257 |
if (!state) { |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
ast_log(LOG_ERROR, "Transport state not found for '%s'\n", transport->factory->info); |
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
verify_status = state_info->ssl_sock_info->verify_status; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if (state->allow_wildcard_certs && |
| 275 |
(verify_status & PJ_SSL_CERT_EIDENTITY_NOT_MATCH)) { |
| 276 |
if (verify_cert_names(&transport->remote_name.host, |
| 277 |
state_info->ssl_sock_info->remote_cert_info)) { |
| 278 |
|
| 279 |
verify_status &= ~PJ_SSL_CERT_EIDENTITY_NOT_MATCH; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if (state->verify_server && verify_status != PJ_SSL_CERT_ESUCCESS) { |
| 284 |
verify_log_result(__LOG_ERROR, transport, verify_status); |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
verify_log_result(__LOG_NOTICE, transport, verify_status); |
| 289 |
return 1; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
static void transport_state_callback(pjsip_transport *transport, |
| 294 |
pjsip_transport_state state, const pjsip_transport_state_info *info) |
| 295 |
{ |
| 296 |
struct ao2_container *transports; |
| 297 |
char transport_remote_ipaddr_port[IP6ADDR_COLON_PORT_BUFLEN]; |
| 298 |
|
| 299 |
|
| 300 |
if (PJSIP_TRANSPORT_IS_RELIABLE(transport) |
| 301 |
&& (transports = ao2_global_obj_ref(active_transports))) { |
| 302 |
struct transport_monitor *monitored; |
| 303 |
AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(transport, transport_remote_ipaddr_port); |
| 304 |
|
| 305 |
ast_debug(3, "Transport %s(%s,%s): RefCnt: %ld state:%s\n", |
| 306 |
transport_remote_ipaddr_port, transport->obj_name, transport->type_name, |
| 307 |
pj_atomic_get(transport->ref_cnt), transport_state2str(state)); |
| 308 |
switch (state) { |
| 309 |
case PJSIP_TP_STATE_CONNECTED: |
| 310 |
if (PJSIP_TRANSPORT_IS_SECURE(transport) && |
| 311 |
!transport_tls_verify(transport, info->ext_info)) { |
| 312 |
pjsip_transport_shutdown(transport); |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
monitored = ao2_alloc_options(sizeof(*monitored), |
| 317 |
transport_monitor_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 318 |
if (!monitored) { |
| 319 |
break; |
| 320 |
} |
| 321 |
monitored->transport = transport; |
| 322 |
ast_copy_string(monitored->key, transport_remote_ipaddr_port, sizeof(monitored->key)); |
| 323 |
monitored->transport_obj_name = ast_strdup(transport->obj_name); |
| 324 |
|
| 325 |
if (AST_VECTOR_INIT(&monitored->monitors, 5)) { |
| 326 |
ao2_ref(monitored, -1); |
| 327 |
break; |
| 328 |
} |
| 329 |
pjsip_transport_add_ref(monitored->transport); |
| 330 |
ast_debug(3, "Transport %s(%s,%s): RefCnt: %ld state:MONITOR_CREATED\n", |
| 331 |
monitored->key, monitored->transport_obj_name, |
| 332 |
monitored->transport->type_name, |
| 333 |
pj_atomic_get(monitored->transport->ref_cnt)); |
| 334 |
|
| 335 |
ao2_link(transports, monitored); |
| 336 |
ao2_ref(monitored, -1); |
| 337 |
break; |
| 338 |
case PJSIP_TP_STATE_DISCONNECTED: |
| 339 |
if (!transport->is_shutdown) { |
| 340 |
pjsip_transport_shutdown(transport); |
| 341 |
} |
| 342 |
transport_state_do_reg_callbacks(transports, transport); |
| 343 |
break; |
| 344 |
case PJSIP_TP_STATE_SHUTDOWN: |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
transport->is_shutdown = PJ_TRUE; |
| 352 |
|
| 353 |
transport_state_do_reg_callbacks(transports, transport); |
| 354 |
break; |
| 355 |
case PJSIP_TP_STATE_DESTROY: |
| 356 |
transport_state_do_reg_callbacks(transports, transport); |
| 357 |
break; |
| 358 |
default: |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
ast_assert(0); |
| 364 |
break; |
| 365 |
} |
| 366 |
|
| 367 |
ao2_ref(transports, -1); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
if (!AST_LIST_EMPTY(&transport_state_list)) { |
| 372 |
struct ast_sip_tpmgr_state_callback *tpmgr_notifier; |
| 373 |
|
| 374 |
AST_RWLIST_RDLOCK(&transport_state_list); |
| 375 |
AST_LIST_TRAVERSE(&transport_state_list, tpmgr_notifier, node) { |
| 376 |
tpmgr_notifier->cb(transport, state, info); |
| 377 |
} |
| 378 |
AST_RWLIST_UNLOCK(&transport_state_list); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
if (tpmgr_state_callback) { |
| 383 |
tpmgr_state_callback(transport, state, info); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
struct callback_data { |
| 388 |
ast_transport_monitor_shutdown_cb cb; |
| 389 |
void *data; |
| 390 |
ast_transport_monitor_data_matcher matches; |
| 391 |
}; |
| 392 |
|
| 393 |
static int transport_monitor_unregister_cb(void *obj, void *arg, int flags) |
| 394 |
{ |
| 395 |
struct transport_monitor *monitored = obj; |
| 396 |
struct callback_data *cb_data = arg; |
| 397 |
int idx; |
| 398 |
|
| 399 |
for (idx = AST_VECTOR_SIZE(&monitored->monitors); idx--;) { |
| 400 |
struct transport_monitor_notifier *notifier; |
| 401 |
|
| 402 |
notifier = AST_VECTOR_GET_ADDR(&monitored->monitors, idx); |
| 403 |
if (notifier->cb == cb_data->cb && (!cb_data->data |
| 404 |
|| cb_data->matches(cb_data->data, notifier->data))) { |
| 405 |
ao2_cleanup(notifier->data); |
| 406 |
AST_VECTOR_REMOVE_UNORDERED(&monitored->monitors, idx); |
| 407 |
ast_debug(3, "Transport %s(%s,%s) RefCnt: %ld : Unregistered monitor %p(%p)\n", |
| 408 |
monitored->key, monitored->transport_obj_name, |
| 409 |
monitored->transport->type_name, |
| 410 |
pj_atomic_get(monitored->transport->ref_cnt), notifier->cb, notifier->data); |
| 411 |
} |
| 412 |
} |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
static int ptr_matcher(void *a, void *b) |
| 417 |
{ |
| 418 |
return a == b; |
| 419 |
} |
| 420 |
|
| 421 |
void ast_sip_transport_monitor_unregister_all(ast_transport_monitor_shutdown_cb cb, |
| 422 |
void *data, ast_transport_monitor_data_matcher matches) |
| 423 |
{ |
| 424 |
struct ao2_container *transports; |
| 425 |
struct callback_data cb_data = { |
| 426 |
.cb = cb, |
| 427 |
.data = data, |
| 428 |
.matches = matches ?: ptr_matcher, |
| 429 |
}; |
| 430 |
|
| 431 |
ast_assert(cb != NULL); |
| 432 |
|
| 433 |
transports = ao2_global_obj_ref(active_transports); |
| 434 |
if (!transports) { |
| 435 |
return; |
| 436 |
} |
| 437 |
ao2_callback(transports, OBJ_MULTIPLE | OBJ_NODATA, transport_monitor_unregister_cb, &cb_data); |
| 438 |
ao2_ref(transports, -1); |
| 439 |
} |
| 440 |
|
| 441 |
void ast_sip_transport_monitor_unregister(pjsip_transport *transport, |
| 442 |
ast_transport_monitor_shutdown_cb cb, void *data, ast_transport_monitor_data_matcher matches) |
| 443 |
{ |
| 444 |
char key[IP6ADDR_COLON_PORT_BUFLEN]; |
| 445 |
AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(transport, key); |
| 446 |
ast_sip_transport_monitor_unregister_key(key, cb, data, matches); |
| 447 |
} |
| 448 |
|
| 449 |
void ast_sip_transport_monitor_unregister_key(const char *transport_key, |
| 450 |
ast_transport_monitor_shutdown_cb cb, void *data, ast_transport_monitor_data_matcher matches) |
| 451 |
{ |
| 452 |
struct ao2_container *transports; |
| 453 |
struct transport_monitor *monitored; |
| 454 |
|
| 455 |
ast_assert(transport_key != NULL && cb != NULL); |
| 456 |
|
| 457 |
transports = ao2_global_obj_ref(active_transports); |
| 458 |
if (!transports) { |
| 459 |
return; |
| 460 |
} |
| 461 |
|
| 462 |
ao2_lock(transports); |
| 463 |
monitored = ao2_find(transports, transport_key, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 464 |
if (monitored) { |
| 465 |
struct callback_data cb_data = { |
| 466 |
.cb = cb, |
| 467 |
.data = data, |
| 468 |
.matches = matches ?: ptr_matcher, |
| 469 |
}; |
| 470 |
|
| 471 |
transport_monitor_unregister_cb(monitored, &cb_data, 0); |
| 472 |
ao2_ref(monitored, -1); |
| 473 |
} |
| 474 |
ao2_unlock(transports); |
| 475 |
ao2_ref(transports, -1); |
| 476 |
} |
| 477 |
|
| 478 |
enum ast_transport_monitor_reg ast_sip_transport_monitor_register(pjsip_transport *transport, |
| 479 |
ast_transport_monitor_shutdown_cb cb, void *ao2_data) |
| 480 |
{ |
| 481 |
char key[IP6ADDR_COLON_PORT_BUFLEN]; |
| 482 |
AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(transport, key); |
| 483 |
|
| 484 |
return ast_sip_transport_monitor_register_replace_key(key, cb, ao2_data, NULL); |
| 485 |
} |
| 486 |
|
| 487 |
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_key(const char *transport_key, |
| 488 |
ast_transport_monitor_shutdown_cb cb, void *ao2_data) |
| 489 |
{ |
| 490 |
return ast_sip_transport_monitor_register_replace_key(transport_key, cb, ao2_data, NULL); |
| 491 |
} |
| 492 |
|
| 493 |
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_replace(pjsip_transport *transport, |
| 494 |
ast_transport_monitor_shutdown_cb cb, void *ao2_data, ast_transport_monitor_data_matcher matches) |
| 495 |
{ |
| 496 |
char key[IP6ADDR_COLON_PORT_BUFLEN]; |
| 497 |
|
| 498 |
AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(transport, key); |
| 499 |
return ast_sip_transport_monitor_register_replace_key(key, cb, ao2_data, NULL); |
| 500 |
} |
| 501 |
|
| 502 |
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_replace_key(const char *transport_key, |
| 503 |
ast_transport_monitor_shutdown_cb cb, void *ao2_data, ast_transport_monitor_data_matcher matches) |
| 504 |
{ |
| 505 |
struct ao2_container *transports; |
| 506 |
struct transport_monitor *monitored; |
| 507 |
enum ast_transport_monitor_reg res = AST_TRANSPORT_MONITOR_REG_NOT_FOUND; |
| 508 |
|
| 509 |
ast_assert(transport_key != NULL && cb != NULL); |
| 510 |
|
| 511 |
transports = ao2_global_obj_ref(active_transports); |
| 512 |
if (!transports) { |
| 513 |
return res; |
| 514 |
} |
| 515 |
|
| 516 |
ao2_lock(transports); |
| 517 |
monitored = ao2_find(transports, transport_key, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 518 |
if (monitored) { |
| 519 |
struct transport_monitor_notifier new_monitor; |
| 520 |
struct callback_data cb_data = { |
| 521 |
.cb = cb, |
| 522 |
.data = ao2_data, |
| 523 |
.matches = matches ?: ptr_matcher, |
| 524 |
}; |
| 525 |
|
| 526 |
transport_monitor_unregister_cb(monitored, &cb_data, 0); |
| 527 |
|
| 528 |
|
| 529 |
new_monitor.cb = cb; |
| 530 |
new_monitor.data = ao2_bump(ao2_data); |
| 531 |
if (AST_VECTOR_APPEND(&monitored->monitors, new_monitor)) { |
| 532 |
ao2_cleanup(ao2_data); |
| 533 |
res = AST_TRANSPORT_MONITOR_REG_FAILED; |
| 534 |
ast_debug(3, "Transport %s(%s) RefCnt: %ld : Monitor registration failed %p(%p)\n", |
| 535 |
monitored->key, monitored->transport_obj_name, |
| 536 |
pj_atomic_get(monitored->transport->ref_cnt), cb, ao2_data); |
| 537 |
} else { |
| 538 |
res = AST_TRANSPORT_MONITOR_REG_SUCCESS; |
| 539 |
ast_debug(3, "Transport %s(%s,%s) RefCnt: %ld : Registered monitor %p(%p)\n", |
| 540 |
monitored->key, monitored->transport_obj_name, |
| 541 |
monitored->transport->type_name, |
| 542 |
pj_atomic_get(monitored->transport->ref_cnt), cb, ao2_data); |
| 543 |
} |
| 544 |
|
| 545 |
ao2_ref(monitored, -1); |
| 546 |
} |
| 547 |
ao2_unlock(transports); |
| 548 |
ao2_ref(transports, -1); |
| 549 |
return res; |
| 550 |
} |
| 551 |
|
| 552 |
void ast_sip_transport_state_unregister(struct ast_sip_tpmgr_state_callback *element) |
| 553 |
{ |
| 554 |
AST_RWLIST_WRLOCK(&transport_state_list); |
| 555 |
AST_LIST_REMOVE(&transport_state_list, element, node); |
| 556 |
AST_RWLIST_UNLOCK(&transport_state_list); |
| 557 |
} |
| 558 |
|
| 559 |
void ast_sip_transport_state_register(struct ast_sip_tpmgr_state_callback *element) |
| 560 |
{ |
| 561 |
struct ast_sip_tpmgr_state_callback *tpmgr_notifier; |
| 562 |
|
| 563 |
AST_RWLIST_WRLOCK(&transport_state_list); |
| 564 |
AST_LIST_TRAVERSE(&transport_state_list, tpmgr_notifier, node) { |
| 565 |
if (element == tpmgr_notifier) { |
| 566 |
|
| 567 |
AST_RWLIST_UNLOCK(&transport_state_list); |
| 568 |
return; |
| 569 |
} |
| 570 |
} |
| 571 |
AST_LIST_INSERT_HEAD(&transport_state_list, element, node); |
| 572 |
AST_RWLIST_UNLOCK(&transport_state_list); |
| 573 |
} |
| 574 |
|
| 575 |
static char *cli_show_monitors(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 576 |
{ |
| 577 |
char *cli_rc = CLI_FAILURE; |
| 578 |
int rc = 0; |
| 579 |
int using_regex = 0; |
| 580 |
regex_t regex = { 0, }; |
| 581 |
int container_count; |
| 582 |
struct ao2_iterator iter; |
| 583 |
struct ao2_container *sorted_monitors = NULL; |
| 584 |
struct ao2_container *transports; |
| 585 |
struct transport_monitor *monitored; |
| 586 |
|
| 587 |
switch (cmd) { |
| 588 |
case CLI_INIT: |
| 589 |
e->command = "pjsip show transport-monitors"; |
| 590 |
e->usage = "Usage: pjsip show transport-monitors [ like <pattern> ]\n" |
| 591 |
" Show pjsip transport monitors\n"; |
| 592 |
return NULL; |
| 593 |
case CLI_GENERATE: |
| 594 |
return NULL; |
| 595 |
} |
| 596 |
|
| 597 |
if (a->argc != 3 && a->argc != 5) { |
| 598 |
return CLI_SHOWUSAGE; |
| 599 |
} |
| 600 |
|
| 601 |
if (a->argc == 5) { |
| 602 |
int regrc; |
| 603 |
if (strcasecmp(a->argv[3], "like")) { |
| 604 |
return CLI_SHOWUSAGE; |
| 605 |
} |
| 606 |
regrc = regcomp(®ex, a->argv[4], REG_EXTENDED | REG_ICASE | REG_NOSUB); |
| 607 |
if (regrc) { |
| 608 |
char err[256]; |
| 609 |
regerror(regrc, ®ex, err, 256); |
| 610 |
ast_cli(a->fd, "PJSIP Transport Monitor: Error: %s\n", err); |
| 611 |
return CLI_FAILURE; |
| 612 |
} |
| 613 |
using_regex = 1; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
sorted_monitors = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 618 |
transport_monitor_sort_fn, NULL); |
| 619 |
if (!sorted_monitors) { |
| 620 |
ast_cli(a->fd, "PJSIP Transport Monitor: Unable to allocate temporary container\n"); |
| 621 |
goto error; |
| 622 |
} |
| 623 |
|
| 624 |
transports = ao2_global_obj_ref(active_transports); |
| 625 |
if (!transports) { |
| 626 |
ast_cli(a->fd, "PJSIP Transport Monitor: Unable to get transports\n"); |
| 627 |
goto error; |
| 628 |
} |
| 629 |
|
| 630 |
ao2_lock(transports); |
| 631 |
rc = ao2_container_dup(sorted_monitors, transports, 0); |
| 632 |
ao2_unlock(transports); |
| 633 |
ao2_ref(transports, -1); |
| 634 |
if (rc != 0) { |
| 635 |
ast_cli(a->fd, "PJSIP Transport Monitors: Unable to sort temporary container\n"); |
| 636 |
goto error; |
| 637 |
} |
| 638 |
container_count = ao2_container_count(sorted_monitors); |
| 639 |
|
| 640 |
ast_cli(a->fd, "PJSIP Transport Monitors:\n\n"); |
| 641 |
|
| 642 |
ast_cli(a->fd, |
| 643 |
"<Remote Host...................................> <State.....> <Direction> <RefCnt> <Monitors> <ObjName............>\n"); |
| 644 |
|
| 645 |
iter = ao2_iterator_init(sorted_monitors, AO2_ITERATOR_UNLINK); |
| 646 |
for (; (monitored = ao2_iterator_next(&iter)); ao2_ref(monitored, -1)) { |
| 647 |
char *state; |
| 648 |
|
| 649 |
if (using_regex && regexec(®ex, monitored->key, 0, NULL, 0) == REG_NOMATCH) { |
| 650 |
continue; |
| 651 |
} |
| 652 |
|
| 653 |
if (monitored->transport->is_destroying) { |
| 654 |
state = "DESTROYING"; |
| 655 |
} else if (monitored->transport->is_shutdown) { |
| 656 |
state = "SHUTDOWN"; |
| 657 |
} else { |
| 658 |
state = "ACTIVE"; |
| 659 |
} |
| 660 |
|
| 661 |
ast_cli(a->fd, " %-46.46s %-10s %-9s %6ld %8zu %s\n", |
| 662 |
monitored->key, state, |
| 663 |
monitored->transport->dir == PJSIP_TP_DIR_OUTGOING ? "Outgoing" : "Incoming", |
| 664 |
pj_atomic_get(monitored->transport->ref_cnt), |
| 665 |
AST_VECTOR_SIZE(&monitored->monitors), monitored->transport->obj_name); |
| 666 |
} |
| 667 |
ao2_iterator_destroy(&iter); |
| 668 |
ast_cli(a->fd, "\nTotal Transport Monitors: %d\n\n", container_count); |
| 669 |
cli_rc = CLI_SUCCESS; |
| 670 |
error: |
| 671 |
if (using_regex) { |
| 672 |
regfree(®ex); |
| 673 |
} |
| 674 |
ao2_cleanup(sorted_monitors); |
| 675 |
|
| 676 |
return cli_rc; |
| 677 |
} |
| 678 |
|
| 679 |
static struct ast_cli_entry cli_commands[] = { |
| 680 |
AST_CLI_DEFINE(cli_show_monitors, "Show pjsip transport monitors"), |
| 681 |
}; |
| 682 |
|
| 683 |
void ast_sip_destroy_transport_events(void) |
| 684 |
{ |
| 685 |
pjsip_tpmgr *tpmgr; |
| 686 |
|
| 687 |
ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands)); |
| 688 |
|
| 689 |
tpmgr = pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()); |
| 690 |
if (tpmgr) { |
| 691 |
pjsip_tpmgr_set_state_cb(tpmgr, tpmgr_state_callback); |
| 692 |
} |
| 693 |
|
| 694 |
ao2_global_obj_release(active_transports); |
| 695 |
} |
| 696 |
|
| 697 |
int ast_sip_initialize_transport_events(void) |
| 698 |
{ |
| 699 |
pjsip_tpmgr *tpmgr; |
| 700 |
struct ao2_container *transports; |
| 701 |
|
| 702 |
tpmgr = pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()); |
| 703 |
if (!tpmgr) { |
| 704 |
return -1; |
| 705 |
} |
| 706 |
|
| 707 |
transports = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 708 |
ACTIVE_TRANSPORTS_BUCKETS, transport_monitor_hash_fn, transport_monitor_sort_fn, |
| 709 |
transport_monitor_cmp_fn); |
| 710 |
if (!transports) { |
| 711 |
return -1; |
| 712 |
} |
| 713 |
ao2_global_obj_replace_unref(active_transports, transports); |
| 714 |
ao2_ref(transports, -1); |
| 715 |
|
| 716 |
tpmgr_state_callback = pjsip_tpmgr_get_state_cb(tpmgr); |
| 717 |
pjsip_tpmgr_set_state_cb(tpmgr, &transport_state_callback); |
| 718 |
|
| 719 |
ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands)); |
| 720 |
|
| 721 |
|
| 722 |
return 0; |
| 723 |
} |