Browse code

- changed db api: now several db modules can be used simultaneously; all the modules were updated to use the new api - small cleaups, like using static db related variables in modules (were it was possible) - changed version no.

Andrei Pelinescu-Onciul authored on 08/06/2004 10:55:29
Showing 49 changed files
... ...
@@ -29,6 +29,8 @@
29 29
  * --------
30 30
  * 2003-04-04  grand acc cleanup (jiri)
31 31
  * 2003-11-04  multidomain support for mysql introduced (jiri)
32
+ * 2004-06-06  updated to the new DB api, cleanup: acc_db_{bind, init,close)
33
+ *              added (andrei)
32 34
  */
33 35
 
34 36
 
... ...
@@ -86,6 +88,15 @@ static int diam_attr[] = { AVP_SIP_FROM_URI, AVP_SIP_TO_URI, AVP_SIP_OURI,
86 88
 	AVP_SIP_CALLID, AVP_SIP_TO_TAG, AVP_SIP_FROM_TAG, AVP_SIP_CSEQ };
87 89
 #endif
88 90
 
91
+#ifdef SQL_ACC
92
+
93
+static char* acc_db_url=0;
94
+static db_func_t acc_dbf;
95
+static db_con_t* db_handle=0;
96
+
97
+#endif
98
+
99
+
89 100
 static inline struct hdr_field *valid_to( struct cell *t, 
90 101
 				struct sip_msg *reply)
91 102
 {
... ...
@@ -425,6 +436,50 @@ void acc_log_ack(  struct cell* t , struct sip_msg *ack )
425 436
 
426 437
 #ifdef SQL_ACC
427 438
 
439
+
440
+/* binds to the corresponding database module
441
+ * returns 0 on success, -1 on error */
442
+int acc_db_bind(char* db_url)
443
+{
444
+	acc_db_url=db_url;
445
+	if (bind_dbmod(acc_db_url, &acc_dbf)<0){
446
+		LOG(L_ERR, "ERROR: acc_db_init: bind_db failed\n");
447
+		return -1;
448
+	}
449
+	
450
+	return 0;
451
+}
452
+
453
+
454
+
455
+/* initialize the database connection
456
+ * returns 0 on success, -1 on error */
457
+int acc_db_init()
458
+{
459
+	if (acc_db_url){
460
+		db_handle=acc_dbf.init(acc_db_url);
461
+		if (db_handle==0){
462
+			LOG(L_ERR, "ERROR: acc_db_init: unable to connect to the "
463
+					"database\n");
464
+			return -1;
465
+		}
466
+		return 0;
467
+	}else{
468
+		LOG(L_CRIT, "BUG: acc_db_init: null db url\n");
469
+		return -1;
470
+	}
471
+}
472
+
473
+
474
+
475
+/* close a db connection */
476
+void acc_db_close()
477
+{
478
+	if (db_handle && acc_dbf.close)	acc_dbf.close(db_handle);
479
+}
480
+
481
+
482
+
428 483
 int acc_db_request( struct sip_msg *rq, struct hdr_field *to, 
429 484
 				str *phrase, char *table, char *fmt)
430 485
 {
... ...
@@ -459,7 +514,7 @@ int acc_db_request( struct sip_msg *rq, struct hdr_field *to,
459 514
 		return -1;
460 515
 	}
461 516
 
462
-	if (!db_url) {
517
+	if (!acc_db_url) {
463 518
 		LOG(L_ERR, "ERROR: can't log -- no db_url set\n");
464 519
 		return -1;
465 520
 	}
... ...
@@ -479,8 +534,8 @@ int acc_db_request( struct sip_msg *rq, struct hdr_field *to,
479 534
 	VAL_NULL(vals+i)=0;
480 535
 	VAL_STRING(vals+i)=time_s;
481 536
 
482
-	db_use_table(db_handle, table);
483
-	if (db_insert(db_handle, keys, vals, i+1) < 0) {
537
+	acc_dbf.use_table(db_handle, table);
538
+	if (acc_dbf.insert(db_handle, keys, vals, i+1) < 0) {
484 539
 		LOG(L_ERR, "ERROR: acc_request: "
485 540
 				"Error while inserting to database\n");
486 541
 		return -1;;
... ...
@@ -28,6 +28,7 @@
28 28
  * --------
29 29
  * 2003-04-04  grand acc cleanup (jiri)
30 30
  * 2003-11-04  multidomain support for mysql introduced (jiri)
31
+ * 2004-06-06  cleanup: acc_db_{bind,init,close} added (andrei)
31 32
  */
32 33
 
33 34
 #ifndef _ACC_H
... ...
@@ -114,6 +115,9 @@ void acc_log_reply(  struct cell* t , struct sip_msg *reply,
114 115
 	unsigned int code);
115 116
 
116 117
 #ifdef SQL_ACC
118
+int acc_db_bind(char* db_url);
119
+int acc_db_init();
120
+void acc_db_close();
117 121
 int acc_db_request( struct sip_msg *rq, struct hdr_field *to,
118 122
 		str* phrase,  char *table, char *fmt);
119 123
 void acc_db_missed( struct cell* t, struct sip_msg *reply,
... ...
@@ -39,6 +39,8 @@
39 39
  * 2003-11-04  multidomain support for mysql introduced (jiri)
40 40
  * 2003-12-04  global TM callbacks switched to per transaction callbacks
41 41
  *             (bogdan)
42
+ * 2004-06-06  db cleanup: static db_url, calls to acc_db_{bind,init,close)
43
+ *              (andrei)
42 44
  */
43 45
 
44 46
 #include <stdio.h>
... ...
@@ -78,9 +80,6 @@ static int mod_init( void );
78 80
 static void destroy(void);
79 81
 static int child_init(int rank);
80 82
 
81
-#ifdef SQL_ACC
82
-db_con_t* db_handle;   /* Database connection handle */
83
-#endif
84 83
 
85 84
 /* buffer used to read from TCP connection*/
86 85
 #ifdef DIAM_ACC
... ...
@@ -126,7 +125,7 @@ int diameter_client_port=3000;
126 125
 #endif
127 126
 
128 127
 #ifdef SQL_ACC
129
-char *db_url=DEFAULT_DB_URL; /* Database url */
128
+static char *db_url=DEFAULT_DB_URL; /* Database url */
130 129
 
131 130
 /* sql flags, that need to be set for a transaction to 
132 131
  * be reported; 0=any, 1..MAX_FLAG otherwise; by default
... ...
@@ -308,8 +307,8 @@ static int mod_init( void )
308 307
 	}
309 308
 
310 309
 #ifdef SQL_ACC
311
-	if (bind_dbmod(db_url)) {
312
-		LOG(L_ERR, "ERROR:acc:mod_init: bind_db failed..."
310
+	if (acc_db_bind(db_url)<0){
311
+		LOG(L_ERR, "ERROR:acc_db_init: failed..."
313 312
 				"did you load a database module?\n");
314 313
 		return -1;
315 314
 	}
... ...
@@ -361,11 +360,8 @@ static int mod_init( void )
361 360
 static int child_init(int rank)
362 361
 {
363 362
 #ifdef SQL_ACC
364
-	db_handle = db_init(db_url);
365
-	if (!db_handle) {
366
-        LOG(L_ERR, "acc:init_child(): Unable to connect database\n");
363
+	if (acc_db_init()<0)
367 364
 		return -1;
368
-	}
369 365
 #endif
370 366
 
371 367
 /* DIAMETER */
... ...
@@ -399,7 +395,7 @@ static int child_init(int rank)
399 395
 static void destroy(void)
400 396
 {
401 397
 #ifdef SQL_ACC
402
-    if (db_handle) db_close(db_handle);
398
+	acc_db_close();
403 399
 #endif
404 400
 #ifdef DIAM_ACC
405 401
 	close_tcp_connection(sockfd);
... ...
@@ -30,6 +30,7 @@
30 30
  * ---------
31 31
  * 2003-04-04  grand acc cleanup (jiri)
32 32
  * 2003-11-04  multidomain support for mysql introduced (jiri)
33
+ * 2004-06-06  removed db_url, db_handle (andrei)
33 34
  */
34 35
 
35 36
 
... ...
@@ -67,9 +68,7 @@ extern int db_flag;
67 68
 extern int db_missed_flag;
68 69
 extern int db_localtime;
69 70
 
70
-extern db_con_t* db_handle; /* Database connection handle */
71 71
 
72
-extern char *db_url;
73 72
 extern char *db_table_acc;
74 73
 extern char *db_table_mc;
75 74
 
... ...
@@ -33,6 +33,7 @@
33 33
  * 2003-03-16: flags export parameter added (janakj)
34 34
  * 2003-03-19  all mallocs/frees replaced w/ pkg_malloc/pkg_free (andrei)
35 35
  * 2003-04-05: default_uri #define used (jiri)
36
+ * 2004-06-06  cleanup: static & auth_db_{init,bind,close.ver} used (andrei)
36 37
  */
37 38
 
38 39
 #include <stdio.h>
... ...
@@ -98,7 +99,7 @@ int (*sl_reply)(struct sip_msg* _msg, char* _str1, char* _str2);
98 99
 /*
99 100
  * Module parameter variables
100 101
  */
101
-str db_url           = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
102
+static str db_url           = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
102 103
 str user_column      = {USER_COL, USER_COL_LEN};
103 104
 str domain_column    = {DOMAIN_COL, DOMAIN_COL_LEN};
104 105
 str rpid_column      = {RPID_COL, RPID_COL_LEN};
... ...
@@ -108,7 +109,6 @@ int calc_ha1         = 0;
108 109
 int use_domain       = 1;    /* Use also domain when looking up a table row */
109 110
 int use_rpid         = 0;    /* Fetch Remote-Party-ID */
110 111
 
111
-db_con_t* db_handle;   /* Database connection handle */
112 112
 
113 113
 
114 114
 /*
... ...
@@ -156,13 +156,7 @@ struct module_exports exports = {
156 156
 static int child_init(int rank)
157 157
 {
158 158
 	     /* Close connection opened in mod_init */
159
-	db_handle = db_init(db_url.s);
160
-	if (!db_handle) {
161
-		LOG(L_ERR, "auth_db:init_child(): Unable to connect database\n");
162
-		return -1;
163
-	}
164
-
165
-	return 0;
159
+	return auth_db_init(db_url.s);
166 160
 }
167 161
 
168 162
 
... ...
@@ -178,10 +172,7 @@ static int mod_init(void)
178 172
 	pass_column_2.len = strlen(pass_column.s);
179 173
 
180 174
 	     /* Find a database module */
181
-	if (bind_dbmod(db_url.s) < 0) {
182
-		LOG(L_ERR, "auth_db:mod_init(): Unable to bind database module\n");
183
-		return -1;
184
-	}
175
+	if (auth_db_bind(db_url.s)<0) return -1;
185 176
 
186 177
 	pre_auth_func = (pre_auth_f)find_export("pre_auth", 0, 0);
187 178
 	post_auth_func = (post_auth_f)find_export("post_auth", 0, 0);
... ...
@@ -204,7 +195,7 @@ static int mod_init(void)
204 195
 
205 196
 static void destroy(void)
206 197
 {
207
-	if (db_handle) db_close(db_handle);
198
+	auth_db_close();
208 199
 }
209 200
 
210 201
 
... ...
@@ -231,17 +222,7 @@ static int str_fixup(void** param, int param_no)
231 222
 		name.s = (char*)*param;
232 223
 		name.len = strlen(name.s);
233 224
 
234
-		db_handle = db_init(db_url.s);
235
-		if (!db_handle) {
236
-			LOG(L_ERR, "auth_db:str_fixup(): Unable to open database connection\n");
237
-			return -1;
238
-		}
239
-
240
-		ver = table_version(db_handle, &name);
241
-
242
-		db_close(db_handle);
243
-		db_handle = 0;
244
-
225
+		ver=auth_db_ver(db_url.s, &name);
245 226
 		if (ver < 0) {
246 227
 			LOG(L_ERR, "auth_db:str_fixup(): Error while querying table version\n");
247 228
 			return -1;
... ...
@@ -41,7 +41,6 @@
41 41
  * Module parameters variables
42 42
  */
43 43
 
44
-extern str db_url;          /* Database URL */
45 44
 extern str user_column;     /* 'username' column name */
46 45
 extern str domain_column;   /* 'domain' column name */
47 46
 extern str rpid_column;     /* 'rpid' column name */
... ...
@@ -53,7 +52,6 @@ extern str pass_column_2;   /* Column containg HA1 string constructed
53 52
 extern int calc_ha1;          /* if set to 1, ha1 is calculated by the server */
54 53
 extern int use_domain;        /* If set to 1 then the domain will be used when selecting a row */
55 54
 extern int use_rpid;          /* If set to 1 then rpid will be fetched from rpid_column */
56
-extern db_con_t* db_handle;   /* Database connection handle */
57 55
 
58 56
 extern pre_auth_f pre_auth_func;
59 57
 extern post_auth_f post_auth_func;
... ...
@@ -30,6 +30,8 @@
30 30
  * ---------
31 31
  * 2003-02-28 scratchpad compatibility abandoned
32 32
  * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
33
+ * 2004-06-06 updated to the new DB api, added auth_db_{init,bind,close,ver}
34
+ *             (andrei)
33 35
  */
34 36
 
35 37
 
... ...
@@ -46,6 +48,8 @@
46 48
 
47 49
 #define MESSAGE_500 "Server Internal Error"
48 50
 
51
+static db_con_t* db_handle=0; /* database connection handle */
52
+static db_func_t  auth_dbf;
49 53
 
50 54
 static char rpid_buffer[MAX_RPID_LEN];
51 55
 static str rpid = {rpid_buffer, 0};
... ...
@@ -76,8 +80,8 @@ static inline int get_ha1(struct username* _username, str* _domain, char* _table
76 80
 
77 81
 	n = (use_domain ? 2 : 1);
78 82
 	nc = (use_rpid ? 2 : 1);
79
-	db_use_table(db_handle, _table);
80
-	if (db_query(db_handle, keys, 0, vals, col, n, nc, 0, &res) < 0) {
83
+	auth_dbf.use_table(db_handle, _table);
84
+	if (auth_dbf.query(db_handle, keys, 0, vals, col, n, nc, 0, &res) < 0) {
81 85
 		LOG(L_ERR, "get_ha1(): Error while querying database\n");
82 86
 		return -1;
83 87
 	}
... ...
@@ -85,7 +89,7 @@ static inline int get_ha1(struct username* _username, str* _domain, char* _table
85 89
 	if (RES_ROW_N(res) == 0) {
86 90
 		DBG("get_ha1(): no result for user \'%.*s@%.*s\'\n", 
87 91
 		    _username->user.len, ZSW(_username->user.s), (use_domain ? (_domain->len) : 0), ZSW(_domain->s));
88
-		db_free_query(db_handle, res);
92
+		auth_dbf.free_query(db_handle, res);
89 93
 		return 1;
90 94
 	}
91 95
 
... ...
@@ -108,7 +112,7 @@ static inline int get_ha1(struct username* _username, str* _domain, char* _table
108 112
 		memcpy(_rpid->s, result.s, _rpid->len);
109 113
 	}
110 114
 
111
-	db_free_query(db_handle, res);
115
+	auth_dbf.free_query(db_handle, res);
112 116
 	return 0;
113 117
 }
114 118
 
... ...
@@ -226,3 +230,61 @@ int www_authorize(struct sip_msg* _m, char* _realm, char* _table)
226 230
 {
227 231
 	return authorize(_m, (str*)_realm, _table, HDR_AUTHORIZATION);
228 232
 }
233
+
234
+
235
+
236
+int auth_db_init(char* db_url)
237
+{
238
+	if (auth_dbf.init==0){
239
+		LOG(L_CRIT, "BUG: auth_db_bind: null dbf\n");
240
+		goto error;
241
+	}
242
+	db_handle=auth_dbf.init(db_url);
243
+	if (db_handle==0){
244
+		LOG(L_ERR, "ERROR: auth_db_bind: unable to connect to the database\n");
245
+		goto error;
246
+	}
247
+	return 0;
248
+error:
249
+	return -1;
250
+}
251
+
252
+
253
+int auth_db_bind(char* db_url)
254
+{
255
+	if (bind_dbmod(db_url, &auth_dbf)<0){
256
+		LOG(L_ERR, "ERROR: auth_db_bind: unable to bind to the database"
257
+				" module\n");
258
+		return -1;
259
+	}
260
+	return 0;
261
+}
262
+
263
+
264
+void auth_db_close()
265
+{
266
+	if (db_handle && auth_dbf.close){
267
+		auth_dbf.close(db_handle);
268
+		db_handle=0;
269
+	}
270
+}
271
+
272
+
273
+int auth_db_ver(char* db_url, str* name)
274
+{
275
+	db_con_t* dbh;
276
+	int ver;
277
+
278
+	if (auth_dbf.init==0){
279
+		LOG(L_CRIT, "BUG: auth_db_ver: unbound database\n");
280
+		return -1;
281
+	}
282
+	dbh=auth_dbf.init(db_url);
283
+	if (dbh==0){
284
+		LOG(L_ERR, "ERROR: auth_db_ver: unable to open database connection\n");
285
+		return -1;
286
+	}
287
+	ver=table_version(&auth_dbf, dbh, name);
288
+	auth_dbf.close(dbh);
289
+	return ver;
290
+}
... ...
@@ -34,6 +34,10 @@
34 34
 
35 35
 #include "../../parser/msg_parser.h"
36 36
 
37
+int auth_db_init(char* db_url);
38
+int auth_db_bind(char* db_url);
39
+void auth_db_close();
40
+int auth_db_ver(char* db_url, str* name);
37 41
 
38 42
 /*
39 43
  * Authorize using Proxy-Authorization header field
... ...
@@ -29,6 +29,7 @@
29 29
  * 2003-03-11: New module interface (janakj)
30 30
  * 2003-03-16: flags export parameter added (janakj)
31 31
  * 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
32
+ * 2004-06-06  updated to the new DB api (andrei)
32 33
  */
33 34
 
34 35
 
... ...
@@ -85,8 +86,6 @@ str    cpl_orig_tz = {0,0}; /* a copy of the original TZ; keept as a null
85 86
                              * terminated string in "TZ=value" format;
86 87
                              * used only by run_time_switch */
87 88
 
88
-/* this vars are used outside only for loading scripts */
89
-db_con_t* db_hdl   = 0;   /* this should be static !!!!*/
90 89
 
91 90
 int (*cpl_sl_reply)(struct sip_msg* _m, char* _s1, char* _s2);
92 91
 
... ...
@@ -258,11 +257,7 @@ static int cpl_init(void)
258 257
 	}
259 258
 
260 259
 	/* bind to the mysql module */
261
-	if (bind_dbmod( DB_URL )) {
262
-		LOG(L_CRIT,"ERROR:cpl_init: cannot bind to database module! "
263
-			"Did you forget to load a database module ?\n");
264
-		goto error;
265
-	}
260
+	if (cpl_db_bind(DB_URL)<0) goto error;
266 261
 
267 262
 	/* import the TM auto-loading function */
268 263
 	if ( !(load_tm=(load_tm_f)find_export("load_tm", NO_SCRIPT, 0))) {
... ...
@@ -386,21 +381,8 @@ static int cpl_child_init(int rank)
386 381
 		}
387 382
 	}
388 383
 
389
-	if ( (db_hdl=db_init(DB_URL))==0 ) {
390
-		LOG(L_CRIT,"ERROR:cpl_child_init: cannot initialize database "
391
-			"connection\n");
392
-		goto error;
393
-	}
394
-	if (db_use_table( db_hdl, DB_TABLE) < 0) {
395
-		LOG(L_CRIT,"ERROR:cpl_child_init: cannot select table \"%s\"\n",
396
-			DB_TABLE);
397
-		goto error;
398
-	}
399
-
400
-	return 0;
384
+	return cpl_db_init(DB_URL, DB_TABLE);
401 385
 error:
402
-	if (db_hdl)
403
-		db_close(db_hdl);
404 386
 	return -1;
405 387
 }
406 388
 
... ...
@@ -554,7 +536,7 @@ static int cpl_invoke_script(struct sip_msg* msg, char* str1, char* str2)
554 536
 	}
555 537
 
556 538
 	/* get the script for this user */
557
-	if (get_user_script( db_hdl, &user, &script, "cpl_bin")==-1)
539
+	if (get_user_script(&user, &script, "cpl_bin")==-1)
558 540
 		goto error1;
559 541
 
560 542
 	/* has the user a non-empty script? if not, return normaly, allowing ser to
... ...
@@ -687,7 +669,7 @@ static inline int do_script_action(struct sip_msg *msg, int action)
687 669
 			}
688 670
 
689 671
 			/* write both the XML and binary formats into database */
690
-			if (write_to_db( db_hdl, user.s, &body, &bin)!=1) {
672
+			if (write_to_db(user.s, &body, &bin)!=1) {
691 673
 				cpl_err = &intern_err;
692 674
 				goto error_1;
693 675
 			}
... ...
@@ -700,7 +682,7 @@ static inline int do_script_action(struct sip_msg *msg, int action)
700 682
 				goto error_1;
701 683
 			}
702 684
 			/* remove the script for the user */
703
-			if (rmv_from_db( db_hdl, user.s)!=1) {
685
+			if (rmv_from_db(user.s)!=1) {
704 686
 				cpl_err = &intern_err;
705 687
 				goto error_1;
706 688
 			}
... ...
@@ -727,7 +709,7 @@ static inline int do_script_download(struct sip_msg *msg)
727 709
 		goto error;
728 710
 
729 711
 	/* get the user's xml script from the database */
730
-	if (get_user_script( db_hdl, &user, &script, "cpl_xml")==-1)
712
+	if (get_user_script(&user, &script, "cpl_xml")==-1)
731 713
 		goto error;
732 714
 
733 715
 	/* add a lump with content-type hdr */
... ...
@@ -24,21 +24,74 @@
24 24
  * along with this program; if not, write to the Free Software
25 25
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 26
  */
27
+ /*
28
+  * History:
29
+  * --------
30
+  *  2004-06-06  updated to the new DB api (andrei)
31
+  */
27 32
 
28 33
 #include "../../mem/shm_mem.h"
29 34
 #include "../../db/db.h"
30 35
 #include "../../dprint.h"
31 36
 #include "cpl_db.h"
32 37
 
38
+static db_con_t* db_hdl=0;
39
+static db_func_t cpl_dbf;
33 40
 
34 41
 
35 42
 
43
+int cpl_db_bind(char* db_url)
44
+{
45
+	if (bind_dbmod(db_url, &cpl_dbf )) {
46
+		LOG(L_CRIT, "ERROR:cpl_db_bind: cannot bind to database module! "
47
+		"Did you forget to load a database module ?\n");
48
+		return -1;
49
+	}
50
+	return 0;
51
+}
52
+
53
+
54
+
55
+int cpl_db_init(char* db_url, char* db_table)
56
+{
57
+	if (cpl_dbf.init==0){
58
+		LOG(L_CRIT, "BUG: cpl_db_init: unbound database module\n");
59
+		return -1;
60
+	}
61
+	db_hdl=cpl_dbf.init(db_url);
62
+	if (db_hdl==0){
63
+		LOG(L_CRIT,"ERROR:cpl_db_init: cannot initialize database "
64
+			"connection\n");
65
+		goto error;
66
+	}
67
+	if (cpl_dbf.use_table(db_hdl, db_table)<0) {
68
+		LOG(L_CRIT,"ERROR:cpl_db_init: cannot select table \"%s\"\n",db_table);
69
+		goto error;
70
+	}
71
+	return 0;
72
+error:
73
+	if (db_hdl){
74
+		cpl_dbf.close(db_hdl);
75
+		db_hdl=0;
76
+	}
77
+	return -1;
78
+}
79
+
80
+void cpl_db_close()
81
+{
82
+	if (db_hdl && cpl_dbf.close){
83
+		cpl_dbf.close(db_hdl);
84
+		db_hdl=0;
85
+	}
86
+}
87
+
88
+
36 89
 /* gets from database the cpl script in binary format; the returned script is
37 90
  * allocated in shared memory
38 91
  * Returns:  1 - success
39 92
  *          -1 - error
40 93
  */
41
-int get_user_script( db_con_t *db_hdl, str *user, str *script, char* key)
94
+int get_user_script(str *user, str *script, char* key)
42 95
 {
43 96
 	db_key_t   keys_cmp[] = {"user"};
44 97
 	db_key_t   keys_ret[] = { key };
... ...
@@ -49,7 +102,8 @@ int get_user_script( db_con_t *db_hdl, str *user, str *script, char* key)
49 102
 	vals[0].type = DB_STRING;
50 103
 	vals[0].nul  = 0;
51 104
 	vals[0].val.string_val = user->s;
52
-	if (db_query(db_hdl, keys_cmp, 0, vals, keys_ret, 1, 1, NULL, &res) < 0) {
105
+	if (cpl_dbf.query(db_hdl, keys_cmp, 0, vals, keys_ret, 1, 1, NULL, &res)
106
+			< 0){
53 107
 		LOG(L_ERR,"ERROR:cpl-c:get_user_script: db_query failed\n");
54 108
 		goto error;
55 109
 	}
... ...
@@ -79,11 +133,11 @@ int get_user_script( db_con_t *db_hdl, str *user, str *script, char* key)
79 133
 		}
80 134
 	}
81 135
 
82
-	db_free_query( db_hdl, res);
136
+	cpl_dbf.free_query( db_hdl, res);
83 137
 	return 1;
84 138
 error:
85 139
 	if (res)
86
-		db_free_query( db_hdl, res);
140
+		cpl_dbf.free_query( db_hdl, res);
87 141
 	script->s = 0;
88 142
 	script->len = 0;
89 143
 	return -1;
... ...
@@ -96,7 +150,7 @@ error:
96 150
  * Returns:  1 - success
97 151
  *          -1 - error
98 152
  */
99
-int write_to_db(db_con_t *db_con, char *usr, str *xml, str *bin)
153
+int write_to_db(char *usr, str *xml, str *bin)
100 154
 {
101 155
 	db_key_t   keys[] = {"user","cpl_xml","cpl_bin"};
102 156
 	db_val_t   vals[3];
... ...
@@ -106,7 +160,7 @@ int write_to_db(db_con_t *db_con, char *usr, str *xml, str *bin)
106 160
 	vals[0].type = DB_STRING;
107 161
 	vals[0].nul  = 0;
108 162
 	vals[0].val.string_val = usr;
109
-	if (db_query(db_con, keys, 0, vals, keys, 1, 1, NULL, &res) < 0) {
163
+	if (cpl_dbf.query(db_hdl, keys, 0, vals, keys, 1, 1, NULL, &res) < 0) {
110 164
 		LOG(L_ERR,"ERROR:cpl:write_to_db: db_query failed\n");
111 165
 		goto error;
112 166
 	}
... ...
@@ -133,14 +187,14 @@ int write_to_db(db_con_t *db_con, char *usr, str *xml, str *bin)
133 187
 	/* insert or update ? */
134 188
 	if (res->n==0) {
135 189
 		DBG("DEBUG:cpl:write_to_db:No user %s in CPL databse->insert\n",usr);
136
-		if (db_insert(db_con, keys, vals, 3) < 0) {
190
+		if (cpl_dbf.insert(db_hdl, keys, vals, 3) < 0) {
137 191
 			LOG(L_ERR,"ERROR:cpl:write_to_db: insert failed !\n");
138 192
 			goto error;
139 193
 		}
140 194
 	} else {
141 195
 		DBG("DEBUG:cpl:write_to_db:User %s already in CPL database ->"
142 196
 			" update\n",usr);
143
-		if (db_update(db_con, keys, 0, vals, keys+1, vals+1, 1, 2) < 0) {
197
+		if (cpl_dbf.update(db_hdl, keys, 0, vals, keys+1, vals+1, 1, 2) < 0) {
144 198
 			LOG(L_ERR,"ERROR:cpl:write_to_db: updare failed !\n");
145 199
 			goto error;
146 200
 		}
... ...
@@ -159,7 +213,7 @@ error:
159 213
  * Returns:  1 - success
160 214
  *          -1 - error
161 215
  */
162
-int rmv_from_db(db_con_t *db_con, char *usr)
216
+int rmv_from_db(char *usr)
163 217
 {
164 218
 	db_key_t   keys[] = {"user"};
165 219
 	db_val_t   vals[1];
... ...
@@ -169,7 +223,7 @@ int rmv_from_db(db_con_t *db_con, char *usr)
169 223
 	vals[0].nul  = 0;
170 224
 	vals[0].val.string_val = usr;
171 225
 
172
-	if (db_delete(db_con, keys, NULL, vals, 1) < 0) {
226
+	if (cpl_dbf.delete(db_hdl, keys, NULL, vals, 1) < 0) {
173 227
 		LOG(L_ERR,"ERROR:cpl-c:rmv_from_db: error when deleting script for "
174 228
 			"user \"%s\"\n",usr);
175 229
 		return -1;
... ...
@@ -31,19 +31,24 @@
31 31
 #include "../../db/db.h"
32 32
 
33 33
 
34
+int cpl_db_bind(char* db_url);
35
+int cpl_db_init(char* db_url, char* db_table);
36
+void cpl_db_close();
37
+
38
+
34 39
 /* inserts into database a cpl script in XML format(xml) along with its binary
35 40
  * format (bin)
36 41
  * Returns:  1 - success
37 42
  *          -1 - error
38 43
  */
39
-int write_to_db(db_con_t *db_con, char *usr, str *xml, str *bin);
44
+int write_to_db( char *usr, str *xml, str *bin);
40 45
 
41 46
 
42 47
 /* fetch from database the binary format of the cpl script for a given user
43 48
  * Returns:  1 - success
44 49
  *          -1 - error
45 50
  */
46
-int get_user_script( db_con_t *db_hdl, str *user, str *script, char *key);
51
+int get_user_script(str *user, str *script, char *key);
47 52
 
48 53
 
49 54
 /* delete from database the entiry record for a given user - if a user has no
... ...
@@ -52,7 +57,7 @@ int get_user_script( db_con_t *db_hdl, str *user, str *script, char *key);
52 57
  * Returns:  1 - success
53 58
  *          -1 - error
54 59
  */
55
-int rmv_from_db(db_con_t *db_con, char *usr);
60
+int rmv_from_db(char *usr);
56 61
 
57 62
 
58 63
 #endif
... ...
@@ -310,7 +310,7 @@ int cpl_load( FILE *fifo_stream, char *response_file )
310 310
 	logs[1] = enc_log;
311 311
 
312 312
 	/* write both the XML and binary formats into database */
313
-	if (write_to_db( db_hdl, user, &xml, &bin)!=1) {
313
+	if (write_to_db(user, &xml, &bin)!=1) {
314 314
 		logs[1].s = DB_SAVE_ERR;
315 315
 		logs[1].len = strlen( DB_SAVE_ERR );
316 316
 		goto error1;
... ...
@@ -382,7 +382,7 @@ int cpl_remove( FILE *fifo_stream, char *response_file )
382 382
 		goto error1;
383 383
 	}
384 384
 
385
-	if (rmv_from_db( db_hdl, user)!=1) {
385
+	if (rmv_from_db(user)!=1) {
386 386
 		logs[1].s = DB_RMV_ERR;
387 387
 		logs[1].len = sizeof(DB_RMV_ERR);
388 388
 		goto error1;
... ...
@@ -445,7 +445,7 @@ int cpl_get( FILE *fifo_stream, char *response_file )
445 445
 	}
446 446
 
447 447
 	/* get the script for this user */
448
-	if (get_user_script( db_hdl, &user, &script, "cpl_xml")==-1) {
448
+	if (get_user_script(&user, &script, "cpl_xml")==-1) {
449 449
 		logs[1].s = DB_GET_ERR;
450 450
 		logs[1].len = strlen( DB_GET_ERR );
451 451
 		goto error1;
... ...
@@ -26,7 +26,12 @@
26 26
  * along with this program; if not, write to the Free Software 
27 27
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 28
  */
29
-
29
+ /*
30
+  * History:
31
+  * --------
32
+  *  2004-06-07  updated to the new DB api, moved reload_table here, created 
33
+  *               domain_db_{init.bind,ver,close} (andrei)
34
+  */
30 35
 
31 36
 #include "domain_mod.h"
32 37
 #include "hash.h"
... ...
@@ -35,6 +40,64 @@
35 40
 #include "../../parser/parse_from.h"
36 41
 #include "../../ut.h"
37 42
 
43
+static db_con_t* db_handle=0;
44
+static db_func_t domain_dbf;
45
+
46
+/* helper db functions*/
47
+
48
+int domain_db_bind(char* db_url)
49
+{
50
+	if (bind_dbmod(db_url, &domain_dbf )) {
51
+		LOG(L_CRIT, "ERROR: domain_db_bind: cannot bind to database module! "
52
+		"Did you forget to load a database module ?\n");
53
+		return -1;
54
+	}
55
+	return 0;
56
+}
57
+
58
+
59
+
60
+int domain_db_init(char* db_url)
61
+{
62
+	if (domain_dbf.init==0){
63
+		LOG(L_CRIT, "BUG: domain_db_init: unbound database module\n");
64
+		goto error;
65
+	}
66
+	db_handle=domain_dbf.init(db_url);
67
+	if (db_handle==0){
68
+		LOG(L_CRIT, "ERROR:domain_db_init: cannot initialize database "
69
+							"connection\n");
70
+		goto error;
71
+	}
72
+	return 0;
73
+error:
74
+	return -1;
75
+}
76
+
77
+
78
+void domain_db_close()
79
+{
80
+	if (db_handle && domain_dbf.close){
81
+		domain_dbf.close(db_handle);
82
+		db_handle=0;
83
+	}
84
+}
85
+
86
+
87
+
88
+int domain_db_ver(str* name)
89
+{
90
+	int ver;
91
+
92
+	if (db_handle==0){
93
+		LOG(L_CRIT, "BUG:domain_db_ver: null database handler\n");
94
+		return -1;
95
+	}
96
+	ver=table_version(&domain_dbf, db_handle, name);
97
+	return ver;
98
+}
99
+
100
+
38 101
 
39 102
 /*
40 103
  * Check if domain is local
... ...
@@ -50,7 +113,7 @@ int is_domain_local(str* _host)
50 113
 		keys[0]=domain_col.s;
51 114
 		cols[0]=domain_col.s;
52 115
 		
53
-		if (db_use_table(db_handle, domain_table.s) < 0) {
116
+		if (domain_dbf.use_table(db_handle, domain_table.s) < 0) {
54 117
 			LOG(L_ERR, "is_local(): Error while trying to use domain table\n");
55 118
 			return -1;
56 119
 		}
... ...
@@ -61,7 +124,8 @@ int is_domain_local(str* _host)
61 124
 		VAL_STR(vals).s = _host->s;
62 125
 		VAL_STR(vals).len = _host->len;
63 126
 
64
-		if (db_query(db_handle, keys, 0, vals, cols, 1, 1, 0, &res) < 0) {
127
+		if (domain_dbf.query(db_handle, keys, 0, vals, cols, 1, 1, 0, &res) < 0
128
+				) {
65 129
 			LOG(L_ERR, "is_local(): Error while querying database\n");
66 130
 			return -1;
67 131
 		}
... ...
@@ -69,12 +133,12 @@ int is_domain_local(str* _host)
69 133
 		if (RES_ROW_N(res) == 0) {
70 134
 			DBG("is_local(): Realm '%.*s' is not local\n", 
71 135
 			    _host->len, ZSW(_host->s));
72
-			db_free_query(db_handle, res);
136
+			domain_dbf.free_query(db_handle, res);
73 137
 			return -1;
74 138
 		} else {
75 139
 			DBG("is_local(): Realm '%.*s' is local\n", 
76 140
 			    _host->len, ZSW(_host->s));
77
-			db_free_query(db_handle, res);
141
+			domain_dbf.free_query(db_handle, res);
78 142
 			return 1;
79 143
 		}
80 144
 	} else {
... ...
@@ -119,3 +183,74 @@ int is_uri_host_local(struct sip_msg* _msg, char* _s1, char* _s2)
119 183
 
120 184
 	return is_domain_local(&(_msg->parsed_uri.host));
121 185
 }
186
+
187
+
188
+
189
+/*
190
+ * Reload domain table to new hash table and when done, make new hash table
191
+ * current one.
192
+ */
193
+int reload_domain_table ( void )
194
+{
195
+/*	db_key_t keys[] = {domain_col}; */
196
+	db_val_t vals[1];
197
+	db_key_t cols[1];
198
+	db_res_t* res;
199
+	db_row_t* row;
200
+	db_val_t* val;
201
+
202
+	struct domain_list **new_hash_table;
203
+	int i;
204
+
205
+	cols[0] = domain_col.s;
206
+
207
+	if (domain_dbf.use_table(db_handle, domain_table.s) < 0) {
208
+		LOG(L_ERR, "reload_domain_table(): Error while trying to use domain table\n");
209
+		return -1;
210
+	}
211
+
212
+	VAL_TYPE(vals) = DB_STR;
213
+	VAL_NULL(vals) = 0;
214
+    
215
+	if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 1, 0, &res) < 0) {
216
+		LOG(L_ERR, "reload_domain_table(): Error while querying database\n");
217
+		return -1;
218
+	}
219
+
220
+	/* Choose new hash table and free its old contents */
221
+	if (*hash_table == hash_table_1) {
222
+		hash_table_free(hash_table_2);
223
+		new_hash_table = hash_table_2;
224
+	} else {
225
+		hash_table_free(hash_table_1);
226
+		new_hash_table = hash_table_1;
227
+	}
228
+
229
+	row = RES_ROWS(res);
230
+
231
+	DBG("Number of rows in domain table: %d\n", RES_ROW_N(res));
232
+		
233
+	for (i = 0; i < RES_ROW_N(res); i++) {
234
+		val = ROW_VALUES(row + i);
235
+		if ((ROW_N(row) == 1) && (VAL_TYPE(val) == DB_STRING)) {
236
+			
237
+			DBG("Value: %s inserted into domain hash table\n", VAL_STRING(val));
238
+
239
+			if (hash_table_install(new_hash_table, (char *)(VAL_STRING(val))) == -1) {
240
+				LOG(L_ERR, "domain_reload(): Hash table problem\n");
241
+				domain_dbf.free_query(db_handle, res);
242
+				return -1;
243
+			}
244
+		} else {
245
+			LOG(L_ERR, "domain_reload(): Database problem\n");
246
+			domain_dbf.free_query(db_handle, res);
247
+			return -1;
248
+		}
249
+	}
250
+	domain_dbf.free_query(db_handle, res);
251
+
252
+	*hash_table = new_hash_table;
253
+	
254
+	return 1;
255
+}
256
+
... ...
@@ -45,5 +45,13 @@ int is_from_local(struct sip_msg* _msg, char* _s1, char* _s2);
45 45
  */
46 46
 int is_uri_host_local(struct sip_msg* _msg, char* _s1, char* _s2);
47 47
 
48
+int domain_db_bind(char* db_url);
49
+int domain_db_init(char* db_url);
50
+void domain_db_close();
51
+int domain_db_ver(str* name);
52
+
53
+int reload_domain_table();
54
+
55
+
48 56
 
49 57
 #endif /* DOMAIN_H */
... ...
@@ -32,6 +32,8 @@
32 32
  * 2003-03-16: flags export parameter added (janakj)
33 33
  * 2003-04-05: default_uri #define used (jiri)
34 34
  * 2003-04-06: db connection closed in mod_init (janakj)
35
+ * 2004-06-06  updated to the new DB api, cleanup: static dbf & handler,
36
+ *              calls to domain_db_{bind,init,close,ver} (andrei)
35 37
  */
36 38
 
37 39
 
... ...
@@ -68,7 +70,7 @@ MODULE_VERSION
68 70
 /*
69 71
  * Module parameter variables
70 72
  */
71
-str db_url = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
73
+static str db_url = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
72 74
 int db_mode = 0;			/* Database usage mode: 0 = no cache, 1 = cache */
73 75
 str domain_table = {DOMAIN_TABLE, DOMAIN_TABLE_LEN};     /* Name of domain table */
74 76
 str domain_col = {DOMAIN_COL, DOMAIN_COL_LEN};           /* Name of domain column */
... ...
@@ -76,7 +78,6 @@ str domain_col = {DOMAIN_COL, DOMAIN_COL_LEN};           /* Name of domain colum
76 78
 /*
77 79
  * Other module variables
78 80
  */
79
-db_con_t* db_handle = NULL;		/* Database connection handle */
80 81
 struct domain_list ***hash_table;	/* Pointer to current hash table pointer */
81 82
 struct domain_list **hash_table_1;	/* Pointer to hash table 1 */
82 83
 struct domain_list **hash_table_2;	/* Pointer to hash table 2 */
... ...
@@ -130,28 +131,22 @@ static int mod_init(void)
130 131
 	domain_col.len = strlen(domain_col.s);
131 132
 
132 133
 	/* Check if database module has been loaded */
133
-	if (bind_dbmod(db_url.s)) {
134
-		LOG(L_ERR, "domain:mod_init(): Unable to bind database module\n");
135
-		return -1;
136
-	}
134
+	if (domain_db_bind(db_url.s)<0)  return -1;
137 135
 
138 136
 	/* Check if cache needs to be loaded from domain table */
139 137
 	if (db_mode == 1) {
140
-		db_handle = db_init(db_url.s);
141
-		if (!db_handle) {
142
-			LOG(L_ERR, "domain:mod_init(): Unable to connect database\n");
143
-			return -1;
144
-		}
145
-
138
+		if (domain_db_init(db_url.s)<0) return -1;
146 139
 		     /* Check table version */
147
-		ver = table_version(db_handle, &domain_table);
140
+		ver = domain_db_ver(&domain_table);
148 141
 		if (ver < 0) {
149
-			LOG(L_ERR, "domain:mod_init(): Error while querying table version\n");
150
-			db_close(db_handle);
142
+			LOG(L_ERR, "ERROR: domain:mod_init(): "
143
+					"error while querying table version\n");
144
+			domain_db_close();
151 145
 			return -1;
152 146
 		} else if (ver < TABLE_VERSION) {
153
-			LOG(L_ERR, "domain:mod_init(): Invalid table version (use ser_mysql.sh reinstall)\n");
154
-			db_close(db_handle);
147
+			LOG(L_ERR, "ERROR: domain:mod_init(): invalid table"
148
+					" version (use ser_mysql.sh reinstall)\n");
149
+			domain_db_close();
155 150
 			return -1;
156 151
 		}		
157 152
 
... ...
@@ -159,20 +154,23 @@ static int mod_init(void)
159 154
 		(void)init_domain_fifo();
160 155
 
161 156
 		if (init_domain_unixsock() < 0) {
162
-			LOG(L_ERR, "domain:mod_init(): Error while initializing unix socket interface\n");
163
-			db_close(db_handle);
157
+			LOG(L_ERR, "ERROR: domain:mod_init(): error while initializing"
158
+					" unix socket interface\n");
159
+			domain_db_close();
164 160
 			return -1;
165 161
 		}
166 162
 
167 163
 		/* Initializing hash tables and hash table variable */
168 164
 		hash_table_1 = (struct domain_list **)shm_malloc(sizeof(struct domain_list *) * HASH_SIZE);
169 165
 		if (hash_table_1 == 0) {
170
-			LOG(L_ERR, "domain: mod_init(): No memory for hash table\n");
166
+			LOG(L_ERR, "ERROR: domain: mod_init(): "
167
+					"No memory for hash table\n");
171 168
 		}
172 169
 
173 170
 		hash_table_2 = (struct domain_list **)shm_malloc(sizeof(struct domain_list *) * HASH_SIZE);
174 171
 		if (hash_table_2 == 0) {
175
-			LOG(L_ERR, "domain: mod_init(): No memory for hash table\n");
172
+			LOG(L_ERR, "ERROR: domain: mod_init():"
173
+					" No memory for hash table\n");
176 174
 		}
177 175
 		for (i = 0; i < HASH_SIZE; i++) {
178 176
 			hash_table_1[i] = hash_table_2[i] = (struct domain_list *)0;
... ...
@@ -182,11 +180,12 @@ static int mod_init(void)
182 180
 		*hash_table = hash_table_1;
183 181
 
184 182
 		if (reload_domain_table() == -1) {
185
-			LOG(L_CRIT, "domain:mod_init(): Domain table reload failed\n");
183
+			LOG(L_CRIT, "ERROR: domain:mod_init():"
184
+					" Domain table reload failed\n");
186 185
 			return -1;
187 186
 		}
188 187
 			
189
-		db_close(db_handle);
188
+		domain_db_close();
190 189
 	}
191 190
 
192 191
 	return 0;
... ...
@@ -197,13 +196,12 @@ static int child_init(int rank)
197 196
 {
198 197
 	/* Check if database is needed by child */
199 198
 	if (((db_mode == 0) && (rank > 0)) || ((db_mode == 1) && (rank == PROC_FIFO))) {
200
-		db_handle = db_init(db_url.s);
201
-		if (!db_handle) {
202
-			LOG(L_ERR, "domain:child_init(): Unable to connect database\n");
199
+		if (domain_db_init(db_url.s)<0) {
200
+			LOG(L_ERR, "ERROR: domain:child_init():"
201
+					" Unable to connect to the database\n");
203 202
 			return -1;
204 203
 		}
205 204
 	}
206
-
207 205
 	return 0;
208 206
 }
209 207
 
... ...
@@ -52,7 +52,6 @@ struct domain_list {
52 52
 /*
53 53
  * Module parameters variables
54 54
  */
55
-extern str db_url;              /* Database URL */
56 55
 extern int db_mode;             /* Database usage mode: 0 = no cache, 1 = cache */
57 56
 extern str domain_table;	/* Domain table name */
58 57
 extern str domain_col;   	/* Domain column name */
... ...
@@ -61,7 +60,6 @@ extern str domain_col;   	/* Domain column name */
61 60
 /*
62 61
  * Other module variables
63 62
  */
64
-extern db_con_t* db_handle;	/* Database connection handle */
65 63
 extern struct domain_list **hash_table_1; /* Hash table for domains */
66 64
 extern struct domain_list **hash_table_2; /* Hash table for domains */
67 65
 extern struct domain_list ***hash_table;  /* Current hash table */
... ...
@@ -26,9 +26,15 @@
26 26
  * along with this program; if not, write to the Free Software 
27 27
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 28
  */
29
+/*
30
+ * History:
31
+ * --------
32
+ *  2004-06-07  moved reload_domain_table() into domain.c (andrei)
33
+ */
29 34
 
30 35
 
31 36
 #include "domain_mod.h"
37
+#include "domain.h"
32 38
 #include "hash.h"
33 39
 #include "fifo.h"
34 40
 #include "../../fifo_server.h"
... ...
@@ -36,73 +42,6 @@
36 42
 #include "../../db/db.h"
37 43
 
38 44
 
39
-/*
40
- * Reload domain table to new hash table and when done, make new hash table
41
- * current one.
42
- */
43
-int reload_domain_table ( void )
44
-{
45
-/*	db_key_t keys[] = {domain_col}; */
46
-	db_val_t vals[1];
47
-	db_key_t cols[1];
48
-	db_res_t* res;
49
-	db_row_t* row;
50
-	db_val_t* val;
51
-
52
-	struct domain_list **new_hash_table;
53
-	int i;
54
-
55
-	cols[0] = domain_col.s;
56
-
57
-	if (db_use_table(db_handle, domain_table.s) < 0) {
58
-		LOG(L_ERR, "reload_domain_table(): Error while trying to use domain table\n");
59
-		return -1;
60
-	}
61
-
62
-	VAL_TYPE(vals) = DB_STR;
63
-	VAL_NULL(vals) = 0;
64
-    
65
-	if (db_query(db_handle, NULL, 0, NULL, cols, 0, 1, 0, &res) < 0) {
66
-		LOG(L_ERR, "reload_domain_table(): Error while querying database\n");
67
-		return -1;
68
-	}
69
-
70
-	/* Choose new hash table and free its old contents */
71
-	if (*hash_table == hash_table_1) {
72
-		hash_table_free(hash_table_2);
73
-		new_hash_table = hash_table_2;
74
-	} else {
75
-		hash_table_free(hash_table_1);
76
-		new_hash_table = hash_table_1;
77
-	}
78
-
79
-	row = RES_ROWS(res);
80
-
81
-	DBG("Number of rows in domain table: %d\n", RES_ROW_N(res));
82
-		
83
-	for (i = 0; i < RES_ROW_N(res); i++) {
84
-		val = ROW_VALUES(row + i);
85
-		if ((ROW_N(row) == 1) && (VAL_TYPE(val) == DB_STRING)) {
86
-			
87
-			DBG("Value: %s inserted into domain hash table\n", VAL_STRING(val));
88
-
89
-			if (hash_table_install(new_hash_table, (char *)(VAL_STRING(val))) == -1) {
90
-				LOG(L_ERR, "domain_reload(): Hash table problem\n");
91
-				db_free_query(db_handle, res);
92
-				return -1;
93
-			}
94
-		} else {
95
-			LOG(L_ERR, "domain_reload(): Database problem\n");
96
-			db_free_query(db_handle, res);
97
-			return -1;
98
-		}
99
-	}
100
-	db_free_query(db_handle, res);
101
-
102
-	*hash_table = new_hash_table;
103
-	
104
-	return 1;
105
-}
106 45
 
107 46
 
108 47
 /*
... ...
@@ -29,6 +29,8 @@
29 29
  * History:
30 30
  * --------
31 31
  * 2003-02-25 - created by janakj
32
+ * 2004-06-07   updated to the new DB api, added group_db_{bind,init,close,ver}
33
+ *               (andrei)
32 34
  *
33 35
  */
34 36
 
... ...
@@ -45,6 +47,10 @@
45 47
 #include "group_mod.h"                   /* Module parameters */
46 48
 
47 49
 
50
+static db_con_t* db_handle = 0;   /* Database connection handle */
51
+static db_func_t group_dbf;
52
+
53
+
48 54
 /*
49 55
  * Get Request-URI
50 56
  */
... ...
@@ -172,8 +178,9 @@ int is_user_in(struct sip_msg* _msg, char* _hf, char* _grp)
172 178
 
173 179
 	VAL_STR(vals + 1) = *((str*)_grp);
174 180
 	
175
-	db_use_table(db_handle, table.s);
176
-	if (db_query(db_handle, keys, 0, vals, col, (use_domain) ? (3): (2), 1, 0, &res) < 0) {
181
+	group_dbf.use_table(db_handle, table.s);
182
+	if (group_dbf.query(db_handle, keys, 0, vals, col, (use_domain) ? (3): (2),
183
+				1, 0, &res) < 0) {
177 184
 		LOG(L_ERR, "is_user_in(): Error while querying database\n");
178 185
 		return -5;
179 186
 	}
... ...
@@ -181,12 +188,71 @@ int is_user_in(struct sip_msg* _msg, char* _hf, char* _grp)
181 188
 	if (RES_ROW_N(res) == 0) {
182 189
 		DBG("is_user_in(): User is not in group '%.*s'\n", 
183 190
 		    ((str*)_grp)->len, ZSW(((str*)_grp)->s));
184
-		db_free_query(db_handle, res);
191
+		group_dbf.free_query(db_handle, res);
185 192
 		return -6;
186 193
 	} else {
187 194
 		DBG("is_user_in(): User is in group '%.*s'\n", 
188 195
 		    ((str*)_grp)->len, ZSW(((str*)_grp)->s));
189
-		db_free_query(db_handle, res);
196
+		group_dbf.free_query(db_handle, res);
190 197
 		return 1;
191 198
 	}
192 199
 }
200
+
201
+
202
+int group_db_init(char* db_url)
203
+{
204
+	if (group_dbf.init==0){
205
+		LOG(L_CRIT, "BUG: group_db_bind: null dbf \n");
206
+		goto error;
207
+	}
208
+	db_handle=group_dbf.init(db_url);
209
+	if (db_handle==0){
210
+		LOG(L_ERR, "ERROR: group_db_bind: unable to connect to the "
211
+				"database\n");
212
+		goto error;
213
+	}
214
+	return 0;
215
+error:
216
+	return -1;
217
+}
218
+
219
+
220
+int group_db_bind(char* db_url)
221
+{
222
+	if (bind_dbmod(db_url, &group_dbf)<0){
223
+		LOG(L_ERR, "ERROR: group_db_bind: unable to bind to the database"
224
+				" module\n");
225
+		return -1;
226
+	}
227
+	return 0;
228
+}
229
+
230
+
231
+void group_db_close()
232
+{
233
+	if (db_handle && group_dbf.close){
234
+		group_dbf.close(db_handle);
235
+		db_handle=0;
236
+	}
237
+}
238
+
239
+
240
+int group_db_ver(char* db_url, str* name)
241
+{
242
+	db_con_t* dbh;
243
+	int ver;
244
+
245
+	if (group_dbf.init==0){
246
+		LOG(L_CRIT, "BUG: group_db_ver: unbound database\n");
247
+		return -1;
248
+	}
249
+	dbh=group_dbf.init(db_url);
250
+	if (dbh==0){
251
+		LOG(L_ERR, "ERROR: group_db_ver: unable to open database "
252
+				"connection\n");
253
+		return -1;
254
+	}
255
+	ver=table_version(&group_dbf, dbh, name);
256
+	group_dbf.close(dbh);
257
+	return ver;
258
+}
... ...
@@ -45,4 +45,9 @@
45 45
 int is_user_in(struct sip_msg* _msg, char* _hf, char* _grp);
46 46
 
47 47
 
48
+int group_db_init(char* db_url);
49
+int group_db_bind(char* db_url);
50
+void group_db_close();
51
+int group_db_ver(char* db_url, str* name);
52
+
48 53
 #endif /* GROUP_H */
... ...
@@ -32,7 +32,8 @@
32 32
  *  2003-03-11 - New module interface (janakj)
33 33
  *  2003-03-16 - flags export parameter added (janakj)
34 34
  *  2003-03-19  all mallocs/frees replaced w/ pkg_malloc/pkg_free
35
- *  2003-04-05 default_uri #define used (jiri)
35
+ *  2003-04-05  default_uri #define used (jiri)
36
+ *  2004-06-07  updated to the new DB api: calls to group_db_* (andrei)
36 37
  */
37 38
 
38 39
 
... ...
@@ -89,14 +90,13 @@ static int hf_fixup(void** param, int param_no);
89 90
 /*
90 91
  * Module parameter variables
91 92
  */
92
-str db_url        = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
93
+static str db_url        = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
93 94
 str table         = {TABLE, TABLE_LEN};         /* Table name where group definitions are stored */
94 95
 str user_column   = {USER_COL, USER_COL_LEN};
95 96
 str domain_column = {DOMAIN_COL, DOMAIN_COL_LEN};
96 97
 str group_column  = {GROUP_COL, GROUP_COL_LEN};
97 98
 int use_domain    = 0;
98 99
 
99
-db_con_t* db_handle = 0;   /* Database connection handle */
100 100
 
101 101
 
102 102
 /*
... ...
@@ -139,13 +139,7 @@ struct module_exports exports = {
139 139
 
140 140
 static int child_init(int rank)
141 141
 {
142
-	db_handle = db_init(db_url.s);
143
-	if (!db_handle) {
144
-		LOG(L_ERR, "group:init_child(): Unable to connect database\n");
145
-		return -1;
146
-	}
147
-
148
-	return 0;
142
+	return group_db_init(db_url.s);
149 143
 }
150 144
 
151 145
 
... ...
@@ -163,38 +157,26 @@ static int mod_init(void)
163 157
 	group_column.len = strlen(group_column.s);
164 158
 
165 159
 	     /* Find a database module */
166
-	if (bind_dbmod(db_url.s)) {
167
-		LOG(L_ERR, "mod_init(): Unable to bind database module\n");
160
+	if (group_db_bind(db_url.s)) {
168 161
 		return -1;
169 162
 	}
170
-
171
-	db_handle = db_init(db_url.s);
172
-	if (!db_handle) {
173
-		LOG(L_ERR, "group:mod_init(): Unable to connect database\n");
174
-		return -1;
175
-	}
176
-
177
-	ver = table_version(db_handle, &table);
163
+	ver = group_db_ver(db_url.s, &table);
178 164
 	if (ver < 0) {
179 165
 		LOG(L_ERR, "group:mod_init(): Error while querying table version\n");
180
-		db_close(db_handle);
181 166
 		return -1;
182 167
 	} else if (ver < TABLE_VERSION) {
183
-		LOG(L_ERR, "group:mod_init(): Invalid table version (use ser_mysql.sh reinstall)\n");
184
-		db_close(db_handle);
168
+		LOG(L_ERR, "group:mod_init(): Invalid table version "
169
+				"(use ser_mysql.sh reinstall)\n");
185 170
 		return -1;
186 171
 	}
187 172
 	
188
-	db_close(db_handle);
189 173
 	return 0;
190 174
 }
191 175
 
192 176
 
193 177
 static void destroy(void)
194 178
 {
195
-	if (db_handle) {
196
-		db_close(db_handle);
197
-	}
179
+	group_db_close();
198 180
 }
199 181
 
200 182
 
... ...
@@ -44,13 +44,11 @@
44 44
 /*
45 45
  * Module parameters variables
46 46
  */
47
-extern str db_url;          /* Database URL */
48 47
 extern str table;           /* 'group' table name */
49 48
 extern str user_column;     /* 'user' column name in group table */
50 49
 extern str domain_column;   /* 'domain' column name in group table */
51 50
 extern str group_column;    /* "group' column name in group table */
52 51
 extern int use_domain;      /* Use domain in is_user_in */
53 52
 
54
-extern db_con_t* db_handle;   /* Database connection handle */
55 53
 
56 54
 #endif /* GROUP_MOD_H */
... ...
@@ -39,6 +39,7 @@
39 39
  * 2003-04-06 rank 0 changed to 1 in child_init (janakj)
40 40
  * 2003-06-19 fixed too many Jabber workers bug (mostly on RH9.0) (dcm)
41 41
  * 2003-08-05 adapted to the new parse_content_type_hdr function (bogdan)
42
+ * 2004-06-07 db API update (andrei)
42 43
  */
43 44
 
44 45
 
... ...
@@ -94,11 +95,12 @@ int xjab_connections(ih_req_p _irp, void *_p, char *_bb, int *_bl,
94 95
 xj_wlist jwl = NULL;
95 96
 
96 97
 /** Structure that represents database connection */
97
-db_con_t** db_con;
98
+static db_con_t** db_con;
99
+static db_func_t jabber_dbf;
98 100
 
99 101
 /** parameters */
100 102
 
101
-char *db_url   = "mysql://root@127.0.0.1/sip_jab";
103
+static char *db_url   = "mysql://root@127.0.0.1/sip_jab";
102 104
 char *db_table = "jusers";
103 105
 char *registrar=NULL; //"sip:registrar@iptel.org";
104 106
 
... ...
@@ -200,7 +202,7 @@ static int mod_init(void)
200 202
 	}
201 203
 
202 204
 	/* import mysql functions */
203
-	if (bind_dbmod(db_url))
205
+	if (bind_dbmod(db_url, &jabber_dbf)<0)
204 206
 	{
205 207
 		LOG(L_ERR, "XJAB:mod_init: error - database module not found\n");
206 208
 		return -1;
... ...
@@ -251,7 +253,7 @@ static int mod_init(void)
251 253
 	
252 254
 	for(i=0; i<nrw; i++)
253 255
 	{
254
-		db_con[i] = db_init(db_url);
256
+		db_con[i] = jabber_dbf.init(db_url);
255 257
 		if (!db_con[i])
256 258
 		{
257 259
 			LOG(L_ERR, "XJAB:mod_init: Error while connecting database\n");
... ...
@@ -259,7 +261,7 @@ static int mod_init(void)
259 261
 		}
260 262
 		else
261 263
 		{
262
-			db_use_table(db_con[i], db_table);
264
+			jabber_dbf.use_table(db_con[i], db_table);
263 265
 			DBG("XJAB:mod_init: Database connection opened successfuly\n");
264 266
 		}
265 267
 	}
... ...
@@ -340,7 +342,8 @@ static int child_init(int rank)
340 342
 										" pid\n");
341 343
 						return -1;
342 344
 					}
343
-					xj_worker_process(jwl,jaddress,jport,i,db_con[i]);
345
+					xj_worker_process(jwl,jaddress,jport,i,db_con[i],
346
+							&jabber_dbf);
344 347
 					exit(0);
345 348
 				}
346 349
 			}
... ...
@@ -667,7 +670,7 @@ void destroy(void)
667 670
 	if(db_con != NULL)
668 671
 	{
669 672
 		for(i = 0; i<nrw; i++)
670
-			db_close(db_con[i]);
673
+			jabber_dbf.close(db_con[i]);
671 674
 		shm_free(db_con);
672 675
 	}
673 676
 			
... ...
@@ -825,7 +828,7 @@ void xjab_check_workers(int mpid)
825 828
 					" worker's pid - w[%d]\n", i);
826 829
 				return;
827 830
 			}
828