@@ -3,7 +3,7 @@ package ua
33import (
44 "context"
55 "fmt"
6- "net "
6+ "strconv "
77 "sync"
88
99 "github.com/cloudwebrtc/go-sip-ua/pkg/account"
@@ -102,32 +102,6 @@ func (ua *UserAgent) buildRequest(
102102 return & req , nil
103103}
104104
105- func (ua * UserAgent ) buildViaHopHeader (target sip.SipUri ) * sip.ViaHop {
106- protocol := "udp"
107- if nt , ok := target .UriParams ().Get ("transport" ); ok {
108- protocol = nt .String ()
109- }
110- s := ua .config .SipStack
111- netinfo := s .GetNetworkInfo (protocol )
112-
113- var host string = netinfo .Host
114- if net .ParseIP (target .Host ()).IsLoopback () {
115- host = "127.0.0.1"
116- }
117-
118- port := netinfo .Port
119-
120- viaHop := & sip.ViaHop {
121- ProtocolName : "SIP" ,
122- ProtocolVersion : "2.0" ,
123- Transport : protocol ,
124- Host : host ,
125- Port : port ,
126- Params : sip .NewParams ().Add ("branch" , sip.String {Str : sip .GenerateBranch ()}),
127- }
128- return viaHop
129- }
130-
131105func (ua * UserAgent ) SendRegister (profile * account.Profile , recipient sip.SipUri , expires uint32 , userdata interface {}) (* Register , error ) {
132106 register := NewRegister (ua , profile , recipient , userdata )
133107 err := register .SendRegister (expires )
@@ -169,7 +143,7 @@ func (ua *UserAgent) Invite(profile *account.Profile, target sip.Uri, recipient
169143 authorizer = auth .NewClientAuthorizer (profile .AuthInfo .AuthUser , profile .AuthInfo .Password )
170144 }
171145
172- resp , err := ua .RequestWithContext (context .TODO (), * request , authorizer , false )
146+ resp , err := ua .RequestWithContext (context .TODO (), * request , authorizer , false , 1 )
173147 if err != nil {
174148 ua .Log ().Errorf ("INVITE: Request [INVITE] failed, err => %v" , err )
175149 return nil , err
@@ -188,7 +162,7 @@ func (ua *UserAgent) Invite(profile *account.Profile, target sip.Uri, recipient
188162 }
189163 }
190164
191- return nil , fmt .Errorf ("Invite session not found, unknown errors. " )
165+ return nil , fmt .Errorf ("invite session not found, unknown errors" )
192166}
193167
194168func (ua * UserAgent ) Request (req * sip.Request ) (sip.ClientTransaction , error ) {
@@ -198,6 +172,32 @@ func (ua *UserAgent) Request(req *sip.Request) (sip.ClientTransaction, error) {
198172func (ua * UserAgent ) handleBye (request sip.Request , tx sip.ServerTransaction ) {
199173 ua .Log ().Debugf ("handleBye: Request => %s, body => %s" , request .Short (), request .Body ())
200174 response := sip .NewResponseFromRequest (request .MessageID (), request , 200 , "OK" , "" )
175+
176+ if viaHop , ok := request .ViaHop (); ok {
177+ var (
178+ host string
179+ port sip.Port
180+ )
181+ host = viaHop .Host
182+ if viaHop .Params != nil {
183+ if received , ok := viaHop .Params .Get ("received" ); ok && received .String () != "" {
184+ host = received .String ()
185+ }
186+ if rport , ok := viaHop .Params .Get ("rport" ); ok && rport != nil && rport .String () != "" {
187+ if p , err := strconv .Atoi (rport .String ()); err == nil {
188+ port = sip .Port (uint16 (p ))
189+ }
190+ } else if request .Recipient ().Port () != nil {
191+ port = * request .Recipient ().Port ()
192+ } else {
193+ port = sip .DefaultPort (request .Transport ())
194+ }
195+ }
196+
197+ dest := fmt .Sprintf ("%v:%v" , host , port )
198+ response .SetDestination (dest )
199+ }
200+
201201 tx .Respond (response )
202202 callID , ok := request .CallID ()
203203 if ok {
@@ -290,7 +290,7 @@ func (ua *UserAgent) handleInvite(request sip.Request, tx sip.ServerTransaction)
290290}
291291
292292// RequestWithContext .
293- func (ua * UserAgent ) RequestWithContext (ctx context.Context , request sip.Request , authorizer sip.Authorizer , waitForResult bool ) (sip.Response , error ) {
293+ func (ua * UserAgent ) RequestWithContext (ctx context.Context , request sip.Request , authorizer sip.Authorizer , waitForResult bool , attempt int ) (sip.Response , error ) {
294294 s := ua .config .SipStack
295295 tx , err := s .Request (request )
296296 if err != nil {
@@ -401,12 +401,13 @@ func (ua *UserAgent) RequestWithContext(ctx context.Context, request sip.Request
401401 }
402402
403403 // unauth request
404- if (response .StatusCode () == 401 || response .StatusCode () == 407 ) && authorizer != nil {
404+ needAuth := (response .StatusCode () == 401 || response .StatusCode () == 407 ) && attempt < 2
405+ if needAuth && authorizer != nil {
405406 if err := authorizer .AuthorizeRequest (request , response ); err != nil {
406407 errs <- err
407408 return
408409 }
409- if response , err := ua .RequestWithContext (ctx , request , nil , true ); err == nil {
410+ if response , err := ua .RequestWithContext (ctx , request , authorizer , true , attempt + 1 ); err == nil {
410411 responses <- response
411412 } else {
412413 errs <- err
@@ -455,7 +456,6 @@ func (ua *UserAgent) RequestWithContext(ctx context.Context, request sip.Request
455456 if v , found := ua .iss .Load (* callID ); found {
456457 is := v .(* session.Session )
457458 ua .iss .Delete (* callID )
458- // handle Ringing or Processing with sdp
459459 is .SetState (session .Failure )
460460 ua .handleInviteState (is , & request , & response , session .Failure , nil )
461461 }
@@ -466,7 +466,6 @@ func (ua *UserAgent) RequestWithContext(ctx context.Context, request sip.Request
466466 if ok {
467467 if v , found := ua .iss .Load (* callID ); found {
468468 if request .IsInvite () {
469- // handle Ringing or Processing with sdp
470469 is := v .(* session.Session )
471470 is .SetState (session .Confirmed )
472471 ua .handleInviteState (is , & request , & response , session .Confirmed , nil )
0 commit comments