1
/* $Id: sip_util_proxy.c 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
#include <pjsip/sip_util.h>
21
#include <pjsip/sip_endpoint.h>
22
#include <pjsip/sip_errno.h>
23
#include <pjsip/sip_msg.h>
24
#include <pj/assert.h>
26
#include <pj/except.h>
29
#include <pj/string.h>
30
#include <pjlib-util/md5.h>
34
* Clone the incoming SIP request or response message. A forwarding proxy
35
* typically would need to clone the incoming SIP message before processing
38
* Once a transmit data is created, the reference counter is initialized to 1.
40
* @param endpt The endpoint instance.
41
* @param rdata The incoming SIP message.
42
* @param p_tdata Pointer to receive the transmit data containing
43
* the duplicated message.
45
* @return PJ_SUCCESS on success.
48
PJ_DEF(pj_status_t) pjsip_endpt_clone_msg( pjsip_endpoint *endpt,
49
const pjsip_rx_data *rdata,
50
pjsip_tx_data **p_tdata)
55
status = pjsip_endpt_create_tdata(endpt, &tdata);
56
if (status != PJ_SUCCESS)
59
tdata->msg = pjsip_msg_clone(tdata->pool, rdata->msg_info.msg);
61
pjsip_tx_data_add_ref(tdata);
71
* Create new request message to be forwarded upstream to new destination URI
74
PJ_DEF(pj_status_t) pjsip_endpt_create_request_fwd(pjsip_endpoint *endpt,
77
const pj_str_t *branch,
79
pjsip_tx_data **p_tdata)
86
PJ_ASSERT_RETURN(endpt && rdata && p_tdata, PJ_EINVAL);
87
PJ_ASSERT_RETURN(rdata->msg_info.msg->type == PJSIP_REQUEST_MSG,
88
PJSIP_ENOTREQUESTMSG);
90
PJ_UNUSED_ARG(options);
93
/* Request forwarding rule in RFC 3261 section 16.6:
95
* For each target, the proxy forwards the request following these
98
* 1. Make a copy of the received request
99
* 2. Update the Request-URI
100
* 3. Update the Max-Forwards header field
101
* 4. Optionally add a Record-route header field value
102
* 5. Optionally add additional header fields
103
* 6. Postprocess routing information
104
* 7. Determine the next-hop address, port, and transport
105
* 8. Add a Via header field value
106
* 9. Add a Content-Length header field if necessary
107
* 10. Forward the new request
109
* Of these steps, we only do step 1-3, since the later will be
110
* done by application.
113
status = pjsip_endpt_create_tdata(endpt, &tdata);
114
if (status != PJ_SUCCESS)
117
/* Always increment ref counter to 1 */
118
pjsip_tx_data_add_ref(tdata);
120
/* Duplicate the request */
123
const pjsip_msg *src = rdata->msg_info.msg;
124
const pjsip_hdr *hsrc;
126
/* Create the request */
127
tdata->msg = dst = pjsip_msg_create(tdata->pool, PJSIP_REQUEST_MSG);
129
/* Duplicate request method */
130
pjsip_method_copy(tdata->pool, &tdata->msg->line.req.method,
131
&src->line.req.method);
133
/* Set request URI */
135
dst->line.req.uri = (pjsip_uri*)
136
pjsip_uri_clone(tdata->pool, uri);
138
dst->line.req.uri= (pjsip_uri*)
139
pjsip_uri_clone(tdata->pool, src->line.req.uri);
142
/* Clone ALL headers */
143
hsrc = src->hdr.next;
144
while (hsrc != &src->hdr) {
148
/* If this is the top-most Via header, insert our own before
149
* cloning the header.
151
if (hsrc == (pjsip_hdr*)rdata->msg_info.via) {
153
hvia = pjsip_via_hdr_create(tdata->pool);
155
pj_strdup(tdata->pool, &hvia->branch_param, branch);
157
pj_str_t new_branch = pjsip_calculate_branch_id(rdata);
158
pj_strdup(tdata->pool, &hvia->branch_param, &new_branch);
160
pjsip_msg_add_hdr(dst, (pjsip_hdr*)hvia);
163
/* Skip Content-Type and Content-Length as these would be
164
* generated when the the message is printed.
166
else if (hsrc->type == PJSIP_H_CONTENT_LENGTH ||
167
hsrc->type == PJSIP_H_CONTENT_TYPE) {
174
/* If this is the top-most Route header and it indicates loose
175
* route, remove the header.
177
else if (hsrc == (pjsip_hdr*)rdata->msg_info.route) {
179
const pjsip_route_hdr *hroute = (const pjsip_route_hdr*) hsrc;
180
const pjsip_sip_uri *sip_uri;
182
if (!PJSIP_URI_SCHEME_IS_SIP(hroute->name_addr.uri) &&
183
!PJSIP_URI_SCHEME_IS_SIPS(hroute->name_addr.uri))
185
/* This is a bad request! */
186
status = PJSIP_EINVALIDHDR;
190
sip_uri = (pjsip_sip_uri*) hroute->name_addr.uri;
192
if (sip_uri->lr_param) {
193
/* Yes lr param is present, skip this Route header */
200
/* Clone the header */
201
hdst = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hsrc);
203
/* If this is Max-Forward header, decrement the value */
204
if (hdst->type == PJSIP_H_MAX_FORWARDS) {
205
pjsip_max_fwd_hdr *hmaxfwd = (pjsip_max_fwd_hdr*)hdst;
209
/* Append header to new request */
210
pjsip_msg_add_hdr(dst, hdst);
217
* If the copy does not contain a Max-Forwards header field, the
218
* proxy MUST add one with a field value, which SHOULD be 70.
220
if (rdata->msg_info.max_fwd == NULL) {
221
pjsip_max_fwd_hdr *hmaxfwd =
222
pjsip_max_fwd_hdr_create(tdata->pool, 70);
223
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hmaxfwd);
226
/* Clone request body */
228
dst->body = pjsip_msg_body_clone(tdata->pool, src->body);
244
pjsip_tx_data_dec_ref(tdata);
249
PJ_DEF(pj_status_t) pjsip_endpt_create_response_fwd( pjsip_endpoint *endpt,
250
pjsip_rx_data *rdata,
252
pjsip_tx_data **p_tdata)
254
pjsip_tx_data *tdata;
258
PJ_UNUSED_ARG(options);
260
status = pjsip_endpt_create_tdata(endpt, &tdata);
261
if (status != PJ_SUCCESS)
264
pjsip_tx_data_add_ref(tdata);
268
const pjsip_msg *src = rdata->msg_info.msg;
269
const pjsip_hdr *hsrc;
271
/* Create the request */
272
tdata->msg = dst = pjsip_msg_create(tdata->pool, PJSIP_RESPONSE_MSG);
274
/* Clone the status line */
275
dst->line.status.code = src->line.status.code;
276
pj_strdup(tdata->pool, &dst->line.status.reason,
277
&src->line.status.reason);
279
/* Duplicate all headers */
280
hsrc = src->hdr.next;
281
while (hsrc != &src->hdr) {
283
/* Skip Content-Type and Content-Length as these would be
284
* generated when the the message is printed.
286
if (hsrc->type == PJSIP_H_CONTENT_LENGTH ||
287
hsrc->type == PJSIP_H_CONTENT_TYPE) {
293
/* Remove the first Via header */
294
else if (hsrc == (pjsip_hdr*) rdata->msg_info.via) {
300
pjsip_msg_add_hdr(dst,
301
(pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hsrc));
306
/* Clone message body */
308
dst->body = pjsip_msg_body_clone(tdata->pool, src->body);
322
pjsip_tx_data_dec_ref(tdata);
327
static void digest2str(const unsigned char digest[], char *output)
330
for (i = 0; i<16; ++i) {
331
pj_val_to_hex_digit(digest[i], output);
337
PJ_DEF(pj_str_t) pjsip_calculate_branch_id( pjsip_rx_data *rdata )
340
pj_uint8_t digest[16];
342
pj_str_t rfc3261_branch = {PJSIP_RFC3261_BRANCH_ID,
343
PJSIP_RFC3261_BRANCH_LEN};
345
/* If incoming request does not have RFC 3261 branch value, create
346
* a branch value from GUID .
348
if (pj_strncmp(&rdata->msg_info.via->branch_param,
349
&rfc3261_branch, PJSIP_RFC3261_BRANCH_LEN) != 0 )
354
pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_BRANCH_LEN);
355
branch.slen = PJSIP_RFC3261_BRANCH_LEN;
356
pj_memcpy(branch.ptr, PJSIP_RFC3261_BRANCH_ID,
357
PJSIP_RFC3261_BRANCH_LEN);
359
tmp.ptr = branch.ptr + PJSIP_RFC3261_BRANCH_LEN + 2;
360
*(tmp.ptr-2) = (pj_int8_t)(branch.slen+73);
361
*(tmp.ptr-1) = (pj_int8_t)(branch.slen+99);
362
pj_generate_unique_string( &tmp );
364
branch.slen = PJSIP_MAX_BRANCH_LEN;
368
/* Create branch ID for new request by calculating MD5 hash
369
* of the branch parameter in top-most Via header.
372
pj_md5_update(&ctx, (pj_uint8_t*)rdata->msg_info.via->branch_param.ptr,
373
rdata->msg_info.via->branch_param.slen);
374
pj_md5_final(&ctx, digest);
377
pj_pool_alloc(rdata->tp_info.pool,
378
34 + PJSIP_RFC3261_BRANCH_LEN);
379
pj_memcpy(branch.ptr, PJSIP_RFC3261_BRANCH_ID, PJSIP_RFC3261_BRANCH_LEN);
380
branch.slen = PJSIP_RFC3261_BRANCH_LEN;
381
*(branch.ptr+PJSIP_RFC3261_BRANCH_LEN) = (pj_int8_t)(branch.slen+73);
382
*(branch.ptr+PJSIP_RFC3261_BRANCH_LEN+1) = (pj_int8_t)(branch.slen+99);
383
digest2str(digest, branch.ptr+PJSIP_RFC3261_BRANCH_LEN+2);
384
branch.slen = 34 + PJSIP_RFC3261_BRANCH_LEN;