Skip to content

Commit

Permalink
fix: dialog server should have same to tag on responses
Browse files Browse the repository at this point in the history
  • Loading branch information
emiago committed Apr 28, 2024
1 parent 2ba54fb commit 0e1fece
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 0 additions & 1 deletion dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type Dialog struct {
state atomic.Int32
stateCh chan sip.DialogState

//
ctx context.Context
cancel context.CancelFunc
}
Expand Down
4 changes: 2 additions & 2 deletions dialog_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *DialogClient) loadDialog(id string) *DialogClientSession {
return t
}

func (s *DialogClient) MatchDialogRequest(req *sip.Request) (*DialogClientSession, error) {
func (s *DialogClient) matchDialogRequest(req *sip.Request) (*DialogClientSession, error) {
id, err := sip.UACReadRequestDialogID(req)
if err != nil {
return nil, errors.Join(err, ErrDialogOutsideDialog)
Expand Down Expand Up @@ -113,7 +113,7 @@ func (c *DialogClient) WriteInvite(ctx context.Context, inviteRequest *sip.Reque
}

func (c *DialogClient) ReadBye(req *sip.Request, tx sip.ServerTransaction) error {
dt, err := c.MatchDialogRequest(req)
dt, err := c.matchDialogRequest(req)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion dialog_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func TestIntegrationDialog(t *testing.T) {
t.Log("UAC: INVITE")
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address, nil)
require.NoError(t, err)
defer sess.Close()

err = sess.WaitAnswer(ctx, AnswerOptions{})
require.NoError(t, err)
Expand All @@ -127,6 +128,7 @@ func TestIntegrationDialog(t *testing.T) {
t.Log("UAC: INVITE")
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address, nil)
require.NoError(t, err)
defer sess.Close()

err = sess.WaitAnswer(ctx, AnswerOptions{})
require.NoError(t, err)
Expand Down Expand Up @@ -228,6 +230,7 @@ func TestIntegrationDialogBrokenUAC(t *testing.T) {
t.Log("UAC: INVITE")
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address, nil)
require.NoError(t, err)
defer sess.Close()

err = sess.WaitAnswer(ctx, AnswerOptions{})
require.NoError(t, err)
Expand All @@ -249,6 +252,7 @@ func TestIntegrationDialogBrokenUAC(t *testing.T) {
t.Log("UAC: INVITE")
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address, nil)
require.NoError(t, err)
defer sess.Close()

err = sess.WaitAnswer(ctx, AnswerOptions{})
require.NoError(t, err)
Expand Down Expand Up @@ -278,5 +282,5 @@ func startTestServer(ctx context.Context, srv *Server, hostPort string) {
)
// Wait server to be ready
<-srvReady
time.Sleep(200 * time.Millisecond) // just to avoid race with listeners on UDP
time.Sleep(500 * time.Millisecond) // just to avoid race with listeners on UDP
}
24 changes: 20 additions & 4 deletions dialog_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/emiago/sipgo/sip"
uuid "github.com/satori/go.uuid"
)

type DialogServer struct {
Expand All @@ -27,7 +28,7 @@ func (s *DialogServer) loadDialog(id string) *DialogServerSession {
return t
}

func (s *DialogServer) MatchDialogRequest(req *sip.Request) (*DialogServerSession, error) {
func (s *DialogServer) matchDialogRequest(req *sip.Request) (*DialogServerSession, error) {
id, err := sip.UASReadRequestDialogID(req)
if err != nil {
return nil, errors.Join(ErrDialogOutsideDialog, err)
Expand Down Expand Up @@ -62,9 +63,22 @@ func (s *DialogServer) ReadInvite(req *sip.Request, tx sip.ServerTransaction) (*
return nil, ErrDialogInviteNoContact
}

// Prebuild already to tag for response as it must be same for all responds
// NewResponseFromRequest will skip this for all 100
uuid, err := uuid.NewV4()
if err != nil {
return nil, fmt.Errorf("generating dialog to tag failed: %w", err)
}
req.To().Params["tag"] = uuid.String()
id, err := sip.UASReadRequestDialogID(req)
if err != nil {
return nil, err
}

ctx, cancel := context.WithCancel(context.Background())
dtx := &DialogServerSession{
Dialog: Dialog{
ID: id, // this id has already prebuilt tag
InviteRequest: req,
lastCSeqNo: req.CSeq().SeqNo,
state: atomic.Int32{},
Expand All @@ -81,7 +95,7 @@ func (s *DialogServer) ReadInvite(req *sip.Request, tx sip.ServerTransaction) (*

// ReadAck should read from your OnAck handler
func (s *DialogServer) ReadAck(req *sip.Request, tx sip.ServerTransaction) error {
dt, err := s.MatchDialogRequest(req)
dt, err := s.matchDialogRequest(req)
if err != nil {
return err
}
Expand All @@ -94,7 +108,7 @@ func (s *DialogServer) ReadAck(req *sip.Request, tx sip.ServerTransaction) error

// ReadBye should read from your OnBye handler
func (s *DialogServer) ReadBye(req *sip.Request, tx sip.ServerTransaction) error {
dt, err := s.MatchDialogRequest(req)
dt, err := s.matchDialogRequest(req)
if err != nil {
// https://datatracker.ietf.org/doc/html/rfc3261#section-15.1.2
// If the BYE does not
Expand Down Expand Up @@ -267,7 +281,9 @@ func (s *DialogServerSession) WriteResponse(res *sip.Response) error {
return err
}

s.Dialog.ID = id
if id != s.Dialog.ID {
return fmt.Errorf("ID do not match. Invite request has changed headers?")
}

// We need to make dialog present as ACK can land immediately after
s.s.dialogs.Store(id, s)
Expand Down

0 comments on commit 0e1fece

Please sign in to comment.