Browse code

- basic support for fifo DB interface

Bogdan-Andrei Iancu authored on 22/10/2003 10:34:59
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,277 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2001-2003 Fhg Fokus
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
+
29
+
30
+#include <stdio.h>
31
+#include <stdlib.h>
32
+#include <strings.h>
33
+#include <string.h>
34
+#include <ctype.h>
35
+
36
+#include "../mem/mem.h"
37
+#include "../fifo_server.h"
38
+#include "../dprint.h"
39
+#include "../str.h"
40
+#include "db.h"
41
+
42
+#define MAX_SIZE_LINE 256
43
+#define MAX_ARRAY     32
44
+
45
+#define SELECT_CMD  1
46
+#define DELETE_CMD  2
47
+#define INSERT_CMD  3
48
+#define UPDATE_CMD  4
49
+#define SELECT_STR      "select"
50
+#define SELECT_STR_LEN  (sizeof(SELECT_STR)-1)
51
+#define DELETE_STR      "delete"
52
+#define DELETE_STR_LEN  (sizeof(DELETE_STR)-1)
53
+#define INSERT_STR      "insert"
54
+#define INSERT_STR_LEN  (sizeof(INSERT_STR)-1)
55
+#define UPDATE_STR      "update"
56
+#define UPDATE_STR_LEN  (sizeof(UPDATE_STR)-1)
57
+#define END_CHR    '.'
58
+
59
+
60
+#define trim_spaces(string) \
61
+	do { \
62
+		for(;*string.s==' ';string.s++,string.len--);\
63
+		for(;string.s[string.len-1]==' ';string.len--);\
64
+	}while(0)
65
+
66
+
67
+static char buf[MAX_SIZE_LINE];
68
+
69
+
70
+
71
+
72
+int get_avps( FILE *fifo , db_key_t *keys, db_op_t *ops, db_val_t *vals,
73
+													int *nr, int max_nr)
74
+{
75
+	str  line;
76
+	str  key,op,val;
77
+	char *c;
78
+
79
+	*nr = 0;
80
+
81
+	while(1) {
82
+		/* read a new line */
83
+		line.s = buf;
84
+		if (read_line( line.s, MAX_SIZE_LINE, fifo, &line.len)!=1) {
85
+			LOG(L_ERR,"ERROR:get_avps: cannot read avp(key|op|val)\n");
86
+			goto error;
87
+		}
88
+		trim_spaces(line);
89
+		/* is this the separter/end char? */
90
+		if (line.len==1 && *line.s==END_CHR)
91
+			return 1;
92
+		/* we have a new avp */
93
+		if (*nr<max_nr) {
94
+			/* parse the line key|op|val */
95
+			c = line.s;
96
+			/* parse thr key name */
97
+			for( key.s=c ; isalnum(*c)||*c=='_'||*c=='-' ; c++ );
98
+			key.len = c-key.s;
99
+			if (key.len==0) goto parse_error;
100
+			/* possible spaces? */
101
+			for( ; isspace(*c) ; c++ );
102
+			/* parse the operater */
103
+			op.s = c;
104
+			if (*c=='='||*c=='<'||*c=='>')
105
+				c++;
106
+			else
107
+				goto parse_error;
108
+			op.len = c - op.s;
109
+			/* possible spaces? */
110
+			for( ; isspace(*c) ; c++ );
111
+			/* parse value */
112
+			val.s = c;
113
+			val.len = line.len - (c-line.s);
114
+			/* duplicate the avp -> make all null terminated */
115
+			c = (char*)pkg_malloc(key.len+op.len+val.len+3);
116
+			if (c==0) {
117
+				LOG(L_ERR,"ERROR:get_avps: no more pkg memory\n");
118
+				goto error;
119
+			}
120
+			/*copy the key */
121
+			keys[*nr] = c;
122
+			memcpy( c, key.s, key.len);
123
+			c[key.len] = 0;
124
+			c += key.len;
125
+			/*copy the op */
126
+			ops[*nr] = c;
127
+			memcpy( c, op.s, op.len);
128
+			c[op.len] = 0;
129
+			c += op.len;
130
+			/*copy the val */
131
+			vals[*nr].val.string_val = c;
132
+			memcpy( c, val.s, val.len);
133
+			c[val.len] = 0;
134
+			c += val.len;
135
+			/* done */
136
+			(*nr)++;
137
+		} else {
138
+			LOG(L_WARN,"WARNING:get_avps: too many avps (max=%d), ignoring "
139
+				"\"%.*s\"\n",max_nr,line.len,line.s);
140
+		}
141
+	}
142
+parse_error:
143
+	LOG(L_ERR,"ERROR:get_avps: parse error in \"%.*s\" at char [%d][%c] "
144
+		"offset %d\n",line.len,line.s,*c,*c,c-line.s);
145
+error:
146
+	for(;*nr;*nr--)
147
+		pkg_free( (void*)keys[*nr] );
148
+	return -1;
149
+}
150
+
151
+
152
+
153
+int get_keys( FILE *fifo , db_key_t *keys, int *nr, int max_nr)
154
+{
155
+	str line;
156
+	char *key;
157
+
158
+	*nr = 0;
159
+
160
+	while(1) {
161
+		/* read a new line */
162
+		line.s = buf;
163
+		if (read_line( line.s, MAX_SIZE_LINE, fifo, &line.len)!=1) {
164
+			LOG(L_ERR,"ERROR:get_keys: cannot read key name\n");
165
+			goto error;
166
+		}
167
+		trim_spaces(line);
168
+		DBG("---read <%.*s>\n",line.len,line.s);
169
+		/* is this the separter/end char? */
170
+		if (line.len==1 && *line.s==END_CHR)
171
+			return 1;
172
+		/* we have a new key */
173
+		if (*nr<max_nr) {
174
+			/* duplicate the key -> null terminated */
175
+			key = (char*)pkg_malloc(line.len+1);
176
+			if (key==0) {
177
+				LOG(L_ERR,"ERROR:get_key: no more pkg memory\n");
178
+				goto error;
179
+			}
180
+			memcpy( key, line.s, line.len);
181
+			key[line.len] = 0;
182
+			keys[*nr] = key;
183
+			(*nr)++;
184
+		} else {
185
+			LOG(L_WARN,"WARNING:get_keys: too many keys (max=%d), ignoring "
186
+				"\"%.*s\"\n",max_nr,line.len,line.s);
187
+		}
188
+	}
189
+error:
190
+	for(;*nr;*nr--)
191
+		pkg_free( (void*)keys[*nr] );
192
+	return -1;
193
+}
194
+
195
+
196
+
197
+int db_fifo( FILE *fifo, char *response_file )
198
+{
199
+	static db_key_t keys1[MAX_ARRAY];
200
+	static db_op_t  ops1[MAX_ARRAY];
201
+	static db_val_t vals1[MAX_ARRAY];
202
+	static db_key_t keys2[MAX_ARRAY];
203
+	static db_op_t  ops2[MAX_ARRAY];
204
+	static db_val_t vals2[MAX_ARRAY];
205
+	FILE *rpl = 0;
206
+	str   line;
207
+	int   db_cmd;
208
+	int   nr1, nr2;
209
+
210
+	/* first check the response file */
211
+	rpl = open_reply_pipe( response_file );
212
+	if (rpl==0)
213
+		goto error;
214
+
215
+	/* first name must be the real name of the DB operation */
216
+	line.s = buf;
217
+	if (read_line( line.s, MAX_SIZE_LINE, fifo, &line.len)!=1) {
218
+		LOG(L_ERR,"ERROR:db_fifo: cannot read fifo cmd name\n");
219
+		goto error;
220
+	}
221
+	trim_spaces(line);
222
+
223
+	/* check the name of the command */
224
+	if (line.len==SELECT_STR_LEN
225
+	&& !strncasecmp( line.s, SELECT_STR, line.len)) {
226
+		db_cmd = SELECT_CMD;
227
+	} else if (line.len==DELETE_STR_LEN
228
+	&& !strncasecmp( line.s, DELETE_STR, line.len)) {
229
+		db_cmd = DELETE_CMD;
230
+	} else if (line.len==INSERT_STR_LEN
231
+	&& !strncasecmp( line.s, INSERT_STR, line.len)) {
232
+		db_cmd = INSERT_CMD;
233
+	} else if (line.len==UPDATE_STR_LEN
234
+	&& !strncasecmp( line.s, UPDATE_STR, line.len)) {
235
+		db_cmd = UPDATE_CMD;
236
+	} else {
237
+		LOG(L_ERR,"ERROR:db_fifo: unknown command \"%.*s\"\n",
238
+			line.len,line.s);
239
+		goto error;
240
+	}
241
+	DBG("DEBUG:db_fifo: cmd \"%.*s\" received\n",line.len,line.s);
242
+
243
+	if (db_cmd==SELECT_CMD) {
244
+		/* read the colums to be fetched */
245
+		if ( get_keys( fifo, keys1, &nr1, MAX_ARRAY)==-1 )
246
+			goto error;
247
+		if (nr1==0) {
248
+			LOG(L_ERR,"ERROR:db_fifo: no keys specified in SELECT cmd\n");
249
+			goto error;
250
+		}
251
+	} else if (db_cmd==UPDATE_CMD) {
252
+		/* read the col=val pairs to be updated */
253
+	}
254
+
255
+	/* read the table name */
256
+	line.s = buf;
257
+	if (read_line( line.s, MAX_SIZE_LINE, fifo, &line.len)!=1) {
258
+		LOG(L_ERR,"ERROR:db_fifo: cannot read table name\n");
259
+		goto error;
260
+	}
261
+	trim_spaces(line);
262
+
263
+	/*read 'where' avps */
264
+	if (get_avps( fifo , keys1, ops1, vals1, &nr2, MAX_ARRAY)==-1)
265
+		goto error;
266
+
267
+
268
+	if (db_cmd==SELECT_CMD) {
269
+		 
270
+	}
271
+
272
+	return 0;
273
+error:
274
+	if (rpl) fclose(rpl);
275
+	return -1;
276
+}
277
+
0 278
new file mode 100644
... ...
@@ -0,0 +1,36 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2001-2003 Fhg Fokus
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
+
29
+#include "db.h"
30
+
31
+#define db_fifo_cmd  db_fifo
32
+#define FIFO_DB      "DB"
33
+
34
+int db_fifo( FILE *fifo_stream, char *response_file );
35
+
36
+
... ...
@@ -1473,11 +1473,11 @@ try_again:
1473 1473
 		goto error;
1474 1474
 	}
1475 1475
 	
1476
-	/* register a diagnostic FIFO command */
1476
+	/* register a diagnostic FIFO command  - moved to fifo server - bogdan
1477 1477
 	if (register_core_fifo()<0) {
1478 1478
 		LOG(L_CRIT, "unable to register core FIFO commands\n");
1479 1479
 		goto error;
1480
-	}
1480
+	}*/
1481 1481
 
1482 1482
 	/*register builtin  modules*/
1483 1483
 	register_builtin_modules();