@@ -369,6 +369,26 @@ static char * topo_ct_param_copy(char *buf, str *name, str *val, int should_quot
369369 return buf ;
370370}
371371
372+ static int is_forced_sips_contact_required (const struct sip_msg * msg , const struct dlg_cell * dlg , const uri_type ct_type ) {
373+ str top_rr ;
374+
375+ if (dlg -> state != DLG_STATE_UNCONFIRMED ) return 0 ;
376+
377+ // RURI is SIPS
378+ if (msg -> parsed_uri_ok && msg -> parsed_uri .type == SIPS_URI_T ) return 1 ;
379+
380+ if (msg -> record_route == NULL ) {
381+ // RR is NULL and Contact is SIPS
382+ if (ct_type == SIPS_URI_T ) return 1 ;
383+ } else {
384+ // Top RR is SIPS
385+ top_rr = ((rr_t * )msg -> record_route -> parsed )-> nameaddr .uri ;
386+ if ((top_rr .len > 5 && strncmp (top_rr .s , "sips:" , 5 ) == 0 )) return 1 ;
387+ }
388+
389+ return 0 ;
390+ }
391+
372392static int topo_dlg_replace_contact (struct sip_msg * msg , struct dlg_cell * dlg )
373393{
374394 char * prefix = NULL ,* suffix = NULL ,* p ,* p_init ,* ct_username = NULL ;
@@ -379,6 +399,7 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
379399 param_t * it ;
380400 str * rr_param ;
381401 struct lump * lump ;
402+ uri_type ct_type = ERROR_URI_T ;
382403
383404 if (!msg -> contact )
384405 {
@@ -408,6 +429,8 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
408429 } else {
409430 ct_username = ctu .user .s ;
410431 ct_username_len = ctu .user .len ;
432+ ct_type = ctu .type ;
433+ if (ct_type == SIPS_URI_T ) prefix_len += 1 ;
411434 LM_DBG ("Trying to propagate username [%.*s]\n" ,ct_username_len ,
412435 ct_username );
413436 if (ct_username_len > 0 ) {
@@ -419,6 +442,8 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
419442 if (dlg_api .is_mod_flag_set (dlg ,TOPOH_DID_IN_USER ))
420443 prefix_len += RR_DLG_PARAM_SIZE + 1 ;
421444
445+ if (dlg -> force_sips_contact && ct_type == SIP_URI_T ) prefix_len += 1 ;
446+
422447 prefix = pkg_malloc (prefix_len );
423448 if (!prefix ) {
424449 LM_ERR ("no more pkg\n" );
@@ -474,14 +499,22 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
474499 rr_param = dlg_api .get_rr_param ();
475500
476501 p = prefix ;
477- memcpy ( p , "<sip:" , 5 );
478- p += 5 ;
502+ if (dlg -> force_sips_contact ) {
503+ memcpy (p , "<sips:" , 6 );
504+ p += 6 ;
505+ } else {
506+ memcpy (p , "<sip:" , 5 );
507+ p += 5 ;
508+ }
509+
510+ if (is_forced_sips_contact_required (msg , dlg , ct_type )) dlg -> force_sips_contact = 1 ;
511+
479512 if (dlg_api .is_mod_flag_set (dlg ,TOPOH_KEEP_USER ) && ct_username_len > 0 ) {
480513 memcpy ( p , ct_username , ct_username_len );
481514 p += ct_username_len ;
482515 }
483516 if (dlg_api .is_mod_flag_set (dlg ,TOPOH_DID_IN_USER )) {
484- if (p == prefix + 5 )
517+ if (p == prefix + prefix_len )
485518 * (p ++ ) = 'X' ;
486519 /* add '.' */
487520 * (p ++ ) = DLG_SEPARATOR ;
@@ -505,7 +538,7 @@ static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
505538 return -1 ;
506539 }
507540 }
508- if (p != prefix + 5 )
541+ if (p != prefix + prefix_len )
509542 * (p ++ ) = '@' ;
510543
511544 prefix_len = p - prefix ;
0 commit comments