uridb_mod.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: uridb_mod.c 4681 2008-08-12 08:29:54Z henningw $ 
00003  *
00004  * Various URI related functions
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * -------
00026  *  2003-03-11: New module interface (janakj)
00027  *  2003-03-16: flags export parameter added (janakj)
00028  *  2003-03-19  replaces all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
00029  *  2003-04-05: default_uri #define used (jiri)
00030  *  2004-03-20: has_totag introduced (jiri)
00031  *  2004-06-07  updated to the new DB api (andrei)
00032  */
00033 
00034 
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include "../../sr_module.h"
00039 #include "../../dprint.h"
00040 #include "../../ut.h"
00041 #include "../../error.h"
00042 #include "../../mem/mem.h"
00043 #include "uridb_mod.h"
00044 #include "checks.h"
00045 
00046 MODULE_VERSION
00047 
00048 /*
00049  * Version of domain table required by the module,
00050  * increment this value if you change the table in
00051  * an backwards incompatible way
00052  */
00053 #define URI_TABLE_VERSION 1
00054 #define SUBSCRIBER_TABLE_VERSION 6
00055 
00056 static void destroy(void);       /* Module destroy function */
00057 static int child_init(int rank); /* Per-child initialization function */
00058 static int mod_init(void);       /* Module initialization function */
00059 
00060 
00061 #define URI_TABLE "uri"
00062 #define URI_TABLE_LEN (sizeof(URI_TABLE) - 1)
00063 
00064 #define USER_COL "username"
00065 #define USER_COL_LEN (sizeof(USER_COL) - 1)
00066 
00067 #define DOMAIN_COL "domain"
00068 #define DOMAIN_COL_LEN (sizeof(DOMAIN_COL) - 1)
00069 
00070 #define URI_USER_COL "uri_user"
00071 #define URI_USER_COL_LEN (sizeof(URI_USER_COL) - 1)
00072 
00073 #define SUBSCRIBER_TABLE "subscriber"
00074 #define SUBSCRIBER_TABLE_LEN (sizeof(SUBSCRIBER_TABLE) - 1)
00075 
00076 
00077 /*
00078  * Module parameter variables
00079  */
00080 static str db_url         = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
00081 str db_table              = {SUBSCRIBER_TABLE, SUBSCRIBER_TABLE_LEN};
00082 str uridb_user_col        = {USER_COL, USER_COL_LEN};
00083 str uridb_domain_col      = {DOMAIN_COL, DOMAIN_COL_LEN};
00084 str uridb_uriuser_col     = {URI_USER_COL, URI_USER_COL_LEN};
00085 
00086 int use_uri_table = 0;     /* Should uri table be used */
00087 int use_domain = 0;        /* Should does_uri_exist honor the domain part ? */
00088 
00089 static int fixup_exist(void** param, int param_no);
00090 
00091 /*
00092  * Exported functions
00093  */
00094 static cmd_export_t cmds[] = {
00095    {"check_to",       (cmd_function)check_to,       0, 0, 0,
00096       REQUEST_ROUTE},
00097    {"check_from",     (cmd_function)check_from,     0, 0, 0,
00098       REQUEST_ROUTE},
00099    {"does_uri_exist", (cmd_function)does_uri_exist, 0, 0, fixup_exist,
00100       REQUEST_ROUTE|LOCAL_ROUTE},
00101    {0, 0, 0, 0, 0, 0}
00102 };
00103 
00104 
00105 /*
00106  * Exported parameters
00107  */
00108 static param_export_t params[] = {
00109    {"db_url",                   STR_PARAM, &db_url.s               },
00110    {"db_table",                 STR_PARAM, &db_table.s             },
00111    {"user_column",              STR_PARAM, &uridb_user_col.s       },
00112    {"domain_column",            STR_PARAM, &uridb_domain_col.s     },
00113    {"uriuser_column",           STR_PARAM, &uridb_uriuser_col.s    },
00114    {"use_uri_table",            INT_PARAM, &use_uri_table          },
00115    {"use_domain",               INT_PARAM, &use_domain             },
00116    {0, 0, 0}
00117 };
00118 
00119 
00120 /*
00121  * Module interface
00122  */
00123 struct module_exports exports = {
00124    "uri_db",
00125    DEFAULT_DLFLAGS, /* dlopen flags */
00126    cmds,      /* Exported functions */
00127    params,    /* Exported parameters */
00128    0,         /* exported statistics */
00129    0 ,        /* exported MI functions */
00130    0,         /* exported pseudo-variables */
00131    0,         /* extra processes */
00132    mod_init,  /* module initialization function */
00133    0,         /* response function */
00134    destroy,   /* destroy function */
00135    child_init /* child initialization function */
00136 };
00137 
00138 
00139 /**
00140  * Module initialization function callee in each child separately
00141  */
00142 static int child_init(int rank)
00143 {
00144    if (db_url.len)
00145       return uridb_db_init(&db_url);
00146    else
00147       return 0;
00148 }
00149 
00150 
00151 /**
00152  * Module initialization function that is called before the main process forks
00153  */
00154 static int mod_init(void)
00155 {
00156    int ver;
00157    db_url.len = strlen(db_url.s);
00158 
00159    if (db_url.len == 0) {
00160       if (use_uri_table) {
00161          LM_ERR("configuration error - no database URL, "
00162             "but use_uri_table is set!\n");
00163          return -1;
00164       }
00165       return 0;
00166    }
00167 
00168    db_table.len = strlen(db_table.s);
00169    uridb_user_col.len = strlen(uridb_user_col.s);
00170    uridb_domain_col.len = strlen(uridb_domain_col.s);
00171    uridb_uriuser_col.len = strlen(uridb_uriuser_col.s);
00172 
00173    if (uridb_db_bind(&db_url)) {
00174       LM_ERR("No database module found\n");
00175       return -1;
00176    }
00177 
00178    /* Check table version */
00179    ver = uridb_db_ver(&db_url, &db_table);
00180    if (ver < 0) {
00181       LM_ERR("Error while querying table version\n");
00182       return -1;
00183    } else {
00184       if (use_uri_table) {
00185          if (ver != URI_TABLE_VERSION) {
00186          LM_ERR("Invalid table version of the uri table\n");
00187          return -1;
00188          }
00189       } else {
00190          if (ver != SUBSCRIBER_TABLE_VERSION) {
00191          LM_ERR("Invalid table version of the subscriber table\n");
00192          return -1;
00193          }
00194       }
00195    }
00196    return 0;
00197 }
00198 
00199 
00200 static void destroy(void)
00201 {
00202    uridb_db_close();
00203 }
00204 
00205 
00206 static int fixup_exist(void** param, int param_no)
00207 {
00208    if (db_url.len == 0) {
00209       LM_ERR("configuration error - does_uri_exist() called with no database URL!\n");
00210       return E_CFG;
00211    }
00212    return 0;
00213 }

Generated on Thu May 24 22:00:35 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6