dlg_req_within.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2007 Voice System SRL
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * History:
  23. * --------
  24. * 2007-07-10 initial version (ancuta)
  25. * 2008-04-04 added direction reporting in dlg callbacks (bogdan)
  26. * 2011-10 added support for early dialog termination (jason)
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "../../dprint.h"
  31. #include "../../ut.h"
  32. #include "../../lib/srdb1/db.h"
  33. #include "../../dprint.h"
  34. #include "../../config.h"
  35. #include "../../socket_info.h"
  36. #include "../../modules/tm/dlg.h"
  37. #include "../../modules/tm/tm_load.h"
  38. #include "../../lib/kmi/tree.h"
  39. #include "../../lib/kcore/kstats_wrapper.h"
  40. #include "../../locking.h"
  41. #include "dlg_timer.h"
  42. #include "dlg_hash.h"
  43. #include "dlg_req_within.h"
  44. #include "dlg_db_handler.h"
  45. #define MAX_FWD_HDR "Max-Forwards: " MAX_FWD CRLF
  46. #define MAX_FWD_HDR_LEN (sizeof(MAX_FWD_HDR) - 1)
  47. extern str dlg_extra_hdrs;
  48. extern int dlg_db_mode;
  49. int free_tm_dlg(dlg_t *td) {
  50. if (td) {
  51. if (td->route_set)
  52. free_rr(&td->route_set);
  53. pkg_free(td);
  54. }
  55. return 0;
  56. }
  57. dlg_t * build_dlg_t(struct dlg_cell * cell, int dir) {
  58. dlg_t* td = NULL;
  59. str cseq;
  60. unsigned int loc_seq;
  61. str route_set;
  62. str contact;
  63. struct dlg_cell_out *dlg_out = 0;
  64. struct dlg_entry_out* dlg_entry_out = 0;
  65. /* if trying to send by to callee we need to get the corresponding dlg_out cell */
  66. lock_get(cell->dlg_out_entries_lock);
  67. dlg_entry_out = &cell->dlg_entry_out;
  68. dlg_out = dlg_entry_out->first;
  69. //must be concurrent call - lets choose - TODO - ie. check if there is more
  70. if (!dlg_out) {
  71. LM_ERR("Trying to send BYE for dialog with no callee leg\n");
  72. lock_release(cell->dlg_out_entries_lock);
  73. return NULL;
  74. }
  75. td = (dlg_t*) pkg_malloc(sizeof (dlg_t));
  76. if (!td) {
  77. LM_ERR("out of pkg memory\n");
  78. lock_release(cell->dlg_out_entries_lock);
  79. return NULL;
  80. }
  81. memset(td, 0, sizeof (dlg_t));
  82. if (dir == DLG_CALLER_LEG) {
  83. cseq = cell->first_req_cseq;
  84. route_set = cell->caller_route_set;
  85. contact = cell->caller_contact;
  86. td->rem_uri = cell->from_uri;
  87. td->loc_uri = dlg_out->to_uri;
  88. td->id.rem_tag = cell->from_tag;
  89. td->id.loc_tag = dlg_out->to_tag;
  90. td->send_sock = cell->caller_bind_addr;
  91. } else {
  92. cseq = dlg_out->callee_cseq;
  93. route_set = dlg_out->callee_route_set;
  94. contact = dlg_out->callee_contact;
  95. td->rem_uri = dlg_out->to_uri;
  96. td->loc_uri = cell->from_uri;
  97. td->id.rem_tag = dlg_out->to_tag;
  98. td->id.loc_tag = cell->from_tag;
  99. td->send_sock = dlg_out->callee_bind_addr;
  100. }
  101. if (str2int(&cseq, &loc_seq) != 0) {
  102. LM_ERR("invalid cseq\n");
  103. goto error;
  104. }
  105. /*we don not increase here the cseq as this will be done by TM*/
  106. td->loc_seq.value = loc_seq;
  107. td->loc_seq.is_set = 1;
  108. /*route set*/
  109. if (route_set.s && route_set.len) {
  110. if (parse_rr_body(route_set.s, route_set.len, &td->route_set) != 0) {
  111. LM_ERR("failed to parse route set\n");
  112. goto error;
  113. }
  114. }
  115. if (contact.s == 0 || contact.len == 0) {
  116. LM_ERR("no contact available\n");
  117. goto error;
  118. }
  119. td->id.call_id = cell->callid;
  120. td->rem_target = contact;
  121. td->state = DLG_CONFIRMED;
  122. lock_release(cell->dlg_out_entries_lock);
  123. return td;
  124. error:
  125. lock_release(cell->dlg_out_entries_lock);
  126. free_tm_dlg(td);
  127. return NULL;
  128. }
  129. /*callback function to handle responses to the BYE request */
  130. void bye_reply_cb(struct cell* t, int type, struct tmcb_params* ps) {
  131. struct dlg_cell* dlg;
  132. int event, old_state, new_state, unref, ret;
  133. struct dlg_cell_out *dlg_out = 0;
  134. if (ps->param == NULL || *ps->param == NULL) {
  135. LM_ERR("invalid parameter\n");
  136. return;
  137. }
  138. if (ps->code < 200) {
  139. LM_DBG("receiving a provisional reply\n");
  140. return;
  141. }
  142. LM_DBG("receiving a final reply %d\n", ps->code);
  143. dlg = (struct dlg_cell *) (*(ps->param));
  144. event = DLG_EVENT_REQBYE;
  145. //get the corresponding dlg out structure for this REQ
  146. struct dlg_entry_out *dlg_entry_out = &dlg->dlg_entry_out;
  147. lock_get(dlg->dlg_out_entries_lock);
  148. dlg_out = dlg_entry_out->first; //TODO check for concurrent call
  149. if (!dlg_out)
  150. return;
  151. next_state_dlg(dlg, event, &old_state, &new_state, &unref, &dlg_out->to_tag);
  152. lock_release(dlg->dlg_out_entries_lock);
  153. if (new_state == DLG_STATE_DELETED && old_state != DLG_STATE_DELETED) {
  154. LM_DBG("removing dialog with h_entry %u and h_id %u\n",
  155. dlg->h_entry, dlg->h_id);
  156. /* remove from timer */
  157. ret = remove_dialog_timer(&dlg->tl);
  158. if (ret < 0) {
  159. LM_CRIT("unable to unlink the timer on dlg %p [%u:%u] "
  160. "with clid '%.*s'\n",
  161. dlg, dlg->h_entry, dlg->h_id,
  162. dlg->callid.len, dlg->callid.s);
  163. } else if (ret > 0) {
  164. LM_WARN("inconsitent dlg timer data on dlg %p [%u:%u] "
  165. "with clid '%.*s'\n",
  166. dlg, dlg->h_entry, dlg->h_id,
  167. dlg->callid.len, dlg->callid.s);
  168. } else {
  169. unref++;
  170. }
  171. /* dialog terminated (BYE) */
  172. run_dlg_callbacks(DLGCB_TERMINATED, dlg, ps->req, ps->rpl, DLG_DIR_NONE, 0);
  173. /* derefering the dialog */
  174. unref_dlg(dlg, unref + 1);
  175. }
  176. if (new_state == DLG_STATE_DELETED && old_state == DLG_STATE_DELETED) {
  177. /* trash the dialog from DB and memory */
  178. if (dlg_db_mode)
  179. remove_dialog_in_from_db(dlg);
  180. /* force delete from mem */
  181. unref_dlg(dlg, 1);
  182. }
  183. }
  184. static inline int build_extra_hdr(struct dlg_cell * cell, str *extra_hdrs,
  185. str *str_hdr) {
  186. char *p;
  187. str_hdr->len = MAX_FWD_HDR_LEN + dlg_extra_hdrs.len;
  188. if (extra_hdrs && extra_hdrs->len > 0)
  189. str_hdr->len += extra_hdrs->len;
  190. str_hdr->s = (char*) pkg_malloc(str_hdr->len * sizeof (char));
  191. if (!str_hdr->s) {
  192. LM_ERR("out of pkg memory\n");
  193. goto error;
  194. }
  195. memcpy(str_hdr->s, MAX_FWD_HDR, MAX_FWD_HDR_LEN);
  196. p = str_hdr->s + MAX_FWD_HDR_LEN;
  197. if (dlg_extra_hdrs.len) {
  198. memcpy(p, dlg_extra_hdrs.s, dlg_extra_hdrs.len);
  199. p += dlg_extra_hdrs.len;
  200. }
  201. if (extra_hdrs && extra_hdrs->len > 0)
  202. memcpy(p, extra_hdrs->s, extra_hdrs->len);
  203. return 0;
  204. error:
  205. return -1;
  206. }
  207. /* cell- pointer to a struct dlg_cell
  208. * dir- direction: the request will be sent to:
  209. * DLG_CALLER_LEG (0): caller
  210. * DLG_CALLEE_LEG (1): callee
  211. */
  212. static inline int send_bye(struct dlg_cell * cell, int dir, str *hdrs) {
  213. uac_req_t uac_r;
  214. dlg_t* dialog_info;
  215. str met = {"BYE", 3};
  216. int result;
  217. /* do not send BYE request for non-confirmed dialogs (not supported) */
  218. if (cell->state != DLG_STATE_CONFIRMED) {
  219. LM_ERR("terminating only 1 side of non-confirmed dialogs not supported by this function\n");
  220. return -1;
  221. }
  222. /*verify direction*/
  223. if ((dialog_info = build_dlg_t(cell, dir)) == 0) {
  224. LM_ERR("failed to create dlg_t\n");
  225. goto err;
  226. }
  227. LM_DBG("sending BYE to %s\n", (dir == DLG_CALLER_LEG) ? "caller" : "callee");
  228. ref_dlg(cell, 1);
  229. memset(&uac_r, '\0', sizeof (uac_req_t));
  230. set_uac_req(&uac_r, &met, hdrs, NULL, dialog_info, TMCB_LOCAL_COMPLETED,
  231. bye_reply_cb, (void*) cell);
  232. result = d_tmb.t_request_within(&uac_r);
  233. if (result < 0) {
  234. LM_ERR("failed to send the BYE request\n");
  235. goto err1;
  236. }
  237. free_tm_dlg(dialog_info);
  238. LM_DBG("BYE sent to %s\n", (dir == 0) ? "caller" : "callee");
  239. return 0;
  240. err1:
  241. unref_dlg(cell, 1);
  242. err:
  243. if (dialog_info)
  244. free_tm_dlg(dialog_info);
  245. return -1;
  246. }
  247. /*static void early_transaction_destroyed(struct cell* t, int type, struct tmcb_params *param) {
  248. struct dlg_cell *dlg = (struct dlg_cell *) (*param->param);
  249. if (!dlg)
  250. return;
  251. LM_DBG("Early transaction destroyed\n");
  252. }*/
  253. /* side =
  254. * 0: caller
  255. * 1: callee
  256. * 2: all
  257. */
  258. int dlg_terminate(struct dlg_cell *dlg, struct sip_msg *msg, str *reason, int side, str *extra_hdrs) {
  259. struct cell* t;
  260. str default_reason = {"call failed", 11};
  261. int cfg_cmd = 0;
  262. str default_extra_headers = {0,0};
  263. if (!dlg) {
  264. LM_ERR("calling end_dialog with NULL pointer dlg\n");
  265. return -1;
  266. }
  267. if (!extra_hdrs)
  268. extra_hdrs = &default_extra_headers;
  269. if (msg) {
  270. //assume called from cfg command -> dlg_terminate, as opposed to internal API or mi interface
  271. cfg_cmd = 1;
  272. }
  273. if (!reason || reason->len <= 0 || !reason->s) {
  274. reason = &default_reason;
  275. }
  276. if (dlg->state != DLG_STATE_CONFIRMED) {
  277. if (side != 2) {
  278. LM_ERR("can't terminate only 1 side of an early dialog\n");
  279. return -1;
  280. }
  281. if (dlg->transaction) {
  282. LM_DBG("terminating early dialog with %d outbound forks\n",
  283. dlg->transaction->nr_of_outgoings);
  284. t = dlg->transaction;
  285. if (t && t!=(void*) -1 && t->uas.request) {
  286. if (t->method.len!=6 || t->method.s[0]!='I' || t->method.s[1]!='N' || t->method.s[2]!='V')
  287. {
  288. //well this is the transaction of a subsequent request within the dialog
  289. //and the dialog is not confirmed yet, so its a PRACK or an UPDATE
  290. //could also be an options, but the important thing is how am i going to get
  291. //the transaction of the invite, that is the one i have to cancel
  292. LM_WARN("this is not my transaction so where am i?\n");
  293. return 1; //TODO - need to check why we got in here once before? this crashed on t_reply as t seemed invalid
  294. }
  295. //TODO: here we are assuming none of the CALLEE's have sent a 200, in
  296. //which case we would have to send an ACK, BYE
  297. //so right now - we are sending 488 to caller and CANCEL's to all CALLEEs
  298. LM_DBG("tearing down dialog in EARLY state - no clients responded > 199\n");
  299. if (cfg_cmd) {
  300. d_tmb.t_reply(msg,488,reason->s);
  301. d_tmb.t_release(msg);
  302. } else {
  303. d_tmb.t_reply(t->uas.request,488,reason->s);
  304. d_tmb.t_release(t->uas.request);
  305. }
  306. }
  307. } else {
  308. LM_WARN("can't terminate early dialog without a transaction\n");
  309. return -1;
  310. }
  311. } else {
  312. LM_DBG("terminating confirmed dialog\n");
  313. if (side == DLG_CALLER_LEG /* 0 */ || side == DLG_CALLEE_LEG /* 1 */) {
  314. if (dlg_bye(dlg, (extra_hdrs->len > 0) ? extra_hdrs : NULL, side) < 0)
  315. return -1;
  316. } else {
  317. if (dlg_bye_all(dlg, (extra_hdrs->len > 0) ? extra_hdrs : NULL) < 0)
  318. return -1;
  319. }
  320. }
  321. return 1;
  322. }
  323. /*parameters from MI: callid, from tag, to tag*/
  324. /* TODO: add reason parameter to mi interface */
  325. struct mi_root * mi_terminate_dlg(struct mi_root *cmd_tree, void *param) {
  326. struct mi_node* node;
  327. struct dlg_cell * dlg = NULL;
  328. str mi_extra_hdrs = {NULL, 0};
  329. int status, msg_len;
  330. char *msg;
  331. str callid = {NULL, 0};
  332. str ftag = {NULL, 0};
  333. str ttag = {NULL, 0};
  334. if (d_table == NULL)
  335. goto end;
  336. node = cmd_tree->node.kids;
  337. if (node == NULL || node->next == NULL || node->next->next == NULL)
  338. return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  339. if (!node->value.s || !node->value.len) {
  340. goto error;
  341. } else {
  342. callid = node->value;
  343. }
  344. node = node->next;
  345. if (!node->value.s || !node->value.len) {
  346. goto error;
  347. } else {
  348. ftag = node->value;
  349. }
  350. node = node->next;
  351. if (!node->value.s || !node->value.len) {
  352. goto error;
  353. } else {
  354. ttag = node->value;
  355. }
  356. if (node->next) {
  357. node = node->next;
  358. if (node->value.len && node->value.s)
  359. mi_extra_hdrs = node->value;
  360. }
  361. unsigned int dir = DLG_DIR_NONE;
  362. LM_DBG("Looking for callid [%.*s]\n", callid.len, callid.s);
  363. dlg = get_dlg(&callid, &ftag, &ttag, &dir); //increments ref count!
  364. if (dlg) {
  365. LM_DBG("Found dialog to terminate and it is in state [%i]\n", dlg->state);
  366. if (dlg_terminate(dlg, 0, NULL/*reson*/, /* all sides of a dialog*/ 2, &mi_extra_hdrs) < 0) {
  367. status = 500;
  368. msg = MI_DLG_OPERATION_ERR;
  369. msg_len = MI_DLG_OPERATION_ERR_LEN;
  370. } else {
  371. status = 200;
  372. msg = MI_OK_S;
  373. msg_len = MI_OK_LEN;
  374. }
  375. unref_dlg(dlg, 1);
  376. return init_mi_tree(status, msg, msg_len);
  377. }
  378. end:
  379. return init_mi_tree(404, MI_DIALOG_NOT_FOUND, MI_DIALOG_NOT_FOUND_LEN);
  380. error:
  381. return init_mi_tree(400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  382. }
  383. int dlg_bye(struct dlg_cell *dlg, str *hdrs, int side) {
  384. str all_hdrs = {0, 0};
  385. int ret;
  386. if (side == DLG_CALLER_LEG) {
  387. if (dlg->dflags & DLG_FLAG_CALLERBYE)
  388. return -1;
  389. dlg->dflags |= DLG_FLAG_CALLERBYE;
  390. } else {
  391. if (dlg->dflags & DLG_FLAG_CALLEEBYE)
  392. return -1;
  393. dlg->dflags |= DLG_FLAG_CALLEEBYE;
  394. }
  395. if ((build_extra_hdr(dlg, hdrs, &all_hdrs)) != 0) {
  396. LM_ERR("failed to build dlg headers\n");
  397. return -1;
  398. }
  399. ret = send_bye(dlg, side, &all_hdrs);
  400. pkg_free(all_hdrs.s);
  401. return ret;
  402. }
  403. /* Wrapper for terminating dialog from API - from other modules */
  404. int w_api_terminate_dlg(str *callid, str *ftag, str *ttag, str *hdrs, str* reason) {
  405. struct dlg_cell *dlg;
  406. unsigned int dir = DLG_DIR_NONE;
  407. dlg = get_dlg(callid, ftag, ttag, &dir); //increments ref count!
  408. if (!dlg) {
  409. LM_ERR("Asked to tear down non existent dialog\n");
  410. return -1;
  411. }
  412. unref_dlg(dlg, 1);
  413. return dlg_terminate(dlg, NULL, NULL/*reason*/, 2, hdrs);
  414. }
  415. int dlg_bye_all(struct dlg_cell *dlg, str *hdrs) {
  416. str all_hdrs = {0, 0};
  417. int ret;
  418. if ((build_extra_hdr(dlg, hdrs, &all_hdrs)) != 0) {
  419. LM_ERR("failed to build dlg headers\n");
  420. return -1;
  421. }
  422. ret = send_bye(dlg, DLG_CALLER_LEG, &all_hdrs);
  423. ret |= send_bye(dlg, DLG_CALLEE_LEG, &all_hdrs);
  424. pkg_free(all_hdrs.s);
  425. return ret;
  426. }
  427. /* Wrapper for terminating dialog from API - from other modules */
  428. int w_api_lookup_terminate_dlg(unsigned int h_entry, unsigned int h_id, str *hdrs) {
  429. struct dlg_cell *dlg;
  430. dlg = lookup_dlg(h_entry, h_id); //increments ref count!
  431. if (!dlg) {
  432. LM_ERR("Asked to tear down non existent dialog\n");
  433. return -1;
  434. }
  435. unref_dlg(dlg, 1);
  436. return dlg_terminate(dlg, NULL, NULL/*reason*/, 2, hdrs);
  437. }