FreeSWITCH API Documentation  1.7.0
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  if (limit) {
51  UNPROTECT_INTERFACE(limit);
52  }
53 }
54 
57  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register event subclass \"%s\"", LIMIT_EVENT_USAGE);
58  }
59 }
60 
61 
62 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)
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 }
77 
79 {
83  const char *backendlist = switch_channel_get_variable(channel, LIMIT_BACKEND_VARIABLE);
84 
85  if (zstr(backendlist)) {
86  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Unset limit backendlist!\n");
87  return SWITCH_STATUS_SUCCESS;
88  }
89 
90  if (state >= CS_HANGUP || (state == CS_ROUTING && !switch_true(vval))) {
91  int argc = 0;
92  char *argv[6] = { 0 };
93  char *mydata = strdup(backendlist);
94  int x;
95 
96  argc = switch_separate_string(mydata, ',', argv, (sizeof(argv) / sizeof(argv[0])));
97  for (x = 0; x < argc; x++) {
98  switch_limit_release(argv[x], session, NULL, NULL);
99  }
100  switch_core_event_hook_remove_state_change(session, limit_state_handler);
101  /* Remove limit_backend variable so we register another hook if limit is called again */
103 
104  free(mydata);
105  }
106 
107  return SWITCH_STATUS_SUCCESS;
108 }
109 
110 
111 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) {
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 }
147 
148 SWITCH_DECLARE(switch_status_t) switch_limit_release(const char *backend, switch_core_session_t *session, const char *realm, const char *resource) {
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 }
164 
165 SWITCH_DECLARE(int) switch_limit_usage(const char *backend, const char *realm, const char *resource, uint32_t *rcount) {
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 }
181 
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 }
198 
199 SWITCH_DECLARE(switch_status_t) switch_limit_interval_reset(const char *backend, const char *realm, const char *resource) {
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 }
220 
221 SWITCH_DECLARE(char *) switch_limit_status(const char *backend) {
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 }
237 
238 /* For Emacs:
239  * Local Variables:
240  * mode:c
241  * indent-tabs-mode:t
242  * tab-width:4
243  * c-basic-offset:4
244  * End:
245  * For VIM:
246  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
247  */
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:412
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.
#define SWITCH_CHANNEL_LOG
#define LIMIT_EVENT_USAGE
Definition: switch_limit.h:122
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:199
switch_memory_pool_t * pool
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
switch_status_t(* incr)(switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)
switch_status_t(* interval_reset)(const char *realm, const char *resource)
switch_status_t(* reset)(void)
char * switch_limit_status(const char *backend)
retrieve arbitrary status information
Definition: switch_limit.c:221
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:148
#define switch_event_create_subclass(_e, _eid, _sn)
Definition: switch_event.h:153
static int switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:450
switch_status_t switch_limit_reset(const char *backend)
reset all usage counters
Definition: switch_limit.c:182
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:111
#define zstr(x)
Definition: switch_utils.h:281
#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:409
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:62
#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.
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_limit_init(switch_memory_pool_t *pool)
Initilize the LIMIT Core System.
Definition: switch_limit.c:55
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:165
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:256
#define switch_event_reserve_subclass(subclass_name)
Reserve a subclass assuming the owner string is the current filename.
Definition: switch_event.h:374
Module Interface Definitions.
Main Library Header.
#define SWITCH_DECLARE(type)
struct apr_pool_t switch_memory_pool_t
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_limit_interface_t * switch_loadable_module_get_limit_interface(const char *name)
Retrieve the limit interface by it's registered name.
#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