1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
/*
* $Id$
*
* Copyright (C) 2007 Voice System SRL
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* --------
* 2007-07-10 initial version (ancuta)
* 2008-04-04 added direction reporting in dlg callbacks (bogdan)
*/
#include <stdlib.h>
#include <string.h>
#include "../../dprint.h"
#include "../../ut.h"
#include "../../lib/srdb1/db.h"
#include "../../dprint.h"
#include "../../config.h"
#include "../../socket_info.h"
#include "../../dset.h"
#include "../../modules/tm/dlg.h"
#include "../../modules/tm/tm_load.h"
#include "../../lib/kmi/tree.h"
#include "../../lib/kcore/kstats_wrapper.h"
#include "dlg_timer.h"
#include "dlg_hash.h"
#include "dlg_handlers.h"
#include "dlg_req_within.h"
#include "dlg_db_handler.h"
#define MAX_FWD_HDR "Max-Forwards: " MAX_FWD CRLF
#define MAX_FWD_HDR_LEN (sizeof(MAX_FWD_HDR) - 1)
extern str dlg_extra_hdrs;
int free_tm_dlg(dlg_t *td)
{
if(td)
{
if(td->route_set)
free_rr(&td->route_set);
pkg_free(td);
}
return 0;
}
dlg_t * build_dlg_t(struct dlg_cell * cell, int dir){
dlg_t* td = NULL;
str cseq;
unsigned int loc_seq;
char nbuf[MAX_URI_SIZE];
char dbuf[80];
str nuri;
str duri;
size_t sz;
char *p;
/*remote target--- Request URI*/
if(cell->contact[dir].s==0 || cell->contact[dir].len==0){
LM_ERR("no contact available\n");
goto error;
}
/*restore alias parameter*/
nuri.s = nbuf;
nuri.len = MAX_URI_SIZE;
duri.s = dbuf;
duri.len = 80;
if(uri_restore_rcv_alias(&cell->contact[dir], &nuri, &duri)<0) {
nuri.len = 0;
duri.len = 0;
}
if(nuri.len>0 && duri.len>0) {
sz = sizeof(dlg_t) + (nuri.len+duri.len+2)*sizeof(char);
} else {
sz = sizeof(dlg_t);
}
td = (dlg_t*)pkg_malloc(sz);
if(!td){
LM_ERR("out of pkg memory\n");
return NULL;
}
memset(td, 0, sz);
/*local sequence number*/
cseq = (dir == DLG_CALLER_LEG) ? cell->cseq[DLG_CALLEE_LEG]:
cell->cseq[DLG_CALLER_LEG];
if(str2int(&cseq, &loc_seq) != 0){
LM_ERR("invalid cseq\n");
goto error;
}
/*we don not increase here the cseq as this will be done by TM*/
td->loc_seq.value = loc_seq;
td->loc_seq.is_set = 1;
/*route set*/
if( cell->route_set[dir].s && cell->route_set[dir].len){
if( parse_rr_body(cell->route_set[dir].s, cell->route_set[dir].len,
&td->route_set) !=0){
LM_ERR("failed to parse route set\n");
goto error;
}
}
if(nuri.len>0 && duri.len>0) {
/* req uri */
p = (char*)td + sizeof(dlg_t);
strncpy(p, nuri.s, nuri.len);
p[nuri.len] = '\0';
td->rem_target.s = p;
td->rem_target.len = nuri.len;
/* dst uri */
p += nuri.len + 1;
strncpy(p, duri.s, duri.len);
p[duri.len] = '\0';
td->dst_uri.s = p;
td->dst_uri.len = duri.len;
} else {
td->rem_target = cell->contact[dir];
}
td->rem_uri = (dir == DLG_CALLER_LEG)? cell->from_uri: cell->to_uri;
td->loc_uri = (dir == DLG_CALLER_LEG)? cell->to_uri: cell->from_uri;
td->id.call_id = cell->callid;
td->id.rem_tag = cell->tag[dir];
td->id.loc_tag = (dir == DLG_CALLER_LEG) ? cell->tag[DLG_CALLEE_LEG]:
cell->tag[DLG_CALLER_LEG];
td->state= DLG_CONFIRMED;
td->send_sock = cell->bind_addr[dir];
return td;
error:
free_tm_dlg(td);
return NULL;
}
/* callback function to handle responses to the BYE request */
void bye_reply_cb(struct cell* t, int type, struct tmcb_params* ps){
struct dlg_cell* dlg;
int event, old_state, new_state, unref, ret;
dlg_iuid_t *iuid = NULL;
if(ps->param == NULL || *ps->param == NULL){
LM_ERR("invalid parameter\n");
return;
}
if(ps->code < 200){
LM_DBG("receiving a provisional reply\n");
return;
}
LM_DBG("receiving a final reply %d\n",ps->code);
iuid = (dlg_iuid_t*)(*ps->param);
dlg = dlg_get_by_iuid(iuid);
if(dlg==0)
return;
event = DLG_EVENT_REQBYE;
next_state_dlg(dlg, event, &old_state, &new_state, &unref);
if(new_state == DLG_STATE_DELETED && old_state != DLG_STATE_DELETED){
LM_DBG("removing dialog with h_entry %u and h_id %u\n",
dlg->h_entry, dlg->h_id);
/* remove from timer */
ret = remove_dialog_timer(&dlg->tl);
if (ret < 0) {
LM_CRIT("unable to unlink the timer on dlg %p [%u:%u] "
"with clid '%.*s' and tags '%.*s' '%.*s'\n",
dlg, dlg->h_entry, dlg->h_id,
dlg->callid.len, dlg->callid.s,
dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
} else if (ret > 0) {
LM_WARN("inconsitent dlg timer data on dlg %p [%u:%u] "
"with clid '%.*s' and tags '%.*s' '%.*s'\n",
dlg, dlg->h_entry, dlg->h_id,
dlg->callid.len, dlg->callid.s,
dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
} else {
unref++;
}
/* dialog terminated (BYE) */
run_dlg_callbacks( DLGCB_TERMINATED, dlg, ps->req, ps->rpl, DLG_DIR_NONE, 0);
LM_DBG("first final reply\n");
/* derefering the dialog */
dlg_unref(dlg, unref+1);
if_update_stat( dlg_enable_stats, active_dlgs, -1);
}
if(new_state == DLG_STATE_DELETED && old_state == DLG_STATE_DELETED ) {
/* trash the dialog from DB and memory */
LM_DBG("second final reply\n");
/* delete the dialog from DB */
if (dlg_db_mode)
remove_dialog_from_db(dlg);
/* force delete from mem */
dlg_unref(dlg, 1);
}
dlg_iuid_sfree(iuid);
}
/* callback function to handle responses to the keep-alive request */
void dlg_ka_cb(struct cell* t, int type, struct tmcb_params* ps){
dlg_cell_t* dlg;
dlg_iuid_t *iuid = NULL;
if(ps->param == NULL || *ps->param == NULL) {
LM_ERR("invalid parameter\n");
return;
}
if(ps->code < 200) {
LM_DBG("receiving a provisional reply\n");
return;
}
LM_DBG("receiving a final reply %d\n",ps->code);
iuid = (dlg_iuid_t*)(*ps->param);
dlg = dlg_get_by_iuid(iuid);
if(dlg==0) {
dlg_iuid_sfree(iuid);
return;
}
if(ps->code==408 || ps->code==481) {
if(update_dlg_timer(&dlg->tl, 10)<0) {
LM_ERR("failed to update dialog lifetime\n");
goto done;
}
dlg->lifetime = 10;
dlg->dflags |= DLG_FLAG_CHANGED;
}
done:
dlg_unref(dlg, 1);
dlg_iuid_sfree(iuid);
}
static inline int build_extra_hdr(struct dlg_cell * cell, str *extra_hdrs,
str *str_hdr)
{
char *p;
str_hdr->len = MAX_FWD_HDR_LEN + dlg_extra_hdrs.len;
if(extra_hdrs && extra_hdrs->len>0)
str_hdr->len += extra_hdrs->len;
str_hdr->s = (char*)pkg_malloc( str_hdr->len * sizeof(char) );
if(!str_hdr->s){
LM_ERR("out of pkg memory\n");
goto error;
}
memcpy(str_hdr->s , MAX_FWD_HDR, MAX_FWD_HDR_LEN );
p = str_hdr->s + MAX_FWD_HDR_LEN;
if (dlg_extra_hdrs.len) {
memcpy( p, dlg_extra_hdrs.s, dlg_extra_hdrs.len);
p += dlg_extra_hdrs.len;
}
if (extra_hdrs && extra_hdrs->len>0)
memcpy( p, extra_hdrs->s, extra_hdrs->len);
return 0;
error:
return -1;
}
/* cell- pointer to a struct dlg_cell
* dir- direction: the request will be sent to:
* DLG_CALLER_LEG (0): caller
* DLG_CALLEE_LEG (1): callee
*/
static inline int send_bye(struct dlg_cell * cell, int dir, str *hdrs)
{
uac_req_t uac_r;
dlg_t* dialog_info;
str met = {"BYE", 3};
int result;
dlg_iuid_t *iuid = NULL;
/* do not send BYE request for non-confirmed dialogs (not supported) */
if (cell->state != DLG_STATE_CONFIRMED_NA && cell->state != DLG_STATE_CONFIRMED) {
LM_ERR("terminating non-confirmed dialogs not supported\n");
return -1;
}
/*verify direction*/
if ((dialog_info = build_dlg_t(cell, dir)) == 0){
LM_ERR("failed to create dlg_t\n");
goto err;
}
LM_DBG("sending BYE to %s\n", (dir==DLG_CALLER_LEG)?"caller":"callee");
iuid = dlg_get_iuid_shm_clone(cell);
if(iuid==NULL)
{
LM_ERR("failed to create dialog unique id clone\n");
goto err;
}
set_uac_req(&uac_r, &met, hdrs, NULL, dialog_info, TMCB_LOCAL_COMPLETED,
bye_reply_cb, (void*)iuid);
result = d_tmb.t_request_within(&uac_r);
if(result < 0){
LM_ERR("failed to send the BYE request\n");
goto err;
}
free_tm_dlg(dialog_info);
LM_DBG("BYE sent to %s\n", (dir==0)?"caller":"callee");
return 0;
err:
if(dialog_info)
free_tm_dlg(dialog_info);
return -1;
}
/* send keep-alive
* dlg - pointer to a struct dlg_cell
* dir - direction: the request will be sent to:
* DLG_CALLER_LEG (0): caller
* DLG_CALLEE_LEG (1): callee
*/
int dlg_send_ka(dlg_cell_t *dlg, int dir, str *hdrs)
{
uac_req_t uac_r;
dlg_t* di;
str met = {"OPTIONS", 7};
int result;
dlg_iuid_t *iuid = NULL;
/* do not send KA request for non-confirmed dialogs (not supported) */
if (dlg->state != DLG_STATE_CONFIRMED) {
LM_DBG("skipping non-confirmed dialogs\n");
return 0;
}
/* build tm dlg by direction */
if ((di = build_dlg_t(dlg, dir)) == 0){
LM_ERR("failed to create dlg_t\n");
goto err;
}
/* tm increases cseq value, decrease it no to make it invalid
* - dialog is ended on timeout (408) or C/L does not exist (481) */
if(di->loc_seq.value>1)
di->loc_seq.value -= 2;
else
di->loc_seq.value -= 1;
LM_DBG("sending OPTIONS to %s\n", (dir==DLG_CALLER_LEG)?"caller":"callee");
iuid = dlg_get_iuid_shm_clone(dlg);
if(iuid==NULL)
{
LM_ERR("failed to create dialog unique id clone\n");
goto err;
}
set_uac_req(&uac_r, &met, hdrs, NULL, di, TMCB_LOCAL_COMPLETED,
dlg_ka_cb, (void*)iuid);
result = d_tmb.t_request_within(&uac_r);
if(result < 0){
LM_ERR("failed to send the OPTIONS request\n");
goto err;
}
free_tm_dlg(di);
LM_DBG("keep-alive sent to %s\n", (dir==0)?"caller":"callee");
return 0;
err:
if(di)
free_tm_dlg(di);
return -1;
}
/*parameters from MI: h_entry, h_id of the requested dialog*/
struct mi_root * mi_terminate_dlg(struct mi_root *cmd_tree, void *param ){
struct mi_node* node;
unsigned int h_entry, h_id;
struct dlg_cell * dlg = NULL;
str mi_extra_hdrs = {NULL,0};
int status, msg_len;
char *msg;
if( d_table ==NULL)
goto end;
node = cmd_tree->node.kids;
h_entry = h_id = 0;
if (node==NULL || node->next==NULL)
return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
if (!node->value.s|| !node->value.len|| strno2int(&node->value,&h_entry)<0)
goto error;
node = node->next;
if ( !node->value.s || !node->value.len || strno2int(&node->value,&h_id)<0)
goto error;
if (node->next) {
node = node->next;
if (node->value.len && node->value.s)
mi_extra_hdrs = node->value;
}
LM_DBG("h_entry %u h_id %u\n", h_entry, h_id);
dlg = dlg_lookup(h_entry, h_id);
// lookup_dlg has incremented the reference count
if(dlg){
if(dlg_bye_all(dlg,(mi_extra_hdrs.len>0)?&mi_extra_hdrs:NULL)<0) {
status = 500;
msg = MI_DLG_OPERATION_ERR;
msg_len = MI_DLG_OPERATION_ERR_LEN;
} else {
status = 200;
msg = MI_OK_S;
msg_len = MI_OK_LEN;
}
dlg_release(dlg);
return init_mi_tree(status, msg, msg_len);
}
end:
return init_mi_tree(404, MI_DIALOG_NOT_FOUND, MI_DIALOG_NOT_FOUND_LEN);
error:
return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
}
int dlg_bye(struct dlg_cell *dlg, str *hdrs, int side)
{
str all_hdrs = { 0, 0 };
int ret;
if(side==DLG_CALLER_LEG)
{
if(dlg->dflags&DLG_FLAG_CALLERBYE)
return -1;
dlg->dflags |= DLG_FLAG_CALLERBYE;
} else {
if(dlg->dflags&DLG_FLAG_CALLEEBYE)
return -1;
dlg->dflags |= DLG_FLAG_CALLEEBYE;
}
if ((build_extra_hdr(dlg, hdrs, &all_hdrs)) != 0)
{
LM_ERR("failed to build dlg headers\n");
return -1;
}
ret = send_bye(dlg, side, &all_hdrs);
pkg_free(all_hdrs.s);
return ret;
}
int dlg_bye_all(struct dlg_cell *dlg, str *hdrs)
{
str all_hdrs = { 0, 0 };
int ret;
if ((build_extra_hdr(dlg, hdrs, &all_hdrs)) != 0)
{
LM_ERR("failed to build dlg headers\n");
return -1;
}
ret = send_bye(dlg, DLG_CALLER_LEG, &all_hdrs);
ret |= send_bye(dlg, DLG_CALLEE_LEG, &all_hdrs);
pkg_free(all_hdrs.s);
return ret;
}
|