rtpengine_db.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * rtpproxy module
  3. *
  4. * Copyright (c) 2013 Crocodile RCS Ltd
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include "../../lib/srdb1/db.h"
  24. #include "../../lib/srdb1/db_res.h"
  25. #include "rtpengine.h"
  26. #define RTPP_TABLE_VERSION 1
  27. static db_func_t rtpp_dbf;
  28. static db1_con_t *rtpp_db_handle = NULL;
  29. str rtpp_db_url = {NULL, 0};
  30. str rtpp_table_name = str_init("rtpproxy");
  31. str rtpp_set_id_col = str_init("set_id");
  32. str rtpp_url_col = str_init("url");
  33. static int rtpp_connect_db(void)
  34. {
  35. if ((rtpp_db_url.s == NULL) || (rtpp_db_url.len == 0))
  36. return -1;
  37. if ((rtpp_db_handle = rtpp_dbf.init(&rtpp_db_url)) == NULL)
  38. {
  39. LM_ERR("Cannot initialize db connection\n");
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. static void rtpp_disconnect_db(void)
  45. {
  46. if (rtpp_db_handle)
  47. {
  48. rtpp_dbf.close(rtpp_db_handle);
  49. rtpp_db_handle = NULL;
  50. }
  51. }
  52. static int rtpp_load_db(void)
  53. {
  54. int i;
  55. struct rtpp_set *rtpp_list = NULL;
  56. db1_res_t *res = NULL;
  57. db_val_t *values = NULL;
  58. db_row_t *rows = NULL;
  59. db_key_t query_cols[] = {&rtpp_set_id_col, &rtpp_url_col};
  60. str url;
  61. int set_id;
  62. /* int weight, flags; */
  63. int n_rows = 0;
  64. int n_cols = 2;
  65. if (rtpp_db_handle == NULL)
  66. {
  67. LM_ERR("invalid db handle\n");
  68. return -1;
  69. }
  70. if (rtpp_dbf.use_table(rtpp_db_handle, &rtpp_table_name) < 0)
  71. {
  72. LM_ERR("unable to use table '%.*s'\n", rtpp_table_name.len, rtpp_table_name.s);
  73. return -1;
  74. }
  75. if (rtpp_dbf.query(rtpp_db_handle, 0, 0, 0, query_cols, 0, n_cols, 0, &res) < 0)
  76. {
  77. LM_ERR("error while running db query\n");
  78. return -1;
  79. }
  80. n_rows = RES_ROW_N(res);
  81. rows = RES_ROWS(res);
  82. if (n_rows == 0)
  83. {
  84. LM_WARN("No rtpproxy instances in database\n");
  85. return 0;
  86. }
  87. for (i=0; i<n_rows; i++)
  88. {
  89. values = ROW_VALUES(rows + i);
  90. set_id = VAL_INT(values);
  91. url.s = VAL_STR(values+1).s;
  92. url.len = strlen(url.s);
  93. /*
  94. weight = VAL_INT(values+2);
  95. flags = VAL_INT(values+3);
  96. */
  97. if ((rtpp_list = get_rtpp_set(set_id)) == NULL)
  98. {
  99. LM_ERR("error getting rtpp_list for set %d\n", set_id);
  100. continue;
  101. }
  102. if (add_rtpengine_socks(rtpp_list, url.s) != 0)
  103. {
  104. LM_ERR("error inserting '%.*s' into set %d\n", url.len, url.s, set_id);
  105. }
  106. }
  107. rtpp_dbf.free_result(rtpp_db_handle, res);
  108. return 0;
  109. }
  110. int init_rtpproxy_db(void)
  111. {
  112. int ret;
  113. int rtpp_table_version;
  114. if (rtpp_db_url.s == NULL)
  115. /* Database not configured */
  116. return 0;
  117. if (db_bind_mod(&rtpp_db_url, &rtpp_dbf) < 0)
  118. {
  119. LM_ERR("Unable to bind to db driver - %.*s\n", rtpp_db_url.len, rtpp_db_url.s);
  120. return -1;
  121. }
  122. if (rtpp_connect_db() != 0)
  123. {
  124. LM_ERR("Unable to connect to db\n");
  125. return -1;
  126. }
  127. rtpp_table_version = db_table_version(&rtpp_dbf, rtpp_db_handle, &rtpp_table_name);
  128. if (rtpp_table_version < 0)
  129. {
  130. LM_ERR("failed to get rtpp table version\n");
  131. ret = -1;
  132. goto done;
  133. }
  134. switch (rtpp_table_version) {
  135. case RTPP_TABLE_VERSION:
  136. break;
  137. default:
  138. LM_ERR("invalid table version (found %d, require %d)\n",
  139. rtpp_table_version, RTPP_TABLE_VERSION);
  140. ret = -1;
  141. goto done;
  142. }
  143. ret = rtpp_load_db();
  144. done:
  145. rtpp_disconnect_db();
  146. return ret;
  147. }