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 * Anthony Minessale II <anthm@freeswitch.org>
27 * Ken Rice <krice@freeswitch.org>
28 * Mathieu Rene <mathieu.rene@gmail.com>
29 * Bret McDanel <trixter AT 0xdecafbad.com>
30 * Rupa Schomaker <rupa@rupa.com>
31 * Emmanuel Schmidbauer <eschmidbauer@gmail.com>
32 *
33 * mod_db.c -- Implements simple db API, group support, and limit db backend
34 *
35 */
36
37 #include <switch.h>
38
39 SWITCH_MODULE_LOAD_FUNCTION(mod_db_load);
40 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_db_shutdown);
41 SWITCH_MODULE_DEFINITION(mod_db, mod_db_load, mod_db_shutdown, NULL);
42
43 static struct {
44 switch_memory_pool_t *pool;
45 char hostname[256];
46 char *dbname;
47 char *odbc_dsn;
48 switch_mutex_t *mutex;
49 switch_mutex_t *db_hash_mutex;
50 switch_hash_t *db_hash;
51 } globals;
52
53 typedef struct {
54 uint32_t total_usage;
55 uint32_t rate_usage;
56 time_t last_check;
57 } limit_hash_item_t;
58
59 struct callback {
60 char *buf;
61 size_t len;
62 int matches;
63 };
64
65 typedef struct callback callback_t;
66
sql2str_callback(void * pArg,int argc,char ** argv,char ** columnNames)67 static int sql2str_callback(void *pArg, int argc, char **argv, char **columnNames)
68 {
69 callback_t *cbt = (callback_t *) pArg;
70
71 switch_copy_string(cbt->buf, argv[0], cbt->len);
72 cbt->matches++;
73 return 0;
74 }
75
76 static char limit_sql[] =
77 "CREATE TABLE limit_data (\n"
78 " hostname VARCHAR(255),\n" " realm VARCHAR(255),\n" " id VARCHAR(255),\n" " uuid VARCHAR(255)\n" ");\n";
79
80 static char db_sql[] =
81 "CREATE TABLE db_data (\n"
82 " hostname VARCHAR(255),\n" " realm VARCHAR(255),\n" " data_key VARCHAR(255),\n" " data VARCHAR(255)\n" ");\n";
83
84 static char group_sql[] =
85 "CREATE TABLE group_data (\n" " hostname VARCHAR(255),\n" " groupname VARCHAR(255),\n" " url VARCHAR(255)\n" ");\n";
86
87
limit_get_db_handle(void)88 switch_cache_db_handle_t *limit_get_db_handle(void)
89 {
90 switch_cache_db_handle_t *dbh = NULL;
91 char *dsn;
92
93 if (!zstr(globals.odbc_dsn)) {
94 dsn = globals.odbc_dsn;
95 } else {
96 dsn = globals.dbname;
97 }
98
99 if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
100 dbh = NULL;
101 }
102
103 return dbh;
104
105 }
106
107
limit_execute_sql(char * sql)108 static switch_status_t limit_execute_sql(char *sql)
109 {
110 switch_cache_db_handle_t *dbh = NULL;
111 switch_status_t status = SWITCH_STATUS_FALSE;
112
113 if (!(dbh = limit_get_db_handle())) {
114 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
115 goto end;
116 }
117
118 status = switch_cache_db_execute_sql(dbh, sql, NULL);
119
120 end:
121
122 switch_cache_db_release_db_handle(&dbh);
123
124 return status;
125 }
126
127
limit_execute_sql_callback(char * sql,switch_core_db_callback_func_t callback,void * pdata)128 static switch_bool_t limit_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, void *pdata)
129 {
130 switch_bool_t ret = SWITCH_FALSE;
131 char *errmsg = NULL;
132 switch_cache_db_handle_t *dbh = NULL;
133
134 if (!(dbh = limit_get_db_handle())) {
135 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
136 goto end;
137 }
138
139 switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, &errmsg);
140
141 if (errmsg) {
142 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
143 free(errmsg);
144 }
145
146 end:
147
148 switch_cache_db_release_db_handle(&dbh);
149
150 return ret;
151 }
152
limit_execute_sql2str(char * sql,char * str,size_t len)153 static char * limit_execute_sql2str(char *sql, char *str, size_t len)
154 {
155 callback_t cbt = { 0 };
156
157 cbt.buf = str;
158 cbt.len = len;
159
160 limit_execute_sql_callback(sql, sql2str_callback, &cbt);
161
162 return cbt.buf;
163 }
164
165 /* \brief Enforces limit restrictions
166 * \param session current session
167 * \param realm limit realm
168 * \param id limit id
169 * \param max maximum count
170 * \return SWITCH_STATUS_SUCCESS if the access is allowed
171 */
SWITCH_LIMIT_INCR(limit_incr_db)172 SWITCH_LIMIT_INCR(limit_incr_db)
173 {
174 switch_channel_t *channel = switch_core_session_get_channel(session);
175 int got = 0;
176 char *sql = NULL;
177 char gotstr[128];
178 switch_status_t status = SWITCH_STATUS_SUCCESS;
179
180 switch_mutex_lock(globals.mutex);
181
182 switch_channel_set_variable(channel, "limit_realm", realm);
183 switch_channel_set_variable(channel, "limit_id", resource);
184 switch_channel_set_variable(channel, "limit_max", switch_core_session_sprintf(session, "%d", max));
185
186 sql = switch_mprintf("select count(hostname) from limit_data where realm='%q' and id='%q';", realm, resource);
187 limit_execute_sql2str(sql, gotstr, 128);
188 switch_safe_free(sql);
189 got = atoi(gotstr);
190
191 if (max < 0) {
192 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Usage for %s_%s is now %d\n", realm, resource, got + 1);
193 } else {
194 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Usage for %s_%s is now %d/%d\n", realm, resource, got + 1, max);
195 }
196
197 if (max >= 0 && got + 1 > max) {
198 status = SWITCH_STATUS_GENERR;
199 goto done;
200 }
201
202 sql =
203 switch_mprintf("insert into limit_data (hostname, realm, id, uuid) values('%q','%q','%q','%q');", globals.hostname, realm, resource,
204 switch_core_session_get_uuid(session));
205 limit_execute_sql(sql);
206 switch_safe_free(sql);
207
208 {
209 const char *susage = switch_core_session_sprintf(session, "%d", ++got);
210
211 switch_channel_set_variable(channel, "limit_usage", susage);
212 switch_channel_set_variable(channel, switch_core_session_sprintf(session, "limit_usage_%s_%s", realm, resource), susage);
213 }
214 switch_limit_fire_event("db", realm, resource, got, 0, max, 0);
215
216 done:
217 switch_mutex_unlock(globals.mutex);
218 return status;
219 }
220
SWITCH_LIMIT_RELEASE(limit_release_db)221 SWITCH_LIMIT_RELEASE(limit_release_db)
222 {
223 char *sql = NULL;
224
225 if (realm == NULL && resource == NULL) {
226 sql = switch_mprintf("delete from limit_data where uuid='%q'", switch_core_session_get_uuid(session));
227 } else {
228 sql = switch_mprintf("delete from limit_data where uuid='%q' and realm='%q' and id = '%q'", switch_core_session_get_uuid(session), realm, resource);
229 }
230 limit_execute_sql(sql);
231 switch_safe_free(sql);
232
233 return SWITCH_STATUS_SUCCESS;
234 }
235
SWITCH_LIMIT_USAGE(limit_usage_db)236 SWITCH_LIMIT_USAGE(limit_usage_db)
237 {
238 char usagestr[128] = "";
239 int usage = 0;
240 char *sql = NULL;
241
242 sql = switch_mprintf("select count(hostname) from limit_data where realm='%q' and id='%q'", realm, resource);
243 limit_execute_sql2str(sql, usagestr, sizeof(usagestr));
244 switch_safe_free(sql);
245 usage = atoi(usagestr);
246
247 return usage;
248 }
249
SWITCH_LIMIT_RESET(limit_reset_db)250 SWITCH_LIMIT_RESET(limit_reset_db)
251 {
252 char *sql = NULL;
253 sql = switch_mprintf("delete from limit_data where hostname='%q';", globals.hostname);
254 limit_execute_sql(sql);
255 switch_safe_free(sql);
256
257 return SWITCH_STATUS_SUCCESS;
258 }
259
SWITCH_LIMIT_STATUS(limit_status_db)260 SWITCH_LIMIT_STATUS(limit_status_db)
261 {
262 char count[128] = "";
263 char *ret = NULL;
264 char *sql = NULL;
265
266 sql = switch_mprintf("select count(hostname) from limit_data where hostname='%q'", globals.hostname);
267 limit_execute_sql2str(sql, count, sizeof(count));
268 switch_safe_free(sql);
269 ret = switch_mprintf("Tracking %s resources for hostname %s.", count, globals.hostname);
270 return ret;
271 }
272
273 /* INIT / Config */
274
275 static switch_xml_config_string_options_t limit_config_dsn = { NULL, 0, "^pgsql|^odbc|^sqlite|[^:]+:[^:]*:.*" };
276
277 static switch_xml_config_item_t config_settings[] = {
278 SWITCH_CONFIG_ITEM("odbc-dsn", SWITCH_CONFIG_STRING, 0, &globals.odbc_dsn, NULL, &limit_config_dsn,
279 "dsn:username:password", "If set, the ODBC DSN used by the limit and db applications"),
280 SWITCH_CONFIG_ITEM_END()
281 };
282
do_config()283 static switch_status_t do_config()
284 {
285 switch_cache_db_handle_t *dbh = NULL;
286 switch_status_t status = SWITCH_STATUS_SUCCESS;
287 char *sql = NULL;
288
289 limit_config_dsn.pool = globals.pool;
290
291 if (switch_xml_config_parse_module_settings("db.conf", SWITCH_FALSE, config_settings) != SWITCH_STATUS_SUCCESS) {
292 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "No config file found, defaulting to sqlite\n");
293 }
294
295 if (globals.odbc_dsn) {
296 if (!(dbh = limit_get_db_handle())) {
297 globals.odbc_dsn = NULL;
298 }
299 }
300
301
302 if (zstr(globals.odbc_dsn)) {
303 globals.dbname = "call_limit";
304 dbh = limit_get_db_handle();
305 }
306
307
308 if (dbh) {
309 int x = 0;
310 char *indexes[] = {
311 "create index ld_hostname on limit_data (hostname)",
312 "create index ld_uuid on limit_data (uuid)",
313 "create index ld_realm on limit_data (realm)",
314 "create index ld_id on limit_data (id)",
315 "create index dd_realm on db_data (realm)",
316 "create unique index dd_data_key_realm on db_data (data_key,realm)",
317 "create index gd_groupname on group_data (groupname)",
318 "create index gd_url on group_data (url)",
319 NULL
320 };
321
322
323
324 switch_cache_db_test_reactive(dbh, "select * from limit_data", NULL, limit_sql);
325 switch_cache_db_test_reactive(dbh, "select * from db_data", NULL, db_sql);
326 switch_cache_db_test_reactive(dbh, "select * from group_data", NULL, group_sql);
327
328 for (x = 0; indexes[x]; x++) {
329 switch_cache_db_create_schema(dbh, indexes[x], NULL);
330 }
331
332 switch_cache_db_release_db_handle(&dbh);
333
334 sql = switch_mprintf("delete from limit_data where hostname='%q';", globals.hostname);
335 limit_execute_sql(sql);
336 switch_safe_free(sql);
337 }
338
339 return status;
340 }
341
342 /* APP/API STUFF */
343
344 /* CORE DB STUFF */
345
346 static int group_callback(void *pArg, int argc, char **argv, char **columnNames);
347
SWITCH_STANDARD_API(db_api_function)348 SWITCH_STANDARD_API(db_api_function)
349 {
350 int argc = 0;
351 char *argv[4] = { 0 };
352 char *mydata = NULL;
353 char *sql;
354
355 if (!zstr(cmd)) {
356 mydata = strdup(cmd);
357 switch_assert(mydata);
358 argc = switch_separate_string(mydata, '/', argv, (sizeof(argv) / sizeof(argv[0])));
359 }
360
361 if (argc < 1 || !argv[0]) {
362 goto error;
363 }
364
365 if (!strcasecmp(argv[0], "insert")) {
366 if (argc < 4) {
367 goto error;
368 }
369 sql = switch_mprintf("delete from db_data where realm='%q' and data_key='%q'", argv[1], argv[2]);
370 switch_assert(sql);
371 limit_execute_sql(sql);
372 switch_safe_free(sql);
373 sql =
374 switch_mprintf("insert into db_data (hostname, realm, data_key, data) values('%q','%q','%q','%q');", globals.hostname, argv[1], argv[2],
375 argv[3]);
376 switch_assert(sql);
377 limit_execute_sql(sql);
378 switch_safe_free(sql);
379 stream->write_function(stream, "+OK");
380 goto done;
381 } else if (!strcasecmp(argv[0], "delete")) {
382 if (argc < 2) {
383 goto error;
384 }
385 sql = switch_mprintf("delete from db_data where realm='%q' and data_key='%q'", argv[1], argv[2]);
386 switch_assert(sql);
387 limit_execute_sql(sql);
388 switch_safe_free(sql);
389 stream->write_function(stream, "+OK");
390 goto done;
391 } else if (!strcasecmp(argv[0], "select")) {
392 char buf[256] = "";
393 if (argc < 3) {
394 goto error;
395 }
396 sql = switch_mprintf("select data from db_data where realm='%q' and data_key='%q'", argv[1], argv[2]);
397 limit_execute_sql2str(sql, buf, sizeof(buf));
398 switch_safe_free(sql);
399 stream->write_function(stream, "%s", buf);
400 goto done;
401 } else if (!strcasecmp(argv[0], "exists")) {
402 char buf[256] = "";
403 if (argc < 3) {
404 goto error;
405 }
406 sql = switch_mprintf("select data from db_data where realm='%q' and data_key='%q'", argv[1], argv[2]);
407 limit_execute_sql2str(sql, buf, sizeof(buf));
408 switch_safe_free(sql);
409 if ( !strcmp(buf, "") ) {
410 stream->write_function(stream, "false");
411 }
412 else {
413 stream->write_function(stream, "true");
414 }
415 goto done;
416 } else if (!strcasecmp(argv[0], "count")) {
417 char buf[256] = "";
418 if (argc < 2) {
419 sql = switch_mprintf("select count(distinct realm) from db_data");
420 } else if (argc < 3) {
421 sql = switch_mprintf("select count(data_key) from db_data where realm='%q'", argv[1]);
422 } else {
423 goto error;
424 }
425 limit_execute_sql2str(sql, buf, sizeof(buf));
426 switch_safe_free(sql);
427 stream->write_function(stream, "%s", buf);
428 goto done;
429 } else if (!strcasecmp(argv[0], "list")) {
430 char buf[4096] = "";
431 callback_t cbt = { 0 };
432 cbt.buf = buf;
433 cbt.len = sizeof(buf);
434
435 if (argc < 2) {
436 sql = switch_mprintf("select distinct realm,',' from db_data");
437 } else if (argc < 3) {
438 sql = switch_mprintf("select distinct data_key,',' from db_data where realm='%q'", argv[1]);
439 } else {
440 goto error;
441 }
442 switch_assert(sql);
443
444 limit_execute_sql_callback(sql, group_callback, &cbt);
445 switch_safe_free(sql);
446
447 if (!zstr(buf)) {
448 *(buf + (strlen(buf) - 1)) = '\0';
449 }
450
451 stream->write_function(stream, "%s", buf);
452
453 goto done;
454 }
455
456 error:
457 stream->write_function(stream, "!err!");
458
459 done:
460
461 switch_safe_free(mydata);
462 return SWITCH_STATUS_SUCCESS;
463 }
464
465 #define DB_USAGE "[insert|delete]/<realm>/<key>/<val>"
466 #define DB_DESC "save data"
467
SWITCH_STANDARD_APP(db_function)468 SWITCH_STANDARD_APP(db_function)
469 {
470 int argc = 0;
471 char *argv[4] = { 0 };
472 char *mydata = NULL;
473 char *sql = NULL;
474
475 if (!zstr(data)) {
476 mydata = switch_core_session_strdup(session, data);
477 argc = switch_separate_string(mydata, '/', argv, (sizeof(argv) / sizeof(argv[0])));
478 }
479
480 if (argc < 3 || !argv[0]) {
481 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "USAGE: db %s\n", DB_USAGE);
482 return;
483 }
484
485 if (!strcasecmp(argv[0], "insert")) {
486 if (argc < 4) {
487 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "USAGE: db %s\n", DB_USAGE);
488 return;
489 }
490 sql = switch_mprintf("delete from db_data where realm='%q' and data_key='%q'", argv[1], argv[2]);
491 switch_assert(sql);
492 limit_execute_sql(sql);
493 switch_safe_free(sql);
494
495 sql =
496 switch_mprintf("insert into db_data (hostname, realm, data_key, data) values('%q','%q','%q','%q');", globals.hostname, argv[1], argv[2],
497 argv[3]);
498 } else if (!strcasecmp(argv[0], "delete")) {
499 sql = switch_mprintf("delete from db_data where realm='%q' and data_key='%q'", argv[1], argv[2]);
500 } else {
501 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "USAGE: db %s\n", DB_USAGE);
502 return;
503 }
504
505 if (sql) {
506 limit_execute_sql(sql);
507 switch_safe_free(sql);
508 }
509 }
510
511 /* GROUP STUFF */
512
group_callback(void * pArg,int argc,char ** argv,char ** columnNames)513 static int group_callback(void *pArg, int argc, char **argv, char **columnNames)
514 {
515 callback_t *cbt = (callback_t *) pArg;
516 switch_snprintf(cbt->buf + strlen(cbt->buf), cbt->len - strlen(cbt->buf), "%s%s", argv[0], argv[1]);
517 cbt->matches++;
518 return 0;
519 }
520
SWITCH_STANDARD_API(group_api_function)521 SWITCH_STANDARD_API(group_api_function)
522 {
523 int argc = 0;
524 char *argv[4] = { 0 };
525 char *mydata = NULL;
526 char *sql;
527
528 if (!zstr(cmd)) {
529 mydata = strdup(cmd);
530 argc = switch_separate_string(mydata, ':', argv, (sizeof(argv) / sizeof(argv[0])));
531 }
532
533 if (argc < 2 || !argv[0]) {
534 goto error;
535 }
536
537 if (!strcasecmp(argv[0], "insert")) {
538 if (argc < 3) {
539 goto error;
540 }
541 sql = switch_mprintf("delete from group_data where groupname='%q' and url='%q';", argv[1], argv[2]);
542 switch_assert(sql);
543
544 limit_execute_sql(sql);
545 switch_safe_free(sql);
546 sql = switch_mprintf("insert into group_data (hostname, groupname, url) values('%q','%q','%q');", globals.hostname, argv[1], argv[2]);
547 switch_assert(sql);
548 limit_execute_sql(sql);
549 switch_safe_free(sql);
550 stream->write_function(stream, "+OK");
551 goto done;
552 } else if (!strcasecmp(argv[0], "delete")) {
553 if (argc < 3) {
554 goto error;
555 }
556 if (!strcmp(argv[2], "*")) {
557 sql = switch_mprintf("delete from group_data where groupname='%q';", argv[1]);
558 } else {
559 sql = switch_mprintf("delete from group_data where groupname='%q' and url='%q';", argv[1], argv[2]);
560 }
561 switch_assert(sql);
562 limit_execute_sql(sql);
563 switch_safe_free(sql);
564 stream->write_function(stream, "+OK");
565 goto done;
566 } else if (!strcasecmp(argv[0], "call")) {
567 char buf[4096] = "";
568 char *how = ",";
569 callback_t cbt = { 0 };
570 cbt.buf = buf;
571 cbt.len = sizeof(buf);
572
573 if (argc > 2) {
574 if (!strcasecmp(argv[2], "order")) {
575 how = "|";
576 }
577 if (!strcasecmp(argv[2], "multi")) {
578 how = ":_:";
579 }
580 }
581
582 sql = switch_mprintf("select url,'%q' from group_data where groupname='%q'", how, argv[1]);
583 switch_assert(sql);
584
585 limit_execute_sql_callback(sql, group_callback, &cbt);
586 switch_safe_free(sql);
587
588 if (!zstr(buf)) {
589 *(buf + (strlen(buf) - strlen(how))) = '\0';
590 }
591
592 stream->write_function(stream, "%s", buf);
593
594 goto done;
595 }
596
597 error:
598 stream->write_function(stream, "!err!");
599
600 done:
601
602 switch_safe_free(mydata);
603 return SWITCH_STATUS_SUCCESS;
604 }
605
606 #define GROUP_USAGE "[insert|delete]:<group name>:<val>"
607 #define GROUP_DESC "save data"
608
SWITCH_STANDARD_APP(group_function)609 SWITCH_STANDARD_APP(group_function)
610 {
611 int argc = 0;
612 char *argv[3] = { 0 };
613 char *mydata = NULL;
614 char *sql;
615
616 if (!zstr(data)) {
617 mydata = switch_core_session_strdup(session, data);
618 argc = switch_separate_string(mydata, ':', argv, (sizeof(argv) / sizeof(argv[0])));
619 }
620
621 if (argc < 3 || !argv[0]) {
622 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "USAGE: group %s\n", DB_USAGE);
623 return;
624 }
625
626 if (!strcasecmp(argv[0], "insert")) {
627 sql = switch_mprintf("insert into group_data (hostname, groupname, url) values('%q','%q','%q');", globals.hostname, argv[1], argv[2]);
628 switch_assert(sql);
629 limit_execute_sql(sql);
630 switch_safe_free(sql);
631 } else if (!strcasecmp(argv[0], "delete")) {
632 sql = switch_mprintf("delete from group_data where groupname='%q' and url='%q';", argv[1], argv[2]);
633 switch_assert(sql);
634 limit_execute_sql(sql);
635 switch_safe_free(sql);
636 }
637 }
638
639 /* INIT/DEINIT STUFF */
640
SWITCH_MODULE_LOAD_FUNCTION(mod_db_load)641 SWITCH_MODULE_LOAD_FUNCTION(mod_db_load)
642 {
643 switch_status_t status;
644 switch_application_interface_t *app_interface;
645 switch_api_interface_t *commands_api_interface;
646 switch_limit_interface_t *limit_interface;
647
648 memset(&globals, 0, sizeof(globals));
649 strncpy(globals.hostname, switch_core_get_switchname(), sizeof(globals.hostname) -1 );
650 globals.pool = pool;
651
652
653 if ((status = do_config() != SWITCH_STATUS_SUCCESS)) {
654 return status;
655 }
656
657 switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, globals.pool);
658 switch_mutex_init(&globals.db_hash_mutex, SWITCH_MUTEX_NESTED, globals.pool);
659 switch_core_hash_init(&globals.db_hash);
660
661 status = switch_event_reserve_subclass(LIMIT_EVENT_USAGE);
662 if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_INUSE) {
663 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register event subclass \"%s\" (%d)\n", LIMIT_EVENT_USAGE, status);
664 return SWITCH_STATUS_FALSE;
665 }
666
667 /* connect my internal structure to the blank pointer passed to me */
668 *module_interface = switch_loadable_module_create_module_interface(pool, modname);
669
670 /* register limit interfaces */
671 SWITCH_ADD_LIMIT(limit_interface, "db", limit_incr_db, limit_release_db, limit_usage_db, limit_reset_db, limit_status_db, NULL);
672
673 SWITCH_ADD_APP(app_interface, "db", "Insert to the db", DB_DESC, db_function, DB_USAGE, SAF_SUPPORT_NOMEDIA | SAF_ZOMBIE_EXEC);
674 SWITCH_ADD_APP(app_interface, "group", "Manage a group", GROUP_DESC, group_function, GROUP_USAGE, SAF_SUPPORT_NOMEDIA | SAF_ZOMBIE_EXEC);
675 SWITCH_ADD_API(commands_api_interface, "db", "db get/set", db_api_function, "[insert|delete|select|exists|count|list]/<realm>/<key>/<value>");
676 switch_console_set_complete("add db insert");
677 switch_console_set_complete("add db delete");
678 switch_console_set_complete("add db select");
679 switch_console_set_complete("add db exists");
680 switch_console_set_complete("add db count");
681 switch_console_set_complete("add db list");
682 SWITCH_ADD_API(commands_api_interface, "group", "group [insert|delete|call]", group_api_function, "[insert|delete|call]:<group name>:<url>");
683 switch_console_set_complete("add group insert");
684 switch_console_set_complete("add group delete");
685 switch_console_set_complete("add group call");
686
687 /* indicate that the module should continue to be loaded */
688 return SWITCH_STATUS_SUCCESS;
689
690 }
691
692
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_db_shutdown)693 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_db_shutdown)
694 {
695
696 switch_xml_config_cleanup(config_settings);
697
698 switch_mutex_destroy(globals.mutex);
699 switch_mutex_destroy(globals.db_hash_mutex);
700
701 switch_core_hash_destroy(&globals.db_hash);
702
703 return SWITCH_STATUS_SUCCESS;
704 }
705
706 /* For Emacs:
707 * Local Variables:
708 * mode:c
709 * indent-tabs-mode:t
710 * tab-width:4
711 * c-basic-offset:4
712 * End:
713 * For VIM:
714 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
715 */
716