Skip to content

Commit 55b117b

Browse files
Support non-default Max-Forward headers for B2BUA (#4473)
1 parent 3713752 commit 55b117b

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

pjsip/include/pjsip/print_util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#ifndef __PJSIP_PRINT_H__
2020
#define __PJSIP_PRINT_H__
2121

22+
PJ_BEGIN_DECL
23+
24+
2225
#define copy_advance_char_check(buf,chr) \
2326
do { \
2427
if (1 >= (endbuf-buf)) return -1; \
@@ -162,5 +165,8 @@ PJ_INLINE(void) init_hdr(void *hptr, pjsip_hdr_e htype, void *vptr)
162165
pj_list_init(hdr);
163166
}
164167

168+
169+
PJ_END_DECL
170+
165171
#endif /* __PJSIP_PRINT_H__ */
166172

pjsip/include/pjsua-lib/pjsua.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,9 @@ struct pjsua_msg_data
26702670
* headers to the list by creating the header, either from the heap/pool
26712671
* or from temporary local variable, and add the header using
26722672
* linked list operation. See pjsua_app.c for some sample codes.
2673+
*
2674+
* Application may override Max-Forwards header value (the default is
2675+
* #PJSIP_MAX_FORWARDS_VALUE) by adding a Max-Forwards header here.
26732676
*/
26742677
pjsip_hdr hdr_list;
26752678

pjsip/include/pjsua2/siptypes.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,9 @@ struct SipTxOption
998998

999999
/**
10001000
* Additional message headers to be included in the outgoing message.
1001+
*
1002+
* Application may override Max-Forwards header value (the default is
1003+
* #PJSIP_MAX_FORWARDS_VALUE) by adding a Max-Forwards header here.
10011004
*/
10021005
SipHeaderVector headers;
10031006

pjsip/src/pjsua-lib/pjsua_core.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3165,6 +3165,29 @@ void pjsua_process_msg_data(pjsip_tx_data *tdata,
31653165
while (hdr && hdr != &msg_data->hdr_list) {
31663166
pjsip_hdr *new_hdr;
31673167

3168+
/* For Max-Forwards header, just update the value of the existing
3169+
* header. In case it does not exist, clone the header as usual.
3170+
*/
3171+
if (hdr->type == PJSIP_H_MAX_FORWARDS) {
3172+
pjsip_max_fwd_hdr *orig_hdr;
3173+
3174+
orig_hdr = pjsip_hdr_find(&tdata->msg->hdr, PJSIP_H_MAX_FORWARDS,
3175+
NULL);
3176+
if (orig_hdr != NULL) {
3177+
pj_uint32_t orig_value = orig_hdr->ivalue;
3178+
pj_uint32_t new_value =
3179+
((const pjsip_max_fwd_hdr*)hdr)->ivalue;
3180+
3181+
orig_hdr->ivalue = new_value;
3182+
PJ_LOG(4, (THIS_FILE,
3183+
"Overriding Max-Forwards header value: %u -> %u",
3184+
orig_value, new_value));
3185+
3186+
hdr = hdr->next;
3187+
continue;
3188+
}
3189+
}
3190+
31683191
new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
31693192
pjsip_msg_add_hdr(tdata->msg, new_hdr);
31703193

pjsip/src/pjsua2/siptypes.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include <pjsua2/types.hpp>
1919
#include <pjsua2/siptypes.hpp>
20+
#include <pjsip/print_util.h> /* For pjsip_hdr_names */
2021
#include "util.hpp"
2122

2223
using namespace pj;
@@ -788,6 +789,24 @@ void SipTxOption::toPj(pjsua_msg_data &msg_data) const
788789
pj_list_init(&msg_data.hdr_list);
789790
for (i = 0; i < headers.size(); i++) {
790791
pjsip_generic_string_hdr& pj_hdr = headers[i].toPj();
792+
793+
/* If the header is Max-Forwards, the header type needs to be
794+
* PJSIP_H_MAX_FORWARDS as PJSUA will compare the header type
795+
* instead of the string name.
796+
*/
797+
if ((headers[i].hName.size() ==
798+
pjsip_hdr_names[PJSIP_H_MAX_FORWARDS].name_len) &&
799+
(pj_ansi_strnicmp(headers[i].hName.c_str(),
800+
pjsip_hdr_names[PJSIP_H_MAX_FORWARDS].name,
801+
headers[i].hName.size()) == 0))
802+
{
803+
pjsip_max_fwd_hdr *tmp = (pjsip_max_fwd_hdr*)&pj_hdr;
804+
805+
pj_assert(sizeof(pjsip_generic_string_hdr) >=
806+
sizeof(pjsip_max_fwd_hdr));
807+
pjsip_max_fwd_hdr_init(NULL, tmp, std::stoi(headers[i].hValue));
808+
}
809+
791810
pj_list_push_back(&msg_data.hdr_list, &pj_hdr);
792811
}
793812

0 commit comments

Comments
 (0)