Skip to content

Commit 1d6cdd6

Browse files
committed
Use from and to tags to match dialog
1 parent 2c8399a commit 1d6cdd6

File tree

1 file changed

+55
-42
lines changed

1 file changed

+55
-42
lines changed

pkg/ua/ua.go

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ import (
2121

2222
// SessionKey - Session Key for Session Storage
2323
type SessionKey struct {
24-
CallID sip.CallID
25-
BranchID sip.MaybeString
24+
CallID sip.CallID
25+
TagID sip.MaybeString
2626
}
2727

2828
// NewSessionKey - Build a Session Key quickly
29-
func NewSessionKey(callID sip.CallID, branchID sip.MaybeString) SessionKey {
29+
func NewSessionKey(callID sip.CallID, tagID sip.MaybeString) SessionKey {
3030
return SessionKey{
31-
CallID: callID,
32-
BranchID: branchID,
31+
CallID: callID,
32+
TagID: tagID,
3333
}
3434
}
3535

@@ -188,9 +188,10 @@ func (ua *UserAgent) InviteWithContext(ctx context.Context, profile *account.Pro
188188
}
189189

190190
callID, ok := (*request).CallID()
191-
if ok {
192-
branchID := utils.GetBranchID(*request)
193-
if v, found := ua.iss.Load(NewSessionKey(*callID, branchID)); found {
191+
fromHeader, ok2 := (*request).From()
192+
if ok && ok2 {
193+
fromTag, _ := fromHeader.Params.Get("tag")
194+
if v, found := ua.iss.Load(NewSessionKey(*callID, fromTag)); found {
194195
return v.(*session.Session), nil
195196
}
196197
}
@@ -233,11 +234,12 @@ func (ua *UserAgent) handleBye(request sip.Request, tx sip.ServerTransaction) {
233234

234235
tx.Respond(response)
235236
callID, ok := request.CallID()
236-
if ok {
237-
branchID := utils.GetBranchID(request)
238-
if v, found := ua.iss.Load(NewSessionKey(*callID, branchID)); found {
237+
toHeader, ok2 := request.To()
238+
if ok && ok2 {
239+
toTag, _ := toHeader.Params.Get("tag")
240+
if v, found := ua.iss.Load(NewSessionKey(*callID, toTag)); found {
239241
is := v.(*session.Session)
240-
ua.iss.Delete(NewSessionKey(*callID, branchID))
242+
ua.iss.Delete(NewSessionKey(*callID, toTag))
241243
var transaction sip.Transaction = tx.(sip.Transaction)
242244
ua.handleInviteState(is, &request, &response, session.Terminated, &transaction)
243245
}
@@ -251,11 +253,12 @@ func (ua *UserAgent) handleCancel(request sip.Request, tx sip.ServerTransaction)
251253
tx.Respond(response)
252254

253255
callID, ok := request.CallID()
254-
if ok {
255-
branchID := utils.GetBranchID(request)
256-
if v, found := ua.iss.Load(NewSessionKey(*callID, branchID)); found {
256+
fromHeader, ok2 := request.From()
257+
if ok && ok2 {
258+
fromTag, _ := fromHeader.Params.Get("tag")
259+
if v, found := ua.iss.Load(NewSessionKey(*callID, fromTag)); found {
257260
is := v.(*session.Session)
258-
ua.iss.Delete(NewSessionKey(*callID, branchID))
261+
ua.iss.Delete(NewSessionKey(*callID, fromTag))
259262
var transaction sip.Transaction = tx.(sip.Transaction)
260263
is.SetState(session.Canceled)
261264
ua.handleInviteState(is, &request, nil, session.Canceled, &transaction)
@@ -266,9 +269,10 @@ func (ua *UserAgent) handleCancel(request sip.Request, tx sip.ServerTransaction)
266269
func (ua *UserAgent) handleACK(request sip.Request, tx sip.ServerTransaction) {
267270
ua.Log().Debugf("handleACK => %s, body => %s", request.Short(), request.Body())
268271
callID, ok := request.CallID()
269-
if ok {
270-
branchID := utils.GetBranchID(request)
271-
if v, found := ua.iss.Load(NewSessionKey(*callID, branchID)); found {
272+
fromHeader, ok2 := request.From()
273+
if ok && ok2 {
274+
fromTag, _ := fromHeader.Params.Get("tag")
275+
if v, found := ua.iss.Load(NewSessionKey(*callID, fromTag)); found {
272276
// handle Ringing or Processing with sdp
273277
is := v.(*session.Session)
274278
is.SetState(session.Confirmed)
@@ -282,10 +286,11 @@ func (ua *UserAgent) handleInvite(request sip.Request, tx sip.ServerTransaction)
282286
ua.Log().Debugf("handleInvite => %s, body => %s", request.Short(), request.Body())
283287

284288
callID, ok := request.CallID()
285-
if ok {
289+
fromHeader, ok2 := request.From()
290+
if ok && ok2 {
291+
fromTag, _ := fromHeader.Params.Get("tag")
286292
var transaction sip.Transaction = tx.(sip.Transaction)
287-
branchID := utils.GetBranchID(request)
288-
v, found := ua.iss.Load(NewSessionKey(*callID, branchID))
293+
v, found := ua.iss.Load(NewSessionKey(*callID, fromTag))
289294
if toHdr, ok := request.To(); ok && toHdr.Params.Has("tag") {
290295
if found {
291296
is := v.(*session.Session)
@@ -307,7 +312,7 @@ func (ua *UserAgent) handleInvite(request sip.Request, tx sip.ServerTransaction)
307312
contactHdr.Address = contactAddr
308313

309314
is := session.NewInviteSession(ua.RequestWithContext, "UAS", contactHdr, request, *callID, transaction, session.Incoming, ua.Log())
310-
ua.iss.Store(NewSessionKey(*callID, branchID), is)
315+
ua.iss.Store(NewSessionKey(*callID, fromTag), is)
311316
is.SetState(session.InviteReceived)
312317
ua.handleInviteState(is, &request, nil, session.InviteReceived, &transaction)
313318
is.SetState(session.WaitingForAnswer)
@@ -320,10 +325,12 @@ func (ua *UserAgent) handleInvite(request sip.Request, tx sip.ServerTransaction)
320325
if cancel != nil {
321326
ua.Log().Debugf("Cancel => %s, body => %s", cancel.Short(), cancel.Body())
322327
response := sip.NewResponseFromRequest(cancel.MessageID(), cancel, 200, "OK", "")
323-
if callID, ok := response.CallID(); ok {
324-
branchID := utils.GetBranchID(cancel)
325-
if v, found := ua.iss.Load(NewSessionKey(*callID, branchID)); found {
326-
ua.iss.Delete(NewSessionKey(*callID, branchID))
328+
callID, ok := response.CallID()
329+
fromHeader, ok2 := request.From()
330+
if ok && ok2 {
331+
fromTag, _ := fromHeader.Params.Get("tag")
332+
if v, found := ua.iss.Load(NewSessionKey(*callID, fromTag)); found {
333+
ua.iss.Delete(NewSessionKey(*callID, fromTag))
327334
is := v.(*session.Session)
328335
is.SetState(session.Canceled)
329336
ua.handleInviteState(is, &request, &response, session.Canceled, nil)
@@ -358,14 +365,17 @@ func (ua *UserAgent) RequestWithContext(ctx context.Context, request sip.Request
358365
var cts sip.Transaction = tx.(sip.Transaction)
359366

360367
if request.IsInvite() {
361-
if callID, ok := request.CallID(); ok {
362-
branchID := utils.GetBranchID(request)
363-
if _, found := ua.iss.Load(NewSessionKey(*callID, branchID)); !found {
368+
369+
callID, ok := request.CallID()
370+
fromHeader, ok2 := request.From()
371+
if ok && ok2 {
372+
fromTag, _ := fromHeader.Params.Get("tag")
373+
if _, found := ua.iss.Load(NewSessionKey(*callID, fromTag)); !found {
364374
contactHdr, _ := request.Contact()
365375
contactAddr := ua.updateContact2UAAddr(request.Transport(), contactHdr.Address)
366376
contactHdr.Address = contactAddr
367377
is := session.NewInviteSession(ua.RequestWithContext, "UAC", contactHdr, request, *callID, cts, session.Outgoing, ua.Log())
368-
ua.iss.Store(NewSessionKey(*callID, branchID), is)
378+
ua.iss.Store(NewSessionKey(*callID, fromTag), is)
369379
is.ProvideOffer(request.Body())
370380
is.SetState(session.InviteSent)
371381
ua.handleInviteState(is, &request, nil, session.InviteSent, &cts)
@@ -491,9 +501,10 @@ func (ua *UserAgent) RequestWithContext(ctx context.Context, request sip.Request
491501
select {
492502
case provisional := <-provisionals:
493503
callID, ok := provisional.CallID()
494-
if ok {
495-
branchID := utils.GetBranchID(provisional)
496-
if v, found := ua.iss.Load(NewSessionKey(*callID, branchID)); found {
504+
fromHeader, ok2 := provisional.From()
505+
if ok && ok2 {
506+
fromTag, _ := fromHeader.Params.Get("tag")
507+
if v, found := ua.iss.Load(NewSessionKey(*callID, fromTag)); found {
497508
is := v.(*session.Session)
498509
is.StoreResponse(provisional)
499510
// handle Ringing or Processing with sdp
@@ -514,28 +525,30 @@ func (ua *UserAgent) RequestWithContext(ctx context.Context, request sip.Request
514525
request := (err.(*sip.RequestError)).Request
515526
response := (err.(*sip.RequestError)).Response
516527
callID, ok := request.CallID()
517-
if ok {
518-
branchID := utils.GetBranchID(request)
519-
if v, found := ua.iss.Load(NewSessionKey(*callID, branchID)); found {
528+
fromHeader, ok2 := request.From()
529+
if ok && ok2 {
530+
fromTag, _ := fromHeader.Params.Get("tag")
531+
if v, found := ua.iss.Load(NewSessionKey(*callID, fromTag)); found {
520532
is := v.(*session.Session)
521-
ua.iss.Delete(NewSessionKey(*callID, branchID))
533+
ua.iss.Delete(NewSessionKey(*callID, fromTag))
522534
is.SetState(session.Failure)
523535
ua.handleInviteState(is, &request, &response, session.Failure, nil)
524536
}
525537
}
526538
return nil, err
527539
case response := <-responses:
528540
callID, ok := response.CallID()
529-
if ok {
530-
branchID := utils.GetBranchID(response)
531-
if v, found := ua.iss.Load(NewSessionKey(*callID, branchID)); found {
541+
fromHeader, ok2 := request.From()
542+
if ok && ok2 {
543+
fromTag, _ := fromHeader.Params.Get("tag")
544+
if v, found := ua.iss.Load(NewSessionKey(*callID, fromTag)); found {
532545
if request.IsInvite() {
533546
is := v.(*session.Session)
534547
is.SetState(session.Confirmed)
535548
ua.handleInviteState(is, &request, &response, session.Confirmed, nil)
536549
} else if request.Method() == sip.BYE {
537550
is := v.(*session.Session)
538-
ua.iss.Delete(NewSessionKey(*callID, branchID))
551+
ua.iss.Delete(NewSessionKey(*callID, fromTag))
539552
is.SetState(session.Terminated)
540553
ua.handleInviteState(is, &request, &response, session.Terminated, nil)
541554
}

0 commit comments

Comments
 (0)