GPLization banner introduced to *.[hc] files
[sip-router] / modules / tm / t_lookup.c
1 /*
2  * $Id$
3  *
4  * This C-file takes care of matching requests and replies with
5  * existing transactions. Note that we do not do SIP-compliant
6  * request matching as asked by SIP spec. We do bitwise matching of 
7  * all header fields in requests which form a transaction key. 
8  * It is much faster and it worx pretty well -- we haven't 
9  * had any interop issue neither in lab nor in bake-offs. The reason
10  * is that retransmissions do look same as original requests
11  * (it would be really silly if they wuld be mangled). The only
12  * exception is we parse To as To in ACK is compared to To in
13  * reply and both  of them are constructed by different software.
14  * 
15  * As for reply matching, we match based on branch value -- that is
16  * faster too. There are two versions .. with SYNONYMs #define
17  * enabled, the branch includes ordinal number of a transaction
18  * in a synonym list in hash table and is somewhat faster but
19  * not reboot-resilient. SYNONYMs turned off are little slower
20  * but work across reboots as well.
21  *
22  * The branch parameter is formed as follows:
23  * SYNONYMS  on: hash.synonym.branch
24  * SYNONYMS off: hash.md5.branch
25  *
26  * -jiri
27  *
28  *
29  * Copyright (C) 2001-2003 Fhg Fokus
30  *
31  * This file is part of ser, a free SIP server.
32  *
33  * ser is free software; you can redistribute it and/or modify
34  * it under the terms of the GNU General Public License as published by
35  * the Free Software Foundation; either version 2 of the License, or
36  * (at your option) any later version
37  *
38  * For a license to use the ser software under conditions
39  * other than those described here, or to purchase support for this
40  * software, please contact iptel.org by e-mail at the following addresses:
41  *    info@iptel.org
42  *
43  * ser is distributed in the hope that it will be useful,
44  * but WITHOUT ANY WARRANTY; without even the implied warranty of
45  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46  * GNU General Public License for more details.
47  *
48  * You should have received a copy of the GNU General Public License 
49  * along with this program; if not, write to the Free Software 
50  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
51  */
52
53
54 #include <assert.h>
55 #include "../../dprint.h"
56 #include "../../config.h"
57 #include "../../parser/parser_f.h"
58 #include "../../ut.h"
59 #include "../../timer.h"
60 #include "../../hash_func.h"
61 #include "../../globals.h"
62 #include "../../forward.h"
63 #include "t_funcs.h"
64 #include "config.h"
65 #include "sip_msg.h"
66 #include "t_hooks.h"
67 #include "t_lookup.h"
68
69
70 #define EQ_LEN(_hf) (t_msg->_hf->body.len==p_msg->_hf->body.len)
71 #define EQ_STR(_hf) (memcmp(t_msg->_hf->body.s,\
72         translate_pointer(p_msg->orig,p_msg->buf,p_msg->_hf->body.s), \
73         p_msg->_hf->body.len)==0)
74 #define EQ_REQ_URI_LEN\
75         (p_msg->first_line.u.request.uri.len==t_msg->first_line.u.request.uri.len)
76 #define EQ_REQ_URI_STR\
77         ( memcmp( t_msg->first_line.u.request.uri.s,\
78         translate_pointer(p_msg->orig,p_msg->buf,p_msg->first_line.u.request.uri.s),\
79         p_msg->first_line.u.request.uri.len)==0)
80 #define EQ_VIA_LEN(_via)\
81         ( (p_msg->via1->bsize-(p_msg->_via->name.s-(p_msg->_via->hdr.s+p_msg->_via->hdr.len)))==\
82         (t_msg->via1->bsize-(t_msg->_via->name.s-(t_msg->_via->hdr.s+t_msg->_via->hdr.len))) )
83
84 #define EQ_VIA_STR(_via)\
85         ( memcmp( t_msg->_via->name.s,\
86          translate_pointer(p_msg->orig,p_msg->buf,p_msg->_via->name.s),\
87          (t_msg->via1->bsize-(t_msg->_via->name.s-(t_msg->_via->hdr.s+t_msg->_via->hdr.len)))\
88         )==0 )
89
90 #define HF_LEN(_hf) ((_hf)->body.s+(_hf)->body.len-(_hf)->name.s)
91
92 /* presumably matching transaction for an e2e ACK */
93 static struct cell *t_ack;
94
95 /* this is a global variable which keeps pointer to
96    transaction currently processed by a process; it it
97    set by t_lookup_request or t_reply_matching; don't
98    dare to change it anywhere else as it would
99    break ref_counting
100 */
101
102 #ifdef _OBSOLETED
103 struct cell      *T;
104 #endif
105
106 static struct cell *T;
107
108 /* number of currently processed message; good to know
109    to be able to doublecheck whether we are still working
110    on a current transaction or a new message arrived;
111    don't even think of changing it
112 */
113 unsigned int     global_msg_id;
114
115 struct cell *get_t() { return T; }
116 void init_t() {global_msg_id=0; T=T_UNDEFINED;}
117
118
119 /* function returns:
120  *      negative - transaction wasn't found
121  *                      (-2 = possibly e2e ACK matched )
122  *      positive - transaction found
123  */
124
125 int t_lookup_request( struct sip_msg* p_msg , int leave_new_locked )
126 {
127         struct cell         *p_cell;
128         unsigned int       isACK;
129         struct sip_msg  *t_msg;
130         int ret;
131
132         /* parse all*/
133         if (check_transaction_quadruple(p_msg)==0)
134         {
135                 LOG(L_ERR, "ERROR: TM module: t_lookup_request: too few headers\n");
136                 T=0;
137                 /* stop processing */
138                 return 0;
139         }
140
141         /* start searching into the table */
142         if (!p_msg->hash_index)
143                 p_msg->hash_index=hash( p_msg->callid->body , 
144                         get_cseq(p_msg)->number ) ;
145         isACK = p_msg->REQ_METHOD==METHOD_ACK;
146         DBG("t_lookup_request: start searching: hash=%d, isACK=%d\n",
147                 p_msg->hash_index,isACK);
148
149         /* asume not found */
150         ret=-1;
151
152         /* lock the hole entry*/
153         LOCK_HASH(p_msg->hash_index);
154
155         /* all the transactions from the entry are compared */
156         for ( p_cell = get_tm_table()->entrys[p_msg->hash_index].first_cell;
157                   p_cell; p_cell = p_cell->next_cell ) 
158         {
159                 t_msg = p_cell->uas.request;
160
161                 if (!isACK) {   
162                         /* compare lengths first */ 
163                         if (!EQ_LEN(callid)) continue;
164                         if (!EQ_LEN(cseq)) continue;
165                         if (!EQ_LEN(from)) continue;
166                         if (!EQ_LEN(to)) continue;
167                         if (!EQ_REQ_URI_LEN) continue;
168                         if (!EQ_VIA_LEN(via1)) continue;
169
170                         /* length ok -- move on */
171                         if (!EQ_STR(callid)) continue;
172                         if (!EQ_STR(cseq)) continue;
173                         if (!EQ_STR(from)) continue;
174                         if (!EQ_STR(to)) continue;
175                         if (!EQ_REQ_URI_STR) continue;
176                         if (!EQ_VIA_STR(via1)) continue;
177
178                         /* request matched ! */
179                         DBG("DEBUG: non-ACK matched\n");
180                         goto found;
181                 } else { /* it's an ACK request*/
182                         /* ACK's relate only to INVITEs */
183                         if (t_msg->REQ_METHOD!=METHOD_INVITE) continue;
184
185                         /* compare lengths now */
186                         if (!EQ_LEN(callid)) continue;
187                         /* CSeq only the number without method ! */
188                         if (get_cseq(t_msg)->number.len!=get_cseq(p_msg)->number.len)
189                                 continue;
190                         if (! EQ_LEN(from)) continue;
191                         /* To only the uri and ... */
192                         if (get_to(t_msg)->uri.len!=get_to(p_msg)->uri.len)
193                                 continue;
194                         /* ... its to-tag compared to reply's tag */
195                         if (p_cell->uas.to_tag.len!=get_to(p_msg)->tag_value.len)
196                                 continue;
197
198                         /* we first skip r-uri and Via and proceed with
199                            content of other header-fields */
200
201                         if ( memcmp(t_msg->callid->body.s, p_msg->callid->body.s,
202                                 p_msg->callid->body.len)!=0) continue;
203                         if ( memcmp(get_cseq(t_msg)->number.s, get_cseq(p_msg)->number.s,
204                                 get_cseq(p_msg)->number.len)!=0) continue;
205                         if (!EQ_STR(from)) continue;
206                         if (memcmp(get_to(t_msg)->uri.s, get_to(p_msg)->uri.s,
207                                 get_to(t_msg)->uri.len)!=0) continue;
208                         if (
209 #ifdef _BUG
210                                 p_cell->uas.to_tag.len!=0 /* to-tags empty */ || 
211 #endif
212                                 memcmp(p_cell->uas.to_tag.s, get_to(p_msg)->tag_value.s,
213                                 p_cell->uas.to_tag.len)!=0) continue;
214         
215                         /* ok, now only r-uri or via can mismatch; they must match
216                            for non-2xx; if it is a 2xx, we don't try to match
217                            (we might have checked that earlier to speed-up, but
218                            we still want to see a diagnosti message telling
219                            "this ACK presumably belongs to this 2xx transaction";
220                            might change in future); the reason is 2xx ACKs are
221                            a separate transaction which may carry different
222                            r-uri/via1 and is thus also impossible to match it
223                            uniquely to a spiraled transaction;
224                         */
225                         if (p_cell->uas.status>=200 && p_cell->uas.status<300) {
226                                 DBG("DEBUG: an ACK hit a 2xx transaction (T=%p); "
227                                         "considered mismatch\n", p_cell );
228                                 /* perhaps there are some spirals on the synonym list, but
229                                    it makes no sense to iterate the list until bitter end */
230                                 t_ack=p_cell;
231                                 ret=-2;
232                                 break;
233                         }
234                         /* its for a >= 300 ... everything must match ! */
235                         if (! EQ_REQ_URI_LEN ) continue;
236                         if (! EQ_VIA_LEN(via1)) continue;
237                         if (!EQ_REQ_URI_STR) continue;
238                         if (!EQ_VIA_STR(via1)) continue;
239
240                         /* wow -- we survived all the check! we matched! */
241                         DBG("DEBUG: non-2xx ACK matched\n");
242                         goto found;
243                 } /* ACK */
244         } /* synonym loop */
245
246         /* no transaction found */
247         T = 0;
248         if (!leave_new_locked) {
249                 UNLOCK_HASH(p_msg->hash_index);
250         }
251         DBG("DEBUG: t_lookup_request: no transaction found\n");
252         return ret;
253
254 found:
255         T=p_cell;
256         REF_UNSAFE( T );
257         T->kr|=REQ_EXIST;
258         UNLOCK_HASH( p_msg->hash_index );
259         DBG("DEBUG: t_lookup_request: transaction found (T=%p)\n",T);
260         return 1;
261 }
262
263
264
265 /* function returns:
266  *       0 - transaction wasn't found
267  *       T - transaction found
268  */
269 struct cell* t_lookupOriginalT(  struct sip_msg* p_msg )
270 {
271         struct cell     *p_cell;
272         unsigned int     hash_index;
273         struct sip_msg  *t_msg;
274
275
276         /* start searching in the table */
277         hash_index = p_msg->hash_index;
278         LOCK_HASH(hash_index);
279         DBG("DEBUG: t_lookupOriginalT: searching on hash entry %d\n",hash_index );
280
281         /* all the transactions from the entry are compared */
282         for (p_cell=get_tm_table()->entrys[hash_index].first_cell;
283                 p_cell; p_cell = p_cell->next_cell )
284         {
285                 t_msg = p_cell->uas.request;
286
287                 /* we don't cancel CANCELs ;-) */
288                 if (p_cell->uas.request->REQ_METHOD==METHOD_CANCEL)
289                         continue;
290
291                 /* check lengths now */ 
292                 if (!EQ_LEN(callid))
293                         continue;
294                 if (get_cseq(t_msg)->number.len!=get_cseq(p_msg)->number.len)
295                         continue;
296                 if (!EQ_LEN(from))
297                         continue;
298                 if (!EQ_LEN(to))
299                         continue;
300                 if (!EQ_REQ_URI_LEN)
301                         continue;
302                 if (!EQ_VIA_LEN(via1))
303                         continue;
304
305                 /* check the content now */
306                 if (!EQ_STR(callid))
307                         continue;
308                 if (memcmp(get_cseq(t_msg)->number.s,
309                         get_cseq(p_msg)->number.s,get_cseq(p_msg)->number.len)!=0)
310                         continue;
311                 if (!EQ_STR(from))
312                         continue;
313                 if (!EQ_STR(to))
314                         continue;
315                 if (!EQ_REQ_URI_STR)
316                         continue;
317                 if (!EQ_VIA_STR(via1))
318                         continue;
319
320                 /* found */
321                 DBG("DEBUG: t_lookupOriginalT: canceled transaction"
322                         " found (%p)! \n",p_cell );
323                 REF_UNSAFE( p_cell );
324                 UNLOCK_HASH(hash_index);
325                 DBG("DEBUG: t_lookupOriginalT completed\n");
326                 return p_cell;
327         }
328
329         /* no transaction found */
330         DBG("DEBUG: t_lookupOriginalT: no CANCEL maching found! \n" );
331         UNLOCK_HASH(hash_index);
332         DBG("DEBUG: t_lookupOriginalT completed\n");
333         return 0;
334 }
335
336
337
338
339 /* Returns 0 - nothing found
340  *         1  - T found
341  */
342 int t_reply_matching( struct sip_msg *p_msg , int *p_branch )
343 {
344         struct cell*  p_cell;
345         int hash_index   = 0;
346         int entry_label  = 0;
347         int branch_id    = 0;
348         char  *hashi, *branchi, *p, *n;
349         int hashl, branchl;
350         int scan_space;
351         str cseq_method;
352         str req_method;
353
354         char *loopi;
355         int loopl;
356         char *syni;
357         int synl;
358         
359         short is_cancel;
360
361         /* make compiler warnnings happy */
362         loopi=0;
363         loopl=0;
364         syni=0;
365         synl=0;
366
367         /* split the branch into pieces: loop_detection_check(ignored),
368          hash_table_id, synonym_id, branch_id */
369
370         if (!(p_msg->via1 && p_msg->via1->branch && p_msg->via1->branch->value.s))
371                 goto nomatch2;
372
373         p=p_msg->via1->branch->value.s;
374         scan_space=p_msg->via1->branch->value.len;
375
376
377         /* hash_id */
378         n=eat_token2_end( p, p+scan_space, BRANCH_SEPARATOR);
379         hashl=n-p;
380         scan_space-=hashl;
381         if (!hashl || scan_space<2 || *n!=BRANCH_SEPARATOR) goto nomatch2;
382         hashi=p;
383         p=n+1;scan_space--;
384
385         if (!syn_branch) {
386                 /* md5 value */
387                 n=eat_token2_end( p, p+scan_space, BRANCH_SEPARATOR );
388                 loopl = n-p;
389                 scan_space-= loopl;
390                 if (n==p || scan_space<2 || *n!=BRANCH_SEPARATOR) 
391                         goto nomatch2;
392                 loopi=p;
393                 p=n+1; scan_space--;
394         } else {
395                 /* synonym id */
396                 n=eat_token2_end( p, p+scan_space, BRANCH_SEPARATOR);
397                 synl=n-p;
398                 scan_space-=synl;
399                 if (!synl || scan_space<2 || *n!=BRANCH_SEPARATOR) 
400                         goto nomatch2;
401                 syni=p;
402                 p=n+1;scan_space--;
403         }
404
405         /* branch id  -  should exceed the scan_space */
406         n=eat_token_end( p, p+scan_space );
407         branchl=n-p;
408         if (!branchl ) goto nomatch2;
409         branchi=p;
410
411         /* sanity check */
412         if ((hash_index=reverse_hex2int(hashi, hashl))<0
413                 ||hash_index>=TABLE_ENTRIES
414                 || (branch_id=reverse_hex2int(branchi, branchl))<0
415                 ||branch_id>=MAX_BRANCHES
416                 || (syn_branch ? (entry_label=reverse_hex2int(syni, synl))<0 
417                         : loopl!=MD5_LEN )
418         ) {
419                 DBG("DEBUG: t_reply_matching: poor reply lables %d label %d "
420                         "branch %d\n",hash_index, entry_label, branch_id );
421                 goto nomatch2;
422         }
423
424
425         DBG("DEBUG: t_reply_matching: hash %d label %d branch %d\n",
426                 hash_index, entry_label, branch_id );
427
428
429         /* search the hash table list at entry 'hash_index'; lock the
430            entry first 
431         */
432         cseq_method=get_cseq(p_msg)->method;
433         is_cancel=cseq_method.len==CANCEL_LEN 
434                 && memcmp(cseq_method.s, CANCEL, CANCEL_LEN)==0;
435         LOCK_HASH(hash_index);
436         for (p_cell = get_tm_table()->entrys[hash_index].first_cell; p_cell; 
437                 p_cell=p_cell->next_cell) {
438
439                 /* first look if branch matches */
440
441                 if (syn_branch) {
442                         if (p_cell->label != entry_label) 
443                                 continue;
444                 } else {
445                         if ( memcmp(p_cell->md5, loopi,MD5_LEN)!=0)
446                                         continue;
447                 }
448
449                 /* sanity check ... too high branch ? */
450                 if ( branch_id>=p_cell->nr_of_outgoings )
451                         continue;
452
453                 /* does method match ? (remember -- CANCELs have the same branch
454                    as cancelled transactions) */
455                 req_method=p_cell->method;
456                 if ( /* method match */
457                         ! ((cseq_method.len==req_method.len 
458                         && memcmp( cseq_method.s, req_method.s, cseq_method.len )==0)
459                         /* or it is a local cancel */
460                         || (is_cancel && p_cell->is_invite 
461                                 /* commented out -- should_cancel_branch set it to
462                                    BUSY_BUFFER to avoid collisions with repliesl;
463                                    thus, we test here by bbuffer size
464                                 */
465                                 /* && p_cell->uac[branch_id].local_cancel.buffer ))) */
466                                 && p_cell->uac[branch_id].local_cancel.buffer_len ))) 
467                         continue;
468
469
470                 /* we passed all disqualifying factors .... the transaction has been
471                    matched !
472                 */
473                 T=p_cell;
474                 *p_branch = branch_id;
475                 REF_UNSAFE( T );
476                 UNLOCK_HASH(hash_index);
477                 DBG("DEBUG: t_reply_matching: reply matched (T=%p)!\n",T);
478                 return 1;
479         } /* for cycle */
480
481         /* nothing found */
482         UNLOCK_HASH(hash_index);
483         DBG("DEBUG: t_reply_matching: no matching transaction exists\n");
484
485 nomatch2:
486         DBG("DEBUG: t_reply_matching: failure to match a transaction\n");
487         *p_branch = -1;
488         T = 0;
489         return -1;
490 }
491
492
493
494
495 /* Functions update T (T gets either a valid pointer in it or it equals zero) if no transaction
496   * for current message exists;
497   * it returns 1 if found, 0 if not found, -1 on error
498   */
499 int t_check( struct sip_msg* p_msg , int *param_branch )
500 {
501         int local_branch;
502
503         /* is T still up-to-date ? */
504         DBG("DEBUG: t_check: msg id=%d global id=%d T start=%p\n", 
505                 p_msg->id,global_msg_id,T);
506         if ( p_msg->id != global_msg_id || T==T_UNDEFINED )
507         {
508                 global_msg_id = p_msg->id;
509                 T = T_UNDEFINED;
510                 /* transaction lookup */
511                 if ( p_msg->first_line.type==SIP_REQUEST ) {
512                         /* force parsing all the needed headers*/
513                         if (parse_headers(p_msg, HDR_EOH, 0 )==-1)
514                                 return -1;
515                         t_lookup_request( p_msg , 0 /* unlock before returning */ );
516                 } else {
517                         /* we need Via for branch and Cseq method to distinguish
518                            replies with the same branch/cseqNr (CANCEL)
519                         */
520                         if ( parse_headers(p_msg, HDR_VIA1|HDR_CSEQ, 0 )==-1
521                         || !p_msg->via1 || !p_msg->cseq ) {
522                                 LOG(L_ERR, "ERROR: reply cannot be parsed\n");
523                                 return -1;
524                         }
525
526                         /* if that is an INVITE, we will also need to-tag
527                            for later ACK matching
528                         */
529             if ( get_cseq(p_msg)->method.len==INVITE_LEN 
530                                 && memcmp( get_cseq(p_msg)->method.s, INVITE, INVITE_LEN )==0 ) {
531                                         if (parse_headers(p_msg, HDR_TO, 0)==-1
532                                                 || !p_msg->to)  {
533                                                 LOG(L_ERR, "ERROR: INVITE reply cannot be parsed\n");
534                                                 return -1;
535                                         }
536                         }
537
538                         t_reply_matching( p_msg ,
539                                 param_branch!=0?param_branch:&local_branch );
540
541                 }
542 #ifdef EXTRA_DEBUG
543                 if ( T && T!=T_UNDEFINED && T->damocles) {
544                         LOG( L_ERR, "ERROR: transaction %p scheduled for deletion "
545                                 "and called from t_check\n", T);
546                         abort();
547                 }
548 #endif
549                 DBG("DEBUG: t_check: msg id=%d global id=%d T end=%p\n",
550                         p_msg->id,global_msg_id,T);
551         } else {
552                 if (T)
553                         DBG("DEBUG: t_check: T alredy found!\n");
554                 else
555                         DBG("DEBUG: t_check: T previously sought and not found\n");
556         }
557
558         return ((T)?1:0) ;
559 }
560
561 int init_rb( struct retr_buf *rb, struct sip_msg *msg )
562 {
563         struct socket_info* send_sock;
564         struct via_body* via;
565
566         if (!reply_to_via) {
567                 update_sock_struct_from_ip( &rb->to, msg );
568         } else {
569                 via=msg->via1;
570                 /*init retrans buffer*/
571                 if (update_sock_struct_from_via( &(rb->to),via )==-1) {
572                         LOG(L_ERR, "ERROR: init_rb: cannot lookup reply dst: %.*s\n",
573                                 via->host.len, via->host.s );
574                         ser_error=E_BAD_VIA;
575                         return 0;
576                 }
577         }
578         send_sock=get_send_socket(&rb->to);
579         if (send_sock==0) {
580                 LOG(L_ERR, "ERROR: init_rb: cannot fwd to af %d "
581                         "no socket\n", rb->to.s.sa_family);
582                 ser_error=E_BAD_VIA;
583                 return 0;
584         }
585         rb->send_sock=send_sock;
586     return 1;
587 }
588
589
590
591 /* atomic "new_tran" construct; it returns:
592
593         <0      on error
594
595         +1      if a request did not match a transaction
596                 - it that was an ack, the calling function
597                   shall forward statelessy
598                 - otherwise it means, a new transaction was
599                   introduced and the calling function
600                   shall reply/relay/whatever_appropriate
601
602         0 on retransmission
603 */
604 int t_newtran( struct sip_msg* p_msg )
605 {
606
607         int ret, lret;
608         struct cell *new_cell;
609         struct sip_msg *shm_msg;
610
611         ret=1;
612
613         /* is T still up-to-date ? */
614         DBG("DEBUG: t_addifnew: msg id=%d , global msg id=%d ,"
615                 " T on entrance=%p\n",p_msg->id,global_msg_id,T);
616
617         if ( T && T!=T_UNDEFINED  ) {
618                 LOG(L_ERR, "ERROR: t_newtran: "
619                         "transaction already in process %p\n", T );
620                 return E_SCRIPT;
621         }
622
623         global_msg_id = p_msg->id;
624         T = T_UNDEFINED;
625         /* first of all, parse everything -- we will store
626            in shared memory and need to have all headers
627            ready for generating potential replies later;
628            parsing later on demand is not an option since
629            the request will be in shmem and applying 
630            parse_headers to it would intermix shmem with
631            pkg_mem
632         */
633         
634         if (parse_headers(p_msg, HDR_EOH, 0 )) {
635                 LOG(L_ERR, "ERROR: t_newtran: parse_headers failed\n");
636                 return E_BAD_REQ;
637         }
638         if ((p_msg->parsed_flag & HDR_EOH)!=HDR_EOH) {
639                         LOG(L_ERR, "ERROR: t_newtran: EoH not parsed\n");
640                         return E_OUT_OF_MEM;
641         }
642         /* t_lookup_requests attmpts to find the transaction; 
643            it also calls check_transaction_quadruple -> it is
644            safe to assume we have from/callid/cseq/to
645         */ 
646         lret = t_lookup_request( p_msg, 1 /* leave locked if not found */ );
647         /* on error, pass the error in the stack ... */
648         if (lret==0) return E_BAD_TUPEL;
649         /* transaction not found, it's a new request;
650            establish a new transaction (unless it is an ACK) */
651         if (lret<0) {
652                 new_cell=0;
653                 if ( p_msg->REQ_METHOD!=METHOD_ACK ) {
654                         /* add new transaction */
655                         new_cell = build_cell( p_msg ) ;
656                         if  ( !new_cell ){
657                                 LOG(L_ERR, "ERROR: t_addifnew: out of mem:\n");
658                                 ret = E_OUT_OF_MEM;
659                         } else {
660                                 insert_into_hash_table_unsafe( new_cell );
661                                 T=new_cell;
662                                 INIT_REF_UNSAFE(T);
663                                 /* init pointers to headers needed to construct local
664                                    requests such as CANCEL/ACK
665                                 */
666
667                                 shm_msg=new_cell->uas.request;
668                                 new_cell->from.s=shm_msg->from->name.s;
669                                 new_cell->from.len=HF_LEN(shm_msg->from);
670                                 new_cell->to.s=shm_msg->to->name.s;
671                                 new_cell->to.len=HF_LEN(shm_msg->to);
672                                 new_cell->callid.s=shm_msg->callid->name.s;
673                                 new_cell->callid.len=HF_LEN(shm_msg->callid);
674                                 new_cell->cseq_n.s=shm_msg->cseq->name.s;
675                                 new_cell->cseq_n.len=get_cseq(shm_msg)->number.s
676                                         +get_cseq(shm_msg)->number.len
677                                         -shm_msg->cseq->name.s;
678
679                                 new_cell->method=new_cell->uas.request->first_line.u.request.method;
680                                 new_cell->is_invite=p_msg->REQ_METHOD==METHOD_INVITE;
681                         }
682
683                 }
684
685                 /* was it an e2e ACK ? if so, trigger a callback */
686                 if (lret==-2) {
687                                 REF_UNSAFE(t_ack);
688                                 UNLOCK_HASH(p_msg->hash_index);
689                                 callback_event( TMCB_E2EACK, t_ack, p_msg, p_msg->REQ_METHOD );
690                                 UNREF(t_ack);
691                 } else { /* not e2e ACK */
692                         UNLOCK_HASH(p_msg->hash_index);
693                         /* now, when the transaction state exists, check if
694                            there is a meaningful Via and calculate it; better
695                            do it now than later: state is established so that
696                            subsequent retransmissions will be absorbed and will
697                            not possibly block during Via DNS resolution; doing
698                            it later would only burn more CPU as if there is an
699                            error, we cannot relay later whatever comes out of the
700                            the transaction 
701                         */
702                         if (new_cell && p_msg->REQ_METHOD!=METHOD_ACK) {
703                                 if (!init_rb( &T->uas.response, p_msg)) {
704                                         LOG(L_ERR, "ERROR: t_newtran: unresolveable via1\n");
705                                         put_on_wait( T );
706                                         t_unref(p_msg);
707                                         ret=E_BAD_VIA;
708                                 }
709                         }
710                 }
711
712                 return ret;
713         } 
714
715         /* transaction found, it's a retransmission  or hbh ACK */
716         if (p_msg->REQ_METHOD==METHOD_ACK) {
717                 t_release_transaction(T);
718         } else {
719                 t_retransmit_reply(T);
720         }
721         /* things are done -- return from script */
722         return 0;
723
724 }
725
726
727 int t_unref( struct sip_msg* p_msg  )
728 {
729         if (T==T_UNDEFINED || T==T_NULL)
730                 return -1;
731         if (T->kr==0 
732                 ||(p_msg->REQ_METHOD==METHOD_ACK && !(T->kr & REQ_RLSD))) {
733                 LOG(L_WARN, "WARNING: script writer didn't release transaction\n");
734                 t_release_transaction(T);
735         }
736         UNREF( T );
737         T=T_UNDEFINED;
738         return 1;
739 }
740