h_table.h

Go to the documentation of this file.
00001 /*
00002  * $Id: h_table.h 5666 2009-03-03 12:13:46Z henningw $
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-03-16  removed _TOTAG (jiri)
00025  * 2003-03-06  we keep a list of 200/INV to-tags now (jiri)
00026  * 2003-03-01  kr set through a function now (jiri)
00027  * 2003-12-04  callbacks per transaction added; completion callback
00028  *             merge into them as LOCAL_COMPETED (bogdan)
00029  * 2004-02-11  FIFO/CANCEL + alignments (hash=f(callid,cseq)) (uli+jiri)
00030  * 2004-02-13  t->is_invite, t->local, t->noisy_ctimer replaced
00031  *             with flags (bogdan)
00032  * 2004-08-23  avp support added - avp list linked in transaction (bogdan)
00033  * 2007-01-25  DNS failover at transaction level added (bogdan) 
00034  */
00035 
00036 /*! \file
00037  * \brief TM :: Hash tables
00038  *
00039  * \ingroup tm
00040  * - Module: \ref tm
00041  */
00042 
00043 #ifndef _H_TABLE_H
00044 #define _H_TABLE_H
00045 
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 
00049 #include "../../parser/msg_parser.h"
00050 #include "../../proxy.h"
00051 #include "../../md5utils.h"
00052 #include "../../usr_avp.h"
00053 #include "config.h"
00054 
00055 struct s_table;
00056 struct entry;
00057 struct cell;
00058 struct timer;
00059 struct retr_buf;
00060 
00061 #include "../../mem/shm_mem.h"
00062 #include "lock.h"
00063 #include "sip_msg.h"
00064 #include "t_reply.h"
00065 #include "t_hooks.h"
00066 #include "timer.h"
00067 
00068 #define LOCK_HASH(_h) lock_hash((_h))
00069 #define UNLOCK_HASH(_h) unlock_hash((_h))
00070 
00071 void lock_hash(int i);
00072 void unlock_hash(int i);
00073 
00074 
00075 #define NO_CANCEL       ( (char*) 0 )
00076 #define EXTERNAL_CANCEL ( (char*) -1)
00077 
00078 #define TYPE_LOCAL_CANCEL -1
00079 #define TYPE_REQUEST       0
00080 
00081 /*! \brief to be able to assess whether a script writer forgot to
00082    release a transaction and leave it for ever in memory,
00083    we mark it with operations done over it; if none of these
00084    flags is set and script is being left, it is a sign of
00085    script error and we need to release on writer's
00086    behalf
00087 
00088    - REQ_FWDED means there is a UAC with final response timer
00089              ticking. If it hits, transaction will be completed.
00090    - REQ_RPLD means that a transaction has been replied -- either
00091             it implies going to wait state, or for invite transactions
00092             FR timer is ticking until ACK arrives
00093    - REQ_RLSD means that a transaction was put on wait explicitly
00094             from t_release_transaction
00095    - REQ_EXIST means that this request is a retransmission which does not
00096             affect transactional state
00097 */
00098 enum kill_reason { REQ_FWDED=1, REQ_RPLD=2, REQ_RLSD=4, REQ_EXIST=8 };
00099 
00100 typedef void (*setkr_f)(enum kill_reason mykr);
00101 
00102 typedef struct retr_buf
00103 {
00104    int activ_type;
00105    /* set to status code if the buffer is a reply,
00106    0 if request or -1 if local CANCEL */
00107 
00108    str buffer;
00109    
00110    struct dest_info dst;
00111 
00112    /* a message can be linked just to retransmission and FR list */
00113    struct timer_link retr_timer;
00114    struct timer_link fr_timer;
00115    enum lists retr_list;
00116 
00117    /*the cell that contains this retrans_buff*/
00118    struct cell* my_T;
00119    unsigned int branch;
00120 } retr_buf_type;
00121 
00122 
00123 
00124 /*! \brief User Agent Server content */
00125 typedef struct ua_server
00126 {
00127    struct sip_msg   *request;
00128    char             *end_request;
00129    struct retr_buf  response;
00130    unsigned int     status;
00131    /* keep to-tags for local 200 replies for INVITE -- 
00132     * we need them for dialog-wise matching of ACKs;
00133     * the pointer shows to shmem-ed reply */
00134    str              local_totag;
00135 }ua_server_type;
00136 
00137 
00138 
00139 /*! \brief User Agent Client content */
00140 typedef struct ua_client
00141 {
00142    struct retr_buf  request;
00143    struct proxy_l   *proxy;
00144    /* we maintain a separate copy of cancel rather than
00145       reuse the structure for original request; the 
00146       original request is no longer needed but its delayed
00147       timer may fire and interfere with whoever tries to
00148       rewrite it */
00149    struct retr_buf local_cancel;
00150    /* pointer to retransmission buffer where uri is printed;
00151       good for generating ACK/CANCEL */
00152    str              uri;
00153    /* the path vector used for this branch */
00154    str              path_vec;
00155    /* number of RR headers that were locally added for this branch */
00156    unsigned int     added_rr;
00157    /* if we store a reply (branch picking), this is where it is */
00158    struct sip_msg  *reply;
00159    /* if we don't store, we at least want to know the status */
00160    short            last_received;
00161    /* UAC specific flags */
00162    short            flags;
00163    /* script flags, specific to this branch */
00164    int              br_flags;
00165 }ua_client_type;
00166 
00167 
00168 struct totag_elem {
00169    str tag;
00170    short acked;
00171    struct totag_elem *next;
00172 };
00173 
00174 
00175 
00176 /*! \name transaction flags */
00177 /*@{*/
00178 #define T_IS_INVITE_FLAG        (1<<0) /*!< is the transaction's request an INVITE? */
00179 #define T_IS_LOCAL_FLAG         (1<<1) /*!< is this a transaction generated by local request? */
00180 #define T_WAS_CANCELLED_FLAG    (1<<3) /*!< set to one if you want to disallow silent transaction
00181                      dropping when C timer hits */
00182 #define T_HOPBYHOP_CANCEL_FLAG  (1<<4)    /*!< transaction was cancelled hopbyhop */
00183 #define T_NO_AUTOACK_FLAG       (1<<5) /*!< ACK must not be auto generated for the local transaction */
00184 #define T_PASS_PROVISIONAL_FLAG (1<<6) /*!< provisional replies must trigger callbacks for local transaction */
00185 #define T_NO_DNS_FAILOVER_FLAG  (1<<7) /*!< do auto DNS failover  */
00186 #define T_NO_NEW_BRANCHES_FLAG  (1<<8) /*!< transaction must not create new branches  */
00187 #define T_BLIND_UAC             (1<<9) /*!< transaction has a blind uac  */
00188 
00189 /* transaction UAC's flags */
00190 #define T_UAC_TO_CANCEL_FLAG  (1<<0)   /*!< UAC :: is the UAC pending for CANCEL ?  */
00191 #define T_UAC_HAS_RECV_REPLY  (1<<1)   /*!< UAC :: have the UAC received any replies?  */
00192 
00193 /*@}*/
00194 
00195 
00196 /*! \brief transaction context */
00197 typedef struct cell
00198 {
00199    /* linking data */
00200    struct cell*     next_cell;
00201    struct cell*     prev_cell;
00202    unsigned int  hash_index;  /*!< tells in which hash table entry the cell lives */
00203    unsigned int  label;    /*!< sequence number within hash collision slot */
00204    unsigned int flags;     /*!< different information about the transaction */
00205 
00206    /*! \brief how many processes are currently processing this transaction? 
00207 
00208       note that only processes working on a request/reply belonging
00209       to a transaction increase ref_count -- timers don't, since we
00210       rely on transaction state machine to clean-up all but wait timer
00211       when entering WAIT state and the wait timer is the only place
00212       from which a transaction can be deleted (if ref_count==0); good
00213       for protecting from conditions in which wait_timer hits and
00214       tries to delete a transaction whereas at the same time 
00215       a delayed message belonging to the transaction is received */
00216    volatile unsigned int ref_count;
00217 
00218    /*! \brief needed for generating local ACK/CANCEL for local
00219       transactions
00220 
00221       all but cseq_n include the entire
00222       header field value, cseq_n only Cseq number; with
00223       local transactions, pointers point to outbound buffer,
00224       with proxied transactions to inbound request */
00225    str from, callid, cseq_n, to;
00226 
00227    /*! \brief method shortcut 
00228 
00229       for local transactions, pointer to
00230       outbound buffer, for proxies transactions pointer to
00231       original message; needed for reply matching */
00232    str method;
00233 
00234    struct tmcb_head_list tmcb_hl;      /*!< head of callback list */
00235 
00236    struct timer_link wait_tl;    /*!< binding to wait timer */
00237    struct timer_link dele_tl;    /*!< binding to delete timer */
00238 
00239    int first_branch;    /*!< first branch - when serial forking is performed, keeps the first
00240                         branch for each step ; it allows proper branch selection */
00241    int nr_of_outgoings;       /*!< number of forks */
00242    int relayed_reply_branch;  /*!< nr of replied branch; 0..MAX_BRANCHES=branch value,
00243                     -1 no reply, -2 local reply */
00244    struct ua_server  uas;        /*!< UA Server */
00245    struct ua_client  uac[ MAX_BRANCHES ]; /*!< UA Clients */
00246 
00247    ser_lock_t   reply_mutex;     /*!< protection against concurrent reply processing */
00248 
00249    unsigned int on_negative;     /*!< the route to take if no final positive reply arrived */
00250    unsigned int on_reply;        /*!< the onreply_route to be processed if registered to do so */
00251    unsigned int on_branch;       /*!< the branch_route to be processed separately for each branch */
00252 
00253    char md5[MD5_LEN];         /*!< MD5checksum  (meaningful only if syn_branch=0) */
00254 
00255 #ifdef   EXTRA_DEBUG
00256    short damocles;            /*!< scheduled for deletion ? */
00257 #endif
00258 
00259    /*! \brief  to-tags of 200/INVITEs which were received from downstream and 
00260     * forwarded or passed to UAC; note that there can be arbitrarily 
00261     * many due to downstream forking; */
00262    struct totag_elem *fwded_totags;
00263 
00264    struct usr_avp *user_avps;    /*!< list with user avp */
00265 
00266    void *dialog_ctx;       /*!< holders for higher contexts */
00267 } cell_type;
00268 
00269 
00270 
00271 /*! \brief double-linked list of cells with hash synonyms */
00272 typedef struct entry
00273 {
00274    struct cell*    first_cell;
00275    struct cell*    last_cell;
00276    /* currently highest sequence number in a synonym list */
00277    unsigned int    next_label;
00278    /* sync mutex */
00279    ser_lock_t      mutex;
00280    unsigned long acc_entries;
00281    unsigned long cur_entries;
00282 }entry_type;
00283 
00284 
00285 
00286 /*! \brief transaction table */
00287 struct s_table
00288 {
00289    /*! \brief table of hash entries; each of them is a list of synonyms  */
00290    struct entry   entrys[ TM_TABLE_ENTRIES ];
00291 };
00292 
00293 
00294 #define list_entry(ptr, type, member) \
00295    ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
00296 
00297 #define get_retr_timer_payload(_tl_) \
00298    list_entry( _tl_, struct retr_buf, retr_timer)
00299 #define get_fr_timer_payload(_tl_) \
00300    list_entry( _tl_, struct retr_buf, fr_timer)
00301 #define get_wait_timer_payload(_tl_) \
00302    list_entry( _tl_, struct cell, wait_tl)
00303 #define get_dele_timer_payload(_tl_) \
00304    list_entry( _tl_, struct cell, dele_tl)
00305 
00306 #define get_T_from_reply_rb(_rb_) \
00307    list_entry( list_entry( _rb_, (struct ua_server), response),\
00308       struct cell, uas)
00309 #define get_T_from_request_rb(_rb_, _br_) \
00310    list_entry( list_entry( (rb_, (struct ua_client), request) - \
00311       (_br_)*sizeof(struct retr_buf), struct cell, uas)
00312 #define get_T_from_cancel_rb(_rb_, _br_) \
00313    list_entry( list_entry( (rb_, (struct ua_client), local_cancel) - \
00314       (_br_)*sizeof(struct retr_buf), struct cell, uas)
00315 
00316 #define is_invite(_t_)           ((_t_)->flags&T_IS_INVITE_FLAG)
00317 #define is_local(_t_)            ((_t_)->flags&T_IS_LOCAL_FLAG)
00318 #define was_cancelled(_t_)       ((_t_)->flags&T_WAS_CANCELLED_FLAG)
00319 #define is_hopbyhop_cancel(_t_)  ((_t_)->flags&T_HOPBYHOP_CANCEL_FLAG)
00320 #define no_autoack(_t_)          ((_t_)->flags&T_NO_AUTOACK_FLAG)
00321 #define pass_provisional(_t_)    ((_t_)->flags&T_PASS_PROVISIONAL_FLAG)
00322 #define no_new_branches(_t_)     ((_t_)->flags&T_NO_NEW_BRANCHES_FLAG)
00323 
00324 
00325 extern int syn_branch;
00326 
00327 
00328 void reset_kr(void);
00329 void set_kr( enum kill_reason kr );
00330 enum kill_reason get_kr(void);
00331 
00332 struct s_table* get_tm_table(void);
00333 struct s_table* init_hash_table(void);
00334 void   free_hash_table(void);
00335 void   free_cell( struct cell* dead_cell );
00336 struct cell*  build_cell( struct sip_msg* p_msg );
00337 void   remove_from_hash_table_unsafe( struct cell * p_cell);
00338 #ifdef OBSOLETED
00339 void   insert_into_hash_table( struct cell * p_cell, unsigned int _hash);
00340 #endif
00341 void   insert_into_hash_table_unsafe( struct cell * p_cell, unsigned int _hash );
00342 
00343 unsigned int transaction_count(void);
00344 
00345 /* Unix socket variant */
00346 int unixsock_hash(str* msg);
00347 
00348 #endif
00349 
00350 

Generated on Wed May 23 06:00:45 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6