| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-01-21 07:06:25 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2004-10-05 06:46:11 +00:00
										 |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * res_odbc.c <ODBC resource manager> | 
					
						
							| 
									
										
										
										
											2005-01-21 07:06:25 +00:00
										 |  |  |  * Copyright (C) 2004 - 2005 Anthony Minessale II <anthmct@yahoo.com> | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief ODBC resource manager | 
					
						
							|  |  |  |  *  | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * \author Mark Spencer <markster@digium.com> | 
					
						
							|  |  |  |  * \author Anthony Minessale II <anthmct@yahoo.com> | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * \arg See also: \ref cdr_odbc | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<depend>unixodbc</depend> | 
					
						
							| 
									
										
										
										
											2007-06-18 16:35:02 +00:00
										 |  |  | 	<depend>ltdl</depend> | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-08 19:47:56 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2005-06-06 22:12:19 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/logger.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/config.h"
 | 
					
						
							|  |  |  | #include "asterisk/options.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/cli.h"
 | 
					
						
							|  |  |  | #include "asterisk/lock.h"
 | 
					
						
							|  |  |  | #include "asterisk/res_odbc.h"
 | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | #include "asterisk/time.h"
 | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | struct odbc_class | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	AST_LIST_ENTRY(odbc_class) list; | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	char name[80]; | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	char dsn[80]; | 
					
						
							|  |  |  | 	char username[80]; | 
					
						
							|  |  |  | 	char password[80]; | 
					
						
							|  |  |  | 	SQLHENV env; | 
					
						
							|  |  |  | 	unsigned int haspool:1;         /* Boolean - TDS databases need this */ | 
					
						
							|  |  |  | 	unsigned int limit:10;          /* Gives a limit of 1023 maximum */ | 
					
						
							|  |  |  | 	unsigned int count:10;          /* Running count of pooled connections */ | 
					
						
							|  |  |  | 	unsigned int delme:1;			/* Purge the class */ | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 	unsigned int backslash_is_escape:1;	/* On this database, the backslash is a native escape sequence */ | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 	unsigned int idlecheck;			/* Recheck the connection if it is idle for this long */ | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	AST_LIST_HEAD(, odbc_obj) odbc_obj; | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | AST_LIST_HEAD_STATIC(odbc_list, odbc_class); | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | static odbc_status odbc_obj_connect(struct odbc_obj *obj); | 
					
						
							|  |  |  | static odbc_status odbc_obj_disconnect(struct odbc_obj *obj); | 
					
						
							|  |  |  | static int odbc_register_class(struct odbc_class *class, int connect); | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data) | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res = 0, i, attempt; | 
					
						
							|  |  |  | 	SQLINTEGER nativeerror=0, numfields=0; | 
					
						
							|  |  |  | 	SQLSMALLINT diagbytes=0; | 
					
						
							|  |  |  | 	unsigned char state[10], diagnostic[256]; | 
					
						
							|  |  |  | 	SQLHSTMT stmt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (attempt = 0; attempt < 2; attempt++) { | 
					
						
							|  |  |  | 		/* This prepare callback may do more than just prepare -- it may also
 | 
					
						
							|  |  |  | 		 * bind parameters, bind results, etc.  The real key, here, is that | 
					
						
							|  |  |  | 		 * when we disconnect, all handles become invalid for most databases. | 
					
						
							|  |  |  | 		 * We must therefore redo everything when we establish a new | 
					
						
							|  |  |  | 		 * connection. */ | 
					
						
							|  |  |  | 		stmt = prepare_cb(obj, data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (stmt) { | 
					
						
							|  |  |  | 			res = SQLExecute(stmt); | 
					
						
							|  |  |  | 			if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) { | 
					
						
							|  |  |  | 				if (res == SQL_ERROR) { | 
					
						
							|  |  |  | 					SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes); | 
					
						
							| 
									
										
										
										
											2007-03-08 21:33:03 +00:00
										 |  |  | 					for (i = 0; i < numfields; i++) { | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 						SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes); | 
					
						
							|  |  |  | 						ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes); | 
					
						
							|  |  |  | 						if (i > 10) { | 
					
						
							|  |  |  | 							ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields); | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res); | 
					
						
							|  |  |  | 				SQLFreeHandle(SQL_HANDLE_STMT, stmt); | 
					
						
							| 
									
										
										
										
											2006-11-13 05:46:18 +00:00
										 |  |  | 				stmt = NULL; | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				obj->up = 0; | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 				/*
 | 
					
						
							|  |  |  | 				 * While this isn't the best way to try to correct an error, this won't automatically | 
					
						
							|  |  |  | 				 * fail when the statement handle invalidates. | 
					
						
							|  |  |  | 				 */ | 
					
						
							|  |  |  | 				/* XXX Actually, it might, if we're using a non-pooled connection. Possible race here. XXX */ | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 				odbc_obj_disconnect(obj); | 
					
						
							|  |  |  | 				odbc_obj_connect(obj); | 
					
						
							|  |  |  | 				continue; | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 			} else | 
					
						
							|  |  |  | 				obj->last_used = ast_tvnow(); | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "SQL Prepare failed.  Attempting a reconnect...\n"); | 
					
						
							|  |  |  | 			odbc_obj_disconnect(obj); | 
					
						
							|  |  |  | 			odbc_obj_connect(obj); | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return stmt; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt)  | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 	int res = 0, i; | 
					
						
							|  |  |  | 	SQLINTEGER nativeerror=0, numfields=0; | 
					
						
							|  |  |  | 	SQLSMALLINT diagbytes=0; | 
					
						
							|  |  |  | 	unsigned char state[10], diagnostic[256]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 	res = SQLExecute(stmt); | 
					
						
							| 
									
										
										
										
											2005-10-31 21:31:25 +00:00
										 |  |  | 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) { | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 		if (res == SQL_ERROR) { | 
					
						
							|  |  |  | 			SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes); | 
					
						
							| 
									
										
										
										
											2007-03-28 03:38:09 +00:00
										 |  |  | 			for (i = 0; i < numfields; i++) { | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 				SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes); | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes); | 
					
						
							|  |  |  | 				if (i > 10) { | 
					
						
							|  |  |  | 					ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | #if 0
 | 
					
						
							|  |  |  | 		/* This is a really bad method of trying to correct a dead connection.  It
 | 
					
						
							|  |  |  | 		 * only ever really worked with MySQL.  It will not work with any other | 
					
						
							|  |  |  | 		 * database, since most databases prepare their statements on the server, | 
					
						
							|  |  |  | 		 * and if you disconnect, you invalidate the statement handle.  Hence, if | 
					
						
							|  |  |  | 		 * you disconnect, you're going to fail anyway, whether you try to execute | 
					
						
							|  |  |  | 		 * a second time or not. | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res); | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 		ast_mutex_lock(&obj->lock); | 
					
						
							|  |  |  | 		obj->up = 0; | 
					
						
							|  |  |  | 		ast_mutex_unlock(&obj->lock); | 
					
						
							|  |  |  | 		odbc_obj_disconnect(obj); | 
					
						
							| 
									
										
										
										
											2005-03-23 05:56:32 +00:00
										 |  |  | 		odbc_obj_connect(obj); | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 		res = SQLExecute(stmt); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 	} else | 
					
						
							|  |  |  | 		obj->last_used = ast_tvnow(); | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | int ast_odbc_sanity_check(struct odbc_obj *obj)  | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *test_sql = "select 1"; | 
					
						
							|  |  |  | 	SQLHSTMT stmt; | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	if (obj->up) { | 
					
						
							|  |  |  | 		res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 		if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 			obj->up = 0; | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 			res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS); | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 			if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 				obj->up = 0; | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				res = SQLExecute(stmt); | 
					
						
							|  |  |  | 				if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 					obj->up = 0; | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		SQLFreeHandle (SQL_HANDLE_STMT, stmt); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	if (!obj->up) { /* Try to reconnect! */ | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n"); | 
					
						
							|  |  |  | 		odbc_obj_disconnect(obj); | 
					
						
							|  |  |  | 		odbc_obj_connect(obj); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return obj->up; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | static int load_odbc_config(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	static char *cfg = "res_odbc.conf"; | 
					
						
							|  |  |  | 	struct ast_config *config; | 
					
						
							|  |  |  | 	struct ast_variable *v; | 
					
						
							|  |  |  | 	char *cat, *dsn, *username, *password; | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 	int enabled, pooling, limit, bse; | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 	unsigned int idlecheck; | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	int connect = 0, res = 0; | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	struct odbc_class *new; | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-25 06:10:20 +00:00
										 |  |  | 	config = ast_config_load(cfg); | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 	if (!config) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) { | 
					
						
							|  |  |  | 		if (!strcasecmp(cat, "ENV")) { | 
					
						
							|  |  |  | 			for (v = ast_variable_browse(config, cat); v; v = v->next) { | 
					
						
							|  |  |  | 				setenv(v->name, v->value, 1); | 
					
						
							|  |  |  | 				ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			/* Reset all to defaults for each class of odbc connections */ | 
					
						
							|  |  |  | 			dsn = username = password = NULL; | 
					
						
							|  |  |  | 			enabled = 1; | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 			connect = idlecheck = 0; | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 			pooling = 0; | 
					
						
							|  |  |  | 			limit = 0; | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 			bse = 1; | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 			for (v = ast_variable_browse(config, cat); v; v = v->next) { | 
					
						
							|  |  |  | 				if (!strcasecmp(v->name, "pooling")) { | 
					
						
							| 
									
										
										
										
											2006-09-21 23:24:41 +00:00
										 |  |  | 					if (ast_true(v->value)) | 
					
						
							|  |  |  | 						pooling = 1; | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 				} else if (!strcasecmp(v->name, "limit")) { | 
					
						
							|  |  |  | 					sscanf(v->value, "%d", &limit); | 
					
						
							|  |  |  | 					if (ast_true(v->value) && !limit) { | 
					
						
							|  |  |  | 						ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Setting limit to 1023 for ODBC class '%s'.\n", v->value, cat); | 
					
						
							|  |  |  | 						limit = 1023; | 
					
						
							|  |  |  | 					} else if (ast_false(v->value)) { | 
					
						
							|  |  |  | 						ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat); | 
					
						
							|  |  |  | 						enabled = 0; | 
					
						
							|  |  |  | 						break; | 
					
						
							| 
									
										
										
										
											2004-10-23 07:03:01 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 				} else if (!strcasecmp(v->name, "idlecheck")) { | 
					
						
							|  |  |  | 					sscanf(v->value, "%d", &idlecheck); | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 				} else if (!strcasecmp(v->name, "enabled")) { | 
					
						
							|  |  |  | 					enabled = ast_true(v->value); | 
					
						
							|  |  |  | 				} else if (!strcasecmp(v->name, "pre-connect")) { | 
					
						
							|  |  |  | 					connect = ast_true(v->value); | 
					
						
							|  |  |  | 				} else if (!strcasecmp(v->name, "dsn")) { | 
					
						
							|  |  |  | 					dsn = v->value; | 
					
						
							|  |  |  | 				} else if (!strcasecmp(v->name, "username")) { | 
					
						
							|  |  |  | 					username = v->value; | 
					
						
							|  |  |  | 				} else if (!strcasecmp(v->name, "password")) { | 
					
						
							|  |  |  | 					password = v->value; | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 				} else if (!strcasecmp(v->name, "backslash_is_escape")) { | 
					
						
							|  |  |  | 					bse = ast_true(v->value); | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 			if (enabled && !ast_strlen_zero(dsn)) { | 
					
						
							|  |  |  | 				new = ast_calloc(1, sizeof(*new)); | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 				if (!new) { | 
					
						
							|  |  |  | 					res = -1; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 				if (cat) | 
					
						
							|  |  |  | 					ast_copy_string(new->name, cat, sizeof(new->name)); | 
					
						
							|  |  |  | 				if (dsn) | 
					
						
							|  |  |  | 					ast_copy_string(new->dsn, dsn, sizeof(new->dsn)); | 
					
						
							|  |  |  | 				if (username) | 
					
						
							|  |  |  | 					ast_copy_string(new->username, username, sizeof(new->username)); | 
					
						
							|  |  |  | 				if (password) | 
					
						
							|  |  |  | 					ast_copy_string(new->password, password, sizeof(new->password)); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 				SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env); | 
					
						
							|  |  |  | 				res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 				if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							|  |  |  | 					ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n"); | 
					
						
							|  |  |  | 					SQLFreeHandle(SQL_HANDLE_ENV, new->env); | 
					
						
							|  |  |  | 					return res; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 				if (pooling) { | 
					
						
							|  |  |  | 					new->haspool = pooling; | 
					
						
							|  |  |  | 					if (limit) { | 
					
						
							|  |  |  | 						new->limit = limit; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n"); | 
					
						
							|  |  |  | 						new->limit = 5; | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 				new->backslash_is_escape = bse ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 				new->idlecheck = idlecheck; | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 				odbc_register_class(new, connect); | 
					
						
							|  |  |  | 				ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn); | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 	ast_config_destroy(config); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2005-01-07 06:36:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int odbc_show_command(int fd, int argc, char **argv) | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	struct odbc_class *class; | 
					
						
							|  |  |  | 	struct odbc_obj *current; | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	AST_LIST_LOCK(&odbc_list); | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE(&odbc_list, class, list) { | 
					
						
							|  |  |  | 		if ((argc == 2) || (argc == 3 && !strcmp(argv[2], "all")) || (!strcmp(argv[2], class->name))) { | 
					
						
							|  |  |  | 			int count = 0; | 
					
						
							|  |  |  | 			ast_cli(fd, "Name: %s\nDSN: %s\n", class->name, class->dsn); | 
					
						
							| 
									
										
										
										
											2005-01-07 06:36:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 			if (class->haspool) { | 
					
						
							|  |  |  | 				ast_cli(fd, "Pooled: yes\nLimit: %d\nConnections in use: %d\n", class->limit, class->count); | 
					
						
							| 
									
										
										
										
											2005-01-07 06:36:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 				AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) { | 
					
						
							| 
									
										
										
										
											2007-11-05 16:20:13 +00:00
										 |  |  | 					ast_cli(fd, "  Connection %d: %s\n", ++count, current->used ? "in use" : current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected"); | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				/* Should only ever be one of these */ | 
					
						
							|  |  |  | 				AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) { | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | 					ast_cli(fd, "Pooled: no\nConnected: %s\n", current->up && ast_odbc_sanity_check(current) ? "yes" : "no"); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-01-07 06:36:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 				ast_cli(fd, "\n"); | 
					
						
							| 
									
										
										
										
											2005-01-07 06:36:02 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	AST_LIST_UNLOCK(&odbc_list); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-07 06:36:02 +00:00
										 |  |  | static char show_usage[] = | 
					
						
							| 
									
										
										
										
											2006-11-02 23:00:20 +00:00
										 |  |  | "Usage: odbc show [<class>]\n" | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | "       List settings of a particular ODBC class.\n" | 
					
						
							|  |  |  | "       or, if not specified, all classes.\n"; | 
					
						
							| 
									
										
										
										
											2005-01-07 06:36:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | static struct ast_cli_entry cli_odbc[] = { | 
					
						
							| 
									
										
										
										
											2006-11-02 23:00:20 +00:00
										 |  |  | 	{ { "odbc", "show", NULL }, | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	odbc_show_command, "List ODBC DSN(s)", | 
					
						
							| 
									
										
										
										
											2006-11-02 23:00:20 +00:00
										 |  |  | 	show_usage }, | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | static int odbc_register_class(struct odbc_class *class, int connect) | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	struct odbc_obj *obj; | 
					
						
							|  |  |  | 	if (class) { | 
					
						
							|  |  |  | 		AST_LIST_LOCK(&odbc_list); | 
					
						
							|  |  |  | 		AST_LIST_INSERT_HEAD(&odbc_list, class, list); | 
					
						
							|  |  |  | 		AST_LIST_UNLOCK(&odbc_list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (connect) { | 
					
						
							|  |  |  | 			/* Request and release builds a connection */ | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | 			obj = ast_odbc_request_obj(class->name, 0); | 
					
						
							| 
									
										
										
										
											2007-01-30 08:34:26 +00:00
										 |  |  | 			if (obj) | 
					
						
							|  |  |  | 				ast_odbc_release_obj(obj); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Attempted to register a NULL class?\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | void ast_odbc_release_obj(struct odbc_obj *obj) | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	/* For pooled connections, this frees the connection to be
 | 
					
						
							|  |  |  | 	 * reused.  For non-pooled connections, it does nothing. */ | 
					
						
							|  |  |  | 	obj->used = 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | int ast_odbc_backslash_is_escape(struct odbc_obj *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return obj->parent->backslash_is_escape; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | struct odbc_obj *ast_odbc_request_obj(const char *name, int check) | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct odbc_obj *obj = NULL; | 
					
						
							|  |  |  | 	struct odbc_class *class; | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	AST_LIST_LOCK(&odbc_list); | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE(&odbc_list, class, list) { | 
					
						
							|  |  |  | 		if (!strcmp(class->name, name)) | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2004-10-05 06:46:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	AST_LIST_UNLOCK(&odbc_list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!class) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AST_LIST_LOCK(&class->odbc_obj); | 
					
						
							|  |  |  | 	if (class->haspool) { | 
					
						
							|  |  |  | 		/* Recycle connections before building another */ | 
					
						
							|  |  |  | 		AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) { | 
					
						
							|  |  |  | 			if (! obj->used) { | 
					
						
							|  |  |  | 				obj->used = 1; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 		if (!obj && (class->count < class->limit)) { | 
					
						
							|  |  |  | 			class->count++; | 
					
						
							|  |  |  | 			obj = ast_calloc(1, sizeof(*obj)); | 
					
						
							|  |  |  | 			if (!obj) { | 
					
						
							|  |  |  | 				AST_LIST_UNLOCK(&class->odbc_obj); | 
					
						
							|  |  |  | 				return NULL; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			ast_mutex_init(&obj->lock); | 
					
						
							|  |  |  | 			obj->parent = class; | 
					
						
							| 
									
										
										
										
											2007-08-11 05:23:04 +00:00
										 |  |  | 			if (odbc_obj_connect(obj) == ODBC_FAIL) { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "Failed to connect to %s\n", name); | 
					
						
							|  |  |  | 				ast_mutex_destroy(&obj->lock); | 
					
						
							|  |  |  | 				free(obj); | 
					
						
							|  |  |  | 				obj = NULL; | 
					
						
							|  |  |  | 				class->count--; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				obj->used = 1; | 
					
						
							|  |  |  | 				AST_LIST_INSERT_TAIL(&class->odbc_obj, obj, list); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		/* Non-pooled connection: multiple modules can use the same connection. */ | 
					
						
							|  |  |  | 		AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) { | 
					
						
							|  |  |  | 			/* Non-pooled connection: if there is an entry, return it */ | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 		if (!obj) { | 
					
						
							|  |  |  | 			/* No entry: build one */ | 
					
						
							|  |  |  | 			obj = ast_calloc(1, sizeof(*obj)); | 
					
						
							|  |  |  | 			if (!obj) { | 
					
						
							|  |  |  | 				AST_LIST_UNLOCK(&class->odbc_obj); | 
					
						
							|  |  |  | 				return NULL; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			ast_mutex_init(&obj->lock); | 
					
						
							|  |  |  | 			obj->parent = class; | 
					
						
							|  |  |  | 			if (odbc_obj_connect(obj) == ODBC_FAIL) { | 
					
						
							| 
									
										
										
										
											2007-01-30 08:34:26 +00:00
										 |  |  | 				ast_log(LOG_WARNING, "Failed to connect to %s\n", name); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 				ast_mutex_destroy(&obj->lock); | 
					
						
							|  |  |  | 				free(obj); | 
					
						
							| 
									
										
										
										
											2007-01-30 08:34:26 +00:00
										 |  |  | 				obj = NULL; | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				AST_LIST_INSERT_HEAD(&class->odbc_obj, obj, list); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-02-02 16:09:01 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	AST_LIST_UNLOCK(&class->odbc_obj); | 
					
						
							| 
									
										
										
										
											2006-02-02 16:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	if (obj && check) { | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | 		ast_odbc_sanity_check(obj); | 
					
						
							| 
									
										
										
										
											2008-01-23 04:20:15 +00:00
										 |  |  | 	} else if (obj && obj->parent->idlecheck > 0 && ast_tvdiff_ms(ast_tvnow(), obj->last_used) / 1000 > obj->parent->idlecheck) | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 		odbc_obj_connect(obj); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	return obj; | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | static odbc_status odbc_obj_disconnect(struct odbc_obj *obj) | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	ast_mutex_lock(&obj->lock); | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 	res = SQLDisconnect(obj->con); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	if (res == ODBC_SUCCESS) { | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn); | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n", | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 		obj->parent->name, obj->parent->dsn); | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 	obj->up = 0; | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	ast_mutex_unlock(&obj->lock); | 
					
						
							|  |  |  | 	return ODBC_SUCCESS; | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | static odbc_status odbc_obj_connect(struct odbc_obj *obj) | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2005-09-02 19:29:18 +00:00
										 |  |  | 	SQLINTEGER err; | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	short int mlen; | 
					
						
							| 
									
										
										
										
											2005-11-09 02:01:12 +00:00
										 |  |  | 	unsigned char msg[200], stat[10]; | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | #ifdef NEEDTRACE
 | 
					
						
							|  |  |  | 	SQLINTEGER enable = 1; | 
					
						
							|  |  |  | 	char *tracefile = "/tmp/odbc.trace"; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	ast_mutex_lock(&obj->lock); | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &obj->con); | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res); | 
					
						
							|  |  |  | 		ast_mutex_unlock(&obj->lock); | 
					
						
							|  |  |  | 		return ODBC_FAIL; | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0); | 
					
						
							| 
									
										
										
										
											2008-01-12 00:05:08 +00:00
										 |  |  | 	SQLSetConnectAttr(obj->con, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER *) 10, 0); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | #ifdef NEEDTRACE
 | 
					
						
							|  |  |  | 	SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER); | 
					
						
							|  |  |  | 	SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile)); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (obj->up) { | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 		odbc_obj_disconnect(obj); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 		ast_log(LOG_NOTICE, "Re-connecting %s\n", obj->parent->name); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_log(LOG_NOTICE, "Connecting %s\n", obj->parent->name); | 
					
						
							| 
									
										
										
										
											2005-02-17 16:31:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	res = SQLConnect(obj->con, | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 		   (SQLCHAR *) obj->parent->dsn, SQL_NTS, | 
					
						
							|  |  |  | 		   (SQLCHAR *) obj->parent->username, SQL_NTS, | 
					
						
							|  |  |  | 		   (SQLCHAR *) obj->parent->password, SQL_NTS); | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							|  |  |  | 		SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen); | 
					
						
							| 
									
										
										
										
											2004-10-31 23:17:11 +00:00
										 |  |  | 		ast_mutex_unlock(&obj->lock); | 
					
						
							| 
									
										
										
										
											2005-09-02 19:29:18 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg); | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 		return ODBC_FAIL; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 		ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->parent->name, obj->parent->dsn); | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 		obj->up = 1; | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 		obj->last_used = ast_tvnow(); | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	ast_mutex_unlock(&obj->lock); | 
					
						
							|  |  |  | 	return ODBC_SUCCESS; | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int reload(void) | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	static char *cfg = "res_odbc.conf"; | 
					
						
							|  |  |  | 	struct ast_config *config; | 
					
						
							|  |  |  | 	struct ast_variable *v; | 
					
						
							|  |  |  | 	char *cat, *dsn, *username, *password; | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 	int enabled, pooling, limit, bse; | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 	unsigned int idlecheck; | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 	int connect = 0, res = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct odbc_class *new, *class; | 
					
						
							|  |  |  | 	struct odbc_obj *current; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* First, mark all to be purged */ | 
					
						
							|  |  |  | 	AST_LIST_LOCK(&odbc_list); | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE(&odbc_list, class, list) { | 
					
						
							|  |  |  | 		class->delme = 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	config = ast_config_load(cfg); | 
					
						
							|  |  |  | 	if (config) { | 
					
						
							|  |  |  | 		for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) { | 
					
						
							|  |  |  | 			if (!strcasecmp(cat, "ENV")) { | 
					
						
							|  |  |  | 				for (v = ast_variable_browse(config, cat); v; v = v->next) { | 
					
						
							|  |  |  | 					setenv(v->name, v->value, 1); | 
					
						
							|  |  |  | 					ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				/* Reset all to defaults for each class of odbc connections */ | 
					
						
							|  |  |  | 				dsn = username = password = NULL; | 
					
						
							|  |  |  | 				enabled = 1; | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 				connect = idlecheck = 0; | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 				pooling = 0; | 
					
						
							|  |  |  | 				limit = 0; | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 				bse = 1; | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 				for (v = ast_variable_browse(config, cat); v; v = v->next) { | 
					
						
							|  |  |  | 					if (!strcasecmp(v->name, "pooling")) { | 
					
						
							|  |  |  | 						pooling = 1; | 
					
						
							|  |  |  | 					} else if (!strcasecmp(v->name, "limit")) { | 
					
						
							|  |  |  | 						sscanf(v->value, "%d", &limit); | 
					
						
							|  |  |  | 						if (ast_true(v->value) && !limit) { | 
					
						
							|  |  |  | 							ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Setting limit to 1023 for ODBC class '%s'.\n", v->value, cat); | 
					
						
							|  |  |  | 							limit = 1023; | 
					
						
							|  |  |  | 						} else if (ast_false(v->value)) { | 
					
						
							|  |  |  | 							ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat); | 
					
						
							|  |  |  | 							enabled = 0; | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 					} else if (!strcasecmp(v->name, "idlecheck")) { | 
					
						
							|  |  |  | 						sscanf(v->value, "%ud", &idlecheck); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 					} else if (!strcasecmp(v->name, "enabled")) { | 
					
						
							|  |  |  | 						enabled = ast_true(v->value); | 
					
						
							|  |  |  | 					} else if (!strcasecmp(v->name, "pre-connect")) { | 
					
						
							|  |  |  | 						connect = ast_true(v->value); | 
					
						
							|  |  |  | 					} else if (!strcasecmp(v->name, "dsn")) { | 
					
						
							|  |  |  | 						dsn = v->value; | 
					
						
							|  |  |  | 					} else if (!strcasecmp(v->name, "username")) { | 
					
						
							|  |  |  | 						username = v->value; | 
					
						
							|  |  |  | 					} else if (!strcasecmp(v->name, "password")) { | 
					
						
							|  |  |  | 						password = v->value; | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 					} else if (!strcasecmp(v->name, "backslash_is_escape")) { | 
					
						
							|  |  |  | 						bse = ast_true(v->value); | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (enabled && !ast_strlen_zero(dsn)) { | 
					
						
							|  |  |  | 					/* First, check the list to see if it already exists */ | 
					
						
							|  |  |  | 					AST_LIST_TRAVERSE(&odbc_list, class, list) { | 
					
						
							|  |  |  | 						if (!strcmp(class->name, cat)) { | 
					
						
							|  |  |  | 							class->delme = 0; | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (class) { | 
					
						
							|  |  |  | 						new = class; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						new = ast_calloc(1, sizeof(*new)); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (!new) { | 
					
						
							|  |  |  | 						res = -1; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (cat) | 
					
						
							|  |  |  | 						ast_copy_string(new->name, cat, sizeof(new->name)); | 
					
						
							|  |  |  | 					if (dsn) | 
					
						
							|  |  |  | 						ast_copy_string(new->dsn, dsn, sizeof(new->dsn)); | 
					
						
							|  |  |  | 					if (username) | 
					
						
							|  |  |  | 						ast_copy_string(new->username, username, sizeof(new->username)); | 
					
						
							|  |  |  | 					if (password) | 
					
						
							|  |  |  | 						ast_copy_string(new->password, password, sizeof(new->password)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (!class) { | 
					
						
							|  |  |  | 						SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env); | 
					
						
							|  |  |  | 						res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							|  |  |  | 							ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n"); | 
					
						
							|  |  |  | 							SQLFreeHandle(SQL_HANDLE_ENV, new->env); | 
					
						
							|  |  |  | 							AST_LIST_UNLOCK(&odbc_list); | 
					
						
							|  |  |  | 							return res; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (pooling) { | 
					
						
							|  |  |  | 						new->haspool = pooling; | 
					
						
							|  |  |  | 						if (limit) { | 
					
						
							|  |  |  | 							new->limit = limit; | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n"); | 
					
						
							|  |  |  | 							new->limit = 5; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 					new->backslash_is_escape = bse; | 
					
						
							| 
									
										
										
										
											2008-01-21 18:11:07 +00:00
										 |  |  | 					new->idlecheck = idlecheck; | 
					
						
							| 
									
										
										
										
											2007-11-25 17:17:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | 					if (class) { | 
					
						
							|  |  |  | 						ast_log(LOG_NOTICE, "Refreshing ODBC class '%s' dsn->[%s]\n", cat, dsn); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						odbc_register_class(new, connect); | 
					
						
							|  |  |  | 						ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ast_config_destroy(config); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Purge classes that we know can go away (pooled with 0, only) */ | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE_SAFE_BEGIN(&odbc_list, class, list) { | 
					
						
							|  |  |  | 		if (class->delme && class->haspool && class->count == 0) { | 
					
						
							|  |  |  | 			AST_LIST_TRAVERSE_SAFE_BEGIN(&(class->odbc_obj), current, list) { | 
					
						
							|  |  |  | 				AST_LIST_REMOVE_CURRENT(&(class->odbc_obj), list); | 
					
						
							|  |  |  | 				odbc_obj_disconnect(current); | 
					
						
							|  |  |  | 				ast_mutex_destroy(¤t->lock); | 
					
						
							|  |  |  | 				free(current); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			AST_LIST_TRAVERSE_SAFE_END; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			AST_LIST_REMOVE_CURRENT(&odbc_list, list); | 
					
						
							|  |  |  | 			free(class); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE_SAFE_END; | 
					
						
							|  |  |  | 	AST_LIST_UNLOCK(&odbc_list); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2006-04-18 18:16:32 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	/* Prohibit unloading */ | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 	if(load_odbc_config() == -1) | 
					
						
							|  |  |  | 		return AST_MODULE_LOAD_DECLINE; | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	ast_cli_register_multiple(cli_odbc, sizeof(cli_odbc) / sizeof(struct ast_cli_entry)); | 
					
						
							| 
									
										
										
										
											2004-07-08 19:58:26 +00:00
										 |  |  | 	ast_log(LOG_NOTICE, "res_odbc loaded.\n"); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2004-06-11 00:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "ODBC Resource", | 
					
						
							|  |  |  | 		.load = load_module, | 
					
						
							|  |  |  | 		.unload = unload_module, | 
					
						
							|  |  |  | 		.reload = reload, | 
					
						
							|  |  |  | 	       ); |