dlg_handlers.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. /*
  2. * Copyright (C) 2006 Voice System SRL
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. /*!
  22. * \file
  23. * \brief Functions related to dialog handling
  24. * \ingroup dialog
  25. * Module: \ref dialog
  26. */
  27. #include <string.h>
  28. #include <time.h>
  29. #include "../../trim.h"
  30. #include "../../pvar.h"
  31. #include "../../timer.h"
  32. #include "../../lib/kcore/statistics.h"
  33. #include "../../action.h"
  34. #include "../../script_cb.h"
  35. #include "../../pt.h"
  36. #include "../../lib/kcore/faked_msg.h"
  37. #include "../../parser/parse_from.h"
  38. #include "../../parser/parse_cseq.h"
  39. #include "../../parser/contact/parse_contact.h"
  40. #include "../../parser/parse_from.h"
  41. #include "../../parser/parse_rr.h"
  42. #include "../../modules/tm/tm_load.h"
  43. #include "../rr/api.h"
  44. #include "dlg_hash.h"
  45. #include "dlg_timer.h"
  46. #include "dlg_cb.h"
  47. #include "dlg_handlers.h"
  48. #include "dlg_req_within.h"
  49. #include "dlg_db_handler.h"
  50. #include "dlg_profile.h"
  51. #include "dlg_var.h"
  52. static str rr_param; /*!< record-route parameter for matching */
  53. static int dlg_flag; /*!< flag for dialog tracking */
  54. static pv_spec_t *timeout_avp; /*!< AVP for timeout setting */
  55. static int default_timeout; /*!< default dialog timeout */
  56. static int seq_match_mode; /*!< dlg_match mode */
  57. static int shutdown_done = 0; /*!< 1 when destroy_dlg_handlers was called */
  58. extern int detect_spirals;
  59. extern int dlg_timeout_noreset;
  60. extern int initial_cbs_inscript;
  61. extern int dlg_send_bye;
  62. extern int dlg_event_rt[DLG_EVENTRT_MAX];
  63. extern int dlg_wait_ack;
  64. int spiral_detected = -1;
  65. extern struct rr_binds d_rrb; /*!< binding to record-routing module */
  66. /* statistic variables */
  67. extern stat_var *early_dlgs; /*!< number of early dialogs */
  68. extern stat_var *processed_dlgs; /*!< number of processed dialogs */
  69. extern stat_var *expired_dlgs; /*!< number of expired dialogs */
  70. extern stat_var *failed_dlgs; /*!< number of failed dialogs */
  71. extern pv_elem_t *ruri_param_model; /*!< pv-string to get r-uri */
  72. static unsigned int CURR_DLG_LIFETIME = 0; /*!< current dialog lifetime */
  73. static unsigned int CURR_DLG_STATUS = 0; /*!< current dialog state */
  74. static unsigned int CURR_DLG_ID = 0xffffffff; /*!< current dialog id */
  75. /*! size of the dialog record-route parameter */
  76. #define RR_DLG_PARAM_SIZE (2*2*sizeof(int)+3+MAX_DLG_RR_PARAM_NAME)
  77. /*! separator inside the record-route paramter */
  78. #define DLG_SEPARATOR '.'
  79. int dlg_set_tm_callbacks(tm_cell_t *t, sip_msg_t *req, dlg_cell_t *dlg,
  80. int mode);
  81. int dlg_set_tm_waitack(tm_cell_t *t, dlg_cell_t *dlg);
  82. /*!
  83. * \brief Initialize the dialog handlers
  84. * \param rr_param_p added record-route parameter
  85. * \param dlg_flag_p dialog flag
  86. * \param timeout_avp_p AVP for timeout setting
  87. * \param default_timeout_p default timeout
  88. * \param seq_match_mode_p matching mode
  89. */
  90. void init_dlg_handlers(char *rr_param_p, int dlg_flag_p,
  91. pv_spec_t *timeout_avp_p ,int default_timeout_p,
  92. int seq_match_mode_p)
  93. {
  94. rr_param.s = rr_param_p;
  95. rr_param.len = strlen(rr_param.s);
  96. dlg_flag = 1<<dlg_flag_p;
  97. timeout_avp = timeout_avp_p;
  98. default_timeout = default_timeout_p;
  99. seq_match_mode = seq_match_mode_p;
  100. }
  101. /*!
  102. * \brief Shutdown operation of the module
  103. */
  104. void destroy_dlg_handlers(void)
  105. {
  106. shutdown_done = 1;
  107. }
  108. /*!
  109. * \brief Add record-route parameter for dialog tracking
  110. * \param req SIP request
  111. * \param entry dialog hash entry
  112. * \param id dialog hash id
  113. * \return 0 on success, -1 on failure
  114. */
  115. static inline int add_dlg_rr_param(struct sip_msg *req, unsigned int entry,
  116. unsigned int id)
  117. {
  118. static char buf[RR_DLG_PARAM_SIZE];
  119. str s;
  120. int n;
  121. char *p;
  122. s.s = p = buf;
  123. *(p++) = ';';
  124. memcpy(p, rr_param.s, rr_param.len);
  125. p += rr_param.len;
  126. *(p++) = '=';
  127. n = RR_DLG_PARAM_SIZE - (p-buf);
  128. if (int2reverse_hex( &p, &n, entry)==-1)
  129. return -1;
  130. *(p++) = DLG_SEPARATOR;
  131. n = RR_DLG_PARAM_SIZE - (p-buf);
  132. if (int2reverse_hex( &p, &n, id)==-1)
  133. return -1;
  134. s.len = p-buf;
  135. if (d_rrb.add_rr_param( req, &s)<0) {
  136. LM_ERR("failed to add rr param\n");
  137. return -1;
  138. }
  139. return 0;
  140. }
  141. /*!
  142. * \brief Parse SIP message and populate leg informations
  143. *
  144. * Parse SIP message and populate leg informations.
  145. * \param dlg the dialog to add cseq, contact & record_route
  146. * \param msg sip message
  147. * \param t transaction
  148. * \param leg type of the call leg
  149. * \param tag SIP To tag
  150. * \return 0 on success, -1 on failure
  151. * \note for a request: get record route in normal order, for a reply get
  152. * in reverse order, skipping the ones from the request and the proxies' own
  153. */
  154. int populate_leg_info( struct dlg_cell *dlg, struct sip_msg *msg,
  155. struct cell* t, unsigned int leg, str *tag)
  156. {
  157. unsigned int skip_recs;
  158. str cseq;
  159. str contact;
  160. str rr_set;
  161. dlg->bind_addr[leg] = msg->rcv.bind_address;
  162. /* extract the cseq number as string */
  163. if (leg==DLG_CALLER_LEG) {
  164. if((!msg->cseq && (parse_headers(msg,HDR_CSEQ_F,0)<0 || !msg->cseq))
  165. || !msg->cseq->parsed){
  166. LM_ERR("bad sip message or missing CSeq hdr :-/\n");
  167. goto error0;
  168. }
  169. cseq = (get_cseq(msg))->number;
  170. } else {
  171. /* use the same as in request */
  172. cseq = dlg->cseq[DLG_CALLEE_LEG];
  173. }
  174. /* extract the contact address */
  175. if (!msg->contact&&(parse_headers(msg,HDR_CONTACT_F,0)<0||!msg->contact)){
  176. LM_ERR("bad sip message or missing Contact hdr\n");
  177. goto error0;
  178. }
  179. if ( parse_contact(msg->contact)<0 ||
  180. ((contact_body_t *)msg->contact->parsed)->contacts==NULL ||
  181. ((contact_body_t *)msg->contact->parsed)->contacts->next!=NULL ) {
  182. LM_ERR("bad Contact HDR\n");
  183. goto error0;
  184. }
  185. contact = ((contact_body_t *)msg->contact->parsed)->contacts->uri;
  186. /* extract the RR parts */
  187. if(!msg->record_route && (parse_headers(msg,HDR_EOH_F,0)<0) ){
  188. LM_ERR("failed to parse record route header\n");
  189. goto error0;
  190. }
  191. if (leg==DLG_CALLER_LEG) {
  192. skip_recs = 0;
  193. } else {
  194. /* was the 200 OK received or local generated */
  195. skip_recs = dlg->from_rr_nb +
  196. ((t->relayed_reply_branch>=0)?
  197. ((t->uac[t->relayed_reply_branch].flags&TM_UAC_FLAG_R2)?2:
  198. ((t->uac[t->relayed_reply_branch].flags&TM_UAC_FLAG_RR)?1:0))
  199. :0);
  200. }
  201. if(msg->record_route){
  202. if( print_rr_body(msg->record_route, &rr_set, leg,
  203. &skip_recs) != 0 ){
  204. LM_ERR("failed to print route records \n");
  205. goto error0;
  206. }
  207. } else {
  208. rr_set.s = 0;
  209. rr_set.len = 0;
  210. }
  211. if(leg==DLG_CALLER_LEG)
  212. dlg->from_rr_nb = skip_recs;
  213. LM_DBG("route_set %.*s, contact %.*s, cseq %.*s and bind_addr %.*s\n",
  214. rr_set.len, rr_set.s, contact.len, contact.s,
  215. cseq.len, cseq.s,
  216. msg->rcv.bind_address->sock_str.len,
  217. msg->rcv.bind_address->sock_str.s);
  218. if (dlg_set_leg_info( dlg, tag, &rr_set, &contact, &cseq, leg)!=0) {
  219. LM_ERR("dlg_set_leg_info failed\n");
  220. if (rr_set.s) pkg_free(rr_set.s);
  221. goto error0;
  222. }
  223. if (rr_set.s) pkg_free(rr_set.s);
  224. return 0;
  225. error0:
  226. return -1;
  227. }
  228. /*!
  229. * \brief Clone dialog internal unique id to shared memory
  230. */
  231. dlg_iuid_t *dlg_get_iuid_shm_clone(dlg_cell_t *dlg)
  232. {
  233. dlg_iuid_t *iuid = NULL;
  234. if(dlg==NULL)
  235. return NULL;
  236. iuid = (dlg_iuid_t*)shm_malloc(sizeof(dlg_iuid_t));
  237. if(iuid==NULL)
  238. {
  239. LM_ERR("failed to clone dialog iuid\n");
  240. return NULL;
  241. }
  242. memset(iuid, 0, sizeof(dlg_iuid_t));
  243. iuid->h_entry = dlg->h_entry;
  244. iuid->h_id = dlg->h_id;
  245. return iuid;
  246. }
  247. /*!
  248. * \brief Free dialog internal unique id stored in shared memory
  249. */
  250. void dlg_iuid_sfree(void *iuid)
  251. {
  252. if(iuid) {
  253. LM_DBG("freeing dlg iuid [%u:%u] (%p)\n",
  254. ((dlg_iuid_t*)iuid)->h_entry,
  255. ((dlg_iuid_t*)iuid)->h_id, iuid);
  256. shm_free(iuid);
  257. }
  258. }
  259. /*!
  260. * \brief Function that executes BYE reply callbacks
  261. * \param t transaction, unused
  262. * \param type type of the callback, should be TMCB_RESPONSE_FWDED
  263. * \param params saved dialog structure inside the callback
  264. */
  265. static void dlg_terminated_confirmed(tm_cell_t *t, int type,
  266. struct tmcb_params* params)
  267. {
  268. dlg_cell_t *dlg = NULL;
  269. dlg_iuid_t *iuid = NULL;
  270. if(!params || !params->req || !params->param)
  271. {
  272. LM_ERR("invalid parameters!\n");
  273. return;
  274. }
  275. iuid = (dlg_iuid_t*)*params->param;
  276. if(iuid==NULL)
  277. return;
  278. dlg = dlg_get_by_iuid(iuid);
  279. if(dlg==NULL)
  280. {
  281. LM_ERR("failed to get dialog from params!\n");
  282. return;
  283. }
  284. /* dialog termination confirmed (BYE reply) */
  285. run_dlg_callbacks(DLGCB_TERMINATED_CONFIRMED,
  286. dlg,
  287. params->req,
  288. params->rpl,
  289. DLG_DIR_UPSTREAM,
  290. 0);
  291. dlg_release(dlg);
  292. }
  293. /*!
  294. * \brief Execute callback for the BYE request and register callback for the BYE reply
  295. * \param req request message
  296. * \param dlg corresponding dialog
  297. * \param dir message direction
  298. */
  299. static void dlg_terminated(sip_msg_t *req, dlg_cell_t *dlg, unsigned int dir)
  300. {
  301. dlg_iuid_t *iuid = NULL;
  302. if(!req) {
  303. LM_ERR("request is empty!");
  304. return;
  305. }
  306. if(!dlg) {
  307. LM_ERR("dialog is empty!");
  308. return;
  309. }
  310. /* dialog terminated (BYE) */
  311. run_dlg_callbacks(DLGCB_TERMINATED, dlg, req, NULL, dir, 0);
  312. iuid = dlg_get_iuid_shm_clone(dlg);
  313. if(iuid==NULL)
  314. return;
  315. /* register callback for the coresponding reply */
  316. if (d_tmb.register_tmcb(req,
  317. 0,
  318. TMCB_RESPONSE_OUT,
  319. dlg_terminated_confirmed,
  320. (void*)iuid,
  321. dlg_iuid_sfree) <= 0 ) {
  322. LM_ERR("cannot register response callback for BYE request\n");
  323. return;
  324. }
  325. }
  326. /*!
  327. * \brief Function that is registered as TM callback and called on T destroy
  328. *
  329. * - happens when wait_ack==1
  330. *
  331. */
  332. static void dlg_ontdestroy(struct cell* t, int type, struct tmcb_params *param)
  333. {
  334. dlg_cell_t *dlg = NULL;
  335. dlg_iuid_t *iuid = NULL;
  336. iuid = (dlg_iuid_t*)(*param->param);
  337. dlg = dlg_get_by_iuid(iuid);
  338. if(dlg==0)
  339. return;
  340. /* 1 for callback and 1 for dlg lookup */
  341. dlg_unref(dlg, 2);
  342. }
  343. /*!
  344. * \brief Function that is registered as TM callback and called on replies
  345. *
  346. * Function that is registered as TM callback and called on replies. It
  347. * parses the reply and set the appropriate event. This is then used to
  348. * update the dialog state, run eventual dialog callbacks and save or
  349. * update the necessary informations about the dialog.
  350. * \see next_state_dlg
  351. * \param t transaction, unused
  352. * \param type type of the entered callback
  353. * \param param saved dialog structure in the callback
  354. */
  355. static void dlg_onreply(struct cell* t, int type, struct tmcb_params *param)
  356. {
  357. dlg_cell_t *dlg = NULL;
  358. dlg_iuid_t *iuid = NULL;
  359. int new_state, old_state, unref, event;
  360. str tag;
  361. sip_msg_t *req = param->req;
  362. sip_msg_t *rpl = param->rpl;
  363. if (shutdown_done)
  364. return;
  365. iuid = (dlg_iuid_t*)(*param->param);
  366. dlg = dlg_get_by_iuid(iuid);
  367. if(dlg==0)
  368. return;
  369. unref = 0;
  370. if (type & (TMCB_RESPONSE_IN|TMCB_ON_FAILURE)) {
  371. /* Set the dialog context so it is available in onreply_route and failure_route*/
  372. set_current_dialog(req, dlg);
  373. dlg_set_ctx_iuid(dlg);
  374. goto done;
  375. }
  376. if (type==TMCB_RESPONSE_FWDED) {
  377. /* The state does not change, but the msg is mutable in this callback*/
  378. run_dlg_callbacks(DLGCB_RESPONSE_FWDED, dlg, req, rpl, DLG_DIR_UPSTREAM, 0);
  379. goto done;
  380. }
  381. if (type==TMCB_DESTROY)
  382. event = DLG_EVENT_TDEL;
  383. else if (param->code<200)
  384. event = DLG_EVENT_RPL1xx;
  385. else if (param->code<300)
  386. event = DLG_EVENT_RPL2xx;
  387. else
  388. event = DLG_EVENT_RPL3xx;
  389. next_state_dlg( dlg, event, &old_state, &new_state, &unref);
  390. dlg_run_event_route(dlg, (rpl==FAKED_REPLY)?NULL:rpl, old_state, new_state);
  391. if (new_state==DLG_STATE_EARLY) {
  392. run_dlg_callbacks(DLGCB_EARLY, dlg, req, rpl, DLG_DIR_UPSTREAM, 0);
  393. if (old_state!=DLG_STATE_EARLY)
  394. if_update_stat(dlg_enable_stats, early_dlgs, 1);
  395. goto done;
  396. }
  397. if (new_state==DLG_STATE_CONFIRMED_NA &&
  398. old_state!=DLG_STATE_CONFIRMED_NA && old_state!=DLG_STATE_CONFIRMED ) {
  399. LM_DBG("dialog %p confirmed (ACK pending)\n",dlg);
  400. if (rpl != FAKED_REPLY) {
  401. /* get to tag*/
  402. if ( !rpl->to && ((parse_headers(rpl, HDR_TO_F,0)<0)
  403. || !rpl->to) ) {
  404. LM_ERR("bad reply or missing TO hdr :-/\n");
  405. tag.s = 0;
  406. tag.len = 0;
  407. } else {
  408. tag = get_to(rpl)->tag_value;
  409. if (tag.s==0 || tag.len==0) {
  410. LM_ERR("missing TAG param in TO hdr :-/\n");
  411. tag.s = 0;
  412. tag.len = 0;
  413. }
  414. }
  415. /* save callee's tag, cseq, contact and record route*/
  416. if (populate_leg_info( dlg, rpl, t, DLG_CALLEE_LEG, &tag) !=0) {
  417. LM_ERR("could not add further info to the dialog\n");
  418. }
  419. } else {
  420. LM_ERR("Faked reply!\n");
  421. }
  422. /* set start time */
  423. dlg->start_ts = (unsigned int)(time(0));
  424. /* save the settings to the database,
  425. * if realtime saving mode configured- save dialog now
  426. * else: the next time the timer will fire the update*/
  427. dlg->dflags |= DLG_FLAG_NEW;
  428. if ( dlg_db_mode==DB_MODE_REALTIME )
  429. update_dialog_dbinfo(dlg);
  430. if (0 != insert_dlg_timer( &dlg->tl, dlg->lifetime )) {
  431. LM_CRIT("Unable to insert dlg %p [%u:%u] on event %d [%d->%d] "
  432. "with clid '%.*s' and tags '%.*s' '%.*s'\n",
  433. dlg, dlg->h_entry, dlg->h_id, event, old_state, new_state,
  434. dlg->callid.len, dlg->callid.s,
  435. dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
  436. dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
  437. } else {
  438. /* dialog pointer inserted in timer list */
  439. dlg_ref(dlg, 1);
  440. }
  441. /* dialog confirmed (ACK pending) */
  442. run_dlg_callbacks( DLGCB_CONFIRMED_NA, dlg, req, rpl, DLG_DIR_UPSTREAM, 0);
  443. if (old_state==DLG_STATE_EARLY)
  444. if_update_stat(dlg_enable_stats, early_dlgs, -1);
  445. if (unref) dlg_unref(dlg, unref);
  446. if_update_stat(dlg_enable_stats, active_dlgs, 1);
  447. goto done;
  448. }
  449. if ( new_state==DLG_STATE_DELETED
  450. && (old_state==DLG_STATE_UNCONFIRMED
  451. || old_state==DLG_STATE_EARLY) ) {
  452. LM_DBG("dialog %p failed (negative reply)\n", dlg);
  453. /* dialog setup not completed (3456XX) */
  454. run_dlg_callbacks( DLGCB_FAILED, dlg, req, rpl, DLG_DIR_UPSTREAM, 0);
  455. if(dlg_wait_ack==1)
  456. dlg_set_tm_waitack(t, dlg);
  457. /* do unref */
  458. if (unref)
  459. dlg_unref(dlg, unref);
  460. if (old_state==DLG_STATE_EARLY)
  461. if_update_stat(dlg_enable_stats, early_dlgs, -1);
  462. if_update_stat(dlg_enable_stats, failed_dlgs, 1);
  463. goto done;
  464. }
  465. if (unref) dlg_unref(dlg, unref);
  466. done:
  467. /* unref due to dlg_get_by_iuid() */
  468. dlg_release(dlg);
  469. return;
  470. }
  471. /*!
  472. * \brief Helper function that run dialog callbacks on forwarded requests
  473. * \see dlg_seq_up_onreply
  474. * \see dlg_seq_down_onreply
  475. * \param t transaction, unused
  476. * \param type type of the callback, should be TMCB_RESPONSE_FWDED
  477. * \param param saved dialog structure inside the callback
  478. * \param direction direction of the request
  479. */
  480. static void dlg_seq_onreply_helper(struct cell* t, int type,
  481. struct tmcb_params *param, const int direction)
  482. {
  483. dlg_cell_t *dlg = NULL;
  484. dlg_iuid_t *iuid = NULL;
  485. if (shutdown_done)
  486. return;
  487. iuid = (dlg_iuid_t*)(*param->param);
  488. dlg = dlg_get_by_iuid(iuid);
  489. if (dlg==0)
  490. return;
  491. if (type==TMCB_RESPONSE_FWDED)
  492. {
  493. run_dlg_callbacks( DLGCB_RESPONSE_WITHIN,
  494. dlg,
  495. param->req,
  496. param->rpl,
  497. direction,
  498. 0);
  499. }
  500. dlg_release(dlg);
  501. return;
  502. }
  503. /*!
  504. * \brief Run dialog callbacks on forwarded requests in upstream direction
  505. * \see dlg_seq_onreply_helper
  506. * \param t transaction, unused
  507. * \param type type of the callback, should be TMCB_RESPONSE_FWDED
  508. * \param param saved dialog structure inside the callback
  509. */
  510. static void dlg_seq_up_onreply(struct cell* t, int type, struct tmcb_params *param)
  511. {
  512. return dlg_seq_onreply_helper(t, type, param, DLG_DIR_UPSTREAM);
  513. }
  514. /*!
  515. * \brief Run dialog callbacks on forwarded requests in downstream direction
  516. * \see dlg_seq_onreply_helper
  517. * \param t transaction, unused
  518. * \param type type of the callback, should be TMCB_RESPONSE_FWDED
  519. * \param param saved dialog structure inside the callback
  520. */
  521. static void dlg_seq_down_onreply(struct cell* t, int type, struct tmcb_params *param)
  522. {
  523. return dlg_seq_onreply_helper(t, type, param, DLG_DIR_DOWNSTREAM);
  524. }
  525. /*!
  526. * \brief Return the timeout for a dialog
  527. * \param req SIP message
  528. * \return value from timeout AVP if present or default timeout
  529. */
  530. inline static int get_dlg_timeout(struct sip_msg *req)
  531. {
  532. pv_value_t pv_val;
  533. if( timeout_avp ) {
  534. if ( pv_get_spec_value( req, timeout_avp, &pv_val)==0 &&
  535. pv_val.flags&PV_VAL_INT && pv_val.ri>0 ) {
  536. return pv_val.ri;
  537. }
  538. LM_DBG("invalid AVP value, using default timeout\n");
  539. }
  540. return default_timeout;
  541. }
  542. /*!
  543. * \brief Helper function to get the necessary content from SIP message
  544. * \param req SIP request
  545. * \param callid found callid
  546. * \param ftag found from tag
  547. * \param ttag found to tag
  548. * \param with_ttag flag set if to tag must be found for success
  549. * \return 0 on success, -1 on failure
  550. */
  551. static inline int pre_match_parse( struct sip_msg *req, str *callid,
  552. str *ftag, str *ttag, int with_ttag)
  553. {
  554. if (parse_headers(req,HDR_CALLID_F|HDR_TO_F,0)<0 || !req->callid ||
  555. !req->to ) {
  556. LM_ERR("bad request or missing CALLID/TO hdr :-/\n");
  557. return -1;
  558. }
  559. if (get_to(req)->tag_value.len==0) {
  560. if (with_ttag == 1) {
  561. /* out of dialog request with preloaded Route headers; ignore. */
  562. return -1;
  563. } else {
  564. ttag->s = NULL;
  565. ttag->len = 0;
  566. }
  567. } else {
  568. *ttag = get_to(req)->tag_value;
  569. }
  570. if (parse_from_header(req)<0 || get_from(req)->tag_value.len==0) {
  571. LM_ERR("failed to get From header\n");
  572. return -1;
  573. }
  574. /* callid */
  575. *callid = req->callid->body;
  576. trim(callid);
  577. /* from tag */
  578. *ftag = get_from(req)->tag_value;
  579. return 0;
  580. }
  581. /*!
  582. * \brief Function that is registered as TM callback and called on requests
  583. * \see dlg_new_dialog
  584. * \param t transaction, used to created the dialog
  585. * \param type type of the entered callback
  586. * \param param saved dialog structure in the callback
  587. */
  588. void dlg_onreq(struct cell* t, int type, struct tmcb_params *param)
  589. {
  590. sip_msg_t *req = param->req;
  591. dlg_cell_t *dlg = NULL;
  592. if(req->first_line.u.request.method_value == METHOD_BYE) {
  593. _dlg_ctx.t = 1;
  594. return;
  595. }
  596. if(req->first_line.u.request.method_value != METHOD_INVITE)
  597. return;
  598. dlg = dlg_get_ctx_dialog();
  599. if (dlg!=NULL) {
  600. if (!initial_cbs_inscript) {
  601. if (spiral_detected == 1)
  602. run_dlg_callbacks( DLGCB_SPIRALED, dlg,
  603. req, NULL, DLG_DIR_DOWNSTREAM, 0);
  604. else if (spiral_detected == 0)
  605. run_create_callbacks(dlg, req);
  606. }
  607. }
  608. if (dlg==NULL) {
  609. if((req->flags&dlg_flag)!=dlg_flag)
  610. return;
  611. LM_DBG("dialog creation on config flag\n");
  612. dlg_new_dialog(req, t, 1);
  613. dlg = dlg_get_ctx_dialog();
  614. }
  615. if (dlg!=NULL) {
  616. LM_DBG("dialog added to tm callbacks\n");
  617. dlg_set_tm_callbacks(t, req, dlg, spiral_detected);
  618. _dlg_ctx.t = 1;
  619. dlg_release(dlg);
  620. }
  621. }
  622. /*!
  623. * \brief Unreference a new dialog, helper function for dlg_onreq
  624. * \see dlg_onreq
  625. * \param dialog unreferenced dialog
  626. */
  627. #if 0
  628. static void unref_new_dialog(void *iuid)
  629. {
  630. struct tmcb_params p;
  631. memset(&p, 0, sizeof(struct tmcb_params));
  632. p.param = (void*)&iuid;
  633. dlg_onreply(0, TMCB_DESTROY, &p);
  634. }
  635. #endif
  636. /*!
  637. * \brief Create a new dialog from a sip message
  638. *
  639. * Create a new dialog from a SIP message, register a callback
  640. * to keep track of the dialog with help of the tm module.
  641. * This function is either called from the request callback, or
  642. * from the dlg_manage function in the configuration script.
  643. * \see dlg_onreq
  644. * \see w_dlg_manage
  645. * \param req SIP message
  646. * \param t transaction
  647. * \param run_initial_cbs if set zero, initial callbacks are not executed
  648. * \return 0 on success, -1 on failure
  649. */
  650. int dlg_new_dialog(sip_msg_t *req, struct cell *t, const int run_initial_cbs)
  651. {
  652. dlg_cell_t *dlg;
  653. str s;
  654. str callid;
  655. str ftag;
  656. str ttag;
  657. str req_uri;
  658. unsigned int dir;
  659. int mlock;
  660. dlg = dlg_get_ctx_dialog();
  661. if(dlg != NULL) {
  662. dlg_release(dlg);
  663. return -1;
  664. }
  665. if(req->first_line.u.request.method_value != METHOD_INVITE)
  666. return -1;
  667. if(pre_match_parse( req, &callid, &ftag, &ttag, 0)<0) {
  668. LM_WARN("pre-matching failed\n");
  669. return -1;
  670. }
  671. if(ttag.s!=0 && ttag.len!=0)
  672. return -1;
  673. if(pv_printf_s(req, ruri_param_model, &req_uri)<0) {
  674. LM_ERR("error - cannot print the r-uri format\n");
  675. return -1;
  676. }
  677. trim(&req_uri);
  678. dir = DLG_DIR_NONE;
  679. mlock = 1;
  680. /* search dialog by SIP attributes
  681. * - if not found, hash table slot is left locked, to avoid races
  682. * to add 'same' dialog on parallel forking or not-handled-yet
  683. * retransmissions. Release slot after linking new dialog */
  684. dlg = search_dlg(&callid, &ftag, &ttag, &dir);
  685. if(dlg) {
  686. mlock = 0;
  687. if (detect_spirals) {
  688. if (spiral_detected == 1)
  689. return 0;
  690. if ( dlg->state != DLG_STATE_DELETED ) {
  691. LM_DBG("Callid '%.*s' found, must be a spiraled request\n",
  692. callid.len, callid.s);
  693. spiral_detected = 1;
  694. if (run_initial_cbs)
  695. run_dlg_callbacks( DLGCB_SPIRALED, dlg, req, NULL,
  696. DLG_DIR_DOWNSTREAM, 0);
  697. /* set ctx dlg id shortcuts */
  698. _dlg_ctx.iuid.h_entry = dlg->h_entry;
  699. _dlg_ctx.iuid.h_id = dlg->h_id;
  700. /* search_dlg() has incremented the ref count by 1 */
  701. dlg_release(dlg);
  702. return 0;
  703. }
  704. dlg_release(dlg);
  705. }
  706. /* lock the slot - dlg found, but in dlg_state_deleted, do a new one */
  707. dlg_hash_lock(&callid);
  708. }
  709. spiral_detected = 0;
  710. dlg = build_new_dlg (&callid /*callid*/,
  711. &(get_from(req)->uri) /*from uri*/,
  712. &(get_to(req)->uri) /*to uri*/,
  713. &ftag/*from_tag*/,
  714. &req_uri /*r-uri*/ );
  715. if (dlg==0) {
  716. if(likely(mlock==1)) dlg_hash_release(&callid);
  717. LM_ERR("failed to create new dialog\n");
  718. return -1;
  719. }
  720. /* save caller's tag, cseq, contact and record route*/
  721. if (populate_leg_info(dlg, req, t, DLG_CALLER_LEG,
  722. &(get_from(req)->tag_value)) !=0) {
  723. if(likely(mlock==1)) dlg_hash_release(&callid);
  724. LM_ERR("could not add further info to the dialog\n");
  725. shm_free(dlg);
  726. return -1;
  727. }
  728. /* Populate initial varlist: */
  729. dlg->vars = get_local_varlist_pointer(req, 1);
  730. /* if search_dlg() returned NULL, slot was kept locked */
  731. link_dlg(dlg, 0, mlock);
  732. if(likely(mlock==1)) dlg_hash_release(&callid);
  733. dlg->lifetime = get_dlg_timeout(req);
  734. s.s = _dlg_ctx.to_route_name;
  735. s.len = strlen(s.s);
  736. dlg_set_toroute(dlg, &s);
  737. dlg->sflags |= _dlg_ctx.flags;
  738. dlg->iflags |= _dlg_ctx.iflags;
  739. if (dlg_send_bye!=0 || _dlg_ctx.to_bye!=0)
  740. dlg->iflags |= DLG_IFLAG_TIMEOUTBYE;
  741. if (run_initial_cbs) run_create_callbacks( dlg, req);
  742. /* first INVITE seen (dialog created, unconfirmed) */
  743. if ( seq_match_mode!=SEQ_MATCH_NO_ID &&
  744. add_dlg_rr_param( req, dlg->h_entry, dlg->h_id)<0 ) {
  745. LM_ERR("failed to add RR param\n");
  746. goto error;
  747. }
  748. if_update_stat( dlg_enable_stats, processed_dlgs, 1);
  749. _dlg_ctx.cpid = my_pid();
  750. _dlg_ctx.iuid.h_entry = dlg->h_entry;
  751. _dlg_ctx.iuid.h_id = dlg->h_id;
  752. set_current_dialog(req, dlg);
  753. return 0;
  754. error:
  755. if (!spiral_detected)
  756. dlg_unref(dlg, 1); // undo ref regarding linking
  757. return -1;
  758. }
  759. /*!
  760. * \brief add dlg structure to tm callbacks
  761. * \param t current transaction
  762. * \param req current sip request
  763. * \param dlg current dialog
  764. * \param smode if the sip request was spiraled
  765. * \return 0 on success, -1 on failure
  766. */
  767. int dlg_set_tm_callbacks(tm_cell_t *t, sip_msg_t *req, dlg_cell_t *dlg,
  768. int smode)
  769. {
  770. dlg_iuid_t *iuid = NULL;
  771. if(t==NULL)
  772. return -1;
  773. if(smode==0) {
  774. iuid = dlg_get_iuid_shm_clone(dlg);
  775. if(iuid==NULL)
  776. {
  777. LM_ERR("failed to create dialog unique id clone\n");
  778. goto error;
  779. }
  780. if ( d_tmb.register_tmcb( req, t,
  781. TMCB_RESPONSE_IN|TMCB_RESPONSE_READY|TMCB_RESPONSE_FWDED|TMCB_ON_FAILURE,
  782. dlg_onreply, (void*)iuid, dlg_iuid_sfree)<0 ) {
  783. LM_ERR("failed to register TMCB\n");
  784. goto error;
  785. }
  786. }
  787. dlg->dflags |= DLG_FLAG_TM;
  788. return 0;
  789. error:
  790. dlg_iuid_sfree(iuid);
  791. return -1;
  792. }
  793. /*!
  794. * \brief add dlg structure to tm callbacks to wait for negative ACK
  795. * \param t current transaction
  796. * \param dlg current dialog
  797. * \return 0 on success, -1 on failure
  798. */
  799. int dlg_set_tm_waitack(tm_cell_t *t, dlg_cell_t *dlg)
  800. {
  801. dlg_iuid_t *iuid = NULL;
  802. if(t==NULL)
  803. return -1;
  804. LM_DBG("registering TMCB to wait for negative ACK\n");
  805. iuid = dlg_get_iuid_shm_clone(dlg);
  806. if(iuid==NULL)
  807. {
  808. LM_ERR("failed to create dialog unique id clone\n");
  809. goto error;
  810. }
  811. dlg_ref(dlg, 1);
  812. if ( d_tmb.register_tmcb( NULL, t,
  813. TMCB_DESTROY,
  814. dlg_ontdestroy, (void*)iuid, dlg_iuid_sfree)<0 ) {
  815. LM_ERR("failed to register TMCB to wait for negative ACK\n");
  816. dlg_unref(dlg, 1);
  817. goto error;
  818. }
  819. return 0;
  820. error:
  821. dlg_iuid_sfree(iuid);
  822. return -1;
  823. }
  824. /*!
  825. * \brief Parse the record-route parameter, to get dialog information back
  826. * \param p start of parameter string
  827. * \param end end of parameter string
  828. * \param h_entry found dialog hash entry
  829. * \param h_id found dialog hash id
  830. * \return 0 on success, -1 on failure
  831. */
  832. static inline int parse_dlg_rr_param(char *p, char *end, int *h_entry, int *h_id)
  833. {
  834. char *s;
  835. for ( s=p ; p<end && *p!=DLG_SEPARATOR ; p++ );
  836. if (*p!=DLG_SEPARATOR) {
  837. LM_ERR("malformed rr param '%.*s'\n", (int)(long)(end-s), s);
  838. return -1;
  839. }
  840. if ( reverse_hex2int( s, p-s, (unsigned int*)h_entry)<0 ) {
  841. LM_ERR("invalid hash entry '%.*s'\n", (int)(long)(p-s), s);
  842. return -1;
  843. }
  844. if ( reverse_hex2int( p+1, end-(p+1), (unsigned int*)h_id)<0 ) {
  845. LM_ERR("invalid hash id '%.*s'\n", (int)(long)(end-(p+1)), p+1 );
  846. return -1;
  847. }
  848. return 0;
  849. }
  850. /*!
  851. * \brief Update the saved CSEQ information in dialog from SIP message
  852. * \param dlg updated dialog
  853. * \param req SIP request
  854. * \param dir direction of request, must DLG_DIR_UPSTREAM or DLG_DIR_DOWNSTREAM
  855. * \return 0 on success, -1 on failure
  856. */
  857. static inline int update_cseqs(struct dlg_cell *dlg, struct sip_msg *req,
  858. unsigned int dir)
  859. {
  860. if ( (!req->cseq && parse_headers(req,HDR_CSEQ_F,0)<0) || !req->cseq ||
  861. !req->cseq->parsed) {
  862. LM_ERR("bad sip message or missing CSeq hdr :-/\n");
  863. return -1;
  864. }
  865. if ( dir==DLG_DIR_UPSTREAM) {
  866. return dlg_update_cseq(dlg, DLG_CALLEE_LEG,&((get_cseq(req))->number));
  867. } else if ( dir==DLG_DIR_DOWNSTREAM) {
  868. return dlg_update_cseq(dlg, DLG_CALLER_LEG,&((get_cseq(req))->number));
  869. } else {
  870. LM_CRIT("dir is not set!\n");
  871. return -1;
  872. }
  873. }
  874. /*!
  875. * \brief Unreference a dialog from tm callback (another wrapper)
  876. * \param t transaction, unused
  877. * \param type type of the entered callback
  878. * \param param saved dialog structure in the callback
  879. */
  880. static void unref_dlg_from_cb(struct cell* t, int type, struct tmcb_params *param)
  881. {
  882. dlg_cell_t *dlg = NULL;
  883. dlg_iuid_t *iuid = NULL;
  884. iuid = (dlg_iuid_t*)(*param->param);
  885. if (iuid==NULL)
  886. return;
  887. dlg = dlg_get_by_iuid(iuid);
  888. if(dlg==NULL)
  889. return;
  890. /* unref by 2: 1 set when adding in tm cb, 1 sent by dlg_get_by_iuid() */
  891. dlg_unref(dlg, 2);
  892. }
  893. /*!
  894. *
  895. */
  896. dlg_cell_t *dlg_lookup_msg_dialog(sip_msg_t *msg, unsigned int *dir)
  897. {
  898. dlg_cell_t *dlg = NULL;
  899. str callid;
  900. str ftag;
  901. str ttag;
  902. unsigned int vdir;
  903. /* Retrieve the current dialog */
  904. dlg = dlg_get_ctx_dialog();
  905. if(dlg!=NULL) {
  906. if(dir) {
  907. if (pre_match_parse(msg, &callid, &ftag, &ttag, 0)<0) {
  908. dlg_release(dlg);
  909. return NULL;
  910. }
  911. if (dlg->tag[DLG_CALLER_LEG].len == ftag.len &&
  912. strncmp(dlg->tag[DLG_CALLER_LEG].s, ftag.s, ftag.len)==0 &&
  913. strncmp(dlg->callid.s, callid.s, callid.len)==0) {
  914. *dir = DLG_DIR_DOWNSTREAM;
  915. } else {
  916. if (ttag.len>0 && dlg->tag[DLG_CALLER_LEG].len == ttag.len &&
  917. strncmp(dlg->tag[DLG_CALLER_LEG].s, ttag.s, ttag.len)==0 &&
  918. strncmp(dlg->callid.s, callid.s, callid.len)==0) {
  919. *dir = DLG_DIR_UPSTREAM;
  920. }
  921. }
  922. }
  923. return dlg;
  924. }
  925. if (pre_match_parse(msg, &callid, &ftag, &ttag, 0)<0)
  926. return NULL;
  927. vdir = DLG_DIR_NONE;
  928. dlg = get_dlg(&callid, &ftag, &ttag, &vdir);
  929. if (dlg==NULL){
  930. LM_DBG("dlg with callid '%.*s' not found\n",
  931. msg->callid->body.len, msg->callid->body.s);
  932. return NULL;
  933. }
  934. if(dir) *dir = vdir;
  935. return dlg;
  936. }
  937. /*!
  938. *
  939. */
  940. dlg_cell_t *dlg_get_msg_dialog(sip_msg_t *msg)
  941. {
  942. return dlg_lookup_msg_dialog(msg, NULL);
  943. }
  944. /*!
  945. * \brief Function that is registered as RR callback for dialog tracking
  946. *
  947. * Function that is registered as RR callback for dialog tracking. It
  948. * sets the appropriate events after the SIP method and run the state
  949. * machine to update the dialog state. It updates then the saved
  950. * dialogs and also the statistics.
  951. * \param req SIP request
  952. * \param route_params record-route parameter
  953. * \param param unused
  954. */
  955. void dlg_onroute(struct sip_msg* req, str *route_params, void *param)
  956. {
  957. dlg_cell_t *dlg;
  958. dlg_iuid_t *iuid;
  959. str val, callid, ftag, ttag;
  960. int h_entry, h_id, new_state, old_state, unref, event, timeout, reset;
  961. unsigned int dir;
  962. int ret = 0;
  963. dlg = dlg_get_ctx_dialog();
  964. if (dlg!=NULL) {
  965. dlg_release(dlg);
  966. return;
  967. }
  968. /* skip initial requests - they may end up here because of the
  969. * preloaded route */
  970. if ( (!req->to && parse_headers(req, HDR_TO_F,0)<0) || !req->to ) {
  971. LM_ERR("bad request or missing TO hdr :-/\n");
  972. return;
  973. }
  974. if ( get_to(req)->tag_value.len==0 )
  975. return;
  976. dlg = 0;
  977. dir = DLG_DIR_NONE;
  978. if ( seq_match_mode!=SEQ_MATCH_NO_ID ) {
  979. if( d_rrb.get_route_param( req, &rr_param, &val)!=0) {
  980. LM_DBG("Route param '%.*s' not found\n", rr_param.len,rr_param.s);
  981. if (seq_match_mode==SEQ_MATCH_STRICT_ID )
  982. return;
  983. } else {
  984. LM_DBG("route param is '%.*s' (len=%d)\n",val.len,val.s,val.len);
  985. if ( parse_dlg_rr_param( val.s, val.s+val.len, &h_entry, &h_id)<0 )
  986. return;
  987. dlg = dlg_lookup(h_entry, h_id);
  988. if (dlg==0) {
  989. LM_WARN("unable to find dialog for %.*s "
  990. "with route param '%.*s' [%u:%u]\n",
  991. req->first_line.u.request.method.len,
  992. req->first_line.u.request.method.s,
  993. val.len,val.s, h_entry, h_id);
  994. if (seq_match_mode==SEQ_MATCH_STRICT_ID )
  995. return;
  996. } else {
  997. if (pre_match_parse( req, &callid, &ftag, &ttag, 1)<0) {
  998. // lookup_dlg has incremented the ref count by 1
  999. dlg_release(dlg);
  1000. return;
  1001. }
  1002. if (match_dialog( dlg, &callid, &ftag, &ttag, &dir )==0) {
  1003. LM_WARN("tight matching failed for %.*s with callid='%.*s'/%d, "
  1004. "ftag='%.*s'/%d, ttag='%.*s'/%d and direction=%d\n",
  1005. req->first_line.u.request.method.len,
  1006. req->first_line.u.request.method.s,
  1007. callid.len, callid.s, callid.len,
  1008. ftag.len, ftag.s, ftag.len,
  1009. ttag.len, ttag.s, ttag.len, dir);
  1010. LM_WARN("dialog identification elements are callid='%.*s'/%d, "
  1011. "caller tag='%.*s'/%d, callee tag='%.*s'/%d\n",
  1012. dlg->callid.len, dlg->callid.s, dlg->callid.len,
  1013. dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
  1014. dlg->tag[DLG_CALLER_LEG].len,
  1015. dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s,
  1016. dlg->tag[DLG_CALLEE_LEG].len);
  1017. // lookup_dlg has incremented the ref count by 1
  1018. dlg_release(dlg);
  1019. // Reset variables in order to do a lookup based on SIP-Elements.
  1020. dlg = 0;
  1021. dir = DLG_DIR_NONE;
  1022. if (seq_match_mode==SEQ_MATCH_STRICT_ID )
  1023. return;
  1024. }
  1025. }
  1026. }
  1027. }
  1028. if (dlg==0) {
  1029. if (pre_match_parse( req, &callid, &ftag, &ttag, 1)<0)
  1030. return;
  1031. /* TODO - try to use the RR dir detection to speed up here the
  1032. * search -bogdan */
  1033. dlg = get_dlg(&callid, &ftag, &ttag, &dir);
  1034. if (dlg==0){
  1035. LM_DBG("Callid '%.*s' not found\n",
  1036. req->callid->body.len, req->callid->body.s);
  1037. return;
  1038. }
  1039. }
  1040. /* set current dialog - re-use ref increment from dlg_get() above */
  1041. set_current_dialog( req, dlg);
  1042. _dlg_ctx.iuid.h_entry = dlg->h_entry;
  1043. _dlg_ctx.iuid.h_id = dlg->h_id;
  1044. if (req->first_line.u.request.method_value != METHOD_ACK) {
  1045. iuid = dlg_get_iuid_shm_clone(dlg);
  1046. if(iuid!=NULL)
  1047. {
  1048. /* register callback for the replies of this request */
  1049. if ( d_tmb.register_tmcb( req, 0, TMCB_RESPONSE_IN|TMCB_ON_FAILURE,
  1050. dlg_onreply, (void*)iuid, dlg_iuid_sfree)<0 ) {
  1051. LM_ERR("failed to register TMCB (3)\n");
  1052. shm_free(iuid);
  1053. }
  1054. iuid = NULL;
  1055. }
  1056. }
  1057. /* run state machine */
  1058. switch ( req->first_line.u.request.method_value ) {
  1059. case METHOD_PRACK:
  1060. event = DLG_EVENT_REQPRACK; break;
  1061. case METHOD_ACK:
  1062. event = DLG_EVENT_REQACK; break;
  1063. case METHOD_BYE:
  1064. event = DLG_EVENT_REQBYE; break;
  1065. default:
  1066. event = DLG_EVENT_REQ;
  1067. }
  1068. next_state_dlg( dlg, event, &old_state, &new_state, &unref);
  1069. CURR_DLG_ID = req->id;
  1070. CURR_DLG_LIFETIME = (unsigned int)(time(0))-dlg->start_ts;
  1071. CURR_DLG_STATUS = new_state;
  1072. dlg_run_event_route(dlg, req, old_state, new_state);
  1073. /* delay deletion of dialog until transaction has died off in order
  1074. * to absorb in-air messages */
  1075. if (new_state==DLG_STATE_DELETED && old_state!=DLG_STATE_DELETED) {
  1076. iuid = dlg_get_iuid_shm_clone(dlg);
  1077. if(iuid!=NULL) {
  1078. if ( d_tmb.register_tmcb(req, NULL, TMCB_DESTROY,
  1079. unref_dlg_from_cb, (void*)iuid, dlg_iuid_sfree)<0 ) {
  1080. LM_ERR("failed to register deletion delay function\n");
  1081. shm_free(iuid);
  1082. } else {
  1083. dlg_ref(dlg, 1);
  1084. }
  1085. }
  1086. }
  1087. if (new_state==DLG_STATE_CONFIRMED && old_state!=DLG_STATE_CONFIRMED)
  1088. dlg_ka_add(dlg);
  1089. /* run actions for the transition */
  1090. if (event==DLG_EVENT_REQBYE && new_state==DLG_STATE_DELETED &&
  1091. old_state!=DLG_STATE_DELETED) {
  1092. LM_DBG("BYE successfully processed\n");
  1093. /* remove from timer */
  1094. ret = remove_dialog_timer(&dlg->tl);
  1095. if (ret < 0) {
  1096. LM_CRIT("unable to unlink the timer on dlg %p [%u:%u] "
  1097. "with clid '%.*s' and tags '%.*s' '%.*s'\n",
  1098. dlg, dlg->h_entry, dlg->h_id,
  1099. dlg->callid.len, dlg->callid.s,
  1100. dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
  1101. dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
  1102. } else if (ret > 0) {
  1103. LM_WARN("inconsitent dlg timer data on dlg %p [%u:%u] "
  1104. "with clid '%.*s' and tags '%.*s' '%.*s'\n",
  1105. dlg, dlg->h_entry, dlg->h_id,
  1106. dlg->callid.len, dlg->callid.s,
  1107. dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
  1108. dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
  1109. } else {
  1110. /* one extra unref due to removal from timer list */
  1111. unref++;
  1112. }
  1113. /* dialog terminated (BYE) */
  1114. dlg_terminated( req, dlg, dir);
  1115. dlg_unref(dlg, unref);
  1116. _dlg_ctx.cpid = my_pid();
  1117. _dlg_ctx.expect_t = 1;
  1118. dlg_set_ctx_iuid(dlg);
  1119. if_update_stat( dlg_enable_stats, active_dlgs, -1);
  1120. goto done;
  1121. }
  1122. if ( (event==DLG_EVENT_REQ || event==DLG_EVENT_REQACK)
  1123. && (new_state==DLG_STATE_CONFIRMED || new_state==DLG_STATE_EARLY)) {
  1124. timeout = get_dlg_timeout(req);
  1125. if (timeout!=default_timeout) {
  1126. dlg->lifetime = timeout;
  1127. }
  1128. reset = !((dlg->iflags & DLG_IFLAG_TIMER_NORESET) || dlg_timeout_noreset);
  1129. if ((new_state!=DLG_STATE_EARLY) && (old_state!=DLG_STATE_CONFIRMED || reset)) {
  1130. if (update_dlg_timer( &dlg->tl, dlg->lifetime )==-1) {
  1131. LM_ERR("failed to update dialog lifetime\n");
  1132. } else {
  1133. dlg->dflags |= DLG_FLAG_CHANGED;
  1134. }
  1135. }
  1136. if(event != DLG_EVENT_REQACK) {
  1137. if(update_cseqs(dlg, req, dir)!=0) {
  1138. LM_ERR("cseqs update failed\n");
  1139. } else {
  1140. dlg->dflags |= DLG_FLAG_CHANGED;
  1141. }
  1142. }
  1143. if(dlg_db_mode==DB_MODE_REALTIME && (dlg->dflags&DLG_FLAG_CHANGED)) {
  1144. update_dialog_dbinfo(dlg);
  1145. }
  1146. if (old_state==DLG_STATE_CONFIRMED_NA) {
  1147. LM_DBG("confirming ACK successfully processed\n");
  1148. /* confirming ACK request */
  1149. run_dlg_callbacks( DLGCB_CONFIRMED, dlg, req, NULL, dir, 0);
  1150. } else {
  1151. LM_DBG("sequential request successfully processed\n");
  1152. /* within dialog request */
  1153. run_dlg_callbacks( DLGCB_REQ_WITHIN, dlg, req, NULL, dir, 0);
  1154. if ( (event!=DLG_EVENT_REQACK) &&
  1155. (dlg->cbs.types)&DLGCB_RESPONSE_WITHIN ) {
  1156. iuid = dlg_get_iuid_shm_clone(dlg);
  1157. if(iuid!=NULL)
  1158. {
  1159. /* register callback for the replies of this request */
  1160. if ( d_tmb.register_tmcb( req, 0, TMCB_RESPONSE_FWDED,
  1161. (dir==DLG_DIR_UPSTREAM)?dlg_seq_down_onreply:
  1162. dlg_seq_up_onreply,
  1163. (void*)iuid, dlg_iuid_sfree)<0 ) {
  1164. LM_ERR("failed to register TMCB (2)\n");
  1165. shm_free(iuid);
  1166. }
  1167. }
  1168. }
  1169. }
  1170. }
  1171. if(new_state==DLG_STATE_CONFIRMED && old_state==DLG_STATE_CONFIRMED_NA){
  1172. dlg->dflags |= DLG_FLAG_CHANGED;
  1173. if(dlg_db_mode == DB_MODE_REALTIME)
  1174. update_dialog_dbinfo(dlg);
  1175. }
  1176. done:
  1177. dlg_release(dlg);
  1178. return;
  1179. }
  1180. /*!
  1181. * \brief Timer function that removes expired dialogs, run timeout route
  1182. * \param tl dialog timer list
  1183. */
  1184. void dlg_ontimeout(struct dlg_tl *tl)
  1185. {
  1186. dlg_cell_t *dlg;
  1187. int new_state, old_state, unref;
  1188. sip_msg_t *fmsg;
  1189. void* timeout_cb = 0;
  1190. /* get the dialog tl payload */
  1191. dlg = ((struct dlg_cell*)((char *)(tl) -
  1192. (unsigned long)(&((struct dlg_cell*)0)->tl)));
  1193. /* mark dialog as expired */
  1194. dlg->dflags |= DLG_FLAG_EXPIRED;
  1195. if(dlg->state==DLG_STATE_CONFIRMED_NA
  1196. || dlg->state==DLG_STATE_CONFIRMED)
  1197. {
  1198. if(dlg->toroute>0 && dlg->toroute<main_rt.entries
  1199. && main_rt.rlist[dlg->toroute]!=NULL)
  1200. {
  1201. fmsg = faked_msg_next();
  1202. if (exec_pre_script_cb(fmsg, REQUEST_CB_TYPE)>0)
  1203. {
  1204. dlg_ref(dlg, 1);
  1205. dlg_set_ctx_iuid(dlg);
  1206. LM_DBG("executing route %d on timeout\n", dlg->toroute);
  1207. set_route_type(REQUEST_ROUTE);
  1208. run_top_route(main_rt.rlist[dlg->toroute], fmsg, 0);
  1209. dlg_reset_ctx_iuid();
  1210. exec_post_script_cb(fmsg, REQUEST_CB_TYPE);
  1211. dlg_unref(dlg, 1);
  1212. }
  1213. }
  1214. if(dlg->iflags&DLG_IFLAG_TIMEOUTBYE)
  1215. {
  1216. /* set the dialog context so that it's available in
  1217. * tm:local-request event route */
  1218. dlg_set_ctx_iuid(dlg);
  1219. if(dlg_bye_all(dlg, NULL)<0)
  1220. dlg_unref(dlg, 1);
  1221. dlg_reset_ctx_iuid();
  1222. /* run event route for end of dlg */
  1223. dlg_run_event_route(dlg, NULL, dlg->state, DLG_STATE_DELETED);
  1224. dlg_unref(dlg, 1);
  1225. if_update_stat(dlg_enable_stats, expired_dlgs, 1);
  1226. return;
  1227. }
  1228. }
  1229. next_state_dlg( dlg, DLG_EVENT_REQBYE, &old_state, &new_state, &unref);
  1230. /* used for computing duration for timed out acknowledged dialog */
  1231. if (DLG_STATE_CONFIRMED == old_state) {
  1232. timeout_cb = (void *)CONFIRMED_DIALOG_STATE;
  1233. }
  1234. dlg_run_event_route(dlg, NULL, old_state, new_state);
  1235. if (new_state==DLG_STATE_DELETED && old_state!=DLG_STATE_DELETED) {
  1236. LM_WARN("timeout for dlg with CallID '%.*s' and tags '%.*s' '%.*s'\n",
  1237. dlg->callid.len, dlg->callid.s,
  1238. dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
  1239. dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
  1240. /* dialog timeout */
  1241. run_dlg_callbacks( DLGCB_EXPIRED, dlg, NULL, NULL, DLG_DIR_NONE, timeout_cb);
  1242. dlg_unref(dlg, unref+1);
  1243. if_update_stat( dlg_enable_stats, expired_dlgs, 1);
  1244. if_update_stat( dlg_enable_stats, active_dlgs, -1);
  1245. } else {
  1246. dlg_unref(dlg, 1);
  1247. }
  1248. return;
  1249. }
  1250. /*!
  1251. * \brief Function that returns the dialog lifetime as pseudo-variable
  1252. * \param msg SIP message
  1253. * \param param pseudo-variable parameter
  1254. * \param res pseudo-variable result
  1255. * \return 0 on success, -1 on failure
  1256. */
  1257. int pv_get_dlg_lifetime(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
  1258. {
  1259. int l = 0;
  1260. char *ch = NULL;
  1261. if(msg==NULL || res==NULL)
  1262. return -1;
  1263. if (CURR_DLG_ID!=msg->id)
  1264. return pv_get_null( msg, param, res);
  1265. res->ri = CURR_DLG_LIFETIME;
  1266. ch = int2str( (unsigned long)res->ri, &l);
  1267. res->rs.s = ch;
  1268. res->rs.len = l;
  1269. res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
  1270. return 0;
  1271. }
  1272. /*!
  1273. * \brief Function that returns the dialog state as pseudo-variable
  1274. * \param msg SIP message
  1275. * \param param pseudo-variable parameter
  1276. * \param res pseudo-variable result
  1277. * \return 0 on success, -1 on failure
  1278. */
  1279. int pv_get_dlg_status(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
  1280. {
  1281. int l = 0;
  1282. char *ch = NULL;
  1283. if(msg==NULL || res==NULL)
  1284. return -1;
  1285. if (CURR_DLG_ID!=msg->id)
  1286. return pv_get_null( msg, param, res);
  1287. res->ri = CURR_DLG_STATUS;
  1288. ch = int2str( (unsigned long)res->ri, &l);
  1289. res->rs.s = ch;
  1290. res->rs.len = l;
  1291. res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
  1292. return 0;
  1293. }
  1294. /*!
  1295. * \brief Execute event routes based on new state
  1296. *
  1297. */
  1298. void dlg_run_event_route(dlg_cell_t *dlg, sip_msg_t *msg, int ostate, int nstate)
  1299. {
  1300. sip_msg_t *fmsg;
  1301. int rt;
  1302. int bkroute;
  1303. if(dlg==NULL)
  1304. return;
  1305. if(ostate==nstate)
  1306. return;
  1307. rt = -1;
  1308. if(nstate==DLG_STATE_CONFIRMED_NA) {
  1309. rt = dlg_event_rt[DLG_EVENTRT_START];
  1310. } else if(nstate==DLG_STATE_DELETED) {
  1311. if(ostate==DLG_STATE_CONFIRMED || ostate==DLG_STATE_CONFIRMED_NA)
  1312. rt = dlg_event_rt[DLG_EVENTRT_END];
  1313. else if(ostate==DLG_STATE_UNCONFIRMED || ostate==DLG_STATE_EARLY)
  1314. rt = dlg_event_rt[DLG_EVENTRT_FAILED];
  1315. }
  1316. if(rt==-1 || event_rt.rlist[rt]==NULL)
  1317. return;
  1318. if(msg==NULL)
  1319. fmsg = faked_msg_next();
  1320. else
  1321. fmsg = msg;
  1322. if (exec_pre_script_cb(fmsg, LOCAL_CB_TYPE)>0)
  1323. {
  1324. dlg_ref(dlg, 1);
  1325. dlg_set_ctx_iuid(dlg);
  1326. LM_DBG("executing event_route %d on state %d\n", rt, nstate);
  1327. bkroute = get_route_type();
  1328. set_route_type(LOCAL_ROUTE);
  1329. run_top_route(event_rt.rlist[rt], fmsg, 0);
  1330. dlg_reset_ctx_iuid();
  1331. exec_post_script_cb(fmsg, LOCAL_CB_TYPE);
  1332. dlg_unref(dlg, 1);
  1333. set_route_type(bkroute);
  1334. }
  1335. }
  1336. int dlg_manage(sip_msg_t *msg)
  1337. {
  1338. str tag;
  1339. int backup_mode;
  1340. dlg_cell_t *dlg = NULL;
  1341. tm_cell_t *t = NULL;
  1342. if( (msg->to==NULL && parse_headers(msg, HDR_TO_F,0)<0) || msg->to==NULL )
  1343. {
  1344. LM_ERR("bad TO header\n");
  1345. return -1;
  1346. }
  1347. tag = get_to(msg)->tag_value;
  1348. if(tag.s!=0 && tag.len!=0)
  1349. {
  1350. backup_mode = seq_match_mode;
  1351. seq_match_mode = SEQ_MATCH_NO_ID;
  1352. dlg_onroute(msg, NULL, NULL);
  1353. seq_match_mode = backup_mode;
  1354. } else {
  1355. t = d_tmb.t_gett();
  1356. if(t==T_UNDEFINED)
  1357. t = NULL;
  1358. if(dlg_new_dialog(msg, t, initial_cbs_inscript)!=0)
  1359. return -1;
  1360. dlg = dlg_get_ctx_dialog();
  1361. if(dlg==NULL)
  1362. return -1;
  1363. if(t!=NULL) {
  1364. dlg_set_tm_callbacks(t, msg, dlg, spiral_detected);
  1365. _dlg_ctx.t = 1;
  1366. LM_DBG("dialog created on existing transaction\n");
  1367. } else {
  1368. LM_DBG("dialog created before transaction\n");
  1369. }
  1370. dlg_release(dlg);
  1371. }
  1372. return 1;
  1373. }