Skip to content

Commit

Permalink
userblocklist: clang-format for coherent indentation and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxmaniac committed May 18, 2023
1 parent 8b36095 commit af6b7b7
Show file tree
Hide file tree
Showing 5 changed files with 529 additions and 501 deletions.
87 changes: 49 additions & 38 deletions src/modules/userblocklist/db.c
Expand Up @@ -34,7 +34,6 @@
#include "../../lib/trie/dtrie.h"



/**
* Builds a d-tree using database entries.
* \return negative on failure, postive on success, indicating the number of d-tree entries
Expand All @@ -45,8 +44,9 @@ extern int match_mode;
int db_build_userbl_tree(const str *username, const str *domain,
const str *dbtable, struct dtrie_node_t *root, int use_domain)
{
db_key_t columns[2] = { &userblocklist_prefix_col, &userblocklist_allowlist_col };
db_key_t key[2] = { &userblocklist_username_col, &userblocklist_domain_col };
db_key_t columns[2] = {
&userblocklist_prefix_col, &userblocklist_allowlist_col};
db_key_t key[2] = {&userblocklist_username_col, &userblocklist_domain_col};

db_val_t val[2];
db1_res_t *res;
Expand All @@ -60,42 +60,47 @@ int db_build_userbl_tree(const str *username, const str *domain,
VAL_STR(val + 1).s = domain->s;
VAL_STR(val + 1).len = domain->len;

if (userblocklist_dbf.use_table(userblocklist_dbh, dbtable) < 0) {
if(userblocklist_dbf.use_table(userblocklist_dbh, dbtable) < 0) {
LM_ERR("cannot use db table '%.*s'.\n", dbtable->len, dbtable->s);
return -1;
}
if (userblocklist_dbf.query(userblocklist_dbh, key, 0, val, columns,
(!use_domain) ? (1) : (2), 2, 0, &res) < 0) {
LM_ERR("error while executing query on db table '%.*s'\n",
dbtable->len, dbtable->s);
if(userblocklist_dbf.query(userblocklist_dbh, key, 0, val, columns,
(!use_domain) ? (1) : (2), 2, 0, &res)
< 0) {
LM_ERR("error while executing query on db table '%.*s'\n", dbtable->len,
dbtable->s);
return -1;
}

dtrie_clear(root, NULL, match_mode);

if (RES_COL_N(res) > 1) {
if(RES_COL_N(res) > 1) {
for(i = 0; i < RES_ROW_N(res); i++) {
if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
if ((RES_ROWS(res)[i].values[0].type == DB1_STRING) &&
(RES_ROWS(res)[i].values[1].type == DB1_INT)) {
if((!RES_ROWS(res)[i].values[0].nul)
&& (!RES_ROWS(res)[i].values[1].nul)) {
if((RES_ROWS(res)[i].values[0].type == DB1_STRING)
&& (RES_ROWS(res)[i].values[1].type == DB1_INT)) {

/* LM_DBG("insert into tree prefix %s, allowlist %d",
RES_ROWS(res)[i].values[0].val.string_val,
RES_ROWS(res)[i].values[1].val.int_val); */
if (RES_ROWS(res)[i].values[1].val.int_val == 0) {
nodeflags=(void *)MARK_BLOCKLIST;
if(RES_ROWS(res)[i].values[1].val.int_val == 0) {
nodeflags = (void *)MARK_BLOCKLIST;
} else {
nodeflags=(void *)MARK_ALLOWLIST;
nodeflags = (void *)MARK_ALLOWLIST;
}

if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val,
strlen(RES_ROWS(res)[i].values[0].val.string_val),
nodeflags, match_mode) < 0)
if(dtrie_insert(root,
RES_ROWS(res)[i].values[0].val.string_val,
strlen(RES_ROWS(res)[i]
.values[0]
.val.string_val),
nodeflags, match_mode)
< 0)
LM_ERR("could not insert values into trie.\n");

n++;
}
else {
} else {
LM_ERR("got invalid result type from query.\n");
}
}
Expand All @@ -113,48 +118,54 @@ int db_build_userbl_tree(const str *username, const str *domain,
*/
int db_reload_source(const str *dbtable, struct dtrie_node_t *root)
{
db_key_t columns[2] = { &globalblocklist_prefix_col, &globalblocklist_allowlist_col };
db_key_t columns[2] = {
&globalblocklist_prefix_col, &globalblocklist_allowlist_col};
db1_res_t *res;
int i;
int n = 0;
void *nodeflags;

if (userblocklist_dbf.use_table(userblocklist_dbh, dbtable) < 0) {
if(userblocklist_dbf.use_table(userblocklist_dbh, dbtable) < 0) {
LM_ERR("cannot use db table '%.*s'\n", dbtable->len, dbtable->s);
return -1;
}
if (userblocklist_dbf.query(userblocklist_dbh, NULL, NULL, NULL,
columns, 0, 2, NULL, &res) < 0) {
LM_ERR("error while executing query on db table '%.*s'\n",
dbtable->len, dbtable->s);
if(userblocklist_dbf.query(
userblocklist_dbh, NULL, NULL, NULL, columns, 0, 2, NULL, &res)
< 0) {
LM_ERR("error while executing query on db table '%.*s'\n", dbtable->len,
dbtable->s);
return -1;
}

dtrie_clear(root, NULL, match_mode);

if (RES_COL_N(res) > 1) {
if(RES_COL_N(res) > 1) {
for(i = 0; i < RES_ROW_N(res); i++) {
if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
if ((RES_ROWS(res)[i].values[0].type == DB1_STRING) &&
(RES_ROWS(res)[i].values[1].type == DB1_INT)) {
if((!RES_ROWS(res)[i].values[0].nul)
&& (!RES_ROWS(res)[i].values[1].nul)) {
if((RES_ROWS(res)[i].values[0].type == DB1_STRING)
&& (RES_ROWS(res)[i].values[1].type == DB1_INT)) {

/* LM_DBG("insert into tree prefix %s, allowlist %d",
RES_ROWS(res)[i].values[0].val.string_val,
RES_ROWS(res)[i].values[1].val.int_val); */
if (RES_ROWS(res)[i].values[1].val.int_val == 0) {
nodeflags=(void *)MARK_BLOCKLIST;
if(RES_ROWS(res)[i].values[1].val.int_val == 0) {
nodeflags = (void *)MARK_BLOCKLIST;
} else {
nodeflags=(void *)MARK_ALLOWLIST;
nodeflags = (void *)MARK_ALLOWLIST;
}

if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val,
strlen(RES_ROWS(res)[i].values[0].val.string_val),
nodeflags, match_mode) < 0)
if(dtrie_insert(root,
RES_ROWS(res)[i].values[0].val.string_val,
strlen(RES_ROWS(res)[i]
.values[0]
.val.string_val),
nodeflags, match_mode)
< 0)
LM_ERR("could not insert values into trie.\n");

n++;
}
else {
} else {
LM_ERR("got invalid result type from query.\n");
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/userblocklist/db.h
Expand Up @@ -34,7 +34,8 @@
#define MARK_ALLOWLIST 1
#define MARK_BLOCKLIST 2

int db_build_userbl_tree(const str *user, const str *domain, const str *table, struct dtrie_node_t *root, int use_domain);
int db_build_userbl_tree(const str *user, const str *domain, const str *table,
struct dtrie_node_t *root, int use_domain);
int db_reload_source(const str *table, struct dtrie_node_t *root);

#endif
36 changes: 21 additions & 15 deletions src/modules/userblocklist/db_userblocklist.c
Expand Up @@ -22,7 +22,7 @@
/* TODO assign read-write or read-only URI, introduce a parameter in XML */

//extern str userblocklist_db_url;
db1_con_t * userblocklist_dbh = NULL;
db1_con_t *userblocklist_dbh = NULL;
db_func_t userblocklist_dbf;

str userblocklist_table = str_init("userblocklist");
Expand Down Expand Up @@ -52,8 +52,9 @@ const unsigned int globalblocklist_version = 1;
/*
* Closes the DB connection.
*/
void userblocklist_db_close(void) {
if (userblocklist_dbh) {
void userblocklist_db_close(void)
{
if(userblocklist_dbh) {
userblocklist_dbf.close(userblocklist_dbh);
userblocklist_dbh = NULL;
}
Expand All @@ -66,27 +67,31 @@ void userblocklist_db_close(void) {
*
* \return 0 means ok, -1 means an error occurred.
*/
int userblocklist_db_init(void) {
if (!userblocklist_db_url.s || !userblocklist_db_url.len) {
int userblocklist_db_init(void)
{
if(!userblocklist_db_url.s || !userblocklist_db_url.len) {
LM_ERR("you have to set the db_url module parameter.\n");
return -1;
}
if (db_bind_mod(&userblocklist_db_url, &userblocklist_dbf) < 0) {
if(db_bind_mod(&userblocklist_db_url, &userblocklist_dbf) < 0) {
LM_ERR("can't bind database module.\n");
return -1;
}
if ((userblocklist_dbh = userblocklist_dbf.init(&userblocklist_db_url)) == NULL) {
if((userblocklist_dbh = userblocklist_dbf.init(&userblocklist_db_url))
== NULL) {
LM_ERR("can't connect to database.\n");
return -1;
}
if (db_check_table_version(&userblocklist_dbf, userblocklist_dbh,
&userblocklist_table, userblocklist_version) < 0) {
if(db_check_table_version(&userblocklist_dbf, userblocklist_dbh,
&userblocklist_table, userblocklist_version)
< 0) {
DB_TABLE_VERSION_ERROR(userblocklist_table);
userblocklist_db_close();
return -1;
}
if (db_check_table_version(&userblocklist_dbf, userblocklist_dbh,
&globalblocklist_table, globalblocklist_version) < 0) {
if(db_check_table_version(&userblocklist_dbf, userblocklist_dbh,
&globalblocklist_table, globalblocklist_version)
< 0) {
DB_TABLE_VERSION_ERROR(globalblocklist_table);
userblocklist_db_close();
return -1;
Expand All @@ -102,14 +107,15 @@ int userblocklist_db_init(void) {
*
* \return 0 means ok, -1 means an error occurred.
*/
int userblocklist_db_open(void) {
if (userblocklist_dbh) {
int userblocklist_db_open(void)
{
if(userblocklist_dbh) {
userblocklist_dbf.close(userblocklist_dbh);
}
if ((userblocklist_dbh = userblocklist_dbf.init(&userblocklist_db_url)) == NULL) {
if((userblocklist_dbh = userblocklist_dbf.init(&userblocklist_db_url))
== NULL) {
LM_ERR("can't connect to database.\n");
return -1;
}
return 0;
}

39 changes: 24 additions & 15 deletions src/modules/userblocklist/db_userblocklist.h
Expand Up @@ -31,12 +31,13 @@
/* database variables */

extern str userblocklist_db_url;
extern db1_con_t * userblocklist_dbh;
extern db1_con_t *userblocklist_dbh;
extern db_func_t userblocklist_dbf;

#define userblocklist_DB_URL { "db_url", PARAM_STR, &userblocklist_db_url },
#define userblocklist_DB_URL {"db_url", PARAM_STR, &userblocklist_db_url},

#define userblocklist_DB_TABLE { "userblocklist_table", PARAM_STR, &userblocklist_table },
#define userblocklist_DB_TABLE \
{"userblocklist_table", PARAM_STR, &userblocklist_table},

extern str userblocklist_table;

Expand All @@ -46,17 +47,22 @@ extern str userblocklist_username_col;
extern str userblocklist_domain_col;
extern str userblocklist_prefix_col;
extern str userblocklist_allowlist_col;
#define userblocklist_DB_COLS \
{ "userblocklist_id_col", PARAM_STR, &userblocklist_id_col }, \
{ "userblocklist_username_col", PARAM_STR, &userblocklist_username_col }, \
{ "userblocklist_domain_col", PARAM_STR, &userblocklist_domain_col }, \
{ "userblocklist_prefix_col", PARAM_STR, &userblocklist_prefix_col }, \
{ "userblocklist_allowlist_col", PARAM_STR, &userblocklist_allowlist_col }, \
#define userblocklist_DB_COLS \
{"userblocklist_id_col", PARAM_STR, &userblocklist_id_col}, \
{"userblocklist_username_col", PARAM_STR, \
&userblocklist_username_col}, \
{"userblocklist_domain_col", PARAM_STR, \
&userblocklist_domain_col}, \
{"userblocklist_prefix_col", PARAM_STR, \
&userblocklist_prefix_col}, \
{"userblocklist_allowlist_col", PARAM_STR, \
&userblocklist_allowlist_col},

/* table version */
extern const unsigned int userblocklist_version;

#define globalblocklist_DB_TABLE { "globalblocklist_table", PARAM_STR, &globalblocklist_table },
#define globalblocklist_DB_TABLE \
{"globalblocklist_table", PARAM_STR, &globalblocklist_table},

extern str globalblocklist_table;

Expand All @@ -65,11 +71,14 @@ extern str globalblocklist_id_col;
extern str globalblocklist_prefix_col;
extern str globalblocklist_allowlist_col;
extern str globalblocklist_description_col;
#define globalblocklist_DB_COLS \
{ "globalblocklist_id_col", PARAM_STR, &globalblocklist_id_col }, \
{ "globalblocklist_prefix_col", PARAM_STR, &globalblocklist_prefix_col }, \
{ "globalblocklist_allowlist_col", PARAM_STR, &globalblocklist_allowlist_col }, \
{ "globalblocklist_description_col", PARAM_STR, &globalblocklist_description_col }, \
#define globalblocklist_DB_COLS \
{"globalblocklist_id_col", PARAM_STR, &globalblocklist_id_col}, \
{"globalblocklist_prefix_col", PARAM_STR, \
&globalblocklist_prefix_col}, \
{"globalblocklist_allowlist_col", PARAM_STR, \
&globalblocklist_allowlist_col}, \
{"globalblocklist_description_col", PARAM_STR, \
&globalblocklist_description_col},

/* table version */
extern const unsigned int globalblocklist_version;
Expand Down

0 comments on commit af6b7b7

Please sign in to comment.