From a5e941b56ea72044c4dadd9bdb8221cb919bea85 Mon Sep 17 00:00:00 2001 From: markster Date: Fri, 11 Jun 2004 00:12:35 +0000 Subject: Merge res_odbc and res_config git-svn-id: http://svn.digium.com/svn/asterisk/trunk@3186 f38db490-d61c-443f-a65b-d21fe96a405b --- asterisk.c | 8 + config.c | 342 +++++++++++++++++++++++++--- configs/extconfig.conf.sample | 6 + configs/res_config_odbc.conf.sample | 3 + configs/res_odbc.conf.sample | 20 ++ include/asterisk/config_pvt.h | 88 ++++++++ include/asterisk/res_odbc.h | 51 +++++ loader.c | 2 + res/Makefile | 5 + res/res_config_odbc.c | 253 +++++++++++++++++++++ res/res_odbc.c | 430 ++++++++++++++++++++++++++++++++++++ 11 files changed, 1176 insertions(+), 32 deletions(-) create mode 100755 configs/extconfig.conf.sample create mode 100755 configs/res_config_odbc.conf.sample create mode 100755 configs/res_odbc.conf.sample create mode 100755 include/asterisk/config_pvt.h create mode 100755 include/asterisk/res_odbc.h create mode 100755 res/res_config_odbc.c create mode 100755 res/res_odbc.c diff --git a/asterisk.c b/asterisk.c index 5894da1a8..76636ae43 100755 --- a/asterisk.c +++ b/asterisk.c @@ -48,6 +48,7 @@ #include "editline/histedit.h" #include "asterisk.h" #include +#include #if defined(__FreeBSD__) #include @@ -1556,6 +1557,13 @@ int main(int argc, char *argv[]) ast_verbose("[ Reading Master Configuration ]"); ast_readconfig(); + if (option_console && !option_verbose) + ast_verbose("[ Initializing Custom Configuration Options]"); + /* custom config setup */ + register_config_cli(); + read_ast_cust_config(); + + if (option_console) { if (el_hist == NULL || el == NULL) ast_el_initialize(); diff --git a/config.c b/config.c index 954d09509..cb4ae06ab 100755 --- a/config.c +++ b/config.c @@ -18,41 +18,26 @@ #include #include #include +#include +#include +#include #include #include #include #include "asterisk.h" #include "astconf.h" -#define MAX_INCLUDE_LEVEL 10 -struct ast_category { - char name[80]; - struct ast_variable *root; - struct ast_category *next; -#ifdef PRESERVE_COMMENTS - struct ast_comment *precomments; - struct ast_comment *sameline; -#endif -}; - -struct ast_config { - /* Maybe this structure isn't necessary but we'll keep it - for now */ - struct ast_category *root; - struct ast_category *prev; -#ifdef PRESERVE_COMMENTS - struct ast_comment *trailingcomments; -#endif -}; - -#ifdef PRESERVE_COMMENTS -struct ast_comment_struct -{ - struct ast_comment *root; - struct ast_comment *prev; -}; +static int ast_cust_config=0; +struct ast_config *(*global_load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int +#ifdef PRESERVE_COMMENTS +,struct ast_comment_struct * #endif +); + +AST_MUTEX_DEFINE_STATIC(ast_cust_config_lock); +static struct ast_config_reg *ast_cust_config_list; +static char *config_conf_file = "extconfig.conf"; static char *strip(char *buf) { @@ -454,6 +439,14 @@ static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, stru { char *c; char *cur; + char *arg=NULL; + struct ast_config_reg *reg=NULL; + struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int +#ifdef PRESERVE_COMMENTS +,struct ast_comment_struct * +#endif +); + struct ast_variable *v; #ifdef PRESERVE_COMMENTS struct ast_comment *com = NULL; @@ -533,18 +526,45 @@ static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, stru else break; } + + if((c = strchr(cur,':'))) { + *c = '\0'; + *c++; + arg = c; + } + if (includelevel < MAX_INCLUDE_LEVEL) { - __ast_load(cur, tmp, _tmpc, _last, includelevel + 1 + if(arg && cur) { + load_func = NULL; + if(ast_cust_config_list) + reg = get_ast_cust_config(cur); + if(reg && reg->func) + load_func = reg->func; + if(load_func) { + ast_log(LOG_NOTICE,"External Include '%s' via '%s' config engine\n",arg,cur); + load_func(configfile,tmp, _tmpc, _last, includelevel +#ifdef PRESERVE_COMMENTS + ,&acs +#endif + ); + } + else + ast_log(LOG_WARNING,"Cant Find Registered Config Engine [%s] for [%s]\n",cur,arg); + } + else { + __ast_load(cur, tmp, _tmpc, _last, includelevel + 1 #ifdef PRESERVE_COMMENTS - ,acs + ,acs #endif - ); - } else + ); + } + } else ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel); } else ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile); /* Strip off leading and trailing "'s and <>'s */ - } else + } + else ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile); } else { /* Just a line (variable = value) */ @@ -712,6 +732,44 @@ static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, s FILE *f; int lineno=0; int master=0; + struct ast_config_reg *reg=NULL; + struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int +#ifdef PRESERVE_COMMENTS +,struct ast_comment_struct * +#endif +); + + + load_func=NULL; + if(strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) { + if(global_load_func) + load_func = global_load_func; + else { + reg = get_ast_cust_config_keyword(configfile); + if(reg && reg->func) + load_func = reg->func; + else { + reg = get_ast_cust_config_keyword("global"); + if(reg && reg->func) + global_load_func = load_func = reg->func; + } + } + + if(load_func) { + ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global"); + tmp = load_func(configfile,tmp, _tmpc, _last, includelevel +#ifdef PRESERVE_COMMENTS +,&acs +#endif +); + + if(tmp) + return tmp; + } + } + + + if (configfile[0] == '/') { strncpy(fn, configfile, sizeof(fn)-1); @@ -770,13 +828,97 @@ static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, s return tmp; } +struct ast_config_reg *get_ast_cust_config_keyword(char *name) { + struct ast_config_reg *reg,*ret=NULL; + int x=0; + ast_mutex_lock(&ast_cust_config_lock); + for(reg=ast_cust_config_list;reg && !ret;reg=reg->next) + for(x=0;xkeycount && !ret ;x++) + if(!strcmp(reg->keywords[x],name)) + ret=reg; + ast_mutex_unlock(&ast_cust_config_lock); + return ret; +} + +struct ast_config_reg *get_ast_cust_config(char *name) { + struct ast_config_reg *ptr=NULL; + ast_mutex_lock(&ast_cust_config_lock); + for(ptr=ast_cust_config_list;ptr;ptr=ptr->next) { + if(!strcmp(name,ptr->name)) + break; + } + ast_mutex_unlock(&ast_cust_config_lock); + return ptr; +} + +void ast_config_destroy_all(void) { + struct ast_config_reg *key; + ast_mutex_lock(&ast_cust_config_lock); + for(key=ast_cust_config_list;key;key=key->next) { + ast_config_deregister(key); + } + ast_cust_config_list = NULL; + ast_mutex_unlock(&ast_cust_config_lock); +} + +struct ast_config_reg *get_config_registrations(void) { + return ast_cust_config_list; +} + +int ast_config_register(struct ast_config_reg *new) { + struct ast_config_reg *ptr; + ast_mutex_lock(&ast_cust_config_lock); + new->keycount = 0; + if(!ast_cust_config_list) + ast_cust_config_list = new; + else { + for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next); + ptr->next = new; + } + ast_mutex_unlock(&ast_cust_config_lock); + ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name); + return 1; +} + +int ast_config_deregister(struct ast_config_reg *del) { + struct ast_config_reg *ptr=NULL,*last=NULL; + ast_mutex_lock(&ast_cust_config_lock); + for(ptr=ast_cust_config_list;ptr;ptr=ptr->next) { + if(ptr == del) { + if(last && ptr->next) { + last->next = ptr->next; + } + else if(last && ! ptr->next) { + last->next = NULL; + } + else if(! last && ptr->next) { + ast_cust_config_list = ptr->next; + } + else if(! last && ! ptr->next) { + ast_cust_config_list = NULL; + } + } + last = ptr; + } + ast_mutex_unlock(&ast_cust_config_lock); + return 0; +} + +int ast_cust_config_active(void) { + return (ast_cust_config >0) ? 1 : 0; +} + struct ast_config *ast_load(char *configfile) { struct ast_category *tmpc=NULL; struct ast_variable *last = NULL; + + #ifdef PRESERVE_COMMENTS struct ast_comment_struct acs = { NULL, NULL }; #endif + + return __ast_load(configfile, NULL, &tmpc, &last, 0 #ifdef PRESERVE_COMMENTS ,&acs @@ -815,3 +957,139 @@ char *ast_category_browse(struct ast_config *config, char *prev) } return NULL; } + + +struct ast_config *ast_new_config(void) { + struct ast_config *config; + config = malloc(sizeof(struct ast_config)); + memset(config,0,sizeof(struct ast_config)); + return config; +} + + + +struct ast_category *ast_new_category(char *name) { + struct ast_category *category; + category = malloc(sizeof(struct ast_category)); + if(category) { + memset(category,0,sizeof(struct ast_category)); + strncpy(category->name,name,sizeof(category->name)); + } + return category; +} + + +struct ast_variable *ast_new_variable(char *name,char *value) { + struct ast_variable *variable; + variable = malloc(sizeof(struct ast_variable)); + if(variable) { + memset(variable,0,sizeof(struct ast_variable)); + variable->object=0; + variable->name = malloc(strlen(name)+1); + if(variable->name) { + strcpy(variable->name,name); + variable->value = malloc(strlen(value)+1); + if(variable->value) { + strcpy(variable->value,value); + } + else { + free(variable->name); + variable->name = NULL; + } + } + } + if(!variable->value) { + free(variable); + variable = NULL; + } + + return variable; +} + +int ast_cust_config_register(struct ast_config_reg *new) { + ast_config_register(new); + read_ast_cust_config(); + return 1; +} +int ast_cust_config_deregister(struct ast_config_reg *new) { + ast_config_deregister(new); + read_ast_cust_config(); + return 1; +} + +static void clear_cust_keywords(void) { + struct ast_config_reg *key; + int x; + ast_mutex_lock(&ast_cust_config_lock); + for(key=get_config_registrations();key;key=key->next) { + for(x=0;xkeycount;x++) { + key->keywords[x][0] = '\0'; + } + key->keycount=0; + } + ast_mutex_unlock(&ast_cust_config_lock); +} + +static int config_command(int fd, int argc, char **argv) { + struct ast_config_reg *key; + int x; + + ast_cli(fd,"\n\n"); + ast_mutex_lock(&ast_cust_config_lock); + for(key=get_config_registrations();key;key=key->next) { + ast_cli(fd,"\nConfig Engine: %s\n",key->name); + for(x=0;xkeycount;x++) + ast_cli(fd,"===>%s\n",key->keywords[x]); + } + ast_mutex_unlock(&ast_cust_config_lock); + ast_cli(fd,"\n\n"); + + return 0; +} + +static struct ast_cli_entry config_command_struct = { + { "show","config","handles", NULL }, config_command, + "Show Config Handles", NULL }; + +int register_config_cli() { + return ast_cli_register(&config_command_struct); +} + +int read_ast_cust_config(void) { + char *cfg = config_conf_file; + struct ast_config *config; + struct ast_variable *v; + struct ast_config_reg *ptr; + struct ast_config_reg *test = NULL; + clear_cust_keywords(); + config = ast_load(cfg); + if(config) { + for(v = ast_variable_browse(config,"settings");v;v=v->next) { + + ptr = get_ast_cust_config(v->value); + if(ptr) { + if(ptr->keycount >= CONFIG_KEYWORD_ARRAYLEN) { + ast_log(LOG_WARNING,"Max Number of Bindings exceeded for %s->%s %d/%d\n",v->name,v->value,ptr->keycount,CONFIG_KEYWORD_ARRAYLEN); + } + else { + if(strcmp(v->name,config_conf_file) && strcmp(v->name,"asterisk.conf")) { + if(!(test = get_ast_cust_config_keyword(v->name))) { + ast_log(LOG_NOTICE,"Binding: %s to %s\n",v->name,v->value); + strncpy(ptr->keywords[ptr->keycount],v->name,sizeof(ptr->keywords[ptr->keycount])); + ptr->keycount++; + } + } + else { + ast_log(LOG_WARNING,"Cannot bind %s, Permission Denied\n",v->name); + } + } + } + } + + ast_destroy(config); + } + else + ast_log(LOG_WARNING,"config loader has no config file so nevermind.\n"); + + return 0; +} diff --git a/configs/extconfig.conf.sample b/configs/extconfig.conf.sample new file mode 100755 index 000000000..612490e57 --- /dev/null +++ b/configs/extconfig.conf.sample @@ -0,0 +1,6 @@ +[settings] + +;uncomment to load queues.conf via the db engine. +;queues.conf => config_odbc + + diff --git a/configs/res_config_odbc.conf.sample b/configs/res_config_odbc.conf.sample new file mode 100755 index 000000000..f11cf9481 --- /dev/null +++ b/configs/res_config_odbc.conf.sample @@ -0,0 +1,3 @@ +[settings] +table => ast_config +connection => mysql1 diff --git a/configs/res_odbc.conf.sample b/configs/res_odbc.conf.sample new file mode 100755 index 000000000..144eb6a95 --- /dev/null +++ b/configs/res_odbc.conf.sample @@ -0,0 +1,20 @@ +;;; odbc setup file + +[mysql1] +dsn => MySQL-asterisk +username => myuser +password => mypass +pre-connect => yes + + +[mysql2] +dsn => MySQL-asterisk +username => myuser +password => mypass +pre-connect => yes + + + + + + diff --git a/include/asterisk/config_pvt.h b/include/asterisk/config_pvt.h new file mode 100755 index 000000000..f87189d31 --- /dev/null +++ b/include/asterisk/config_pvt.h @@ -0,0 +1,88 @@ +#ifndef _ASTERISK_CONFIG_PVT_H +#define _ASTERISK_CONFIG_PVT_H +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif + +#define CONFIG_KEYWORD_STRLEN 128 +#define CONFIG_KEYWORD_ARRAYLEN 512 +#include + +#define MAX_INCLUDE_LEVEL 10 + +struct ast_category { + char name[80]; + struct ast_variable *root; + struct ast_category *next; +#ifdef PRESERVE_COMMENTS + struct ast_comment *precomments; + struct ast_comment *sameline; +#endif +}; + +struct ast_config { + /* Maybe this structure isn't necessary but we'll keep it + for now */ + struct ast_category *root; + struct ast_category *prev; +#ifdef PRESERVE_COMMENTS + struct ast_comment *trailingcomments; +#endif +}; + +#ifdef PRESERVE_COMMENTS +struct ast_comment_struct +{ + struct ast_comment *root; + struct ast_comment *prev; +}; +#endif + +struct ast_category; + +struct ast_config_reg { + char name[CONFIG_KEYWORD_STRLEN]; + struct ast_config *(*func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int +#ifdef PRESERVE_COMMENTS +,struct ast_comment_struct * +#endif +); + char keywords[CONFIG_KEYWORD_STRLEN][CONFIG_KEYWORD_ARRAYLEN]; + int keycount; + struct ast_config_reg *next; +}; + + + +int ast_config_register(struct ast_config_reg *new); +int ast_config_deregister(struct ast_config_reg *del); +void ast_cust_config_on(void); +void ast_cust_config_off(void); +int ast_cust_config_active(void); +struct ast_config_reg *get_config_registrations(void); +struct ast_config_reg *get_ast_cust_config(char *name); +struct ast_config_reg *get_ast_cust_config_keyword(char *name); +void ast_config_destroy_all(void); + + +int ast_category_delete(struct ast_config *cfg, char *category); +int ast_variable_delete(struct ast_config *cfg, char *category, char *variable, char *value); +int ast_save(char *filename, struct ast_config *cfg, char *generator); + +struct ast_config *ast_new_config(void); +struct ast_category *ast_new_category(char *name); +struct ast_variable *ast_new_variable(char *name,char *value); +int ast_cust_config_register(struct ast_config_reg *new); +int ast_cust_config_deregister(struct ast_config_reg *new); +int register_config_cli(void); +int read_ast_cust_config(void); + + + + +#if defined(__cplusplus) || defined(c_plusplus) +} +#endif + + +#endif diff --git a/include/asterisk/res_odbc.h b/include/asterisk/res_odbc.h new file mode 100755 index 000000000..1a68acb98 --- /dev/null +++ b/include/asterisk/res_odbc.h @@ -0,0 +1,51 @@ +/* + * Asterisk -- A telephony toolkit for Linux. + * + * Copyright (C) 1999, Mark Spencer + * + * Mark Spencer + * + * res_odbc.h + * Copyright (C) 2004 Anthony Minessale II + */ + +#ifndef _RES_ODBC_H +#define _RES_ODBC_H + +#include +#include +#include + + + + +typedef struct odbc_obj odbc_obj; + +typedef enum { ODBC_SUCCESS=0,ODBC_FAIL=-1} odbc_status; + +struct odbc_obj { + char *name; + char *dsn; + char *username; + char *password; + SQLHENV env; /* ODBC Environment */ + SQLHDBC con; /* ODBC Connection Handle */ + SQLHSTMT stmt; /* ODBC Statement Handle */ + ast_mutex_t lock; + int up; + +}; + + + + +/* functions */ +odbc_obj *new_odbc_obj(char *name,char *dsn,char *username, char *password); +odbc_status odbc_obj_connect(odbc_obj *obj); +odbc_status odbc_obj_disconnect(odbc_obj *obj); +void destroy_obdc_obj(odbc_obj **obj); +int register_odbc_obj(char *name,odbc_obj *obj); +odbc_obj *fetch_odbc_obj(char *name); +int odbc_dump_fd(int fd,odbc_obj *obj); + +#endif diff --git a/loader.c b/loader.c index b05cdf766..d6108f8f6 100755 --- a/loader.c +++ b/loader.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -156,6 +157,7 @@ void ast_module_reload(void) ast_verbose("The previous reload command didn't finish yet\n"); return; } + read_ast_cust_config(); reload_manager(); ast_enum_reload(); ast_rtp_reload(); diff --git a/res/Makefile b/res/Makefile index 32e42a2e2..fa6e3cf81 100755 --- a/res/Makefile +++ b/res/Makefile @@ -12,6 +12,8 @@ # MODS=res_adsi.so res_parking.so res_crypto.so res_musiconhold.so res_indications.so res_monitor.so +MODS+=$(shell if [ -f "/usr/include/odbcinst.h" ]; then echo "res_odbc.so res_config_odbc.so"; fi) +MODS+=$(shell if [ -f "/usr/local/include/odbcinst.h" ]; then echo "res_odbc.so res_config_odbc.so"; fi) CRYPTO_LIBS=-lssl -lcrypto @@ -34,6 +36,9 @@ res_crypto.so: res_crypto.o clean: rm -f *.so *.o .depend +res_odbc.so: res_odbc.o + $(CC) $(SOLINK) -o $@ $< -lodbc + %.so : %.o $(CC) $(SOLINK) -o $@ $< diff --git a/res/res_config_odbc.c b/res/res_config_odbc.c new file mode 100755 index 000000000..ffaccb8cb --- /dev/null +++ b/res/res_config_odbc.c @@ -0,0 +1,253 @@ +/* + * Asterisk -- A telephony toolkit for Linux. + * + * Copyright (C) 1999, Mark Spencer + * + * Mark Spencer + * + * res_config_odbc.c + * Copyright (C) 2004 Anthony Minessale II + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +static char *tdesc = "Odbc Configuration"; +static struct ast_config_reg reg1; + + +STANDARD_LOCAL_USER; + +LOCAL_USER_DECL; + + +static struct ast_config *config_odbc(char *file, struct ast_config *new_config_s,struct ast_category **new_cat_p,struct ast_variable **new_v_p,int recur +#ifdef PRESERVE_COMMENTS + ,struct ast_comment_struct *acs +#endif + + ) { + + + + + struct ast_config *config,*new; + struct ast_variable *v,*cur_v,*new_v; + struct ast_category *cur_cat,*new_cat; + char table[128]; + char connection[128]; + int configured=0, res=0; + odbc_obj *obj; + SQLINTEGER err,commented,cat_metric,var_metric; + SQLBIGINT id; + char sql[255],filename[128],category[128],var_name[128],var_val[128]; + SQLSMALLINT rowcount; + SQLHSTMT stmt; + char last[80]; + int cat_started=0; + int var_started=0; + + + if(new_config_s) { + new = new_config_s; + cat_started++; + } + else + new = ast_new_config(); + + last[0] = '\0'; + + if(!file || !strcmp(file,"res_config_odbc.conf")) + return NULL; // cant configure myself with myself ! + + config = ast_load("res_config_odbc.conf"); + + if(config) { + for(v = ast_variable_browse(config,"settings");v;v=v->next) { + if(!strcmp(v->name,"table")) { + strncpy(table,v->value,sizeof(table)); + configured++; + } + else if(!strcmp(v->name,"connection")) { + strncpy(connection,v->value,sizeof(connection)); + configured++; + } + } + ast_destroy(config); + } + + if(configured < 2) + return NULL; + + obj = fetch_odbc_obj(connection); + if(!obj) + return NULL; + + + + + + res=SQLAllocHandle(SQL_HANDLE_STMT,obj->con, &stmt); + + SQLBindCol(stmt,1,SQL_C_ULONG,&id,sizeof(id),&err); + SQLBindCol(stmt,2,SQL_C_ULONG,&cat_metric,sizeof(cat_metric),&err); + SQLBindCol(stmt,3,SQL_C_ULONG,&var_metric,sizeof(var_metric),&err); + SQLBindCol(stmt,4,SQL_C_ULONG,&commented,sizeof(commented),&err); + SQLBindCol(stmt,5,SQL_C_CHAR,&filename,sizeof(filename),&err); + SQLBindCol(stmt,6,SQL_C_CHAR,&category,sizeof(category),&err); + SQLBindCol(stmt,7,SQL_C_CHAR,&var_name,sizeof(var_name),&err); + SQLBindCol(stmt,8,SQL_C_CHAR,&var_val,sizeof(var_val),&err); + + sprintf(sql,"select * from %s where filename='%s' and commented=0 order by filename,cat_metric,var_metric,id",table,file); + res = SQLExecDirect(stmt,sql,SQL_NTS); + + if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + ast_log(LOG_WARNING,"SQL select error!\n[%s]\n\n",sql); + return NULL; + } + + res=SQLNumResultCols(stmt,&rowcount); + + if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + ast_log(LOG_WARNING,"SQL select error!\n[%s]\n\n",sql); + return NULL; + } + + + if(rowcount) { + + res=SQLFetch(stmt); + cat_started=0; + + + + cur_cat = *new_cat_p; + cur_v = *new_v_p; + + if(cur_cat) + cat_started=1; + if(cur_v) + var_started=1; + + + + while(res != SQL_NO_DATA) { + //ast_log(LOG_NOTICE,"found %s %s %s\n",filename,var_name,var_val); + // build the config sql table probably needs to be improved! + + if(!strcmp(var_name,"#include") && recur < MAX_INCLUDE_LEVEL) { + + config_odbc(var_val,new,&cur_cat,&cur_v,recur + 1 +#ifdef PRESERVE_COMMENTS + ,acs +#endif + ); + + } + else { + + + + if(strcmp(last,category)) { + strcpy(last,category); + new_cat = (struct ast_category *) ast_new_category(category); + + if(!cat_started) { + cat_started++; + new->root = new_cat; + cur_cat=new->root; + } + else { + cur_cat->next = new_cat; + cur_cat = cur_cat->next; + } + var_started=0; + + } + + new_v = ast_new_variable(var_name,var_val); + + + if(!var_started) { + var_started++; + cur_cat->root = new_v; + cur_v = cur_cat->root; + } + else { + cur_v->next = new_v; + cur_v = cur_v->next; + } + } + + // next row + res=SQLFetch(stmt); + } + + SQLFreeHandle(SQL_HANDLE_STMT,stmt); + } + else + ast_log(LOG_NOTICE,"found nothing\n"); + + + + + + + + + return new; + +} + + + + +int unload_module(void) +{ + ast_cust_config_deregister(®1); + ast_log(LOG_NOTICE,"res_config_odbc unloaded.\n"); + STANDARD_HANGUP_LOCALUSERS; + return 0; +} + +int load_module(void) +{ + + memset(®1,0,sizeof(struct ast_config_reg)); + strcpy(reg1.name,"odbc"); + reg1.func = config_odbc; + ast_cust_config_register(®1); + ast_log(LOG_NOTICE,"res_config_odbc loaded.\n"); + return 0; +} + +char *description(void) +{ + return tdesc; +} + +int usecount(void) +{ + /* never unload a config module */ + return 1; +} + +char *key() +{ + return ASTERISK_GPL_KEY; +} + + diff --git a/res/res_odbc.c b/res/res_odbc.c new file mode 100755 index 000000000..24c28dd3f --- /dev/null +++ b/res/res_odbc.c @@ -0,0 +1,430 @@ +/* + * Asterisk -- A telephony toolkit for Linux. + * + * Copyright (C) 1999, Mark Spencer + * + * Mark Spencer + * + * res_odbc.c + * Copyright (C) 2004 Anthony Minessale II + */ + + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define MAX_ODBC_HANDLES 25 + +struct odbc_list { + char name[80]; + odbc_obj *obj; + int used; +}; + +static struct odbc_list ODBC_REGISTRY[MAX_ODBC_HANDLES]; + +static void odbc_destroy(void) { + int x=0; + + for(x=0;xnext) { + env_var = malloc( strlen(v->name) + strlen(v->value) +2); + sprintf(env_var,"%s=%s",v->name,v->value); + ast_log(LOG_NOTICE,"Adding ENV var: %s=%s\n",v->name,v->value); + putenv(env_var); + free(env_var); + } + + cat=ast_category_browse(config,cat); + } + + + dsn = username = password = NULL; + enabled = 1; + connect = 0; + for(v = ast_variable_browse(config,cat);v;v=v->next) { + if(!strcmp(v->name,"enabled")) + enabled = ast_true(v->value); + if(!strcmp(v->name,"pre-connect")) + connect = ast_true(v->value); + if(!strcmp(v->name,"dsn")) + dsn = v->value; + if(!strcmp(v->name,"username")) + username = v->value; + if(!strcmp(v->name,"password")) + password = v->value; + } + + if(enabled && dsn && username && password) { + obj = new_odbc_obj(cat,dsn,username,password); + if(obj) { + register_odbc_obj(cat,obj); + ast_log(LOG_NOTICE,"registered database handle '%s' dsn->[%s]\n",cat,obj->dsn); + if(connect) { + odbc_obj_connect(obj); + } + + } + else + ast_log(LOG_WARNING,"Addition of obj %s failed.\n",cat); + } + + + } + ast_destroy(config); + } + + + return 0; +} + + +int odbc_dump_fd(int fd,odbc_obj *obj) { + + ast_cli(fd,"\n\nName: %s\nDSN: %s\nConnected: %s\n\n",obj->name,obj->dsn,obj->up ? "yes" : "no"); + + return 0; +} + +static int odbc_usage(int fd) { + ast_cli(fd,"\n\nusage odbc .. \n\n"); + + return 0; +} + +static int odbc_command(int fd, int argc, char **argv) { + odbc_obj *obj; + int x=0; + if(!argv[1]) + return odbc_usage(fd); + + ast_cli(fd,"\n\n"); + + if(!strcmp(argv[1],"connect") || !strcmp(argv[1],"disconnect")) { + + if(!argv[2]) + return odbc_usage(fd); + + obj=odbc_read(ODBC_REGISTRY,argv[2]); + if(obj) { + if(!strcmp(argv[1],"connect")) + odbc_obj_connect(obj); + + if(!strcmp(argv[1],"disconnect")) + odbc_obj_disconnect(obj); + + } + + + + } + else if(!strcmp(argv[1],"show")) { + if(!argv[2] || (argv[2] && !strcmp(argv[2],"all"))) { + for(x=0;x .. ", NULL }; + +/* api calls */ + +int register_odbc_obj(char *name,odbc_obj *obj) { + if(obj != NULL) + return odbc_write(ODBC_REGISTRY,name,obj); + + return 0; +} + +odbc_obj *fetch_odbc_obj(char *name) { + return (odbc_obj *) odbc_read(ODBC_REGISTRY,name); +} + + +odbc_obj *new_odbc_obj(char *name,char *dsn,char *username, char *password) { + static odbc_obj *new; + + new=malloc(sizeof(odbc_obj)); + memset(new,0,sizeof(odbc_obj)); + new->env = SQL_NULL_HANDLE; + + new->name = malloc(strlen(name)+1); + if(new->name == NULL) + return NULL; + + new->dsn = malloc(strlen(dsn)+1); + if(new->dsn == NULL) + return NULL; + + new->username = malloc(strlen(username)+1); + if(new->username == NULL) + return NULL; + + new->password = malloc(strlen(password)+1); + if(new->password == NULL) + return NULL; + + strcpy(new->name,name); + strcpy(new->dsn,dsn); + strcpy(new->username,username); + strcpy(new->password,password); + new->up=0; + ast_mutex_init(&new->lock); + return new; +} + +void destroy_obdc_obj(odbc_obj **obj) { + + odbc_obj_disconnect(*obj); + + ast_mutex_lock(&(*obj)->lock); + SQLFreeHandle(SQL_HANDLE_STMT, (*obj)->stmt); + SQLFreeHandle(SQL_HANDLE_DBC,(*obj)->con); + SQLFreeHandle(SQL_HANDLE_ENV,(*obj)->env); + + free((*obj)->name); + free((*obj)->dsn); + free((*obj)->username); + free((*obj)->password); + ast_mutex_unlock(&(*obj)->lock); + free(*obj); +} + +odbc_status odbc_obj_disconnect(odbc_obj *obj) { + ast_mutex_lock(&obj->lock); + int res; + + if(obj->up) + res = SQLDisconnect(obj->con); + else + res = -1; + + if(res == ODBC_SUCCESS) { + ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n",res,obj->name,obj->dsn); + obj->up=0; + } + else + ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",obj->name,obj->dsn); + + ast_mutex_unlock(&obj->lock); + return ODBC_SUCCESS; +} + + +odbc_status odbc_obj_connect(odbc_obj *obj) { + int res; + long int err; + short int mlen; + char msg[200], stat[10]; + + ast_mutex_lock(&obj->lock); + + if(obj->env == SQL_NULL_HANDLE) { + + res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &obj->env); + + if((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + if(option_verbose > 3) + ast_log(LOG_WARNING, "res_odbc: Error AllocHandle\n"); + ast_mutex_unlock(&obj->lock); + return ODBC_FAIL; + } + + res = SQLSetEnvAttr(obj->env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); + + if((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + if(option_verbose > 3) + ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n"); + SQLFreeHandle(SQL_HANDLE_ENV, obj->env); + ast_mutex_unlock(&obj->lock); + return ODBC_FAIL; + } + + res = SQLAllocHandle(SQL_HANDLE_DBC, obj->env, &obj->con); + + if((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + + if(option_verbose > 3) + ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res); + SQLFreeHandle(SQL_HANDLE_ENV, obj->env); + + ast_mutex_unlock(&obj->lock); + return ODBC_FAIL; + } + SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)10, 0); + } + + + + res = SQLConnect( + + obj->con, + (SQLCHAR*) obj->dsn,SQL_NTS, + (SQLCHAR*) obj->username,SQL_NTS, + (SQLCHAR*) obj->password,SQL_NTS + + ); + + + if((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen); + SQLFreeHandle(SQL_HANDLE_ENV, obj->env); + if(option_verbose > 3) + ast_log(LOG_WARNING,"res_odbc: Error SQLConnect=%d errno=%ld %s\n", res,err,msg); + return ODBC_FAIL; + } + else { + + if(option_verbose > 3) + ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n",obj->name,obj->dsn); + obj->up=1; + } + + ast_mutex_unlock(&obj->lock); + return ODBC_SUCCESS; + +} + + + + + + +STANDARD_LOCAL_USER; + +LOCAL_USER_DECL; + + +int unload_module(void) { + STANDARD_HANGUP_LOCALUSERS; + + odbc_destroy(); + ast_cli_unregister(&odbc_command_struct); + ast_log(LOG_NOTICE,"res_odbc unloaded.\n"); + + return 0; +} + +int load_module(void) { + odbc_init(); + load_odbc_config(); + ast_cli_register(&odbc_command_struct); + ast_log(LOG_NOTICE,"res_odbc loaded.\n"); + return 0; +} + +char *description(void) { + return tdesc; +} + +int usecount(void) { + int res; + STANDARD_USECOUNT(res); + return res; +} + +char *key() { + return ASTERISK_GPL_KEY; +} + + + + + + + + + + + + -- cgit v1.2.3