Browse code

sqlops: allow to set the size of sql escape buffer for transformations

- alternative to GH #899

Daniel-Constantin Mierla authored on 08/01/2017 21:10:59
Showing 1 changed files
... ...
@@ -31,30 +31,56 @@
31 31
 
32 32
 #include "sql_trans.h"
33 33
 
34
-#define TR_BUFFER_SIZE 2048
34
+int sqlops_tr_buf_size = 2048;
35 35
 
36
+static char *_sqlops_tr_buffer = NULL;
37
+
38
+int sqlops_tr_buffer_init(void)
39
+{
40
+	if(_sqlops_tr_buffer!=NULL)
41
+		return 0;
42
+	if(sqlops_tr_buf_size<=0) {
43
+		LM_ERR("invalid buffer size: %d\n", sqlops_tr_buf_size);
44
+		return -1;
45
+	}
46
+	_sqlops_tr_buffer = pkg_malloc(sqlops_tr_buf_size * sizeof(char));
47
+	if(_sqlops_tr_buffer==NULL) {
48
+		LM_ERR("no more pkg memory\n");
49
+		return -1;
50
+	}
51
+	return 0;
52
+}
53
+
54
+void sqlops_tr_buffer_destroy(void)
55
+{
56
+	if(_sqlops_tr_buffer==NULL)
57
+		return;
58
+
59
+	pkg_free(_sqlops_tr_buffer);
60
+
61
+	_sqlops_tr_buffer = NULL;
62
+}
36 63
 
37 64
 static int _tr_eval_sql_val(pv_value_t *val)
38 65
 {
39 66
 	int i;
40
-	static char _tr_buffer[TR_BUFFER_SIZE];
41 67
 
42 68
 	if(val->flags&PV_TYPE_INT || !(val->flags&PV_VAL_STR)) {
43 69
 		val->rs.s = sint2str(val->ri, &val->rs.len);
44 70
 		val->flags = PV_VAL_STR;
45 71
 		return 0;
46 72
 	}
47
-	if(val->rs.len>TR_BUFFER_SIZE/2-1) {
73
+	if(val->rs.len>sqlops_tr_buf_size/2-1) {
48 74
 		LM_ERR("escape buffer to short");
49 75
 		return -1;
50 76
 	}
51
-	_tr_buffer[0] = '\'';
52
-	i = escape_common(_tr_buffer+1, val->rs.s, val->rs.len);
53
-	_tr_buffer[++i] = '\'';
54
-	_tr_buffer[++i] = '\0';
77
+	_sqlops_tr_buffer[0] = '\'';
78
+	i = escape_common(_sqlops_tr_buffer+1, val->rs.s, val->rs.len);
79
+	_sqlops_tr_buffer[++i] = '\'';
80
+	_sqlops_tr_buffer[++i] = '\0';
55 81
 	memset(val, 0, sizeof(pv_value_t));
56 82
 	val->flags = PV_VAL_STR;
57
-	val->rs.s = _tr_buffer;
83
+	val->rs.s = _sqlops_tr_buffer;
58 84
 	val->rs.len = i;
59 85
 	return 0;
60 86
 }
... ...
@@ -69,7 +95,7 @@ int tr_eval_sql(struct sip_msg *msg, tr_param_t *tp, int subtype,
69 95
 
70 96
 	if(val==NULL)
71 97
 		return -1;
72
-	
98
+
73 99
 	switch(subtype) {
74 100
 		case TR_SQL_VAL:
75 101
 			if (val->flags&PV_VAL_NULL) {
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -23,11 +23,11 @@
23 23
 #include <sys/types.h>
24 24
 #include <unistd.h>
25 25
 
26
-#include "../../mem/mem.h"
27
-#include "../../dprint.h"
28
-#include "../../trim.h"
29
-#include "../../ut.h"
30
-#include "../../strutils.h"
26
+#include "../../core/mem/mem.h"
27
+#include "../../core/dprint.h"
28
+#include "../../core/trim.h"
29
+#include "../../core/ut.h"
30
+#include "../../core/strutils.h"
31 31
 
32 32
 #include "sql_trans.h"
33 33
 
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,156 @@
1
+/**
2
+ * Copyright (C) 2011 SpeakUp B.V. (alex@speakup.nl)
3
+ *
4
+ * This file is part of kamailio, a free SIP server.
5
+ *
6
+ * Kamailio is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * Kamailio is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
+ */
20
+
21
+#include <stdio.h>
22
+#include <string.h>
23
+#include <sys/types.h>
24
+#include <unistd.h>
25
+
26
+#include "../../mem/mem.h"
27
+#include "../../dprint.h"
28
+#include "../../trim.h"
29
+#include "../../ut.h"
30
+#include "../../strutils.h"
31
+
32
+#include "sql_trans.h"
33
+
34
+#define TR_BUFFER_SIZE 2048
35
+
36
+
37
+static int _tr_eval_sql_val(pv_value_t *val)
38
+{
39
+	int i;
40
+	static char _tr_buffer[TR_BUFFER_SIZE];
41
+
42
+	if(val->flags&PV_TYPE_INT || !(val->flags&PV_VAL_STR)) {
43
+		val->rs.s = sint2str(val->ri, &val->rs.len);
44
+		val->flags = PV_VAL_STR;
45
+		return 0;
46
+	}
47
+	if(val->rs.len>TR_BUFFER_SIZE/2-1) {
48
+		LM_ERR("escape buffer to short");
49
+		return -1;
50
+	}
51
+	_tr_buffer[0] = '\'';
52
+	i = escape_common(_tr_buffer+1, val->rs.s, val->rs.len);
53
+	_tr_buffer[++i] = '\'';
54
+	_tr_buffer[++i] = '\0';
55
+	memset(val, 0, sizeof(pv_value_t));
56
+	val->flags = PV_VAL_STR;
57
+	val->rs.s = _tr_buffer;
58
+	val->rs.len = i;
59
+	return 0;
60
+}
61
+
62
+
63
+int tr_eval_sql(struct sip_msg *msg, tr_param_t *tp, int subtype,
64
+		pv_value_t *val)
65
+{
66
+	static str _sql_null = { "NULL", 4 };
67
+	static str _sql_zero = { "0", 1 };
68
+	static str _sql_empty = { "''", 2 };
69
+
70
+	if(val==NULL)
71
+		return -1;
72
+	
73
+	switch(subtype) {
74
+		case TR_SQL_VAL:
75
+			if (val->flags&PV_VAL_NULL) {
76
+				val->flags = PV_VAL_STR;
77
+				val->rs = _sql_null;
78
+				return 0;
79
+			} else {
80
+				return _tr_eval_sql_val(val);
81
+			}
82
+			break;
83
+
84
+		case TR_SQL_VAL_INT:
85
+			if (val->flags&PV_VAL_NULL) {
86
+				val->flags = PV_VAL_STR;
87
+				val->rs = _sql_zero;
88
+				return 0;
89
+			} else {
90
+				return _tr_eval_sql_val(val);
91
+			}
92
+			break;
93
+
94
+		case TR_SQL_VAL_STR:
95
+			if (val->flags&PV_VAL_NULL) {
96
+				val->flags = PV_VAL_STR;
97
+				val->rs = _sql_empty;
98
+				return 0;
99
+			} else {
100
+				return _tr_eval_sql_val(val);
101
+			}
102
+			break;
103
+
104
+		default:
105
+			LM_ERR("unknown subtype %d\n",
106
+					subtype);
107
+			return -1;
108
+	}
109
+	return 0;
110
+}
111
+
112
+
113
+char* tr_parse_sql(str *in, trans_t *t)
114
+{
115
+	char *p;
116
+	str name;
117
+
118
+
119
+	if(in==NULL || t==NULL)
120
+		return NULL;
121
+
122
+	p = in->s;
123
+	name.s = in->s;
124
+	t->type = TR_SQL;
125
+	t->trf = tr_eval_sql;
126
+
127
+	/* find next token */
128
+	while(is_in_str(p, in) && *p!=TR_PARAM_MARKER && *p!=TR_RBRACKET) p++;
129
+	if(*p=='\0') {
130
+		LM_ERR("unable to find transformation start: %.*s\n", in->len, in->s);
131
+		return NULL;
132
+	}
133
+	name.len = p - name.s;
134
+	trim(&name);
135
+
136
+	if(name.len==3 && strncasecmp(name.s, "val", 3)==0) {
137
+		t->subtype = TR_SQL_VAL;
138
+		goto done;
139
+	}
140
+	if(name.len==7 && strncasecmp(name.s, "val.int", 7)==0) {
141
+		t->subtype = TR_SQL_VAL_INT;
142
+		goto done;
143
+	}
144
+	if(name.len==7 && strncasecmp(name.s, "val.str", 7)==0) {
145
+		t->subtype = TR_SQL_VAL_STR;
146
+		goto done;
147
+	}
148
+
149
+	LM_ERR("unknown transformation: %.*s/%.*s/%d!\n", in->len, in->s,
150
+			name.len, name.s, name.len);
151
+	return NULL;
152
+done:
153
+	t->name = name;
154
+	return p;
155
+}
156
+