@@ -3,7 +3,7 @@ package ua
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "net "
6
+ "strconv "
7
7
"sync"
8
8
9
9
"github.com/cloudwebrtc/go-sip-ua/pkg/account"
@@ -102,32 +102,6 @@ func (ua *UserAgent) buildRequest(
102
102
return & req , nil
103
103
}
104
104
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
-
131
105
func (ua * UserAgent ) SendRegister (profile * account.Profile , recipient sip.SipUri , expires uint32 , userdata interface {}) (* Register , error ) {
132
106
register := NewRegister (ua , profile , recipient , userdata )
133
107
err := register .SendRegister (expires )
@@ -169,7 +143,7 @@ func (ua *UserAgent) Invite(profile *account.Profile, target sip.Uri, recipient
169
143
authorizer = auth .NewClientAuthorizer (profile .AuthInfo .AuthUser , profile .AuthInfo .Password )
170
144
}
171
145
172
- resp , err := ua .RequestWithContext (context .TODO (), * request , authorizer , false )
146
+ resp , err := ua .RequestWithContext (context .TODO (), * request , authorizer , false , 1 )
173
147
if err != nil {
174
148
ua .Log ().Errorf ("INVITE: Request [INVITE] failed, err => %v" , err )
175
149
return nil , err
@@ -188,7 +162,7 @@ func (ua *UserAgent) Invite(profile *account.Profile, target sip.Uri, recipient
188
162
}
189
163
}
190
164
191
- return nil , fmt .Errorf ("Invite session not found, unknown errors. " )
165
+ return nil , fmt .Errorf ("invite session not found, unknown errors" )
192
166
}
193
167
194
168
func (ua * UserAgent ) Request (req * sip.Request ) (sip.ClientTransaction , error ) {
@@ -198,6 +172,32 @@ func (ua *UserAgent) Request(req *sip.Request) (sip.ClientTransaction, error) {
198
172
func (ua * UserAgent ) handleBye (request sip.Request , tx sip.ServerTransaction ) {
199
173
ua .Log ().Debugf ("handleBye: Request => %s, body => %s" , request .Short (), request .Body ())
200
174
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
+
201
201
tx .Respond (response )
202
202
callID , ok := request .CallID ()
203
203
if ok {
@@ -290,7 +290,7 @@ func (ua *UserAgent) handleInvite(request sip.Request, tx sip.ServerTransaction)
290
290
}
291
291
292
292
// 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 ) {
294
294
s := ua .config .SipStack
295
295
tx , err := s .Request (request )
296
296
if err != nil {
@@ -401,12 +401,13 @@ func (ua *UserAgent) RequestWithContext(ctx context.Context, request sip.Request
401
401
}
402
402
403
403
// 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 {
405
406
if err := authorizer .AuthorizeRequest (request , response ); err != nil {
406
407
errs <- err
407
408
return
408
409
}
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 {
410
411
responses <- response
411
412
} else {
412
413
errs <- err
@@ -455,7 +456,6 @@ func (ua *UserAgent) RequestWithContext(ctx context.Context, request sip.Request
455
456
if v , found := ua .iss .Load (* callID ); found {
456
457
is := v .(* session.Session )
457
458
ua .iss .Delete (* callID )
458
- // handle Ringing or Processing with sdp
459
459
is .SetState (session .Failure )
460
460
ua .handleInviteState (is , & request , & response , session .Failure , nil )
461
461
}
@@ -466,7 +466,6 @@ func (ua *UserAgent) RequestWithContext(ctx context.Context, request sip.Request
466
466
if ok {
467
467
if v , found := ua .iss .Load (* callID ); found {
468
468
if request .IsInvite () {
469
- // handle Ringing or Processing with sdp
470
469
is := v .(* session.Session )
471
470
is .SetState (session .Confirmed )
472
471
ua .handleInviteState (is , & request , & response , session .Confirmed , nil )
0 commit comments