Skip to content

Commit

Permalink
Push DialogID into username when doing TH
Browse files Browse the repository at this point in the history
The new "D" flag of the topology_hiding() function forces the insertion of the dialog ID (DID) into the Contact Username, rather than Contact URI parameters (as before).

This work was sponsored by Stella Telecom (https://www.stella-telecom.fr/), thank you for supporting OpenSIPS project !

Closes #1014.
  • Loading branch information
bogdan-iancu committed Feb 4, 2017
1 parent fb0d97d commit 1fcb7e5
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 17 deletions.
24 changes: 24 additions & 0 deletions modules/dialog/dialog.c
Expand Up @@ -1104,6 +1104,8 @@ static int w_match_dialog(struct sip_msg *msg)
int backup,i;
void *match_param = NULL;
struct sip_uri *r_uri;
str s;
char *p;


/* dialog already found ? */
Expand Down Expand Up @@ -1139,7 +1141,29 @@ static int w_match_dialog(struct sip_msg *msg)
r_uri->u_val[i].len,r_uri->u_val[i].s);
/* pass the param value to the matching funcs */
match_param = (void *)(&r_uri->u_val[i]);
break;
}
if (match_param==NULL) {
/* looking for ".did.hash.label" in the USERNAME */
s = r_uri->user;
while( (p=q_memchr(s.s,DLG_SEPARATOR,s.len))!=NULL ) {
if ( s.s+s.len-p-1 > rr_param.len+2 ) {
if (strncmp( p+1, rr_param.s, rr_param.len)==0 &&
p[rr_param.len+1]==DLG_SEPARATOR ) {
p += rr_param.len+2;
s.len = s.s+s.len-p;
s.s = p;
match_param = (void*)(&s);
break;
}
}
if (p+1<s.s+s.len) {
s.len = s.s+s.len-p+1;
s.s = p+1;
} else
break;
}
}
}

sipwise:
Expand Down
1 change: 1 addition & 0 deletions modules/dialog/dlg_hash.h
Expand Up @@ -117,6 +117,7 @@ struct dlg_leg {
#define TOPOH_ONGOING (1 << 1)
#define TOPOH_KEEP_USER (1 << 2)
#define TOPOH_HIDE_CALLID (1 << 3)
#define TOPOH_DID_IN_USER (1 << 4)

struct dlg_cell
{
Expand Down
3 changes: 3 additions & 0 deletions modules/topology_hiding/README
Expand Up @@ -216,6 +216,9 @@ modparam("topology_hiding", "th_contact_encode_param", "customparam")
Optionally,the function also receives a string parameter, which
holds string flags. Current options for the string flags are :
* U - Propagate the Username in the Contact header URI
* D - Dialog ID (DID) is pushed into Contact username, rather
than URI param. This option makes sense only when using
topology hiding with dialog support.
* C - Encode the callid header
There are many cases where propagating the callid towards
the callee side is not a good idea, since sometimes the
Expand Down
4 changes: 4 additions & 0 deletions modules/topology_hiding/doc/topology_hiding_admin.xml
Expand Up @@ -228,6 +228,10 @@ modparam("topology_hiding", "th_contact_encode_param", "customparam")
<para><emphasis>U</emphasis> - Propagate the Username in the Contact header URI
</para>
</listitem>
<listitem>
<para><emphasis>D</emphasis> - Dialog ID (DID) is pushed into Contact username, rather than URI param. This option makes sense only when using topology hiding with dialog support.
</para>
</listitem>
<listitem>
<para><emphasis>C</emphasis> - Encode the callid header
</para>
Expand Down
77 changes: 60 additions & 17 deletions modules/topology_hiding/topo_hiding_logic.c
Expand Up @@ -376,15 +376,24 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
contact = ((contact_body_t *)msg->contact->parsed)->contacts->uri;
if(parse_uri(contact.s, contact.len, &ctu) < 0) {
LM_ERR("Bad Contact URI\n");
if (dlg_api.is_mod_flag_set(dlg,TOPOH_DID_IN_USER))
prefix_len += RR_DLG_PARAM_SIZE + 1;
} else {
ct_username = ctu.user.s;
ct_username_len = ctu.user.len;
LM_DBG("Trying to propagate username [%.*s]\n",ct_username_len,
ct_username);
if (ct_username_len > 0)
if (ct_username_len > 0) {
prefix_len += 1 + /* @ */ + ct_username_len;
if (dlg_api.is_mod_flag_set(dlg,TOPOH_DID_IN_USER))
prefix_len += RR_DLG_PARAM_SIZE;
} else if (dlg_api.is_mod_flag_set(dlg,TOPOH_DID_IN_USER)) {
prefix_len += RR_DLG_PARAM_SIZE + 1;
}
}
}
} else if (dlg_api.is_mod_flag_set(dlg,TOPOH_DID_IN_USER)) {
prefix_len += RR_DLG_PARAM_SIZE + 1;
}

prefix = pkg_malloc(prefix_len);
Expand All @@ -393,7 +402,10 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
goto error;
}

suffix_len = RR_DLG_PARAM_SIZE+1; /* > */
if (dlg_api.is_mod_flag_set(dlg,TOPOH_DID_IN_USER))
suffix_len = 1; /* > */
else
suffix_len = RR_DLG_PARAM_SIZE+1; /* > */
if (th_param_list) {
if ( parse_contact(msg->contact)<0 ||
((contact_body_t *)msg->contact->parsed)->contacts==NULL ||
Expand Down Expand Up @@ -450,28 +462,59 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
goto error;
}

memcpy(prefix,"<sip:", 5);
rr_param = dlg_api.get_rr_param();

p = prefix;
memcpy( p, "<sip:", 5);
p += 5;
if (dlg_api.is_mod_flag_set(dlg,TOPOH_KEEP_USER) && ct_username_len > 0) {
memcpy(prefix+5,ct_username,ct_username_len);
prefix[prefix_len-1] = '@';
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)
*(p++) = 'X';
/* add '.' */
*(p++) = DLG_SEPARATOR;
/* add "did" */
memcpy(p,rr_param->s,rr_param->len);
p+=rr_param->len;
/* add '.' */
*(p++) = DLG_SEPARATOR;
/* add hash entry as hexa */
n = RR_DLG_PARAM_SIZE - (p-prefix);
if (int2reverse_hex( &p, &n, dlg->h_entry)==-1)
return -1;
/* add '.' */
*(p++) = DLG_SEPARATOR;
/* add hash entry as hexa */
n = RR_DLG_PARAM_SIZE - (p-prefix);
if (int2reverse_hex( &p, &n, dlg->h_id)==-1)
return -1;
}
if (p!=prefix+5)
*(p++) = '@';

prefix_len = p - prefix;

rr_param = dlg_api.get_rr_param();
p_init = p = suffix;
*p++ = ';';
memcpy(p,rr_param->s,rr_param->len);
p+=rr_param->len;
*p++ = '=';

n = RR_DLG_PARAM_SIZE - (p-p_init);
if (int2reverse_hex( &p, &n, dlg->h_entry)==-1)
return -1;
if (!dlg_api.is_mod_flag_set(dlg,TOPOH_DID_IN_USER)) {
*p++ = ';';
memcpy(p,rr_param->s,rr_param->len);
p+=rr_param->len;
*p++ = '=';

n = RR_DLG_PARAM_SIZE - (p-p_init);
if (int2reverse_hex( &p, &n, dlg->h_entry)==-1)
return -1;

*(p++) = DLG_SEPARATOR;
*(p++) = DLG_SEPARATOR;

n = RR_DLG_PARAM_SIZE - (p-p_init);
if (int2reverse_hex( &p, &n, dlg->h_id)==-1)
return -1;
n = RR_DLG_PARAM_SIZE - (p-p_init);
if (int2reverse_hex( &p, &n, dlg->h_id)==-1)
return -1;
}

if (th_param_list) {
for (el=th_param_list;el;el=el->next) {
Expand Down
4 changes: 4 additions & 0 deletions modules/topology_hiding/topology_hiding.c
Expand Up @@ -212,6 +212,10 @@ int w_topology_hiding1(struct sip_msg *req,char *param)
flags |= TOPOH_HIDE_CALLID;
LM_DBG("Will change callid while doing topo hiding\n");
break;
case 'D':
flags |= TOPOH_DID_IN_USER;
LM_DBG("Will push DID into contact username\n");
break;
default:
LM_DBG("unknown topology_hiding flag : [%c] . Skipping\n",*p);
}
Expand Down

0 comments on commit 1fcb7e5

Please sign in to comment.