km_my_con.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2001-2004 iptel.org
  3. * Copyright (C) 2008 1&1 Internet AG
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /*! \file
  22. * \brief DB_MYSQL :: Connections
  23. * \ingroup db_mysql
  24. * Module: \ref db_mysql
  25. */
  26. #include "km_my_con.h"
  27. #include "km_db_mysql.h"
  28. #include <mysql/mysql_version.h>
  29. #include "../../mem/mem.h"
  30. #include "../../dprint.h"
  31. #include "../../ut.h"
  32. #include "mysql_mod.h"
  33. /*! \brief
  34. * Create a new connection structure,
  35. * open the MySQL connection and set reference count to 1
  36. */
  37. struct my_con* db_mysql_new_connection(const struct db_id* id)
  38. {
  39. struct my_con* ptr;
  40. char *host, *grp, *egrp;
  41. unsigned int connection_flag = 0;
  42. if (!id) {
  43. LM_ERR("invalid parameter value\n");
  44. return 0;
  45. }
  46. ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con));
  47. if (!ptr) {
  48. LM_ERR("no private memory left\n");
  49. return 0;
  50. }
  51. egrp = 0;
  52. memset(ptr, 0, sizeof(struct my_con));
  53. ptr->ref = 1;
  54. ptr->con = (MYSQL*)pkg_malloc(sizeof(MYSQL));
  55. if (!ptr->con) {
  56. LM_ERR("no private memory left\n");
  57. goto err;
  58. }
  59. mysql_init(ptr->con);
  60. if (id->host[0] == '[' && (egrp = strchr(id->host, ']')) != NULL) {
  61. grp = id->host + 1;
  62. *egrp = '\0';
  63. host = egrp;
  64. if (host != id->host + strlen(id->host)-1) {
  65. host += 1; // host found after closing bracket
  66. }
  67. else {
  68. // let mysql read host info from my.cnf
  69. // (defaults to "localhost")
  70. host = NULL;
  71. }
  72. // read [client] and [<grp>] sections in the order
  73. // given in my.cnf
  74. mysql_options(ptr->con, MYSQL_READ_DEFAULT_GROUP, grp);
  75. }
  76. else {
  77. host = id->host;
  78. }
  79. if (id->port) {
  80. LM_DBG("opening connection: mysql://xxxx:xxxx@%s:%d/%s\n", ZSW(host),
  81. id->port, ZSW(id->database));
  82. } else {
  83. LM_DBG("opening connection: mysql://xxxx:xxxx@%s/%s\n", ZSW(host),
  84. ZSW(id->database));
  85. }
  86. // set connect, read and write timeout, the value counts three times
  87. mysql_options(ptr->con, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&db_mysql_timeout_interval);
  88. mysql_options(ptr->con, MYSQL_OPT_READ_TIMEOUT, (const char *)&db_mysql_timeout_interval);
  89. mysql_options(ptr->con, MYSQL_OPT_WRITE_TIMEOUT, (const char *)&db_mysql_timeout_interval);
  90. if (db_mysql_update_affected_found) {
  91. connection_flag |= CLIENT_FOUND_ROWS;
  92. }
  93. #if (MYSQL_VERSION_ID >= 40100)
  94. if (!mysql_real_connect(ptr->con, host, id->username, id->password,
  95. id->database, id->port, 0, connection_flag|CLIENT_MULTI_STATEMENTS)) {
  96. #else
  97. if (!mysql_real_connect(ptr->con, host, id->username, id->password,
  98. id->database, id->port, 0, connection_flag)) {
  99. #endif
  100. LM_ERR("driver error: %s\n", mysql_error(ptr->con));
  101. /* increase error counter */
  102. counter_inc(mysql_cnts_h.driver_err);
  103. mysql_close(ptr->con);
  104. goto err;
  105. }
  106. /* force reconnection if enabled */
  107. if (db_mysql_auto_reconnect)
  108. ptr->con->reconnect = 1;
  109. else
  110. ptr->con->reconnect = 0;
  111. LM_DBG("connection type is %s\n", mysql_get_host_info(ptr->con));
  112. LM_DBG("protocol version is %d\n", mysql_get_proto_info(ptr->con));
  113. LM_DBG("server version is %s\n", mysql_get_server_info(ptr->con));
  114. ptr->timestamp = time(0);
  115. ptr->id = (struct db_id*)id;
  116. if(egrp) *egrp = ']';
  117. return ptr;
  118. err:
  119. if (ptr && ptr->con) pkg_free(ptr->con);
  120. if (ptr) pkg_free(ptr);
  121. if(egrp) *egrp = ']';
  122. return 0;
  123. }
  124. /*! \brief
  125. * Close the connection and release memory
  126. */
  127. void db_mysql_free_connection(struct pool_con* con)
  128. {
  129. struct my_con * _c;
  130. if (!con) return;
  131. _c = (struct my_con*) con;
  132. if (_c->id) free_db_id(_c->id);
  133. if (_c->con) {
  134. mysql_close(_c->con);
  135. pkg_free(_c->con);
  136. }
  137. pkg_free(_c);
  138. }