2
0

alias_db.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * $Id$
  3. *
  4. * ALIAS_DB Module
  5. *
  6. * Copyright (C) 2004 Voice Sistem SRL
  7. *
  8. * This file is part of a module for Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2004-09-01: first version (ramona)
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include "../../sr_module.h"
  31. #include "../../lib/srdb1/db.h"
  32. #include "../../dprint.h"
  33. #include "../../error.h"
  34. #include "../../mem/mem.h"
  35. #include "../../ut.h"
  36. #include "../../mod_fix.h"
  37. #include "alookup.h"
  38. #include "api.h"
  39. MODULE_VERSION
  40. /* Module destroy function prototype */
  41. static void destroy(void);
  42. /* Module child-init function prototype */
  43. static int child_init(int rank);
  44. /* Module initialization function prototype */
  45. static int mod_init(void);
  46. static int w_alias_db_lookup(struct sip_msg* _msg, char* _table, char* _str2);
  47. /* Module parameter variables */
  48. static str db_url = str_init(DEFAULT_RODB_URL);
  49. str user_column = str_init("username");
  50. str domain_column = str_init("domain");
  51. str alias_user_column = str_init("alias_username");
  52. str alias_domain_column = str_init("alias_domain");
  53. str domain_prefix = {NULL, 0};
  54. int use_domain = 0;
  55. int ald_append_branches = 0;
  56. db1_con_t* db_handle; /* Database connection handle */
  57. db_func_t adbf; /* DB functions */
  58. /* Exported functions */
  59. static cmd_export_t cmds[] = {
  60. {"alias_db_lookup", (cmd_function)w_alias_db_lookup, 1, fixup_spve_null, 0,
  61. REQUEST_ROUTE|FAILURE_ROUTE},
  62. {"bind_alias_db", (cmd_function)bind_alias_db, 1, 0, 0,
  63. 0},
  64. {0, 0, 0, 0, 0, 0}
  65. };
  66. /* Exported parameters */
  67. static param_export_t params[] = {
  68. {"db_url", PARAM_STR, &db_url },
  69. {"user_column", PARAM_STR, &user_column },
  70. {"domain_column", PARAM_STR, &domain_column },
  71. {"alias_user_column", PARAM_STR, &alias_user_column },
  72. {"alias_domain_column", PARAM_STR, &alias_domain_column },
  73. {"use_domain", INT_PARAM, &use_domain },
  74. {"domain_prefix", PARAM_STR, &domain_prefix },
  75. {"append_branches", INT_PARAM, &ald_append_branches },
  76. {0, 0, 0}
  77. };
  78. /* Module interface */
  79. struct module_exports exports = {
  80. "alias_db",
  81. DEFAULT_DLFLAGS, /* dlopen flags */
  82. cmds, /* Exported functions */
  83. params, /* Exported parameters */
  84. 0, /* exported statistics */
  85. 0, /* exported MI functions */
  86. 0, /* exported pseudo-variables */
  87. 0, /* extra processes */
  88. mod_init, /* module initialization function */
  89. 0, /* response function */
  90. destroy, /* destroy function */
  91. child_init /* child initialization function */
  92. };
  93. /**
  94. *
  95. */
  96. static int child_init(int rank)
  97. {
  98. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  99. return 0; /* do nothing for the main process */
  100. db_handle = adbf.init(&db_url);
  101. if (!db_handle)
  102. {
  103. LM_ERR("unable to connect database\n");
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. /**
  109. *
  110. */
  111. static int mod_init(void)
  112. {
  113. /* Find a database module */
  114. if (db_bind_mod(&db_url, &adbf))
  115. {
  116. LM_ERR("unable to bind database module\n");
  117. return -1;
  118. }
  119. if (!DB_CAPABILITY(adbf, DB_CAP_QUERY))
  120. {
  121. LM_CRIT("database modules does not "
  122. "provide all functions needed by avpops module\n");
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. /**
  128. *
  129. */
  130. static void destroy(void)
  131. {
  132. if (db_handle) {
  133. adbf.close(db_handle);
  134. db_handle = 0;
  135. }
  136. }
  137. static int w_alias_db_lookup(struct sip_msg* _msg, char* _table, char* _str2)
  138. {
  139. str table_s;
  140. if(_table==NULL || fixup_get_svalue(_msg, (gparam_p)_table, &table_s)!=0)
  141. {
  142. LM_ERR("invalid table parameter\n");
  143. return -1;
  144. }
  145. return alias_db_lookup(_msg, table_s);
  146. }
  147. int bind_alias_db(struct alias_db_binds *pxb)
  148. {
  149. if (pxb == NULL)
  150. {
  151. LM_WARN("bind_alias_db: Cannot load alias_db API into a NULL pointer\n");
  152. return -1;
  153. }
  154. pxb->alias_db_lookup = alias_db_lookup;
  155. return 0;
  156. }