2
0

p_usrloc_mod.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * $Id$
  3. *
  4. * Usrloc module interface
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * ---------
  26. * 2003-01-27 timer activity printing #ifdef-ed to EXTRA_DEBUG (jiri)
  27. * 2003-03-11 New module interface (janakj)
  28. * 2003-03-12 added replication and state columns (nils)
  29. * 2003-03-16 flags export parameter added (janakj)
  30. * 2003-04-05: default_uri #define used (jiri)
  31. * 2003-04-21 failed fifo init stops init process (jiri)
  32. * 2004-03-17 generic callbacks added (bogdan)
  33. * 2004-06-07 updated to the new DB api (andrei)
  34. * 2011-01-03 extended module definition for partitioned userlocking (marius)
  35. */
  36. /*! \file
  37. * \brief USRLOC - Usrloc module interface
  38. * \ingroup usrloc
  39. *
  40. * - Module \ref usrloc
  41. */
  42. /*!
  43. * \defgroup usrloc Usrloc :: User location module
  44. * \brief User location module
  45. *
  46. * The module keeps a user location table and provides access
  47. * to the table to other modules. The module exports no functions
  48. * that could be used directly from scripts, all access is done
  49. * over a API. A main user of this API is the registrar module.
  50. * \see registrar
  51. */
  52. #include <stdio.h>
  53. #include "p_usrloc_mod.h"
  54. #include "../../sr_module.h"
  55. #include "../../dprint.h"
  56. #include "../../rpc_lookup.h"
  57. #include "../../timer_proc.h"
  58. #include "../../globals.h" /* is_main */
  59. #include "../../ut.h" /* str_init */
  60. #include "udomain.h" /* {insert,delete,get,release}_urecord */
  61. #include "urecord.h" /* {insert,delete,get}_ucontact */
  62. #include "ucontact.h" /* update_ucontact */
  63. #include "ul_mi.h"
  64. #include "../usrloc/ul_callback.h"
  65. #include "ul_db_api.h"
  66. #include "ul_db_watch.h"
  67. #include "ul_check.h"
  68. #include "ul_db.h"
  69. #include "ul_db_layer.h"
  70. #include "dlist.h"
  71. MODULE_VERSION
  72. #define RUID_COL "ruid"
  73. #define USER_COL "username"
  74. #define DOMAIN_COL "domain"
  75. #define CONTACT_COL "contact"
  76. #define EXPIRES_COL "expires"
  77. #define Q_COL "q"
  78. #define CALLID_COL "callid"
  79. #define CSEQ_COL "cseq"
  80. #define FLAGS_COL "flags"
  81. #define CFLAGS_COL "cflags"
  82. #define USER_AGENT_COL "user_agent"
  83. #define RECEIVED_COL "received"
  84. #define PATH_COL "path"
  85. #define SOCK_COL "socket"
  86. #define METHODS_COL "methods"
  87. #define INSTANCE_COL "instance"
  88. #define REG_ID_COL "reg_id"
  89. #define LAST_MOD_COL "last_modified"
  90. static int mod_init(void); /*!< Module initialization function */
  91. static void destroy(void); /*!< Module destroy function */
  92. static int child_init(int rank); /*!< Per-child init function */
  93. static int mi_child_init(void);
  94. static int mi_child_loc_nr_init(void);
  95. extern int bind_usrloc(usrloc_api_t* api);
  96. extern int ul_locks_no;
  97. /*
  98. * Module parameters and their default values
  99. */
  100. /**
  101. * @var params
  102. * defines the parameters which can be set in the Kamailio config file
  103. * is stored. Only used when @see use_domain is set to 1
  104. * @param write_db_url Url to the database where the key and database information is
  105. * stored and where errors are reported to. Only used when @see write_on_db is active.
  106. * @param read_db_url Url to the database where the key and database information is
  107. * stored.
  108. * @param reg_db_table the name of the table containing the information about the
  109. * partitioned databases.
  110. * @param id_column name of the column containing the id mapping to a key.
  111. * @param num_column name of the column containing the number of the entry.
  112. * @param url_column name of the column containing the URL to the database.
  113. * @param status_column name of the column containing the status of the database.
  114. * (1=ON, 2=OFF)
  115. * @param failover_time_column name of the column containing the time whem the
  116. * database's status changed or a spare has been activated.
  117. * @param spare_flag_column name of the column containing the information if an entry
  118. * works as spare for broken dbs (0=no spare, 1=spare)
  119. * @param error_column name of the column containing the errors which occured on
  120. * the database.
  121. * @param risk_group_column name of the column containing the databases risk group
  122. * Only used when spare databases are used.
  123. * @param expire_time specifies the expire time of contacts
  124. * @param db_err_threshold specifies the amount of errors when at which a db
  125. * gets deactivated
  126. * @param failover_level defines if the module shall search for spares or just
  127. * turnoff a broken db
  128. * @param db_retry_interval defines in which intervals the module shall try to
  129. * reconnect to a deactivated database
  130. * @param write_on_db defines if the module has write access on the databases or not
  131. * @param alg_location defines the algorithm for the location matching - based on crc32 for now
  132. */
  133. str ruid_col = str_init(RUID_COL); /*!< Name of column containing record unique id */
  134. str user_col = str_init(USER_COL); /*!< Name of column containing usernames */
  135. str domain_col = str_init(DOMAIN_COL); /*!< Name of column containing domains */
  136. str contact_col = str_init(CONTACT_COL); /*!< Name of column containing contact addresses */
  137. str expires_col = str_init(EXPIRES_COL); /*!< Name of column containing expires values */
  138. str q_col = str_init(Q_COL); /*!< Name of column containing q values */
  139. str callid_col = str_init(CALLID_COL); /*!< Name of column containing callid string */
  140. str cseq_col = str_init(CSEQ_COL); /*!< Name of column containing cseq values */
  141. str flags_col = str_init(FLAGS_COL); /*!< Name of column containing internal flags */
  142. str cflags_col = str_init(CFLAGS_COL);
  143. str user_agent_col = str_init(USER_AGENT_COL); /*!< Name of column containing user agent string */
  144. str received_col = str_init(RECEIVED_COL); /*!< Name of column containing transport info of REGISTER */
  145. str path_col = str_init(PATH_COL); /*!< Name of column containing the Path header */
  146. str sock_col = str_init(SOCK_COL); /*!< Name of column containing the received socket */
  147. str methods_col = str_init(METHODS_COL); /*!< Name of column containing the supported methods */
  148. str instance_col = str_init(INSTANCE_COL); /*!< Name of column containing the SIP instance value */
  149. str reg_id_col = str_init(REG_ID_COL); /*!< Name of column containing the reg-id value */
  150. str last_mod_col = str_init(LAST_MOD_COL); /*!< Name of column containing the last modified date */
  151. int db_mode = 3; /*!< Database sync scheme: 1-write through, 2-write back, 3-only db */
  152. int use_domain = 0; /*!< Whether usrloc should use domain part of aor */
  153. int desc_time_order = 0; /*!< By default do not enable timestamp ordering */
  154. int ul_fetch_rows = 2000; /*!< number of rows to fetch from result */
  155. int ul_hash_size = 9;
  156. str write_db_url = str_init(DEFAULT_DB_URL);
  157. str read_db_url = str_init(DEFAULT_DB_URL);
  158. str reg_table = str_init(REG_TABLE);
  159. str id_col = str_init(ID_COL);
  160. str url_col = str_init(URL_COL);
  161. str num_col = str_init(NUM_COL);
  162. str status_col = str_init(STATUS_COL);
  163. str failover_time_col = str_init(FAILOVER_T_COL);
  164. str spare_col = str_init(SPARE_COL);
  165. str error_col = str_init(ERROR_COL);
  166. str risk_group_col = str_init(RISK_GROUP_COL);
  167. int expire_time = DEFAULT_EXPIRE;
  168. int db_error_threshold = DEFAULT_ERR_THRESHOLD;
  169. int failover_level = DEFAULT_FAILOVER_LEVEL;
  170. int retry_interval = DB_RETRY;
  171. int policy = DB_DEFAULT_POLICY;
  172. int db_write = 0;
  173. int db_master_write = 0;
  174. int alg_location = 0;
  175. int db_use_transactions = 0;
  176. str db_transaction_level = str_init(DB_DEFAULT_TRANSACTION_LEVEL);
  177. char * isolation_level;
  178. int connection_expires = DB_DEFAULT_CONNECTION_EXPIRES;
  179. int max_loc_nr = 0 ;
  180. /* flags */
  181. unsigned int nat_bflag = (unsigned int)-1;
  182. unsigned int init_flag = 0;
  183. str default_db_url = str_init(DEFAULT_DB_URL);
  184. str default_db_type = str_init(DEFAULT_DB_TYPE);
  185. str domain_db = str_init(DEFAULT_DOMAIN_DB);
  186. int default_dbt = 0;
  187. int expire = 0;
  188. /*! \brief
  189. * Exported functions
  190. */
  191. static cmd_export_t cmds[] = {
  192. {"ul_bind_usrloc", (cmd_function)bind_usrloc, 1, 0, 0, 0},
  193. {0, 0, 0, 0, 0, 0}
  194. };
  195. /*! \brief
  196. * Exported parameters
  197. */
  198. static param_export_t params[] = {
  199. {"ruid_column", PARAM_STR, &ruid_col },
  200. {"user_column", PARAM_STR, &user_col },
  201. {"domain_column", PARAM_STR, &domain_col },
  202. {"contact_column", PARAM_STR, &contact_col },
  203. {"expires_column", PARAM_STR, &expires_col },
  204. {"q_column", PARAM_STR, &q_col },
  205. {"callid_column", PARAM_STR, &callid_col },
  206. {"cseq_column", PARAM_STR, &cseq_col },
  207. {"flags_column", PARAM_STR, &flags_col },
  208. {"cflags_column", PARAM_STR, &cflags_col },
  209. {"db_mode", INT_PARAM, &db_mode },
  210. {"use_domain", INT_PARAM, &use_domain },
  211. {"desc_time_order", INT_PARAM, &desc_time_order },
  212. {"user_agent_column", PARAM_STR, &user_agent_col},
  213. {"received_column", PARAM_STR, &received_col },
  214. {"path_column", PARAM_STR, &path_col },
  215. {"socket_column", PARAM_STR, &sock_col },
  216. {"methods_column", PARAM_STR, &methods_col },
  217. {"matching_mode", INT_PARAM, &matching_mode },
  218. {"cseq_delay", INT_PARAM, &cseq_delay },
  219. {"fetch_rows", INT_PARAM, &ul_fetch_rows },
  220. {"hash_size", INT_PARAM, &ul_hash_size },
  221. {"nat_bflag", INT_PARAM, &nat_bflag },
  222. {"default_db_url", PARAM_STR, &default_db_url },
  223. {"default_db_type", PARAM_STR, &default_db_type },
  224. {"domain_db", PARAM_STR, &domain_db },
  225. {"instance_column", PARAM_STR, &instance_col },
  226. {"reg_id_column", PARAM_STR, &reg_id_col },
  227. {"write_db_url", PARAM_STR, &write_db_url },
  228. {"read_db_url", PARAM_STR, &read_db_url },
  229. {"reg_db_table", PARAM_STR, &reg_table },
  230. {"id_column", PARAM_STR, &id_col },
  231. {"num_column", PARAM_STR, &num_col },
  232. {"url_column", PARAM_STR, &url_col },
  233. {"status_column", PARAM_STR, &status_col },
  234. {"failover_time_column", PARAM_STR, &failover_time_col },
  235. {"spare_flag_column", PARAM_STR, &spare_col },
  236. {"error_column", PARAM_STR, &error_col },
  237. {"risk_group_column", PARAM_STR, &risk_group_col },
  238. {"expire_time", INT_PARAM, &expire_time },
  239. {"db_err_threshold", INT_PARAM, &db_error_threshold },
  240. {"failover_level", INT_PARAM, &failover_level },
  241. {"db_retry_interval", INT_PARAM, &retry_interval },
  242. {"db_use_transactions", INT_PARAM, &db_use_transactions },
  243. {"db_transaction_level", INT_PARAM, &db_transaction_level},
  244. {"write_on_db", INT_PARAM, &db_write },
  245. {"write_on_master_db", INT_PARAM, &db_master_write },
  246. {"connection_expires", INT_PARAM, &connection_expires },
  247. {"alg_location", INT_PARAM, &alg_location },
  248. {0, 0, 0}
  249. };
  250. stat_export_t mod_stats[] = {
  251. {"registered_users" , STAT_IS_FUNC, (stat_var**)get_number_of_users },
  252. {0,0,0}
  253. };
  254. static mi_export_t mi_cmds[] = {
  255. { MI_USRLOC_RM, mi_usrloc_rm_aor, 0, 0,
  256. mi_child_init },
  257. { MI_USRLOC_RM_CONTACT, mi_usrloc_rm_contact, 0, 0,
  258. mi_child_init },
  259. { MI_USRLOC_DUMP, mi_usrloc_dump, 0, 0,
  260. 0 },
  261. { MI_USRLOC_FLUSH, mi_usrloc_flush, MI_NO_INPUT_FLAG, 0,
  262. mi_child_init },
  263. { MI_USRLOC_ADD, mi_usrloc_add, 0, 0,
  264. mi_child_init },
  265. { MI_USRLOC_SHOW_CONTACT, mi_usrloc_show_contact, 0, 0,
  266. mi_child_init },
  267. { "loc_refresh", mi_ul_db_refresh, MI_NO_INPUT_FLAG, 0, mi_child_init },
  268. { "loc_nr_refresh", mi_loc_nr_refresh, MI_NO_INPUT_FLAG, 0, mi_child_loc_nr_init },
  269. { 0, 0, 0, 0, 0}
  270. };
  271. struct module_exports exports = {
  272. "p_usrloc",
  273. DEFAULT_DLFLAGS, /*!< dlopen flags */
  274. cmds, /*!< Exported functions */
  275. params, /*!< Export parameters */
  276. mod_stats, /*!< exported statistics */
  277. mi_cmds, /*!< exported MI functions */
  278. 0, /*!< exported pseudo-variables */
  279. 0, /*!< extra processes */
  280. mod_init, /*!< Module initialization function */
  281. 0, /*!< Response function */
  282. destroy, /*!< Destroy function */
  283. child_init /*!< Child initialization function */
  284. };
  285. /*! \brief
  286. * Module initialization function
  287. */
  288. static int mod_init(void)
  289. {
  290. #ifdef STATISTICS
  291. /* register statistics */
  292. if (register_module_stats( exports.name, mod_stats)!=0 ) {
  293. LM_ERR("failed to register core statistics\n");
  294. return -1;
  295. }
  296. #endif
  297. if(register_mi_mod(exports.name, mi_cmds)!=0)
  298. {
  299. LM_ERR("failed to register MI commands\n");
  300. return -1;
  301. }
  302. if(ul_hash_size<=1)
  303. ul_hash_size = 512;
  304. else
  305. ul_hash_size = 1<<ul_hash_size;
  306. ul_locks_no = ul_hash_size;
  307. /* check matching mode */
  308. switch (matching_mode) {
  309. case CONTACT_ONLY:
  310. case CONTACT_CALLID:
  311. case CONTACT_PATH:
  312. break;
  313. default:
  314. LM_ERR("invalid matching mode %d\n", matching_mode);
  315. }
  316. if(ul_init_locks()!=0)
  317. {
  318. LM_ERR("locks array initialization failed\n");
  319. return -1;
  320. }
  321. /* init the callbacks list */
  322. if ( init_ulcb_list() < 0) {
  323. LM_ERR("usrloc/callbacks initialization failed\n");
  324. return -1;
  325. }
  326. if (db_mode != DB_ONLY) {
  327. LM_ERR("DB_ONLY is the only mode possible for partitioned usrloc. Please set db_mode to 3");
  328. return -1;
  329. }
  330. /* Shall we use database ? */
  331. if (db_mode != NO_DB) { /* Yes */
  332. if(!default_db_url.s || default_db_url.len<=0){
  333. LM_ERR("must set default_db_url parameter\n");
  334. return -1;
  335. }
  336. if(strcmp(DB_TYPE_CLUSTER_STR, default_db_type.s) == 0){
  337. default_dbt = DB_TYPE_CLUSTER;
  338. } else {
  339. default_dbt = DB_TYPE_SINGLE;
  340. }
  341. if (ul_db_layer_init() < 0) {
  342. return -1;
  343. }
  344. if(ul_fetch_rows<=0) {
  345. LM_ERR("invalid fetch_rows number '%d'\n", ul_fetch_rows);
  346. return -1;
  347. }
  348. }
  349. if (nat_bflag==(unsigned int)-1) {
  350. nat_bflag = 0;
  351. } else if ( nat_bflag>=8*sizeof(nat_bflag) ) {
  352. LM_ERR("bflag index (%d) too big!\n", nat_bflag);
  353. return -1;
  354. } else {
  355. nat_bflag = 1<<nat_bflag;
  356. }
  357. init_flag = 1;
  358. if((isolation_level = pkg_malloc(strlen("SET TRANSACTION ISOLATION LEVEL ") + db_transaction_level.len + 1)) == NULL){
  359. LM_ERR("couldn't allocate private memory.\n");
  360. return -1;
  361. }
  362. sprintf(isolation_level, "SET TRANSACTION ISOLATION LEVEL %s", db_transaction_level.s);
  363. if(init_list() < 0) {
  364. LM_ERR("could not init check list.\n");
  365. return -1;
  366. }
  367. if(ul_db_init() < 0){
  368. LM_ERR("could not initialise databases.\n");
  369. return -1;
  370. }
  371. if(ul_db_watch_init() < 0){
  372. LM_ERR("could not init database watch environment.\n");
  373. return -1;
  374. }
  375. if(db_master_write){
  376. /* register extra dummy timer to be created in init_db_check() */
  377. register_dummy_timers(1);
  378. }
  379. return 0;
  380. }
  381. static int child_init(int _rank)
  382. {
  383. if(_rank==PROC_INIT) {
  384. if(init_db_check() < 0){
  385. LM_ERR("could not initialise database check.\n");
  386. return -1;
  387. }
  388. return 0;
  389. }
  390. if(ul_db_child_init() < 0){
  391. LM_ERR("could not initialise databases.\n");
  392. return -1;
  393. }
  394. return 0;
  395. }
  396. /* */
  397. static int mi_child_init(void)
  398. {
  399. return ul_db_child_init();
  400. }
  401. /*! \brief
  402. * Module destroy function
  403. */
  404. static void destroy(void)
  405. {
  406. /* we need to sync DB in order to flush the cache */
  407. ul_unlock_locks();
  408. free_all_udomains();
  409. ul_destroy_locks();
  410. /* free callbacks list */
  411. destroy_ulcb_list();
  412. ul_db_shutdown();
  413. destroy_list();
  414. ul_db_watch_destroy();
  415. }
  416. static int mi_child_loc_nr_init(void)
  417. {
  418. if(ul_db_child_locnr_init() < 0){
  419. LM_ERR("could not retrive location number from database. Try to reinitialize the db handles\n");
  420. return -1;
  421. }
  422. return 0;
  423. }
  424. struct mi_root* mi_ul_db_refresh(struct mi_root* cmd_tree, void* param) {
  425. int ret;
  426. ret = set_must_refresh();
  427. LM_INFO("sp-ul_db location databases were refreshed (%i elements).\n", ret);
  428. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  429. }
  430. struct mi_root* mi_loc_nr_refresh(struct mi_root* cmd_tree, void* param) {
  431. /* this function does nothing, all work is done per each child in the mi_child_loc_nr_init function */
  432. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  433. }