Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions
autochan.h File Reference

"smart" channels that update automatically if a channel is masqueraded More...

#include "asterisk.h"
#include "asterisk/linkedlists.h"
Include dependency graph for autochan.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_autochan
 

Macros

#define ast_autochan_channel_lock(autochan)
 Lock the autochan's channel lock. More...
 
#define ast_autochan_channel_unlock(autochan)   ast_channel_unlock(autochan->chan)
 

Functions

void ast_autochan_destroy (struct ast_autochan *autochan)
 destroy an ast_autochan structure More...
 
void ast_autochan_new_channel (struct ast_channel *old_chan, struct ast_channel *new_chan)
 Switch what channel autochans point to. More...
 
struct ast_autochanast_autochan_setup (struct ast_channel *chan)
 set up a new ast_autochan structure More...
 

Detailed Description

"smart" channels that update automatically if a channel is masqueraded

Author
Mark Michelson mmich.nosp@m.elso.nosp@m.n@dig.nosp@m.ium..nosp@m.com

Definition in file autochan.h.

Macro Definition Documentation

◆ ast_autochan_channel_lock

#define ast_autochan_channel_lock (   autochan)

Lock the autochan's channel lock.

Just what the $!# is an autochan?

An ast_autochan is a structure which contains an ast_channel. The pointer inside an autochan has the ability to update itself if the channel it points to is masqueraded into a different channel.

This has a great benefit for any application or service which creates a thread outside of the channel's main operating thread which keeps a pointer to said channel. when a masquerade occurs on the channel, the autochan's chan pointer will automatically update to point to the new channel.

Some rules for autochans

  1. If you are going to use an autochan, then be sure to always refer to the channel using the chan pointer inside the autochan if possible, since this is the pointer that will be updated during a masquerade.
  2. If you are going to save off a pointer to the autochan's chan, then be sure to save off the pointer using ast_channel_ref and to unref the channel when you are finished with the pointer. If you do not do this and a masquerade occurs on the channel, then it is possible that your saved pointer will become invalid.
  3. If you want to lock the autochan->chan channel, be sure to use ast_autochan_channel_lock and ast_autochan_channel_unlock. An attempt to lock the autochan->chan directly may result in it being changed after you've retrieved the value of chan, but before you've had a chance to lock it. While chan is locked, the autochan structure is guaranteed to keep the same channel.
Note
We must do deadlock avoidance because the channel lock is superior to the autochan lock in locking order.

Definition at line 75 of file autochan.h.

Referenced by ast_autochan_destroy(), attach_barge(), channel_spy(), common_exec(), mixmonitor_autochan_is_bridged(), mixmonitor_thread(), setup_mixmonitor_ds(), and start_spying().

◆ ast_autochan_channel_unlock

#define ast_autochan_channel_unlock (   autochan)    ast_channel_unlock(autochan->chan)

Function Documentation

◆ ast_autochan_destroy()

void ast_autochan_destroy ( struct ast_autochan autochan)

destroy an ast_autochan structure

Removes the passed-in autochan from the list of autochans and unrefs the channel that is pointed to. Also frees the autochan struct itself. This function will unref the channel reference which was made in ast_autochan_setup

Parameters
autochanThe autochan that you wish to destroy
Return values
void

Definition at line 64 of file autochan.c.

References ast_autochan_channel_lock, ast_autochan_channel_unlock, ast_channel_autochans(), ast_channel_unref, ast_debug, ast_free, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_mutex_destroy, ast_autochan::chan, ast_autochan::list, and ast_autochan::lock.

Referenced by channel_spy(), common_exec(), launch_monitor_thread(), and mixmonitor_thread().

65 {
66  struct ast_autochan *autochan_iter;
67 
68  ast_autochan_channel_lock(autochan);
69  AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_autochans(autochan->chan), autochan_iter, list) {
70  if (autochan_iter == autochan) {
72  ast_debug(1, "Removed autochan %p from the list, about to free it\n", autochan);
73  break;
74  }
75  }
78 
79  autochan->chan = ast_channel_unref(autochan->chan);
80 
81  ast_mutex_destroy(&autochan->lock);
82 
83  ast_free(autochan);
84 }
#define ast_autochan_channel_lock(autochan)
Lock the autochan's channel lock.
Definition: autochan.h:75
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2981
struct ast_channel * chan
Definition: autochan.h:33
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:614
struct ast_autochan::@226 list
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:556
#define ast_free(a)
Definition: astmm.h:182
struct ast_autochan_list * ast_channel_autochans(struct ast_channel *chan)
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:528
#define ast_autochan_channel_unlock(autochan)
Definition: autochan.h:84
#define ast_mutex_destroy(a)
Definition: lock.h:186
ast_mutex_t lock
Definition: autochan.h:35

◆ ast_autochan_new_channel()

void ast_autochan_new_channel ( struct ast_channel old_chan,
struct ast_channel new_chan 
)

Switch what channel autochans point to.

Traverses the list of autochans. All autochans which point to old_chan will be updated to point to new_chan instead. Currently this is only called during an ast_channel_move() operation in channel.c.

Precondition
Both channels must be locked before calling this function.
Parameters
old_chanThe channel that autochans may currently point to
new_chanThe channel that we want to point those autochans to now
Return values
void

Definition at line 86 of file autochan.c.

References ast_channel_autochans(), ast_channel_name(), ast_channel_ref, ast_channel_unref, ast_debug, AST_LIST_APPEND_LIST, AST_LIST_TRAVERSE, ast_mutex_lock, ast_mutex_unlock, ast_autochan::chan, ast_autochan::list, and ast_autochan::lock.

Referenced by channel_do_masquerade().

87 {
88  struct ast_autochan *autochan;
89 
91 
92  /* Deadlock avoidance is not needed since the channels are already locked. */
93  AST_LIST_TRAVERSE(ast_channel_autochans(new_chan), autochan, list) {
94  ast_mutex_lock(&autochan->lock);
95  if (autochan->chan == old_chan) {
96  autochan->chan = ast_channel_ref(new_chan);
97  ast_channel_unref(old_chan);
98 
99  ast_debug(1, "Autochan %p used to hold channel %s (%p) but now holds channel %s (%p)\n",
100  autochan, ast_channel_name(old_chan), old_chan, ast_channel_name(new_chan), new_chan);
101  }
102  ast_mutex_unlock(&autochan->lock);
103  }
104 }
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2981
struct ast_channel * chan
Definition: autochan.h:33
#define ast_mutex_lock(a)
Definition: lock.h:187
struct ast_autochan::@226 list
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
struct ast_autochan_list * ast_channel_autochans(struct ast_channel *chan)
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2970
const char * ast_channel_name(const struct ast_channel *chan)
#define ast_mutex_unlock(a)
Definition: lock.h:188
ast_mutex_t lock
Definition: autochan.h:35
#define AST_LIST_APPEND_LIST(head, list, field)
Appends a whole list to the tail of a list.
Definition: linkedlists.h:782

◆ ast_autochan_setup()

struct ast_autochan* ast_autochan_setup ( struct ast_channel chan)

set up a new ast_autochan structure

Allocates and initializes an ast_autochan, sets the autochan's chan pointer to point to the chan parameter, and adds the autochan to the global list of autochans. The newly- created autochan is returned to the caller. This function will cause the refcount of chan to increase by 1.

Parameters
chanThe channel our new autochan will point to
Note
autochans must be freed using ast_autochan_destroy
Return values
NULLFailure
non-NULLsuccess

Definition at line 38 of file autochan.c.

References ast_calloc, ast_channel_autochans(), ast_channel_lock, ast_channel_name(), ast_channel_ref, ast_channel_unlock, ast_debug, AST_LIST_INSERT_TAIL, ast_mutex_init, ast_autochan::chan, ast_autochan::list, ast_autochan::lock, and NULL.

Referenced by attach_barge(), common_exec(), launch_monitor_thread(), and next_channel().

39 {
40  struct ast_autochan *autochan;
41 
42  if (!chan) {
43  return NULL;
44  }
45 
46  if (!(autochan = ast_calloc(1, sizeof(*autochan)))) {
47  return NULL;
48  }
49  ast_mutex_init(&autochan->lock);
50 
51  autochan->chan = ast_channel_ref(chan);
52 
53  ast_debug(1, "Created autochan %p to hold channel %s (%p)\n",
54  autochan, ast_channel_name(chan), chan);
55 
56  /* autochan is still private, no need for ast_autochan_channel_lock() */
57  ast_channel_lock(autochan->chan);
58  AST_LIST_INSERT_TAIL(ast_channel_autochans(autochan->chan), autochan, list);
59  ast_channel_unlock(autochan->chan);
60 
61  return autochan;
62 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
struct ast_channel * chan
Definition: autochan.h:33
#define NULL
Definition: resample.c:96
struct ast_autochan::@226 list
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:730
#define ast_channel_unlock(chan)
Definition: channel.h:2946
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
struct ast_autochan_list * ast_channel_autochans(struct ast_channel *chan)
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2970
const char * ast_channel_name(const struct ast_channel *chan)
#define ast_mutex_init(pmutex)
Definition: lock.h:184
ast_mutex_t lock
Definition: autochan.h:35