sip_replaces.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_REPLACES_H__
  20. #define __PJSIP_REPLACES_H__
  21. /**
  22. * @file sip_replaces.h
  23. * @brief SIP Replaces support (RFC 3891 - SIP "Replaces" Header)
  24. */
  25. #include <pjsip/sip_msg.h>
  26. /**
  27. * @defgroup PJSIP_REPLACES SIP Replaces support (RFC 3891 - "Replaces" Header)
  28. * @ingroup PJSIP_HIGH_UA
  29. * @brief SIP Replaces support (RFC 3891 - "Replaces" Header)
  30. * @{
  31. *
  32. * This module implements support for Replaces header in PJSIP. The Replaces
  33. * specification is written in RFC 3891 - The Session Initiation Protocol (SIP)
  34. * "Replaces" Header, and can be used to enable a variety of features,
  35. * for example: "Attended Transfer" and "Call Pickup".
  36. *
  37. *
  38. *
  39. * \section PJSIP_REPLACES_USING_SEC Using PJSIP Replaces Support
  40. *
  41. * \subsection PJSIP_REPLACES_INIT_SUBSEC Initialization
  42. *
  43. * Application needs to call #pjsip_replaces_init_module() during application
  44. * initialization stage to register "replaces" support in PJSIP.
  45. *
  46. *
  47. *
  48. * \subsection PJSIP_REPLACES_UAC_SUBSEC UAC Behavior: Sending a Replaces Header
  49. *
  50. * A User Agent that wishes to replace a single existing early or
  51. * confirmed dialog with a new dialog of its own, MAY send the target
  52. * User Agent an INVITE request containing a Replaces header field. The
  53. * User Agent Client (UAC) places the Call-ID, to-tag, and from-tag
  54. * information for the target dialog in a single Replaces header field
  55. * and sends the new INVITE to the target.
  56. *
  57. * To initiate outgoing INVITE request with Replaces header, application
  58. * would create the INVITE request with #pjsip_inv_invite(), then adds
  59. * #pjsip_replaces_hdr instance into the request, filling up the Call-ID,
  60. * To-tag, and From-tag properties of the header with the identification
  61. * of the dialog to be replaced. Application may also optionally
  62. * set the \a early_only property of the header to indicate that it only
  63. * wants to replace early dialog.
  64. *
  65. * Note that when the outgoing INVITE request (with Replaces) is initiated
  66. * from an incoming REFER request (as in Attended Call Transfer case),
  67. * this process should be done rather more automatically by PJSIP. Upon
  68. * receiving incoming incoming REFER request, normally these processes
  69. * will be performed:
  70. * - Application finds \a Refer-To header,
  71. * - Application creates outgoing dialog/invite session, specifying
  72. * the URI in the \a Refer-To header as the initial remote target,
  73. * - The URI in the \a Refer-To header may contain header parameters such
  74. * as \a Replaces and \a Require headers.
  75. * - The dialog keeps the header fields in the header parameters
  76. * of the URI, and the invite session would add these headers into
  77. * the outgoing INVITE request. Because of this, the outgoing
  78. * INVITE request will contain the \a Replaces and \a Require headers.
  79. *
  80. *
  81. * For more information, please see the implementation of
  82. * #pjsua_call_xfer_replaces() in \ref PJSUA_LIB source code.
  83. *
  84. *
  85. * \subsection PJSIP_REPLACES_UAS_SUBSEC UAS Behavior: Receiving a Replaces Header
  86. *
  87. * The Replaces header contains information used to match an existing
  88. * SIP dialog (call-id, to-tag, and from-tag). Upon receiving an INVITE
  89. * with a Replaces header, the User Agent (UA) attempts to match this
  90. * information with a confirmed or early dialog.
  91. *
  92. * In PJSIP, if application wants to process the Replaces header in the
  93. * incoming INVITE request, it should call #pjsip_replaces_verify_request()
  94. * before creating the INVITE session. The #pjsip_replaces_verify_request()
  95. * function checks and verifies the request to see if Replaces request
  96. * can be processed. To be more specific, it performs the following
  97. * verification:
  98. * - checks that Replaces header is present. If not, the function will
  99. * return PJ_SUCCESS without doing anything.
  100. * - checks that no duplicate Replaces headers are present, or otherwise
  101. * it will return 400 "Bad Request" response.
  102. * - checks for matching dialog and verifies that the invite session has
  103. * the correct state, and may return 481 "Call/Transaction Does Not Exist",
  104. * 603 "Declined", or 486 "Busy Here" according to the processing rules
  105. * specified in RFC 3891.
  106. * - if matching dialog with correct state is found, it will give PJ_SUCCESS
  107. * status and return the matching dialog back to the application.
  108. *
  109. * The following pseudocode illustrates how application can process the
  110. * incoming INVITE if it wants to support Replaces extension:
  111. *
  112. \code
  113. // Incoming INVITE request handler
  114. pj_bool_t on_rx_invite(pjsip_rx_data *rdata)
  115. {
  116. pjsip_dialog *dlg, *replaced_dlg;
  117. pjsip_inv_session *inv;
  118. pjsip_tx_data *response;
  119. pj_status_t status;
  120. // Check whether Replaces header is present in the request and process accordingly.
  121. //
  122. status = pjsip_replaces_verify_request(rdata, &replaced_dlg, PJ_FALSE, &response);
  123. if (status != PJ_SUCCESS) {
  124. // Something wrong with Replaces request.
  125. //
  126. pj_status_t status;
  127. if (response) {
  128. status = pjsip_endpt_send_response(endpt, rdata, response, NULL, NULL);
  129. if (status != PJ_SUCCESS) pjsip_tx_data_dec_ref(tdata);
  130. } else {
  131. // Respond with 500 (Internal Server Error)
  132. status = pjsip_endpt_respond_stateless(endpt, rdata, 500, NULL, NULL, NULL);
  133. if (status != PJ_SUCCESS) pjsip_tx_data_dec_ref(tdata);
  134. }
  135. }
  136. // Create UAS Invite session as usual.
  137. //
  138. status = pjsip_dlg_create_uas_and_inc_lock(.., rdata, .., &dlg);
  139. ..
  140. status = pjsip_inv_create_uas(dlg, .., &inv);
  141. // Send initial 100 "Trying" to the INVITE request
  142. //
  143. status = pjsip_inv_initial_answer(inv, rdata, 100, ..., &response);
  144. if (status == PJ_SUCCESS)
  145. pjsip_inv_send_msg(inv, response);
  146. // This is where processing is different between normal call
  147. // (without Replaces) and call with Replaces.
  148. //
  149. if (replaced_dlg) {
  150. pjsip_inv_session *replaced_inv;
  151. // Always answer the new INVITE with 200, regardless whether
  152. // the replaced call is in early or confirmed state.
  153. //
  154. status = pjsip_inv_answer(inv, 200, NULL, NULL, &response);
  155. if (status == PJ_SUCCESS)
  156. pjsip_inv_send_msg(inv, response);
  157. // Get the INVITE session associated with the replaced dialog.
  158. //
  159. replaced_inv = pjsip_dlg_get_inv_session(replaced_dlg);
  160. // Disconnect the "replaced" INVITE session.
  161. //
  162. status = pjsip_inv_end_session(replaced_inv, PJSIP_SC_GONE, NULL, &tdata);
  163. if (status == PJ_SUCCESS && tdata)
  164. status = pjsip_inv_send_msg(replaced_inv, tdata);
  165. // It's up to application to associate the new INVITE session
  166. // with the old (now terminated) session. For example, application
  167. // may assign the same User Interface object for the new INVITE
  168. // session.
  169. } else {
  170. // Process normal INVITE without Replaces.
  171. ...
  172. }
  173. }
  174. \endcode
  175. *
  176. *
  177. * For a complete sample implementation, please see \a pjsua_call_on_incoming()
  178. * function of \ref PJSUA_LIB in \a pjsua_call.c file.
  179. *
  180. *
  181. * \section PJSIP_REPLACES_REFERENCE References
  182. *
  183. * References:
  184. * - <A HREF="http://www.ietf.org/rfc/rfc3891.txt">RFC 3891: The Session
  185. * Initiation Protocol (SIP) "Replaces" Header</A>
  186. * - \ref PJSUA_XFER
  187. */
  188. PJ_BEGIN_DECL
  189. /**
  190. * Declaration of SIP Replaces header (RFC 3891).
  191. */
  192. typedef struct pjsip_replaces_hdr
  193. {
  194. /** Standard header field. */
  195. PJSIP_DECL_HDR_MEMBER(struct pjsip_replaces_hdr);
  196. /** Call-Id */
  197. pj_str_t call_id;
  198. /** to-tag */
  199. pj_str_t to_tag;
  200. /** from-tag */
  201. pj_str_t from_tag;
  202. /** early-only? */
  203. pj_bool_t early_only;
  204. /** Other parameters */
  205. pjsip_param other_param;
  206. } pjsip_replaces_hdr;
  207. /**
  208. * Initialize Replaces support in PJSIP. This would, among other things,
  209. * register the header parser for Replaces header.
  210. *
  211. * @param endpt The endpoint instance.
  212. *
  213. * @return PJ_SUCCESS on success.
  214. */
  215. PJ_DECL(pj_status_t) pjsip_replaces_init_module(pjsip_endpoint *endpt);
  216. /**
  217. * Create Replaces header.
  218. *
  219. * @param pool Pool to allocate the header instance from.
  220. *
  221. * @return An empty Replaces header instance.
  222. */
  223. PJ_DECL(pjsip_replaces_hdr*) pjsip_replaces_hdr_create(pj_pool_t *pool);
  224. /**
  225. * Verify that incoming request with Replaces header can be processed.
  226. * This function will perform all necessary checks according to RFC 3891
  227. * Section 3 "User Agent Server Behavior: Receiving a Replaces Header".
  228. *
  229. * @param rdata The incoming request to be verified.
  230. * @param p_dlg On return, it will be filled with the matching
  231. * dialog.
  232. * @param lock_dlg Specifies whether this function should acquire lock
  233. * to the matching dialog. If yes (and should be yes!),
  234. * then application will need to release the dialog's
  235. * lock with #pjsip_dlg_dec_lock() when the function
  236. * returns PJ_SUCCESS and the \a p_dlg parameter is filled
  237. * with the dialog instance.
  238. * @param p_tdata Upon error, it will be filled with the final response
  239. * to be sent to the request sender.
  240. *
  241. * @return The function returns the following:
  242. * - If the request doesn't contain Replaces header, the
  243. * function returns PJ_SUCCESS and \a p_dlg parameter
  244. * will be set to NULL.
  245. * - If the request contains Replaces header and a valid,
  246. * matching dialog is found, the function returns
  247. * PJ_SUCCESS and \a p_dlg parameter will be set to the
  248. * matching dialog instance.
  249. * - Upon error condition (as described by RFC 3891), the
  250. * function returns non-PJ_SUCCESS, and \a p_tdata
  251. * parameter SHOULD be set with a final response message
  252. * to be sent to the sender of the request.
  253. */
  254. PJ_DECL(pj_status_t) pjsip_replaces_verify_request(pjsip_rx_data *rdata,
  255. pjsip_dialog **p_dlg,
  256. pj_bool_t lock_dlg,
  257. pjsip_tx_data **p_tdata);
  258. PJ_END_DECL
  259. /**
  260. * @}
  261. */
  262. #endif /* __PJSIP_REPLACES_H__ */