00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
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
00061
00062
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
00074
00075 static str db_url = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
00076 int db_mode = 0;
00077 str domain_table = {DOMAIN_TABLE, DOMAIN_TABLE_LEN};
00078 str domain_col = {DOMAIN_COL, DOMAIN_COL_LEN};
00079
00080
00081
00082
00083 struct domain_list ***hash_table = 0;
00084 struct domain_list **hash_table_1 = 0;
00085 struct domain_list **hash_table_2 = 0;
00086
00087
00088
00089
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
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
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
00128
00129 struct module_exports exports = {
00130 "domain",
00131 DEFAULT_DLFLAGS,
00132 cmds,
00133 params,
00134 0,
00135 mi_cmds,
00136 0,
00137 0,
00138 mod_init,
00139 0,
00140 destroy,
00141 child_init
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
00156 if (domain_db_bind(&db_url) < 0) return -1;
00157
00158
00159 if (db_mode != 0) {
00160
00161 if (domain_db_init(&db_url)<0) return -1;
00162
00163
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
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
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
00228
00229
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 }