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