FreeSWITCH API Documentation  1.7.0
Macros | Functions
LIMIT code
+ Collaboration diagram for LIMIT code:

Macros

#define SWITCH_LIMIT_INCR(name)   static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)
 
#define SWITCH_LIMIT_RELEASE(name)   static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource)
 
#define SWITCH_LIMIT_USAGE(name)   static int name (const char *realm, const char *resource, uint32_t *rcount)
 
#define SWITCH_LIMIT_RESET(name)   static switch_status_t name (void)
 
#define SWITCH_LIMIT_INTERVAL_RESET(name)   static switch_status_t name (const char *realm, const char *resource)
 
#define SWITCH_LIMIT_STATUS(name)   static char * name (void)
 
#define LIMIT_IGNORE_TRANSFER_VARIABLE   "limit_ignore_transfer"
 
#define LIMIT_BACKEND_VARIABLE   "limit_backend"
 
#define LIMIT_EVENT_USAGE   "limit::usage"
 
#define LIMIT_DEF_XFER_EXTEN   "limit_exceeded"
 

Functions

SWITCH_BEGIN_EXTERN_C void switch_limit_init (switch_memory_pool_t *pool)
 Initilize the LIMIT Core System. More...
 
switch_status_t switch_limit_incr (const char *backend, switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)
 Increment resource. More...
 
switch_status_t switch_limit_release (const char *backend, switch_core_session_t *session, const char *realm, const char *resource)
 Release resource. More...
 
int switch_limit_usage (const char *backend, const char *realm, const char *resource, uint32_t *rcount)
 get usage count for resource More...
 
switch_status_t switch_limit_interval_reset (const char *backend, const char *realm, const char *resource)
 reset interval usage counter for a given resource More...
 
switch_status_t switch_limit_reset (const char *backend)
 reset all usage counters More...
 
void switch_limit_fire_event (const char *backend, const char *realm, const char *resource, uint32_t usage, uint32_t rate, uint32_t max, uint32_t ratemax)
 fire event for limit usage More...
 
char * switch_limit_status (const char *backend)
 retrieve arbitrary status information More...
 

Detailed Description

Macro Definition Documentation

#define LIMIT_BACKEND_VARIABLE   "limit_backend"

Definition at line 121 of file switch_limit.h.

Referenced by limit_state_handler(), and switch_limit_incr().

#define LIMIT_DEF_XFER_EXTEN   "limit_exceeded"

Definition at line 123 of file switch_limit.h.

#define LIMIT_EVENT_USAGE   "limit::usage"

Definition at line 122 of file switch_limit.h.

Referenced by switch_limit_fire_event(), and switch_limit_init().

#define LIMIT_IGNORE_TRANSFER_VARIABLE   "limit_ignore_transfer"

Definition at line 120 of file switch_limit.h.

Referenced by limit_state_handler().

#define SWITCH_LIMIT_INCR (   name)    static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)

callback to init a backend

Definition at line 113 of file switch_limit.h.

#define SWITCH_LIMIT_INTERVAL_RESET (   name)    static switch_status_t name (const char *realm, const char *resource)

Definition at line 117 of file switch_limit.h.

#define SWITCH_LIMIT_RELEASE (   name)    static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource)

Definition at line 114 of file switch_limit.h.

#define SWITCH_LIMIT_RESET (   name)    static switch_status_t name (void)

Definition at line 116 of file switch_limit.h.

#define SWITCH_LIMIT_STATUS (   name)    static char * name (void)

Definition at line 118 of file switch_limit.h.

#define SWITCH_LIMIT_USAGE (   name)    static int name (const char *realm, const char *resource, uint32_t *rcount)

Definition at line 115 of file switch_limit.h.

Function Documentation

void switch_limit_fire_event ( const char *  backend,
const char *  realm,
const char *  resource,
uint32_t  usage,
uint32_t  rate,
uint32_t  max,
uint32_t  ratemax 
)

fire event for limit usage

Parameters
backendto use
realm
resource
usage
rate
max
ratemax

Definition at line 62 of file switch_limit.c.

References LIMIT_EVENT_USAGE, switch_event_add_header(), switch_event_add_header_string(), switch_event_create_subclass, SWITCH_EVENT_CUSTOM, switch_event_fire, SWITCH_STACK_BOTTOM, and SWITCH_STATUS_SUCCESS.

63 {
64  switch_event_t *event;
65 
67  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "backend", backend);
70  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "usage", "%d", usage);
71  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "rate", "%d", rate);
72  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "max", "%d", max);
73  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ratemax", "%d", ratemax);
74  switch_event_fire(&event);
75  }
76 }
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:412
#define LIMIT_EVENT_USAGE
Definition: switch_limit.h:122
switch_status_t switch_event_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...) PRINTF_FUNCTION(4
Add a header to an event.
Representation of an event.
Definition: switch_event.h:80
#define switch_event_create_subclass(_e, _eid, _sn)
Definition: switch_event.h:153
static const char usage[]
Definition: switch.c:409
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
switch_status_t switch_limit_incr ( const char *  backend,
switch_core_session_t session,
const char *  realm,
const char *  resource,
const int  max,
const int  interval 
)

Increment resource.

Parameters
backendto use
realm
resource
max- 0 means no limit, just count
interval- 0 means no interval
Returns
true/false - true ok, false over limit

Definition at line 111 of file switch_limit.c.

References get_backend(), switch_limit_interface::incr, LIMIT_BACKEND_VARIABLE, limit_state_handler(), release_backend(), switch_channel_get_variable, SWITCH_CHANNEL_SESSION_LOG, switch_channel_set_variable, switch_channel_set_variable_printf(), switch_core_session_get_channel(), switch_goto_status, SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

111  {
112  switch_limit_interface_t *limit = NULL;
113  switch_channel_t *channel = NULL;
114  int status = SWITCH_STATUS_SUCCESS;
115 
116  assert(session);
117 
118  channel = switch_core_session_get_channel(session);
119 
120  /* locate impl, call appropriate func */
121  if (!(limit = get_backend(backend))) {
122  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
124  }
125 
126  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "incr called: %s_%s max:%d, interval:%d\n",
127  realm, resource, max, interval);
128 
129  if ((status = limit->incr(session, realm, resource, max, interval)) == SWITCH_STATUS_SUCCESS) {
130  /* race condition? what if another leg is doing the same thing? */
131  const char *existing = switch_channel_get_variable(channel, LIMIT_BACKEND_VARIABLE);
132  if (existing) {
133  if (!strstr(existing, backend)) {
134  switch_channel_set_variable_printf(channel, LIMIT_BACKEND_VARIABLE, "%s,%s", existing, backend);
135  }
136  } else {
138  switch_core_event_hook_add_state_change(session, limit_state_handler);
139  }
140  }
141 
142  release_backend(limit);
143 
144 end:
145  return status;
146 }
switch_status_t switch_channel_set_variable_printf(switch_channel_t *channel, const char *varname, const char *fmt,...)
#define SWITCH_CHANNEL_SESSION_LOG(x)
Abstract interface to a limit module.
switch_status_t(* incr)(switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
#define switch_channel_get_variable(_c, _v)
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:256
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35
#define LIMIT_BACKEND_VARIABLE
Definition: switch_limit.h:121
#define switch_channel_set_variable(_channel, _var, _val)
static switch_status_t limit_state_handler(switch_core_session_t *session)
Definition: switch_limit.c:78
SWITCH_BEGIN_EXTERN_C void switch_limit_init ( switch_memory_pool_t pool)

Initilize the LIMIT Core System.

Parameters
poolthe memory pool to use for long term allocations
Note
Generally called by the core_init

Definition at line 55 of file switch_limit.c.

References LIMIT_EVENT_USAGE, SWITCH_CHANNEL_LOG, switch_event_reserve_subclass, SWITCH_LOG_ERROR, switch_log_printf(), and SWITCH_STATUS_SUCCESS.

55  {
57  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register event subclass \"%s\"", LIMIT_EVENT_USAGE);
58  }
59 }
#define SWITCH_CHANNEL_LOG
#define LIMIT_EVENT_USAGE
Definition: switch_limit.h:122
#define switch_event_reserve_subclass(subclass_name)
Reserve a subclass assuming the owner string is the current filename.
Definition: switch_event.h:374
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_limit_interval_reset ( const char *  backend,
const char *  realm,
const char *  resource 
)

reset interval usage counter for a given resource

Parameters
backend
realm
resource

Definition at line 199 of file switch_limit.c.

References get_backend(), switch_limit_interface::interval_reset, release_backend(), SWITCH_CHANNEL_LOG, switch_goto_status, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

199  {
200  switch_limit_interface_t *limit = NULL;
201  int status = SWITCH_STATUS_SUCCESS;
202 
203  /* locate impl, call appropriate func */
204  if (!(limit = get_backend(backend))) {
205  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
207  }
208 
209  if (!limit->interval_reset) {
210  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s does not implement interval_reset!\n", backend);
212  }
213 
214  status = limit->interval_reset(realm, resource);
215 
216 end:
217  release_backend(limit);
218  return status;
219 }
Abstract interface to a limit module.
#define SWITCH_CHANNEL_LOG
switch_status_t(* interval_reset)(const char *realm, const char *resource)
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:256
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35
switch_status_t switch_limit_release ( const char *  backend,
switch_core_session_t session,
const char *  realm,
const char *  resource 
)

Release resource.

Parameters
backendto use
realm
resource
Returns
true/false - true ok, false over limit

Definition at line 148 of file switch_limit.c.

References get_backend(), switch_limit_interface::release, release_backend(), SWITCH_CHANNEL_SESSION_LOG, switch_goto_status, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

Referenced by limit_state_handler().

148  {
149  switch_limit_interface_t *limit = NULL;
150  int status = SWITCH_STATUS_SUCCESS;
151 
152  /* locate impl, call appropriate func */
153  if (!(limit = get_backend(backend))) {
154  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
156  }
157 
158  status = limit->release(session, realm, resource);
159 
160 end:
161  release_backend(limit);
162  return status;
163 }
switch_status_t(* release)(switch_core_session_t *session, const char *realm, const char *resource)
#define SWITCH_CHANNEL_SESSION_LOG(x)
Abstract interface to a limit module.
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:256
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35
switch_status_t switch_limit_reset ( const char *  backend)

reset all usage counters

Parameters
backendto use

Definition at line 182 of file switch_limit.c.

References get_backend(), release_backend(), switch_limit_interface::reset, SWITCH_CHANNEL_LOG, switch_goto_status, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

182  {
183  switch_limit_interface_t *limit = NULL;
184  int status = SWITCH_STATUS_SUCCESS;
185 
186  /* locate impl, call appropriate func */
187  if (!(limit = get_backend(backend))) {
188  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
190  }
191 
192  status = limit->reset();
193 
194 end:
195  release_backend(limit);
196  return status;
197 }
Abstract interface to a limit module.
#define SWITCH_CHANNEL_LOG
switch_status_t(* reset)(void)
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:256
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35
char* switch_limit_status ( const char *  backend)

retrieve arbitrary status information

Parameters
backendto use
Note
caller must free returned value

Definition at line 221 of file switch_limit.c.

References get_backend(), release_backend(), switch_limit_interface::status, SWITCH_CHANNEL_LOG, switch_goto_status, SWITCH_LOG_ERROR, and switch_log_printf().

221  {
222  switch_limit_interface_t *limit = NULL;
223  char *status = NULL;
224 
225  /* locate impl, call appropriate func */
226  if (!(limit = get_backend(backend))) {
227  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
228  switch_goto_status(strdup("-ERR"), end);
229  }
230 
231  status = limit->status();
232 
233 end:
234  release_backend(limit);
235  return status;
236 }
Abstract interface to a limit module.
#define SWITCH_CHANNEL_LOG
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:256
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35
int switch_limit_usage ( const char *  backend,
const char *  realm,
const char *  resource,
uint32_t *  rcount 
)

get usage count for resource

Parameters
backendto use
realm
resource
rcount- output paramter, rate counter

Definition at line 165 of file switch_limit.c.

References get_backend(), release_backend(), SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), usage, and switch_limit_interface::usage.

165  {
166  switch_limit_interface_t *limit = NULL;
167  int usage = 0;
168 
169  /* locate impl, call appropriate func */
170  if (!(limit = get_backend(backend))) {
171  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
172  goto end;
173  }
174 
175  usage = limit->usage(realm, resource, rcount);
176 
177 end:
178  release_backend(limit);
179  return usage;
180 }
Abstract interface to a limit module.
#define SWITCH_CHANNEL_LOG
static const char usage[]
Definition: switch.c:409
int(* usage)(const char *realm, const char *resource, uint32_t *rcount)
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35