RTS API Documentation  1.10.11
switch_limit.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Rupa Schomaker <rupa@rupa.com>
27  *
28  * switch_limit.c Limit support
29  *
30  */
31 
32 #include <switch.h>
33 #include <switch_module_interfaces.h> /* this is odd VS 2008 Express requires this- include order problem?? */
34 
35 static switch_limit_interface_t *get_backend(const char *backend) {
36  switch_limit_interface_t *limit = NULL;
37 
38  if (!backend) {
39  return NULL;
40  }
41 
42  if (!(limit = switch_loadable_module_get_limit_interface(backend))) {
43  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Unable to locate limit backend: %s\n", backend);
44  }
45 
46  return limit;
47 }
48 
50  UNPROTECT_INTERFACE(limit);
51 }
52 
55  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register event subclass \"%s\"", LIMIT_EVENT_USAGE);
56  }
57 }
58 
59 
60 SWITCH_DECLARE(void) switch_limit_fire_event(const char *backend, const char *realm, const char *key, uint32_t usage, uint32_t rate, uint32_t max, uint32_t ratemax)
61 {
62  switch_event_t *event;
63 
65  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "backend", backend);
68  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "usage", "%d", usage);
69  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "rate", "%d", rate);
70  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "max", "%d", max);
71  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ratemax", "%d", ratemax);
72  switch_event_fire(&event);
73  }
74 }
75 
77 {
81  const char *backendlist = switch_channel_get_variable(channel, LIMIT_BACKEND_VARIABLE);
82 
83  if (zstr(backendlist)) {
84  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Unset limit backendlist!\n");
85  return SWITCH_STATUS_SUCCESS;
86  }
87 
88  if (state >= CS_HANGUP || (state == CS_ROUTING && !switch_true(vval))) {
89  int argc = 0;
90  char *argv[6] = { 0 };
91  char *mydata = strdup(backendlist);
92  int x;
93 
94  argc = switch_separate_string(mydata, ',', argv, (sizeof(argv) / sizeof(argv[0])));
95  for (x = 0; x < argc; x++) {
96  switch_limit_release(argv[x], session, NULL, NULL);
97  }
98  switch_core_event_hook_remove_state_change(session, limit_state_handler);
99  /* Remove limit_backend variable so we register another hook if limit is called again */
101 
102  free(mydata);
103  }
104 
105  return SWITCH_STATUS_SUCCESS;
106 }
107 
108 
109 SWITCH_DECLARE(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) {
110  switch_limit_interface_t *limit = NULL;
111  switch_channel_t *channel = NULL;
112  int status = SWITCH_STATUS_SUCCESS;
113 
114  assert(session);
115 
116  channel = switch_core_session_get_channel(session);
117 
118  /* locate impl, call appropriate func */
119  if (!(limit = get_backend(backend))) {
120  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
122  }
123 
124  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "incr called: %s_%s max:%d, interval:%d\n",
125  realm, resource, max, interval);
126 
127  if ((status = limit->incr(session, realm, resource, max, interval)) == SWITCH_STATUS_SUCCESS) {
128  /* race condition? what if another leg is doing the same thing? */
129  const char *existing = switch_channel_get_variable(channel, LIMIT_BACKEND_VARIABLE);
130  if (existing) {
131  if (!strstr(existing, backend)) {
132  switch_channel_set_variable_printf(channel, LIMIT_BACKEND_VARIABLE, "%s,%s", existing, backend);
133  }
134  } else {
136  switch_core_event_hook_add_state_change(session, limit_state_handler);
137  }
138  }
139 
140  release_backend(limit);
141 
142 end:
143  return status;
144 }
145 
146 SWITCH_DECLARE(switch_status_t) switch_limit_release(const char *backend, switch_core_session_t *session, const char *realm, const char *resource) {
147  switch_limit_interface_t *limit = NULL;
148  int status = SWITCH_STATUS_SUCCESS;
149 
150  /* locate impl, call appropriate func */
151  if (!(limit = get_backend(backend))) {
152  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
154  }
155 
156  status = limit->release(session, realm, resource);
157 
158 end:
159  release_backend(limit);
160  return status;
161 }
162 
163 SWITCH_DECLARE(int) switch_limit_usage(const char *backend, const char *realm, const char *resource, uint32_t *rcount) {
164  switch_limit_interface_t *limit = NULL;
165  int usage = 0;
166 
167  /* locate impl, call appropriate func */
168  if (!(limit = get_backend(backend))) {
169  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
170  goto end;
171  }
172 
173  usage = limit->usage(realm, resource, rcount);
174 
175 end:
176  release_backend(limit);
177  return usage;
178 }
179 
181  switch_limit_interface_t *limit = NULL;
182  int status = SWITCH_STATUS_SUCCESS;
183 
184  /* locate impl, call appropriate func */
185  if (!(limit = get_backend(backend))) {
186  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
188  }
189 
190  status = limit->reset();
191 
192 end:
193  release_backend(limit);
194  return status;
195 }
196 
197 SWITCH_DECLARE(switch_status_t) switch_limit_interval_reset(const char *backend, const char *realm, const char *resource) {
198  switch_limit_interface_t *limit = NULL;
199  int status = SWITCH_STATUS_SUCCESS;
200 
201  /* locate impl, call appropriate func */
202  if (!(limit = get_backend(backend))) {
203  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
205  }
206 
207  if (!limit->interval_reset) {
208  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s does not implement interval_reset!\n", backend);
210  }
211 
212  status = limit->interval_reset(realm, resource);
213 
214 end:
215  release_backend(limit);
216  return status;
217 }
218 
219 SWITCH_DECLARE(char *) switch_limit_status(const char *backend) {
220  switch_limit_interface_t *limit = NULL;
221  char *status = NULL;
222 
223  /* locate impl, call appropriate func */
224  if (!(limit = get_backend(backend))) {
225  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
226  switch_goto_status(strdup("-ERR"), end);
227  }
228 
229  status = limit->status();
230 
231 end:
232  release_backend(limit);
233  return status;
234 }
235 
236 /* For Emacs:
237  * Local Variables:
238  * mode:c
239  * indent-tabs-mode:t
240  * tab-width:4
241  * c-basic-offset:4
242  * End:
243  * For VIM:
244  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
245  */
switch_status_t switch_channel_set_variable_printf(switch_channel_t *channel, const char *varname, const char *fmt,...)
switch_channel_state_t switch_channel_get_state(switch_channel_t *channel)
Get the current state of a channel in the state engine.
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:413
static switch_bool_t switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:519
#define SWITCH_CHANNEL_SESSION_LOG(x)
Abstract interface to a limit module.
#define SWITCH_CHANNEL_LOG
#define LIMIT_EVENT_USAGE
Definition: switch_limit.h:122
switch_status_t(* reset)(void)
switch_status_t(* release)(switch_core_session_t *session, const char *realm, const char *resource)
switch_status_t switch_limit_interval_reset(const char *backend, const char *realm, const char *resource)
reset interval usage counter for a given resource
Definition: switch_limit.c:197
switch_memory_pool_t * pool
switch_status_t(* interval_reset)(const char *realm, const char *resource)
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
char * switch_limit_status(const char *backend)
retrieve arbitrary status information
Definition: switch_limit.c:219
switch_status_t switch_limit_release(const char *backend, switch_core_session_t *session, const char *realm, const char *resource)
Release resource.
Definition: switch_limit.c:146
#define switch_event_create_subclass(_e, _eid, _sn)
Definition: switch_event.h:153
switch_status_t switch_limit_reset(const char *backend)
reset all usage counters
Definition: switch_limit.c:180
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.
Definition: switch_limit.c:109
#define zstr(x)
Definition: switch_utils.h:314
#define LIMIT_IGNORE_TRANSFER_VARIABLE
Definition: switch_limit.h:120
_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 UNPROTECT_INTERFACE(_it)
static const char usage[]
Definition: switch.c:419
unsigned int switch_separate_string(_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen)
Separate a string into an array based on a character delimiter.
void switch_limit_fire_event(const char *backend, const char *realm, const char *key, uint32_t usage, uint32_t rate, uint32_t max, uint32_t ratemax)
fire event for limit usage
Definition: switch_limit.c:60
#define switch_channel_get_variable(_c, _v)
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.
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
void switch_limit_init(switch_memory_pool_t *pool)
Initilize the LIMIT Core System.
Definition: switch_limit.c:53
int(* usage)(const char *realm, const char *resource, uint32_t *rcount)
int switch_limit_usage(const char *backend, const char *realm, const char *resource, uint32_t *rcount)
get usage count for resource
Definition: switch_limit.c:163
switch_channel_state_t
Channel States (these are the defaults, CS_SOFT_EXECUTE, CS_EXCHANGE_MEDIA, and CS_CONSUME_MEDIA are ...
switch_status_t
Common return values.
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:287
#define switch_event_reserve_subclass(subclass_name)
Reserve a subclass assuming the owner string is the current filename.
Definition: switch_event.h:375
Module Interface Definitions.
Main Library Header.
#define SWITCH_DECLARE(type)
char * key
Definition: switch_msrp.c:64
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
struct fspr_pool_t switch_memory_pool_t
switch_limit_interface_t * switch_loadable_module_get_limit_interface(const char *name)
Retrieve the limit interface by it&#39;s registered name.
#define LIMIT_BACKEND_VARIABLE
Definition: switch_limit.h:121
#define switch_channel_set_variable(_channel, _var, _val)
switch_status_t(* incr)(switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)
static switch_status_t limit_state_handler(switch_core_session_t *session)
Definition: switch_limit.c:76