From c53fdf747a55c83b5e82eaeb3f07f48f2d249088 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Tue, 5 Dec 2023 20:40:13 +0200 Subject: [PATCH] Properly send BYE for inbound. --- pkg/sip/config.go | 3 +++ pkg/sip/inbound.go | 26 +++++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/sip/config.go b/pkg/sip/config.go index 6bd3f1bb..00863b35 100644 --- a/pkg/sip/config.go +++ b/pkg/sip/config.go @@ -22,6 +22,8 @@ import ( "net" "net/http" "time" + + "github.com/livekit/protocol/logger" ) var source *rand.Rand @@ -115,6 +117,7 @@ func listenUDPInPortRange(portMin, portMax int, IP net.IP) (*net.UDPConn, error) for { c, e := net.ListenUDP("udp", &net.UDPAddr{IP: IP, Port: portCurrent}) if e == nil { + logger.Debugw("begin listening on UDP", "port", portCurrent) return c, nil } diff --git a/pkg/sip/inbound.go b/pkg/sip/inbound.go index eaa3ff30..9986d143 100644 --- a/pkg/sip/inbound.go +++ b/pkg/sip/inbound.go @@ -184,7 +184,7 @@ func (c *inboundCall) handleInvite(req *sip.Request, tx sip.ServerTransaction, c res.AppendHeader(&contentTypeHeaderSDP) if err = tx.Respond(res); err != nil { logger.Errorw("Cannot respond to INVITE", err) - // TODO: should we close the call in this case? + c.Close() return } c.inviteReq = req @@ -201,8 +201,24 @@ func (c *inboundCall) sendBye() { if c.inviteReq == nil { return } - res := sip.NewByeRequest(c.inviteReq, c.inviteResp, nil) - c.s.sipSrv.TransportLayer().WriteMsg(res) + // This function is for clients, so we need to swap src and dest + bye := sip.NewByeRequest(c.inviteReq, c.inviteResp, nil) + if contact, ok := c.inviteReq.Contact(); ok { + bye.Recipient = &contact.Address + } else { + bye.Recipient = &c.from.Address + } + bye.SetSource(c.inviteResp.Source()) + bye.SetDestination(c.inviteResp.Destination()) + bye.RemoveHeader("From") + bye.AppendHeader((*sip.FromHeader)(c.to)) + bye.RemoveHeader("To") + bye.AppendHeader((*sip.ToHeader)(c.from)) + if route, ok := bye.RecordRoute(); ok { + bye.RemoveHeader("Record-Route") + bye.AppendHeader(&sip.RouteHeader{Address: route.Address}) + } + _ = c.s.sipSrv.TransportLayer().WriteMsg(bye) c.inviteReq = nil c.inviteResp = nil } @@ -274,11 +290,11 @@ func (c *inboundCall) Close() error { return nil } logger.Infow("Closing inbound call", "tag", c.tag, "from", c.from.Address.User, "to", c.to.Address.User) + c.sendBye() + c.closeMedia() c.s.cmu.Lock() delete(c.s.activeCalls, c.tag) c.s.cmu.Unlock() - c.closeMedia() - c.sendBye() return nil }