From 8022f024a470e0b7455f3b5b983e32a37aa51bb9 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Thu, 14 Nov 2024 17:13:23 +0200 Subject: [PATCH] Preserve Call-ID header during auth loop. --- pkg/sip/outbound.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/sip/outbound.go b/pkg/sip/outbound.go index ca61c84..bed7a5e 100644 --- a/pkg/sip/outbound.go +++ b/pkg/sip/outbound.go @@ -601,8 +601,11 @@ func (c *sipOutbound) Invite(ctx context.Context, to URI, user, pass string, hea } } authLoop: - for { - req, resp, err = c.attemptInvite(ctx, dest, toHeader, sdpOffer, authHeaderRespName, authHeader, sipHeaders) + for try := 0; ; try++ { + if try >= 5 { + return nil, fmt.Errorf("max auth retry attemps reached") + } + req, resp, err = c.attemptInvite(ctx, req, dest, toHeader, sdpOffer, authHeaderRespName, authHeader, sipHeaders) if err != nil { return nil, err } @@ -700,10 +703,16 @@ func (c *sipOutbound) AckInvite(ctx context.Context) error { return c.c.sipCli.WriteRequest(sip.NewAckRequest(c.invite, c.inviteOk, nil)) } -func (c *sipOutbound) attemptInvite(ctx context.Context, dest string, to *sip.ToHeader, offer []byte, authHeaderName, authHeader string, headers Headers) (*sip.Request, *sip.Response, error) { +func (c *sipOutbound) attemptInvite(ctx context.Context, prev *sip.Request, dest string, to *sip.ToHeader, offer []byte, authHeaderName, authHeader string, headers Headers) (*sip.Request, *sip.Response, error) { ctx, span := tracer.Start(ctx, "sipOutbound.attemptInvite") defer span.End() req := sip.NewRequest(sip.INVITE, &to.Address) + if prev != nil { + if cid, _ := prev.CallID(); cid != nil { + req.RemoveHeader("Call-ID") + req.AppendHeader(cid) + } + } req.SetDestination(dest) req.SetBody(offer) req.AppendHeader(to)