Skip to content

Commit

Permalink
TELECOM-9088: topology_hiding: force SIPS in contact header when needed
Browse files Browse the repository at this point in the history
https://www.rfc-editor.org/rfc/rfc3261.html#section-12.1.1

"If the request that initiated the dialog contained a SIPS URI in the
Request-URI or in the top Record-Route header field value, if there was
any, or the Contact header field if there was no Record-Route header
field, the Contact header field in the response MUST be a SIPS URI."
  • Loading branch information
benceszigeti committed Nov 30, 2023
1 parent 957fc1d commit 1153353
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions modules/dialog/dlg_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ struct dlg_cell
int rt_on_timeout;
int rt_on_hangup;

unsigned int force_sips_contact;

#ifdef DBG_DIALOG
struct struct_hist *hist;
#endif
Expand Down
41 changes: 37 additions & 4 deletions modules/topology_hiding/topo_hiding_logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,26 @@ static char * topo_ct_param_copy(char *buf, str *name, str *val, int should_quot
return buf;
}

static int is_forced_sips_contact_required(const struct sip_msg* msg, const struct dlg_cell* dlg, const uri_type ct_type) {
str top_rr;

if (dlg->state != DLG_STATE_UNCONFIRMED) return 0;

// RURI is SIPS
if (msg->parsed_uri_ok && msg->parsed_uri.type == SIPS_URI_T) return 1;

if (msg->record_route == NULL) {
// RR is NULL and Contact is SIPS
if (ct_type == SIPS_URI_T) return 1;
} else {
// Top RR is SIPS
top_rr = ((rr_t*)msg->record_route->parsed)->nameaddr.uri;
if ((top_rr.len > 5 && strncmp(top_rr.s, "sips:", 5) == 0)) return 1;
}

return 0;
}

static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
{
char *prefix=NULL,*suffix=NULL,*p,*p_init,*ct_username=NULL;
Expand All @@ -379,6 +399,7 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
param_t *it;
str *rr_param;
struct lump* lump;
uri_type ct_type = ERROR_URI_T;

if(!msg->contact)
{
Expand Down Expand Up @@ -408,6 +429,8 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
} else {
ct_username = ctu.user.s;
ct_username_len = ctu.user.len;
ct_type = ctu.type;
if (ct_type == SIPS_URI_T) prefix_len += 1;
LM_DBG("Trying to propagate username [%.*s]\n",ct_username_len,
ct_username);
if (ct_username_len > 0) {
Expand All @@ -419,6 +442,8 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
if (dlg_api.is_mod_flag_set(dlg,TOPOH_DID_IN_USER))
prefix_len += RR_DLG_PARAM_SIZE + 1;

if (dlg->force_sips_contact && ct_type == SIP_URI_T) prefix_len += 1;

prefix = pkg_malloc(prefix_len);
if (!prefix) {
LM_ERR("no more pkg\n");
Expand Down Expand Up @@ -474,14 +499,22 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
rr_param = dlg_api.get_rr_param();

p = prefix;
memcpy( p, "<sip:", 5);
p += 5;
if (dlg->force_sips_contact) {
memcpy(p, "<sips:", 6);
p += 6;
} else {
memcpy(p, "<sip:", 5);
p += 5;
}

if (is_forced_sips_contact_required(msg, dlg, ct_type)) dlg->force_sips_contact = 1;

if (dlg_api.is_mod_flag_set(dlg,TOPOH_KEEP_USER) && ct_username_len > 0) {
memcpy( p, ct_username, ct_username_len);
p += ct_username_len;
}
if (dlg_api.is_mod_flag_set(dlg,TOPOH_DID_IN_USER)) {
if (p==prefix+5)
if (p == prefix + prefix_len)
*(p++) = 'X';
/* add '.' */
*(p++) = DLG_SEPARATOR;
Expand All @@ -505,7 +538,7 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
return -1;
}
}
if (p!=prefix+5)
if (p != prefix + prefix_len)
*(p++) = '@';

prefix_len = p - prefix;
Expand Down

0 comments on commit 1153353

Please sign in to comment.