t_hooks.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * History:
  28. * --------
  29. * 2003-03-16 : backwards-compatibility callback names introduced (jiri)
  30. * 2003-03-06 : old callbacks renamed, new one introduced (jiri)
  31. * 2003-12-04 : global callbacks moved into transaction callbacks;
  32. * multiple events per callback added; single list per
  33. * transaction for all its callbacks (bogdan)
  34. * 2007-03-14 added *_SENT callbacks (andrei)
  35. * 2007-03-17 added TMCB_NEG_ACK_IN, TMCB_REQ_RETR_IN &
  36. * TMCB_LOCAL_RESPONSE_IN (andrei)
  37. * 2007-03-23 added TMCB_LOCAL_REQUEST_IN (andrei)
  38. * 2007-05-16 added TMCB_DESTROY (andrei)
  39. * 2007-05-24 fixed has_tran_tmcbs() & added TMCB_E2ECANCEL_IN (andrei)
  40. * 2007-11-12 added TMCB_E2EACK_RETR_IN (andrei)
  41. */
  42. #ifndef _HOOKS_H
  43. #define _HOOKS_H
  44. #include "defs.h"
  45. /* if defined support for ONSEND callbacks will be added and
  46. * the tmcb_params structure will get some additional members */
  47. /*
  48. #define TMCB_ONSEND
  49. */
  50. #ifdef TMCB_ONSEND
  51. #include "../../ip_addr.h" /* dest_info */
  52. #endif
  53. struct sip_msg;
  54. struct cell;
  55. #define TMCB_REQUEST_IN_N 0
  56. #define TMCB_RESPONSE_IN_N 1
  57. #define TMCB_E2EACK_IN_N 2
  58. #define TMCB_REQUEST_FWDED_N 3
  59. #define TMCB_RESPONSE_FWDED_N 4
  60. #define TMCB_ON_FAILURE_RO_N 5
  61. #define TMCB_ON_FAILURE_N 6
  62. #define TMCB_RESPONSE_OUT_N 7
  63. #define TMCB_LOCAL_COMPLETED_N 8
  64. #define TMCB_LOCAL_RESPONSE_OUT_N 9
  65. #define TMCB_ACK_NEG_IN_N 10
  66. #define TMCB_REQ_RETR_IN_N 11
  67. #define TMCB_LOCAL_RESPONSE_IN_N 12
  68. #define TMCB_LOCAL_REQUEST_IN_N 13
  69. #define TMCB_DLG_N 14
  70. #define TMCB_DESTROY_N 15 /* called on transaction destroy */
  71. #define TMCB_E2ECANCEL_IN_N 16
  72. #define TMCB_E2EACK_RETR_IN_N 17
  73. #ifdef WITH_AS_SUPPORT
  74. #define TMCB_DONT_ACK_N 18 /* TM shoudn't ACK a local UAC */
  75. #endif
  76. #ifdef TMCB_ONSEND
  77. #define TMCB_REQUEST_SENT_N 19
  78. #define TMCB_RESPONSE_SENT_N 20
  79. #define TMCB_MAX_N 20
  80. #else
  81. #define TMCB_MAX_N 18
  82. #endif
  83. #define TMCB_REQUEST_IN (1<<TMCB_REQUEST_IN_N)
  84. #define TMCB_RESPONSE_IN (1<<TMCB_RESPONSE_IN_N)
  85. #define TMCB_E2EACK_IN (1<<TMCB_E2EACK_IN_N)
  86. #define TMCB_REQUEST_FWDED (1<<TMCB_REQUEST_FWDED_N)
  87. #define TMCB_RESPONSE_FWDED (1<<TMCB_RESPONSE_FWDED_N)
  88. #define TMCB_ON_FAILURE_RO (1<<TMCB_ON_FAILURE_RO_N)
  89. #define TMCB_ON_FAILURE (1<<TMCB_ON_FAILURE_N)
  90. #define TMCB_RESPONSE_OUT (1<<TMCB_RESPONSE_OUT_N)
  91. #define TMCB_LOCAL_COMPLETED (1<<TMCB_LOCAL_COMPLETED_N)
  92. #define TMCB_LOCAL_RESPONSE_OUT (1<<TMCB_LOCAL_RESPONSE_OUT_N)
  93. #define TMCB_ACK_NEG_IN (1<<TMCB_ACK_NEG_IN_N)
  94. #define TMCB_REQ_RETR_IN (1<<TMCB_REQ_RETR_IN_N)
  95. #define TMCB_LOCAL_RESPONSE_IN (1<<TMCB_LOCAL_RESPONSE_IN_N)
  96. #define TMCB_LOCAL_REQUEST_IN (1<<TMCB_LOCAL_REQUEST_IN_N)
  97. #define TMCB_DLG (1<<TMCB_DLG_N)
  98. #define TMCB_DESTROY (1<<TMCB_DESTROY_N)
  99. #define TMCB_E2ECANCEL_IN (1<<TMCB_E2ECANCEL_IN_N)
  100. #define TMCB_E2EACK_RETR_IN (1<<TMCB_E2EACK_RETR_IN_N)
  101. #ifdef WITH_AS_SUPPORT
  102. #define TMCB_DONT_ACK (1<<TMCB_DONT_ACK_N)
  103. #endif
  104. #ifdef TMCB_ONSEND
  105. #define TMCB_REQUEST_SENT (1<<TMCB_REQUEST_SENT_N)
  106. #define TMCB_RESPONSE_SENT (1<<TMCB_RESPONSE_SENT_N)
  107. #endif
  108. #define TMCB_MAX ((1<<(TMCB_MAX_N+1))-1)
  109. /*
  110. * Caution: most of the callbacks work with shmem-ized messages
  111. * which you can no more change (e.g., lumps are fixed). Most
  112. * reply-processing callbacks are also called from a mutex,
  113. * which may cause deadlock if you are not careful. Also, reply
  114. * callbacks may pass the value of FAKED_REPLY messages, which
  115. * is a non-dereferencable pointer indicating that no message
  116. * was received and a timer hit instead.
  117. *
  118. * All callbacks excepting the TMCB_REQUEST_IN are associates to a
  119. * transaction. It means they will be run only when the event will hint
  120. * the transaction the callbacks were register for.
  121. * TMCB_REQUEST_IN is a global callback - it means it will be run for
  122. * all transactions.
  123. *
  124. *
  125. * Callback description:
  126. * ---------------------
  127. *
  128. * TMCB_REQUEST_IN -- a brand-new request was received and is
  129. * about to establish transaction; it is not yet cloned and
  130. * lives in pkg mem -- your last chance to mangle it before
  131. * it gets shmem-ized (then, it's read-only); it's called from
  132. * HASH_LOCK, so be careful. It is guaranteed not to be
  133. * a retransmission. The transactional context is mostly
  134. * incomplete -- this callback is called in very early stage
  135. * before the message is shmem-ized (so that you can work
  136. * with it).
  137. * It's safe to install other TMCB callbacks from here.
  138. * Note: this callback MUST be installed before forking
  139. * (the req_in_tmcb_hl callback list does not live in shmem and has no access
  140. * protection), i.e., at best from mod_init functions.
  141. *
  142. * Note: all the other callbacks MUST be installed before or immediately after
  143. * the transaction is created (they must not be installed from any place
  144. * where the transaction callbacks can be run or added simultaneously with
  145. * the add operation). Another safe way of installing them is to hold the
  146. * REPLY lock prior to adding a callback.
  147. * In general it's not safe to register callbacks from other TMCB callbacks,
  148. * unless the contrary is explicitly stated in the TMCB callback description.
  149. * For example a good place for installing them is from a TMCB_REQUEST_IN
  150. * callback.
  151. *
  152. * TMCB_RESPONSE_IN -- a brand-new reply was received which matches
  153. * an existing non-local transaction. It may or may not be a retransmission.
  154. * No lock is held here (yet).
  155. * Note: for an invite transaction this callback will also catch the reply
  156. * to local cancels (e.g. branch canceled due to fr_inv_timeout). To
  157. * distinguish between the two, one would need to look at the method in
  158. * Cseq (look at t_reply.c:1630 (reply_received()) for an example).
  159. *
  160. * TMCB_RESPONSE_OUT -- a final reply was sent out (either local
  161. * or proxied) -- there is nothing more you can change from
  162. * the callback, it is good for accounting-like uses. No lock is held.
  163. * Known BUGS: it's called also for provisional replies for relayed replies.
  164. * For local replies it's called only for the final reply.
  165. *
  166. * Note: the message passed to callback may also have
  167. * value FAKED_REPLY (like other reply callbacks) which
  168. * indicates a pseudo_reply caused by a timer. Check for
  169. * this value before deferring -- you will cause a segfault
  170. * otherwise. Check for t->uas.request validity too if you
  171. * need it ... locally initiated UAC transactions set it to 0.
  172. *
  173. * Also note, that reply callbacks are not called if a transaction
  174. * is dropped silently. That's the case when noisy_ctimer is
  175. * disabled (by default) and C-timer hits. The proxy server then
  176. * drops state silently, doesn't use callbacks and expects the
  177. * transaction to complete statelessly.
  178. *
  179. * TMCB_ON_FAILURE_RO -- called on receipt of a reply or timer;
  180. * it means all branches completed with a failure; the callback
  181. * function MUST not change anything in the transaction (READONLY)
  182. * that's a chance for doing ACC or stuff like this
  183. *
  184. * TMCB_ON_FAILURE -- called on receipt of a reply or timer;
  185. * it means all branches completed with a failure; that's
  186. * a chance for example to add new transaction branches.
  187. * WARNING: the REPLY lock is held.
  188. * It is safe to add more callbacks from here.
  189. *
  190. * TMCB_RESPONSE_FWDED -- called when a reply is about to be
  191. * forwarded; it is called after a message is received but before
  192. * a message is sent out: it is called when the decision is
  193. * made to forward a reply; it is parametrized by pkg message
  194. * which caused the transaction to complete (which is not
  195. * necessarily the same which will be forwarded). As forwarding
  196. * has not been executed and may fail, there is no guarantee
  197. * a reply will be successfully sent out at this point of time.
  198. *
  199. * Note: TMCB_ON_FAILURE and TMCB_REPLY_FWDED are
  200. * called from reply mutex which is used to deterministically
  201. * process multiple replies received in parallel. A failure
  202. * to set the mutex again or stay too long in the callback
  203. * may result in deadlock.
  204. *
  205. * Note: the reply callbacks will not be evoked if "silent
  206. * C-timer hits". That's a feature to clean transactional
  207. * state from a proxy quickly -- transactions will then
  208. * complete statelessly. If you wish to disable this
  209. * feature, either set the global option "noisy_ctimer"
  210. * to 1, or set t->noisy_ctimer for selected transaction.
  211. *
  212. * TMCB_E2EACK_IN -- called when an ACK belonging to a proxied
  213. * INVITE transaction completed with 200 arrived. Note that
  214. * because it can be only dialog-wise matched, only the first
  215. * transaction occurrence will be matched with spirals. If
  216. * record-routing is not enabled, you will never receive the
  217. * ACK and the callback will be never triggered. In general it's called only
  218. * for the first ACK but it can be also called multiple times
  219. * quasi-simultaneously if multiple ACK copies arrive in parallel or if
  220. * ACKs with different (never seen before) to-tags are received.
  221. *
  222. * TMCB_E2EACK_RETR_IN -- like TMCB_E2EACK_IN, but matches retransmissions
  223. * and it's called for every retransmission (but not for the "first" ACK).
  224. *
  225. * TMCB_E2ECANCEL_IN -- called when a CANCEL for the INVITE transaction
  226. * for which the callback was registered arrives.
  227. * The transaction parameter will point to the invite transaction (and
  228. * not the cancel) and the request parameter to the CANCEL sip msg.
  229. * Note: the callback should be registered for an INVITE transaction.
  230. *
  231. * TMCB_REQUEST_FWDED -- request is being forwarded out. It is
  232. * called before a message is forwarded, when the corresponding branch
  233. * is created (it's called for each branch) and it is your last
  234. * chance to change its shape. It can also be called from the failure
  235. * router (via t_relay/t_forward_nonack) and in this case the REPLY lock
  236. * will be held.
  237. *
  238. * TMCB_LOCAL_COMPLETED -- final reply for localy initiated
  239. * transaction arrived. Message may be FAKED_REPLY. Can be called multiple
  240. * times, no lock is held.
  241. *
  242. * TMCB_LOCAL_REPONSE_OUT -- provisional reply for localy initiated
  243. * transaction. The message may be a FAKED_REPLY and the callback might be
  244. * called multiple time quasi-simultaneously. No lock is held.
  245. *
  246. * TMCB_NEG_ACK_IN -- an ACK to a negative reply was received, thus ending
  247. * the transaction (this happens only when the final reply sent by tm is
  248. * negative). The callback might be called simultaneously. No lock is held.
  249. *
  250. * TMCB_REQ_RETR_IN -- a retransmitted request was received. This callback
  251. * might be called simultaneously. No lock is held.
  252. *
  253. * TMCB_LOCAL_RESPONSE_IN -- a brand-new reply was received which matches
  254. * an existing local transaction (like TMCB_RESPONSE_IN but for local
  255. * transactions). It may or may not be a retransmission.
  256. *
  257. * TMCB_LOCAL_REQUEST_IN -- like TMCB_REQUEST_IN but for locally generated
  258. * request (e.g. via fifo/rpc): a brand-new local request was
  259. * received/generated and a transaction for it is about to be created.
  260. * It's called from HASH_LOCK, so be careful. It is guaranteed not to be
  261. * a retransmission. The transactional context is mostly
  262. * incomplete -- this callback is called in very early stage
  263. * before the message is shmem-ized (so that you can work
  264. * with it).
  265. * It's safe to install other TMCB callbacks from here.
  266. * Note: this callback MUST be installed before forking
  267. * (the local_req_in_tmcb_hl callback list does not live in shmem and has no
  268. * access protection), i.e., at best from mod_init functions.
  269. *
  270. *
  271. * All of the following callbacks are called immediately after or before
  272. * sending a message. All of them are read-only (no change can be made to
  273. * the message). These callbacks use the t_rbuf, send_buf, dst, is_retr
  274. * and the code members of the tmcb_params structure.
  275. * For a request code is <=0. code values can be TYPE_LOCAL_ACK for an ACK
  276. * generated by ser, TYPE_LOCAL_CANCEL for a CANCEL generated by ser
  277. * and TYPE_REQUEST for all the other requests or requests generated via
  278. * t_uac.
  279. * For a reply the code is the response status (which is always >0, e.g. 200,
  280. * 408, a.s.o).
  281. * Note: - these callbacks can be used only if TMCB_ONSEND is defined.
  282. * - the callbacks will be called sometimes with the REPLY lock held
  283. * and sometimes without it, so trying to acquire the REPLY lock
  284. * from these callbacks could lead to deadlocks (avoid it unless
  285. * you really know what you're doing).
  286. *
  287. * TMCB_REQUEST_SENT (present only if TMCB_ONSEND is defined) -- called
  288. * each time a request was sent (even for retransmissions), it includes
  289. * local and forwarded request, ser generated CANCELs and ACKs. The
  290. * tmcb_params structure will have the t_rbuf, dst, send_buf and is_retr
  291. * members filled.
  292. * This callback is "read-only", the message was already sent and no changes
  293. * are allowed.
  294. * Note: send_buf can be different from t_rbuf->buffer for ACKs (in this
  295. * case t_rbuf->buf will contain the last request sent on the branch and
  296. * its destination). The same goes for t_rbuf->dst and tmcb->dst for local
  297. * transactions ACKs to 2xxs.
  298. *
  299. * TMCB_RESPONSE_SENT (present only if TMCB_ONSEND is defined) -- called
  300. * each time a response was sent (even for retransmissions). The tmcb_params
  301. * structure will have t_rbuf set to the reply retransmission buffer and
  302. * send_buf set to the data sent (in this case it will always be the same
  303. * with t_rbuf->buf). is_retr will also be set if the reply is retransmitted
  304. * by ser.
  305. * This callback is "read-only", the message was already sent and no changes
  306. * are allowed.
  307. *
  308. * TMCB_DESTROY -- called when the transaction is destroyed. Everything but
  309. * the cell* parameter (t) and the tmcb are set to 0. Only the param is
  310. * is filled inside TMCB. For dialogs callbacks t is also 0.
  311. *
  312. *
  313. * TMCB_DONT_ACK (requires AS support) -- for localy generated INVITEs, TM
  314. * automatically generates an ACK for the received 2xx replies. But, if this
  315. * flag is passed to TM when creating the initial UAC request, this won't
  316. * happen anymore: the ACK generation must be triggered from outside, using
  317. * TM's interface.
  318. * While this isn't exactly a callback type, it is used as part of the flags
  319. * mask when registering callbacks.
  320. the callback's param MUST be in shared memory and will
  321. NOT be freed by TM; you must do it yourself from the
  322. callback function if necessary (for example register it also for
  323. TMCB_DESTROY and when called with TMCB_DESTROY just free the param
  324. ).
  325. */
  326. #ifdef TMCB_ONSEND
  327. #define TMCB_RETR_F 1
  328. #define TMCB_LOCAL_F 2
  329. #endif
  330. /* pack structure with all params passed to callback function */
  331. struct tmcb_params {
  332. struct sip_msg* req;
  333. struct sip_msg* rpl;
  334. void **param;
  335. int code;
  336. #ifdef TMCB_ONSEND
  337. unsigned short flags; /* set to a combination of:
  338. TMCB_RETR_F if this is a _ser_ retransmission
  339. (but not if if it's a "forwarded" retr., like a
  340. retr. 200 Ok for example)
  341. TMCB_LOCAL_F if this is a local generated message
  342. (and not forwarded) */
  343. unsigned short branch;
  344. /* could also be: send_buf, dst, branch */
  345. struct retr_buf* t_rbuf; /* transaction retr. buf., all the information
  346. regarding destination, data that is/was
  347. actually sent on the net, branch a.s.o is
  348. inside */
  349. struct dest_info* dst; /* destination */
  350. str send_buf; /* what was/will be sent on the net, used for ACKs
  351. (which don't have a retr_buf). */
  352. #endif
  353. };
  354. #define INIT_TMCB_PARAMS(tmcb, request, reply, r_code)\
  355. do{\
  356. memset(&(tmcb), 0, sizeof((tmcb))); \
  357. (tmcb).req=(request); (tmcb).rpl=(reply); \
  358. (tmcb).code=(r_code); \
  359. }while(0)
  360. #ifdef TMCB_ONSEND
  361. #define INIT_TMCB_ONSEND_PARAMS(tmcb, req, repl, rbuf, dest, buf, buf_len, \
  362. onsend_flags, t_branch, code) \
  363. do{ \
  364. INIT_TMCB_PARAMS(tmcb, req, repl, code); \
  365. tmcb.t_rbuf=(rbuf); tmcb.dst=(dest); \
  366. tmcb.send_buf.s=(buf); tmcb.send_buf.len=(buf_len); \
  367. tmcb.flags=(onsend_flags); tmcb.branch=(t_branch); \
  368. }while(0)
  369. #endif
  370. /* callback function prototype */
  371. typedef void (transaction_cb) (struct cell* t, int type, struct tmcb_params*);
  372. /* register callback function prototype */
  373. typedef int (*register_tmcb_f)(struct sip_msg* p_msg, struct cell *t,
  374. int cb_types, transaction_cb f, void *param);
  375. struct tm_callback {
  376. int id; /* id of this callback - useless */
  377. int types; /* types of events that trigger the callback*/
  378. transaction_cb* callback; /* callback function */
  379. void *param; /* param to be passed to callback function */
  380. struct tm_callback* next;
  381. };
  382. struct tmcb_head_list {
  383. struct tm_callback volatile *first;
  384. int reg_types;
  385. };
  386. extern struct tmcb_head_list* req_in_tmcb_hl;
  387. extern struct tmcb_head_list* local_req_in_tmcb_hl;
  388. #define has_tran_tmcbs(_T_, _types_) \
  389. ( ((_T_)->tmcb_hl.reg_types)&(_types_) )
  390. #define has_reqin_tmcbs() \
  391. ( req_in_tmcb_hl->first!=0 )
  392. #define has_local_reqin_tmcbs() \
  393. ( local_req_in_tmcb_hl->first!=0 )
  394. int init_tmcb_lists();
  395. void destroy_tmcb_lists();
  396. /* register a callback for several types of events */
  397. int register_tmcb( struct sip_msg* p_msg, struct cell *t, int types,
  398. transaction_cb f, void *param );
  399. /* inserts a callback into the a callback list */
  400. int insert_tmcb(struct tmcb_head_list *cb_list, int types,
  401. transaction_cb f, void *param );
  402. /* run all transaction callbacks for an event type */
  403. void run_trans_callbacks( int type , struct cell *trans,
  404. struct sip_msg *req, struct sip_msg *rpl, int code );
  405. /* helper function */
  406. void run_trans_callbacks_internal(struct tmcb_head_list* cb_lst, int type,
  407. struct cell *trans,
  408. struct tmcb_params *params);
  409. /* run all REQUEST_IN callbacks */
  410. void run_reqin_callbacks( struct cell *trans, struct sip_msg *req, int code );
  411. void run_local_reqin_callbacks( struct cell *trans, struct sip_msg *req,
  412. int code );
  413. #ifdef TMCB_ONSEND
  414. void run_onsend_callbacks(int type, struct retr_buf* rbuf, struct sip_msg* req,
  415. struct sip_msg* repl, short flags);
  416. void run_onsend_callbacks2(int type, struct cell* t, struct tmcb_params* p);
  417. #endif
  418. #endif