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
get_backend(const char * backend)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
release_backend(switch_limit_interface_t * limit)49 static void release_backend(switch_limit_interface_t *limit) {
50 UNPROTECT_INTERFACE(limit);
51 }
52
switch_limit_init(switch_memory_pool_t * pool)53 SWITCH_DECLARE(void) switch_limit_init(switch_memory_pool_t *pool) {
54 if (switch_event_reserve_subclass(LIMIT_EVENT_USAGE) != SWITCH_STATUS_SUCCESS) {
55 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register event subclass \"%s\"", LIMIT_EVENT_USAGE);
56 }
57 }
58
59
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)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
64 if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, LIMIT_EVENT_USAGE) == SWITCH_STATUS_SUCCESS) {
65 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "backend", backend);
66 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "realm", realm);
67 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "key", key);
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
limit_state_handler(switch_core_session_t * session)76 static switch_status_t limit_state_handler(switch_core_session_t *session)
77 {
78 switch_channel_t *channel = switch_core_session_get_channel(session);
79 switch_channel_state_t state = switch_channel_get_state(channel);
80 const char *vval = switch_channel_get_variable(channel, LIMIT_IGNORE_TRANSFER_VARIABLE);
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 */
100 switch_channel_set_variable(channel, LIMIT_BACKEND_VARIABLE, NULL);
101
102 free(mydata);
103 }
104
105 return SWITCH_STATUS_SUCCESS;
106 }
107
108
switch_limit_incr(const char * backend,switch_core_session_t * session,const char * realm,const char * resource,const int max,const int interval)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);
121 switch_goto_status(SWITCH_STATUS_GENERR, end);
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 {
135 switch_channel_set_variable(channel, LIMIT_BACKEND_VARIABLE, backend);
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
switch_limit_release(const char * backend,switch_core_session_t * session,const char * realm,const char * resource)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);
153 switch_goto_status(SWITCH_STATUS_GENERR, end);
154 }
155
156 status = limit->release(session, realm, resource);
157
158 end:
159 release_backend(limit);
160 return status;
161 }
162
switch_limit_usage(const char * backend,const char * realm,const char * resource,uint32_t * rcount)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
switch_limit_reset(const char * backend)180 SWITCH_DECLARE(switch_status_t) switch_limit_reset(const char *backend) {
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);
187 switch_goto_status(SWITCH_STATUS_GENERR, end);
188 }
189
190 status = limit->reset();
191
192 end:
193 release_backend(limit);
194 return status;
195 }
196
switch_limit_interval_reset(const char * backend,const char * realm,const char * resource)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);
204 switch_goto_status(SWITCH_STATUS_GENERR, end);
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);
209 switch_goto_status(SWITCH_STATUS_GENERR, end);
210 }
211
212 status = limit->interval_reset(realm, resource);
213
214 end:
215 release_backend(limit);
216 return status;
217 }
218
switch_limit_status(const char * backend)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 */
246