sip_transaction.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJSIP_SIP_TRANSACTION_H__
  20. #define __PJSIP_SIP_TRANSACTION_H__
  21. /**
  22. * @file sip_transaction.h
  23. * @brief SIP Transaction
  24. */
  25. #include <pjsip/sip_msg.h>
  26. #include <pjsip/sip_util.h>
  27. #include <pjsip/sip_transport.h>
  28. #include <pj/timer.h>
  29. PJ_BEGIN_DECL
  30. /**
  31. * @defgroup PJSIP_TRANSACT Transaction Layer
  32. * @brief Provides statefull message processing.
  33. *
  34. * This module provides stateful processing to incoming or outgoing SIP
  35. * messages.
  36. * Before performing any stateful operations, application must register the
  37. * transaction layer module by calling #pjsip_tsx_layer_init_module().
  38. *
  39. * Application should link with <b>pjsip-core</b> library to
  40. * use the transaction layer.
  41. */
  42. /**
  43. * @defgroup PJSIP_TRANSACT_TRANSACTION Transaction
  44. * @ingroup PJSIP_TRANSACT
  45. * @brief Transaction instance for all types of SIP transactions.
  46. * @{
  47. * The pjsip_transaction describes SIP transaction, and is used for
  48. * both INVITE and non-INVITE, UAC or UAS. Application must register the
  49. * transaction layer module with #pjsip_tsx_layer_init_module() before
  50. * performing any stateful operations.
  51. */
  52. /**
  53. * This enumeration represents transaction state.
  54. */
  55. typedef enum pjsip_tsx_state_e
  56. {
  57. PJSIP_TSX_STATE_NULL, /**< For UAC, before any message is sent. */
  58. PJSIP_TSX_STATE_CALLING, /**< For UAC, just after request is sent. */
  59. PJSIP_TSX_STATE_TRYING, /**< For UAS, just after request is received.*/
  60. PJSIP_TSX_STATE_PROCEEDING, /**< For UAS/UAC, after provisional response.*/
  61. PJSIP_TSX_STATE_COMPLETED, /**< For UAS/UAC, after final response. */
  62. PJSIP_TSX_STATE_CONFIRMED, /**< For UAS, after ACK is received. */
  63. PJSIP_TSX_STATE_TERMINATED, /**< For UAS/UAC, before it's destroyed. */
  64. PJSIP_TSX_STATE_DESTROYED, /**< For UAS/UAC, will be destroyed now. */
  65. PJSIP_TSX_STATE_MAX /**< Number of states. */
  66. } pjsip_tsx_state_e;
  67. /**
  68. * This structure describes SIP transaction object. The transaction object
  69. * is used to handle both UAS and UAC transaction.
  70. */
  71. struct pjsip_transaction
  72. {
  73. /*
  74. * Administrivia
  75. */
  76. pj_pool_t *pool; /**< Pool owned by the tsx. */
  77. pjsip_module *tsx_user; /**< Transaction user. */
  78. pjsip_endpoint *endpt; /**< Endpoint instance. */
  79. pj_bool_t terminating; /**< terminate() was called */
  80. pj_grp_lock_t *grp_lock; /**< Transaction grp lock. */
  81. pj_mutex_t *mutex_b; /**< Second mutex to avoid
  82. deadlock. It is used to
  83. protect timer. */
  84. /*
  85. * Transaction identification.
  86. */
  87. char obj_name[PJ_MAX_OBJ_NAME]; /**< Log info. */
  88. pjsip_role_e role; /**< Role (UAS or UAC) */
  89. pjsip_method method; /**< The method. */
  90. pj_int32_t cseq; /**< The CSeq */
  91. pj_str_t transaction_key;/**< Hash table key. */
  92. pj_str_t transaction_key2;/**< Hash table key (2)
  93. for merged requests
  94. tsx lookup. */
  95. pj_uint32_t hashed_key; /**< Key's hashed value. */
  96. pj_uint32_t hashed_key2; /**< Key's hashed value (2).*/
  97. pj_str_t branch; /**< The branch Id. */
  98. /*
  99. * State and status.
  100. */
  101. int status_code; /**< Last status code seen. */
  102. pj_str_t status_text; /**< Last reason phrase. */
  103. pjsip_tsx_state_e state; /**< State. */
  104. int handle_200resp; /**< UAS 200/INVITE retrsm.*/
  105. int tracing; /**< Tracing enabled? */
  106. /** Handler according to current state. */
  107. pj_status_t (*state_handler)(struct pjsip_transaction *, pjsip_event *);
  108. /*
  109. * Transport.
  110. */
  111. pjsip_transport *transport; /**< Transport to use. */
  112. pj_bool_t is_reliable; /**< Transport is reliable. */
  113. pj_sockaddr addr; /**< Destination address. */
  114. int addr_len; /**< Address length. */
  115. pjsip_response_addr res_addr; /**< Response address. */
  116. unsigned transport_flag; /**< Miscelaneous flag. */
  117. pj_status_t transport_err; /**< Internal error code. */
  118. pjsip_tpselector tp_sel; /**< Transport selector. */
  119. pjsip_tx_data *pending_tx; /**< Tdata which caused
  120. pending transport flag
  121. to be set on tsx. */
  122. pjsip_tp_state_listener_key *tp_st_key; /**< Transport state listener
  123. key. */
  124. /*
  125. * Messages and timer.
  126. */
  127. pjsip_tx_data *last_tx; /**< Msg kept for retrans. */
  128. int retransmit_count;/**< Retransmission count. */
  129. pj_timer_entry retransmit_timer;/**< Retransmit timer. */
  130. pj_timer_entry timeout_timer; /**< Timeout timer. */
  131. /** Module specific data. */
  132. void *mod_data[PJSIP_MAX_MODULE];
  133. };
  134. /**
  135. * Create and register transaction layer module to the specified endpoint.
  136. *
  137. * @param endpt The endpoint instance.
  138. *
  139. * @return PJ_SUCCESS on success.
  140. */
  141. PJ_DECL(pj_status_t) pjsip_tsx_layer_init_module(pjsip_endpoint *endpt);
  142. /**
  143. * Get the instance of the transaction layer module.
  144. *
  145. * @return The transaction layer module.
  146. */
  147. PJ_DECL(pjsip_module*) pjsip_tsx_layer_instance(void);
  148. /**
  149. * Unregister and destroy transaction layer module.
  150. *
  151. * @return PJ_SUCCESS on success.
  152. */
  153. PJ_DECL(pj_status_t) pjsip_tsx_layer_destroy(void);
  154. /**
  155. * Retrieve the current number of transactions currently registered
  156. * in the hash table.
  157. *
  158. * @return Number of transactions.
  159. */
  160. PJ_DECL(unsigned) pjsip_tsx_layer_get_tsx_count(void);
  161. /**
  162. * Find a transaction with the specified key. The transaction key normally
  163. * is created by calling #pjsip_tsx_create_key() from an incoming message.
  164. *
  165. * IMPORTANT: To prevent deadlock, application should use
  166. * #pjsip_tsx_layer_find_tsx2() instead which only adds a reference to
  167. * the transaction instead of locking it.
  168. *
  169. * @param key The key string to find the transaction.
  170. * @param lock If non-zero, transaction will be locked before the
  171. * function returns, to make sure that it's not deleted
  172. * by other threads.
  173. *
  174. * @return The matching transaction instance, or NULL if transaction
  175. * can not be found.
  176. */
  177. PJ_DECL(pjsip_transaction*) pjsip_tsx_layer_find_tsx( const pj_str_t *key,
  178. pj_bool_t lock );
  179. /**
  180. * Find a transaction with the specified key. The transaction key normally
  181. * is created by calling #pjsip_tsx_create_key() from an incoming message.
  182. *
  183. * @param key The key string to find the transaction.
  184. * @param add_ref If non-zero, transaction's reference will be added
  185. * by one before the function returns, to make sure that
  186. * it's not deleted by other threads.
  187. *
  188. * @return The matching transaction instance, or NULL if transaction
  189. * can not be found.
  190. */
  191. PJ_DECL(pjsip_transaction*) pjsip_tsx_layer_find_tsx2( const pj_str_t *key,
  192. pj_bool_t add_ref );
  193. /**
  194. * Create, initialize, and register a new transaction as UAC from the
  195. * specified transmit data (\c tdata). The transmit data must have a valid
  196. * \c Request-Line and \c CSeq header.
  197. *
  198. * If \c Via header does not exist, it will be created along with a unique
  199. * \c branch parameter. If it exists and contains branch parameter, then
  200. * the \c branch parameter will be used as is as the transaction key. If
  201. * it exists but branch parameter doesn't exist, a unique branch parameter
  202. * will be created.
  203. *
  204. * @param tsx_user Module to be registered as transaction user of the new
  205. * transaction, which will receive notification from the
  206. * transaction via on_tsx_state() callback.
  207. * @param tdata The outgoing request message.
  208. * @param p_tsx On return will contain the new transaction instance.
  209. *
  210. * @return PJ_SUCCESS if successfull.
  211. */
  212. PJ_DECL(pj_status_t) pjsip_tsx_create_uac( pjsip_module *tsx_user,
  213. pjsip_tx_data *tdata,
  214. pjsip_transaction **p_tsx);
  215. /**
  216. * Variant of pjsip_tsx_create_uac() with additional parameter to specify
  217. * the group lock to use. Group lock can be used to synchronize locking
  218. * among several objects to prevent deadlock, and to synchronize the
  219. * lifetime of objects sharing the same group lock.
  220. *
  221. * See pjsip_tsx_create_uac() for general info about this function.
  222. *
  223. * @param tsx_user Module to be registered as transaction user of the new
  224. * transaction, which will receive notification from the
  225. * transaction via on_tsx_state() callback.
  226. * @param tdata The outgoing request message.
  227. * @param grp_lock Optional group lock to use by this transaction. If
  228. * the value is NULL, the transaction will create its
  229. * own group lock.
  230. * @param p_tsx On return will contain the new transaction instance.
  231. *
  232. * @return PJ_SUCCESS if successfull.
  233. */
  234. PJ_DECL(pj_status_t) pjsip_tsx_create_uac2(pjsip_module *tsx_user,
  235. pjsip_tx_data *tdata,
  236. pj_grp_lock_t *grp_lock,
  237. pjsip_transaction **p_tsx);
  238. /**
  239. * Create, initialize, and register a new transaction as UAS from the
  240. * specified incoming request in \c rdata. After calling this function,
  241. * application MUST call #pjsip_tsx_recv_msg() so that transaction
  242. * moves from state NULL.
  243. *
  244. * @param tsx_user Module to be registered as transaction user of the new
  245. * transaction, which will receive notification from the
  246. * transaction via on_tsx_state() callback.
  247. * @param rdata The received incoming request.
  248. * @param p_tsx On return will contain the new transaction instance.
  249. *
  250. * @return PJ_SUCCESS if successfull.
  251. */
  252. PJ_DECL(pj_status_t) pjsip_tsx_create_uas( pjsip_module *tsx_user,
  253. pjsip_rx_data *rdata,
  254. pjsip_transaction **p_tsx );
  255. /**
  256. * Variant of pjsip_tsx_create_uas() with additional parameter to specify
  257. * the group lock to use. Group lock can be used to synchronize locking
  258. * among several objects to prevent deadlock, and to synchronize the
  259. * lifetime of objects sharing the same group lock.
  260. *
  261. * See pjsip_tsx_create_uas() for general info about this function.
  262. *
  263. * @param tsx_user Module to be registered as transaction user of the new
  264. * transaction, which will receive notification from the
  265. * transaction via on_tsx_state() callback.
  266. * @param rdata The received incoming request.
  267. * @param grp_lock Optional group lock to use by this transaction. If
  268. * the value is NULL, the transaction will create its
  269. * own group lock.
  270. * @param p_tsx On return will contain the new transaction instance.
  271. *
  272. * @return PJ_SUCCESS if successfull.
  273. */
  274. PJ_DECL(pj_status_t) pjsip_tsx_create_uas2(pjsip_module *tsx_user,
  275. pjsip_rx_data *rdata,
  276. pj_grp_lock_t *grp_lock,
  277. pjsip_transaction **p_tsx );
  278. /**
  279. * Detect merged requests according to RFC 3261 section 8.2.2.2:
  280. * If the request has no tag in the To header field, the function will
  281. * check the request against ongoing transactions. If the From tag,
  282. * Call-ID, and CSeq exactly match those associated with an ongoing
  283. * transaction, but the request does not match that transaction based
  284. * on the matching rules described in section 17.2.3, the function
  285. * will return that transaction.
  286. *
  287. * @param rdata The received incoming request.
  288. *
  289. * @return The matching transaction, if any, or NULL.
  290. */
  291. PJ_DECL(pjsip_transaction *)
  292. pjsip_tsx_detect_merged_requests(pjsip_rx_data *rdata);
  293. /**
  294. * Lock/bind transaction to a specific transport/listener. This is optional,
  295. * as normally transport will be selected automatically based on the
  296. * destination of the message upon resolver completion.
  297. *
  298. * @param tsx The transaction.
  299. * @param sel Transport selector containing the specification of
  300. * transport or listener to be used by this transaction
  301. * to send requests.
  302. *
  303. * @return PJ_SUCCESS on success, or the appropriate error code.
  304. */
  305. PJ_DECL(pj_status_t) pjsip_tsx_set_transport(pjsip_transaction *tsx,
  306. const pjsip_tpselector *sel);
  307. /**
  308. * Call this function to manually feed a message to the transaction.
  309. * For UAS transaction, application MUST call this function after
  310. * UAS transaction has been created.
  311. *
  312. * This function SHOULD only be called to pass initial request message
  313. * to UAS transaction. Before this function returns, on_tsx_state()
  314. * callback of the transaction user will be called. If response message
  315. * is passed to this function, then on_rx_response() will also be called
  316. * before on_tsx_state().
  317. *
  318. * @param tsx The transaction.
  319. * @param rdata The message.
  320. */
  321. PJ_DECL(void) pjsip_tsx_recv_msg( pjsip_transaction *tsx,
  322. pjsip_rx_data *rdata);
  323. /**
  324. * Transmit message in tdata with this transaction. It is possible to
  325. * pass NULL in tdata for UAC transaction, which in this case the last
  326. * message transmitted, or the request message which was specified when
  327. * calling #pjsip_tsx_create_uac(), will be sent.
  328. *
  329. * This function decrements the reference counter of the transmit buffer
  330. * only when it returns PJ_SUCCESS;
  331. *
  332. * @param tsx The transaction.
  333. * @param tdata The outgoing message. If NULL is specified, then the
  334. * last message transmitted (or the message specified
  335. * in UAC initialization) will be sent.
  336. *
  337. * @return PJ_SUCCESS if successfull.
  338. */
  339. PJ_DECL(pj_status_t) pjsip_tsx_send_msg( pjsip_transaction *tsx,
  340. pjsip_tx_data *tdata);
  341. /**
  342. * Manually retransmit the last message transmitted by this transaction,
  343. * without updating the transaction state. This function is useful when
  344. * TU wants to maintain the retransmision by itself (for example,
  345. * retransmitting reliable provisional response).
  346. *
  347. * @param tsx The transaction.
  348. * @param tdata The outgoing message. If NULL is specified, then the
  349. * last message transmitted (or the message specified
  350. * in UAC initialization) will be sent.
  351. *
  352. *
  353. * @return PJ_SUCCESS if successful.
  354. */
  355. PJ_DECL(pj_status_t) pjsip_tsx_retransmit_no_state(pjsip_transaction *tsx,
  356. pjsip_tx_data *tdata);
  357. /**
  358. * Create transaction key, which is used to match incoming requests
  359. * or response (retransmissions) against transactions.
  360. *
  361. * @param pool The pool
  362. * @param key Output key.
  363. * @param role The role of the transaction.
  364. * @param method The method to be put as a key.
  365. * @param rdata The received data to calculate.
  366. *
  367. * @return PJ_SUCCESS or the appropriate error code.
  368. */
  369. PJ_DECL(pj_status_t) pjsip_tsx_create_key( pj_pool_t *pool,
  370. pj_str_t *key,
  371. pjsip_role_e role,
  372. const pjsip_method *method,
  373. const pjsip_rx_data *rdata );
  374. /**
  375. * Force terminate transaction.
  376. *
  377. * @param tsx The transaction.
  378. * @param code The status code to report.
  379. *
  380. * @return PJ_SUCCESS or the appropriate error code.
  381. */
  382. PJ_DECL(pj_status_t) pjsip_tsx_terminate( pjsip_transaction *tsx,
  383. int code );
  384. /**
  385. * Force terminate transaction asynchronously, using the transaction
  386. * internal timer.
  387. *
  388. * @param tsx The transaction.
  389. * @param code The status code to report.
  390. *
  391. * @return PJ_SUCCESS or the appropriate error code.
  392. */
  393. PJ_DECL(pj_status_t) pjsip_tsx_terminate_async(pjsip_transaction *tsx,
  394. int code );
  395. /**
  396. * Cease retransmission on the UAC transaction. The UAC transaction is
  397. * still considered running, and it will complete when either final
  398. * response is received or the transaction times out.
  399. *
  400. * This operation normally is used for INVITE transaction only, when
  401. * the transaction is cancelled before any provisional response has been
  402. * received.
  403. *
  404. * @param tsx The transaction.
  405. *
  406. * @return PJ_SUCCESS or the appropriate error code.
  407. */
  408. PJ_DECL(pj_status_t) pjsip_tsx_stop_retransmit(pjsip_transaction *tsx);
  409. /**
  410. * Start a timer to terminate transaction after the specified time
  411. * has elapsed. This function is only valid for INVITE transaction,
  412. * and only before final response is received for the INVITE transaction.
  413. * It is normally called after the UAC has sent CANCEL for this
  414. * INVITE transaction.
  415. *
  416. * The purpose of this function is to terminate the transaction if UAS
  417. * does not send final response to this INVITE transaction even after
  418. * it sends 200/OK to CANCEL (for example when the UAS complies to RFC
  419. * 2543).
  420. *
  421. * Once this timer is set, the transaction will be terminated either when
  422. * a final response is received or the timer expires.
  423. *
  424. * @param tsx The transaction.
  425. * @param millisec Timeout value in milliseconds.
  426. *
  427. * @return PJ_SUCCESS or the appropriate error code.
  428. */
  429. PJ_DECL(pj_status_t) pjsip_tsx_set_timeout(pjsip_transaction *tsx,
  430. unsigned millisec);
  431. /**
  432. * Change timer values used by transaction layer. Currently scheduled
  433. * timers will not be changed. Any value set to 0 will be left
  434. * unchanged.
  435. *
  436. * @param t1 Transaction T1 timeout, in msec
  437. * @param t2 Transaction T2 timeout, in msec
  438. * @param t4 Transaction completed timer for non-INVITE, in msec
  439. * @param td Transaction completed timer for INVITE, in msec
  440. */
  441. PJ_DECL(void) pjsip_tsx_set_timers(unsigned t1, unsigned t2, unsigned t4,
  442. unsigned td);
  443. /**
  444. * (Re)Initializes timer values from pjsip_cfg().
  445. */
  446. PJ_DECL(void) pjsip_tsx_initialize_timer_values(void);
  447. /**
  448. * Set maximum retransmission count in transaction layer for outgoing
  449. * requests. When retransmission counter reaches the specified number,
  450. * the transaction will be considered timeout. This will affect any
  451. * ongoing transactions immediately.
  452. *
  453. * By default (or when this is set to -1), retransmission number will
  454. * not cause a transaction to be timeout, only the timer settings will
  455. * cause transaction timeout. Also, this will not override the timer
  456. * settings, i.e: if the timeout timer occurs before the maximum
  457. * retransmission is reached, the transaction will still gets timeout.
  458. *
  459. * When this is set to zero or possitive number P, a transaction timeout
  460. * will occur right before the retransmission number (P+1). For example,
  461. * if this is set to 1 there will be two transmissions: the initial
  462. * transmission and one retransmission, before the transaction gets timeout.
  463. *
  464. * @param max Maximum retransmission count.
  465. */
  466. PJ_DECL(void) pjsip_tsx_set_max_retransmit_count(int max);
  467. /**
  468. * Get the transaction instance in the incoming message. If the message
  469. * has a corresponding transaction, this function will return non NULL
  470. * value.
  471. *
  472. * @param rdata The incoming message buffer.
  473. *
  474. * @return The transaction instance associated with this message,
  475. * or NULL if the message doesn't match any transactions.
  476. */
  477. PJ_DECL(pjsip_transaction*) pjsip_rdata_get_tsx( pjsip_rx_data *rdata );
  478. /**
  479. * @}
  480. */
  481. /*
  482. * Internal.
  483. */
  484. /**
  485. * Dump transaction layer.
  486. */
  487. PJ_DECL(void) pjsip_tsx_layer_dump(pj_bool_t detail);
  488. /**
  489. * Get the string name for the state.
  490. * @param state State
  491. */
  492. PJ_DECL(const char *) pjsip_tsx_state_str(pjsip_tsx_state_e state);
  493. /**
  494. * Get the role name.
  495. * @param role Role.
  496. */
  497. PJ_DECL(const char *) pjsip_role_name(pjsip_role_e role);
  498. PJ_END_DECL
  499. #endif /* __PJSIP_TRANSACT_H__ */