Browse code

- Replaced with db_uri structure

Jan Janak authored on 29/03/2007 10:06:17
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,280 +0,0 @@
1
-/* 
2
- * $Id$
3
- *
4
- * Copyright (C) 2001-2005 iptel.org
5
- *
6
- * This file is part of ser, a free SIP server.
7
- *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License 
24
- * along with this program; if not, write to the Free Software 
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
- */
27
-
28
-#include "db_id.h"
29
-#include "../dprint.h"
30
-#include "../mem/mem.h"
31
-#include "../ut.h"
32
-#include <stdlib.h>
33
-#include <string.h>
34
-
35
-
36
-/*
37
- * Duplicate a string
38
- */
39
-static int dupl_string(char** dst, const char* begin, const char* end)
40
-{
41
-	if (*dst) pkg_free(*dst);
42
-
43
-	*dst = pkg_malloc(end - begin + 1);
44
-	if ((*dst) == NULL) {
45
-		return -1;
46
-	}
47
-
48
-	memcpy(*dst, begin, end - begin);
49
-	(*dst)[end - begin] = '\0';
50
-	return 0;
51
-}
52
-
53
-
54
-/*
55
- * Parse a database URL of form 
56
- * scheme://[username[:password]@]hostname[:port]/database
57
- *
58
- * Returns 0 if parsing was successful and -1 otherwise
59
- */
60
-static int parse_db_url(struct db_id* id, const char* url)
61
-{
62
-#define SHORTEST_DB_URL "s://a/b"
63
-#define SHORTEST_DB_URL_LEN (sizeof(SHORTEST_DB_URL) - 1)
64
-
65
-	enum state {
66
-		ST_SCHEME,     /* Scheme part */
67
-		ST_SLASH1,     /* First slash */
68
-		ST_SLASH2,     /* Second slash */
69
-		ST_USER_HOST,  /* Username or hostname */
70
-		ST_PASS_PORT,  /* Password or port part */
71
-		ST_HOST,       /* Hostname part */
72
-		ST_PORT,       /* Port part */
73
-		ST_DB          /* Database part */
74
-	};
75
-
76
-	enum state st;
77
-	int len, i;
78
-	const char* begin;
79
-	char* prev_token;
80
-
81
-	prev_token = 0;
82
-
83
-	if (!id || !url) {
84
-		goto err;
85
-	}
86
-	
87
-	len = strlen(url);
88
-	if (len < SHORTEST_DB_URL_LEN) {
89
-		goto err;
90
-	}
91
-	
92
-	     /* Initialize all attributes to 0 */
93
-	memset(id, 0, sizeof(struct db_id));
94
-	st = ST_SCHEME;
95
-	begin = url;
96
-
97
-	for(i = 0; i < len; i++) {
98
-		switch(st) {
99
-		case ST_SCHEME:
100
-			switch(url[i]) {
101
-			case ':':
102
-				st = ST_SLASH1;
103
-				if (dupl_string(&id->scheme, begin, url + i) < 0) goto err;
104
-				break;
105
-			}
106
-			break;
107
-
108
-		case ST_SLASH1:
109
-			switch(url[i]) {
110
-			case '/':
111
-				st = ST_SLASH2;
112
-				break;
113
-
114
-			default:
115
-				goto err;
116
-			}
117
-			break;
118
-
119
-		case ST_SLASH2:
120
-			switch(url[i]) {
121
-			case '/':
122
-				st = ST_USER_HOST;
123
-				begin = url + i + 1;
124
-				break;
125
-				
126
-			default:
127
-				goto err;
128
-			}
129
-			break;
130
-
131
-		case ST_USER_HOST:
132
-			switch(url[i]) {
133
-			case '@':
134
-				st = ST_HOST;
135
-				if (dupl_string(&id->username, begin, url + i) < 0) goto err;
136
-				begin = url + i + 1;
137
-				break;
138
-
139
-			case ':':
140
-				st = ST_PASS_PORT;
141
-				if (dupl_string(&prev_token, begin, url + i) < 0) goto err;
142
-				begin = url + i + 1;
143
-				break;
144
-
145
-			case '/':
146
-				if (dupl_string(&id->host, begin, url + i) < 0) goto err;
147
-				if (dupl_string(&id->database, url + i + 1, url + len) < 0) goto err;
148
-				return 0;
149
-			}
150
-			break;
151
-
152
-		case ST_PASS_PORT:
153
-			switch(url[i]) {
154
-			case '@':
155
-				st = ST_HOST;
156
-				id->username = prev_token;
157
-				if (dupl_string(&id->password, begin, url + i) < 0) goto err;
158
-				begin = url + i + 1;
159
-				break;
160
-
161
-			case '/':
162
-				id->host = prev_token;
163
-				id->port = str2s(begin, url + i - begin, 0);
164
-				if (dupl_string(&id->database, url + i + 1, url + len) < 0) goto err;
165
-				return 0;
166
-			}
167
-			break;
168
-
169
-		case ST_HOST:
170
-			switch(url[i]) {
171
-			case ':':
172
-				st = ST_PORT;
173
-				if (dupl_string(&id->host, begin, url + i) < 0) goto err;
174
-				begin = url + i + 1;
175
-				break;
176
-
177
-			case '/':
178
-				if (dupl_string(&id->host, begin, url + i) < 0) goto err;
179
-				if (dupl_string(&id->database, url + i + 1, url + len) < 0) goto err;
180
-				return 0;
181
-			}
182
-			break;
183
-
184
-		case ST_PORT:
185
-			switch(url[i]) {
186
-			case '/':
187
-				id->port = str2s(begin, url + i - begin, 0);
188
-				if (dupl_string(&id->database, url + i + 1, url + len) < 0) goto err;
189
-				return 0;
190
-			}
191
-			break;
192
-			
193
-		case ST_DB:
194
-			break;
195
-		}
196
-	}
197
-
198
-	if (st != ST_DB) goto err;
199
-	return 0;
200
-
201
- err:
202
-	if (id->scheme) pkg_free(id->scheme);
203
-	if (id->username) pkg_free(id->username);
204
-	if (id->password) pkg_free(id->password);
205
-	if (id->host) pkg_free(id->host);
206
-	if (id->database) pkg_free(id->database);
207
-	if (prev_token) pkg_free(prev_token);
208
-	return -1;
209
-}
210
-
211
-
212
-/*
213
- * Create a new connection identifier
214
- */
215
-struct db_id* new_db_id(const char* url)
216
-{
217
-	struct db_id* ptr;
218
-
219
-	if (!url) {
220
-		LOG(L_ERR, "new_db_id: Invalid parameter\n");
221
-		return 0;
222
-	}
223
-
224
-	ptr = (struct db_id*)pkg_malloc(sizeof(struct db_id));
225
-	if (!ptr) {
226
-		LOG(L_ERR, "new_db_id: No memory left\n");
227
-		goto err;
228
-	}
229
-	memset(ptr, 0, sizeof(struct db_id));
230
-
231
-	if (parse_db_url(ptr, url) < 0) {
232
-		LOG(L_ERR, "new_db_id: Error while parsing database URL: %s\n", url);
233
-		goto err;
234
-	}
235
-
236
-	return ptr;
237
-
238
- err:
239
-	if (ptr) pkg_free(ptr);
240
-	return 0;
241
-}
242
-
243
-
244
-/* compare s1 & s2  with a function f (which should return 0 if ==);
245
- * s1 & s2 can be null
246
- * return 0 if match, 1 if not */
247
-#define cmpstr(s1, s2, f) \
248
-	((s1)!=(s2)) && ((s1)==0 || (s2)==0 || (f)((s1), (s2))!=0)
249
-
250
-/*
251
- * Compare two connection identifiers
252
- */
253
-unsigned char cmp_db_id(struct db_id* id1, struct db_id* id2)
254
-{
255
-	if (!id1 || !id2) return 0;
256
-	if (id1->port != id2->port) return 0;
257
-
258
-	if (cmpstr(id1->scheme, id2->scheme, strcmp)) return 0;
259
-	if (cmpstr(id1->username, id2->username, strcmp)) return 0;
260
-	if (cmpstr(id1->password, id2->password, strcmp)) return 0;
261
-	if (cmpstr(id1->host, id2->host, strcasecmp)) return 0;
262
-	if (cmpstr(id1->database, id2->database, strcmp)) return 0;
263
-	return 1;
264
-}
265
-
266
-
267
-/*
268
- * Free a connection identifier
269
- */
270
-void free_db_id(struct db_id* id)
271
-{
272
-	if (!id) return;
273
-
274
-	if (id->scheme) pkg_free(id->scheme);
275
-	if (id->username) pkg_free(id->username);
276
-	if (id->password) pkg_free(id->password);
277
-	if (id->host) pkg_free(id->host);
278
-	if (id->database) pkg_free(id->database);
279
-	pkg_free(id);
280
-}
Browse code

- db: fix cmp_db_id to work also with null strings

Andrei Pelinescu-Onciul authored on 10/11/2006 18:21:14
Showing 1 changed files
... ...
@@ -241,6 +241,12 @@ struct db_id* new_db_id(const char* url)
241 241
 }
242 242
 
243 243
 
244
+/* compare s1 & s2  with a function f (which should return 0 if ==);
245
+ * s1 & s2 can be null
246
+ * return 0 if match, 1 if not */
247
+#define cmpstr(s1, s2, f) \
248
+	((s1)!=(s2)) && ((s1)==0 || (s2)==0 || (f)((s1), (s2))!=0)
249
+
244 250
 /*
245 251
  * Compare two connection identifiers
246 252
  */
... ...
@@ -249,11 +255,11 @@ unsigned char cmp_db_id(struct db_id* id1, struct db_id* id2)
249 255
 	if (!id1 || !id2) return 0;
250 256
 	if (id1->port != id2->port) return 0;
251 257
 
252
-	if (strcmp(id1->scheme, id2->scheme)) return 0;
253
-	if (strcmp(id1->username, id2->username)) return 0;
254
-	if (strcmp(id1->password, id2->password)) return 0;
255
-	if (strcasecmp(id1->host, id2->host)) return 0;
256
-	if (strcmp(id1->database, id2->database)) return 0;
258
+	if (cmpstr(id1->scheme, id2->scheme, strcmp)) return 0;
259
+	if (cmpstr(id1->username, id2->username, strcmp)) return 0;
260
+	if (cmpstr(id1->password, id2->password, strcmp)) return 0;
261
+	if (cmpstr(id1->host, id2->host, strcasecmp)) return 0;
262
+	if (cmpstr(id1->database, id2->database, strcmp)) return 0;
257 263
 	return 1;
258 264
 }
259 265
 
Browse code

- improved database connection pool

Jan Janak authored on 02/02/2005 19:08:43
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,274 @@
1
+/* 
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2001-2005 iptel.org
5
+ *
6
+ * This file is part of ser, a free SIP server.
7
+ *
8
+ * ser is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * For a license to use the ser software under conditions
14
+ * other than those described here, or to purchase support for this
15
+ * software, please contact iptel.org by e-mail at the following addresses:
16
+ *    info@iptel.org
17
+ *
18
+ * ser is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License 
24
+ * along with this program; if not, write to the Free Software 
25
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
+ */
27
+
28
+#include "db_id.h"
29
+#include "../dprint.h"
30
+#include "../mem/mem.h"
31
+#include "../ut.h"
32
+#include <stdlib.h>
33
+#include <string.h>
34
+
35
+
36
+/*
37
+ * Duplicate a string
38
+ */
39
+static int dupl_string(char** dst, const char* begin, const char* end)
40
+{
41
+	if (*dst) pkg_free(*dst);
42
+
43
+	*dst = pkg_malloc(end - begin + 1);
44
+	if ((*dst) == NULL) {
45
+		return -1;
46
+	}
47
+
48
+	memcpy(*dst, begin, end - begin);
49
+	(*dst)[end - begin] = '\0';
50
+	return 0;
51
+}
52
+
53
+
54
+/*
55
+ * Parse a database URL of form 
56
+ * scheme://[username[:password]@]hostname[:port]/database
57
+ *
58
+ * Returns 0 if parsing was successful and -1 otherwise
59
+ */
60
+static int parse_db_url(struct db_id* id, const char* url)
61
+{
62
+#define SHORTEST_DB_URL "s://a/b"
63
+#define SHORTEST_DB_URL_LEN (sizeof(SHORTEST_DB_URL) - 1)
64
+
65
+	enum state {
66
+		ST_SCHEME,     /* Scheme part */
67
+		ST_SLASH1,     /* First slash */
68
+		ST_SLASH2,     /* Second slash */
69
+		ST_USER_HOST,  /* Username or hostname */
70
+		ST_PASS_PORT,  /* Password or port part */
71
+		ST_HOST,       /* Hostname part */
72
+		ST_PORT,       /* Port part */
73
+		ST_DB          /* Database part */
74
+	};
75
+
76
+	enum state st;
77
+	int len, i;
78
+	const char* begin;
79
+	char* prev_token;
80
+
81
+	prev_token = 0;
82
+
83
+	if (!id || !url) {
84
+		goto err;
85
+	}
86
+	
87
+	len = strlen(url);
88
+	if (len < SHORTEST_DB_URL_LEN) {
89
+		goto err;
90
+	}
91
+	
92
+	     /* Initialize all attributes to 0 */
93
+	memset(id, 0, sizeof(struct db_id));
94
+	st = ST_SCHEME;
95
+	begin = url;
96
+
97
+	for(i = 0; i < len; i++) {
98
+		switch(st) {
99
+		case ST_SCHEME:
100
+			switch(url[i]) {
101
+			case ':':
102
+				st = ST_SLASH1;
103
+				if (dupl_string(&id->scheme, begin, url + i) < 0) goto err;
104
+				break;
105
+			}
106
+			break;
107
+
108
+		case ST_SLASH1:
109
+			switch(url[i]) {
110
+			case '/':
111
+				st = ST_SLASH2;
112
+				break;
113
+
114
+			default:
115
+				goto err;
116
+			}
117
+			break;
118
+
119
+		case ST_SLASH2:
120
+			switch(url[i]) {
121
+			case '/':
122
+				st = ST_USER_HOST;
123
+				begin = url + i + 1;
124
+				break;
125
+				
126
+			default:
127
+				goto err;
128
+			}
129
+			break;
130
+
131
+		case ST_USER_HOST:
132
+			switch(url[i]) {
133
+			case '@':
134
+				st = ST_HOST;
135
+				if (dupl_string(&id->username, begin, url + i) < 0) goto err;
136
+				begin = url + i + 1;
137
+				break;
138
+
139
+			case ':':
140
+				st = ST_PASS_PORT;
141
+				if (dupl_string(&prev_token, begin, url + i) < 0) goto err;
142
+				begin = url + i + 1;
143
+				break;
144
+
145
+			case '/':
146
+				if (dupl_string(&id->host, begin, url + i) < 0) goto err;
147
+				if (dupl_string(&id->database, url + i + 1, url + len) < 0) goto err;
148
+				return 0;
149
+			}
150
+			break;
151
+
152
+		case ST_PASS_PORT:
153
+			switch(url[i]) {
154
+			case '@':
155
+				st = ST_HOST;
156
+				id->username = prev_token;
157
+				if (dupl_string(&id->password, begin, url + i) < 0) goto err;
158
+				begin = url + i + 1;
159
+				break;
160
+
161
+			case '/':
162
+				id->host = prev_token;
163
+				id->port = str2s(begin, url + i - begin, 0);
164
+				if (dupl_string(&id->database, url + i + 1, url + len) < 0) goto err;
165
+				return 0;
166
+			}
167
+			break;
168
+
169
+		case ST_HOST:
170
+			switch(url[i]) {
171
+			case ':':
172
+				st = ST_PORT;
173
+				if (dupl_string(&id->host, begin, url + i) < 0) goto err;
174
+				begin = url + i + 1;
175
+				break;
176
+
177
+			case '/':
178
+				if (dupl_string(&id->host, begin, url + i) < 0) goto err;
179
+				if (dupl_string(&id->database, url + i + 1, url + len) < 0) goto err;
180
+				return 0;
181
+			}
182
+			break;
183
+
184
+		case ST_PORT:
185
+			switch(url[i]) {
186
+			case '/':
187
+				id->port = str2s(begin, url + i - begin, 0);
188
+				if (dupl_string(&id->database, url + i + 1, url + len) < 0) goto err;
189
+				return 0;
190
+			}
191
+			break;
192
+			
193
+		case ST_DB:
194
+			break;
195
+		}
196
+	}
197
+
198
+	if (st != ST_DB) goto err;
199
+	return 0;
200
+
201
+ err:
202
+	if (id->scheme) pkg_free(id->scheme);
203
+	if (id->username) pkg_free(id->username);
204
+	if (id->password) pkg_free(id->password);
205
+	if (id->host) pkg_free(id->host);
206
+	if (id->database) pkg_free(id->database);
207
+	if (prev_token) pkg_free(prev_token);
208
+	return -1;
209
+}
210
+
211
+
212
+/*
213
+ * Create a new connection identifier
214
+ */
215
+struct db_id* new_db_id(const char* url)
216
+{
217
+	struct db_id* ptr;
218
+
219
+	if (!url) {
220
+		LOG(L_ERR, "new_db_id: Invalid parameter\n");
221
+		return 0;
222
+	}
223
+
224
+	ptr = (struct db_id*)pkg_malloc(sizeof(struct db_id));
225
+	if (!ptr) {
226
+		LOG(L_ERR, "new_db_id: No memory left\n");
227
+		goto err;
228
+	}
229
+	memset(ptr, 0, sizeof(struct db_id));
230
+
231
+	if (parse_db_url(ptr, url) < 0) {
232
+		LOG(L_ERR, "new_db_id: Error while parsing database URL: %s\n", url);
233
+		goto err;
234
+	}
235
+
236
+	return ptr;
237
+
238
+ err:
239
+	if (ptr) pkg_free(ptr);
240
+	return 0;
241
+}
242
+
243
+
244
+/*
245
+ * Compare two connection identifiers
246
+ */
247
+unsigned char cmp_db_id(struct db_id* id1, struct db_id* id2)
248
+{
249
+	if (!id1 || !id2) return 0;
250
+	if (id1->port != id2->port) return 0;
251
+
252
+	if (strcmp(id1->scheme, id2->scheme)) return 0;
253
+	if (strcmp(id1->username, id2->username)) return 0;
254
+	if (strcmp(id1->password, id2->password)) return 0;
255
+	if (strcasecmp(id1->host, id2->host)) return 0;
256
+	if (strcmp(id1->database, id2->database)) return 0;
257
+	return 1;
258
+}
259
+
260
+
261
+/*
262
+ * Free a connection identifier
263
+ */
264
+void free_db_id(struct db_id* id)
265
+{
266
+	if (!id) return;
267
+
268
+	if (id->scheme) pkg_free(id->scheme);
269
+	if (id->username) pkg_free(id->username);
270
+	if (id->password) pkg_free(id->password);
271
+	if (id->host) pkg_free(id->host);
272
+	if (id->database) pkg_free(id->database);
273
+	pkg_free(id);
274
+}