km_my_con.c 4.2 KB

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