Bladeren bron

- More sane sources, additional include paths in Makefile

Jan Janak 22 jaren geleden
bovenliggende
commit
37f120c108

+ 6 - 1
modules/db_mysql/Makefile

@@ -9,7 +9,12 @@ NAME=mysql.so
 
 # mysql.h locations (freebsd,openbsd  solaris)
 DEFS +=-I$(LOCALBASE)/include -I$(LOCALBASE)/include/mysql \
-		-I$(LOCALBASE)/mysql/include -I/usr/pkg/include
+		-I$(LOCALBASE)/mysql/include -I/usr/pkg/include \
+		-I/usr/include/mysql
+
+# Database API, standard includes
+DEFS +=-I../../db -I../.. -I../../mem
+
 # libmysqlclient locations on RH/Suse, Solaris /OpenBSD, FreeBSD
 # (Debian does the right thing and puts it in /usr/lib)
 LIBS=-L/usr/lib/mysql -L$(LOCALBASE)/lib -L$(LOCALBASE)/lib/mysql \

+ 1 - 1
modules/db_mysql/db_con.c

@@ -29,7 +29,7 @@
 
 
 #include <string.h>
-#include "../../db/db_con.h"
+#include "../../db/db.h"
 #include "../../dprint.h"
 #include "../../mem/mem.h"
 #include "defs.h"

+ 7 - 19
modules/db_mysql/db_mod.c

@@ -33,21 +33,17 @@
  *  2003-03-16  flags export parameter added (janakj)
  */
 
-#include <stdio.h>
-#include "../../sr_module.h"
+#include <sr_module.h>
 #include "dbase.h"
 
-MODULE_VERSION
-
 
-static int mod_init(void);
+MODULE_VERSION
 
 
 /*
  * MySQL database module interface
  */
 
-
 static cmd_export_t cmds[]={
 	{"db_use_table",  (cmd_function)use_table,     2, 0, 0},
 	{"db_init",       (cmd_function)db_init,       1, 0, 0},
@@ -62,22 +58,14 @@ static cmd_export_t cmds[]={
 };
 
 
-
 struct module_exports exports = {	
 	"mysql",
 	cmds,
 	0,   /*  module paramers */
 
-	mod_init, /* module initialization function */
-	0,        /* response function*/
-	0,        /* destroy function */
-	0,        /* oncancel function */
-	0         /* per-child init function */
+	0,   /* module initialization function */
+	0,   /* response function*/
+	0,   /* destroy function */
+	0,   /* oncancel function */
+	0    /* per-child init function */
 };
-
-
-static int mod_init(void)
-{
-	DBG("mysql - initializing\n");
-	return 0;
-}

+ 8 - 7
modules/db_mysql/dbase.c

@@ -27,17 +27,18 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-
+#include <stdio.h>
+#include <mem.h>
+#include <mysql.h>
+#include <dprint.h>
 #include <string.h>
 #include <stdlib.h>
-#include <stdio.h>
-#include "../../dprint.h"
-#include "../../mem/mem.h"
-#include "db_utils.h"
 #include "defs.h"
-#include "dbase.h"
+#include "utils.h"
+#include "val.h"
 #include "con_mysql.h"
-
+#include "res.h"
+#include "dbase.h"
 
 static char sql_buf[SQL_BUF_LEN];
 

+ 12 - 5
modules/db_mysql/dbase.h

@@ -31,11 +31,11 @@
 #ifndef DBASE_H
 #define DBASE_H
 
-#include "../../db/db_con.h"
-#include "../../db/db_res.h"
-#include "../../db/db_key.h"
-#include "../../db/db_op.h"
-#include "../../db/db_val.h"
+#include <db_con.h>
+#include <db_res.h>
+#include <db_key.h>
+#include <db_op.h>
+#include <db_val.h>
 
 
 /*
@@ -94,4 +94,11 @@ int db_update(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
 	      db_key_t* _uk, db_val_t* _uv, int _n, int _un);
 
 
+/*
+ * Store name of table that will be used by
+ * subsequent database functions
+ */
+int use_table(db_con_t* _h, const char* _t);
+
+
 #endif /* DBASE_H */

+ 6 - 5
modules/db_mysql/db_res.c → modules/db_mysql/res.c

@@ -27,13 +27,14 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include <stdlib.h>
-#include <mysql/mysql.h>
-#include "../../db/db_res.h"
-#include "../../dprint.h"
-#include "../../mem/mem.h"
+
+#include <mysql.h>
+#include <mem.h>
+#include <dprint.h>
+#include "row.h"
 #include "defs.h"
 #include "con_mysql.h"
+#include "res.h"
 
 
 /*

+ 55 - 0
modules/db_mysql/res.h

@@ -0,0 +1,55 @@
+/* 
+ * $Id$ 
+ *
+ * MySQL module result related functions
+ *
+ * Copyright (C) 2001-2003 Fhg Fokus
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef RES_H
+#define RES_H
+
+#include <db_res.h>
+#include <db_con.h>
+
+
+/*
+ * Create a new result structure and initialize it
+ */
+db_res_t* new_result(void);
+
+
+/*
+ * Fill the structure with data from database
+ */
+int convert_result(db_con_t* _h, db_res_t* _r);
+
+
+/*
+ * Release memory used by a result structure
+ */
+int free_result(db_res_t* _r);
+
+
+#endif /* RES_H */

+ 7 - 6
modules/db_mysql/db_row.c → modules/db_mysql/row.c

@@ -27,12 +27,13 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+
+#include <dprint.h>
+#include <mem.h>
 #include <mysql/mysql.h>
-#include "../../db/db_row.h"
-#include "../../dprint.h"
-#include "../../mem/mem.h"
-#include "defs.h"
+#include "val.h"
 #include "con_mysql.h"
+#include "row.h"
 
 
 /*
@@ -43,8 +44,8 @@ int convert_row(db_con_t* _h, db_res_t* _res, db_row_t* _r)
 	unsigned long* lengths;
 	int i;
 #ifndef PARANOID
-	if ((!_h) || (!_r) || (!_n)) {
-		log(L_ERR, "convert_row(): Invalid parameter value\n");
+	if ((!_h) || (!_res) || (!_r)) {
+		LOG(L_ERR, "convert_row(): Invalid parameter value\n");
 		return -1;
 	}
 #endif

+ 50 - 0
modules/db_mysql/row.h

@@ -0,0 +1,50 @@
+/* 
+ * $Id$ 
+ *
+ * MySQL module row related functions
+ *
+ * Copyright (C) 2001-2003 Fhg Fokus
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef ROW_H
+#define ROW_H
+
+#include <db_con.h>
+#include <db_res.h>
+#include <db_row.h>
+
+
+/*
+ * Convert a row from result into db API representation
+ */
+int convert_row(db_con_t* _h, db_res_t* _res, db_row_t* _r);
+
+
+/*
+ * Release memory used by row
+ */
+int free_row(db_row_t* _r);
+
+
+#endif /* ROW_H */

+ 117 - 2
modules/db_mysql/utils.c

@@ -25,12 +25,18 @@
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * History:
+ * -------
+ * 2003-04-14 tm_isdst in struct tm set to -1 to let mktime 
+ *            guess daylight saving (janakj)
  */
 
+#define _XOPEN_SOURCE
+
 #include <string.h>
-#include "utils.h"
 #include "defs.h"
-
+#include "utils.h"
 
 /* FIXME: to be removed */
 
@@ -61,3 +67,112 @@ char* trim(char* _s)
 
 	return _s;
 }
+
+
+/*
+ * Convert time_t structure to format accepted by MySQL database
+ */
+int time2mysql(time_t _time, char* _result, int _res_len)
+{
+	struct tm* t;
+	
+	     /*
+	       if (_time == MAX_TIME_T) {
+	       snprintf(_result, _res_len, "0000-00-00 00:00:00");
+	       }
+	     */
+
+	t = localtime(&_time);
+	return strftime(_result, _res_len, "%Y-%m-%d %H:%M:%S", t);
+}
+
+
+/*
+ * Convert MySQL time representation to time_t structure
+ */
+time_t mysql2time(const char* _str)
+{
+	struct tm time;
+	
+	     /* It is neccessary to zero tm structure first */
+	memset(&time, '\0', sizeof(struct tm));
+	strptime(_str, "%Y-%m-%d %H:%M:%S", &time);
+
+	     /* Daylight saving information got lost in the database
+	      * so let mktime to guess it. This eliminates the bug when
+	      * contacts reloaded from the database have different time
+	      * of expiration by one hour when daylight saving is used
+	      */ 
+	time.tm_isdst = -1;   
+	return mktime(&time);
+}
+
+
+/* FIXME */
+/*
+ * SQL URL parser
+ */
+int parse_sql_url(char* _url, char** _user, char** _pass, 
+		  char** _host, char** _port, char** _db)
+{
+	char* slash, *dcolon, *at, *db_slash;
+
+	*_user = '\0';
+	*_pass = '\0';
+	*_host = '\0';
+	*_port = '\0';
+	*_db   = '\0';
+
+	     /* Remove any leading and trailing spaces and tab */
+	_url = trim(_url);
+
+	if (strlen(_url) < 6) return -1;
+
+	if (*_url == '\0') return -2; /* Empty string */
+
+	slash = strchr(_url, '/');
+	if (!slash) return -3;   /* Invalid string, slashes not found */
+
+	if ((*(++slash)) != '/') {  /* Invalid URL, 2nd slash not found */
+		return -4;
+	}
+
+	slash++;
+
+	at = strchr(slash, '@');
+
+	db_slash = strchr(slash, '/');
+	if (db_slash) {
+		*db_slash++ = '\0';
+		*_db = trim(db_slash);
+	}
+
+	if (!at) {
+		dcolon = strchr(slash, ':');
+		if (dcolon) {
+			*dcolon++ = '\0';
+			*_port = trim(dcolon);
+		}
+		*_host = trim(slash);
+	} else {
+		dcolon = strchr(slash, ':');
+	        *at++ = '\0';
+		if (dcolon) {
+			*dcolon++ = '\0';
+			if (dcolon < at) {   /* user:passwd */
+				*_pass = trim(dcolon);
+				dcolon = strchr(at, ':');
+				if (dcolon) {  /* host:port */
+					*dcolon++ = '\0';
+					*_port = trim(dcolon);
+				}
+			} else {            /* host:port */
+				*_port = trim(dcolon);
+			}
+		}
+		*_host = trim(at);
+		*_user = trim(slash);
+	}
+
+	return 0;
+}

+ 23 - 0
modules/db_mysql/utils.h

@@ -31,6 +31,29 @@
 #ifndef UTILS_H
 #define UTILS_H
 
+#include <time.h>
+
+
 char* trim(char* _s);
 
+
+/*
+ * Convert time_t structure to format accepted by MySQL database
+ */
+int time2mysql(time_t _time, char* _result, int _res_len);
+
+
+/*
+ * Convert MySQL time representation to time_t structure
+ */
+time_t mysql2time(const char* _str);
+
+
+/*
+ * SQL URL parser
+ */
+int parse_sql_url(char* _url, char** _user, char** _pass, 
+		  char** _host, char** _port, char** _db);
+
+
 #endif /* UTILS_H */

+ 5 - 5
modules/db_mysql/db_val.c → modules/db_mysql/val.c

@@ -25,14 +25,14 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include <stdlib.h>
 #include <stdio.h>
+#include <dprint.h>
+#include <stdlib.h>
 #include <string.h>
-#include <mysql/mysql.h>
-#include "../../db/db_val.h"
-#include "../../dprint.h"
+#include <mysql.h>
+#include "utils.h"
 #include "defs.h"
-#include "db_utils.h"
+#include "val.h"
 
 
 /*

+ 46 - 0
modules/db_mysql/val.h

@@ -0,0 +1,46 @@
+/* 
+ * $Id$ 
+ *
+ * Copyright (C) 2001-2003 Fhg Fokus
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef VAL_H
+#define VAL_H
+
+#include <db_val.h>
+
+
+/*
+ * Does not copy strings
+ */
+int str2val(db_type_t _t, db_val_t* _v, const char* _s, int _l);
+
+
+/*
+ * Used when converting result from a query
+ */
+int val2str(db_val_t* _v, char* _s, int* _len);
+
+
+#endif /* VAL_H */