domain_mod.c

Go to the documentation of this file.
00001 /*
00002  * $Id: domain_mod.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * Domain module
00005  *
00006  * Copyright (C) 2002-2008 Juha Heinanen
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-04-05: default_uri #define used (jiri)
00029  * 2003-04-06: db connection closed in mod_init (janakj)
00030  * 2004-06-06: updated to the new DB api, cleanup: static dbf & handler,
00031  *             calls to domain_db_{bind,init,close,ver} (andrei)
00032  * 2006-01-22: added is_domain_local(variable) function (dan)
00033  *
00034  */
00035 
00036 
00037 #include "domain_mod.h"
00038 #include <stdio.h>
00039 #include "../../mem/shm_mem.h"
00040 #include "../../mem/mem.h"
00041 #include "../../sr_module.h"
00042 #include "../../pvar.h"
00043 #include "../../mod_fix.h"
00044 #include "domain.h"
00045 #include "mi.h"
00046 #include "hash.h"
00047 #include "api.h"
00048 
00049 /*
00050  * Module management function prototypes
00051  */
00052 static int mod_init(void);
00053 static void destroy(void);
00054 static int child_init(int rank);
00055 static int mi_child_init(void);
00056 
00057 MODULE_VERSION
00058 
00059 /*
00060  * Version of domain table required by the module,
00061  * increment this value if you change the table in
00062  * an backwards incompatible way
00063  */
00064 #define TABLE_VERSION 1
00065 
00066 #define DOMAIN_TABLE "domain"
00067 #define DOMAIN_TABLE_LEN (sizeof(DOMAIN_TABLE) - 1)
00068 
00069 #define DOMAIN_COL "domain"
00070 #define DOMAIN_COL_LEN (sizeof(DOMAIN_COL) - 1)
00071 
00072 /*
00073  * Module parameter variables
00074  */
00075 static str db_url = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
00076 int db_mode = 0;        /* Database usage mode: 0 = no cache, 1 = cache */
00077 str domain_table = {DOMAIN_TABLE, DOMAIN_TABLE_LEN}; /* Name of domain table */
00078 str domain_col = {DOMAIN_COL, DOMAIN_COL_LEN};       /* Name of domain column */
00079 
00080 /*
00081  * Other module variables
00082  */
00083 struct domain_list ***hash_table = 0;  /* Pointer to current hash table pointer */
00084 struct domain_list **hash_table_1 = 0; /* Pointer to hash table 1 */
00085 struct domain_list **hash_table_2 = 0; /* Pointer to hash table 2 */
00086 
00087 
00088 /*
00089  * Exported functions
00090  */
00091 static cmd_export_t cmds[] = {
00092    {"is_from_local", (cmd_function)is_from_local, 0, 0, 0,
00093     REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
00094    {"is_uri_host_local", (cmd_function)is_uri_host_local, 0, 0, 0,
00095     REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
00096    {"is_domain_local", (cmd_function)w_is_domain_local, 1, fixup_pvar_null,
00097     fixup_free_pvar_null,
00098     REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
00099    {"bind_domain", (cmd_function)bind_domain, 0, 0, 0, 0},
00100    {0, 0, 0, 0, 0, 0}
00101 };
00102 
00103 
00104 /*
00105  * Exported parameters
00106  */
00107 static param_export_t params[] = {
00108    {"db_url",         STR_PARAM, &db_url.s      },
00109    {"db_mode",        INT_PARAM, &db_mode       },
00110    {"domain_table",   STR_PARAM, &domain_table.s},
00111    {"domain_col",     STR_PARAM, &domain_col.s  },
00112    {0, 0, 0}
00113 };
00114 
00115 
00116 /*
00117  * Exported MI functions
00118  */
00119 static mi_export_t mi_cmds[] = {
00120    { MI_DOMAIN_RELOAD, mi_domain_reload, MI_NO_INPUT_FLAG, 0, mi_child_init },
00121    { MI_DOMAIN_DUMP,   mi_domain_dump,   MI_NO_INPUT_FLAG, 0, 0             },
00122    { 0, 0, 0, 0, 0}
00123 };
00124 
00125 
00126 /*
00127  * Module interface
00128  */
00129 struct module_exports exports = {
00130    "domain",
00131    DEFAULT_DLFLAGS, /* dlopen flags */
00132    cmds,      /* Exported functions */
00133    params,    /* Exported parameters */
00134    0,         /* exported statistics */
00135    mi_cmds,   /* exported MI functions */
00136    0,         /* exported pseudo-variables */
00137    0,         /* extra processes */
00138    mod_init,  /* module initialization function */
00139    0,         /* response function*/
00140    destroy,   /* destroy function */
00141    child_init /* per-child init function */
00142 };
00143 
00144 
00145 static int mod_init(void)
00146 {
00147         int i;
00148 
00149    LM_DBG("Initializing\n");
00150    
00151    db_url.len = strlen(db_url.s);
00152    domain_table.len = strlen(domain_table.s);
00153    domain_col.len = strlen(domain_col.s);
00154 
00155    /* Check if database module has been loaded */
00156    if (domain_db_bind(&db_url) < 0)  return -1;
00157 
00158    /* Check if cache needs to be loaded from domain table */
00159    if (db_mode != 0) {
00160 
00161       if (domain_db_init(&db_url)<0) return -1;
00162 
00163       /* Check table version */
00164       if (domain_db_ver(&domain_table, TABLE_VERSION) < 0) {
00165           LM_ERR("error during check of domain table version\n");
00166           goto error;
00167       }
00168 
00169       /* Initializing hash tables and hash table variable */
00170       hash_table_1 = (struct domain_list **)shm_malloc
00171          (sizeof(struct domain_list *) * DOM_HASH_SIZE);
00172       if (hash_table_1 == 0) {
00173          LM_ERR("No memory for hash table\n");
00174          goto error;
00175       }
00176 
00177       hash_table_2 = (struct domain_list **)shm_malloc
00178          (sizeof(struct domain_list *) * DOM_HASH_SIZE);
00179       if (hash_table_2 == 0) {
00180          LM_ERR("No memory for hash table\n");
00181          goto error;
00182       }
00183       for (i = 0; i < DOM_HASH_SIZE; i++) {
00184          hash_table_1[i] = hash_table_2[i] = (struct domain_list *)0;
00185       }
00186 
00187       hash_table = (struct domain_list ***)shm_malloc
00188          (sizeof(struct domain_list *));
00189       *hash_table = hash_table_1;
00190 
00191       if (reload_domain_table() == -1) {
00192          LM_ERR("Domain table reload failed\n");
00193          goto error;
00194       }
00195 
00196       domain_db_close();
00197    }
00198 
00199    return 0;
00200 error:
00201    domain_db_close();
00202    return -1;
00203 }
00204 
00205 
00206 static int child_init(int rank)
00207 {
00208    /* Check if database is needed by child */
00209    if ( db_mode==0 && rank>0 ) {
00210       if (domain_db_init(&db_url)<0) {
00211          LM_ERR("Unable to connect to the database\n");
00212          return -1;
00213       }
00214    }
00215    return 0;
00216 }
00217 
00218 
00219 static int mi_child_init(void)
00220 {
00221    return domain_db_init(&db_url);
00222 }
00223 
00224 
00225 static void destroy(void)
00226 {
00227    /* Destroy is called from the main process only,
00228     * there is no need to close database here because
00229     * it is closed in mod_init already
00230     */
00231    if (hash_table) {
00232       shm_free(hash_table);
00233       hash_table = 0;
00234    }
00235    if (hash_table_1) {
00236       hash_table_free(hash_table_1);
00237       shm_free(hash_table_1);
00238       hash_table_1 = 0;
00239    }
00240    if (hash_table_2) {
00241       hash_table_free(hash_table_2);
00242       shm_free(hash_table_2);
00243       hash_table_2 = 0;
00244    }
00245 }

Generated on Tue May 22 16:00:27 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6