@@ -405,6 +405,7 @@ impl Registration {
405405 ) ;
406406
407407 // Thanks to https://github.com/restsend/rsipstack/issues/32
408+ let contact_for_retry = contact. clone ( ) ;
408409 request. headers . unique_push ( self . call_id . clone ( ) . into ( ) ) ;
409410 request. headers . unique_push ( contact. into ( ) ) ;
410411 request. headers . unique_push ( self . allow . clone ( ) . into ( ) ) ;
@@ -446,10 +447,26 @@ impl Registration {
446447 if let Some ( cred) = & self . credential {
447448 self . last_seq += 1 ;
448449
449- // Handle authentication with the existing transaction
450- // The contact will be updated in the next registration cycle if needed
451450 tx = handle_client_authenticate ( self . last_seq , & tx, resp, cred) . await ?;
452451
452+ // Update Contact in the retry request to use the discovered
453+ // public address (rport/received from 401 Via header).
454+ // handle_client_authenticate clones tx.original which has
455+ // the stale Contact from the initial REGISTER.
456+ if let Some ( ref pa) = self . public_address {
457+ let new_contact_uri = rsip:: Uri {
458+ auth : contact_for_retry. uri . auth . clone ( ) ,
459+ scheme : Some ( rsip:: Scheme :: Sip ) ,
460+ host_with_port : pa. clone ( ) ,
461+ params : vec ! [ ] ,
462+ headers : vec ! [ ] ,
463+ } ;
464+ let mut new_contact = contact_for_retry. clone ( ) ;
465+ new_contact. uri = new_contact_uri;
466+ tx. original . headers . retain ( |h| !matches ! ( h, rsip:: Header :: Contact ( _) ) ) ;
467+ tx. original . headers . unique_push ( new_contact. into ( ) ) ;
468+ }
469+
453470 tx. send ( ) . await ?;
454471 auth_sent = true ;
455472 continue ;
0 commit comments