t_hooks.h

00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of ser, a free SIP server.
00007  *
00008  * ser 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  * For a license to use the ser software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * ser is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  *
00027  * History:
00028  * --------
00029  * 2003-03-16 : backwards-compatibility callback names introduced (jiri)
00030  * 2003-03-06 : old callbacks renamed, new one introduced (jiri)
00031  * 2003-12-04 : global callbacks moved into transaction callbacks;
00032  *              multiple events per callback added; single list per
00033  *              transaction for all its callbacks (bogdan)
00034  * 2007-03-14   added *_SENT callbacks (andrei)
00035  * 2007-03-17   added TMCB_NEG_ACK_IN, TMCB_REQ_RETR_IN & 
00036  *               TMCB_LOCAL_RESPONSE_IN (andrei)
00037  * 2007-03-23   added TMCB_LOCAL_REQUEST_IN (andrei)
00038  * 2007-05-16   added TMCB_DESTROY (andrei)
00039  * 2007-05-24   fixed has_tran_tmcbs() & added TMCB_E2ECANCEL_IN (andrei)
00040  * 2007-11-12   added TMCB_E2EACK_RETR_IN (andrei)
00041  */
00042 
00043 
00044 #ifndef _HOOKS_H
00045 #define _HOOKS_H
00046 
00047 #include "defs.h"
00048 
00049 /* TMCB_ONSEND used to enable certain callback-related features when
00050  * ONSEND was set, these days it's always enabled. For compatibility
00051  * reasons with modules that check ONSEND, continue to set it
00052  * unconditionally*/
00053 #define TMCB_ONSEND
00054 #include "../../ip_addr.h" /* dest_info */
00055 
00056 struct sip_msg;
00057 struct cell;
00058 
00059 #define TMCB_REQUEST_IN_N       0
00060 #define TMCB_RESPONSE_IN_N      1
00061 #define TMCB_E2EACK_IN_N        2
00062 #define TMCB_REQUEST_PENDING_N  3
00063 #define TMCB_REQUEST_FWDED_N    4
00064 #define TMCB_RESPONSE_FWDED_N   5
00065 #define TMCB_ON_FAILURE_RO_N    6
00066 #define TMCB_ON_FAILURE_N       7
00067 #define TMCB_REQUEST_OUT_N      8
00068 #define TMCB_RESPONSE_OUT_N     9
00069 #define TMCB_LOCAL_COMPLETED_N  10
00070 #define TMCB_LOCAL_RESPONSE_OUT_N 11
00071 #define TMCB_ACK_NEG_IN_N       12
00072 #define TMCB_REQ_RETR_IN_N      13
00073 #define TMCB_LOCAL_RESPONSE_IN_N 14
00074 #define TMCB_LOCAL_REQUEST_IN_N  15
00075 #define TMCB_DLG_N              16
00076 #define TMCB_DESTROY_N          17  /* called on transaction destroy */
00077 #define TMCB_E2ECANCEL_IN_N     18
00078 #define TMCB_E2EACK_RETR_IN_N   19
00079 #define TMCB_RESPONSE_READY_N   20
00080 #ifdef WITH_AS_SUPPORT
00081 #define TMCB_DONT_ACK_N         21 /* TM shoudn't ACK a local UAC  */
00082 #endif
00083 #define TMCB_REQUEST_SENT_N     22
00084 #define TMCB_RESPONSE_SENT_N    23
00085 #define TMCB_MAX_N              23
00086 
00087 
00088 #define TMCB_REQUEST_IN       (1<<TMCB_REQUEST_IN_N)
00089 #define TMCB_RESPONSE_IN      (1<<TMCB_RESPONSE_IN_N)
00090 #define TMCB_E2EACK_IN        (1<<TMCB_E2EACK_IN_N)
00091 #define TMCB_REQUEST_PENDING  (1<<TMCB_REQUEST_PENDING_N)
00092 #define TMCB_REQUEST_FWDED    (1<<TMCB_REQUEST_FWDED_N)
00093 #define TMCB_RESPONSE_FWDED   (1<<TMCB_RESPONSE_FWDED_N)
00094 #define TMCB_ON_FAILURE_RO    (1<<TMCB_ON_FAILURE_RO_N)
00095 #define TMCB_ON_FAILURE       (1<<TMCB_ON_FAILURE_N)
00096 #define TMCB_REQUEST_OUT      (1<<TMCB_REQUEST_OUT_N)
00097 #define TMCB_RESPONSE_OUT     (1<<TMCB_RESPONSE_OUT_N)
00098 #define TMCB_LOCAL_COMPLETED  (1<<TMCB_LOCAL_COMPLETED_N)
00099 #define TMCB_LOCAL_RESPONSE_OUT (1<<TMCB_LOCAL_RESPONSE_OUT_N)
00100 #define TMCB_ACK_NEG_IN       (1<<TMCB_ACK_NEG_IN_N)
00101 #define TMCB_REQ_RETR_IN      (1<<TMCB_REQ_RETR_IN_N)
00102 #define TMCB_LOCAL_RESPONSE_IN (1<<TMCB_LOCAL_RESPONSE_IN_N)
00103 #define TMCB_LOCAL_REQUEST_IN (1<<TMCB_LOCAL_REQUEST_IN_N)
00104 #define TMCB_DLG              (1<<TMCB_DLG_N)
00105 #define TMCB_DESTROY          (1<<TMCB_DESTROY_N)
00106 #define TMCB_E2ECANCEL_IN     (1<<TMCB_E2ECANCEL_IN_N)
00107 #define TMCB_E2EACK_RETR_IN   (1<<TMCB_E2EACK_RETR_IN_N)
00108 #define TMCB_RESPONSE_READY   (1<<TMCB_RESPONSE_READY_N)
00109 #ifdef WITH_AS_SUPPORT
00110 #define TMCB_DONT_ACK         (1<<TMCB_DONT_ACK_N)
00111 #endif
00112 #define TMCB_REQUEST_SENT      (1<<TMCB_REQUEST_SENT_N)
00113 #define TMCB_RESPONSE_SENT     (1<<TMCB_RESPONSE_SENT_N)
00114 #define TMCB_MAX              ((1<<(TMCB_MAX_N+1))-1)
00115 
00116 
00117 /*
00118  *  Caution: most of the callbacks work with shmem-ized messages
00119  *  which you can no more change (e.g., lumps are fixed). Most
00120  *  reply-processing callbacks are also called from a mutex,
00121  *  which may cause deadlock if you are not careful. Also, reply
00122  *  callbacks may pass the value of FAKED_REPLY messages, which
00123  *  is a non-dereferencable pointer indicating that no message
00124  *  was received and a timer hit instead.
00125  *
00126  *  All callbacks excepting the TMCB_REQUEST_IN are associates to a
00127  *  transaction. It means they will be run only when the event will hint
00128  *  the transaction the callbacks were register for.
00129  *  TMCB_REQUEST_IN is a global callback - it means it will be run for
00130  *  all transactions.
00131  *
00132  *
00133  *  Callback description:
00134  *  ---------------------
00135  *
00136  * TMCB_REQUEST_IN -- a brand-new request was received and is
00137  * about to establish transaction; it is not yet cloned and
00138  * lives in pkg mem -- your last chance to mangle it before
00139  * it gets shmem-ized (then, it's read-only); it's called from
00140  * HASH_LOCK, so be careful. It is guaranteed not to be
00141  * a retransmission. The transactional context is mostly
00142  * incomplete -- this callback is called in very early stage
00143  * before the message is shmem-ized (so that you can work
00144  * with it).
00145  * Note: this callback MUST be installed before forking
00146  * (the req_in_tmcb_hl callback list does not live in shmem and has no access
00147  * protection), i.e., at best from mod_init functions.
00148  *
00149  * Note: All the other callbacks can be safely installed when the
00150  * transaction already exists, it does not need to be locked.
00151  * 
00152  * TMCB_RESPONSE_IN -- a brand-new reply was received which matches
00153  * an existing non-local transaction. It may or may not be a retransmission.
00154  * No lock is held here (yet).
00155  * Note: for an invite transaction this callback will also catch the reply
00156  *  to local cancels (e.g. branch canceled due to fr_inv_timeout). To
00157  *  distinguish between the two, one would need to look at the method in
00158  *  Cseq (look at t_reply.c:1630 (reply_received()) for an example).
00159  *
00160  *  TMCB_RESPONSE_OUT -- a final or provisional reply was sent out
00161  *  successfully (either a local reply  or a proxied one).
00162  *  For final replies is called only for the first one (it's not called
00163  *  for retransmissions).
00164  *  For non-local replies (proxied) is called also for provisional responses
00165  *  (NOTE: this might change and in the future it might be called only
00166  *  for final replies --andrei).
00167  *  For local replies is called _only_ for the final reply.
00168  *  There is nothing more you can change from the callback, it is good for 
00169  *  accounting-like uses. No lock is held.
00170  *  Known oddities: it's called for provisional replies for relayed replies,
00171  *  but not for local responses (see NOTE above).
00172  *  Note: if the send fails or via cannot be resolved, this callback is 
00173  *  _not_ called.
00174  *  Note: local reply means locally generated reply (via t_reply() & friends)
00175  *  and not local transaction.
00176  *
00177  *    Note: the message passed to the callback may also have
00178  *    value FAKED_REPLY (like other reply callbacks) which
00179  *    indicates a local reply caused by a timer, calling t_reply() a.s.o.
00180 *     Check for this value before deferring -- you will cause a segfault
00181  *    otherwise. Check for t->uas.request validity too if you
00182  *    need it ... locally initiated UAC transactions set it to 0.
00183  *
00184  *    Also note, that reply callbacks are not called if a transaction
00185  *    is dropped silently. That's the case when noisy_ctimer is
00186  *    disabled (by default) and C-timer hits. The proxy server then
00187  *    drops state silently, doesn't use callbacks and expects the
00188  *    transaction to complete statelessly.
00189  *
00190  *  TMCB_ON_FAILURE_RO -- called on receipt of a reply or timer;
00191  *  it means all branches completed with a failure; the callback
00192  *  function MUST not change anything in the transaction (READONLY)
00193  *  that's a chance for doing ACC or stuff like this
00194  *
00195  *  TMCB_ON_FAILURE -- called on receipt of a reply or timer;
00196  *  it means all branches completed with a failure; that's
00197  *  a chance for example to add new transaction branches.
00198  *  WARNING: the REPLY lock is held.
00199  *  It is safe to add more callbacks from here.
00200  *
00201  *  TMCB_RESPONSE_FWDED -- called when a reply is about to be
00202  *  forwarded; it is called after a message is received but before
00203  *  a message is sent out: it is called when the decision is
00204  *  made to forward a reply; it is parametrized by pkg message
00205  *  which caused the transaction to complete (which is not
00206  *  necessarily the same which will be forwarded). As forwarding
00207  *  has not been executed and may fail, there is no guarantee
00208  *  a reply will be successfully sent out at this point of time.
00209  *
00210  *     Note: TMCB_ON_FAILURE and TMCB_REPLY_FWDED are
00211  *     called from reply mutex which is used to deterministically
00212  *     process multiple replies received in parallel. A failure
00213  *     to set the mutex again or stay too long in the callback
00214  *     may result in deadlock.
00215  *
00216  *     Note: the reply callbacks will not be evoked if "silent
00217  *     C-timer hits". That's a feature to clean transactional
00218  *     state from a proxy quickly -- transactions will then
00219  *     complete statelessly. If you wish to disable this
00220  *     feature, either set the global option "noisy_ctimer"
00221  *     to 1, or set t->noisy_ctimer for selected transaction.
00222  *
00223  *  TMCB_E2EACK_IN -- called when an ACK belonging to a proxied
00224  *  INVITE transaction completed with 200 arrived. Note that
00225  *  because it can be only dialog-wise matched, only the first
00226  *  transaction occurrence will be matched with spirals. If
00227  *  record-routing is not enabled, you will never receive the
00228  *  ACK and the callback will be never triggered. In general it's called only
00229  *   for the first ACK but it can be also called multiple times 
00230  *   quasi-simultaneously if multiple ACK copies arrive in parallel or if
00231  *   ACKs with different (never seen before) to-tags are received.
00232  *
00233  *   TMCB_E2EACK_RETR_IN -- like TMCB_E2EACK_IN, but matches retransmissions
00234  *   and it's called for every retransmission (but not for the "first" ACK).
00235  *
00236  *  TMCB_E2ECANCEL_IN -- called when a CANCEL for the INVITE transaction
00237  *  for which the callback was registered arrives.
00238  *   The transaction parameter will point to the invite transaction (and 
00239  *   not the cancel) and the request parameter to the CANCEL sip msg.
00240  *   Note: the callback should be registered for an INVITE transaction.
00241  *
00242  *  TMCB_REQUEST_FWDED -- request is being forwarded out. It is
00243  *  called before a message is forwarded, when the corresponding branch
00244  *   is created (it's called for each branch) and it is your last
00245  *  chance to change its shape. It can also be called from the failure
00246  *   router (via t_relay/t_forward_nonack) and in this case the REPLY lock 
00247  *   will be held.
00248  *
00249  *  TMCB_REQUEST_OUT -- request was sent out successfully.
00250  *  There is nothing more you can change from the callback, it is good for
00251  *  accounting-like uses.
00252  *  Note: if the send fails or via cannot be resolved, this callback is
00253  *  _not_ called.
00254  *
00255  *  TMCB_LOCAL_COMPLETED -- final reply for localy initiated
00256  *  transaction arrived. Message may be FAKED_REPLY. Can be called multiple
00257  *  times, no lock is held.
00258  *
00259  *  TMCB_LOCAL_RESPONSE_OUT -- provisional reply for localy initiated 
00260  *  transaction. The message may be a FAKED_REPLY and the callback might be 
00261  *  called multiple time quasi-simultaneously. No lock is held.
00262  *  Note: depends on tm.pass_provisional_replies.
00263  *  Note: the name is very unfortunate and it will probably be changed
00264  *   (e.g. TMCB_LOCAL_PROVISIONAL).
00265  *
00266  *  TMCB_NEG_ACK_IN -- an ACK to a negative reply was received, thus ending
00267  *  the transaction (this happens only when the final reply sent by tm is 
00268  *  negative). The callback might be called simultaneously. No lock is held.
00269  *
00270  *  TMCB_REQ_RETR_IN -- a retransmitted request was received. This callback
00271  *   might be called simultaneously. No lock is held.
00272  *
00273  * TMCB_LOCAL_RESPONSE_IN -- a brand-new reply was received which matches
00274  * an existing local transaction (like TMCB_RESPONSE_IN but for local 
00275  * transactions). It may or may not be a retransmission.
00276  *
00277  * TMCB_LOCAL_REQUEST_IN -- like TMCB_REQUEST_IN but for locally generated 
00278  * request (e.g. via fifo/rpc):  a brand-new local request was 
00279  * received/generated and a transaction for it is about to be created.
00280  * It's called from HASH_LOCK, so be careful. It is guaranteed not to be
00281  * a retransmission. The transactional context is mostly
00282  * incomplete -- this callback is called in very early stage
00283  * before the message is shmem-ized (so that you can work
00284  * with it).
00285  * It's safe to install other TMCB callbacks from here.
00286  * Note: this callback MUST be installed before forking
00287  * (the local_req_in_tmcb_hl callback list does not live in shmem and has no 
00288  * access protection), i.e., at best from mod_init functions.
00289  *
00290  *
00291  *  All of the following callbacks are called immediately after or before 
00292  *  sending a message. All of them are read-only (no change can be made to
00293  * the message). These callbacks use the t_rbuf, send_buf, dst, is_retr
00294  *  and the code members of the tmcb_params structure.
00295  *  For a request code is <=0. code values can be TYPE_LOCAL_ACK for an ACK 
00296  *  generated by ser, TYPE_LOCAL_CANCEL for a CANCEL generated by ser 
00297  *  and TYPE_REQUEST for all the other requests or requests generated via 
00298  *  t_uac.
00299  *   For a reply the code is the response status (which is always >0, e.g. 200,
00300  *   408, a.s.o).
00301  *        - the callbacks will be called sometimes with the REPLY lock held
00302  *          and sometimes without it, so trying to acquire the REPLY lock
00303  *          from these callbacks could lead to deadlocks (avoid it unless
00304  *           you really know what you're doing).
00305  *
00306  *  TMCB_REQUEST_SENT -- called each time a request was sent (even for
00307  *  retransmissions), it includes local and forwarded request, ser generated
00308  *  CANCELs and ACKs. The tmcb_params structure will have the t_rbuf, dst,
00309  *  send_buf and is_retr members filled.
00310  *  This callback is "read-only", the message was already sent and no changes
00311  *  are allowed.
00312  *  Note: send_buf can be different from t_rbuf->buffer for ACKs (in this
00313  *   case t_rbuf->buf will contain the last request sent on the branch and
00314  *   its destination). The same goes for t_rbuf->dst and tmcb->dst for local 
00315  *   transactions ACKs to 2xxs.
00316  *
00317  *  TMCB_RESPONSE_SENT -- called each time a response was sent (even for
00318  *  retransmissions). The tmcb_params structure will have t_rbuf set to the
00319  *  reply retransmission buffer and send_buf set to the data sent (in this case
00320  *  it will always be the same with t_rbuf->buf). is_retr will also be set if
00321  *  the reply is retransmitted
00322  *   by ser.
00323  *  This callback is "read-only", the message was already sent and no changes
00324  *  are allowed.
00325  *
00326  *  TMCB_DESTROY -- called when the transaction is destroyed. Everything but
00327  *  the cell* parameter (t) and the tmcb are set to 0. Only the param is
00328  *  is filled inside TMCB. For dialogs callbacks t is also 0.
00329  *
00330  * TMCB_RESPONSE_READY -- a reply is ready to be sent out. Callback is
00331  *  is executed just before writing the reply content to network.
00332  *
00333  * TMCB_DONT_ACK (requires AS support) -- for localy generated INVITEs, TM 
00334  * automatically generates an ACK for the received 2xx replies. But, if this 
00335  * flag is passed to TM when creating the initial UAC request, this won't
00336  * happen anymore: the ACK generation must be triggered from outside, using
00337  * TM's interface.
00338  * While this isn't exactly a callback type, it is used as part of the flags
00339  * mask when registering callbacks.
00340 
00341         the callback's param MUST be in shared memory and will
00342         NOT be freed by TM; you must do it yourself from the
00343         callback function if necessary (for example register it also for 
00344          TMCB_DESTROY and when called with TMCB_DESTROY just free the param
00345         ).
00346 */
00347 
00348 #define TMCB_RETR_F 1
00349 #define TMCB_LOCAL_F 2
00350 
00351 /* pack structure with all params passed to callback function */
00352 struct tmcb_params {
00353         struct sip_msg* req;
00354         struct sip_msg* rpl;
00355         void **param;
00356         int code;
00357         unsigned short flags; /* set to a combination of:
00358                                                          TMCB_RETR_F if this is a _ser_ retransmission
00359                                                          (but not if if it's a "forwarded" retr., like a 
00360                                                          retr. 200 Ok for example)
00361                                                          TMCB_LOCAL_F if this is a local generated message
00362                                                           (and not forwarded) */
00363         unsigned short branch;
00364         /* could also be: send_buf, dst, branch */
00365         struct retr_buf* t_rbuf; /* transaction retr. buf., all the information
00366                                                                  regarding destination, data that is/was
00367                                                                  actually sent on the net, branch a.s.o is
00368                                                                  inside */
00369         struct dest_info* dst; /* destination */
00370         str send_buf; /* what was/will be sent on the net, used for ACKs
00371                                         (which don't have a retr_buf). */
00372 };
00373 
00374 #define INIT_TMCB_PARAMS(tmcb, request, reply, r_code)\
00375 do{\
00376         memset(&(tmcb), 0, sizeof((tmcb))); \
00377         (tmcb).req=(request); (tmcb).rpl=(reply);  \
00378         (tmcb).code=(r_code); \
00379 }while(0)
00380 
00381 #define INIT_TMCB_ONSEND_PARAMS(tmcb, req, repl, rbuf, dest, buf, buf_len, \
00382                                                                 onsend_flags, t_branch, code) \
00383 do{ \
00384         INIT_TMCB_PARAMS(tmcb, req, repl, code); \
00385         tmcb.t_rbuf=(rbuf); tmcb.dst=(dest); \
00386         tmcb.send_buf.s=(buf); tmcb.send_buf.len=(buf_len); \
00387         tmcb.flags=(onsend_flags); tmcb.branch=(t_branch); \
00388 }while(0)
00389 
00390 /* callback function prototype */
00391 typedef void (transaction_cb) (struct cell* t, int type, struct tmcb_params*);
00393 typedef void (release_tmcb_param) (void* param);
00394 /* register callback function prototype */
00395 typedef int (*register_tmcb_f)(struct sip_msg* p_msg, struct cell *t,
00396                                                            int cb_types, transaction_cb f, void *param,
00397                                                            release_tmcb_param func);
00398 
00399 
00400 struct tm_callback {
00401         int id;                      /* id of this callback - useless */
00402         int types;                   /* types of events that trigger the callback*/
00403         transaction_cb* callback;    /* callback function */
00404         void *param;                 /* param to be passed to callback function */
00405         release_tmcb_param* release; 
00407         struct tm_callback* next;
00408 };
00409 
00410 struct tmcb_head_list {
00411         struct tm_callback volatile *first;
00412         int reg_types;
00413 };
00414 
00415 
00416 extern struct tmcb_head_list*  req_in_tmcb_hl;
00417 extern struct tmcb_head_list*  local_req_in_tmcb_hl;
00418 
00419 void set_early_tmcb_list(struct sip_msg *msg,
00420                 struct cell *t);
00421 
00422 #define has_tran_tmcbs(_T_, _types_) \
00423         ( ((_T_)->tmcb_hl.reg_types)&(_types_) )
00424 #define has_reqin_tmcbs() \
00425         ( req_in_tmcb_hl->first!=0 )
00426 #define has_local_reqin_tmcbs() \
00427         ( local_req_in_tmcb_hl->first!=0 )
00428 
00429 
00430 int init_tmcb_lists(void);
00431 
00432 void destroy_tmcb_lists(void);
00433 
00434 
00435 /* register a callback for several types of events */
00436 int register_tmcb( struct sip_msg* p_msg, struct cell *t, int types,
00437                                    transaction_cb f, void *param,
00438                                    release_tmcb_param rel_func);
00439 
00440 /* inserts a callback into the a callback list */
00441 int insert_tmcb(struct tmcb_head_list *cb_list, int types,
00442                                 transaction_cb f, void *param,
00443                                 release_tmcb_param rel_func);
00444 
00445 /* run all transaction callbacks for an event type */
00446 void run_trans_callbacks( int type , struct cell *trans,
00447                                                 struct sip_msg *req, struct sip_msg *rpl, int code );
00448 /* helper function */
00449 void run_trans_callbacks_internal(struct tmcb_head_list* cb_lst, int type,
00450                                                                         struct cell *trans, 
00451                                                                         struct tmcb_params *params);
00452 /* run all REQUEST_IN callbacks */
00453 void run_reqin_callbacks( struct cell *trans, struct sip_msg *req, int code );
00454 void run_local_reqin_callbacks( struct cell *trans, struct sip_msg *req, 
00455                 int code );
00456 
00457 /* like run_trans_callbacks but provide outgoing buffer (i.e., the
00458  * processed message) to callback */
00459 void run_trans_callbacks_with_buf(int type, struct retr_buf* rbuf, struct sip_msg* req,
00460                                                                   struct sip_msg* repl, short flags);
00461 
00462 /* like run_trans_callbacks but tmcb_params assumed to contain data already */
00463 void run_trans_callbacks_off_params(int type, struct cell* t, struct tmcb_params* p);
00464 
00465 #endif