/*
 * Copyright (C) 2001-2004 iptel.org
 * Copyright (C) 2006-2007 iptelorg GmbH
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * 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
 *
 * Kamailio is distributed in the hope that it will be useful,
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "my_con.h"

#include "db_mysql.h"
#include "my_uri.h"

#include "../../core/mem/mem.h"
#include "../../core/dprint.h"
#include "../../core/ut.h"

#include <string.h>
#include <time.h>

extern int db_mysql_opt_ssl_mode;

/*
 * Close the connection and release memory
 */
static void my_con_free(db_con_t* con, struct my_con* payload)
{
	if (!payload) return;

	/* 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;

	db_pool_entry_free(&payload->gen);
	if (payload->con) pkg_free(payload->con);
	pkg_free(payload);
}


int my_con_connect(db_con_t* con)
{
	struct my_con* mcon;
	struct my_uri* muri;

	mcon = DB_GET_PAYLOAD(con);
	muri = DB_GET_PAYLOAD(con->uri);

	/* Do not reconnect already connected connections */
	if (mcon->flags & MY_CONNECTED) return 0;

	DBG("Connecting to %.*s:%.*s\n",
		con->uri->scheme.len, ZSW(con->uri->scheme.s),
		con->uri->body.len, ZSW(con->uri->body.s));

	if (my_connect_to) {
		if (mysql_options(mcon->con, MYSQL_OPT_CONNECT_TIMEOUT,
					(const void*)&my_connect_to))
			WARN("failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
	}
#if MYSQL_VERSION_ID > 50710 && !defined(MARIADB_BASE_VERSION)
	if(db_mysql_opt_ssl_mode!=0) {
		unsigned int optuint = 0;
		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);
	}
#else
	if(db_mysql_opt_ssl_mode!=0) {
		LM_WARN("ssl mode not supported by mysql version (value %u) - ignoring\n",
						(unsigned int)db_mysql_opt_ssl_mode);
	}
#endif

#if MYSQL_VERSION_ID >= 40101
	if ((my_client_ver >= 50025) ||
		((my_client_ver >= 40122) &&
			(my_client_ver < 50000))) {
		if (my_send_to) {
			if (mysql_options(mcon->con, MYSQL_OPT_WRITE_TIMEOUT ,
						(const void*)&my_send_to))
				WARN("failed to set MYSQL_OPT_WRITE_TIMEOUT\n");
		}
		if (my_recv_to){
			if (mysql_options(mcon->con, MYSQL_OPT_READ_TIMEOUT ,
						(const void*)&my_recv_to))
				WARN("failed to set MYSQL_OPT_READ_TIMEOUT\n");
		}
	}
#endif

	if (!mysql_real_connect(mcon->con, muri->host, muri->username,
						muri->password, muri->database, muri->port, 0, 0)) {
		ERR("could not connect: %s\n", mysql_error(mcon->con));
		return -1;
	}

	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));

	mcon->flags |= MY_CONNECTED;
	return 0;
}


void my_con_disconnect(db_con_t* con)
{
	struct my_con* mcon;

	mcon = DB_GET_PAYLOAD(con);

	if ((mcon->flags & MY_CONNECTED) == 0) return;

	DBG("Disconnecting from %.*s:%.*s\n",
		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;

	/* 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++;
}


int my_con(db_con_t* con)
{
	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) {
		DBG("Connection to %.*s:%.*s found in connection pool\n",
			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) {
		PKG_MEM_ERROR;
		goto error;
	}
	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) {
		PKG_MEM_ERROR;
		goto error;
	}
	mysql_init(ptr->con);

	DBG("Creating new connection to: %.*s:%.*s\n",
		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);
	DBG("Connection stored in connection pool\n");

found:
	/* 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;

error:
	if (ptr) {
		db_pool_entry_free(&ptr->gen);
		if (ptr->con) pkg_free(ptr->con);
		pkg_free(ptr);
	}
	return 0;
}
