Skip to content

Commit

Permalink
sqlops: allow to set the size of sql escape buffer for transformations
Browse files Browse the repository at this point in the history
- alternative to GH #899
  • Loading branch information
miconda committed Jan 8, 2017
1 parent 9077a87 commit ac56699
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 23 deletions.
27 changes: 23 additions & 4 deletions src/modules/sqlops/doc/sqlops_admin.xml
Expand Up @@ -10,9 +10,9 @@
<!-- Module User's Guide -->

<chapter>

<title>&adminguide;</title>

<section>
<title>Overview</title>
<para>
Expand Down Expand Up @@ -105,7 +105,7 @@
</section>
<section>
<title>Parameters</title>
<section>
<section id="sqlops.p.sqlcon">
<title><varname>sqlcon</varname> (str)</title>
<para>
The definition of a DB connection. The value of the parameter must have
Expand Down Expand Up @@ -152,7 +152,7 @@ modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
</programlisting>
</example>
</section>
<section>
<section id="sqlops.p.sqlres">
<title><varname>sqlres</varname> (str)</title>
<para>
The definition of a database result ID. The value of the parameter can be
Expand All @@ -172,6 +172,25 @@ modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
...
modparam("sqlops", "sqlres", "ra")
...
</programlisting>
</example>
</section>
<section id="sqlops.p.tr_buf_size">
<title><varname>tr_buf_size</varname> (int)</title>
<para>
The size of the transformations for SQL operations.
</para>
<para>
<emphasis>
Default value is 2048.
</emphasis>
</para>
<example>
<title>Set <varname>tr_buf_size</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("sqlops", "tr_buf_size", 4096)
...
</programlisting>
</example>
</section>
Expand Down
44 changes: 35 additions & 9 deletions src/modules/sqlops/sql_trans.c
Expand Up @@ -31,30 +31,56 @@

#include "sql_trans.h"

#define TR_BUFFER_SIZE 2048
int sqlops_tr_buf_size = 2048;

static char *_sqlops_tr_buffer = NULL;

int sqlops_tr_buffer_init(void)
{
if(_sqlops_tr_buffer!=NULL)
return 0;
if(sqlops_tr_buf_size<=0) {
LM_ERR("invalid buffer size: %d\n", sqlops_tr_buf_size);
return -1;
}
_sqlops_tr_buffer = pkg_malloc(sqlops_tr_buf_size * sizeof(char));
if(_sqlops_tr_buffer==NULL) {
LM_ERR("no more pkg memory\n");
return -1;
}
return 0;
}

void sqlops_tr_buffer_destroy(void)
{
if(_sqlops_tr_buffer==NULL)
return;

pkg_free(_sqlops_tr_buffer);

_sqlops_tr_buffer = NULL;
}

static int _tr_eval_sql_val(pv_value_t *val)
{
int i;
static char _tr_buffer[TR_BUFFER_SIZE];

if(val->flags&PV_TYPE_INT || !(val->flags&PV_VAL_STR)) {
val->rs.s = sint2str(val->ri, &val->rs.len);
val->flags = PV_VAL_STR;
return 0;
}
if(val->rs.len>TR_BUFFER_SIZE/2-1) {
if(val->rs.len>sqlops_tr_buf_size/2-1) {
LM_ERR("escape buffer to short");
return -1;
}
_tr_buffer[0] = '\'';
i = escape_common(_tr_buffer+1, val->rs.s, val->rs.len);
_tr_buffer[++i] = '\'';
_tr_buffer[++i] = '\0';
_sqlops_tr_buffer[0] = '\'';
i = escape_common(_sqlops_tr_buffer+1, val->rs.s, val->rs.len);
_sqlops_tr_buffer[++i] = '\'';
_sqlops_tr_buffer[++i] = '\0';
memset(val, 0, sizeof(pv_value_t));
val->flags = PV_VAL_STR;
val->rs.s = _tr_buffer;
val->rs.s = _sqlops_tr_buffer;
val->rs.len = i;
return 0;
}
Expand All @@ -69,7 +95,7 @@ int tr_eval_sql(struct sip_msg *msg, tr_param_t *tp, int subtype,

if(val==NULL)
return -1;

switch(subtype) {
case TR_SQL_VAL:
if (val->flags&PV_VAL_NULL) {
Expand Down
7 changes: 5 additions & 2 deletions src/modules/sqlops/sql_trans.h
Expand Up @@ -17,16 +17,19 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef _SQL_TRANS_H_
#define _SQL_TRANS_H_

#include "../../core/pvar.h"

enum _tr_sql_type { TR_SQL_NONE=0, TR_SQL };
enum _tr_sql_subtype {
enum _tr_sql_subtype {
TR_SQL_ST_NONE=0, TR_SQL_VAL, TR_SQL_VAL_INT, TR_SQL_VAL_STR };

char* tr_parse_sql(str *in, trans_t *tr);

int sqlops_tr_buffer_init(void);
void sqlops_tr_buffer_destroy(void);

#endif
28 changes: 20 additions & 8 deletions src/modules/sqlops/sqlops.c
Expand Up @@ -13,8 +13,8 @@
* 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
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

Expand Down Expand Up @@ -65,6 +65,7 @@ static int sql_xquery(struct sip_msg *msg, char *dbl, char *query, char *res);
#endif
static int sql_pvquery(struct sip_msg *msg, char *dbl, char *query, char *res);
static int sql_rfree(struct sip_msg*, char*, char*);
static int mod_init(void);
static int child_init(int rank);
static void destroy(void);

Expand All @@ -78,6 +79,8 @@ static int fixup_sql_rfree(void** param, int param_no);
static int sql_con_param(modparam_t type, void* val);
static int sql_res_param(modparam_t type, void* val);

extern int sqlops_tr_buf_size;

static pv_export_t mod_pvs[] = {
{ {"dbr", sizeof("dbr")-1}, PVT_OTHER, pv_get_dbr, 0,
pv_parse_dbr_name, 0, 0, 0 },
Expand All @@ -87,19 +90,19 @@ static pv_export_t mod_pvs[] = {
};

static cmd_export_t cmds[]={
{"sql_query", (cmd_function)sql_query, 3, fixup_sql_query, 0,
{"sql_query", (cmd_function)sql_query, 3, fixup_sql_query, 0,
ANY_ROUTE},
{"sql_query", (cmd_function)sql_query2, 2, fixup_sql_query, 0,
{"sql_query", (cmd_function)sql_query2, 2, fixup_sql_query, 0,
ANY_ROUTE},
{"sql_query_async", (cmd_function)sql_query_async, 2, fixup_sql_query, 0,
{"sql_query_async", (cmd_function)sql_query_async, 2, fixup_sql_query, 0,
ANY_ROUTE},
#ifdef WITH_XAVP
{"sql_xquery", (cmd_function)sql_xquery, 3, fixup_sql_xquery, 0,
{"sql_xquery", (cmd_function)sql_xquery, 3, fixup_sql_xquery, 0,
ANY_ROUTE},
#endif
{"sql_pvquery", (cmd_function)sql_pvquery, 3, fixup_sql_pvquery, 0,
ANY_ROUTE},
{"sql_result_free", (cmd_function)sql_rfree, 1, fixup_sql_rfree, 0,
{"sql_result_free", (cmd_function)sql_rfree, 1, fixup_sql_rfree, 0,
ANY_ROUTE},
{"bind_sqlops", (cmd_function)bind_sqlops, 0, 0, 0, 0},
{0,0,0,0,0,0}
Expand All @@ -108,6 +111,7 @@ static cmd_export_t cmds[]={
static param_export_t params[]={
{"sqlcon", PARAM_STRING|USE_FUNC_PARAM, (void*)sql_con_param},
{"sqlres", PARAM_STRING|USE_FUNC_PARAM, (void*)sql_res_param},
{"tr_buf_size", PARAM_INT, &sqlops_tr_buf_size},
{0,0,0}
};

Expand All @@ -127,7 +131,7 @@ struct module_exports exports= {
0 , /* exported MI functions */
mod_pvs, /* exported pseudo-variables */
0, /* extra processes */
0, /* module initialization function */
mod_init, /* module initialization function */
0,
(destroy_function) destroy,
child_init /* per-child init function */
Expand All @@ -138,6 +142,14 @@ int mod_register(char *path, int *dlflags, void *p1, void *p2)
return register_trans_mod(path, mod_trans);
}

static int mod_init(void)
{
if(sqlops_tr_buffer_init()<0) {
return -1;
}
return 0;
}

static int child_init(int rank)
{
if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
Expand Down

0 comments on commit ac56699

Please sign in to comment.