db_drv.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG FOKUS
  5. * Copyright (C) 2006-2007 iptelorg GmbH
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. /** \ingroup DB_API
  29. * @{
  30. */
  31. #include "db_drv.h"
  32. #include "db_gen.h"
  33. #include "../../mem/mem.h"
  34. #include "../../sr_module.h"
  35. #include "../../ut.h"
  36. #include <string.h>
  37. #include <stdlib.h>
  38. int db_drv_init(db_drv_t* ptr, void* free_func)
  39. {
  40. ptr->free = free_func;
  41. return 0;
  42. }
  43. void db_drv_free(db_drv_t* ptr)
  44. {
  45. ptr->free = NULL;
  46. }
  47. /*
  48. * Find function with given name in module named <module>
  49. * RETURNS: -1 on error
  50. * 0 if a function has been found
  51. * 1 if no function has been found
  52. */
  53. int db_drv_func(db_drv_func_t* func, str* module, char* func_name)
  54. {
  55. static str prefix = STR_STATIC_INIT("db_");
  56. char* buf = NULL, *name;
  57. if ((buf = pkg_malloc(prefix.len + module->len + 1)) == NULL) {
  58. LOG(L_ERR, "db_drv_func: No memory left\n");
  59. goto error;
  60. }
  61. memcpy(buf, prefix.s, prefix.len);
  62. memcpy(buf + prefix.len, module->s, module->len);
  63. buf[prefix.len + module->len] = '\0';
  64. /* First try to find the module with prefix "db_" */
  65. name = buf;
  66. if (find_module_by_name(name) == 0) {
  67. /* Not found, so try without the prefix */
  68. name = buf + prefix.len;
  69. if (find_module_by_name(name) == 0) {
  70. ERR("db_drv_func: database driver for '%.*s' not found\n", STR_FMT(module));
  71. goto error;
  72. }
  73. }
  74. *func = (db_drv_func_t)find_mod_export(name, func_name, 0, 0);
  75. if (buf) pkg_free(buf);
  76. if (*func) return 0;
  77. else return 1;
  78. error:
  79. if (buf) pkg_free(buf);
  80. return -1;
  81. }
  82. /*
  83. * Call function with name <func_name> in DB driver <module>, give
  84. * it pointer <db_struct> as the pointer to the corresponding DB structure
  85. * (type of the structure is function-specific) and <offset> is the offset
  86. * of the driver/connection within the context (used to make DB_DRV_ATTACH
  87. * and DB_DRV_DATA macros work)
  88. */
  89. int db_drv_call(str* module, char* func_name, void* db_struct, int offset)
  90. {
  91. db_drv_func_t func;
  92. int ret;
  93. ret = db_drv_func(&func, module, func_name);
  94. if (ret < 0) {
  95. ERR("db: db_drv_call failed\n");
  96. return -1;
  97. }
  98. if (ret == 0) {
  99. db_payload_idx = offset;
  100. return func(db_struct);
  101. } else {
  102. DBG("db_drv_call: DB driver for %.*s does not export function %s\n",
  103. module->len, ZSW(module->s), func_name);
  104. return 1;
  105. }
  106. }
  107. /** @} */