km_my_con.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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/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;
  43. if (!id) {
  44. LM_ERR("invalid parameter value\n");
  45. return 0;
  46. }
  47. ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con));
  48. if (!ptr) {
  49. LM_ERR("no private memory left\n");
  50. return 0;
  51. }
  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] == '[' && (host = strchr(id->host, ']')) != NULL) {
  61. grp = id->host + 1;
  62. *host = '\0';
  63. if (host != id->host + strlen(id->host)-1) {
  64. host += 1; // host found after closing bracket
  65. }
  66. else {
  67. // let mysql read host info from my.cnf
  68. // (defaults to "localhost")
  69. host = NULL;
  70. }
  71. // read [client] and [<grp>] sections in the order
  72. // given in my.cnf
  73. mysql_options(ptr->con, MYSQL_READ_DEFAULT_GROUP, grp);
  74. }
  75. else {
  76. host = id->host;
  77. }
  78. if (id->port) {
  79. LM_DBG("opening connection: mysql://xxxx:xxxx@%s:%d/%s\n", ZSW(host),
  80. id->port, ZSW(id->database));
  81. } else {
  82. LM_DBG("opening connection: mysql://xxxx:xxxx@%s/%s\n", ZSW(host),
  83. ZSW(id->database));
  84. }
  85. // set connect, read and write timeout, the value counts three times
  86. mysql_options(ptr->con, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&db_mysql_timeout_interval);
  87. mysql_options(ptr->con, MYSQL_OPT_READ_TIMEOUT, (const char *)&db_mysql_timeout_interval);
  88. mysql_options(ptr->con, MYSQL_OPT_WRITE_TIMEOUT, (const char *)&db_mysql_timeout_interval);
  89. #if (MYSQL_VERSION_ID >= 40100)
  90. if (!mysql_real_connect(ptr->con, host, id->username, id->password,
  91. id->database, id->port, 0, CLIENT_MULTI_STATEMENTS)) {
  92. #else
  93. if (!mysql_real_connect(ptr->con, host, id->username, id->password,
  94. id->database, id->port, 0, 0)) {
  95. #endif
  96. LM_ERR("driver error: %s\n", mysql_error(ptr->con));
  97. /* increase error counter */
  98. counter_inc(mysql_cnts_h.driver_err);
  99. mysql_close(ptr->con);
  100. goto err;
  101. }
  102. /* force reconnection if enabled */
  103. if (db_mysql_auto_reconnect)
  104. ptr->con->reconnect = 1;
  105. else
  106. ptr->con->reconnect = 0;
  107. LM_DBG("connection type is %s\n", mysql_get_host_info(ptr->con));
  108. LM_DBG("protocol version is %d\n", mysql_get_proto_info(ptr->con));
  109. LM_DBG("server version is %s\n", mysql_get_server_info(ptr->con));
  110. ptr->timestamp = time(0);
  111. ptr->id = (struct db_id*)id;
  112. return ptr;
  113. err:
  114. if (ptr && ptr->con) pkg_free(ptr->con);
  115. if (ptr) pkg_free(ptr);
  116. return 0;
  117. }
  118. /*! \brief
  119. * Close the connection and release memory
  120. */
  121. void db_mysql_free_connection(struct pool_con* con)
  122. {
  123. struct my_con * _c;
  124. if (!con) return;
  125. _c = (struct my_con*) con;
  126. if (_c->id) free_db_id(_c->id);
  127. if (_c->con) {
  128. mysql_close(_c->con);
  129. pkg_free(_c->con);
  130. }
  131. pkg_free(_c);
  132. }