my_con.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2004 iptel.org
  5. * Copyright (C) 2006-2007 iptelorg GmbH
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser 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. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. #include "my_con.h"
  29. #include "mysql_mod.h"
  30. #include "my_uri.h"
  31. #include "../../mem/mem.h"
  32. #include "../../dprint.h"
  33. #include "../../ut.h"
  34. #include <string.h>
  35. #include <time.h>
  36. /*
  37. * Close the connection and release memory
  38. */
  39. static void my_con_free(db_con_t* con, struct my_con* payload)
  40. {
  41. if (!payload) return;
  42. /* Delete the structure only if there are no more references
  43. * to it in the connection pool
  44. */
  45. if (db_pool_remove((db_pool_entry_t*)payload) == 0) return;
  46. db_pool_entry_free(&payload->gen);
  47. if (payload->con) pkg_free(payload->con);
  48. pkg_free(payload);
  49. }
  50. int my_con_connect(db_con_t* con)
  51. {
  52. struct my_con* mcon;
  53. struct my_uri* muri;
  54. mcon = DB_GET_PAYLOAD(con);
  55. muri = DB_GET_PAYLOAD(con->uri);
  56. /* Do not reconnect already connected connections */
  57. if (mcon->flags & MY_CONNECTED) return 0;
  58. DBG("mysql: Connecting to %.*s:%.*s\n",
  59. con->uri->scheme.len, ZSW(con->uri->scheme.s),
  60. con->uri->body.len, ZSW(con->uri->body.s));
  61. if (my_connect_to) {
  62. if (mysql_options(mcon->con, MYSQL_OPT_CONNECT_TIMEOUT,
  63. (char*)&my_connect_to))
  64. WARN("mysql: failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
  65. }
  66. #if MYSQL_VERSION_ID >= 40101
  67. if ((my_client_ver >= 50025) ||
  68. ((my_client_ver >= 40122) &&
  69. (my_client_ver < 50000))) {
  70. if (my_send_to) {
  71. if (mysql_options(mcon->con, MYSQL_OPT_WRITE_TIMEOUT ,
  72. (char*)&my_send_to))
  73. WARN("mysql: failed to set MYSQL_OPT_WRITE_TIMEOUT\n");
  74. }
  75. if (my_recv_to){
  76. if (mysql_options(mcon->con, MYSQL_OPT_READ_TIMEOUT ,
  77. (char*)&my_recv_to))
  78. WARN("mysql: failed to set MYSQL_OPT_READ_TIMEOUT\n");
  79. }
  80. }
  81. #endif
  82. if (!mysql_real_connect(mcon->con, muri->host, muri->username,
  83. muri->password, muri->database, muri->port, 0, 0)) {
  84. LOG(L_ERR, "mysql: %s\n", mysql_error(mcon->con));
  85. return -1;
  86. }
  87. DBG("mysql: Connection type is %s\n", mysql_get_host_info(mcon->con));
  88. DBG("mysql: Protocol version is %d\n", mysql_get_proto_info(mcon->con));
  89. DBG("mysql: Server version is %s\n", mysql_get_server_info(mcon->con));
  90. mcon->flags |= MY_CONNECTED;
  91. return 0;
  92. }
  93. void my_con_disconnect(db_con_t* con)
  94. {
  95. struct my_con* mcon;
  96. mcon = DB_GET_PAYLOAD(con);
  97. if ((mcon->flags & MY_CONNECTED) == 0) return;
  98. DBG("mysql: Disconnecting from %.*s:%.*s\n",
  99. con->uri->scheme.len, ZSW(con->uri->scheme.s),
  100. con->uri->body.len, ZSW(con->uri->body.s));
  101. mysql_close(mcon->con);
  102. mcon->flags &= ~MY_CONNECTED;
  103. /* Increase the variable that keeps track of number of connection
  104. * resets on this connection. The mysql module uses the variable to
  105. * determine when a pre-compiled command needs to be uploaded to the
  106. * server again. If the number in the my_con structure is larger than
  107. * the number kept in my_cmd then it means that we have to upload the
  108. * command to the server again because the connection was reset.
  109. */
  110. mcon->resets++;
  111. }
  112. int my_con(db_con_t* con)
  113. {
  114. struct my_con* ptr;
  115. /* First try to lookup the connection in the connection pool and
  116. * re-use it if a match is found
  117. */
  118. ptr = (struct my_con*)db_pool_get(con->uri);
  119. if (ptr) {
  120. DBG("mysql: Connection to %.*s:%.*s found in connection pool\n",
  121. con->uri->scheme.len, ZSW(con->uri->scheme.s),
  122. con->uri->body.len, ZSW(con->uri->body.s));
  123. goto found;
  124. }
  125. ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con));
  126. if (!ptr) {
  127. LOG(L_ERR, "mysql: No memory left\n");
  128. goto error;
  129. }
  130. memset(ptr, '\0', sizeof(struct my_con));
  131. if (db_pool_entry_init(&ptr->gen, my_con_free, con->uri) < 0) goto error;
  132. ptr->con = (MYSQL*)pkg_malloc(sizeof(MYSQL));
  133. if (!ptr->con) {
  134. LOG(L_ERR, "mysql: No enough memory\n");
  135. goto error;
  136. }
  137. mysql_init(ptr->con);
  138. DBG("mysql: Creating new connection to: %.*s:%.*s\n",
  139. con->uri->scheme.len, ZSW(con->uri->scheme.s),
  140. con->uri->body.len, ZSW(con->uri->body.s));
  141. /* Put the newly created mysql connection into the pool */
  142. db_pool_put((struct db_pool_entry*)ptr);
  143. DBG("mysql: Connection stored in connection pool\n");
  144. found:
  145. /* Attach driver payload to the db_con structure and set connect and
  146. * disconnect functions
  147. */
  148. DB_SET_PAYLOAD(con, ptr);
  149. con->connect = my_con_connect;
  150. con->disconnect = my_con_disconnect;
  151. return 0;
  152. error:
  153. if (ptr) {
  154. db_pool_entry_free(&ptr->gen);
  155. if (ptr->con) pkg_free(ptr->con);
  156. pkg_free(ptr);
  157. }
  158. return 0;
  159. }