Browse code

db_mysql: use MARIADB_BASE_VERSION macro to check for MariaDB library

Daniel-Constantin Mierla authored on 15/06/2020 07:09:20
Showing 1 changed files
... ...
@@ -55,7 +55,6 @@ int my_con_connect(db_con_t* con)
55 55
 {
56 56
 	struct my_con* mcon;
57 57
 	struct my_uri* muri;
58
-	unsigned int optuint = 0;
59 58
 
60 59
 	mcon = DB_GET_PAYLOAD(con);
61 60
 	muri = DB_GET_PAYLOAD(con->uri);
... ...
@@ -72,9 +71,9 @@ int my_con_connect(db_con_t* con)
72 71
 					(const void*)&my_connect_to))
73 72
 			WARN("failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
74 73
 	}
75
-#ifndef LIBMARIADB
76
-#if MYSQL_VERSION_ID > 50710
74
+#if MYSQL_VERSION_ID > 50710 && !defined(MARIADB_BASE_VERSION)
77 75
 	if(db_mysql_opt_ssl_mode!=0) {
76
+		unsigned int optuint = 0;
78 77
 		if(db_mysql_opt_ssl_mode==1) {
79 78
 			if(db_mysql_opt_ssl_mode!=SSL_MODE_DISABLED) {
80 79
 				LM_WARN("ssl mode disabled is not 1 (value %u) - enforcing\n",
... ...
@@ -88,16 +87,8 @@ int my_con_connect(db_con_t* con)
88 87
 	}
89 88
 #else
90 89
 	if(db_mysql_opt_ssl_mode!=0) {
91
-		optuint = (unsigned int)db_mysql_opt_ssl_mode;
92 90
 		LM_WARN("ssl mode not supported by mysql version (value %u) - ignoring\n",
93
-						optuint);
94
-	}
95
-#endif
96
-#else
97
-	if(db_mysql_opt_ssl_mode!=0) {
98
-		optuint = (unsigned int)db_mysql_opt_ssl_mode;
99
-		LM_WARN("ssl mode not supported by mariadb (value %u) - ignoring\n",
100
-						optuint);
91
+						(unsigned int)db_mysql_opt_ssl_mode);
101 92
 	}
102 93
 #endif
103 94
 
Browse code

db_mysql: mariadb client library does not support ssl mode option

Daniel-Constantin Mierla authored on 14/06/2020 10:14:40
Showing 1 changed files
... ...
@@ -72,6 +72,7 @@ int my_con_connect(db_con_t* con)
72 72
 					(const void*)&my_connect_to))
73 73
 			WARN("failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
74 74
 	}
75
+#ifndef LIBMARIADB
75 76
 #if MYSQL_VERSION_ID > 50710
76 77
 	if(db_mysql_opt_ssl_mode!=0) {
77 78
 		if(db_mysql_opt_ssl_mode==1) {
... ...
@@ -85,6 +86,19 @@ int my_con_connect(db_con_t* con)
85 86
 		}
86 87
 		mysql_options(mcon->con, MYSQL_OPT_SSL_MODE, (const void*)&optuint);
87 88
 	}
89
+#else
90
+	if(db_mysql_opt_ssl_mode!=0) {
91
+		optuint = (unsigned int)db_mysql_opt_ssl_mode;
92
+		LM_WARN("ssl mode not supported by mysql version (value %u) - ignoring\n",
93
+						optuint);
94
+	}
95
+#endif
96
+#else
97
+	if(db_mysql_opt_ssl_mode!=0) {
98
+		optuint = (unsigned int)db_mysql_opt_ssl_mode;
99
+		LM_WARN("ssl mode not supported by mariadb (value %u) - ignoring\n",
100
+						optuint);
101
+	}
88 102
 #endif
89 103
 
90 104
 #if MYSQL_VERSION_ID >= 40101
Browse code

db_mysql: new parameter opt_ssl_mode - allow controling ssl mode

- can disable or enforce use of ssl
- some combinations of libmysqlclient and libssl1.1 can result in
crashing when ssl/tls is used, even on 127.0.0.1, this param can be used
to disable it

Daniel-Constantin Mierla authored on 14/06/2020 09:18:15
Showing 1 changed files
... ...
@@ -31,6 +31,7 @@
31 31
 #include <string.h>
32 32
 #include <time.h>
33 33
 
34
+extern int db_mysql_opt_ssl_mode;
34 35
 
35 36
 /*
36 37
  * Close the connection and release memory
... ...
@@ -54,6 +55,7 @@ int my_con_connect(db_con_t* con)
54 55
 {
55 56
 	struct my_con* mcon;
56 57
 	struct my_uri* muri;
58
+	unsigned int optuint = 0;
57 59
 
58 60
 	mcon = DB_GET_PAYLOAD(con);
59 61
 	muri = DB_GET_PAYLOAD(con->uri);
... ...
@@ -70,6 +72,20 @@ int my_con_connect(db_con_t* con)
70 72
 					(const void*)&my_connect_to))
71 73
 			WARN("failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
72 74
 	}
75
+#if MYSQL_VERSION_ID > 50710
76
+	if(db_mysql_opt_ssl_mode!=0) {
77
+		if(db_mysql_opt_ssl_mode==1) {
78
+			if(db_mysql_opt_ssl_mode!=SSL_MODE_DISABLED) {
79
+				LM_WARN("ssl mode disabled is not 1 (value %u) - enforcing\n",
80
+						SSL_MODE_DISABLED);
81
+			}
82
+			optuint = SSL_MODE_DISABLED;
83
+		} else {
84
+			optuint = (unsigned int)db_mysql_opt_ssl_mode;
85
+		}
86
+		mysql_options(mcon->con, MYSQL_OPT_SSL_MODE, (const void*)&optuint);
87
+	}
88
+#endif
73 89
 
74 90
 #if MYSQL_VERSION_ID >= 40101
75 91
 	if ((my_client_ver >= 50025) ||
Browse code

db_mysql: use cast to (const void*) for mysql_options() value

- match the function signature, cast to (char*) or (const char*) being
used so far

Daniel-Constantin Mierla authored on 11/03/2020 07:41:19
Showing 1 changed files
... ...
@@ -67,7 +67,7 @@ int my_con_connect(db_con_t* con)
67 67
 
68 68
 	if (my_connect_to) {
69 69
 		if (mysql_options(mcon->con, MYSQL_OPT_CONNECT_TIMEOUT,
70
-					(char*)&my_connect_to))
70
+					(const void*)&my_connect_to))
71 71
 			WARN("failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
72 72
 	}
73 73
 
... ...
@@ -77,12 +77,12 @@ int my_con_connect(db_con_t* con)
77 77
 			(my_client_ver < 50000))) {
78 78
 		if (my_send_to) {
79 79
 			if (mysql_options(mcon->con, MYSQL_OPT_WRITE_TIMEOUT ,
80
-						(char*)&my_send_to))
80
+						(const void*)&my_send_to))
81 81
 				WARN("failed to set MYSQL_OPT_WRITE_TIMEOUT\n");
82 82
 		}
83 83
 		if (my_recv_to){
84 84
 			if (mysql_options(mcon->con, MYSQL_OPT_READ_TIMEOUT ,
85
-						(char*)&my_recv_to))
85
+						(const void*)&my_recv_to))
86 86
 				WARN("failed to set MYSQL_OPT_READ_TIMEOUT\n");
87 87
 		}
88 88
 	}
Browse code

db_mysql: remove old module name prefix from several log statements, already added from core

Henning Westerholt authored on 21/01/2019 22:25:02
Showing 1 changed files
... ...
@@ -61,14 +61,14 @@ int my_con_connect(db_con_t* con)
61 61
 	/* Do not reconnect already connected connections */
62 62
 	if (mcon->flags & MY_CONNECTED) return 0;
63 63
 
64
-	DBG("mysql: Connecting to %.*s:%.*s\n",
64
+	DBG("Connecting to %.*s:%.*s\n",
65 65
 		con->uri->scheme.len, ZSW(con->uri->scheme.s),
66 66
 		con->uri->body.len, ZSW(con->uri->body.s));
67 67
 
68 68
 	if (my_connect_to) {
69 69
 		if (mysql_options(mcon->con, MYSQL_OPT_CONNECT_TIMEOUT,
70 70
 					(char*)&my_connect_to))
71
-			WARN("mysql: failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
71
+			WARN("failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
72 72
 	}
73 73
 
74 74
 #if MYSQL_VERSION_ID >= 40101
... ...
@@ -78,25 +78,25 @@ int my_con_connect(db_con_t* con)
78 78
 		if (my_send_to) {
79 79
 			if (mysql_options(mcon->con, MYSQL_OPT_WRITE_TIMEOUT ,
80 80
 						(char*)&my_send_to))
81
-				WARN("mysql: failed to set MYSQL_OPT_WRITE_TIMEOUT\n");
81
+				WARN("failed to set MYSQL_OPT_WRITE_TIMEOUT\n");
82 82
 		}
83 83
 		if (my_recv_to){
84 84
 			if (mysql_options(mcon->con, MYSQL_OPT_READ_TIMEOUT ,
85 85
 						(char*)&my_recv_to))
86
-				WARN("mysql: failed to set MYSQL_OPT_READ_TIMEOUT\n");
86
+				WARN("failed to set MYSQL_OPT_READ_TIMEOUT\n");
87 87
 		}
88 88
 	}
89 89
 #endif
90 90
 
91 91
 	if (!mysql_real_connect(mcon->con, muri->host, muri->username,
92 92
 						muri->password, muri->database, muri->port, 0, 0)) {
93
-		ERR("mysql: %s\n", mysql_error(mcon->con));
93
+		ERR("could not connect: %s\n", mysql_error(mcon->con));
94 94
 		return -1;
95 95
 	}
96 96
 
97
-	DBG("mysql: Connection type is %s\n", mysql_get_host_info(mcon->con));
98
-	DBG("mysql: Protocol version is %d\n", mysql_get_proto_info(mcon->con));
99
-	DBG("mysql: Server version is %s\n", mysql_get_server_info(mcon->con));
97
+	DBG("Connection type is %s\n", mysql_get_host_info(mcon->con));
98
+	DBG("Protocol version is %d\n", mysql_get_proto_info(mcon->con));
99
+	DBG("Server version is %s\n", mysql_get_server_info(mcon->con));
100 100
 
101 101
 	mcon->flags |= MY_CONNECTED;
102 102
 	return 0;
... ...
@@ -111,7 +111,7 @@ void my_con_disconnect(db_con_t* con)
111 111
 
112 112
 	if ((mcon->flags & MY_CONNECTED) == 0) return;
113 113
 
114
-	DBG("mysql: Disconnecting from %.*s:%.*s\n",
114
+	DBG("Disconnecting from %.*s:%.*s\n",
115 115
 		con->uri->scheme.len, ZSW(con->uri->scheme.s),
116 116
 		con->uri->body.len, ZSW(con->uri->body.s));
117 117
 
... ...
@@ -138,7 +138,7 @@ int my_con(db_con_t* con)
138 138
 	 */
139 139
 	ptr = (struct my_con*)db_pool_get(con->uri);
140 140
 	if (ptr) {
141
-		DBG("mysql: Connection to %.*s:%.*s found in connection pool\n",
141
+		DBG("Connection to %.*s:%.*s found in connection pool\n",
142 142
 			con->uri->scheme.len, ZSW(con->uri->scheme.s),
143 143
 			con->uri->body.len, ZSW(con->uri->body.s));
144 144
 		goto found;
... ...
@@ -159,13 +159,13 @@ int my_con(db_con_t* con)
159 159
 	}
160 160
 	mysql_init(ptr->con);
161 161
 
162
-	DBG("mysql: Creating new connection to: %.*s:%.*s\n",
162
+	DBG("Creating new connection to: %.*s:%.*s\n",
163 163
 		con->uri->scheme.len, ZSW(con->uri->scheme.s),
164 164
 		con->uri->body.len, ZSW(con->uri->body.s));
165 165
 
166 166
 	/* Put the newly created mysql connection into the pool */
167 167
 	db_pool_put((struct db_pool_entry*)ptr);
168
-	DBG("mysql: Connection stored in connection pool\n");
168
+	DBG("Connection stored in connection pool\n");
169 169
 
170 170
 found:
171 171
 	/* Attach driver payload to the db_con structure and set connect and
Browse code

db_mysql: use generic PKG_MEM_ERROR, SHM_ERROR and SYS_MEM_ERROR helper defines

Henning Westerholt authored on 21/01/2019 22:17:21
Showing 1 changed files
... ...
@@ -146,7 +146,7 @@ int my_con(db_con_t* con)
146 146
 
147 147
 	ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con));
148 148
 	if (!ptr) {
149
-		LOG(L_ERR, "mysql: No memory left\n");
149
+		PKG_MEM_ERROR;
150 150
 		goto error;
151 151
 	}
152 152
 	memset(ptr, '\0', sizeof(struct my_con));
... ...
@@ -154,7 +154,7 @@ int my_con(db_con_t* con)
154 154
 
155 155
 	ptr->con = (MYSQL*)pkg_malloc(sizeof(MYSQL));
156 156
 	if (!ptr->con) {
157
-		LOG(L_ERR, "mysql: No enough memory\n");
157
+		PKG_MEM_ERROR;
158 158
 		goto error;
159 159
 	}
160 160
 	mysql_init(ptr->con);
Browse code

db_mysql: small unification for log macro usage for errors and warning

Henning Westerholt authored on 21/01/2019 21:58:35
Showing 1 changed files
... ...
@@ -90,7 +90,7 @@ int my_con_connect(db_con_t* con)
90 90
 
91 91
 	if (!mysql_real_connect(mcon->con, muri->host, muri->username,
92 92
 						muri->password, muri->database, muri->port, 0, 0)) {
93
-		LOG(L_ERR, "mysql: %s\n", mysql_error(mcon->con));
93
+		ERR("mysql: %s\n", mysql_error(mcon->con));
94 94
 		return -1;
95 95
 	}
96 96
 
Browse code

db_mysql: coherent indentation and whitespacing

Daniel-Constantin Mierla authored on 09/11/2017 06:03:02
Showing 1 changed files
... ...
@@ -1,4 +1,4 @@
1
-/* 
1
+/*
2 2
  * Copyright (C) 2001-2004 iptel.org
3 3
  * Copyright (C) 2006-2007 iptelorg GmbH
4 4
  *
... ...
@@ -14,8 +14,8 @@
14 14
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 15
  * GNU General Public License for more details.
16 16
  *
17
- * You should have received a copy of the GNU General Public License 
18
- * along with this program; if not, write to the Free Software 
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with this program; if not, write to the Free Software
19 19
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 20
  */
21 21
 
... ...
@@ -38,12 +38,12 @@
38 38
 static void my_con_free(db_con_t* con, struct my_con* payload)
39 39
 {
40 40
 	if (!payload) return;
41
-	
41
+
42 42
 	/* Delete the structure only if there are no more references
43 43
 	 * to it in the connection pool
44 44
 	 */
45 45
 	if (db_pool_remove((db_pool_entry_t*)payload) == 0) return;
46
-	
46
+
47 47
 	db_pool_entry_free(&payload->gen);
48 48
 	if (payload->con) pkg_free(payload->con);
49 49
 	pkg_free(payload);
... ...
@@ -54,10 +54,10 @@ int my_con_connect(db_con_t* con)
54 54
 {
55 55
 	struct my_con* mcon;
56 56
 	struct my_uri* muri;
57
-	
57
+
58 58
 	mcon = DB_GET_PAYLOAD(con);
59 59
 	muri = DB_GET_PAYLOAD(con->uri);
60
-	
60
+
61 61
 	/* Do not reconnect already connected connections */
62 62
 	if (mcon->flags & MY_CONNECTED) return 0;
63 63
 
... ...
@@ -66,34 +66,34 @@ int my_con_connect(db_con_t* con)
66 66
 		con->uri->body.len, ZSW(con->uri->body.s));
67 67
 
68 68
 	if (my_connect_to) {
69
-		if (mysql_options(mcon->con, MYSQL_OPT_CONNECT_TIMEOUT, 
70
-						  (char*)&my_connect_to))
69
+		if (mysql_options(mcon->con, MYSQL_OPT_CONNECT_TIMEOUT,
70
+					(char*)&my_connect_to))
71 71
 			WARN("mysql: failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
72 72
 	}
73 73
 
74
-#if MYSQL_VERSION_ID >= 40101 
75
-	if ((my_client_ver >= 50025) || 
76
-		((my_client_ver >= 40122) && 
77
-		 (my_client_ver < 50000))) {
74
+#if MYSQL_VERSION_ID >= 40101
75
+	if ((my_client_ver >= 50025) ||
76
+		((my_client_ver >= 40122) &&
77
+			(my_client_ver < 50000))) {
78 78
 		if (my_send_to) {
79
-			if (mysql_options(mcon->con, MYSQL_OPT_WRITE_TIMEOUT , 
80
-							  (char*)&my_send_to))
79
+			if (mysql_options(mcon->con, MYSQL_OPT_WRITE_TIMEOUT ,
80
+						(char*)&my_send_to))
81 81
 				WARN("mysql: failed to set MYSQL_OPT_WRITE_TIMEOUT\n");
82 82
 		}
83 83
 		if (my_recv_to){
84
-			if (mysql_options(mcon->con, MYSQL_OPT_READ_TIMEOUT , 
85
-							  (char*)&my_recv_to))
84
+			if (mysql_options(mcon->con, MYSQL_OPT_READ_TIMEOUT ,
85
+						(char*)&my_recv_to))
86 86
 				WARN("mysql: failed to set MYSQL_OPT_READ_TIMEOUT\n");
87 87
 		}
88 88
 	}
89 89
 #endif
90
-	
91
-	if (!mysql_real_connect(mcon->con, muri->host, muri->username, 
92
-							muri->password, muri->database, muri->port, 0, 0)) {
90
+
91
+	if (!mysql_real_connect(mcon->con, muri->host, muri->username,
92
+						muri->password, muri->database, muri->port, 0, 0)) {
93 93
 		LOG(L_ERR, "mysql: %s\n", mysql_error(mcon->con));
94 94
 		return -1;
95 95
 	}
96
-	
96
+
97 97
 	DBG("mysql: Connection type is %s\n", mysql_get_host_info(mcon->con));
98 98
 	DBG("mysql: Protocol version is %d\n", mysql_get_proto_info(mcon->con));
99 99
 	DBG("mysql: Server version is %s\n", mysql_get_server_info(mcon->con));
... ...
@@ -167,7 +167,7 @@ int my_con(db_con_t* con)
167 167
 	db_pool_put((struct db_pool_entry*)ptr);
168 168
 	DBG("mysql: Connection stored in connection pool\n");
169 169
 
170
- found:
170
+found:
171 171
 	/* Attach driver payload to the db_con structure and set connect and
172 172
 	 * disconnect functions
173 173
 	 */
... ...
@@ -176,7 +176,7 @@ int my_con(db_con_t* con)
176 176
 	con->disconnect = my_con_disconnect;
177 177
 	return 0;
178 178
 
179
- error:
179
+error:
180 180
 	if (ptr) {
181 181
 		db_pool_entry_free(&ptr->gen);
182 182
 		if (ptr->con) pkg_free(ptr->con);
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -24,9 +24,9 @@
24 24
 #include "db_mysql.h"
25 25
 #include "my_uri.h"
26 26
 
27
-#include "../../mem/mem.h"
28
-#include "../../dprint.h"
29
-#include "../../ut.h"
27
+#include "../../core/mem/mem.h"
28
+#include "../../core/dprint.h"
29
+#include "../../core/ut.h"
30 30
 
31 31
 #include <string.h>
32 32
 #include <time.h>
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

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