@@ -884,19 +884,82 @@ static char* build_encoded_contact_suffix_legacy(struct sip_msg* msg, str rr_set
884884 return NULL ;
885885}
886886
887+ static int th_binary_encode_record_route (rr_t * record_route , rr_t * * out_rr , int encode_self ) {
888+ struct sip_uri rr_uri = { 0 }, rr_uri_r2 = { 0 };
889+ const struct socket_info * rr_sock = NULL ;
890+
891+ if (record_route == NULL ) {
892+ LM_DBG ("Record-Route to encode is NULL, skipping\n" );
893+ return -1 ;
894+ }
895+
896+ if (parse_uri (record_route -> nameaddr .uri .s , record_route -> nameaddr .uri .len , & rr_uri ) < 0 ) {
897+ LM_ERR ("Failed to parse SIP uri\n" );
898+ return -1 ;
899+ }
900+
901+ rr_sock = grep_sock_info (& rr_uri .host , rr_uri .port_no ? rr_uri .port_no : SIP_PORT , rr_uri .proto );
902+
903+ if (th_no_dlg_one_way_hiding (rr_sock ) && !encode_self ) {
904+ LM_DBG ("Route header is self, skipping encode\n" );
905+ return 1 ;
906+ }
907+
908+ if (!is_2rr (& rr_uri .params )) {
909+ if (thinfo_encode_uri (& encoded_uri_buf , & rr_uri , 0 , NULL , 1 ) == -1 ) {
910+ LM_ERR ("Error encoding Route URI\n" );
911+ return -1 ;
912+ }
913+ } else {
914+ record_route = record_route -> next ;
915+ if (record_route != NULL ) {
916+ if (parse_uri (record_route -> nameaddr .uri .s , record_route -> nameaddr .uri .len , & rr_uri_r2 ) < 0 ) {
917+ LM_ERR ("Failed to parse SIP uri\n" );
918+ return -1 ;
919+ }
920+
921+ if (is_2rr (& rr_uri_r2 .params ) && str_match (& rr_uri .host , & rr_uri_r2 .host )) {
922+ if (thinfo_encode_dual_uri (& encoded_uri_buf , & rr_uri , & rr_uri_r2 ) == -1 ) {
923+ LM_ERR ("Error encoding Route URI\n" );
924+ return -1 ;
925+ }
926+ } else {
927+ if (thinfo_encode_uri (& encoded_uri_buf , & rr_uri , 0 , NULL , 1 ) == -1 ) {
928+ LM_ERR ("Error encoding Route URI\n" );
929+ return -1 ;
930+ }
931+
932+ if (thinfo_encode_uri (& encoded_uri_buf , & rr_uri_r2 , 0 , NULL , 1 ) == -1 ) {
933+ LM_ERR ("Error encoding Route URI\n" );
934+ return -1 ;
935+ }
936+ }
937+ } else {
938+ if (thinfo_encode_uri (& encoded_uri_buf , & rr_uri , 0 , NULL , 1 ) == -1 ) {
939+ LM_ERR ("Error encoding Route URI\n" );
940+ return -1 ;
941+ }
942+ LM_WARN ("Previous Route has r2=on but no next Route\n" );
943+ }
944+ }
945+
946+ * out_rr = record_route != NULL ? record_route -> next : NULL ;
947+ return 0 ;
948+ }
949+
887950static char * build_encoded_thinfo_suffix (struct sip_msg * msg , str rr_set , int * suffix_len , uint16_t flags , int socket_only ) {
888951 uint16_t enc_len = 0 ;
889952 char * suffix_enc , * s ;
890953 rr_t * next = NULL , * head = NULL ;
891954 int i , x , params_len = 0 ;
892- struct sip_uri ctu = { 0 }, rr_uri = { 0 }, rr_uri_r2 = { 0 } ;
955+ struct sip_uri ctu = { 0 };
893956 struct th_ct_params * el ;
894957 param_t * it ;
895958 str contact = STR_NULL ;
896959 char * rr_set_free_str = NULL ;
897- const struct socket_info * rr_sock = NULL ;
898960 str ct_uri_params_skip [URI_MAX_U_PARAMS ];
899961 int param_count = 0 ;
962+ int encode_self_rr = 0 , encode_ret_code = 0 ;
900963
901964 /* parse all headers as we can have multiple
902965 RR headers in the same message */
@@ -957,60 +1020,15 @@ static char* build_encoded_thinfo_suffix(struct sip_msg* msg, str rr_set, int *s
9571020 next = head ;
9581021 }
9591022
960- while (next != NULL ) {
961- if (parse_uri (next -> nameaddr .uri .s , next -> nameaddr .uri .len , & rr_uri ) < 0 ) {
962- LM_ERR ("Failed to parse SIP uri\n" );
963- goto error ;
964- }
965-
966- rr_sock = grep_sock_info (& rr_uri .host , rr_uri .port_no ? rr_uri .port_no : SIP_PORT , rr_uri .proto );
967-
968- if (!th_no_dlg_one_way_hiding (rr_sock )) {
969- if (!is_2rr (& rr_uri .params )) {
970- if (thinfo_encode_uri (& encoded_uri_buf , & rr_uri , 0 , NULL , 1 ) == -1 ) {
971- LM_ERR ("Error encoding Route URI\n" );
972- goto error ;
973- }
974- } else {
975- next = next -> next ;
976- if (next != NULL ) {
977- if (parse_uri (next -> nameaddr .uri .s , next -> nameaddr .uri .len , & rr_uri_r2 ) < 0 ) {
978- LM_ERR ("Failed to parse SIP uri\n" );
979- goto error ;
980- }
981- } else {
982- if (thinfo_encode_uri (& encoded_uri_buf , & rr_uri , 0 , NULL , 1 ) == -1 ) {
983- LM_ERR ("Error encoding Route URI\n" );
984- goto error ;
985- }
986- LM_WARN ("Previous Route has r2=on but no next Route\n" );
987- continue ;
988- }
989-
990- if (is_2rr (& rr_uri_r2 .params ) && str_match (& rr_uri .host , & rr_uri_r2 .host )) {
991- if (thinfo_encode_dual_uri (& encoded_uri_buf , & rr_uri , & rr_uri_r2 ) == -1 ) {
992- LM_ERR ("Error encoding Route URI\n" );
993- goto error ;
994- }
995- } else {
996- if (thinfo_encode_uri (& encoded_uri_buf , & rr_uri , 0 , NULL , 1 ) == -1 ) {
997- LM_ERR ("Error encoding Route URI\n" );
998- goto error ;
999- }
1000-
1001- if (thinfo_encode_uri (& encoded_uri_buf , & rr_uri_r2 , 0 , NULL , 1 ) == -1 ) {
1002- LM_ERR ("Error encoding Route URI\n" );
1003- goto error ;
1004- }
1005- }
1006-
1007- memset (& rr_uri_r2 , 0 , sizeof (rr_uri_r2 ));
1008- }
1009- }
1010-
1011- memset (& rr_uri , 0 , sizeof (rr_uri ));
1012- next = next -> next ;
1013- }
1023+ while (next != NULL ) {
1024+ encode_ret_code = th_binary_encode_record_route (next , & next , encode_self_rr );
1025+ if (encode_ret_code < 0 ) {
1026+ goto error ;
1027+ } else if (encode_ret_code == 1 ) {
1028+ // Found first occurence of self Record-Route, need to encode the rest
1029+ encode_self_rr = 1 ;
1030+ }
1031+ }
10141032
10151033 if (head != NULL ) {
10161034 pkg_free (head );
0 commit comments