ul_mod.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * $Id$
  3. *
  4. * Usrloc module interface
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser 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. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. *
  30. * History:
  31. * ---------
  32. * 2003-01-27 timer activity printing #ifdef-ed to EXTRA_DEBUG (jiri)
  33. * 2003-03-11 New module interface (janakj)
  34. * 2003-03-12 added replication and state columns (nils)
  35. * 2003-03-16 flags export parameter added (janakj)
  36. * 2003-04-05: default_uri #define used (jiri)
  37. * 2003-04-21 failed fifo init stops init process (jiri)
  38. * 2004-03-17 generic callbacks added (bogdan)
  39. * 2004-06-07 updated to the new DB api (andrei)
  40. * 2005-02-25 incoming socket is saved in ucontact record (bogdan)
  41. */
  42. #include <stdio.h>
  43. #include "ul_mod.h"
  44. #include "../../sr_module.h"
  45. #include "../../dprint.h"
  46. #include "../../timer.h" /* register_timer */
  47. #include "../../globals.h" /* is_main */
  48. #include "dlist.h" /* register_udomain */
  49. #include "udomain.h" /* {insert,delete,get,release}_urecord */
  50. #include "urecord.h" /* {insert,delete,get}_ucontact */
  51. #include "ucontact.h" /* update_ucontact */
  52. #include "ul_callback.h"
  53. #include "notify.h"
  54. #include "ul_rpc.h"
  55. #include "usrloc.h"
  56. #include "reg_avps.h"
  57. MODULE_VERSION
  58. #define UID_COL "uid"
  59. #define CONTACT_COL "contact"
  60. #define EXPIRES_COL "expires"
  61. #define Q_COL "q"
  62. #define CALLID_COL "callid"
  63. #define CSEQ_COL "cseq"
  64. #define METHOD_COL "method"
  65. #define STATE_COL "state"
  66. #define FLAGS_COL "flags"
  67. #define USER_AGENT_COL "user_agent"
  68. #define RECEIVED_COL "received"
  69. #define INSTANCE_COL "instance"
  70. #define AOR_COL "aor"
  71. #define SERVER_ID_COL "server_id"
  72. static int mod_init(void); /* Module initialization function */
  73. static void destroy(void); /* Module destroy function */
  74. static void timer(unsigned int ticks, void* param); /* Timer handler */
  75. static int child_init(int rank); /* Per-child init function */
  76. extern int bind_usrloc(usrloc_api_t* api);
  77. /*
  78. * Module parameters and their default values
  79. */
  80. str uid_col = STR_STATIC_INIT(UID_COL); /* Name of column containing usernames */
  81. str contact_col = STR_STATIC_INIT(CONTACT_COL); /* Name of column containing contact addresses */
  82. str expires_col = STR_STATIC_INIT(EXPIRES_COL); /* Name of column containing expires values */
  83. str q_col = STR_STATIC_INIT(Q_COL); /* Name of column containing q values */
  84. str callid_col = STR_STATIC_INIT(CALLID_COL); /* Name of column containing callid string */
  85. str cseq_col = STR_STATIC_INIT(CSEQ_COL); /* Name of column containing cseq values */
  86. str method_col = STR_STATIC_INIT(METHOD_COL); /* Name of column containing supported method */
  87. str state_col = STR_STATIC_INIT(STATE_COL); /* Name of column containing contact state */
  88. str flags_col = STR_STATIC_INIT(FLAGS_COL); /* Name of column containing flags */
  89. str user_agent_col = STR_STATIC_INIT(USER_AGENT_COL); /* Name of column containing user agent string */
  90. str received_col = STR_STATIC_INIT(RECEIVED_COL); /* Name of column containing transport info of REGISTER */
  91. str instance_col = STR_STATIC_INIT(INSTANCE_COL); /* Name of column containing sip-instance parameter */
  92. str aor_col = STR_STATIC_INIT(AOR_COL); /* Name of column containing address of record */
  93. str server_id_col = STR_STATIC_INIT(SERVER_ID_COL); /* Name of column containing server id */
  94. str db_url = STR_STATIC_INIT(DEFAULT_DB_URL); /* Database URL */
  95. int timer_interval = 60; /* Timer interval in seconds */
  96. int db_mode = 0; /* Database sync scheme: 0-no db, 1-write through, 2-write back */
  97. int desc_time_order = 0; /* By default do not enable timestamp ordering */
  98. int db_skip_delete = 0; /* Enable/disable contact deletion in database */
  99. db_ctx_t* db = NULL;
  100. db_cmd_t** del_contact = NULL;
  101. db_cmd_t** ins_contact = NULL;
  102. int cmd_n = 0, cur_cmd = 0;
  103. static char *reg_avp_flag_name = NULL;
  104. /*
  105. * Exported functions
  106. */
  107. static cmd_export_t cmds[] = {
  108. {"ul_register_udomain", (cmd_function)register_udomain, 1, 0, 0},
  109. {"ul_insert_urecord", (cmd_function)insert_urecord, 1, 0, 0},
  110. {"ul_delete_urecord", (cmd_function)delete_urecord, 1, 0, 0},
  111. {"ul_get_urecord", (cmd_function)get_urecord, 1, 0, 0},
  112. {"ul_lock_udomain", (cmd_function)lock_udomain, 1, 0, 0},
  113. {"ul_unlock_udomain", (cmd_function)unlock_udomain, 1, 0, 0},
  114. {"ul_release_urecord", (cmd_function)release_urecord, 1, 0, 0},
  115. {"ul_insert_ucontact", (cmd_function)insert_ucontact, 1, 0, 0},
  116. {"ul_delete_ucontact", (cmd_function)delete_ucontact, 1, 0, 0},
  117. {"ul_get_ucontact", (cmd_function)get_ucontact, 1, 0, 0},
  118. {"ul_get_ucontact_by_inst", (cmd_function)get_ucontact_by_instance, 1, 0, 0},
  119. {"ul_get_all_ucontacts", (cmd_function)get_all_ucontacts, 1, 0, 0},
  120. {"ul_update_ucontact", (cmd_function)update_ucontact, 1, 0, 0},
  121. {"ul_register_watcher", (cmd_function)register_watcher, 1, 0, 0},
  122. {"ul_unregister_watcher", (cmd_function)unregister_watcher, 1, 0, 0},
  123. {"ul_bind_usrloc", (cmd_function)bind_usrloc, 1, 0, 0},
  124. {"ul_register_ulcb", (cmd_function)register_ulcb, 1, 0, 0},
  125. {"read_reg_avps", read_reg_avps, 2, read_reg_avps_fixup, REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE },
  126. {0, 0, 0, 0, 0}
  127. };
  128. /*
  129. * Exported parameters
  130. */
  131. static param_export_t params[] = {
  132. {"uid_column", PARAM_STR, &uid_col },
  133. {"contact_column", PARAM_STR, &contact_col },
  134. {"expires_column", PARAM_STR, &expires_col },
  135. {"q_column", PARAM_STR, &q_col },
  136. {"callid_column", PARAM_STR, &callid_col },
  137. {"cseq_column", PARAM_STR, &cseq_col },
  138. {"method_column", PARAM_STR, &method_col },
  139. {"flags_column", PARAM_STR, &flags_col },
  140. {"db_url", PARAM_STR, &db_url },
  141. {"timer_interval", PARAM_INT, &timer_interval },
  142. {"db_mode", PARAM_INT, &db_mode },
  143. {"desc_time_order", PARAM_INT, &desc_time_order},
  144. {"user_agent_column", PARAM_STR, &user_agent_col },
  145. {"received_column", PARAM_STR, &received_col },
  146. {"instance_column", PARAM_STR, &instance_col },
  147. {"aor_column", PARAM_STR, &aor_col },
  148. {"reg_avp_column", PARAM_STRING, &avp_column },
  149. {"reg_avp_flag", PARAM_STRING, &reg_avp_flag_name },
  150. {"db_skip_delete", PARAM_INT, &db_skip_delete},
  151. {0, 0, 0}
  152. };
  153. struct module_exports exports = {
  154. "usrloc",
  155. cmds, /* Exported functions */
  156. ul_rpc, /* RPC methods */
  157. params, /* Export parameters */
  158. mod_init, /* Module initialization function */
  159. 0, /* Response function */
  160. destroy, /* Destroy function */
  161. 0, /* OnCancel function */
  162. child_init /* Child initialization function */
  163. };
  164. /*
  165. * Module initialization function
  166. */
  167. static int mod_init(void)
  168. {
  169. DBG("usrloc - initializing\n");
  170. if ((db_mode < 0) || (db_mode >= UL_DB_MAX)) {
  171. ERR("Invalid database mode '%d'\n", db_mode);
  172. return -1;
  173. }
  174. /* Register cache timer */
  175. register_timer(timer, 0, timer_interval);
  176. /* init the callbacks list */
  177. if ( init_ulcb_list() < 0) {
  178. LOG(L_ERR, "ERROR: usrloc/callbacks initialization failed\n");
  179. return -1;
  180. }
  181. set_reg_avpflag_name(reg_avp_flag_name);
  182. return 0;
  183. }
  184. static int build_db_cmds(void)
  185. {
  186. db_fld_t del_contact_match[] = {
  187. {.name = uid_col.s, .type = DB_STR},
  188. {.name = contact_col.s, .type = DB_STR},
  189. {.name = NULL},
  190. };
  191. db_fld_t ins_contact_values[] = {
  192. {.name = uid_col.s, .type = DB_STR},
  193. {.name = contact_col.s, .type = DB_STR},
  194. {.name = expires_col.s, .type = DB_DATETIME},
  195. {.name = q_col.s, .type = DB_DOUBLE},
  196. {.name = callid_col.s, .type = DB_STR},
  197. {.name = cseq_col.s, .type = DB_INT},
  198. {.name = flags_col.s, .type = DB_BITMAP},
  199. {.name = user_agent_col.s, .type = DB_STR},
  200. {.name = received_col.s, .type = DB_STR},
  201. {.name = instance_col.s, .type = DB_STR},
  202. {.name = aor_col.s, .type = DB_STR},
  203. {.name = server_id_col.s, .type = DB_INT},
  204. {.name = avp_column, .type = DB_STR}, /* Must be the last element in the array */
  205. {.name = NULL},
  206. };
  207. dlist_t* ptr;
  208. int i;
  209. INFO("usrloc: build_db_cmds()\n");
  210. for(cmd_n = 0, ptr = root; ptr; cmd_n++, ptr = ptr->next);
  211. del_contact = pkg_malloc(cmd_n);
  212. if (del_contact == NULL) {
  213. ERR("No memory left\n");
  214. return -1;
  215. }
  216. memset(del_contact, '\0', sizeof(del_contact) * cmd_n);
  217. ins_contact = pkg_malloc(cmd_n);
  218. if (ins_contact == NULL) {
  219. ERR("No memory left\n");
  220. return -1;
  221. }
  222. memset(ins_contact, '\0', sizeof(ins_contact) * cmd_n);
  223. INFO("usrloc: building del_contact queries()\n");
  224. for(i = 0, ptr = root; ptr; ptr = ptr->next, i++) {
  225. del_contact[i] = db_cmd(DB_DEL, db, ptr->name.s, NULL, del_contact_match, NULL);
  226. if (del_contact[i] == NULL) return -1;
  227. }
  228. INFO("usrloc: building inst_contact queries()\n");
  229. for(i = 0, ptr = root; ptr; ptr = ptr->next, i++) {
  230. ins_contact[i] = db_cmd(DB_PUT, db, ptr->name.s, NULL, NULL, ins_contact_values);
  231. if (ins_contact[i] == NULL) return -1;
  232. }
  233. return 0;
  234. }
  235. static int child_init(int _rank)
  236. {
  237. INFO("usrloc: child_init( rank: %d)\n", _rank);
  238. if (_rank==PROC_INIT || _rank==PROC_MAIN || _rank==PROC_TCP_MAIN) {
  239. INFO("usrloc: do nothing for the init, main or tcp_main processes\n");
  240. return 0; /* do nothing for the main or tcp_main processes */
  241. }
  242. INFO("usrloc: db_mode = %d\n", db_mode);
  243. /* Shall we use database ? */
  244. if ( db_mode != NO_DB) { /* Yes */
  245. db = db_ctx("usrloc");
  246. if (db == NULL) {
  247. ERR("Error while initializing database layer\n");
  248. return -1;
  249. }
  250. if (db_add_db(db, db_url.s) < 0) return -1;
  251. if (db_connect(db) < 0) return -1;
  252. if (build_db_cmds() < 0) return -1;
  253. }
  254. INFO("usrloc: child_init( rank: %d), done OK\n", _rank);
  255. return 0;
  256. }
  257. /*
  258. * Module destroy function
  259. */
  260. static void destroy(void)
  261. {
  262. int i;
  263. /* Parent only, synchronize the world
  264. * and then nuke it */
  265. if (is_main) {
  266. if (db && synchronize_all_udomains() != 0) {
  267. LOG(L_ERR, "destroy(): Error while flushing cache\n");
  268. }
  269. free_all_udomains();
  270. }
  271. if (del_contact) {
  272. for(i = 0; i < cmd_n; i++) {
  273. if (del_contact[i]) db_cmd_free(del_contact[i]);
  274. }
  275. pkg_free(del_contact);
  276. }
  277. if (ins_contact) {
  278. for(i = 0; i < cmd_n; i++) {
  279. if (ins_contact[i]) db_cmd_free(ins_contact[i]);
  280. }
  281. pkg_free(ins_contact);
  282. }
  283. if (db) db_ctx_free(db);
  284. /* free callbacks list */
  285. destroy_ulcb_list();
  286. }
  287. /*
  288. * Timer handler
  289. */
  290. static void timer(unsigned int ticks, void* param)
  291. {
  292. if (synchronize_all_udomains() != 0) {
  293. LOG(L_ERR, "timer(): Error while synchronizing cache\n");
  294. }
  295. }