1
/* $Id: sip_replaces.h 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJSIP_REPLACES_H__
21
#define __PJSIP_REPLACES_H__
25
* @file sip_replaces.h
26
* @brief SIP Replaces support (RFC 3891 - SIP "Replaces" Header)
28
#include <pjsip/sip_msg.h>
31
* @defgroup PJSIP_REPLACES SIP Replaces support (RFC 3891 - "Replaces" Header)
32
* @ingroup PJSIP_HIGH_UA
33
* @brief SIP Replaces support (RFC 3891 - "Replaces" Header)
36
* This module implements support for Replaces header in PJSIP. The Replaces
37
* specification is written in RFC 3891 - The Session Initiation Protocol (SIP)
38
* "Replaces" Header, and can be used to enable a variety of features,
39
* for example: "Attended Transfer" and "Call Pickup".
43
* \section PJSIP_REPLACES_USING_SEC Using PJSIP Replaces Support
45
* \subsection PJSIP_REPLACES_INIT_SUBSEC Initialization
47
* Application needs to call #pjsip_replaces_init_module() during application
48
* initialization stage to register "replaces" support in PJSIP.
52
* \subsection PJSIP_REPLACES_UAC_SUBSEC UAC Behavior: Sending a Replaces Header
54
* A User Agent that wishes to replace a single existing early or
55
* confirmed dialog with a new dialog of its own, MAY send the target
56
* User Agent an INVITE request containing a Replaces header field. The
57
* User Agent Client (UAC) places the Call-ID, to-tag, and from-tag
58
* information for the target dialog in a single Replaces header field
59
* and sends the new INVITE to the target.
61
* To initiate outgoing INVITE request with Replaces header, application
62
* would create the INVITE request with #pjsip_inv_invite(), then adds
63
* #pjsip_replaces_hdr instance into the request, filling up the Call-ID,
64
* To-tag, and From-tag properties of the header with the identification
65
* of the dialog to be replaced. Application may also optionally
66
* set the \a early_only property of the header to indicate that it only
67
* wants to replace early dialog.
69
* Note that when the outgoing INVITE request (with Replaces) is initiated
70
* from an incoming REFER request (as in Attended Call Transfer case),
71
* this process should be done rather more automatically by PJSIP. Upon
72
* receiving incoming incoming REFER request, normally these processes
74
* - Application finds \a Refer-To header,
75
* - Application creates outgoing dialog/invite session, specifying
76
* the URI in the \a Refer-To header as the initial remote target,
77
* - The URI in the \a Refer-To header may contain header parameters such
78
* as \a Replaces and \a Require headers.
79
* - The dialog keeps the header fields in the header parameters
80
* of the URI, and the invite session would add these headers into
81
* the outgoing INVITE request. Because of this, the outgoing
82
* INVITE request will contain the \a Replaces and \a Require headers.
85
* For more information, please see the implementation of
86
* #pjsua_call_xfer_replaces() in \ref PJSUA_LIB source code.
89
* \subsection PJSIP_REPLACES_UAS_SUBSEC UAS Behavior: Receiving a Replaces Header
91
* The Replaces header contains information used to match an existing
92
* SIP dialog (call-id, to-tag, and from-tag). Upon receiving an INVITE
93
* with a Replaces header, the User Agent (UA) attempts to match this
94
* information with a confirmed or early dialog.
96
* In PJSIP, if application wants to process the Replaces header in the
97
* incoming INVITE request, it should call #pjsip_replaces_verify_request()
98
* before creating the INVITE session. The #pjsip_replaces_verify_request()
99
* function checks and verifies the request to see if Replaces request
100
* can be processed. To be more specific, it performs the following
102
* - checks that Replaces header is present. If not, the function will
103
* return PJ_SUCCESS without doing anything.
104
* - checks that no duplicate Replaces headers are present, or otherwise
105
* it will return 400 "Bad Request" response.
106
* - checks for matching dialog and verifies that the invite session has
107
* the correct state, and may return 481 "Call/Transaction Does Not Exist",
108
* 603 "Declined", or 486 "Busy Here" according to the processing rules
109
* specified in RFC 3891.
110
* - if matching dialog with correct state is found, it will give PJ_SUCCESS
111
* status and return the matching dialog back to the application.
113
* The following pseudocode illustrates how application can process the
114
* incoming INVITE if it wants to support Replaces extension:
117
// Incoming INVITE request handler
118
pj_bool_t on_rx_invite(pjsip_rx_data *rdata)
120
pjsip_dialog *dlg, *replaced_dlg;
121
pjsip_inv_session *inv;
122
pjsip_tx_data *response;
125
// Check whether Replaces header is present in the request and process accordingly.
127
status = pjsip_replaces_verify_request(rdata, &replaced_dlg, PJ_FALSE, &response);
128
if (status != PJ_SUCCESS) {
129
// Something wrong with Replaces request.
132
pjsip_endpt_send_response(endpt, rdata, response, NULL, NULL);
134
// Respond with 500 (Internal Server Error)
135
pjsip_endpt_respond_stateless(endpt, rdata, 500, NULL, NULL, NULL);
139
// Create UAS Invite session as usual.
141
status = pjsip_dlg_create_uas(.., rdata, .., &dlg);
143
status = pjsip_inv_create_uas(dlg, .., &inv);
145
// Send initial 100 "Trying" to the INVITE request
147
status = pjsip_inv_initial_answer(inv, rdata, 100, ..., &response);
148
if (status == PJ_SUCCESS)
149
pjsip_inv_send_msg(inv, response);
152
// This is where processing is different between normal call
153
// (without Replaces) and call with Replaces.
156
pjsip_inv_session *replaced_inv;
158
// Always answer the new INVITE with 200, regardless whether
159
// the replaced call is in early or confirmed state.
161
status = pjsip_inv_answer(inv, 200, NULL, NULL, &response);
162
if (status == PJ_SUCCESS)
163
pjsip_inv_send_msg(inv, response);
166
// Get the INVITE session associated with the replaced dialog.
168
replaced_inv = pjsip_dlg_get_inv_session(replaced_dlg);
171
// Disconnect the "replaced" INVITE session.
173
status = pjsip_inv_end_session(replaced_inv, PJSIP_SC_GONE, NULL, &tdata);
174
if (status == PJ_SUCCESS && tdata)
175
status = pjsip_inv_send_msg(replaced_inv, tdata);
178
// It's up to application to associate the new INVITE session
179
// with the old (now terminated) session. For example, application
180
// may assign the same User Interface object for the new INVITE
184
// Process normal INVITE without Replaces.
192
* For a complete sample implementation, please see \a pjsua_call_on_incoming()
193
* function of \ref PJSUA_LIB in \a pjsua_call.c file.
196
* \section PJSIP_REPLACES_REFERENCE References
199
* - <A HREF="http://www.ietf.org/rfc/rfc3891.txt">RFC 3891: The Session
200
* Initiation Protocol (SIP) "Replaces" Header</A>
208
* Declaration of SIP Replaces header (RFC 3891).
210
typedef struct pjsip_replaces_hdr
212
/** Standard header field. */
213
PJSIP_DECL_HDR_MEMBER(struct pjsip_replaces_hdr);
225
pj_bool_t early_only;
227
/** Other parameters */
228
pjsip_param other_param;
230
} pjsip_replaces_hdr;
235
* Initialize Replaces support in PJSIP. This would, among other things,
236
* register the header parser for Replaces header.
238
* @param endpt The endpoint instance.
240
* @return PJ_SUCCESS on success.
242
PJ_DECL(pj_status_t) pjsip_replaces_init_module(pjsip_endpoint *endpt);
246
* Create Replaces header.
248
* @param pool Pool to allocate the header instance from.
250
* @return An empty Replaces header instance.
252
PJ_DECL(pjsip_replaces_hdr*) pjsip_replaces_hdr_create(pj_pool_t *pool);
256
* Verify that incoming request with Replaces header can be processed.
257
* This function will perform all necessary checks according to RFC 3891
258
* Section 3 "User Agent Server Behavior: Receiving a Replaces Header".
260
* @param rdata The incoming request to be verified.
261
* @param p_dlg On return, it will be filled with the matching
263
* @param lock_dlg Specifies whether this function should acquire lock
264
* to the matching dialog. If yes (and should be yes!),
265
* then application will need to release the dialog's
266
* lock with #pjsip_dlg_dec_lock() when the function
267
* returns PJ_SUCCESS and the \a p_dlg parameter is filled
268
* with the dialog instance.
269
* @param p_tdata Upon error, it will be filled with the final response
270
* to be sent to the request sender.
272
* @return The function returns the following:
273
* - If the request doesn't contain Replaces header, the
274
* function returns PJ_SUCCESS and \a p_dlg parameter
275
* will be set to NULL.
276
* - If the request contains Replaces header and a valid,
277
* matching dialog is found, the function returns
278
* PJ_SUCCESS and \a p_dlg parameter will be set to the
279
* matching dialog instance.
280
* - Upon error condition (as described by RFC 3891), the
281
* function returns non-PJ_SUCCESS, and \a p_tdata
282
* parameter SHOULD be set with a final response message
283
* to be sent to the sender of the request.
285
PJ_DECL(pj_status_t) pjsip_replaces_verify_request(pjsip_rx_data *rdata,
286
pjsip_dialog **p_dlg,
288
pjsip_tx_data **p_tdata);
300
#endif /* __PJSIP_REPLACES_H__ */