Skip to content

Commit a1b014b

Browse files
committed
self-review
1 parent cbbf048 commit a1b014b

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

pkg/sip/inbound.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,22 @@ func (s *Server) getCallInfo(id string) *inboundCallInfo {
138138
return c
139139
}
140140

141-
func (s *Server) getInvite(sipCallID string) *inProgressInvite {
141+
func (s *Server) getInvite(sipCallID, toTag, fromTag string) *inProgressInvite {
142+
key := fmt.Sprintf("%s:%s:%s", sipCallID, toTag, fromTag)
142143
s.imu.Lock()
143144
defer s.imu.Unlock()
144-
is, ok := s.inProgressInvites[sipCallID]
145+
is, ok := s.inProgressInvites[key]
145146
if ok {
146147
return is
147148
}
148149
is = &inProgressInvite{sipCallID: sipCallID}
149-
s.inProgressInvites[sipCallID] = is
150+
s.inProgressInvites[key] = is
150151

151152
go func() {
152153
time.Sleep(inviteCredentialValidity)
153154
s.imu.Lock()
154155
defer s.imu.Unlock()
155-
delete(s.inProgressInvites, sipCallID)
156+
delete(s.inProgressInvites, key)
156157
}()
157158
return is
158159
}
@@ -322,7 +323,7 @@ func (s *Server) processInvite(req *sip.Request, tx sip.ServerTransaction) (retE
322323
)
323324

324325
var call *inboundCall
325-
cc := s.newInbound(log, "unassigned", s.ContactURI(legTr), req, tx, func(headers map[string]string) map[string]string {
326+
cc := s.newInbound(log, s.ContactURI(legTr), req, tx, func(headers map[string]string) map[string]string {
326327
c := call
327328
if c == nil || len(c.attrsToHdr) == 0 {
328329
return headers
@@ -346,21 +347,22 @@ func (s *Server) processInvite(req *sip.Request, tx sip.ServerTransaction) (retE
346347
}
347348

348349
// Establish ID
349-
if _, ok := req.To().Params.Get("tag"); !ok {
350+
fromTag, _ := req.From().Params.Get("tag") // always exists, via ValidateInvite() check
351+
toTag, ok := req.To().Params.Get("tag") // To() always exists, via ValidateInvite() check
352+
if !ok {
350353
// No to-tag on the invite means we need to generate one per RFC 3261 section 12.
351-
if !inviteHasAuth(req) {
352-
// No auth = a 407 response and another INVITE+auth.
353-
// Generate a new to-tag early, to make sure both INVITES have the same ID.
354-
uuid, _ := uuid.NewV4() // Same as NewResponseFromRequest in sipgo
355-
req.To().Params.Add("tag", uuid.String())
356-
}
354+
// Generate a new to-tag early, to make sure both INVITES have the same ID.
355+
uuid, _ := uuid.NewV4() // Same as NewResponseFromRequest in sipgo
356+
toTag = uuid.String()
357+
req.To().Params.Add("tag", toTag)
357358
}
358-
inviteProgress := s.getInvite(req.CallID().Value())
359+
inviteProgress := s.getInvite(sipCallID, toTag, fromTag)
359360
callID := inviteProgress.lkCallID
360361
if callID == "" {
361362
callID = lksip.NewCallID()
362363
inviteProgress.lkCallID = callID
363364
}
365+
cc.id = LocalTag(callID)
364366

365367
log = log.WithValues("callID", callID)
366368
cc.log = log
@@ -1390,11 +1392,11 @@ func (c *inboundCall) transferCall(ctx context.Context, transferTo string, heade
13901392

13911393
}
13921394

1393-
func (s *Server) newInbound(log logger.Logger, id LocalTag, contact URI, invite *sip.Request, inviteTx sip.ServerTransaction, getHeaders setHeadersFunc) *sipInbound {
1395+
func (s *Server) newInbound(log logger.Logger, contact URI, invite *sip.Request, inviteTx sip.ServerTransaction, getHeaders setHeadersFunc) *sipInbound {
13941396
c := &sipInbound{
13951397
log: log,
13961398
s: s,
1397-
id: id,
1399+
id: "unassigned",
13981400
invite: invite,
13991401
inviteTx: inviteTx,
14001402
legTr: legTransportFromReq(invite),

pkg/sip/service_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@ func TestSameCallIDForAuthFlow(t *testing.T) {
792792
username = "testuser"
793793
password = "testpass"
794794
callID = "same-call-id@test.com"
795+
fromTag = "fixed-from-tag-12345"
795796
)
796797

797798
var capturedCallIDs []string
@@ -860,13 +861,20 @@ func TestSameCallIDForAuthFlow(t *testing.T) {
860861
offerData, err := offer.SDP.Marshal()
861862
require.NoError(t, err)
862863

864+
inviteFromHeader := sip.FromHeader{
865+
DisplayName: fromUser,
866+
Address: sip.Uri{User: fromUser, Host: sipServerAddress},
867+
Params: sip.NewParams().Add("tag", fromTag), // Key bit here
868+
}
869+
863870
// Create first INVITE request (without auth)
864871
inviteRecipient := sip.Uri{User: toUser, Host: sipServerAddress}
865872
inviteRequest1 := sip.NewRequest(sip.INVITE, inviteRecipient)
866873
inviteRequest1.SetDestination(sipServerAddress)
867874
inviteRequest1.SetBody(offerData)
868875
inviteRequest1.AppendHeader(sip.NewHeader("Content-Type", "application/sdp"))
869876
inviteRequest1.AppendHeader(sip.NewHeader("Call-ID", callID))
877+
inviteRequest1.AppendHeader(&inviteFromHeader)
870878

871879
tx1, err := sipClient.TransactionRequest(inviteRequest1)
872880
require.NoError(t, err)
@@ -878,6 +886,12 @@ func TestSameCallIDForAuthFlow(t *testing.T) {
878886
res1 = getResponseOrFail(t, tx1)
879887
require.Equal(t, sip.StatusCode(407), res1.StatusCode, "First request should receive 407 Unauthorized")
880888

889+
// Get the To tag from the 407 response
890+
toHeader := res1.To()
891+
require.NotNil(t, toHeader, "407 response should have To header")
892+
_, ok := toHeader.Params.Get("tag")
893+
require.True(t, ok, "407 response To header should have tag parameter")
894+
881895
// Get the challenge from first response
882896
authHeader1 := res1.GetHeader("Proxy-Authenticate")
883897
require.NotNil(t, authHeader1, "First response should have Proxy-Authenticate header")
@@ -896,13 +910,15 @@ func TestSameCallIDForAuthFlow(t *testing.T) {
896910
})
897911
require.NoError(t, err, "Should be able to compute digest response")
898912

899-
// Create second INVITE request (with auth) using the SAME Call-ID
913+
// Create second INVITE request (with auth) using the SAME Call-ID, From tag, and To tag
900914
inviteRequest2 := sip.NewRequest(sip.INVITE, inviteRecipient)
901915
inviteRequest2.SetDestination(sipServerAddress)
902916
inviteRequest2.SetBody(offerData)
903917
inviteRequest2.AppendHeader(sip.NewHeader("Content-Type", "application/sdp"))
904918
inviteRequest2.AppendHeader(sip.NewHeader("Call-ID", callID))
905919
inviteRequest2.AppendHeader(sip.NewHeader("Proxy-Authorization", cred.String()))
920+
inviteRequest2.AppendHeader(&inviteFromHeader)
921+
inviteRequest2.AppendHeader(toHeader)
906922

907923
tx2, err := sipClient.TransactionRequest(inviteRequest2)
908924
require.NoError(t, err)

0 commit comments

Comments
 (0)