Asterisk - The Open Source Telephony Project  18.5.0
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 "include/res_pjsip_private.h"
#include "asterisk/linkedlists.h"
#include "asterisk/vector.h"
Include dependency graph for pjsip_transport_events.c:

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. More...
 

Functions

static AO2_GLOBAL_OBJ_STATIC (active_transports)
 Global container of active reliable transports. More...
 
 AO2_STRING_FIELD_CMP_FN (transport_monitor, transport->obj_name)
 Comparison function for struct transport_monitor. More...
 
 AO2_STRING_FIELD_HASH_FN (transport_monitor, transport->obj_name)
 Hashing function for struct transport_monitor. More...
 
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_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...
 
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_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 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. More...
 
static void transport_state_do_reg_callbacks (struct ao2_container *transports, pjsip_transport *transport)
 

Variables

static pjsip_tp_state_callback tpmgr_state_callback
 Existing transport events callback that we need to invoke. More...
 
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.

Macro Definition Documentation

◆ ACTIVE_TRANSPORTS_BUCKETS

#define ACTIVE_TRANSPORTS_BUCKETS   127

Number of buckets for monitored active transports.

Definition at line 40 of file pjsip_transport_events.c.

Referenced by ast_sip_initialize_transport_events().

Function Documentation

◆ AO2_GLOBAL_OBJ_STATIC()

static AO2_GLOBAL_OBJ_STATIC ( active_transports  )
static

Global container of active reliable transports.

◆ AO2_STRING_FIELD_CMP_FN()

AO2_STRING_FIELD_CMP_FN ( transport_monitor  ,
transport->  obj_name 
)

Comparison function for struct transport_monitor.

◆ AO2_STRING_FIELD_HASH_FN()

AO2_STRING_FIELD_HASH_FN ( transport_monitor  ,
transport->  obj_name 
)

Hashing function for struct transport_monitor.

◆ ast_sip_destroy_transport_events()

void ast_sip_destroy_transport_events ( void  )

Definition at line 382 of file pjsip_transport_events.c.

References ao2_global_obj_release, ast_sip_get_pjsip_endpoint(), and tpmgr_state_callback.

Referenced by unload_pjsip().

383 {
384  pjsip_tpmgr *tpmgr;
385 
386  tpmgr = pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint());
387  if (tpmgr) {
388  pjsip_tpmgr_set_state_cb(tpmgr, tpmgr_state_callback);
389  }
390 
391  ao2_global_obj_release(active_transports);
392 }
static pjsip_tp_state_callback tpmgr_state_callback
Existing transport events callback that we need to invoke.
#define ao2_global_obj_release(holder)
Definition: astobj2.h:865
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718

◆ ast_sip_initialize_transport_events()

int ast_sip_initialize_transport_events ( void  )

Definition at line 394 of file pjsip_transport_events.c.

References ACTIVE_TRANSPORTS_BUCKETS, AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_hash, ao2_global_obj_replace_unref, ao2_ref, ast_sip_get_pjsip_endpoint(), NULL, tpmgr_state_callback, and transport_state_callback().

Referenced by load_module().

395 {
396  pjsip_tpmgr *tpmgr;
397  struct ao2_container *transports;
398 
399  tpmgr = pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint());
400  if (!tpmgr) {
401  return -1;
402  }
403 
405  ACTIVE_TRANSPORTS_BUCKETS, transport_monitor_hash_fn, NULL,
406  transport_monitor_cmp_fn);
407  if (!transports) {
408  return -1;
409  }
410  ao2_global_obj_replace_unref(active_transports, transports);
411  ao2_ref(transports, -1);
412 
413  tpmgr_state_callback = pjsip_tpmgr_get_state_cb(tpmgr);
414  pjsip_tpmgr_set_state_cb(tpmgr, &transport_state_callback);
415 
416  return 0;
417 }
static pjsip_tp_state_callback tpmgr_state_callback
Existing transport events callback that we need to invoke.
#define ACTIVE_TRANSPORTS_BUCKETS
Number of buckets for monitored active transports.
#define NULL
Definition: resample.c:96
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.
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
#define ao2_global_obj_replace_unref(holder, obj)
Definition: astobj2.h:908
Generic container type.

◆ ast_sip_transport_monitor_register()

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.

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 306 of file pjsip_transport_events.c.

References ast_sip_transport_monitor_register_replace(), and NULL.

Referenced by registration_transport_monitor_setup(), and subscription_persistence_update().

308 {
309  return ast_sip_transport_monitor_register_replace(transport, cb, ao2_data, NULL);
310 }
#define NULL
Definition: resample.c:96
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.

◆ ast_sip_transport_monitor_register_replace()

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.

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 312 of file pjsip_transport_events.c.

References ao2_bump, ao2_cleanup, ao2_find, ao2_global_obj_ref, ao2_lock, ao2_ref, ao2_unlock, ast_assert, ast_debug, AST_TRANSPORT_MONITOR_REG_FAILED, AST_TRANSPORT_MONITOR_REG_NOT_FOUND, AST_TRANSPORT_MONITOR_REG_SUCCESS, AST_VECTOR_APPEND, transport_monitor_notifier::cb, callback_data::cb, transport_monitor_notifier::data, NULL, OBJ_NOLOCK, OBJ_SEARCH_KEY, ptr_matcher(), and transport_monitor_unregister_cb().

Referenced by ast_sip_transport_monitor_register(), and register_aor_core().

314 {
315  struct ao2_container *transports;
316  struct transport_monitor *monitored;
318 
319  ast_assert(transport != NULL && cb != NULL);
320 
321  transports = ao2_global_obj_ref(active_transports);
322  if (!transports) {
323  return res;
324  }
325 
326  ao2_lock(transports);
327  monitored = ao2_find(transports, transport->obj_name, OBJ_SEARCH_KEY | OBJ_NOLOCK);
328  if (monitored) {
329  struct transport_monitor_notifier new_monitor;
330  struct callback_data cb_data = {
331  .cb = cb,
332  .data = ao2_data,
333  .matches = matches ?: ptr_matcher,
334  };
335 
336  transport_monitor_unregister_cb(monitored, &cb_data, 0);
337 
338  /* Add new monitor to vector */
339  new_monitor.cb = cb;
340  new_monitor.data = ao2_bump(ao2_data);
341  if (AST_VECTOR_APPEND(&monitored->monitors, new_monitor)) {
342  ao2_cleanup(ao2_data);
344  ast_debug(3, "Register monitor %p(%p) to transport %s FAILED\n",
345  cb, ao2_data, transport->obj_name);
346  } else {
348  ast_debug(3, "Registered monitor %p(%p) to transport %s\n",
349  cb, ao2_data, transport->obj_name);
350  }
351 
352  ao2_ref(monitored, -1);
353  }
354  ao2_unlock(transports);
355  ao2_ref(transports, -1);
356  return res;
357 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
ast_transport_monitor_reg
Definition: res_pjsip.h:3459
#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:1067
#define ao2_global_obj_ref(holder)
Definition: astobj2.h:925
#define ast_assert(a)
Definition: utils.h:695
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
Structure for transport to be monitored.
ast_transport_monitor_data_matcher matches
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_lock(a)
Definition: astobj2.h:718
Successfully registered the transport monitor.
Definition: res_pjsip.h:3461
Transport not found to monitor.
Definition: res_pjsip.h:3468
pjsip_transport * transport
The underlying PJSIP transport.
static int transport_monitor_unregister_cb(void *obj, void *arg, int flags)
static int ptr_matcher(void *a, void *b)
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Generic container type.
Error while registering transport monitor.
Definition: res_pjsip.h:3470
ast_transport_monitor_shutdown_cb cb

◆ ast_sip_transport_monitor_unregister()

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.

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 callbck 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 decremeneted.
Returns
Nothing

Definition at line 277 of file pjsip_transport_events.c.

References ao2_find, ao2_global_obj_ref, ao2_lock, ao2_ref, ao2_unlock, ast_assert, transport_monitor_notifier::cb, callback_data::cb, transport_monitor_notifier::data, NULL, OBJ_NOLOCK, OBJ_SEARCH_KEY, ptr_matcher(), and transport_monitor_unregister_cb().

Referenced by handle_registration_response(), registrar_contact_delete(), and subscription_persistence_remove().

279 {
280  struct ao2_container *transports;
281  struct transport_monitor *monitored;
282 
283  ast_assert(transport != NULL && cb != NULL);
284 
285  transports = ao2_global_obj_ref(active_transports);
286  if (!transports) {
287  return;
288  }
289 
290  ao2_lock(transports);
291  monitored = ao2_find(transports, transport->obj_name, OBJ_SEARCH_KEY | OBJ_NOLOCK);
292  if (monitored) {
293  struct callback_data cb_data = {
294  .cb = cb,
295  .data = data,
296  .matches = matches ?: ptr_matcher,
297  };
298 
299  transport_monitor_unregister_cb(monitored, &cb_data, 0);
300  ao2_ref(monitored, -1);
301  }
302  ao2_unlock(transports);
303  ao2_ref(transports, -1);
304 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
#define ao2_global_obj_ref(holder)
Definition: astobj2.h:925
#define ast_assert(a)
Definition: utils.h:695
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
Structure for transport to be monitored.
ast_transport_monitor_data_matcher matches
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_lock(a)
Definition: astobj2.h:718
pjsip_transport * transport
The underlying PJSIP transport.
static int transport_monitor_unregister_cb(void *obj, void *arg, int flags)
static int ptr_matcher(void *a, void *b)
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
Generic container type.
ast_transport_monitor_shutdown_cb cb

◆ ast_sip_transport_monitor_unregister_all()

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 callbck 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 decremeneted.
Returns
Nothing

Definition at line 257 of file pjsip_transport_events.c.

References ao2_callback, ao2_global_obj_ref, ao2_ref, ast_assert, transport_monitor_notifier::cb, callback_data::cb, transport_monitor_notifier::data, NULL, OBJ_MULTIPLE, OBJ_NODATA, ptr_matcher(), and transport_monitor_unregister_cb().

Referenced by registrar_contact_delete(), and unload_module().

259 {
260  struct ao2_container *transports;
261  struct callback_data cb_data = {
262  .cb = cb,
263  .data = data,
264  .matches = matches ?: ptr_matcher,
265  };
266 
267  ast_assert(cb != NULL);
268 
269  transports = ao2_global_obj_ref(active_transports);
270  if (!transports) {
271  return;
272  }
274  ao2_ref(transports, -1);
275 }
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
#define ao2_global_obj_ref(holder)
Definition: astobj2.h:925
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
ast_transport_monitor_data_matcher matches
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static int transport_monitor_unregister_cb(void *obj, void *arg, int flags)
static int ptr_matcher(void *a, void *b)
Generic container type.
ast_transport_monitor_shutdown_cb cb

◆ ast_sip_transport_state_register()

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.
Returns
Nothing

Definition at line 366 of file pjsip_transport_events.c.

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

Referenced by ast_sip_initialize_transport_management().

367 {
368  struct ast_sip_tpmgr_state_callback *tpmgr_notifier;
369 
371  AST_LIST_TRAVERSE(&transport_state_list, tpmgr_notifier, node) {
372  if (element == tpmgr_notifier) {
373  /* Already registered. */
375  return;
376  }
377  }
380 }
Definition: test_heap.c:38
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static struct @480 transport_state_list
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:710

◆ ast_sip_transport_state_unregister()

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.
Returns
Nothing

Definition at line 359 of file pjsip_transport_events.c.

References AST_LIST_REMOVE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and transport_state_list.

Referenced by ast_sip_destroy_transport_management().

360 {
364 }
Definition: test_heap.c:38
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
Definition: linkedlists.h:855
static struct @480 transport_state_list

◆ ptr_matcher()

static int ptr_matcher ( void *  a,
void *  b 
)
static

Definition at line 252 of file pjsip_transport_events.c.

References b.

Referenced by ast_sip_transport_monitor_register_replace(), ast_sip_transport_monitor_unregister(), and ast_sip_transport_monitor_unregister_all().

253 {
254  return a == b;
255 }
static struct test_val b
static struct test_val a

◆ transport_monitor_dtor()

static void transport_monitor_dtor ( void *  vdoomed)
static

Definition at line 103 of file pjsip_transport_events.c.

References ao2_cleanup, AST_VECTOR_FREE, AST_VECTOR_GET_ADDR, AST_VECTOR_SIZE, and transport_monitor_notifier::data.

Referenced by transport_state_callback().

104 {
105  struct transport_monitor *monitored = vdoomed;
106  int idx;
107 
108  for (idx = AST_VECTOR_SIZE(&monitored->monitors); idx--;) {
109  struct transport_monitor_notifier *notifier;
110 
111  notifier = AST_VECTOR_GET_ADDR(&monitored->monitors, idx);
112  ao2_cleanup(notifier->data);
113  }
114  AST_VECTOR_FREE(&monitored->monitors);
115 }
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174
Structure for transport to be monitored.
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.
Definition: vector.h:670
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611

◆ transport_monitor_unregister_cb()

static int transport_monitor_unregister_cb ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 231 of file pjsip_transport_events.c.

References ao2_cleanup, ast_debug, AST_VECTOR_GET_ADDR, AST_VECTOR_REMOVE_UNORDERED, AST_VECTOR_SIZE, transport_monitor_notifier::cb, callback_data::cb, transport_monitor_notifier::data, callback_data::data, callback_data::matches, and transport_monitor::transport.

Referenced by ast_sip_transport_monitor_register_replace(), ast_sip_transport_monitor_unregister(), and ast_sip_transport_monitor_unregister_all().

232 {
233  struct transport_monitor *monitored = obj;
234  struct callback_data *cb_data = arg;
235  int idx;
236 
237  for (idx = AST_VECTOR_SIZE(&monitored->monitors); idx--;) {
238  struct transport_monitor_notifier *notifier;
239 
240  notifier = AST_VECTOR_GET_ADDR(&monitored->monitors, idx);
241  if (notifier->cb == cb_data->cb && (!cb_data->data
242  || cb_data->matches(cb_data->data, notifier->data))) {
243  ao2_cleanup(notifier->data);
244  AST_VECTOR_REMOVE_UNORDERED(&monitored->monitors, idx);
245  ast_debug(3, "Unregistered monitor %p(%p) from transport %s\n",
246  notifier->cb, notifier->data, monitored->transport->obj_name);
247  }
248  }
249  return 0;
250 }
#define AST_VECTOR_REMOVE_UNORDERED(vec, idx)
Remove an element from an unordered vector by index.
Definition: vector.h:438
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
Structure for transport to be monitored.
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.
Definition: vector.h:670
ast_transport_monitor_data_matcher matches
pjsip_transport * transport
The underlying PJSIP transport.
ast_transport_monitor_shutdown_cb cb
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611
ast_transport_monitor_shutdown_cb cb

◆ transport_state2str()

static const char* transport_state2str ( pjsip_transport_state  state)
static

Definition at line 74 of file pjsip_transport_events.c.

References ast_assert, and name.

Referenced by transport_state_callback().

75 {
76  const char *name;
77 
78  switch (state) {
79  case PJSIP_TP_STATE_CONNECTED:
80  name = "CONNECTED";
81  break;
82  case PJSIP_TP_STATE_DISCONNECTED:
83  name = "DISCONNECTED";
84  break;
85  case PJSIP_TP_STATE_SHUTDOWN:
86  name = "SHUTDOWN";
87  break;
88  case PJSIP_TP_STATE_DESTROY:
89  name = "DESTROY";
90  break;
91  default:
92  /*
93  * We have to have a default case because the enum is
94  * defined by a third-party library.
95  */
96  ast_assert(0);
97  name = "<unknown>";
98  break;
99  }
100  return name;
101 }
#define ast_assert(a)
Definition: utils.h:695
static const char name[]
Definition: cdr_mysql.c:74

◆ transport_state_callback()

static void transport_state_callback ( pjsip_transport *  transport,
pjsip_transport_state  state,
const pjsip_transport_state_info *  info 
)
static

Callback invoked when transport state changes occur.

Definition at line 148 of file pjsip_transport_events.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_alloc_options, ao2_global_obj_ref, ao2_link, ao2_ref, ast_assert, ast_debug, AST_LIST_EMPTY, AST_LIST_TRAVERSE, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, AST_VECTOR_INIT, ast_sip_tpmgr_state_callback::cb, tpmgr_state_callback, transport_monitor::transport, transport_monitor_dtor(), transport_state2str(), transport_state_do_reg_callbacks(), and transport_state_list.

Referenced by ast_sip_initialize_transport_events().

150 {
151  struct ao2_container *transports;
152 
153  /* We only care about monitoring reliable transports */
154  if (PJSIP_TRANSPORT_IS_RELIABLE(transport)
155  && (transports = ao2_global_obj_ref(active_transports))) {
156  struct transport_monitor *monitored;
157 
158  ast_debug(3, "Reliable transport '%s' state:%s\n",
159  transport->obj_name, transport_state2str(state));
160  switch (state) {
161  case PJSIP_TP_STATE_CONNECTED:
162  monitored = ao2_alloc_options(sizeof(*monitored),
164  if (!monitored) {
165  break;
166  }
167  monitored->transport = transport;
168  if (AST_VECTOR_INIT(&monitored->monitors, 5)) {
169  ao2_ref(monitored, -1);
170  break;
171  }
172 
173  ao2_link(transports, monitored);
174  ao2_ref(monitored, -1);
175  break;
176  case PJSIP_TP_STATE_DISCONNECTED:
177  if (!transport->is_shutdown) {
178  pjsip_transport_shutdown(transport);
179  }
181  break;
182  case PJSIP_TP_STATE_SHUTDOWN:
183  /*
184  * Set shutdown flag early so we can force a new transport to be
185  * created if a monitor callback needs to reestablish a link.
186  * PJPROJECT sets the flag after this routine returns even though
187  * it has already called the transport's shutdown routine.
188  */
189  transport->is_shutdown = PJ_TRUE;
190 
192  break;
193  case PJSIP_TP_STATE_DESTROY:
195  break;
196  default:
197  /*
198  * We have to have a default case because the enum is
199  * defined by a third-party library.
200  */
201  ast_assert(0);
202  break;
203  }
204 
205  ao2_ref(transports, -1);
206  }
207 
208  /* Loop over other transport state callbacks registered with us. */
210  struct ast_sip_tpmgr_state_callback *tpmgr_notifier;
211 
213  AST_LIST_TRAVERSE(&transport_state_list, tpmgr_notifier, node) {
214  tpmgr_notifier->cb(transport, state, info);
215  }
217  }
218 
219  /* Forward to the old state callback if present */
220  if (tpmgr_state_callback) {
221  tpmgr_state_callback(transport, state, info);
222  }
223 }
Definition: test_heap.c:38
static pjsip_tp_state_callback tpmgr_state_callback
Existing transport events callback that we need to invoke.
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define ao2_global_obj_ref(holder)
Definition: astobj2.h:925
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:449
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define ast_assert(a)
Definition: utils.h:695
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
Structure for transport to be monitored.
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113
static const char * transport_state2str(pjsip_transport_state state)
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static void transport_state_do_reg_callbacks(struct ao2_container *transports, pjsip_transport *transport)
pjsip_transport * transport
The underlying PJSIP transport.
def info(msg)
static struct @480 transport_state_list
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
pjsip_tp_state_callback cb
Definition: res_pjsip.h:3555
Generic container type.
static void transport_monitor_dtor(void *vdoomed)
#define ao2_link(container, obj)
Definition: astobj2.h:1549

◆ transport_state_do_reg_callbacks()

static void transport_state_do_reg_callbacks ( struct ao2_container transports,
pjsip_transport *  transport 
)
static

Definition at line 127 of file pjsip_transport_events.c.

References ao2_find, ao2_ref, ast_debug, AST_VECTOR_GET_ADDR, AST_VECTOR_SIZE, transport_monitor_notifier::cb, transport_monitor_notifier::data, OBJ_SEARCH_KEY, and OBJ_UNLINK.

Referenced by transport_state_callback().

128 {
129  struct transport_monitor *monitored;
130 
131  monitored = ao2_find(transports, transport->obj_name, OBJ_SEARCH_KEY | OBJ_UNLINK);
132  if (monitored) {
133  int idx;
134 
135  for (idx = AST_VECTOR_SIZE(&monitored->monitors); idx--;) {
136  struct transport_monitor_notifier *notifier;
137 
138  notifier = AST_VECTOR_GET_ADDR(&monitored->monitors, idx);
139  ast_debug(3, "running callback %p(%p) for transport %s\n",
140  notifier->cb, notifier->data, transport->obj_name);
141  notifier->cb(notifier->data);
142  }
143  ao2_ref(monitored, -1);
144  }
145 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
Structure for transport to be monitored.
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.
Definition: vector.h:670
#define ao2_ref(o, delta)
Definition: astobj2.h:464
pjsip_transport * transport
The underlying PJSIP transport.
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
ast_transport_monitor_shutdown_cb cb
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611

Variable Documentation

◆ first

Definition at line 65 of file pjsip_transport_events.c.

◆ last

Definition at line 65 of file pjsip_transport_events.c.

◆ lock

Definition at line 65 of file pjsip_transport_events.c.

◆ tpmgr_state_callback

pjsip_tp_state_callback tpmgr_state_callback
static

Existing transport events callback that we need to invoke.

Definition at line 62 of file pjsip_transport_events.c.

Referenced by ast_sip_destroy_transport_events(), ast_sip_initialize_transport_events(), and transport_state_callback().

◆ transport_state_list

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().