modules/userblacklist/db.c

Go to the documentation of this file.
00001 /*
00002  * $Id: db.c 5227 2008-11-19 13:16:15Z henningw $
00003  *
00004  * Copyright (C) 2007 1&1 Internet AG
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00023 /*!
00024  * \file
00025  * \brief USERBLACKLIST :: database access
00026  * \ingroup userblacklist
00027  * - Module: \ref userblacklist
00028  */
00029 
00030 #include "db.h"
00031 #include "db_userblacklist.h"
00032 
00033 #include "../../db/db.h"
00034 #include "../../mem/mem.h"
00035 #include "../../ut.h"
00036 #include "../../trie/dtrie.h"
00037 
00038 
00039 /**
00040  * Builds a d-tree using database entries.
00041  * \return negative on failure, postive on success, indicating the number of d-tree entries
00042  */
00043 int db_build_userbl_tree(const str *username, const str *domain, const str *table, struct dtrie_node_t *root, int use_domain)
00044 {
00045    db_key_t columns[2] = { &userblacklist_prefix_col, &userblacklist_whitelist_col };
00046    db_key_t key[2] = { &userblacklist_username_col, &userblacklist_domain_col };
00047 
00048    db_val_t val[2];
00049    VAL_TYPE(val) = VAL_TYPE(val + 1) = DB_STR;
00050    VAL_NULL(val) = VAL_NULL(val + 1) = 0;
00051    VAL_STR(val).s = username->s;
00052    VAL_STR(val).len = username->len;
00053    VAL_STR(val + 1).s = domain->s;
00054    VAL_STR(val + 1).len = domain->len;
00055 
00056    db_res_t *res;
00057    int i;
00058    int n = 0;
00059    void *nodeflags;
00060    
00061    if (userblacklist_dbf.use_table(userblacklist_dbh, table) < 0) {
00062       LM_ERR("cannot use table '%.*s'.\n", table->len, table->s);
00063       return -1;
00064    }
00065    if (userblacklist_dbf.query(userblacklist_dbh, key, 0, val, columns, (!use_domain) ? (1) : (2), 2, 0, &res) < 0) {
00066       LM_ERR("error while executing query.\n");
00067       return -1;
00068    }
00069 
00070    dtrie_clear(root, NULL, 10);
00071 
00072    if (RES_COL_N(res) > 1) {
00073       for(i = 0; i < RES_ROW_N(res); i++) {
00074          if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
00075             if ((RES_ROWS(res)[i].values[0].type == DB_STRING) &&
00076                (RES_ROWS(res)[i].values[1].type == DB_INT)) {
00077 
00078                /* LM_DBG("insert into tree prefix %s, whitelist %d",
00079                   RES_ROWS(res)[i].values[0].val.string_val,
00080                   RES_ROWS(res)[i].values[1].val.int_val); */
00081                if (RES_ROWS(res)[i].values[1].val.int_val == 0) nodeflags=(void *)MARK_BLACKLIST;
00082                if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val, strlen(RES_ROWS(res)[i].values[0].val.string_val),
00083                   nodeflags, 10) < 0) LM_ERR("could not insert values into trie.\n");
00084                n++;
00085             }
00086             else {
00087                LM_ERR("got invalid result type from query.\n");
00088             }
00089          }
00090       }
00091    }
00092    userblacklist_dbf.free_result(userblacklist_dbh, res);
00093 
00094    return n;
00095 }
00096 
00097 
00098 /**
00099  * Rebuild d-tree using database entries
00100  * \return negative on failure, positive on success, indicating the number of d-tree entries
00101  */
00102 int db_reload_source(const str *table, struct dtrie_node_t *root)
00103 {
00104    db_key_t columns[2] = { &globalblacklist_prefix_col, &globalblacklist_whitelist_col };
00105    db_res_t *res;
00106    int i;
00107    int n = 0;
00108    void *nodeflags;
00109    
00110    if (userblacklist_dbf.use_table(userblacklist_dbh, table) < 0) {
00111       LM_ERR("cannot use table '%.*s'.\n", table->len, table->s);
00112       return -1;
00113    }
00114    if (userblacklist_dbf.query(userblacklist_dbh, NULL, NULL, NULL, columns, 0, 2, NULL, &res) < 0) {
00115       LM_ERR("error while executing query.\n");
00116       return -1;
00117    }
00118 
00119    dtrie_clear(root, NULL, 10);
00120 
00121    if (RES_COL_N(res) > 1) {
00122       for(i = 0; i < RES_ROW_N(res); i++) {
00123          if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
00124             if ((RES_ROWS(res)[i].values[0].type == DB_STRING) &&
00125                (RES_ROWS(res)[i].values[1].type == DB_INT)) {
00126 
00127                /* LM_DBG("insert into tree prefix %s, whitelist %d",
00128                   RES_ROWS(res)[i].values[0].val.string_val,
00129                   RES_ROWS(res)[i].values[1].val.int_val); */
00130                if (RES_ROWS(res)[i].values[1].val.int_val == 0) nodeflags=(void *) MARK_BLACKLIST;
00131                else nodeflags=(void *)MARK_WHITELIST;
00132                if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val, strlen(RES_ROWS(res)[i].values[0].val.string_val),
00133                   nodeflags, 10) < 0) LM_ERR("could not insert values into trie.\n");
00134                n++;
00135             }
00136             else {
00137                LM_ERR("got invalid result type from query.\n");
00138             }
00139          }
00140       }
00141    }
00142    userblacklist_dbf.free_result(userblacklist_dbh, res);
00143 
00144    return n;
00145 }

Generated on Wed May 23 20:00:25 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6