dialog.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. /*
  2. * dialog module - basic support for dialog tracking
  3. *
  4. * Copyright (C) 2006 Voice Sistem SRL
  5. * Copyright (C) 2011 Carsten Bock, [email protected]
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. /*!
  25. * \file
  26. * \brief Module interface
  27. * \ingroup dialog
  28. * Module: \ref dialog
  29. */
  30. /**
  31. * @defgroup dialog dialog :: Kamailio dialog module
  32. * @brief Kamailio dialog module
  33. *
  34. * The dialog module provides dialog awareness to the Kamailio proxy. Its
  35. * functionality is to keep track of the current dialogs, to offer
  36. * information about them (like how many dialogs are active) or to manage
  37. * them. The module exports several functions that could be used directly
  38. * from scripts.
  39. * The module, via an internal API, also provide the foundation to build
  40. * on top of it more complex dialog-based functionalities via other
  41. * Kamailio modules.
  42. */
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46. #include <sys/time.h>
  47. #include "../../sr_module.h"
  48. #include "../../lib/srdb1/db.h"
  49. #include "../../dprint.h"
  50. #include "../../error.h"
  51. #include "../../ut.h"
  52. #include "../../pvar.h"
  53. #include "../../mod_fix.h"
  54. #include "../../script_cb.h"
  55. #include "../../lib/kcore/faked_msg.h"
  56. #include "../../hashes.h"
  57. #include "../../lib/kcore/kstats_wrapper.h"
  58. #include "../../mem/mem.h"
  59. #include "../../lib/kmi/mi.h"
  60. #include "../../timer_proc.h"
  61. #include "../../lvalue.h"
  62. #include "../../parser/parse_to.h"
  63. #include "../../modules/tm/tm_load.h"
  64. #include "../../rpc_lookup.h"
  65. #include "../rr/api.h"
  66. #include "dlg_hash.h"
  67. #include "dlg_timer.h"
  68. #include "dlg_handlers.h"
  69. #include "dlg_load.h"
  70. #include "dlg_cb.h"
  71. #include "dlg_db_handler.h"
  72. #include "dlg_req_within.h"
  73. #include "dlg_profile.h"
  74. #include "dlg_var.h"
  75. #include "dlg_transfer.h"
  76. #include "dlg_cseq.h"
  77. MODULE_VERSION
  78. #define RPC_DATE_BUF_LEN 21
  79. static int mod_init(void);
  80. static int child_init(int rank);
  81. static void mod_destroy(void);
  82. /* module parameter */
  83. static int dlg_hash_size = 4096;
  84. static char* rr_param = "did";
  85. static int dlg_flag = -1;
  86. static str timeout_spec = {NULL, 0};
  87. static int default_timeout = 60 * 60 * 12; /* 12 hours */
  88. static int seq_match_mode = SEQ_MATCH_STRICT_ID;
  89. static char* profiles_wv_s = NULL;
  90. static char* profiles_nv_s = NULL;
  91. str dlg_extra_hdrs = {NULL,0};
  92. static int db_fetch_rows = 200;
  93. int initial_cbs_inscript = 1;
  94. int dlg_wait_ack = 1;
  95. static int dlg_timer_procs = 0;
  96. static int _dlg_track_cseq_updates = 0;
  97. int dlg_event_rt[DLG_EVENTRT_MAX];
  98. str dlg_bridge_controller = str_init("sip:[email protected]");
  99. str dlg_bridge_contact = str_init("sip:[email protected]:5060");
  100. str ruri_pvar_param = str_init("$ru");
  101. pv_elem_t * ruri_param_model = NULL;
  102. str empty_str = STR_NULL;
  103. /* statistic variables */
  104. int dlg_enable_stats = 1;
  105. int active_dlgs_cnt = 0;
  106. int early_dlgs_cnt = 0;
  107. int detect_spirals = 1;
  108. int dlg_send_bye = 0;
  109. int dlg_timeout_noreset = 0;
  110. stat_var *active_dlgs = 0;
  111. stat_var *processed_dlgs = 0;
  112. stat_var *expired_dlgs = 0;
  113. stat_var *failed_dlgs = 0;
  114. stat_var *early_dlgs = 0;
  115. struct tm_binds d_tmb;
  116. struct rr_binds d_rrb;
  117. pv_spec_t timeout_avp;
  118. int dlg_db_mode_param = DB_MODE_NONE;
  119. str dlg_xavp_cfg = {0};
  120. int dlg_ka_timer = 0;
  121. int dlg_ka_interval = 0;
  122. int dlg_clean_timer = 90;
  123. str dlg_lreq_callee_headers = {0};
  124. /* db stuff */
  125. static str db_url = str_init(DEFAULT_DB_URL);
  126. static unsigned int db_update_period = DB_DEFAULT_UPDATE_PERIOD;
  127. static int pv_get_dlg_count( struct sip_msg *msg, pv_param_t *param,
  128. pv_value_t *res);
  129. void dlg_ka_timer_exec(unsigned int ticks, void* param);
  130. void dlg_clean_timer_exec(unsigned int ticks, void* param);
  131. /* commands wrappers and fixups */
  132. static int fixup_profile(void** param, int param_no);
  133. static int fixup_get_profile2(void** param, int param_no);
  134. static int fixup_get_profile3(void** param, int param_no);
  135. static int w_set_dlg_profile(struct sip_msg*, char*, char*);
  136. static int w_unset_dlg_profile(struct sip_msg*, char*, char*);
  137. static int w_is_in_profile(struct sip_msg*, char*, char*);
  138. static int w_get_profile_size2(struct sip_msg*, char*, char*);
  139. static int w_get_profile_size3(struct sip_msg*, char*, char*, char*);
  140. static int w_dlg_isflagset(struct sip_msg *msg, char *flag, str *s2);
  141. static int w_dlg_resetflag(struct sip_msg *msg, char *flag, str *s2);
  142. static int w_dlg_setflag(struct sip_msg *msg, char *flag, char *s2);
  143. static int w_dlg_set_property(struct sip_msg *msg, char *prop, char *s2);
  144. static int w_dlg_manage(struct sip_msg*, char*, char*);
  145. static int w_dlg_bye(struct sip_msg*, char*, char*);
  146. static int w_dlg_refer(struct sip_msg*, char*, char*);
  147. static int w_dlg_bridge(struct sip_msg*, char*, char*, char*);
  148. static int w_dlg_set_timeout(struct sip_msg*, char*, char*, char*);
  149. static int w_dlg_set_timeout_by_profile2(struct sip_msg *, char *, char *);
  150. static int w_dlg_set_timeout_by_profile3(struct sip_msg *, char *, char *,
  151. char *);
  152. static int fixup_dlg_bye(void** param, int param_no);
  153. static int fixup_dlg_refer(void** param, int param_no);
  154. static int fixup_dlg_bridge(void** param, int param_no);
  155. static int w_dlg_get(struct sip_msg*, char*, char*, char*);
  156. static int w_is_known_dlg(struct sip_msg *);
  157. static int w_dlg_remote_profile(sip_msg_t *msg, char *cmd, char *pname,
  158. char *pval, char *puid, char *expires);
  159. static int fixup_dlg_remote_profile(void** param, int param_no);
  160. static cmd_export_t cmds[]={
  161. {"dlg_manage", (cmd_function)w_dlg_manage, 0,0,
  162. 0, REQUEST_ROUTE },
  163. {"set_dlg_profile", (cmd_function)w_set_dlg_profile, 1,fixup_profile,
  164. 0, ANY_ROUTE },
  165. {"set_dlg_profile", (cmd_function)w_set_dlg_profile, 2,fixup_profile,
  166. 0, ANY_ROUTE },
  167. {"unset_dlg_profile", (cmd_function)w_unset_dlg_profile, 1,fixup_profile,
  168. 0, REQUEST_ROUTE| FAILURE_ROUTE | ONREPLY_ROUTE | BRANCH_ROUTE },
  169. {"unset_dlg_profile", (cmd_function)w_unset_dlg_profile, 2,fixup_profile,
  170. 0, REQUEST_ROUTE| FAILURE_ROUTE | ONREPLY_ROUTE | BRANCH_ROUTE },
  171. {"is_in_profile", (cmd_function)w_is_in_profile, 1,fixup_profile,
  172. 0, ANY_ROUTE },
  173. {"is_in_profile", (cmd_function)w_is_in_profile, 2,fixup_profile,
  174. 0, ANY_ROUTE },
  175. {"get_profile_size",(cmd_function)w_get_profile_size2, 2,fixup_get_profile2,
  176. 0, ANY_ROUTE },
  177. {"get_profile_size",(cmd_function)w_get_profile_size3, 3,fixup_get_profile3,
  178. 0, ANY_ROUTE },
  179. {"dlg_setflag", (cmd_function)w_dlg_setflag, 1,fixup_igp_null,
  180. 0, ANY_ROUTE },
  181. {"dlg_resetflag", (cmd_function)w_dlg_resetflag, 1,fixup_igp_null,
  182. 0, ANY_ROUTE },
  183. {"dlg_isflagset", (cmd_function)w_dlg_isflagset, 1,fixup_igp_null,
  184. 0, ANY_ROUTE },
  185. {"dlg_bye",(cmd_function)w_dlg_bye, 1,fixup_dlg_bye,
  186. 0, ANY_ROUTE },
  187. {"dlg_refer",(cmd_function)w_dlg_refer, 2,fixup_dlg_refer,
  188. 0, ANY_ROUTE },
  189. {"dlg_bridge",(cmd_function)w_dlg_bridge, 3,fixup_dlg_bridge,
  190. 0, ANY_ROUTE },
  191. {"dlg_get",(cmd_function)w_dlg_get, 3,fixup_dlg_bridge,
  192. 0, ANY_ROUTE },
  193. {"is_known_dlg", (cmd_function)w_is_known_dlg, 0, NULL,
  194. 0, ANY_ROUTE },
  195. {"dlg_set_timeout", (cmd_function)w_dlg_set_timeout, 1,fixup_igp_null,
  196. 0, ANY_ROUTE },
  197. {"dlg_set_timeout", (cmd_function)w_dlg_set_timeout, 3,fixup_igp_all,
  198. 0, ANY_ROUTE },
  199. {"dlg_set_timeout_by_profile",
  200. (cmd_function) w_dlg_set_timeout_by_profile2, 2, fixup_profile,
  201. 0, ANY_ROUTE },
  202. {"dlg_set_timeout_by_profile",
  203. (cmd_function) w_dlg_set_timeout_by_profile3, 3, fixup_profile,
  204. 0, ANY_ROUTE },
  205. {"dlg_set_property", (cmd_function)w_dlg_set_property,1,fixup_spve_null,
  206. 0, ANY_ROUTE },
  207. {"dlg_remote_profile", (cmd_function)w_dlg_remote_profile, 5, fixup_dlg_remote_profile,
  208. 0, ANY_ROUTE },
  209. {"load_dlg", (cmd_function)load_dlg, 0, 0, 0, 0},
  210. {0,0,0,0,0,0}
  211. };
  212. static param_export_t mod_params[]={
  213. { "enable_stats", INT_PARAM, &dlg_enable_stats },
  214. { "hash_size", INT_PARAM, &dlg_hash_size },
  215. { "rr_param", PARAM_STRING, &rr_param },
  216. { "dlg_flag", INT_PARAM, &dlg_flag },
  217. { "timeout_avp", PARAM_STR, &timeout_spec },
  218. { "default_timeout", INT_PARAM, &default_timeout },
  219. { "dlg_extra_hdrs", PARAM_STR, &dlg_extra_hdrs },
  220. { "dlg_match_mode", INT_PARAM, &seq_match_mode },
  221. { "detect_spirals", INT_PARAM, &detect_spirals, },
  222. { "db_url", PARAM_STR, &db_url },
  223. { "db_mode", INT_PARAM, &dlg_db_mode_param },
  224. { "table_name", PARAM_STR, &dialog_table_name },
  225. { "call_id_column", PARAM_STR, &call_id_column },
  226. { "from_uri_column", PARAM_STR, &from_uri_column },
  227. { "from_tag_column", PARAM_STR, &from_tag_column },
  228. { "to_uri_column", PARAM_STR, &to_uri_column },
  229. { "to_tag_column", PARAM_STR, &to_tag_column },
  230. { "h_id_column", PARAM_STR, &h_id_column },
  231. { "h_entry_column", PARAM_STR, &h_entry_column },
  232. { "state_column", PARAM_STR, &state_column },
  233. { "start_time_column", PARAM_STR, &start_time_column },
  234. { "timeout_column", PARAM_STR, &timeout_column },
  235. { "to_cseq_column", PARAM_STR, &to_cseq_column },
  236. { "from_cseq_column", PARAM_STR, &from_cseq_column },
  237. { "to_route_column", PARAM_STR, &to_route_column },
  238. { "from_route_column", PARAM_STR, &from_route_column },
  239. { "to_contact_column", PARAM_STR, &to_contact_column },
  240. { "from_contact_column", PARAM_STR, &from_contact_column },
  241. { "to_sock_column", PARAM_STR, &to_sock_column },
  242. { "from_sock_column", PARAM_STR, &from_sock_column },
  243. { "sflags_column", PARAM_STR, &sflags_column },
  244. { "toroute_name_column", PARAM_STR, &toroute_name_column },
  245. { "vars_table_name", PARAM_STR, &dialog_vars_table_name },
  246. { "vars_h_id_column", PARAM_STR, &vars_h_id_column },
  247. { "vars_h_entry_column", PARAM_STR, &vars_h_entry_column },
  248. { "vars_key_column", PARAM_STR, &vars_key_column },
  249. { "vars_value_column", PARAM_STR, &vars_value_column },
  250. { "db_update_period", INT_PARAM, &db_update_period },
  251. { "db_fetch_rows", INT_PARAM, &db_fetch_rows },
  252. { "profiles_with_value", PARAM_STRING, &profiles_wv_s },
  253. { "profiles_no_value", PARAM_STRING, &profiles_nv_s },
  254. { "bridge_controller", PARAM_STR, &dlg_bridge_controller },
  255. { "bridge_contact", PARAM_STR, &dlg_bridge_contact },
  256. { "ruri_pvar", PARAM_STR, &ruri_pvar_param },
  257. { "initial_cbs_inscript", INT_PARAM, &initial_cbs_inscript },
  258. { "send_bye", INT_PARAM, &dlg_send_bye },
  259. { "wait_ack", INT_PARAM, &dlg_wait_ack },
  260. { "xavp_cfg", PARAM_STR, &dlg_xavp_cfg },
  261. { "ka_timer", INT_PARAM, &dlg_ka_timer },
  262. { "ka_interval", INT_PARAM, &dlg_ka_interval },
  263. { "timeout_noreset", INT_PARAM, &dlg_timeout_noreset },
  264. { "timer_procs", PARAM_INT, &dlg_timer_procs },
  265. { "track_cseq_updates", PARAM_INT, &_dlg_track_cseq_updates },
  266. { "lreq_callee_headers", PARAM_STR, &dlg_lreq_callee_headers },
  267. { 0,0,0 }
  268. };
  269. static stat_export_t mod_stats[] = {
  270. {"active_dialogs" , STAT_NO_RESET, &active_dlgs },
  271. {"early_dialogs", STAT_NO_RESET, &early_dlgs },
  272. {"processed_dialogs" , 0, &processed_dlgs },
  273. {"expired_dialogs" , 0, &expired_dlgs },
  274. {"failed_dialogs", 0, &failed_dlgs },
  275. {0,0,0}
  276. };
  277. struct mi_root * mi_dlg_bridge(struct mi_root *cmd_tree, void *param);
  278. static mi_export_t mi_cmds[] = {
  279. { "dlg_list", mi_print_dlgs, 0, 0, 0},
  280. { "dlg_list_ctx", mi_print_dlgs_ctx, 0, 0, 0},
  281. { "dlg_end_dlg", mi_terminate_dlg, 0, 0, 0},
  282. { "dlg_terminate_dlg", mi_terminate_dlgs, 0, 0, 0},
  283. { "profile_get_size", mi_get_profile, 0, 0, 0},
  284. { "profile_list_dlgs", mi_profile_list, 0, 0, 0},
  285. { "dlg_bridge", mi_dlg_bridge, 0, 0, 0},
  286. { 0, 0, 0, 0, 0}
  287. };
  288. static rpc_export_t rpc_methods[];
  289. static pv_export_t mod_items[] = {
  290. { {"DLG_count", sizeof("DLG_count")-1}, PVT_OTHER, pv_get_dlg_count, 0,
  291. 0, 0, 0, 0 },
  292. { {"DLG_lifetime",sizeof("DLG_lifetime")-1}, PVT_OTHER, pv_get_dlg_lifetime, 0,
  293. 0, 0, 0, 0 },
  294. { {"DLG_status", sizeof("DLG_status")-1}, PVT_OTHER, pv_get_dlg_status, 0,
  295. 0, 0, 0, 0 },
  296. { {"dlg_ctx", sizeof("dlg_ctx")-1}, PVT_OTHER, pv_get_dlg_ctx,
  297. pv_set_dlg_ctx, pv_parse_dlg_ctx_name, 0, 0, 0 },
  298. { {"dlg", sizeof("dlg")-1}, PVT_OTHER, pv_get_dlg,
  299. 0, pv_parse_dlg_name, 0, 0, 0 },
  300. { {"dlg_var", sizeof("dlg_var")-1}, PVT_OTHER, pv_get_dlg_variable,
  301. pv_set_dlg_variable, pv_parse_dialog_var_name, 0, 0, 0},
  302. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  303. };
  304. struct module_exports exports= {
  305. "dialog", /* module's name */
  306. DEFAULT_DLFLAGS, /* dlopen flags */
  307. cmds, /* exported functions */
  308. mod_params, /* param exports */
  309. mod_stats, /* exported statistics */
  310. mi_cmds, /* exported MI functions */
  311. mod_items, /* exported pseudo-variables */
  312. 0, /* extra processes */
  313. mod_init, /* module initialization function */
  314. 0, /* reply processing function */
  315. mod_destroy,
  316. child_init /* per-child init function */
  317. };
  318. static int fixup_profile(void** param, int param_no)
  319. {
  320. struct dlg_profile_table *profile;
  321. pv_elem_t *model=NULL;
  322. str s;
  323. s.s = (char*)(*param);
  324. s.len = strlen(s.s);
  325. if(s.len==0) {
  326. LM_ERR("param %d is empty string!\n", param_no);
  327. return E_CFG;
  328. }
  329. if (param_no==1) {
  330. profile = search_dlg_profile( &s );
  331. if (profile==NULL) {
  332. LM_CRIT("profile <%s> not defined\n",s.s);
  333. return E_CFG;
  334. }
  335. pkg_free(*param);
  336. *param = (void*)profile;
  337. return 0;
  338. } else if (param_no==2) {
  339. if(pv_parse_format(&s ,&model) || model==NULL) {
  340. LM_ERR("wrong format [%s] for value param!\n", s.s);
  341. return E_CFG;
  342. }
  343. *param = (void*)model;
  344. }
  345. return 0;
  346. }
  347. static int fixup_get_profile2(void** param, int param_no)
  348. {
  349. pv_spec_t *sp;
  350. int ret;
  351. if (param_no==1) {
  352. return fixup_profile(param, 1);
  353. } else if (param_no==2) {
  354. ret = fixup_pvar_null(param, 1);
  355. if (ret<0) return ret;
  356. sp = (pv_spec_t*)(*param);
  357. if (sp->type!=PVT_AVP && sp->type!=PVT_SCRIPTVAR) {
  358. LM_ERR("return must be an AVP or SCRIPT VAR!\n");
  359. return E_SCRIPT;
  360. }
  361. }
  362. return 0;
  363. }
  364. static int fixup_get_profile3(void** param, int param_no)
  365. {
  366. if (param_no==1) {
  367. return fixup_profile(param, 1);
  368. } else if (param_no==2) {
  369. return fixup_profile(param, 2);
  370. } else if (param_no==3) {
  371. return fixup_get_profile2( param, 2);
  372. }
  373. return 0;
  374. }
  375. int load_dlg( struct dlg_binds *dlgb )
  376. {
  377. dlgb->register_dlgcb = register_dlgcb;
  378. dlgb->terminate_dlg = dlg_bye_all;
  379. dlgb->set_dlg_var = set_dlg_variable;
  380. dlgb->get_dlg_var = get_dlg_variable;
  381. dlgb->get_dlg = dlg_get_msg_dialog;
  382. dlgb->release_dlg = dlg_release;
  383. return 1;
  384. }
  385. static int pv_get_dlg_count(struct sip_msg *msg, pv_param_t *param,
  386. pv_value_t *res)
  387. {
  388. int n;
  389. int l;
  390. char *ch;
  391. if(msg==NULL || res==NULL)
  392. return -1;
  393. n = active_dlgs ? get_stat_val(active_dlgs) : 0;
  394. l = 0;
  395. ch = int2str( n, &l);
  396. res->rs.s = ch;
  397. res->rs.len = l;
  398. res->ri = n;
  399. res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
  400. return 0;
  401. }
  402. static int mod_init(void)
  403. {
  404. unsigned int n;
  405. if(dlg_ka_interval!=0 && dlg_ka_interval<30) {
  406. LM_ERR("ka interval too low (%d), has to be at least 30\n",
  407. dlg_ka_interval);
  408. return -1;
  409. }
  410. dlg_event_rt[DLG_EVENTRT_START] = route_lookup(&event_rt, "dialog:start");
  411. dlg_event_rt[DLG_EVENTRT_END] = route_lookup(&event_rt, "dialog:end");
  412. dlg_event_rt[DLG_EVENTRT_FAILED] = route_lookup(&event_rt, "dialog:failed");
  413. #ifdef STATISTICS
  414. /* register statistics */
  415. if (register_module_stats( exports.name, mod_stats)!=0 ) {
  416. LM_ERR("failed to register %s statistics\n", exports.name);
  417. return -1;
  418. }
  419. #endif
  420. if(register_mi_mod(exports.name, mi_cmds)!=0)
  421. {
  422. LM_ERR("failed to register MI commands\n");
  423. return -1;
  424. }
  425. if (rpc_register_array(rpc_methods)!=0) {
  426. LM_ERR("failed to register RPC commands\n");
  427. return -1;
  428. }
  429. if(faked_msg_init()<0)
  430. return -1;
  431. if(dlg_bridge_init_hdrs()<0)
  432. return -1;
  433. /* param checkings */
  434. if (dlg_flag==-1) {
  435. LM_ERR("no dlg flag set!!\n");
  436. return -1;
  437. } else if (dlg_flag>MAX_FLAG) {
  438. LM_ERR("invalid dlg flag %d!!\n",dlg_flag);
  439. return -1;
  440. }
  441. if (rr_param==0 || rr_param[0]==0) {
  442. LM_ERR("empty rr_param!!\n");
  443. return -1;
  444. } else if (strlen(rr_param)>MAX_DLG_RR_PARAM_NAME) {
  445. LM_ERR("rr_param too long (max=%d)!!\n", MAX_DLG_RR_PARAM_NAME);
  446. return -1;
  447. }
  448. if (timeout_spec.s) {
  449. if ( pv_parse_spec(&timeout_spec, &timeout_avp)==0
  450. && (timeout_avp.type!=PVT_AVP)){
  451. LM_ERR("malformed or non AVP timeout "
  452. "AVP definition in '%.*s'\n", timeout_spec.len,timeout_spec.s);
  453. return -1;
  454. }
  455. }
  456. if (default_timeout<=0) {
  457. LM_ERR("0 default_timeout not accepted!!\n");
  458. return -1;
  459. }
  460. if (ruri_pvar_param.s==NULL || ruri_pvar_param.len<=0) {
  461. LM_ERR("invalid r-uri PV string\n");
  462. return -1;
  463. }
  464. if(pv_parse_format(&ruri_pvar_param, &ruri_param_model) < 0
  465. || ruri_param_model==NULL) {
  466. LM_ERR("malformed r-uri PV string: %s\n", ruri_pvar_param.s);
  467. return -1;
  468. }
  469. if (initial_cbs_inscript != 0 && initial_cbs_inscript != 1) {
  470. LM_ERR("invalid parameter for running initial callbacks in-script"
  471. " (must be either 0 or 1)\n");
  472. return -1;
  473. }
  474. if (seq_match_mode!=SEQ_MATCH_NO_ID &&
  475. seq_match_mode!=SEQ_MATCH_FALLBACK &&
  476. seq_match_mode!=SEQ_MATCH_STRICT_ID ) {
  477. LM_ERR("invalid value %d for seq_match_mode param!!\n",seq_match_mode);
  478. return -1;
  479. }
  480. if (detect_spirals != 0 && detect_spirals != 1) {
  481. LM_ERR("invalid value %d for detect_spirals param!!\n",detect_spirals);
  482. return -1;
  483. }
  484. if (dlg_timeout_noreset != 0 && dlg_timeout_noreset != 1) {
  485. LM_ERR("invalid value %d for timeout_noreset param!!\n",
  486. dlg_timeout_noreset);
  487. return -1;
  488. }
  489. /* if statistics are disabled, prevent their registration to core */
  490. if (dlg_enable_stats==0)
  491. exports.stats = 0;
  492. /* create profile hashes */
  493. if (add_profile_definitions( profiles_nv_s, 0)!=0 ) {
  494. LM_ERR("failed to add profiles without value\n");
  495. return -1;
  496. }
  497. if (add_profile_definitions( profiles_wv_s, 1)!=0 ) {
  498. LM_ERR("failed to add profiles with value\n");
  499. return -1;
  500. }
  501. /* load the TM API */
  502. if (load_tm_api(&d_tmb)!=0) {
  503. LM_ERR("can't load TM API\n");
  504. return -1;
  505. }
  506. /* load RR API also */
  507. if (load_rr_api(&d_rrb)!=0) {
  508. LM_ERR("can't load RR API\n");
  509. return -1;
  510. }
  511. /* register callbacks*/
  512. /* listen for all incoming requests */
  513. if ( d_tmb.register_tmcb( 0, 0, TMCB_REQUEST_IN, dlg_onreq, 0, 0 ) <=0 ) {
  514. LM_ERR("cannot register TMCB_REQUEST_IN callback\n");
  515. return -1;
  516. }
  517. /* listen for all routed requests */
  518. if ( d_rrb.register_rrcb( dlg_onroute, 0 ) <0 ) {
  519. LM_ERR("cannot register RR callback\n");
  520. return -1;
  521. }
  522. if (register_script_cb( profile_cleanup, POST_SCRIPT_CB|REQUEST_CB,0)<0) {
  523. LM_ERR("cannot register script callback");
  524. return -1;
  525. }
  526. if (register_script_cb(dlg_cfg_cb,
  527. PRE_SCRIPT_CB|REQUEST_CB,0)<0)
  528. {
  529. LM_ERR("cannot register pre-script ctx callback\n");
  530. return -1;
  531. }
  532. if (register_script_cb(dlg_cfg_cb,
  533. POST_SCRIPT_CB|REQUEST_CB,0)<0)
  534. {
  535. LM_ERR("cannot register post-script ctx callback\n");
  536. return -1;
  537. }
  538. if (register_script_cb(spiral_detect_reset,POST_SCRIPT_CB|REQUEST_CB,0)<0) {
  539. LM_ERR("cannot register req pre-script spiral detection reset callback\n");
  540. return -1;
  541. }
  542. if (register_script_cb(cb_dlg_locals_reset,POST_SCRIPT_CB|ONREPLY_CB,0)<0) {
  543. LM_ERR("cannot register reply post-script dlg locals reset callback\n");
  544. return -1;
  545. }
  546. if (register_script_cb(cb_dlg_locals_reset,POST_SCRIPT_CB|FAILURE_CB,0)<0) {
  547. LM_ERR("cannot register failure post-script dlg locals reset callback\n");
  548. return -1;
  549. }
  550. if(dlg_timer_procs<=0) {
  551. if ( register_timer( dlg_timer_routine, 0, 1)<0 ) {
  552. LM_ERR("failed to register timer \n");
  553. return -1;
  554. }
  555. } else {
  556. register_sync_timers(1);
  557. }
  558. /* init handlers */
  559. init_dlg_handlers( rr_param, dlg_flag,
  560. timeout_spec.s?&timeout_avp:0, default_timeout, seq_match_mode);
  561. /* init timer */
  562. if (init_dlg_timer(dlg_ontimeout)!=0) {
  563. LM_ERR("cannot init timer list\n");
  564. return -1;
  565. }
  566. /* sanitize dlg_hash_zie */
  567. if (dlg_hash_size < 1){
  568. LM_WARN("hash_size is smaller "
  569. "then 1 -> rounding from %d to 1\n",
  570. dlg_hash_size);
  571. dlg_hash_size = 1;
  572. }
  573. /* initialized the hash table */
  574. for( n=0 ; n<(8*sizeof(n)) ; n++) {
  575. if (dlg_hash_size==(1<<n))
  576. break;
  577. if (n && dlg_hash_size<(1<<n)) {
  578. LM_WARN("hash_size is not a power "
  579. "of 2 as it should be -> rounding from %d to %d\n",
  580. dlg_hash_size, 1<<(n-1));
  581. dlg_hash_size = 1<<(n-1);
  582. }
  583. }
  584. if ( init_dlg_table(dlg_hash_size)<0 ) {
  585. LM_ERR("failed to create hash table\n");
  586. return -1;
  587. }
  588. /* if a database should be used to store the dialogs' information */
  589. dlg_db_mode = dlg_db_mode_param;
  590. if (dlg_db_mode==DB_MODE_NONE) {
  591. db_url.s = 0; db_url.len = 0;
  592. } else {
  593. if (dlg_db_mode!=DB_MODE_REALTIME &&
  594. dlg_db_mode!=DB_MODE_DELAYED && dlg_db_mode!=DB_MODE_SHUTDOWN ) {
  595. LM_ERR("unsupported db_mode %d\n", dlg_db_mode);
  596. return -1;
  597. }
  598. if ( !db_url.s || db_url.len==0 ) {
  599. LM_ERR("db_url not configured for db_mode %d\n", dlg_db_mode);
  600. return -1;
  601. }
  602. if (init_dlg_db(&db_url, dlg_hash_size, db_update_period,db_fetch_rows)!=0) {
  603. LM_ERR("failed to initialize the DB support\n");
  604. return -1;
  605. }
  606. run_load_callbacks();
  607. }
  608. destroy_dlg_callbacks( DLGCB_LOADED );
  609. /* timer process to send keep alive requests */
  610. if(dlg_ka_timer>0 && dlg_ka_interval>0)
  611. register_sync_timers(1);
  612. /* timer process to clean old unconfirmed dialogs */
  613. register_sync_timers(1);
  614. if(_dlg_track_cseq_updates!=0)
  615. dlg_register_cseq_callbacks();
  616. return 0;
  617. }
  618. static int child_init(int rank)
  619. {
  620. dlg_db_mode = dlg_db_mode_param;
  621. if(rank==PROC_MAIN) {
  622. if(dlg_timer_procs>0) {
  623. if(fork_sync_timer(PROC_TIMER, "Dialog Main Timer", 1 /*socks flag*/,
  624. dlg_timer_routine, NULL, 1 /*every sec*/)<0) {
  625. LM_ERR("failed to start main timer routine as process\n");
  626. return -1; /* error */
  627. }
  628. }
  629. if(dlg_ka_timer>0 && dlg_ka_interval>0) {
  630. if(fork_sync_timer(PROC_TIMER, "Dialog KA Timer", 1 /*socks flag*/,
  631. dlg_ka_timer_exec, NULL, dlg_ka_timer /*sec*/)<0) {
  632. LM_ERR("failed to start ka timer routine as process\n");
  633. return -1; /* error */
  634. }
  635. }
  636. if(fork_sync_timer(PROC_TIMER, "Dialog Clean Timer", 1 /*socks flag*/,
  637. dlg_clean_timer_exec, NULL, dlg_clean_timer /*sec*/)<0) {
  638. LM_ERR("failed to start clean timer routine as process\n");
  639. return -1; /* error */
  640. }
  641. }
  642. if (rank==1) {
  643. if_update_stat(dlg_enable_stats, active_dlgs, active_dlgs_cnt);
  644. if_update_stat(dlg_enable_stats, early_dlgs, early_dlgs_cnt);
  645. }
  646. if ( ((dlg_db_mode==DB_MODE_REALTIME || dlg_db_mode==DB_MODE_DELAYED) &&
  647. (rank>0 || rank==PROC_TIMER)) ||
  648. (dlg_db_mode==DB_MODE_SHUTDOWN && (rank==PROC_MAIN)) ) {
  649. if ( dlg_connect_db(&db_url) ) {
  650. LM_ERR("failed to connect to database (rank=%d)\n",rank);
  651. return -1;
  652. }
  653. }
  654. /* in DB_MODE_SHUTDOWN only PROC_MAIN will do a DB dump at the end, so
  655. * for the rest of the processes will be the same as DB_MODE_NONE */
  656. if (dlg_db_mode==DB_MODE_SHUTDOWN && rank!=PROC_MAIN)
  657. dlg_db_mode = DB_MODE_NONE;
  658. /* in DB_MODE_REALTIME and DB_MODE_DELAYED the PROC_MAIN have no DB handle */
  659. if ( (dlg_db_mode==DB_MODE_REALTIME || dlg_db_mode==DB_MODE_DELAYED) &&
  660. rank==PROC_MAIN)
  661. dlg_db_mode = DB_MODE_NONE;
  662. return 0;
  663. }
  664. static void mod_destroy(void)
  665. {
  666. if(dlg_db_mode == DB_MODE_DELAYED || dlg_db_mode == DB_MODE_SHUTDOWN) {
  667. dialog_update_db(0, 0);
  668. destroy_dlg_db();
  669. }
  670. dlg_bridge_destroy_hdrs();
  671. /* no DB interaction from now on */
  672. dlg_db_mode = DB_MODE_NONE;
  673. destroy_dlg_table();
  674. destroy_dlg_timer();
  675. destroy_dlg_callbacks( DLGCB_CREATED|DLGCB_LOADED );
  676. destroy_dlg_handlers();
  677. destroy_dlg_profiles();
  678. }
  679. static int w_set_dlg_profile(struct sip_msg *msg, char *profile, char *value)
  680. {
  681. pv_elem_t *pve;
  682. str val_s;
  683. pve = (pv_elem_t *)value;
  684. if (((struct dlg_profile_table*)profile)->has_value) {
  685. if ( pve==NULL || pv_printf_s(msg, pve, &val_s)!=0 ||
  686. val_s.len == 0 || val_s.s == NULL) {
  687. LM_WARN("cannot get string for value\n");
  688. return -1;
  689. }
  690. if ( set_dlg_profile( msg, &val_s,
  691. (struct dlg_profile_table*)profile) < 0 ) {
  692. LM_ERR("failed to set profile");
  693. return -1;
  694. }
  695. } else {
  696. if ( set_dlg_profile( msg, NULL,
  697. (struct dlg_profile_table*)profile) < 0 ) {
  698. LM_ERR("failed to set profile");
  699. return -1;
  700. }
  701. }
  702. return 1;
  703. }
  704. static int w_unset_dlg_profile(struct sip_msg *msg, char *profile, char *value)
  705. {
  706. pv_elem_t *pve;
  707. str val_s;
  708. pve = (pv_elem_t *)value;
  709. if (((struct dlg_profile_table*)profile)->has_value) {
  710. if ( pve==NULL || pv_printf_s(msg, pve, &val_s)!=0 ||
  711. val_s.len == 0 || val_s.s == NULL) {
  712. LM_WARN("cannot get string for value\n");
  713. return -1;
  714. }
  715. if ( unset_dlg_profile( msg, &val_s,
  716. (struct dlg_profile_table*)profile) < 0 ) {
  717. LM_ERR("failed to unset profile");
  718. return -1;
  719. }
  720. } else {
  721. if ( unset_dlg_profile( msg, NULL,
  722. (struct dlg_profile_table*)profile) < 0 ) {
  723. LM_ERR("failed to unset profile");
  724. return -1;
  725. }
  726. }
  727. return 1;
  728. }
  729. static int w_is_in_profile(struct sip_msg *msg, char *profile, char *value)
  730. {
  731. pv_elem_t *pve;
  732. str val_s;
  733. pve = (pv_elem_t *)value;
  734. if ( pve!=NULL && ((struct dlg_profile_table*)profile)->has_value) {
  735. if ( pv_printf_s(msg, pve, &val_s)!=0 ||
  736. val_s.len == 0 || val_s.s == NULL) {
  737. LM_WARN("cannot get string for value\n");
  738. return -1;
  739. }
  740. return is_dlg_in_profile( msg, (struct dlg_profile_table*)profile,
  741. &val_s);
  742. } else {
  743. return is_dlg_in_profile( msg, (struct dlg_profile_table*)profile,
  744. NULL);
  745. }
  746. }
  747. /**
  748. * get dynamic name profile size
  749. */
  750. static int w_get_profile_size3(struct sip_msg *msg, char *profile,
  751. char *value, char *result)
  752. {
  753. pv_elem_t *pve;
  754. str val_s;
  755. pv_spec_t *sp_dest;
  756. unsigned int size;
  757. pv_value_t val;
  758. if(result!=NULL)
  759. {
  760. pve = (pv_elem_t *)value;
  761. sp_dest = (pv_spec_t *)result;
  762. } else {
  763. pve = NULL;
  764. sp_dest = (pv_spec_t *)value;
  765. }
  766. if ( pve!=NULL && ((struct dlg_profile_table*)profile)->has_value) {
  767. if ( pv_printf_s(msg, pve, &val_s)!=0 ||
  768. val_s.len == 0 || val_s.s == NULL) {
  769. LM_WARN("cannot get string for value\n");
  770. return -1;
  771. }
  772. size = get_profile_size( (struct dlg_profile_table*)profile, &val_s );
  773. } else {
  774. size = get_profile_size( (struct dlg_profile_table*)profile, NULL );
  775. }
  776. memset(&val, 0, sizeof(pv_value_t));
  777. val.flags = PV_VAL_INT|PV_TYPE_INT;
  778. val.ri = (int)size;
  779. if(sp_dest->setf(msg, &sp_dest->pvp, (int)EQ_T, &val)<0)
  780. {
  781. LM_ERR("setting profile PV failed\n");
  782. return -1;
  783. }
  784. return 1;
  785. }
  786. /**
  787. * get static name profile size
  788. */
  789. static int w_get_profile_size2(struct sip_msg *msg, char *profile, char *result)
  790. {
  791. return w_get_profile_size3(msg, profile, result, NULL);
  792. }
  793. static int w_dlg_setflag(struct sip_msg *msg, char *flag, char *s2)
  794. {
  795. dlg_ctx_t *dctx;
  796. dlg_cell_t *d;
  797. int val;
  798. if(fixup_get_ivalue(msg, (gparam_p)flag, &val)!=0)
  799. {
  800. LM_ERR("no flag value\n");
  801. return -1;
  802. }
  803. if(val<0 || val>31)
  804. return -1;
  805. if ( (dctx=dlg_get_dlg_ctx())==NULL )
  806. return -1;
  807. dctx->flags |= 1<<val;
  808. d = dlg_get_by_iuid(&dctx->iuid);
  809. if(d!=NULL) {
  810. d->sflags |= 1<<val;
  811. dlg_release(d);
  812. }
  813. return 1;
  814. }
  815. static int w_dlg_resetflag(struct sip_msg *msg, char *flag, str *s2)
  816. {
  817. dlg_ctx_t *dctx;
  818. dlg_cell_t *d;
  819. int val;
  820. if(fixup_get_ivalue(msg, (gparam_p)flag, &val)!=0)
  821. {
  822. LM_ERR("no flag value\n");
  823. return -1;
  824. }
  825. if(val<0 || val>31)
  826. return -1;
  827. if ( (dctx=dlg_get_dlg_ctx())==NULL )
  828. return -1;
  829. dctx->flags &= ~(1<<val);
  830. d = dlg_get_by_iuid(&dctx->iuid);
  831. if(d!=NULL) {
  832. d->sflags &= ~(1<<val);
  833. dlg_release(d);
  834. }
  835. return 1;
  836. }
  837. static int w_dlg_isflagset(struct sip_msg *msg, char *flag, str *s2)
  838. {
  839. dlg_ctx_t *dctx;
  840. dlg_cell_t *d;
  841. int val;
  842. int ret;
  843. if(fixup_get_ivalue(msg, (gparam_p)flag, &val)!=0)
  844. {
  845. LM_ERR("no flag value\n");
  846. return -1;
  847. }
  848. if(val<0 || val>31)
  849. return -1;
  850. if ( (dctx=dlg_get_dlg_ctx())==NULL )
  851. return -1;
  852. d = dlg_get_by_iuid(&dctx->iuid);
  853. if(d!=NULL) {
  854. ret = (d->sflags&(1<<val))?1:-1;
  855. dlg_release(d);
  856. return ret;
  857. }
  858. return (dctx->flags&(1<<val))?1:-1;
  859. }
  860. /**
  861. *
  862. */
  863. static int w_dlg_manage(struct sip_msg *msg, char *s1, char *s2)
  864. {
  865. return dlg_manage(msg);
  866. }
  867. static int w_dlg_bye(struct sip_msg *msg, char *side, char *s2)
  868. {
  869. dlg_cell_t *dlg = NULL;
  870. int n;
  871. dlg = dlg_get_ctx_dialog();
  872. if(dlg==NULL)
  873. return -1;
  874. n = (int)(long)side;
  875. if(n==1)
  876. {
  877. if(dlg_bye(dlg, NULL, DLG_CALLER_LEG)!=0)
  878. goto error;
  879. goto done;
  880. } else if(n==2) {
  881. if(dlg_bye(dlg, NULL, DLG_CALLEE_LEG)!=0)
  882. goto error;
  883. goto done;
  884. } else {
  885. if(dlg_bye_all(dlg, NULL)!=0)
  886. goto error;
  887. goto done;
  888. }
  889. done:
  890. dlg_release(dlg);
  891. return 1;
  892. error:
  893. dlg_release(dlg);
  894. return -1;
  895. }
  896. static int w_dlg_refer(struct sip_msg *msg, char *side, char *to)
  897. {
  898. dlg_cell_t *dlg;
  899. int n;
  900. str st = {0,0};
  901. dlg = dlg_get_ctx_dialog();
  902. if(dlg==NULL)
  903. return -1;
  904. n = (int)(long)side;
  905. if(fixup_get_svalue(msg, (gparam_p)to, &st)!=0)
  906. {
  907. LM_ERR("unable to get To\n");
  908. goto error;
  909. }
  910. if(st.s==NULL || st.len == 0)
  911. {
  912. LM_ERR("invalid To parameter\n");
  913. goto error;
  914. }
  915. if(n==1)
  916. {
  917. if(dlg_transfer(dlg, &st, DLG_CALLER_LEG)!=0)
  918. goto error;
  919. } else {
  920. if(dlg_transfer(dlg, &st, DLG_CALLEE_LEG)!=0)
  921. goto error;
  922. }
  923. dlg_release(dlg);
  924. return 1;
  925. error:
  926. dlg_release(dlg);
  927. return -1;
  928. }
  929. static int w_dlg_bridge(struct sip_msg *msg, char *from, char *to, char *op)
  930. {
  931. str sf = {0,0};
  932. str st = {0,0};
  933. str so = {0,0};
  934. if(from==0 || to==0 || op==0)
  935. {
  936. LM_ERR("invalid parameters\n");
  937. return -1;
  938. }
  939. if(fixup_get_svalue(msg, (gparam_p)from, &sf)!=0)
  940. {
  941. LM_ERR("unable to get From\n");
  942. return -1;
  943. }
  944. if(sf.s==NULL || sf.len == 0)
  945. {
  946. LM_ERR("invalid From parameter\n");
  947. return -1;
  948. }
  949. if(fixup_get_svalue(msg, (gparam_p)to, &st)!=0)
  950. {
  951. LM_ERR("unable to get To\n");
  952. return -1;
  953. }
  954. if(st.s==NULL || st.len == 0)
  955. {
  956. LM_ERR("invalid To parameter\n");
  957. return -1;
  958. }
  959. if(fixup_get_svalue(msg, (gparam_p)op, &so)!=0)
  960. {
  961. LM_ERR("unable to get OP\n");
  962. return -1;
  963. }
  964. if(dlg_bridge(&sf, &st, &so, NULL)!=0)
  965. return -1;
  966. return 1;
  967. }
  968. /**
  969. *
  970. */
  971. static int w_dlg_set_timeout(struct sip_msg *msg, char *pto, char *phe, char *phi)
  972. {
  973. int to = 0;
  974. unsigned int he = 0;
  975. unsigned int hi = 0;
  976. dlg_cell_t *dlg = NULL;
  977. if(fixup_get_ivalue(msg, (gparam_p)pto, &to)!=0)
  978. {
  979. LM_ERR("no timeout value\n");
  980. return -1;
  981. }
  982. if(phe!=NULL)
  983. {
  984. if(fixup_get_ivalue(msg, (gparam_p)phe, (int*)&he)!=0)
  985. {
  986. LM_ERR("no hash entry value value\n");
  987. return -1;
  988. }
  989. if(fixup_get_ivalue(msg, (gparam_p)phi, (int*)&hi)!=0)
  990. {
  991. LM_ERR("no hash id value value\n");
  992. return -1;
  993. }
  994. dlg = dlg_lookup(he, hi);
  995. } else {
  996. dlg = dlg_get_msg_dialog(msg);
  997. }
  998. if(dlg==NULL)
  999. {
  1000. LM_DBG("no dialog found\n");
  1001. return -1;
  1002. }
  1003. if(update_dlg_timeout(dlg, to) != 0)
  1004. return -1;
  1005. return 1;
  1006. }
  1007. static int w_dlg_set_property(struct sip_msg *msg, char *prop, char *s2)
  1008. {
  1009. dlg_ctx_t *dctx;
  1010. dlg_cell_t *d;
  1011. str val;
  1012. if(fixup_get_svalue(msg, (gparam_t*)prop, &val)!=0)
  1013. {
  1014. LM_ERR("no property value\n");
  1015. return -1;
  1016. }
  1017. if(val.len<=0)
  1018. {
  1019. LM_ERR("empty property value\n");
  1020. return -1;
  1021. }
  1022. if ( (dctx=dlg_get_dlg_ctx())==NULL )
  1023. return -1;
  1024. if(val.len==6 && strncmp(val.s, "ka-src", 6)==0) {
  1025. dctx->iflags |= DLG_IFLAG_KA_SRC;
  1026. d = dlg_get_by_iuid(&dctx->iuid);
  1027. if(d!=NULL) {
  1028. d->iflags |= DLG_IFLAG_KA_SRC;
  1029. dlg_release(d);
  1030. }
  1031. } else if(val.len==6 && strncmp(val.s, "ka-dst", 6)==0) {
  1032. dctx->iflags |= DLG_IFLAG_KA_DST;
  1033. d = dlg_get_by_iuid(&dctx->iuid);
  1034. if(d!=NULL) {
  1035. d->iflags |= DLG_IFLAG_KA_DST;
  1036. dlg_release(d);
  1037. }
  1038. } else if(val.len==15 && strncmp(val.s, "timeout-noreset", 15)==0) {
  1039. dctx->iflags |= DLG_IFLAG_TIMER_NORESET;
  1040. d = dlg_get_by_iuid(&dctx->iuid);
  1041. if(d!=NULL) {
  1042. d->iflags |= DLG_IFLAG_TIMER_NORESET;
  1043. dlg_release(d);
  1044. }
  1045. } else {
  1046. LM_ERR("unknown property value [%.*s]\n", val.len, val.s);
  1047. return -1;
  1048. }
  1049. return 1;
  1050. }
  1051. static int w_dlg_set_timeout_by_profile3(struct sip_msg *msg, char *profile,
  1052. char *value, char *timeout_str)
  1053. {
  1054. pv_elem_t *pve = NULL;
  1055. str val_s;
  1056. pve = (pv_elem_t *) value;
  1057. if(pve != NULL && ((struct dlg_profile_table *) profile)->has_value) {
  1058. if(pv_printf_s(msg,pve, &val_s) != 0 ||
  1059. !val_s.s || val_s.len == 0) {
  1060. LM_WARN("cannot get string for value\n");
  1061. return -1;
  1062. }
  1063. }
  1064. if(dlg_set_timeout_by_profile((struct dlg_profile_table *) profile,
  1065. &val_s, atoi(timeout_str)) != 0)
  1066. return -1;
  1067. return 1;
  1068. }
  1069. static int w_dlg_set_timeout_by_profile2(struct sip_msg *msg,
  1070. char *profile, char *timeout_str)
  1071. {
  1072. return w_dlg_set_timeout_by_profile3(msg, profile, NULL, timeout_str);
  1073. }
  1074. void dlg_ka_timer_exec(unsigned int ticks, void* param)
  1075. {
  1076. dlg_ka_run(ticks);
  1077. }
  1078. void dlg_clean_timer_exec(unsigned int ticks, void* param)
  1079. {
  1080. dlg_clean_run(ticks);
  1081. remove_expired_remote_profiles(time(NULL));
  1082. }
  1083. static int fixup_dlg_bye(void** param, int param_no)
  1084. {
  1085. char *val;
  1086. int n = 0;
  1087. if (param_no==1) {
  1088. val = (char*)*param;
  1089. if (strcasecmp(val,"all")==0) {
  1090. n = 0;
  1091. } else if (strcasecmp(val,"caller")==0) {
  1092. n = 1;
  1093. } else if (strcasecmp(val,"callee")==0) {
  1094. n = 2;
  1095. } else {
  1096. LM_ERR("invalid param \"%s\"\n", val);
  1097. return E_CFG;
  1098. }
  1099. pkg_free(*param);
  1100. *param=(void*)(long)n;
  1101. } else {
  1102. LM_ERR("called with parameter != 1\n");
  1103. return E_BUG;
  1104. }
  1105. return 0;
  1106. }
  1107. static int fixup_dlg_refer(void** param, int param_no)
  1108. {
  1109. char *val;
  1110. int n = 0;
  1111. if (param_no==1) {
  1112. val = (char*)*param;
  1113. if (strcasecmp(val,"caller")==0) {
  1114. n = 1;
  1115. } else if (strcasecmp(val,"callee")==0) {
  1116. n = 2;
  1117. } else {
  1118. LM_ERR("invalid param \"%s\"\n", val);
  1119. return E_CFG;
  1120. }
  1121. pkg_free(*param);
  1122. *param=(void*)(long)n;
  1123. } else if (param_no==2) {
  1124. return fixup_spve_null(param, 1);
  1125. } else {
  1126. LM_ERR("called with parameter idx %d\n", param_no);
  1127. return E_BUG;
  1128. }
  1129. return 0;
  1130. }
  1131. static int fixup_dlg_bridge(void** param, int param_no)
  1132. {
  1133. if (param_no>=1 && param_no<=3) {
  1134. return fixup_spve_null(param, 1);
  1135. } else {
  1136. LM_ERR("called with parameter idx %d\n", param_no);
  1137. return E_BUG;
  1138. }
  1139. return 0;
  1140. }
  1141. static int w_dlg_get(struct sip_msg *msg, char *ci, char *ft, char *tt)
  1142. {
  1143. dlg_cell_t *dlg = NULL;
  1144. str sc = {0,0};
  1145. str sf = {0,0};
  1146. str st = {0,0};
  1147. unsigned int dir = 0;
  1148. if(ci==0 || ft==0 || tt==0)
  1149. {
  1150. LM_ERR("invalid parameters\n");
  1151. return -1;
  1152. }
  1153. if(fixup_get_svalue(msg, (gparam_p)ci, &sc)!=0)
  1154. {
  1155. LM_ERR("unable to get Call-ID\n");
  1156. return -1;
  1157. }
  1158. if(sc.s==NULL || sc.len == 0)
  1159. {
  1160. LM_ERR("invalid Call-ID parameter\n");
  1161. return -1;
  1162. }
  1163. if(fixup_get_svalue(msg, (gparam_p)ft, &sf)!=0)
  1164. {
  1165. LM_ERR("unable to get From tag\n");
  1166. return -1;
  1167. }
  1168. if(sf.s==NULL || sf.len == 0)
  1169. {
  1170. LM_ERR("invalid From tag parameter\n");
  1171. return -1;
  1172. }
  1173. if(fixup_get_svalue(msg, (gparam_p)tt, &st)!=0)
  1174. {
  1175. LM_ERR("unable to get To Tag\n");
  1176. return -1;
  1177. }
  1178. if(st.s==NULL || st.len == 0)
  1179. {
  1180. LM_ERR("invalid To tag parameter\n");
  1181. return -1;
  1182. }
  1183. dlg = get_dlg(&sc, &sf, &st, &dir);
  1184. if(dlg==NULL)
  1185. return -1;
  1186. /* set shorcut to dialog internal unique id */
  1187. _dlg_ctx.iuid.h_entry = dlg->h_entry;
  1188. _dlg_ctx.iuid.h_id = dlg->h_id;
  1189. _dlg_ctx.dir = dir;
  1190. dlg_release(dlg);
  1191. return 1;
  1192. }
  1193. /**
  1194. *
  1195. */
  1196. static int w_dlg_remote_profile(sip_msg_t *msg, char *cmd, char *pname,
  1197. char *pval, char *puid, char *expires)
  1198. {
  1199. str scmd;
  1200. str sname;
  1201. str sval;
  1202. str suid;
  1203. int ival;
  1204. int ret;
  1205. if(fixup_get_svalue(msg, (gparam_t*)cmd, &scmd)!=0) {
  1206. LM_ERR("unable to get command\n");
  1207. return -1;
  1208. }
  1209. if(fixup_get_svalue(msg, (gparam_t*)pname, &sname)!=0) {
  1210. LM_ERR("unable to get profile name\n");
  1211. return -1;
  1212. }
  1213. if(fixup_get_svalue(msg, (gparam_t*)pval, &sval)!=0) {
  1214. LM_ERR("unable to get profile value\n");
  1215. return -1;
  1216. }
  1217. if(fixup_get_svalue(msg, (gparam_t*)puid, &suid)!=0) {
  1218. LM_ERR("unable to get profile uid\n");
  1219. return -1;
  1220. }
  1221. if(fixup_get_ivalue(msg, (gparam_t*)expires, &ival)!=0) {
  1222. LM_ERR("no hash entry value value\n");
  1223. return -1;
  1224. }
  1225. ret = dlg_cmd_remote_profile(&scmd, &sname, &sval, &suid, (time_t)ival, 0);
  1226. if(ret==0)
  1227. return 1;
  1228. return ret;
  1229. }
  1230. static int fixup_dlg_remote_profile(void** param, int param_no)
  1231. {
  1232. if(param_no>=1 && param_no<=4)
  1233. return fixup_spve_null(param, 1);
  1234. if(param_no==5)
  1235. return fixup_igp_null(param, 1);
  1236. return 0;
  1237. }
  1238. struct mi_root * mi_dlg_bridge(struct mi_root *cmd_tree, void *param)
  1239. {
  1240. str from = {0,0};
  1241. str to = {0,0};
  1242. str op = {0,0};
  1243. str bd = {0,0};
  1244. struct mi_node* node;
  1245. node = cmd_tree->node.kids;
  1246. if(node == NULL)
  1247. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  1248. from = node->value;
  1249. if(from.len<=0 || from.s==NULL)
  1250. {
  1251. LM_ERR("bad From value\n");
  1252. return init_mi_tree( 500, "Bad From value", 14);
  1253. }
  1254. node = node->next;
  1255. if(node == NULL)
  1256. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  1257. to = node->value;
  1258. if(to.len<=0 || to.s == NULL)
  1259. {
  1260. return init_mi_tree(500, "Bad To value", 12);
  1261. }
  1262. node= node->next;
  1263. if(node != NULL)
  1264. {
  1265. op = node->value;
  1266. if(op.len<=0 || op.s==NULL)
  1267. {
  1268. return init_mi_tree(500, "Bad OP value", 12);
  1269. }
  1270. if(op.len==1 && *op.s=='.')
  1271. {
  1272. op.s = NULL;
  1273. op.len = 0;
  1274. }
  1275. node= node->next;
  1276. if(node != NULL)
  1277. {
  1278. bd = node->value;
  1279. if(bd.len<=0 || bd.s==NULL)
  1280. {
  1281. return init_mi_tree(500, "Bad SDP value", 13);
  1282. }
  1283. }
  1284. }
  1285. if(dlg_bridge(&from, &to, &op, &bd)!=0)
  1286. return init_mi_tree(500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN);
  1287. return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  1288. }
  1289. /**************************** RPC functions ******************************/
  1290. /*!
  1291. * \brief Helper method that outputs a dialog via the RPC interface
  1292. * \see rpc_print_dlg
  1293. * \param rpc RPC node that should be filled
  1294. * \param c RPC void pointer
  1295. * \param dlg printed dialog
  1296. * \param with_context if 1 then the dialog context will be also printed
  1297. * \return 0 on success, -1 on failure
  1298. */
  1299. static inline void internal_rpc_print_dlg(rpc_t *rpc, void *c, dlg_cell_t *dlg,
  1300. int with_context)
  1301. {
  1302. rpc_cb_ctx_t rpc_cb;
  1303. void *h, *sh, *ssh;
  1304. dlg_profile_link_t *pl;
  1305. dlg_var_t *var;
  1306. if (rpc->add(c, "{", &h) < 0) goto error;
  1307. rpc->struct_add(h, "ddSSSdddddddd",
  1308. "h_entry", dlg->h_entry,
  1309. "h_id", dlg->h_id,
  1310. "call-id", &dlg->callid,
  1311. "from_uri", &dlg->from_uri,
  1312. "to_uri", &dlg->to_uri,
  1313. "state", dlg->state,
  1314. "start_ts", dlg->start_ts,
  1315. "init_ts", dlg->init_ts,
  1316. "timeout", dlg->tl.timeout ? time(0) + dlg->tl.timeout - get_ticks() : 0,
  1317. "lifetime", dlg->lifetime,
  1318. "dflags", dlg->dflags,
  1319. "sflags", dlg->sflags,
  1320. "iflags", dlg->iflags);
  1321. if (rpc->struct_add(h, "{", "caller", &sh) < 0) goto error;
  1322. rpc->struct_add(sh, "SSSSS",
  1323. "tag", &dlg->tag[DLG_CALLER_LEG],
  1324. "contact", &dlg->contact[DLG_CALLER_LEG],
  1325. "cseq", &dlg->cseq[DLG_CALLER_LEG],
  1326. "route_set", &dlg->route_set[DLG_CALLER_LEG],
  1327. "socket", dlg->bind_addr[DLG_CALLER_LEG] ? &dlg->bind_addr[DLG_CALLER_LEG]->sock_str : &empty_str);
  1328. if (rpc->struct_add(h, "{", "callee", &sh) < 0) goto error;
  1329. rpc->struct_add(sh, "SSSSS",
  1330. "tag", &dlg->tag[DLG_CALLEE_LEG],
  1331. "contact", &dlg->contact[DLG_CALLEE_LEG],
  1332. "cseq", &dlg->cseq[DLG_CALLEE_LEG],
  1333. "route_set", &dlg->route_set[DLG_CALLEE_LEG],
  1334. "socket", dlg->bind_addr[DLG_CALLEE_LEG] ? &dlg->bind_addr[DLG_CALLEE_LEG]->sock_str : &empty_str);
  1335. if (rpc->struct_add(h, "[", "profiles", &sh) < 0) goto error;
  1336. for (pl = dlg->profile_links ; pl ; pl=pl->next) {
  1337. if (pl->profile->has_value) {
  1338. rpc->array_add(sh, "{", &ssh);
  1339. rpc->struct_add(ssh, "S", pl->profile->name.s, &pl->hash_linker.value);
  1340. } else {
  1341. rpc->array_add(sh, "S", &pl->profile->name);
  1342. }
  1343. }
  1344. if (rpc->struct_add(h, "[", "variables", &sh) < 0) goto error;
  1345. for(var=dlg->vars ; var ; var=var->next) {
  1346. rpc->array_add(sh, "{", &ssh);
  1347. rpc->struct_add(ssh, "S", var->key.s, &var->value);
  1348. }
  1349. if (with_context) {
  1350. rpc_cb.rpc = rpc;
  1351. rpc_cb.c = h;
  1352. run_dlg_callbacks( DLGCB_RPC_CONTEXT, dlg, NULL, NULL, DLG_DIR_NONE, (void *)&rpc_cb);
  1353. }
  1354. return;
  1355. error:
  1356. LM_ERR("Failed to add item to RPC response\n");
  1357. return;
  1358. }
  1359. /*!
  1360. * \brief Helper function that outputs all dialogs via the RPC interface
  1361. * \see rpc_print_dlgs
  1362. * \param rpc RPC node that should be filled
  1363. * \param c RPC void pointer
  1364. * \param with_context if 1 then the dialog context will be also printed
  1365. */
  1366. static void internal_rpc_print_dlgs(rpc_t *rpc, void *c, int with_context)
  1367. {
  1368. dlg_cell_t *dlg;
  1369. unsigned int i;
  1370. for( i=0 ; i<d_table->size ; i++ ) {
  1371. dlg_lock( d_table, &(d_table->entries[i]) );
  1372. for( dlg=d_table->entries[i].first ; dlg ; dlg=dlg->next ) {
  1373. internal_rpc_print_dlg(rpc, c, dlg, with_context);
  1374. }
  1375. dlg_unlock( d_table, &(d_table->entries[i]) );
  1376. }
  1377. }
  1378. /*!
  1379. * \brief Helper function that outputs a dialog via the RPC interface
  1380. * \see rpc_print_dlgs
  1381. * \param rpc RPC node that should be filled
  1382. * \param c RPC void pointer
  1383. * \param with_context if 1 then the dialog context will be also printed
  1384. */
  1385. static void internal_rpc_print_single_dlg(rpc_t *rpc, void *c, int with_context) {
  1386. str callid, from_tag;
  1387. dlg_entry_t *d_entry;
  1388. dlg_cell_t *dlg;
  1389. unsigned int h_entry;
  1390. if (rpc->scan(c, ".S.S", &callid, &from_tag) < 2) return;
  1391. h_entry = core_hash( &callid, 0, d_table->size);
  1392. d_entry = &(d_table->entries[h_entry]);
  1393. dlg_lock( d_table, d_entry);
  1394. for( dlg = d_entry->first ; dlg ; dlg = dlg->next ) {
  1395. if (match_downstream_dialog( dlg, &callid, &from_tag)==1) {
  1396. internal_rpc_print_dlg(rpc, c, dlg, with_context);
  1397. }
  1398. }
  1399. dlg_unlock( d_table, d_entry);
  1400. }
  1401. /*!
  1402. * \brief Helper function that outputs the size of a given profile via the RPC interface
  1403. * \see rpc_profile_get_size
  1404. * \see rpc_profile_w_value_get_size
  1405. * \param rpc RPC node that should be filled
  1406. * \param c RPC void pointer
  1407. * \param profile_name the given profile
  1408. * \param value the given profile value
  1409. */
  1410. static void internal_rpc_profile_get_size(rpc_t *rpc, void *c, str *profile_name,
  1411. str *value) {
  1412. unsigned int size;
  1413. dlg_profile_table_t *profile;
  1414. profile = search_dlg_profile( profile_name );
  1415. if (!profile) {
  1416. rpc->fault(c, 404, "Profile not found: %.*s",
  1417. profile_name->len, profile_name->s);
  1418. return;
  1419. }
  1420. size = get_profile_size(profile, value);
  1421. rpc->add(c, "d", size);
  1422. return;
  1423. }
  1424. /*!
  1425. * \brief Helper function that outputs the dialogs belonging to a given profile via the RPC interface
  1426. * \see rpc_profile_print_dlgs
  1427. * \see rpc_profile_w_value_print_dlgs
  1428. * \param rpc RPC node that should be filled
  1429. * \param c RPC void pointer
  1430. * \param profile_name the given profile
  1431. * \param value the given profile value
  1432. * \param with_context if 1 then the dialog context will be also printed
  1433. */
  1434. static void internal_rpc_profile_print_dlgs(rpc_t *rpc, void *c, str *profile_name,
  1435. str *value) {
  1436. dlg_profile_table_t *profile;
  1437. dlg_profile_hash_t *ph;
  1438. unsigned int i;
  1439. profile = search_dlg_profile( profile_name );
  1440. if (!profile) {
  1441. rpc->fault(c, 404, "Profile not found: %.*s",
  1442. profile_name->len, profile_name->s);
  1443. return;
  1444. }
  1445. /* go through the hash and print the dialogs */
  1446. if (profile->has_value==0)
  1447. value=NULL;
  1448. lock_get( &profile->lock );
  1449. for ( i=0 ; i< profile->size ; i++ ) {
  1450. ph = profile->entries[i].first;
  1451. if(ph) {
  1452. do {
  1453. if ((!value || (STR_EQ(*value, ph->value))) && ph->dlg) {
  1454. /* print dialog */
  1455. internal_rpc_print_dlg(rpc, c, ph->dlg, 0);
  1456. }
  1457. /* next */
  1458. ph=ph->next;
  1459. }while(ph!=profile->entries[i].first);
  1460. }
  1461. lock_release(&profile->lock);
  1462. }
  1463. }
  1464. /*
  1465. * Wrapper around is_known_dlg().
  1466. */
  1467. static int w_is_known_dlg(sip_msg_t *msg) {
  1468. return is_known_dlg(msg);
  1469. }
  1470. static const char *rpc_print_dlgs_doc[2] = {
  1471. "Print all dialogs", 0
  1472. };
  1473. static const char *rpc_print_dlgs_ctx_doc[2] = {
  1474. "Print all dialogs with associated context", 0
  1475. };
  1476. static const char *rpc_print_dlg_doc[2] = {
  1477. "Print dialog based on callid and fromtag", 0
  1478. };
  1479. static const char *rpc_print_dlg_ctx_doc[2] = {
  1480. "Print dialog with associated context based on callid and fromtag", 0
  1481. };
  1482. static const char *rpc_end_dlg_entry_id_doc[2] = {
  1483. "End a given dialog based on [h_entry] [h_id]", 0
  1484. };
  1485. static const char *rpc_profile_get_size_doc[2] = {
  1486. "Returns the number of dialogs belonging to a profile", 0
  1487. };
  1488. static const char *rpc_profile_print_dlgs_doc[2] = {
  1489. "Lists all the dialogs belonging to a profile", 0
  1490. };
  1491. static const char *rpc_dlg_bridge_doc[2] = {
  1492. "Bridge two SIP addresses in a call using INVITE(hold)-REFER-BYE mechanism:\
  1493. to, from, [outbound SIP proxy]", 0
  1494. };
  1495. static void rpc_print_dlgs(rpc_t *rpc, void *c) {
  1496. internal_rpc_print_dlgs(rpc, c, 0);
  1497. }
  1498. static void rpc_print_dlgs_ctx(rpc_t *rpc, void *c) {
  1499. internal_rpc_print_dlgs(rpc, c, 1);
  1500. }
  1501. static void rpc_print_dlg(rpc_t *rpc, void *c) {
  1502. internal_rpc_print_single_dlg(rpc, c, 0);
  1503. }
  1504. static void rpc_print_dlg_ctx(rpc_t *rpc, void *c) {
  1505. internal_rpc_print_single_dlg(rpc, c, 1);
  1506. }
  1507. static void rpc_end_dlg_entry_id(rpc_t *rpc, void *c) {
  1508. unsigned int h_entry, h_id;
  1509. dlg_cell_t * dlg = NULL;
  1510. str rpc_extra_hdrs = {NULL,0};
  1511. int n;
  1512. n = rpc->scan(c, "dd", &h_entry, &h_id);
  1513. if (n < 2) {
  1514. LM_ERR("unable to read the parameters (%d)\n", n);
  1515. rpc->fault(c, 500, "Invalid parameters");
  1516. return;
  1517. }
  1518. if(rpc->scan(c, "*S", &rpc_extra_hdrs)<1)
  1519. {
  1520. rpc_extra_hdrs.s = NULL;
  1521. rpc_extra_hdrs.len = 0;
  1522. }
  1523. dlg = dlg_lookup(h_entry, h_id);
  1524. if(dlg==NULL) {
  1525. rpc->fault(c, 404, "Dialog not found");
  1526. return;
  1527. }
  1528. dlg_bye_all(dlg, (rpc_extra_hdrs.len>0)?&rpc_extra_hdrs:NULL);
  1529. dlg_release(dlg);
  1530. }
  1531. static void rpc_profile_get_size(rpc_t *rpc, void *c) {
  1532. str profile_name = {NULL,0};
  1533. str value = {NULL,0};
  1534. if (rpc->scan(c, ".S", &profile_name) < 1) return;
  1535. if (rpc->scan(c, "*.S", &value) > 0) {
  1536. internal_rpc_profile_get_size(rpc, c, &profile_name, &value);
  1537. } else {
  1538. internal_rpc_profile_get_size(rpc, c, &profile_name, NULL);
  1539. }
  1540. return;
  1541. }
  1542. static void rpc_profile_print_dlgs(rpc_t *rpc, void *c) {
  1543. str profile_name = {NULL,0};
  1544. str value = {NULL,0};
  1545. if (rpc->scan(c, ".S", &profile_name) < 1) return;
  1546. if (rpc->scan(c, "*.S", &value) > 0) {
  1547. internal_rpc_profile_print_dlgs(rpc, c, &profile_name, &value);
  1548. } else {
  1549. internal_rpc_profile_print_dlgs(rpc, c, &profile_name, NULL);
  1550. }
  1551. return;
  1552. }
  1553. static void rpc_dlg_bridge(rpc_t *rpc, void *c) {
  1554. str from = {NULL,0};
  1555. str to = {NULL,0};
  1556. str op = {NULL,0};
  1557. str bd = {NULL,0};
  1558. int n;
  1559. n = rpc->scan(c, "SS", &from, &to);
  1560. if (n< 2) {
  1561. LM_ERR("unable to read the parameters (%d)\n", n);
  1562. rpc->fault(c, 500, "Invalid parameters");
  1563. return;
  1564. }
  1565. if(rpc->scan(c, "*S", &op)<1) {
  1566. op.s = NULL;
  1567. op.len = 0;
  1568. } else {
  1569. if(op.len==1 && *op.s=='.') {
  1570. op.s = NULL;
  1571. op.len = 0;
  1572. }
  1573. if(rpc->scan(c, "*S", &bd)<1) {
  1574. bd.s = NULL;
  1575. bd.len = 0;
  1576. }
  1577. }
  1578. dlg_bridge(&from, &to, &op, &bd);
  1579. }
  1580. static rpc_export_t rpc_methods[] = {
  1581. {"dlg.list", rpc_print_dlgs, rpc_print_dlgs_doc, RET_ARRAY},
  1582. {"dlg.list_ctx", rpc_print_dlgs_ctx, rpc_print_dlgs_ctx_doc, RET_ARRAY},
  1583. {"dlg.dlg_list", rpc_print_dlg, rpc_print_dlg_doc, 0},
  1584. {"dlg.dlg_list_ctx", rpc_print_dlg_ctx, rpc_print_dlg_ctx_doc, 0},
  1585. {"dlg.end_dlg", rpc_end_dlg_entry_id, rpc_end_dlg_entry_id_doc, 0},
  1586. {"dlg.profile_get_size", rpc_profile_get_size, rpc_profile_get_size_doc, 0},
  1587. {"dlg.profile_list", rpc_profile_print_dlgs, rpc_profile_print_dlgs_doc, RET_ARRAY},
  1588. {"dlg.bridge_dlg", rpc_dlg_bridge, rpc_dlg_bridge_doc, 0},
  1589. {0, 0, 0, 0}
  1590. };