t_lookup.c

Go to the documentation of this file.
00001 /*
00002  * $Id: t_lookup.c 5748 2009-03-20 15:33:36Z osas $
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * ----------
00024  * 2003-01-23  options for disabling r-uri matching introduced (jiri)
00025  *              nameser_compat.h (andrei)
00026  * 2003-01-27  next baby-step to removing ZT - PRESERVE_ZT (jiri)
00027  * 2003-01-28  scratchpad removed (jiri)
00028  * 2003-02-13  init_rb() is proto indep. & it uses struct dest_info (andrei)
00029  * 2003-02-24  s/T_NULL/T_NULL_CELL/ to avoid redefinition conflict w/
00030  * 2003-02-27  3261 ACK/200 consumption bug removed (jiri)
00031  * 2003-02-28 scratchpad compatibility abandoned (jiri)
00032  * 2003-03-01  kr set through a function now (jiri)
00033  * 2003-03-06  dialog matching introduced for ACKs -- that's important for 
00034  *             INVITE UAS (like INVITE) and 200/ACK proxy matching (jiri)
00035  * 2003-03-29  optimization: e2e ACK matching only if callback installed
00036  *             (jiri)
00037  * 2003-03-30  set_kr for requests only (jiri)
00038  * 2003-04-04  bug_fix: RESPONSE_IN callback not called for local
00039  *             UAC transactions (jiri)
00040  * 2003-04-07  new transactions inherit on_negative and on_relpy from script
00041  *             variables on instantiation (jiri)
00042  * 2003-04-30  t_newtran clean up (jiri)
00043  * 2003-08-21  request lookups fixed to skip UAC transactions, 
00044  *             thanks Ed (jiri)
00045  * 2003-12-04  global TM callbacks switched to per transaction callbacks
00046  *             (bogdan)
00047  * 2004-02-11  FIFO/CANCEL + alignments (hash=f(callid,cseq)) (uli+jiri)
00048  * 2004-02-13: t->is_invite and t->local replaced with flags (bogdan)
00049  * 2004-10-10: use of mhomed disabled for replies (jiri)
00050  * 2005-02-01: use the incoming request interface for sending the replies
00051  *             - changes in init_rb() (bogdan)
00052  */
00053 
00054 /*! \file
00055  * \brief TM :: Transaction matching
00056  *
00057  * \ingroup tm
00058  * - Module: \ref tm
00059  *
00060  * This C-file takes care of matching requests and replies with
00061  * existing transactions. Note that we do not do SIP-compliant
00062  * request matching as asked by SIP spec. We do bitwise matching of 
00063  * all header fields in requests which form a transaction key. 
00064  * It is much faster and it works pretty well -- we haven't 
00065  * had any interop issue neither in lab nor in bake-offs. The reason
00066  * is that retransmissions do look same as original requests
00067  * (it would be really silly if they would be mangled). The only
00068  * exception is we parse To as To in ACK is compared to To in
00069  * reply and both  of them are constructed by different software.
00070  * 
00071  * As for reply matching, we match based on branch value -- that is
00072  * faster too. There are two versions .. with SYNONYMs #define
00073  * enabled, the branch includes ordinal number of a transaction
00074  * in a synonym list in hash table and is somewhat faster but
00075  * not reboot-resilient. SYNONYMs turned off are little slower
00076  * but work across reboots as well.
00077  *
00078  * The branch parameter is formed as follows:
00079  * SYNONYMS  on: hash.synonym.branch
00080  * SYNONYMS off: hash.md5.branch
00081  */
00082 
00083 #include "../../dprint.h"
00084 #include "../../parser/parser_f.h"
00085 #include "../../parser/parse_from.h"
00086 #include "../../ut.h"
00087 #include "../../timer.h"
00088 #include "../../hash_func.h"
00089 #include "../../forward.h"
00090 #include "t_funcs.h"
00091 #include "config.h"
00092 #include "sip_msg.h"
00093 #include "t_hooks.h"
00094 #include "t_lookup.h"
00095 #include "dlg.h" /* for t_lookup_callid */
00096 #include "t_msgbuilder.h" /* for t_lookup_callid */
00097 #include "t_fwd.h" /* for get_on_branch */
00098 
00099 #define EQ_VIA_LEN(_via)\
00100    ( (p_msg->via1->bsize-(p_msg->_via->name.s-(p_msg->_via->hdr.s+p_msg->_via->hdr.len)))==\
00101       (t_msg->via1->bsize-(t_msg->_via->name.s-(t_msg->_via->hdr.s+t_msg->_via->hdr.len))) )
00102 
00103 
00104 
00105 #define EQ_LEN(_hf) (t_msg->_hf->body.len==p_msg->_hf->body.len)
00106 #define EQ_REQ_URI_LEN\
00107    (p_msg->first_line.u.request.uri.len==t_msg->first_line.u.request.uri.len)
00108 
00109 #define EQ_STR(_hf) (memcmp(t_msg->_hf->body.s,\
00110    p_msg->_hf->body.s, \
00111    p_msg->_hf->body.len)==0)
00112 #define EQ_REQ_URI_STR\
00113    ( memcmp( t_msg->first_line.u.request.uri.s,\
00114    p_msg->first_line.u.request.uri.s,\
00115    p_msg->first_line.u.request.uri.len)==0)
00116 #define EQ_VIA_STR(_via)\
00117    ( memcmp( t_msg->_via->name.s,\
00118     p_msg->_via->name.s,\
00119     (t_msg->via1->bsize-(t_msg->_via->name.s-(t_msg->_via->hdr.s+t_msg->_via->hdr.len)))\
00120    )==0 )
00121 #define EQ_STRS( _s1, _s2 ) \
00122    ( (_s1).len==(_s2).len && memcmp((_s1).s,(_s2).s,(_s2).len)==0)
00123 
00124 
00125 #define HF_LEN(_hf) ((_hf)->len)
00126 
00127 /*!
00128  * Should be request-uri matching used as a part of pre-3261 
00129  * transaction matching, as the standard wants us to do so
00130  * (and is reasonable to do so, to be able to distinguish
00131  * spirals)? Turn only off for better interaction with 
00132  * devices that are broken and send different r-uri in
00133  * CANCEL/ACK than in original INVITE
00134  */
00135 int ruri_matching=1;
00136 int via1_matching=1;
00137 
00138 /*!
00139  * This is a global variable which keeps a pointer to the
00140  * transaction currently processed by a process; it is
00141  * set by t_lookup_request or t_reply_matching; don't
00142  * dare to change it anywhere else as it would
00143  * break ref_counting
00144  */
00145 static struct cell *T;
00146 
00147 /*!
00148  * Simillar to T, but it is used for the cancelled invite
00149  * transaction (when processing a CANCEL)
00150  */
00151 static struct cell *cancelled_T;
00152 
00153 /*!
00154  * Simillar to T, but it is used for the ack-ed invite
00155  * transaction (when processing a end-to-end 200 ACK )
00156  */
00157 static struct cell *e2eack_T;
00158 
00159 /* getter, setter and initialisation functions for the diverse T */
00160 
00161 struct cell *get_t(void) { return T; }
00162 void set_t(struct cell *t) { T=t; }
00163 
00164 void init_t(void) {set_t(T_UNDEFINED);}
00165 
00166 struct cell *get_cancelled_t(void) { return cancelled_T; }
00167 void reset_cancelled_t(void) { cancelled_T=T_UNDEFINED; }
00168 
00169 struct cell *get_e2eack_t(void) { return e2eack_T; }
00170 void reset_e2eack_t(void) { e2eack_T=T_UNDEFINED; }
00171 
00172 
00173 /*!
00174  * \brief Parse necessary dialog related headers in the SIP message
00175  * \param msg SIP message
00176  * \return 1 on success, 0 on errors
00177  */
00178 static inline int parse_dlg( struct sip_msg *msg )
00179 {
00180    if (parse_headers(msg, HDR_FROM_F | HDR_CSEQ_F | HDR_TO_F, 0)==-1) {
00181       LM_ERR("From or Cseq or To invalid\n");
00182       return 0;
00183    }
00184    if ((msg->from==0)||(msg->cseq==0)||(msg->to==0)) {
00185       LM_ERR("missing From or Cseq or To\n");
00186       return 0;
00187    }
00188 
00189    if (parse_from_header(msg)<0) {
00190       LM_ERR("From broken\n");
00191       return 0;
00192    }
00193    /* To is automatically parsed through HDR_TO in parse bitmap,
00194     * we don't need to worry about it now */
00195    return 1;
00196 }
00197 
00198 
00199 /*!
00200  * \brief Partial dialog matching for two SIP messages
00201  *
00202  * Partial dialog matching in two SIP messages. Is the ACK (p_msg)
00203  * in p_msg dialog-wise equal to the INVITE (t_msg) except to-tags?
00204  * \param t_msg first SIP message with INVITE
00205  * \param p_msg second SIP message with ACK
00206  * \return 1 on success, 0 on errors
00207  */
00208 static inline int partial_dlg_matching(struct sip_msg *t_msg, struct sip_msg *p_msg)
00209 {
00210    struct to_body *inv_from;
00211 
00212    if (!EQ_LEN(callid)) return 0;
00213    if (get_cseq(t_msg)->number.len!=get_cseq(p_msg)->number.len)
00214       return 0;
00215    inv_from=get_from(t_msg);
00216    if (!inv_from) {
00217       LM_ERR("INV/From not parsed\n");
00218       return 0;
00219    }
00220    if (inv_from->tag_value.len!=get_from(p_msg)->tag_value.len)
00221       return 0;
00222    if (!EQ_STR(callid)) 
00223       return 0;
00224    if (memcmp(get_cseq(t_msg)->number.s, get_cseq(p_msg)->number.s,
00225          get_cseq(p_msg)->number.len)!=0)
00226       return 0;
00227    if (memcmp(inv_from->tag_value.s, get_from(p_msg)->tag_value.s,
00228          get_from(p_msg)->tag_value.len)!=0)
00229       return 0;
00230    return 1;
00231 }
00232 
00233 
00234 /*!
00235  * \brief Dialog matching
00236  *
00237  * Dialog matching, are to-tags in ACK/200 same as those we sent out?
00238  * \param p_cell transaction with the TO tag
00239  * \param ack checked SIP message
00240  * \return 1 on success, zero on errors
00241  */
00242 static inline int dlg_matching(struct cell *p_cell, struct sip_msg *ack )
00243 {
00244    if (get_to(ack)->tag_value.len!=p_cell->uas.local_totag.len)
00245       return 0;
00246    if (memcmp(get_to(ack)->tag_value.s,p_cell->uas.local_totag.s,
00247             p_cell->uas.local_totag.len)!=0)
00248       return 0;
00249    return 1;
00250 }
00251 
00252 /*!
00253  * \brief ACK matching
00254  * \param p_cell transaction
00255  * \param p_msg checked SIP message
00256  */
00257 static inline int ack_matching(struct cell *p_cell, struct sip_msg *p_msg) 
00258 {
00259    /* partial dialog matching -- no to-tag, only from-tag, 
00260     * callid, cseq number */
00261    if (!partial_dlg_matching(p_cell->uas.request, p_msg)) 
00262       return 0;
00263 
00264    /* if this transaction is proxied (as opposed to UAS) we're
00265     * done now -- we ignore to-tags; the ACK simply belongs to
00266     * this UAS part of dialog, whatever to-tag it gained
00267     */
00268    if (p_cell->relayed_reply_branch!=-2) {
00269       return 2; /* e2e proxied ACK */
00270    }
00271    /* it's a local dialog -- we wish to verify to-tags too */
00272    if (dlg_matching(p_cell, p_msg)) {
00273       return 2;
00274    }
00275    return 0;
00276 }
00277 
00278 
00279 /*!
00280  * \brief Branch-based transaction matching
00281  *
00282  * \param inv_via VIA body
00283  * \param ack_via VIA from ACK
00284  * \return 1 on match, 0 for no match
00285  */
00286 static inline int via_matching( struct via_body *inv_via, 
00287             struct via_body *ack_via )
00288 {
00289    if (inv_via->tid.len!=ack_via->tid.len)
00290       return 0;
00291    if (memcmp(inv_via->tid.s, ack_via->tid.s,
00292             ack_via->tid.len)!=0)
00293       return 0;
00294    /* ok, tid matches -- now make sure that the
00295     * originator matches too to avoid confusion with
00296     * different senders generating the same tid
00297     */
00298    if (inv_via->host.len!=ack_via->host.len)
00299       return 0;;
00300    if (memcmp(inv_via->host.s, ack_via->host.s,
00301          ack_via->host.len)!=0)
00302       return 0;
00303    if (inv_via->port!=ack_via->port)
00304       return 0;
00305    if (inv_via->transport.len!=ack_via->transport.len)
00306       return 0;
00307    if (memcmp(inv_via->transport.s, ack_via->transport.s,
00308          ack_via->transport.len)!=0)
00309       return 0;
00310    /* everything matched -- we found it */
00311    return 1;
00312 }
00313 
00314 
00315 /*!
00316  * \brief Transaction matching a-la RFC-3261 using transaction ID in branch
00317  *
00318  * Transaction matching a-la RFC-3261 using transaction ID in branch.
00319  * The function assumes there is magic cookie in branch
00320  * \param p_msg SIP message
00321  * \param trans transaction
00322  * \param skip_method SIP message method for skipping?
00323  * \return 2 if e2e ACK for a proxied transaction found, 1 if found (covers ACK for local UAS)
00324  * 0 if not found (undefined transaction)
00325  */
00326 static int matching_3261( struct sip_msg *p_msg, struct cell **trans,
00327          enum request_method skip_method)
00328 {
00329    struct cell *p_cell;
00330    struct sip_msg  *t_msg;
00331    struct via_body *via1;
00332    int is_ack;
00333    int dlg_parsed;
00334    int ret = 0;
00335    struct cell *e2e_ack_trans;
00336    int e2e_ack_ret = 0;
00337 
00338    e2e_ack_trans=0;
00339    via1=p_msg->via1;
00340    is_ack=p_msg->REQ_METHOD==METHOD_ACK;
00341    dlg_parsed=0;
00342    /* update parsed tid */
00343    via1->tid.s=via1->branch->value.s+MCOOKIE_LEN;
00344    via1->tid.len=via1->branch->value.len-MCOOKIE_LEN;
00345 
00346    for ( p_cell = get_tm_table()->entrys[p_msg->hash_index].first_cell;
00347       p_cell; p_cell = p_cell->next_cell ) 
00348    {
00349       t_msg=p_cell->uas.request;
00350       if (!t_msg) continue;  /* don't try matching UAC transactions */
00351       if (skip_method & t_msg->REQ_METHOD) continue;
00352 
00353       /* here we do an exercise which will be removed from future code
00354        * versions: we try to match end-2-end ACKs if they appear at our
00355        * server. This allows some applications bound to TM via callbacks
00356        * to correlate the e2e ACKs with transaction context, e.g., for
00357        * purpose of accounting. We think it is a bad place here, among
00358        * other things because it is not reliable. If a transaction loops
00359        * via SER the ACK can't be matched to proper INVITE transaction
00360        * (it is a separate transaction with its own branch ID) and it
00361        * matches all transaction instances in the loop dialog-wise.
00362        * Eventually, regardless to which transaction in the loop the
00363        * ACK belongs, only the first one will match.
00364        */
00365 
00366       /* dialog matching needs to be applied for ACK/200s */
00367       if (is_ack && e2e_ack_trans==0 &&
00368       p_cell->uas.status>=200 && p_cell->uas.status<300) {
00369          /* make sure we have parsed all things we need for dialog
00370           * matching */
00371          if (!dlg_parsed) {
00372             dlg_parsed=1;
00373             if (!parse_dlg(p_msg)) {
00374                LM_ERR("dlg parsing failed\n");
00375                return 0;
00376             }
00377          }
00378          ret=ack_matching(p_cell /* t w/invite */, p_msg /* ack */);
00379          if (ret>0) {
00380             e2e_ack_trans=p_cell;
00381             e2e_ack_ret = ret;
00382             continue;
00383          }
00384          /* this ACK is neither local "negative" one, nor a proxied
00385           * end-2-end one, nor an end-2-end one for a UAS transaction
00386           * -- we failed to match */
00387          continue;
00388       }
00389       /* now real tid matching occurs  for negative ACKs and any 
00390        * other requests */
00391       if (!via_matching(t_msg->via1 /* inv via */, via1 /* ack */ ))
00392          continue;
00393       /* all matched -- we found the transaction ! */
00394       LM_DBG("RFC3261 transaction matched, tid=%.*s\n",
00395          via1->tid.len, via1->tid.s);
00396 
00397       *trans=p_cell;
00398       return 1;
00399    }
00400    /* :-( ... we didn't find any */
00401    
00402    /* just check if it we found an e2e ACK previously */
00403    if (e2e_ack_trans) {
00404       *trans=e2e_ack_trans;
00405       return e2e_ack_ret;
00406    }
00407    LM_DBG("RFC3261 transaction matching failed\n");
00408    return 0;
00409 }
00410 
00411 
00412 /*!
00413  * \brief Lookup request
00414  * \return negative - transaction wasn't found, -2 for possibly e2e ACK matched
00415  * positive - transaction found
00416  */
00417 int t_lookup_request( struct sip_msg* p_msg , int leave_new_locked )
00418 {
00419    struct cell *p_cell;
00420    unsigned int isACK;
00421    struct sip_msg *t_msg;
00422    int ret, match_status;
00423    struct via_param *branch;
00424    struct cell *e2e_ack_trans;
00425 
00426    isACK = p_msg->REQ_METHOD==METHOD_ACK;
00427 
00428    if (isACK) {
00429       if (e2eack_T==NULL)
00430          return -1;
00431       if (e2eack_T!=T_UNDEFINED)
00432          return -2;
00433    }
00434 
00435    /* parse all*/
00436    if (check_transaction_quadruple(p_msg)==0) {
00437       LM_ERR("too few headers\n");
00438       set_t(0);
00439       /* stop processing */
00440       return 0;
00441    }
00442 
00443    /* start searching into the table */
00444    if (!p_msg->hash_index)
00445       p_msg->hash_index=tm_hash( p_msg->callid->body , 
00446          get_cseq(p_msg)->number ) ;
00447    LM_DBG("start searching: hash=%d, isACK=%d\n",
00448       p_msg->hash_index,isACK);
00449 
00450 
00451    /* assume not found */
00452    ret=-1;
00453    e2e_ack_trans=0;
00454 
00455    /* first of all, look if there is RFC3261 magic cookie in branch; if
00456     * so, we can do very quick matching and skip the old-RFC bizzar
00457     * comparison of many header fields
00458     */
00459    if (!p_msg->via1) {
00460       LM_ERR("no via\n");
00461       set_t(0);
00462       return 0;
00463    }
00464    branch=p_msg->via1->branch;
00465    if (branch && branch->value.s && branch->value.len>MCOOKIE_LEN
00466          && memcmp(branch->value.s,MCOOKIE,MCOOKIE_LEN)==0) {
00467       /* huhuhu! the cookie is there -- let's proceed fast */
00468       LOCK_HASH(p_msg->hash_index);
00469       match_status=matching_3261(p_msg,&p_cell, 
00470             /* skip transactions with different method; otherwise CANCEL 
00471              * would match the previous INVITE trans.  */
00472             isACK ? ~METHOD_INVITE: ~p_msg->REQ_METHOD);
00473       switch(match_status) {
00474             case 0:  goto notfound; /* no match */
00475             case 1:  goto found;    /* match */
00476             case 2:
00477                LM_DBG("RFC3261 ACK matched\n");
00478                goto e2e_ack;  /* e2e proxy ACK */
00479       }
00480    }
00481 
00482    /* ok -- it's ugly old-fashioned transaction matching -- it is
00483     * a bit simplified to be fast -- we don't do all the comparisons
00484     * of parsed uri, which was simply too bloated */
00485    LM_DBG("proceeding to pre-RFC3261 transaction matching\n");
00486 
00487    /* lock the whole entry*/
00488    LOCK_HASH(p_msg->hash_index);
00489 
00490    /* all the transactions from the entry are compared */
00491    for ( p_cell = get_tm_table()->entrys[p_msg->hash_index].first_cell;
00492         p_cell; p_cell = p_cell->next_cell ) 
00493    {
00494       t_msg = p_cell->uas.request;
00495 
00496       if (!t_msg) continue; /* skip UAC transactions */
00497 
00498       if (!isACK) {  
00499          /* compare lengths first */ 
00500          if (!EQ_LEN(callid)) continue;
00501          if (!EQ_LEN(cseq)) continue;
00502          if (!EQ_LEN(from)) continue;
00503          if (!EQ_LEN(to)) continue;
00504          if (ruri_matching && !EQ_REQ_URI_LEN) continue;
00505          if (via1_matching && !EQ_VIA_LEN(via1)) continue;
00506 
00507          /* length ok -- move on */
00508          if (!EQ_STR(callid)) continue;
00509          if (!EQ_STR(cseq)) continue;
00510          if (!EQ_STR(from)) continue;
00511          if (!EQ_STR(to)) continue;
00512          if (ruri_matching && !EQ_REQ_URI_STR) continue;
00513          if (via1_matching && !EQ_VIA_STR(via1)) continue;
00514 
00515          /* request matched ! */
00516          LM_DBG("non-ACK matched\n");
00517          goto found;
00518       } else { /* it's an ACK request*/
00519          /* ACK's relate only to INVITEs */
00520          if (t_msg->REQ_METHOD!=METHOD_INVITE) continue;
00521 
00522          /* From|To URI , CallID, CSeq # must be always there */
00523          /* compare lengths now */
00524          if (!EQ_LEN(callid)) continue;
00525          /* CSeq only the number without method ! */
00526          if (get_cseq(t_msg)->number.len!=get_cseq(p_msg)->number.len)
00527             continue;
00528          if (! EQ_LEN(from)) continue;
00529          /* To only the uri -- to many UACs screw up tags  */
00530          if (get_to(t_msg)->uri.len!=get_to(p_msg)->uri.len)
00531             continue;
00532          if (!EQ_STR(callid)) continue;
00533          if (memcmp(get_cseq(t_msg)->number.s, get_cseq(p_msg)->number.s,
00534             get_cseq(p_msg)->number.len)!=0) continue;
00535          if (!EQ_STR(from)) continue;
00536          if (memcmp(get_to(t_msg)->uri.s, get_to(p_msg)->uri.s,
00537             get_to(t_msg)->uri.len)!=0) continue;
00538 
00539          /* it is e2e ACK/200 */
00540          if (p_cell->uas.status<300) {
00541             if (e2e_ack_trans==0) {
00542                /* all criteria for proxied ACK are ok */
00543                if (p_cell->relayed_reply_branch!=-2) {
00544                   e2e_ack_trans=p_cell;
00545                   continue;
00546                }
00547                /* it's a local UAS transaction */
00548                if (dlg_matching(p_cell, p_msg))
00549                {
00550                   LM_DBG("preRFC3261 ACK matched\n");
00551                   goto e2e_ack;
00552                }
00553                continue;
00554             }
00555             continue;
00556          }
00557 
00558          /* it is not an e2e ACK/200 -- perhaps it is 
00559           * local negative case; in which case we will want
00560           * more elements to match: r-uri and via; allow
00561           * mismatching r-uri as an config option for broken
00562           * UACs */
00563          if (ruri_matching && !EQ_REQ_URI_LEN ) continue;
00564          if (via1_matching && !EQ_VIA_LEN(via1)) continue;
00565          if (ruri_matching && !EQ_REQ_URI_STR) continue;
00566          if (via1_matching && !EQ_VIA_STR(via1)) continue;
00567 
00568          /* wow -- we survived all the check! we matched! */
00569          LM_DBG("non-2xx ACK matched\n");
00570          goto found;
00571       } /* ACK */
00572    } /* synonym loop */
00573 
00574 notfound:
00575    if (e2e_ack_trans) {
00576       p_cell=e2e_ack_trans;
00577       goto e2e_ack;
00578    }
00579 
00580    /* no transaction found */
00581    set_t(0);
00582    e2eack_T = NULL;
00583    if (!leave_new_locked || isACK) {
00584       UNLOCK_HASH(p_msg->hash_index);
00585    }
00586    LM_DBG("no transaction found\n");
00587    return -1;
00588 
00589 e2e_ack:
00590    /* todo: extend second condition for * branches */
00591    if((p_cell->nr_of_outgoings==0) ||
00592          (p_cell->nr_of_outgoings==1 && (p_cell->flags&T_BLIND_UAC)))
00593    {
00594       UNLOCK_HASH(p_msg->hash_index);
00595       /* release local transaction - nothing else to do */
00596       t_release_transaction(p_cell);
00597       set_t(0);
00598       LM_DBG("local ACK found - nothing to forward\n");
00599       return -3;
00600    }
00601 
00602    REF_UNSAFE( p_cell );
00603    UNLOCK_HASH(p_msg->hash_index);
00604    e2eack_T = p_cell;
00605    set_t(0);
00606    LM_DBG("e2e proxy ACK found\n");
00607    return -2;
00608 
00609 found:
00610    set_t(p_cell);
00611    REF_UNSAFE( T );
00612    set_kr(REQ_EXIST);
00613    UNLOCK_HASH( p_msg->hash_index );
00614    LM_DBG("transaction found (T=%p)\n",T);
00615    return 1;
00616 }
00617 
00618 
00619 
00620 /*!
00621  * \brief Lookups transaction being canceled by CANCEL in p_msg
00622  *
00623  * Lookups transaction from hash table that is being canceled
00624  * by the CANCEL in the message.
00625  * \param p_msg SIP message with CANCEL
00626  * \return 0 - transaction wasn't found, T - transaction found
00627  */
00628 struct cell* t_lookupOriginalT(  struct sip_msg* p_msg )
00629 {
00630    struct cell     *p_cell;
00631    unsigned int     hash_index;
00632    struct sip_msg  *t_msg;
00633    struct via_param *branch;
00634    int ret;
00635 
00636    /* already looked for it? */
00637    if (cancelled_T!=T_UNDEFINED)
00638       return cancelled_T;
00639 
00640    /* start searching in the table */
00641    hash_index = p_msg->hash_index;
00642    LM_DBG("searching on hash entry %d\n",hash_index );
00643 
00644 
00645    /* first of all, look if there is RFC3261 magic cookie in branch; if
00646     * so, we can do very quick matching and skip the old-RFC bizzar
00647     * comparison of many header fields
00648     */
00649    if (!p_msg->via1) {
00650       LM_ERR("no via\n");
00651       cancelled_T = NULL;
00652       return 0;
00653    }
00654    branch=p_msg->via1->branch;
00655    if (branch && branch->value.s && branch->value.len>MCOOKIE_LEN
00656          && memcmp(branch->value.s,MCOOKIE,MCOOKIE_LEN)==0) {
00657       /* huhuhu! the cookie is there -- let's proceed fast */
00658       LOCK_HASH(hash_index);
00659       ret=matching_3261(p_msg, &p_cell,
00660             /* we are seeking the original transaction --
00661              * skip CANCEL transactions during search
00662              */
00663             METHOD_CANCEL);
00664       if (ret==1) goto found; else goto notfound;
00665    }
00666 
00667    /* no cookies --proceed to old-fashioned pre-3261 t-matching */
00668 
00669    LOCK_HASH(hash_index);
00670 
00671    /* all the transactions from the entry are compared */
00672    for (p_cell=get_tm_table()->entrys[hash_index].first_cell;
00673       p_cell; p_cell = p_cell->next_cell )
00674    {
00675       t_msg = p_cell->uas.request;
00676 
00677       if (!t_msg) continue; /* skip UAC transactions */
00678 
00679       /* we don't cancel CANCELs ;-) */
00680       if (t_msg->REQ_METHOD==METHOD_CANCEL)
00681          continue;
00682 
00683       /* check lengths now */ 
00684       if (!EQ_LEN(callid))
00685          continue;
00686       if (get_cseq(t_msg)->number.len!=get_cseq(p_msg)->number.len)
00687          continue;
00688       if (!EQ_LEN(from))
00689          continue;
00690 #ifdef CANCEL_TAG
00691       if (!EQ_LEN(to))
00692          continue;
00693 #else
00694       /* relaxed matching -- we don't care about to-tags anymore,
00695        * many broken UACs screw them up and ignoring them does not
00696        * actually hurt
00697        */
00698       if (get_to(t_msg)->uri.len!=get_to(p_msg)->uri.len)
00699          continue;
00700 #endif
00701       if (ruri_matching && !EQ_REQ_URI_LEN)
00702          continue;
00703       if (via1_matching && !EQ_VIA_LEN(via1))
00704          continue;
00705 
00706       /* check the content now */
00707       if (!EQ_STR(callid))
00708          continue;
00709       if (memcmp(get_cseq(t_msg)->number.s,
00710          get_cseq(p_msg)->number.s,get_cseq(p_msg)->number.len)!=0)
00711          continue;
00712       if (!EQ_STR(from))
00713          continue;
00714 #ifdef CANCEL_TAG
00715       if (!EQ_STR(to))
00716          continue;
00717 #else
00718       if (memcmp(get_to(t_msg)->uri.s, get_to(p_msg)->uri.s,
00719                get_to(t_msg)->uri.len)!=0)
00720          continue;
00721 #endif
00722       if (ruri_matching && !EQ_REQ_URI_STR)
00723          continue;
00724       if (via1_matching && !EQ_VIA_STR(via1))
00725          continue;
00726 
00727       /* found */
00728       goto found;
00729    }
00730 
00731 notfound:
00732    /* no transaction found */
00733    LM_DBG("no CANCEL matching found! \n" );
00734    UNLOCK_HASH(hash_index);
00735    cancelled_T = NULL;
00736    LM_DBG("t_lookupOriginalT completed\n");
00737    return 0;
00738 
00739 found:
00740    LM_DBG("canceled transaction found (%p)! \n",p_cell );
00741    cancelled_T = p_cell;
00742    REF_UNSAFE( p_cell );
00743    UNLOCK_HASH(hash_index);
00744    LM_DBG("t_lookupOriginalT completed\n");
00745    return p_cell;
00746 }
00747 
00748 
00749 /*!
00750  * \brief Reply matching
00751  * \return 0 - nothing found, 1  - T found
00752  */
00753 int t_reply_matching( struct sip_msg *p_msg , int *p_branch )
00754 {
00755    struct cell*  p_cell;
00756    int hash_index, entry_label, branch_id, hashl, branchl, scan_space, loopl, synl;
00757    hash_index = entry_label = branch_id = loopl = synl = 0;
00758    char  *hashi, *branchi, *p, *n, *loopi, *syni;
00759    loopi = syni = NULL;
00760    struct cseq_body *cseq;
00761 
00762    /* split the branch into pieces: loop_detection_check(ignored),
00763     hash_table_id, synonym_id, branch_id */
00764 
00765    if (!(p_msg->via1 && p_msg->via1->branch && p_msg->via1->branch->value.s))
00766       goto nomatch2;
00767 
00768    /* we do RFC 3261 tid matching and want to see first if there is
00769     * magic cookie in branch */
00770    if (p_msg->via1->branch->value.len<=MCOOKIE_LEN)
00771       goto nomatch2;
00772    if (memcmp(p_msg->via1->branch->value.s, MCOOKIE, MCOOKIE_LEN)!=0)
00773       goto nomatch2;
00774 
00775    p=p_msg->via1->branch->value.s+MCOOKIE_LEN;
00776    scan_space=p_msg->via1->branch->value.len-MCOOKIE_LEN;
00777 
00778 
00779    /* hash_id */
00780    n=eat_token2_end( p, p+scan_space, BRANCH_SEPARATOR);
00781    hashl=n-p;
00782    scan_space-=hashl;
00783    if (!hashl || scan_space<2 || *n!=BRANCH_SEPARATOR) goto nomatch2;
00784    hashi=p;
00785    p=n+1;scan_space--;
00786 
00787    if (!syn_branch) {
00788       /* md5 value */
00789       n=eat_token2_end( p, p+scan_space, BRANCH_SEPARATOR );
00790       loopl = n-p;
00791       scan_space-= loopl;
00792       if (n==p || scan_space<2 || *n!=BRANCH_SEPARATOR) 
00793          goto nomatch2;
00794       loopi=p;
00795       p=n+1; scan_space--;
00796    } else {
00797       /* synonym id */
00798       n=eat_token2_end( p, p+scan_space, BRANCH_SEPARATOR);
00799       synl=n-p;
00800       scan_space-=synl;
00801       if (!synl || scan_space<2 || *n!=BRANCH_SEPARATOR) 
00802          goto nomatch2;
00803       syni=p;
00804       p=n+1;scan_space--;
00805    }
00806 
00807    /* branch id  -  should exceed the scan_space */
00808    n=eat_token_end( p, p+scan_space );
00809    branchl=n-p;
00810    if (!branchl ) goto nomatch2;
00811    branchi=p;
00812 
00813    /* sanity check */
00814    if ((hash_index=reverse_hex2int(hashi, hashl))<0
00815       ||hash_index>=TM_TABLE_ENTRIES
00816       || (branch_id=reverse_hex2int(branchi, branchl))<0
00817       ||branch_id>=MAX_BRANCHES
00818       || (syn_branch ? (entry_label=reverse_hex2int(syni, synl))<0 
00819          : loopl!=MD5_LEN )
00820    ) {
00821       LM_DBG("poor reply labels %d label %d branch %d\n",
00822             hash_index, entry_label, branch_id );
00823       goto nomatch2;
00824    }
00825 
00826    LM_DBG("hash %d label %d branch %d\n",hash_index, entry_label, branch_id);
00827 
00828    cseq = get_cseq(p_msg);
00829 
00830    /* search the hash table list at entry 'hash_index'; lock the
00831       entry first */
00832    LOCK_HASH(hash_index);
00833 
00834    for (p_cell = get_tm_table()->entrys[hash_index].first_cell; p_cell; 
00835       p_cell=p_cell->next_cell) {
00836 
00837       /* first look if branch matches */
00838       if (syn_branch) {
00839          if (p_cell->label != entry_label) 
00840             continue;
00841       } else {
00842          if ( memcmp(p_cell->md5, loopi,MD5_LEN)!=0)
00843                continue;
00844       }
00845 
00846       /* sanity check ... too high branch ? */
00847       if ( branch_id>=p_cell->nr_of_outgoings )
00848          continue;
00849 
00850       /* does method match ? (remember -- CANCELs have the same branch
00851          as canceled transactions) */
00852       if (!( /* it's a local cancel */
00853          (cseq->method_id==METHOD_CANCEL && is_invite(p_cell)
00854             && p_cell->uac[branch_id].local_cancel.buffer.len )
00855          /* method match */
00856          || ((cseq->method_id!=METHOD_OTHER && p_cell->uas.request)?
00857             (cseq->method_id==REQ_LINE(p_cell->uas.request).method_value)
00858             :(EQ_STRS(cseq->method,p_cell->method)))
00859       ))
00860          continue;
00861 
00862       /* we passed all disqualifying factors .... the transaction has been
00863          matched !
00864       */
00865       set_t(p_cell);
00866       *p_branch = branch_id;
00867       REF_UNSAFE( T );
00868       UNLOCK_HASH(hash_index);
00869       LM_DBG("reply matched (T=%p)!\n",T);
00870       /* if this is a 200 for INVITE, we will wish to store to-tags to be
00871        * able to distinguish retransmissions later and not to call
00872        * TMCB_RESPONSE_OUT uselessly; we do it only if callbacks are
00873        * enabled -- except callback customers, nobody cares about 
00874        * retransmissions of multiple 200/INV or ACK/200s
00875        */
00876       if (is_invite(p_cell) && p_msg->REPLY_STATUS>=200 
00877       && p_msg->REPLY_STATUS<300 
00878       && ( (!is_local(p_cell) &&
00879             has_tran_tmcbs(p_cell,
00880             TMCB_RESPONSE_PRE_OUT|TMCB_RESPONSE_OUT|TMCB_E2EACK_IN) )
00881          || (is_local(p_cell)&&has_tran_tmcbs(p_cell,TMCB_LOCAL_COMPLETED))
00882       )) {
00883          if (parse_headers(p_msg, HDR_TO_F, 0)==-1) {
00884             LM_ERR("to parsing failed\n");
00885          }
00886       }
00887       if (!is_local(p_cell)) {
00888          run_trans_callbacks( TMCB_RESPONSE_IN, T, T->uas.request, p_msg,
00889             p_msg->REPLY_STATUS);
00890       }
00891       return 1;
00892    } /* for cycle */
00893 
00894    /* nothing found */
00895    UNLOCK_HASH(hash_index);
00896    LM_DBG("no matching transaction exists\n");
00897 
00898 nomatch2:
00899    LM_DBG("failure to match a transaction\n");
00900    *p_branch = -1;
00901    set_t(0);
00902    return -1;
00903 }
00904 
00905 
00906 
00907 
00908 /*!
00909  * \brief Determine current transaction
00910  *
00911  * Determine current transaction for SIP message.
00912  * Set T to a ptr if the transaction could be found, to 0 if not found,
00913  * and to T_UNDEFINED on errors:
00914  * Found      Not Found     Error (e.g. parsing)
00915  * Return Value     1          0             -1
00916  * T                ptr        0             T_UNDEFINED
00917  * \return 1 if transaction found, 0 if not found, -1 on errors (e.g. parsing)
00918  */
00919 int t_check( struct sip_msg* p_msg , int *param_branch )
00920 {
00921    int local_branch;
00922 
00923    /* is T still up-to-date ? */
00924    LM_DBG("start=%p\n", T);
00925    if ( T==T_UNDEFINED )
00926    {
00927       /* transaction lookup */
00928       if ( p_msg->first_line.type==SIP_REQUEST ) {
00929          /* force parsing all the needed headers*/
00930          if (parse_headers(p_msg, HDR_EOH_F, 0 )==-1) {
00931             LM_ERR("parsing error\n");
00932             return -1;
00933          }
00934          /* in case, we act as UAS for INVITE and reply with 200,
00935           * we will need to run dialog-matching for subsequent
00936           * ACK, for which we need From-tag; We also need from-tag
00937           * in case people want to have proxied e2e ACKs accounted
00938           */
00939          if (p_msg->REQ_METHOD==METHOD_INVITE 
00940                      && parse_from_header(p_msg)<0) {
00941             LM_ERR("from parsing failed\n");
00942             return -1;
00943          }
00944          t_lookup_request( p_msg , 0 /* unlock before returning */ );
00945       } else {
00946          /* we need Via for branch and Cseq method to distinguish
00947             replies with the same branch/cseqNr (CANCEL)
00948          */
00949          if ( parse_headers(p_msg, HDR_VIA1_F|HDR_CSEQ_F, 0 )==-1
00950          || !p_msg->via1 || !p_msg->cseq ) {
00951             LM_ERR("reply cannot be parsed\n");
00952             return -1;
00953          }
00954 
00955          /* if that is an INVITE, we will also need to-tag
00956             for later ACK matching */
00957          if ( get_cseq(p_msg)->method_id==METHOD_INVITE ) {
00958                if (parse_headers(p_msg, HDR_TO_F, 0)==-1 || !p_msg->to)  {
00959                   LM_ERR("INVITE reply cannot be parsed\n");
00960                   return -1;
00961                }
00962          }
00963 
00964          t_reply_matching( p_msg ,
00965             param_branch!=0?param_branch:&local_branch );
00966 
00967       }
00968 #ifdef EXTRA_DEBUG
00969       if ( T && T!=T_UNDEFINED && T->damocles) {
00970          LM_ERR("transaction %p scheduled for deletion "
00971             "and called from t_check\n", T);
00972          abort();
00973       }
00974 #endif
00975       LM_DBG("end=%p\n",T);
00976    } else {
00977       if (T)
00978          LM_DBG("transaction already found!\n");
00979       else
00980          LM_DBG("transaction previously sought and not found\n");
00981    }
00982 
00983    return T ? (T==T_UNDEFINED ? -1 : 1 ) : 0;
00984 }
00985 
00986 int init_rb( struct retr_buf *rb, struct sip_msg *msg)
00987 {
00988    /*struct socket_info* send_sock;*/
00989    struct via_body* via;
00990    int proto;
00991 
00992    via=msg->via1;
00993    if (!reply_to_via) {
00994       update_sock_struct_from_ip( &rb->dst.to, msg );
00995       proto=msg->rcv.proto;
00996    } else {
00997       /*init retrans buffer*/
00998       if (update_sock_struct_from_via( &(rb->dst.to), msg, via )==-1) {
00999          LM_ERR("init_rb: cannot lookup reply dst: %.*s\n",
01000             via->host.len, via->host.s );
01001          ser_error=E_BAD_VIA;
01002          return 0;
01003       }
01004       proto=via->proto;
01005    }
01006    rb->dst.proto=proto;
01007    rb->dst.proto_reserved1=msg->rcv.proto_reserved1;
01008    /* use for sending replies the incoming interface of the request -bogdan */
01009    rb->dst.send_sock=msg->rcv.bind_address;
01010    return 1;
01011 }
01012 
01013 /*!
01014  * \brief Initialize a new transaction for a message
01015  *
01016  * Initialize a new transaction for a message in shared memory.
01017  * \param new_cell new transaction
01018  * \param p_msg SIP message
01019  */
01020 static inline void init_new_t(struct cell *new_cell, struct sip_msg *p_msg)
01021 {
01022    struct sip_msg *shm_msg;
01023 
01024    shm_msg=new_cell->uas.request;
01025    new_cell->from.s=shm_msg->from->name.s;
01026    new_cell->from.len=HF_LEN(shm_msg->from);
01027    new_cell->to.s=shm_msg->to->name.s;
01028    new_cell->to.len=HF_LEN(shm_msg->to);
01029    new_cell->callid.s=shm_msg->callid->name.s;
01030    new_cell->callid.len=HF_LEN(shm_msg->callid);
01031    new_cell->cseq_n.s=shm_msg->cseq->name.s;
01032    new_cell->cseq_n.len=get_cseq(shm_msg)->number.s
01033       +get_cseq(shm_msg)->number.len
01034       -shm_msg->cseq->name.s;
01035 
01036    new_cell->method=new_cell->uas.request->first_line.u.request.method;
01037    if (p_msg->REQ_METHOD==METHOD_INVITE) new_cell->flags |= T_IS_INVITE_FLAG;
01038    new_cell->on_negative=get_on_negative();
01039    new_cell->on_reply=get_on_reply();
01040    new_cell->on_branch=get_on_branch();
01041 }
01042 
01043 
01044 /*!
01045  * \brief Create a new transaction
01046  * \return 1 on success, negative on errors
01047  */
01048 static inline int new_t(struct sip_msg *p_msg)
01049 {
01050    struct cell *new_cell;
01051 
01052    /* for ACK-dlw-wise matching, we want From-tags */
01053    if (p_msg->REQ_METHOD==METHOD_INVITE && parse_from_header(p_msg)<0) {
01054          LM_ERR("no valid From in INVITE\n");
01055          return E_BAD_REQ;
01056    }
01057    /* make sure uri will be parsed before cloning */
01058    if (parse_sip_msg_uri(p_msg)<0) {
01059       LM_ERR("uri invalid\n");
01060       return E_BAD_REQ;
01061    }
01062 
01063    /* add new transaction */
01064    new_cell = build_cell( p_msg ) ;
01065    if  ( !new_cell ){
01066       LM_ERR("out of mem\n");
01067       return E_OUT_OF_MEM;
01068    } 
01069 
01070    insert_into_hash_table_unsafe( new_cell, p_msg->hash_index );
01071    set_t(new_cell);
01072    INIT_REF_UNSAFE(T);
01073    /* init pointers to headers needed to construct local
01074       requests such as CANCEL/ACK
01075    */
01076    init_new_t(new_cell, p_msg);
01077    return 1;
01078 }
01079 
01080 /*!
01081  * \brief Atomic "new_tran" construct
01082  * \return negative on error, 1 if a request did not match a transaction
01083  * - it that was an ack, the calling function shall forward statelessly
01084  * - otherwise it means, a new transaction was introduced and the calling function
01085  * shall reply/relay/whatever_appropriate
01086  * 0 on retransmission
01087  */
01088 int t_newtran( struct sip_msg* p_msg )
01089 {
01090    int lret, my_err;
01091 
01092    /* is T still up-to-date ? */
01093    LM_DBG("transaction on entrance=%p\n",T);
01094 
01095    if ( T && T!=T_UNDEFINED  ) {
01096       LM_ERR("transaction already in process %p\n", T );
01097       return E_SCRIPT;
01098    }
01099 
01100    T = T_UNDEFINED;
01101    /* first of all, parse everything -- we will store in shared memory 
01102       and need to have all headers ready for generating potential replies 
01103       later; parsing later on demand is not an option since the request 
01104       will be in shmem and applying parse_headers to it would intermix 
01105       shmem with pkg_mem
01106    */
01107 
01108    if (parse_headers(p_msg, HDR_EOH_F, 0 )<0) {
01109       LM_ERR("parse_headers failed\n");
01110       return E_BAD_REQ;
01111    }
01112    if ((p_msg->parsed_flag & HDR_EOH_F)!=HDR_EOH_F) {
01113          LM_ERR("EoH not parsed\n");
01114          return E_OUT_OF_MEM;
01115    }
01116    /* auto update internal flags for detecting changes in important hdrs
01117     * headers already parsed to EOH */
01118    check_hdrs_changes(p_msg);
01119 
01120    /* t_lookup_requests attempts to find the transaction; 
01121       it also calls check_transaction_quadruple -> it is
01122       safe to assume we have from/callid/cseq/to
01123    */ 
01124    lret = t_lookup_request( p_msg, 1 /* leave locked if not found */ );
01125 
01126    /* on error, pass the error in the stack ... nothing is locked yet
01127       if 0 is returned */
01128    if (lret==0) return E_BAD_TUPEL;
01129 
01130    /* transaction found, it's a retransmission  */
01131    if (lret>0) {
01132       if (p_msg->REQ_METHOD==METHOD_ACK) {
01133          t_release_transaction(T);
01134       } else {
01135          t_retransmit_reply(T);
01136       }
01137       /* things are done -- return from script */
01138       return 0;
01139    }
01140 
01141    /* from now on, be careful -- hash table is locked */
01142 
01143    if (lret==-2) { /* was it an e2e ACK ? if so, trigger a callback */
01144       LM_DBG("building branch for end2end ACK\n");
01145       /* to ensure unigueness acros time and space, compute the ACK 
01146        * branch in the same maner as for INVITE, but put a t->branch 
01147        * value that cannot exist for that INVITE - as it is compute as 
01148        * an INVITE, it will not overlapp with other INVITEs or requests.
01149        * But the faked value for t->branch guarantee no overalap with 
01150        * corresponding INVITE  --bogdan */
01151       if (!t_calc_branch(e2eack_T, e2eack_T->nr_of_outgoings+1,
01152       p_msg->add_to_branch_s, &p_msg->add_to_branch_len )) {
01153          LM_ERR("ACK branch computation failed\n");
01154       }
01155 
01156       /* no callbacks? complete quickly */
01157       if ( !has_tran_tmcbs(e2eack_T,TMCB_E2EACK_IN) )
01158          return 1;
01159 
01160       /* we don't call from within REPLY_LOCK -- that introduces
01161        * a race condition; however, it is so unlikely and the
01162        * impact is so small (callback called multiple times of
01163        * multiple ACK/200s received in parallel), that we do not
01164        * better waste time in locks  */
01165       if (unmatched_totag(e2eack_T, p_msg)) {
01166          run_trans_callbacks( TMCB_E2EACK_IN , e2eack_T, p_msg, 0,
01167             -p_msg->REQ_METHOD );
01168       }
01169       return 1;
01170    }
01171 
01172    /* transaction not found, it's a new request (lret<0, lret!=-2);
01173       establish a new transaction ... */
01174    if (p_msg->REQ_METHOD==METHOD_ACK) /* ... unless it is in ACK */
01175       return 1;
01176 
01177    my_err=new_t(p_msg);
01178    if (my_err<0) {
01179       LM_ERR("new_t failed\n");
01180       goto new_err;
01181    }
01182 
01183 
01184    UNLOCK_HASH(p_msg->hash_index);
01185    /* now, when the transaction state exists, check if
01186       there is a meaningful Via and calculate it; better
01187       do it now than later: state is established so that
01188       subsequent retransmissions will be absorbed and will
01189      not possibly block during Via DNS resolution; doing
01190       it later would only burn more CPU as if there is an
01191       error, we cannot relay later whatever comes out of the
01192       the transaction 
01193    */
01194    if (!init_rb( &T->uas.response, p_msg)) {
01195       LM_ERR("unresolvable via1\n");
01196       put_on_wait( T );
01197       t_unref(p_msg);
01198       return E_BAD_VIA;
01199    }
01200 
01201    return 1;
01202 new_err:
01203    UNLOCK_HASH(p_msg->hash_index);
01204    return my_err;
01205 
01206 }
01207 
01208 /*!
01209  * \brief Unreference a transaction, release it if necessary
01210  * \return 1 on success, -1 on errors
01211  */
01212 int t_unref( struct sip_msg* p_msg  )
01213 {
01214    enum kill_reason kr;
01215 
01216    if (T==T_UNDEFINED)
01217       return -1;
01218    if (T!=T_NULL_CELL) {
01219       if (p_msg->first_line.type==SIP_REQUEST){
01220          kr=get_kr();
01221          if (kr==0 
01222                ||(p_msg->REQ_METHOD==METHOD_ACK && !(kr & REQ_RLSD))) {
01223             LM_WARN("script writer didn't release transaction\n");
01224             t_release_transaction(T);
01225          }
01226       }
01227       UNREF( T );
01228    }
01229    set_t(T_UNDEFINED);
01230    return 1;
01231 }
01232 
01233 
01234 void t_unref_cell(struct cell *c)
01235 {
01236    UNREF(c);
01237 }
01238 
01239 
01240 /*!
01241  * \brief Get transaction identifier
01242  * \return 1 on success, -1 if not found and on errors
01243  */
01244 int t_get_trans_ident(struct sip_msg* p_msg, unsigned int* hash_index,
01245                                              unsigned int* label)
01246 {
01247    struct cell* t;
01248    if(t_check(p_msg,0) != 1){
01249       LM_ERR("no transaction found\n");
01250       return -1;
01251    }
01252    t = get_t();
01253    if(!t){
01254       LM_ERR("transaction found is NULL\n");
01255       return -1;
01256    }
01257 
01258    *hash_index = t->hash_index;
01259    *label = t->label;
01260 
01261    return 1;
01262 }
01263 
01264 
01265 /*!
01266  * \brief Lookup a transaction from a hash identifier
01267  * \return 1 on success, -1 if not found, and on errors
01268  */
01269 int t_lookup_ident(struct cell ** trans, unsigned int hash_index,
01270                                              unsigned int label)
01271 {
01272    struct cell* p_cell;
01273 
01274    if(hash_index >= TM_TABLE_ENTRIES){
01275       LM_ERR("invalid hash_index=%u\n",hash_index);
01276       return -1;
01277    }
01278 
01279    LOCK_HASH(hash_index);
01280 
01281    /* all the transactions from the entry are compared */
01282    for ( p_cell = get_tm_table()->entrys[hash_index].first_cell;
01283       p_cell; p_cell = p_cell->next_cell ) 
01284    {
01285       if(p_cell->label == label){
01286          REF_UNSAFE(p_cell);
01287          UNLOCK_HASH(hash_index);
01288          set_t(p_cell);
01289          *trans=p_cell;
01290          LM_DBG("transaction found\n");
01291          return 1;
01292       }
01293    }
01294 
01295    UNLOCK_HASH(hash_index);
01296    set_t(0);
01297    *trans=p_cell;
01298 
01299    LM_DBG("transaction not found\n");
01300    return -1;
01301 }
01302 
01303 
01304 /*!
01305  * \brief Small wrapper around is_local
01306  * \return 1 on success, -1 if transaction not found and on errors
01307  */
01308 int t_is_local(struct sip_msg* p_msg)
01309 {
01310    struct cell* t;
01311    if(t_check(p_msg,0) != 1){
01312       LM_ERR("no transaction found\n");
01313       return -1;
01314    }
01315    t = get_t();
01316    if(!t){
01317       LM_ERR("transaction found is NULL\n");
01318       return -1;
01319    }
01320 
01321    return is_local(t);
01322 }
01323 
01324 
01325 
01326 /*!
01327  * \brief Lookup a transaction by callid and cseq.
01328  *
01329  * Lookup a transaction by callid and cseq, parameters are pure
01330  * header field content only, e.g. "123@10.0.0.1" and "11"
01331  * \param trans transaction
01332  * \param callid Call-Id value
01333  * \param cseq CSEQ value
01334  * \return 1 if transaction was found, -1 when not found and on errors
01335  */
01336 int t_lookup_callid(struct cell ** trans, str callid, str cseq) {
01337    struct cell* p_cell;
01338    unsigned int hash_index;
01339 
01340    /* I use MAX_HEADER, not sure if this is a good choice... */
01341    char callid_header[MAX_HEADER], cseq_header[MAX_HEADER];
01342    /* save return value of print_* functions here */
01343    char* endpos;
01344 
01345    /* need method, which is always INVITE in our case */
01346    /* CANCEL is only useful after INVITE */
01347    str invite_method;
01348    char* invite_string = INVITE;
01349    
01350    invite_method.s = invite_string;
01351    invite_method.len = INVITE_LEN;
01352    
01353    /* lookup the hash index where the transaction is stored */
01354    hash_index=tm_hash(callid, cseq);
01355 
01356    if(hash_index >= TM_TABLE_ENTRIES){
01357       LM_ERR("invalid hash_index=%u\n",hash_index);
01358       return -1;
01359    }
01360 
01361    /* create header fields the same way tm does itself, then compare headers */
01362    endpos = print_callid_mini(callid_header, callid);
01363    LM_DBG("created comparable call_id header field: >%.*s<\n",
01364          (int)(endpos - callid_header), callid_header); 
01365 
01366    endpos = print_cseq_mini(cseq_header, &cseq, &invite_method);
01367    LM_DBG("created comparable cseq header field: >%.*s<\n",
01368          (int)(endpos - cseq_header), cseq_header); 
01369 
01370    LOCK_HASH(hash_index);
01371 
01372    /* all the transactions from the entry are compared */
01373    p_cell = get_tm_table()->entrys[hash_index].first_cell;
01374    for ( ; p_cell; p_cell = p_cell->next_cell ) {
01375 
01376       /* compare complete header fields, casecmp to make sure invite=INVITE */
01377       LM_DBG(" <%.*s>  <%.*s>\n", p_cell->callid.len, p_cell->callid.s,
01378          p_cell->cseq_n.len,p_cell->cseq_n.s);
01379       if ( (strncmp(callid_header, p_cell->callid.s, p_cell->callid.len) == 0)
01380          && (strncasecmp(cseq_header, p_cell->cseq_n.s, p_cell->cseq_n.len) == 0) ) {
01381          LM_DBG("we have a match: callid=>>%.*s<< cseq=>>%.*s<<\n",
01382             p_cell->callid.len,p_cell->callid.s, p_cell->cseq_n.len,
01383             p_cell->cseq_n.s);
01384          REF_UNSAFE(p_cell);
01385          UNLOCK_HASH(hash_index);
01386          set_t(p_cell);
01387          *trans=p_cell;
01388          LM_DBG("transaction found.\n");
01389          return 1;
01390       }
01391       LM_DBG("NO match: callid=%.*s cseq=%.*s\n",
01392          p_cell->callid.len, p_cell->callid.s,
01393          p_cell->cseq_n.len, p_cell->cseq_n.s);
01394    }
01395 
01396    UNLOCK_HASH(hash_index);
01397    LM_DBG("transaction not found.\n");
01398 
01399    return -1;
01400 }

Generated on Thu May 24 20:00:32 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6