src/modules/db_mysql/my_con.c
4b19cda7
 /*
cf63aa85
  * Copyright (C) 2001-2004 iptel.org
4198a3a2
  * Copyright (C) 2006-2007 iptelorg GmbH
cf63aa85
  *
db26c5ce
  * This file is part of Kamailio, a free SIP server.
cf63aa85
  *
db26c5ce
  * Kamailio is free software; you can redistribute it and/or modify
cf63aa85
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
db26c5ce
  * Kamailio is distributed in the hope that it will be useful,
cf63aa85
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
4b19cda7
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
cf63aa85
  */
 
a201f9ae
 #include "my_con.h"
 
43620e90
 #include "db_mysql.h"
a201f9ae
 #include "my_uri.h"
 
cf83221d
 #include "../../core/mem/mem.h"
 #include "../../core/dprint.h"
 #include "../../core/ut.h"
a201f9ae
 
344f101a
 #include <string.h>
 #include <time.h>
cf63aa85
 
260e25e1
 extern int db_mysql_opt_ssl_mode;
cf63aa85
 
 /*
4198a3a2
  * Close the connection and release memory
cf63aa85
  */
4198a3a2
 static void my_con_free(db_con_t* con, struct my_con* payload)
cf63aa85
 {
4198a3a2
 	if (!payload) return;
4b19cda7
 
4198a3a2
 	/* Delete the structure only if there are no more references
 	 * to it in the connection pool
 	 */
 	if (db_pool_remove((db_pool_entry_t*)payload) == 0) return;
4b19cda7
 
4198a3a2
 	db_pool_entry_free(&payload->gen);
 	if (payload->con) pkg_free(payload->con);
 	pkg_free(payload);
 }
cf63aa85
 
 
a201f9ae
 int my_con_connect(db_con_t* con)
4198a3a2
 {
 	struct my_con* mcon;
 	struct my_uri* muri;
4b19cda7
 
4198a3a2
 	mcon = DB_GET_PAYLOAD(con);
 	muri = DB_GET_PAYLOAD(con->uri);
4b19cda7
 
4198a3a2
 	/* Do not reconnect already connected connections */
 	if (mcon->flags & MY_CONNECTED) return 0;
cf63aa85
 
0206c949
 	DBG("Connecting to %.*s:%.*s\n",
4198a3a2
 		con->uri->scheme.len, ZSW(con->uri->scheme.s),
 		con->uri->body.len, ZSW(con->uri->body.s));
c802ee88
 
1bc0f4e4
 	if (my_connect_to) {
4b19cda7
 		if (mysql_options(mcon->con, MYSQL_OPT_CONNECT_TIMEOUT,
3bfc01f0
 					(const void*)&my_connect_to))
0206c949
 			WARN("failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
1bc0f4e4
 	}
c5acc588
 #if MYSQL_VERSION_ID > 50710 && !defined(MARIADB_BASE_VERSION)
260e25e1
 	if(db_mysql_opt_ssl_mode!=0) {
c5acc588
 		unsigned int optuint = 0;
260e25e1
 		if(db_mysql_opt_ssl_mode==1) {
 			if(db_mysql_opt_ssl_mode!=SSL_MODE_DISABLED) {
 				LM_WARN("ssl mode disabled is not 1 (value %u) - enforcing\n",
 						SSL_MODE_DISABLED);
 			}
 			optuint = SSL_MODE_DISABLED;
 		} else {
 			optuint = (unsigned int)db_mysql_opt_ssl_mode;
 		}
 		mysql_options(mcon->con, MYSQL_OPT_SSL_MODE, (const void*)&optuint);
5b727447
 	}
 #else
 	if(db_mysql_opt_ssl_mode!=0) {
 		LM_WARN("ssl mode not supported by mysql version (value %u) - ignoring\n",
c5acc588
 						(unsigned int)db_mysql_opt_ssl_mode);
260e25e1
 	}
 #endif
a201f9ae
 
4b19cda7
 #if MYSQL_VERSION_ID >= 40101
 	if ((my_client_ver >= 50025) ||
 		((my_client_ver >= 40122) &&
 			(my_client_ver < 50000))) {
1bc0f4e4
 		if (my_send_to) {
4b19cda7
 			if (mysql_options(mcon->con, MYSQL_OPT_WRITE_TIMEOUT ,
3bfc01f0
 						(const void*)&my_send_to))
0206c949
 				WARN("failed to set MYSQL_OPT_WRITE_TIMEOUT\n");
1bc0f4e4
 		}
 		if (my_recv_to){
4b19cda7
 			if (mysql_options(mcon->con, MYSQL_OPT_READ_TIMEOUT ,
3bfc01f0
 						(const void*)&my_recv_to))
0206c949
 				WARN("failed to set MYSQL_OPT_READ_TIMEOUT\n");
1bc0f4e4
 		}
 	}
 #endif
4b19cda7
 
 	if (!mysql_real_connect(mcon->con, muri->host, muri->username,
 						muri->password, muri->database, muri->port, 0, 0)) {
0206c949
 		ERR("could not connect: %s\n", mysql_error(mcon->con));
4198a3a2
 		return -1;
cf63aa85
 	}
4b19cda7
 
0206c949
 	DBG("Connection type is %s\n", mysql_get_host_info(mcon->con));
 	DBG("Protocol version is %d\n", mysql_get_proto_info(mcon->con));
 	DBG("Server version is %s\n", mysql_get_server_info(mcon->con));
cf63aa85
 
4198a3a2
 	mcon->flags |= MY_CONNECTED;
 	return 0;
 }
c2f1d073
 
c802ee88
 
a201f9ae
 void my_con_disconnect(db_con_t* con)
4198a3a2
 {
 	struct my_con* mcon;
c802ee88
 
4198a3a2
 	mcon = DB_GET_PAYLOAD(con);
cf63aa85
 
4198a3a2
 	if ((mcon->flags & MY_CONNECTED) == 0) return;
 
0206c949
 	DBG("Disconnecting from %.*s:%.*s\n",
4198a3a2
 		con->uri->scheme.len, ZSW(con->uri->scheme.s),
 		con->uri->body.len, ZSW(con->uri->body.s));
 
 	mysql_close(mcon->con);
 	mcon->flags &= ~MY_CONNECTED;
0e564bcd
 
 	/* Increase the variable that keeps track of number of connection
 	 * resets on this connection. The mysql module uses the variable to
 	 * determine when a pre-compiled command needs to be uploaded to the
 	 * server again. If the number in the my_con structure is larger than
 	 * the number kept in my_cmd then it means that we have to upload the
 	 * command to the server again because the connection was reset.
 	 */
 	mcon->resets++;
cf63aa85
 }
 
 
4198a3a2
 int my_con(db_con_t* con)
cf63aa85
 {
4198a3a2
 	struct my_con* ptr;
 
 	/* First try to lookup the connection in the connection pool and
 	 * re-use it if a match is found
 	 */
 	ptr = (struct my_con*)db_pool_get(con->uri);
 	if (ptr) {
0206c949
 		DBG("Connection to %.*s:%.*s found in connection pool\n",
4198a3a2
 			con->uri->scheme.len, ZSW(con->uri->scheme.s),
 			con->uri->body.len, ZSW(con->uri->body.s));
 		goto found;
 	}
 
 	ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con));
 	if (!ptr) {
f3fa0039
 		PKG_MEM_ERROR;
4198a3a2
 		goto error;
cf63aa85
 	}
4198a3a2
 	memset(ptr, '\0', sizeof(struct my_con));
 	if (db_pool_entry_init(&ptr->gen, my_con_free, con->uri) < 0) goto error;
 
 	ptr->con = (MYSQL*)pkg_malloc(sizeof(MYSQL));
 	if (!ptr->con) {
f3fa0039
 		PKG_MEM_ERROR;
4198a3a2
 		goto error;
 	}
 	mysql_init(ptr->con);
 
0206c949
 	DBG("Creating new connection to: %.*s:%.*s\n",
4198a3a2
 		con->uri->scheme.len, ZSW(con->uri->scheme.s),
 		con->uri->body.len, ZSW(con->uri->body.s));
 
 	/* Put the newly created mysql connection into the pool */
 	db_pool_put((struct db_pool_entry*)ptr);
0206c949
 	DBG("Connection stored in connection pool\n");
4198a3a2
 
4b19cda7
 found:
4198a3a2
 	/* Attach driver payload to the db_con structure and set connect and
 	 * disconnect functions
 	 */
 	DB_SET_PAYLOAD(con, ptr);
 	con->connect = my_con_connect;
 	con->disconnect = my_con_disconnect;
 	return 0;
 
4b19cda7
 error:
4198a3a2
 	if (ptr) {
 		db_pool_entry_free(&ptr->gen);
 		if (ptr->con) pkg_free(ptr->con);
 		pkg_free(ptr);
 	}
 	return 0;
cf63aa85
 }