Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Functions | Variables
pjsip_transport_events.c File Reference

Manages the global transport event notification callbacks. More...

#include "asterisk.h"
#include "asterisk/res_pjsip.h"
#include "asterisk/res_pjsip_cli.h"
#include "include/res_pjsip_private.h"
#include "asterisk/linkedlists.h"
#include "asterisk/vector.h"

Go to the source code of this file.

Data Structures

struct  callback_data
 
struct  transport_monitor
 Structure for transport to be monitored. More...
 
struct  transport_monitor_notifier
 

Macros

#define ACTIVE_TRANSPORTS_BUCKETS   127
 Number of buckets for monitored active transports.
 

Functions

static AO2_GLOBAL_OBJ_STATIC (active_transports)
 Global container of active reliable transports.
 
 AO2_STRING_FIELD_CMP_FN (transport_monitor, key)
 Comparison function for struct transport_monitor.
 
 AO2_STRING_FIELD_HASH_FN (transport_monitor, key)
 Hashing function for struct transport_monitor.
 
 AO2_STRING_FIELD_SORT_FN (transport_monitor, key)
 Sort function for struct transport_monitor.
 
void ast_sip_destroy_transport_events (void)
 
int ast_sip_initialize_transport_events (void)
 
enum ast_transport_monitor_reg ast_sip_transport_monitor_register (pjsip_transport *transport, ast_transport_monitor_shutdown_cb cb, void *ao2_data)
 Register a reliable transport shutdown monitor callback. More...
 
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_key (const char *transport_key, ast_transport_monitor_shutdown_cb cb, void *ao2_data)
 Register a reliable transport shutdown monitor callback. More...
 
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_replace (pjsip_transport *transport, ast_transport_monitor_shutdown_cb cb, void *ao2_data, ast_transport_monitor_data_matcher matches)
 Register a reliable transport shutdown monitor callback replacing any duplicate. More...
 
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_replace_key (const char *transport_key, ast_transport_monitor_shutdown_cb cb, void *ao2_data, ast_transport_monitor_data_matcher matches)
 Register a reliable transport shutdown monitor callback replacing any duplicate. More...
 
void ast_sip_transport_monitor_unregister (pjsip_transport *transport, ast_transport_monitor_shutdown_cb cb, void *data, ast_transport_monitor_data_matcher matches)
 Unregister a reliable transport shutdown monitor. More...
 
void ast_sip_transport_monitor_unregister_all (ast_transport_monitor_shutdown_cb cb, void *data, ast_transport_monitor_data_matcher matches)
 Unregister a transport shutdown monitor from all reliable transports. More...
 
void ast_sip_transport_monitor_unregister_key (const char *transport_key, ast_transport_monitor_shutdown_cb cb, void *data, ast_transport_monitor_data_matcher matches)
 Unregister a reliable transport shutdown monitor. More...
 
void ast_sip_transport_state_register (struct ast_sip_tpmgr_state_callback *element)
 Register a transport state notification callback element. More...
 
void ast_sip_transport_state_unregister (struct ast_sip_tpmgr_state_callback *element)
 Unregister a transport state notification callback element. More...
 
static char * cli_show_monitors (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static int ptr_matcher (void *a, void *b)
 
static void transport_monitor_dtor (void *vdoomed)
 
static int transport_monitor_unregister_cb (void *obj, void *arg, int flags)
 
static const char * transport_state2str (pjsip_transport_state state)
 
static void transport_state_callback (pjsip_transport *transport, pjsip_transport_state state, const pjsip_transport_state_info *info)
 Callback invoked when transport state changes occur.
 
static void transport_state_do_reg_callbacks (struct ao2_container *transports, pjsip_transport *transport)
 
static int transport_tls_verify (const pjsip_transport *transport, const pjsip_tls_state_info *state_info)
 
static int verify_cert_name (const pj_str_t *local, const pj_str_t *remote)
 
static int verify_cert_names (const pj_str_t *host, const pj_ssl_cert_info *remote)
 
static void verify_log_result (int log_level, const pjsip_transport *transport, pj_uint32_t verify_status)
 

Variables

static struct ast_cli_entry cli_commands []
 
static pjsip_tp_state_callback tpmgr_state_callback
 Existing transport events callback that we need to invoke.
 
struct {
   struct ast_sip_tpmgr_state_callback *   first
 
   struct ast_sip_tpmgr_state_callback *   last
 
   ast_rwlock_t   lock
 
transport_state_list
 

Detailed Description

Manages the global transport event notification callbacks.

Author
Richard Mudgett rmudg.nosp@m.ett@.nosp@m.digiu.nosp@m.m.co.nosp@m.m See Also:

Definition in file pjsip_transport_events.c.

Function Documentation

enum ast_transport_monitor_reg ast_sip_transport_monitor_register ( pjsip_transport *  transport,
ast_transport_monitor_shutdown_cb  cb,
void *  ao2_data 
)

Register a reliable transport shutdown monitor callback.

Deprecated:
Replaced with ast_sip_transport_monitor_register_key().
Since
13.20.0
Parameters
transportTransport to monitor for shutdown.
cbWho to call when transport is shutdown.
ao2_dataData to pass with the callback.
Note
The data object passed will have its reference count automatically incremented by this call and automatically decremented after the callback runs or when the callback is unregistered.

There is no checking for duplicate registrations.

Returns
enum ast_transport_monitor_reg

Definition at line 466 of file pjsip_transport_events.c.

References ast_sip_transport_monitor_register_replace_key().

468 {
469  char key[IP6ADDR_COLON_PORT_BUFLEN];
470  AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(transport, key);
471 
472  return ast_sip_transport_monitor_register_replace_key(key, cb, ao2_data, NULL);
473 }
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_replace_key(const char *transport_key, ast_transport_monitor_shutdown_cb cb, void *ao2_data, ast_transport_monitor_data_matcher matches)
Register a reliable transport shutdown monitor callback replacing any duplicate.
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_key ( const char *  transport_key,
ast_transport_monitor_shutdown_cb  cb,
void *  ao2_data 
)

Register a reliable transport shutdown monitor callback.

Parameters
transport_keyKey for the transport to monitor for shutdown. Create the key with AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR.
cbWho to call when transport is shutdown.
ao2_dataData to pass with the callback.
Note
The data object passed will have its reference count automatically incremented by this call and automatically decremented after the callback runs or when the callback is unregistered.

There is no checking for duplicate registrations.

Returns
enum ast_transport_monitor_reg

Definition at line 475 of file pjsip_transport_events.c.

References ast_sip_transport_monitor_register_replace_key().

477 {
478  return ast_sip_transport_monitor_register_replace_key(transport_key, cb, ao2_data, NULL);
479 }
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_replace_key(const char *transport_key, ast_transport_monitor_shutdown_cb cb, void *ao2_data, ast_transport_monitor_data_matcher matches)
Register a reliable transport shutdown monitor callback replacing any duplicate.
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_replace ( pjsip_transport *  transport,
ast_transport_monitor_shutdown_cb  cb,
void *  ao2_data,
ast_transport_monitor_data_matcher  matches 
)

Register a reliable transport shutdown monitor callback replacing any duplicate.

Deprecated:
Replaced with ast_sip_transport_monitor_register_replace_key().
Since
13.26.0
16.3.0
Parameters
transportTransport to monitor for shutdown.
cbWho to call when transport is shutdown.
ao2_dataData to pass with the callback.
matchesMatcher function that returns true if data matches a previously registered data object
Note
The data object passed will have its reference count automatically incremented by this call and automatically decremented after the callback runs or when the callback is unregistered.

This function checks for duplicates, and overwrites/replaces the old monitor with the given one.

Returns
enum ast_transport_monitor_reg

Definition at line 481 of file pjsip_transport_events.c.

References ast_sip_transport_monitor_register_replace_key().

483 {
484  char key[IP6ADDR_COLON_PORT_BUFLEN];
485 
486  AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(transport, key);
487  return ast_sip_transport_monitor_register_replace_key(key, cb, ao2_data, NULL);
488 }
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_replace_key(const char *transport_key, ast_transport_monitor_shutdown_cb cb, void *ao2_data, ast_transport_monitor_data_matcher matches)
Register a reliable transport shutdown monitor callback replacing any duplicate.
enum ast_transport_monitor_reg ast_sip_transport_monitor_register_replace_key ( const char *  transport_key,
ast_transport_monitor_shutdown_cb  cb,
void *  ao2_data,
ast_transport_monitor_data_matcher  matches 
)

Register a reliable transport shutdown monitor callback replacing any duplicate.

Parameters
transport_keyKey for the transport to monitor for shutdown. Create the key with AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR.
cbWho to call when transport is shutdown.
ao2_dataData to pass with the callback.
matchesMatcher function that returns true if data matches a previously registered data object
Note
The data object passed will have its reference count automatically incremented by this call and automatically decremented after the callback runs or when the callback is unregistered.

This function checks for duplicates, and overwrites/replaces the old monitor with the given one.

Returns
enum ast_transport_monitor_reg

Definition at line 490 of file pjsip_transport_events.c.

References ao2_bump, ao2_global_obj_ref, ao2_ref, ast_debug, AST_VECTOR_APPEND, transport_monitor_notifier::cb, transport_monitor_notifier::data, transport_monitor::key, transport_monitor::monitors, OBJ_NOLOCK, OBJ_SEARCH_KEY, transport_monitor::transport, and transport_monitor::transport_obj_name.

Referenced by ast_sip_transport_monitor_register(), ast_sip_transport_monitor_register_key(), and ast_sip_transport_monitor_register_replace().

492 {
493  struct ao2_container *transports;
494  struct transport_monitor *monitored;
495  enum ast_transport_monitor_reg res = AST_TRANSPORT_MONITOR_REG_NOT_FOUND;
496 
497  ast_assert(transport_key != NULL && cb != NULL);
498 
499  transports = ao2_global_obj_ref(active_transports);
500  if (!transports) {
501  return res;
502  }
503 
504  ao2_lock(transports);
505  monitored = ao2_find(transports, transport_key, OBJ_SEARCH_KEY | OBJ_NOLOCK);
506  if (monitored) {
507  struct transport_monitor_notifier new_monitor;
508  struct callback_data cb_data = {
509  .cb = cb,
510  .data = ao2_data,
511  .matches = matches ?: ptr_matcher,
512  };
513 
514  transport_monitor_unregister_cb(monitored, &cb_data, 0);
515 
516  /* Add new monitor to vector */
517  new_monitor.cb = cb;
518  new_monitor.data = ao2_bump(ao2_data);
519  if (AST_VECTOR_APPEND(&monitored->monitors, new_monitor)) {
520  ao2_cleanup(ao2_data);
521  res = AST_TRANSPORT_MONITOR_REG_FAILED;
522  ast_debug(3, "Transport %s(%s) RefCnt: %ld : Monitor registration failed %p(%p)\n",
523  monitored->key, monitored->transport_obj_name,
524  pj_atomic_get(monitored->transport->ref_cnt), cb, ao2_data);
525  } else {
526  res = AST_TRANSPORT_MONITOR_REG_SUCCESS;
527  ast_debug(3, "Transport %s(%s,%s) RefCnt: %ld : Registered monitor %p(%p)\n",
528  monitored->key, monitored->transport_obj_name,
529  monitored->transport->type_name,
530  pj_atomic_get(monitored->transport->ref_cnt), cb, ao2_data);
531  }
532 
533  ao2_ref(monitored, -1);
534  }
535  ao2_unlock(transports);
536  ao2_ref(transports, -1);
537  return res;
538 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
struct transport_monitor::@462 monitors
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
Structure for transport to be monitored.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ast_debug(level,...)
Log a DEBUG message.
pjsip_transport * transport
The underlying PJSIP transport.
Generic container type.
char key[IP6ADDR_COLON_PORT_BUFLEN]
Key :
void ast_sip_transport_monitor_unregister ( pjsip_transport *  transport,
ast_transport_monitor_shutdown_cb  cb,
void *  data,
ast_transport_monitor_data_matcher  matches 
)

Unregister a reliable transport shutdown monitor.

Deprecated:
Replaced with ast_sip_transport_monitor_unregister_key().
Since
13.20.0
Parameters
transportTransport to monitor for shutdown.
cbThe callback that was used for the original register.
dataData to pass to the matcher. May be NULL and does NOT need to be an ao2 object. If NULL, all monitors with the provided callback are unregistered.
matchesMatcher function that returns true if data matches the previously registered data object. If NULL, a simple pointer comparison is done.
Note
The data object passed into the original register will have its reference count automatically decremented.

Definition at line 429 of file pjsip_transport_events.c.

References ast_sip_transport_monitor_unregister_key().

431 {
432  char key[IP6ADDR_COLON_PORT_BUFLEN];
433  AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR(transport, key);
434  ast_sip_transport_monitor_unregister_key(key, cb, data, matches);
435 }
void ast_sip_transport_monitor_unregister_key(const char *transport_key, ast_transport_monitor_shutdown_cb cb, void *data, ast_transport_monitor_data_matcher matches)
Unregister a reliable transport shutdown monitor.
void ast_sip_transport_monitor_unregister_all ( ast_transport_monitor_shutdown_cb  cb,
void *  data,
ast_transport_monitor_data_matcher  matches 
)

Unregister a transport shutdown monitor from all reliable transports.

Since
13.20.0
Parameters
cbThe callback that was used for the original register.
dataData to pass to the matcher. May be NULL and does NOT need to be an ao2 object. If NULL, all monitors with the provided callback are unregistered.
matchesMatcher function that returns true if ao2_data matches the previously registered data object. If NULL, a simple pointer comparison is done.
Note
The data object passed into the original register will have its reference count automatically decremented.

Definition at line 409 of file pjsip_transport_events.c.

References ao2_callback, ao2_global_obj_ref, ao2_ref, OBJ_MULTIPLE, and OBJ_NODATA.

411 {
412  struct ao2_container *transports;
413  struct callback_data cb_data = {
414  .cb = cb,
415  .data = data,
416  .matches = matches ?: ptr_matcher,
417  };
418 
419  ast_assert(cb != NULL);
420 
421  transports = ao2_global_obj_ref(active_transports);
422  if (!transports) {
423  return;
424  }
425  ao2_callback(transports, OBJ_MULTIPLE | OBJ_NODATA, transport_monitor_unregister_cb, &cb_data);
426  ao2_ref(transports, -1);
427 }
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
Definition: astobj2.h:1693
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Generic container type.
void ast_sip_transport_monitor_unregister_key ( const char *  transport_key,
ast_transport_monitor_shutdown_cb  cb,
void *  data,
ast_transport_monitor_data_matcher  matches 
)

Unregister a reliable transport shutdown monitor.

Parameters
transport_keyKey for the transport to monitor for shutdown. Create the key with AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR.
cbThe callback that was used for the original register.
dataData to pass to the matcher. May be NULL and does NOT need to be an ao2 object. If NULL, all monitors with the provided callback are unregistered.
matchesMatcher function that returns true if data matches the previously registered data object. If NULL, a simple pointer comparison is done.
Note
The data object passed into the original register will have its reference count automatically decremented.

Definition at line 437 of file pjsip_transport_events.c.

References ao2_global_obj_ref, ao2_ref, OBJ_NOLOCK, and OBJ_SEARCH_KEY.

Referenced by ast_sip_transport_monitor_unregister().

439 {
440  struct ao2_container *transports;
441  struct transport_monitor *monitored;
442 
443  ast_assert(transport_key != NULL && cb != NULL);
444 
445  transports = ao2_global_obj_ref(active_transports);
446  if (!transports) {
447  return;
448  }
449 
450  ao2_lock(transports);
451  monitored = ao2_find(transports, transport_key, OBJ_SEARCH_KEY | OBJ_NOLOCK);
452  if (monitored) {
453  struct callback_data cb_data = {
454  .cb = cb,
455  .data = data,
456  .matches = matches ?: ptr_matcher,
457  };
458 
459  transport_monitor_unregister_cb(monitored, &cb_data, 0);
460  ao2_ref(monitored, -1);
461  }
462  ao2_unlock(transports);
463  ao2_ref(transports, -1);
464 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
Structure for transport to be monitored.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Generic container type.
void ast_sip_transport_state_register ( struct ast_sip_tpmgr_state_callback element)

Register a transport state notification callback element.

Since
13.18.0
Parameters
elementWhat we are registering.

Definition at line 547 of file pjsip_transport_events.c.

References AST_LIST_INSERT_HEAD, AST_LIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and transport_state_list.

548 {
549  struct ast_sip_tpmgr_state_callback *tpmgr_notifier;
550 
552  AST_LIST_TRAVERSE(&transport_state_list, tpmgr_notifier, node) {
553  if (element == tpmgr_notifier) {
554  /* Already registered. */
556  return;
557  }
558  }
561 }
Definition: test_heap.c:38
static struct @461 transport_state_list
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:711
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
void ast_sip_transport_state_unregister ( struct ast_sip_tpmgr_state_callback element)

Unregister a transport state notification callback element.

Since
13.18.0
Parameters
elementWhat we are unregistering.

Definition at line 540 of file pjsip_transport_events.c.

References AST_LIST_REMOVE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and transport_state_list.

541 {
545 }
Definition: test_heap.c:38
static struct @461 transport_state_list
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
Definition: linkedlists.h:856
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151

Variable Documentation

struct ast_cli_entry cli_commands[]
static
Initial value:
= {
{ .handler = cli_show_monitors , .summary = "Show pjsip transport monitors" ,},
}

Definition at line 667 of file pjsip_transport_events.c.

struct { ... } transport_state_list

List of registered transport state callbacks.

Referenced by ast_sip_transport_state_register(), ast_sip_transport_state_unregister(), and transport_state_callback().