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
|
/*
* Copyright (C) 2014 Daniel-Constantin Mierla (asipto.com)
*
* 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
*/
/*! \file
* \brief TMX :: Pretran
*
* \ingroup tm
* - Module: \ref tm
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../../dprint.h"
#include "../../mem/shm_mem.h"
#include "../../locking.h"
#include "../../hashes.h"
#include "../../config.h"
#include "../../parser/parse_via.h"
#include "../../parser/parse_from.h"
#include "../../route.h"
#include "../../trim.h"
#include "../../pt.h"
#include "tmx_pretran.h"
typedef struct _pretran {
unsigned int hid;
unsigned int linked;
str callid;
str ftag;
str cseqnum;
str cseqmet;
unsigned int cseqmetid;
str vbranch;
str dbuf;
int pid;
struct _pretran *next;
struct _pretran *prev;
} pretran_t;
typedef struct pretran_slot {
pretran_t *plist;
gen_lock_t lock;
} pretran_slot_t;
static pretran_t *_tmx_proc_ptran = NULL;
static pretran_slot_t *_tmx_ptran_table = NULL;
static int _tmx_ptran_size = 0;
/**
*
*/
int tmx_init_pretran_table(void)
{
int n;
int pn;
pn = get_max_procs();
if(pn<=0)
return -1;
if(_tmx_ptran_table!=NULL)
return -1;
/* get the highest power of two less than number of processes */
n = -1;
while (pn >> ++n > 0);
n--;
if(n<=1) n = 2;
if(n>8) n = 8;
_tmx_ptran_size = 1<<n;
_tmx_ptran_table = (pretran_slot_t*)shm_malloc(_tmx_ptran_size*sizeof(pretran_slot_t));
if(_tmx_ptran_table == NULL) {
LM_ERR("not enough shared memory\n");
return -1;
}
memset(_tmx_ptran_table, 0, _tmx_ptran_size*sizeof(pretran_slot_t));
for(n=0; n<_tmx_ptran_size; n++) {
if(lock_init(&_tmx_ptran_table[n].lock)==NULL)
{
LM_ERR("cannot init the lock %d\n", n);
n--;
while(n>=0) {
lock_destroy(&_tmx_ptran_table[n].lock);
n--;
}
shm_free(_tmx_ptran_table);
_tmx_ptran_table = 0;
_tmx_ptran_size = 0;
return -1;
}
}
return 0;
}
/**
*
*/
void tmx_pretran_link_safe(int slotid)
{
if(_tmx_proc_ptran==NULL)
return;
if(_tmx_ptran_table[slotid].plist==NULL) {
_tmx_ptran_table[slotid].plist = _tmx_proc_ptran;
_tmx_proc_ptran->linked = 1;
return;
}
_tmx_proc_ptran->next = _tmx_ptran_table[slotid].plist;
_tmx_ptran_table[slotid].plist->prev = _tmx_proc_ptran;
_tmx_ptran_table[slotid].plist = _tmx_proc_ptran;
_tmx_proc_ptran->linked = 1;
return;
}
/**
*
*/
void tmx_pretran_unlink_safe(int slotid)
{
if(_tmx_proc_ptran==NULL)
return;
if(_tmx_proc_ptran->linked == 0)
return;
if(_tmx_ptran_table[slotid].plist==NULL) {
_tmx_proc_ptran->prev = _tmx_proc_ptran->next = NULL;
_tmx_proc_ptran->linked = 0;
return;
}
if(_tmx_proc_ptran->prev==NULL) {
_tmx_ptran_table[slotid].plist = _tmx_proc_ptran->next;
if(_tmx_ptran_table[slotid].plist!=NULL)
_tmx_ptran_table[slotid].plist->prev = NULL;
} else {
_tmx_proc_ptran->prev->next = _tmx_proc_ptran->next;
if(_tmx_proc_ptran->next)
_tmx_proc_ptran->next->prev = _tmx_proc_ptran->prev;
}
_tmx_proc_ptran->prev = _tmx_proc_ptran->next = NULL;
_tmx_proc_ptran->linked = 0;
return;
}
/**
*
*/
void tmx_pretran_unlink(void)
{
int slotid;
if(_tmx_proc_ptran==NULL)
return;
slotid = _tmx_proc_ptran->hid & (_tmx_ptran_size-1);
lock_get(&_tmx_ptran_table[slotid].lock);
tmx_pretran_unlink_safe(slotid);
lock_release(&_tmx_ptran_table[slotid].lock);
}
/**
* return:
* - -1: error
* - 0: not found
* - 1: found
*/
int tmx_check_pretran(sip_msg_t *msg)
{
unsigned int chid;
unsigned int slotid;
int dsize;
struct via_param *vbr;
str scallid;
str scseqmet;
str scseqnum;
str sftag;
str svbranch = {NULL, 0};
pretran_t *it;
if(_tmx_ptran_table==NULL) {
LM_ERR("pretran hash table not intialized yet\n");
return -1;
}
if(get_route_type()!=REQUEST_ROUTE) {
LM_ERR("invalid usage - not in request route\n");
return -1;
}
if(msg->first_line.type!=SIP_REQUEST) {
LM_ERR("invalid usage - not a sip request\n");
return -1;
}
if(parse_headers(msg, HDR_FROM_F|HDR_VIA1_F|HDR_CALLID_F|HDR_CSEQ_F, 0)<0) {
LM_ERR("failed to parse required headers\n");
return -1;
}
if(msg->cseq==NULL || msg->cseq->parsed==NULL) {
LM_ERR("failed to parse cseq headers\n");
return -1;
}
if(get_cseq(msg)->method_id==METHOD_ACK
|| get_cseq(msg)->method_id==METHOD_CANCEL) {
LM_DBG("no pre-transaction management for ACK or CANCEL\n");
return -1;
}
if (msg->via1==0) {
LM_ERR("failed to get Via header\n");
return -1;
}
if (parse_from_header(msg)<0 || get_from(msg)->tag_value.len==0) {
LM_ERR("failed to get From header\n");
return -1;
}
if (msg->callid==NULL || msg->callid->body.s==NULL) {
LM_ERR("failed to parse callid headers\n");
return -1;
}
vbr = msg->via1->branch;
scallid = msg->callid->body;
trim(&scallid);
scseqmet = get_cseq(msg)->method;
trim(&scseqmet);
scseqnum = get_cseq(msg)->number;
trim(&scseqnum);
sftag = get_from(msg)->tag_value;
trim(&sftag);
chid = get_hash1_raw(msg->callid->body.s, msg->callid->body.len);
slotid = chid & (_tmx_ptran_size-1);
if(unlikely(_tmx_proc_ptran == NULL)) {
_tmx_proc_ptran = (pretran_t*)shm_malloc(sizeof(pretran_t));
if(_tmx_proc_ptran == NULL) {
LM_ERR("not enough memory for pretran structure\n");
return -1;
}
memset(_tmx_proc_ptran, 0, sizeof(pretran_t));
_tmx_proc_ptran->pid = my_pid();
}
dsize = scallid.len + scseqnum.len + scseqmet.len
+ sftag.len + 4;
if(likely(vbr!=NULL)) {
svbranch = vbr->value;
trim(&svbranch);
dsize += svbranch.len + 1;
}
if(dsize<256) dsize = 256;
tmx_pretran_unlink();
if(dsize > _tmx_proc_ptran->dbuf.len) {
if(_tmx_proc_ptran->dbuf.s) shm_free(_tmx_proc_ptran->dbuf.s);
_tmx_proc_ptran->dbuf.s = (char*)shm_malloc(dsize);
if(_tmx_proc_ptran->dbuf.s==NULL) {
LM_ERR("not enough memory for pretran data\n");
return -1;
}
_tmx_proc_ptran->dbuf.len = dsize;
}
_tmx_proc_ptran->hid = chid;
_tmx_proc_ptran->cseqmetid = (get_cseq(msg))->method_id;
_tmx_proc_ptran->callid.s = _tmx_proc_ptran->dbuf.s;
memcpy(_tmx_proc_ptran->callid.s, scallid.s, scallid.len);
_tmx_proc_ptran->callid.len = scallid.len;
_tmx_proc_ptran->callid.s[_tmx_proc_ptran->callid.len] = '\0';
_tmx_proc_ptran->ftag.s = _tmx_proc_ptran->callid.s
+ _tmx_proc_ptran->callid.len + 1;
memcpy(_tmx_proc_ptran->ftag.s, sftag.s, sftag.len);
_tmx_proc_ptran->ftag.len = sftag.len;
_tmx_proc_ptran->ftag.s[_tmx_proc_ptran->ftag.len] = '\0';
_tmx_proc_ptran->cseqnum.s = _tmx_proc_ptran->ftag.s
+ _tmx_proc_ptran->ftag.len + 1;
memcpy(_tmx_proc_ptran->cseqnum.s, scseqnum.s, scseqnum.len);
_tmx_proc_ptran->cseqnum.len = scseqnum.len;
_tmx_proc_ptran->cseqnum.s[_tmx_proc_ptran->cseqnum.len] = '\0';
_tmx_proc_ptran->cseqmet.s = _tmx_proc_ptran->cseqnum.s
+ _tmx_proc_ptran->cseqnum.len + 1;
memcpy(_tmx_proc_ptran->cseqmet.s, scseqmet.s, scseqmet.len);
_tmx_proc_ptran->cseqmet.len = scseqmet.len;
_tmx_proc_ptran->cseqmet.s[_tmx_proc_ptran->cseqmet.len] = '\0';
if(likely(vbr!=NULL)) {
_tmx_proc_ptran->vbranch.s = _tmx_proc_ptran->cseqmet.s
+ _tmx_proc_ptran->cseqmet.len + 1;
memcpy(_tmx_proc_ptran->vbranch.s, svbranch.s, svbranch.len);
_tmx_proc_ptran->vbranch.len = svbranch.len;
_tmx_proc_ptran->vbranch.s[_tmx_proc_ptran->vbranch.len] = '\0';
} else {
_tmx_proc_ptran->vbranch.s = NULL;
_tmx_proc_ptran->vbranch.len = 0;
}
lock_get(&_tmx_ptran_table[slotid].lock);
it = _tmx_ptran_table[slotid].plist;
tmx_pretran_link_safe(slotid);
for(; it!=NULL; it=it->next) {
if(_tmx_proc_ptran->hid != it->hid
|| _tmx_proc_ptran->cseqmetid != it->cseqmetid
|| _tmx_proc_ptran->callid.len != it->callid.len
|| _tmx_proc_ptran->ftag.len != it->ftag.len
|| _tmx_proc_ptran->cseqmet.len != it->cseqmet.len
|| _tmx_proc_ptran->cseqnum.len != it->cseqnum.len)
continue;
if(_tmx_proc_ptran->vbranch.s != NULL && it->vbranch.s != NULL) {
if(_tmx_proc_ptran->vbranch.len != it->vbranch.len)
continue;
/* shortcut - check last char in Via branch
* - kamailio/ser adds there branch index => in case of paralel
* forking by previous hop, catch it here quickly */
if(_tmx_proc_ptran->vbranch.s[it->vbranch.len-1]
!= it->vbranch.s[it->vbranch.len-1])
continue;
if(memcmp(_tmx_proc_ptran->vbranch.s,
it->vbranch.s, it->vbranch.len)!=0)
continue;
/* shall stop by matching magic cookie?
if (vbr && vbr->value.s && vbr->value.len > MCOOKIE_LEN
&& memcmp(vbr->value.s, MCOOKIE, MCOOKIE_LEN)==0) {
LM_DBG("rfc3261 cookie found in Via branch\n");
}
*/
}
if(memcmp(_tmx_proc_ptran->callid.s,
it->callid.s, it->callid.len)!=0
|| memcmp(_tmx_proc_ptran->ftag.s,
it->ftag.s, it->ftag.len)!=0
|| memcmp(_tmx_proc_ptran->cseqnum.s,
it->cseqnum.s, it->cseqnum.len)!=0)
continue;
if((it->cseqmetid==METHOD_OTHER || it->cseqmetid==METHOD_UNDEF)
&& memcmp(_tmx_proc_ptran->cseqmet.s,
it->cseqmet.s, it->cseqmet.len)!=0)
continue;
LM_DBG("matched another pre-transaction by pid %d for [%.*s]\n",
it->pid, it->callid.len, it->callid.s);
lock_release(&_tmx_ptran_table[slotid].lock);
return 1;
}
lock_release(&_tmx_ptran_table[slotid].lock);
return 0;
}
|