Skip to content

Commit bab01a0

Browse files
authored
Use new call info types. (#304)
1 parent 93eba37 commit bab01a0

File tree

9 files changed

+106
-65
lines changed

9 files changed

+106
-65
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/jfreymuth/oggvorbis v1.0.5
1313
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1
1414
github.com/livekit/mediatransportutil v0.0.0-20241220010243-a2bdee945564
15-
github.com/livekit/protocol v1.34.1-0.20250227175732-e8f463c43a44
15+
github.com/livekit/protocol v1.34.1-0.20250301124708-e35a55f14851
1616
github.com/livekit/psrpc v0.6.1-0.20250205181828-a0beed2e4126
1717
github.com/livekit/server-sdk-go/v2 v2.5.0
1818
github.com/livekit/sipgo v0.13.2-0.20250130142851-36ed3228d934
@@ -61,7 +61,7 @@ require (
6161
github.com/emiago/sipgo v0.24.1 // indirect
6262
github.com/fsnotify/fsnotify v1.8.0 // indirect
6363
github.com/gammazero/deque v1.0.0 // indirect
64-
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
64+
github.com/go-jose/go-jose/v3 v3.0.4 // indirect
6565
github.com/go-logr/logr v1.4.2 // indirect
6666
github.com/go-logr/stdr v1.2.2 // indirect
6767
github.com/gobwas/httphead v0.1.0 // indirect

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/
6666
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
6767
github.com/gammazero/deque v1.0.0 h1:LTmimT8H7bXkkCy6gZX7zNLtkbz4NdS2z8LZuor3j34=
6868
github.com/gammazero/deque v1.0.0/go.mod h1:iflpYvtGfM3U8S8j+sZEKIak3SAKYpA5/SQewgfXDKo=
69-
github.com/go-jose/go-jose/v3 v3.0.3 h1:fFKWeig/irsp7XD2zBxvnmA/XaRWp5V3CBsZXJF7G7k=
70-
github.com/go-jose/go-jose/v3 v3.0.3/go.mod h1:5b+7YgP7ZICgJDBdfjZaIt+H/9L9T/YQrVfLAMboGkQ=
69+
github.com/go-jose/go-jose/v3 v3.0.4 h1:Wp5HA7bLQcKnf6YYao/4kpRpVMp/yf6+pJKV8WFSaNY=
70+
github.com/go-jose/go-jose/v3 v3.0.4/go.mod h1:5b+7YgP7ZICgJDBdfjZaIt+H/9L9T/YQrVfLAMboGkQ=
7171
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
7272
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
7373
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
@@ -127,8 +127,8 @@ github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 h1:jm09419p0lqTkD
127127
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
128128
github.com/livekit/mediatransportutil v0.0.0-20241220010243-a2bdee945564 h1:GX7KF/V9ExmcfT/2Bdia8aROjkxrgx7WpyH7w9MB4J4=
129129
github.com/livekit/mediatransportutil v0.0.0-20241220010243-a2bdee945564/go.mod h1:36s+wwmU3O40IAhE+MjBWP3W71QRiEE9SfooSBvtBqY=
130-
github.com/livekit/protocol v1.34.1-0.20250227175732-e8f463c43a44 h1:Cr8s9Vwm72S6Wu0TAiFnuXopnjL/aA0JhNgsz3AH7CI=
131-
github.com/livekit/protocol v1.34.1-0.20250227175732-e8f463c43a44/go.mod h1:yXuQ7ucrLj91nbxL6/AHgtxdha1DGzLj1LkgvnT90So=
130+
github.com/livekit/protocol v1.34.1-0.20250301124708-e35a55f14851 h1:avsQZ/oaPS3PZnnKcqsggrkg0k31sXmI+swuRhVEweE=
131+
github.com/livekit/protocol v1.34.1-0.20250301124708-e35a55f14851/go.mod h1:WrT/CYRxtMNOVUjnIPm5OjWtEkmreffTeE1PRZwlRg4=
132132
github.com/livekit/psrpc v0.6.1-0.20250205181828-a0beed2e4126 h1:fzuYpAQbCid7ySPpQWWePfQOWUrs8x6dJ0T3Wl07n+Y=
133133
github.com/livekit/psrpc v0.6.1-0.20250205181828-a0beed2e4126/go.mod h1:X5WtEZ7OnEs72Fi5/J+i0on3964F1aynQpCalcgMqRo=
134134
github.com/livekit/server-sdk-go/v2 v2.5.0 h1:HCKm3f6PvefGp8emNC2mi9+9IXzBYrynuGbtUdp5u+w=

pkg/service/psrpc.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package service
33
import (
44
"context"
55
"fmt"
6-
"net/netip"
76

87
"github.com/livekit/protocol/logger"
98
"github.com/livekit/protocol/rpc"
@@ -12,15 +11,18 @@ import (
1211
"github.com/livekit/sip/pkg/sip"
1312
)
1413

15-
func GetAuthCredentials(ctx context.Context, psrpcClient rpc.IOInfoClient, callID, from, to, toHost string, srcAddress netip.Addr) (sip.AuthInfo, error) {
14+
func GetAuthCredentials(ctx context.Context, psrpcClient rpc.IOInfoClient, call *rpc.SIPCall) (sip.AuthInfo, error) {
1615
ctx, span := tracer.Start(ctx, "service.GetAuthCredentials")
1716
defer span.End()
1817
resp, err := psrpcClient.GetSIPTrunkAuthentication(ctx, &rpc.GetSIPTrunkAuthenticationRequest{
19-
SipCallId: callID,
20-
From: from,
21-
To: to,
22-
ToHost: toHost,
23-
SrcAddress: srcAddress.String(),
18+
Call: call,
19+
20+
SipCallId: call.LkCallId,
21+
From: call.From.User,
22+
FromHost: call.From.Host,
23+
To: call.To.User,
24+
ToHost: call.To.Host,
25+
SrcAddress: call.SourceIp,
2426
})
2527

2628
if err != nil {
@@ -52,14 +54,17 @@ func DispatchCall(ctx context.Context, psrpcClient rpc.IOInfoClient, log logger.
5254
ctx, span := tracer.Start(ctx, "service.DispatchCall")
5355
defer span.End()
5456
resp, err := psrpcClient.EvaluateSIPDispatchRules(ctx, &rpc.EvaluateSIPDispatchRulesRequest{
55-
SipCallId: info.ID,
56-
SipTrunkId: info.TrunkID,
57-
CallingNumber: info.FromUser,
58-
CalledNumber: info.ToUser,
59-
CalledHost: info.ToHost,
60-
SrcAddress: info.SrcAddress.String(),
61-
Pin: info.Pin,
62-
NoPin: info.NoPin,
57+
SipTrunkId: info.TrunkID,
58+
Call: info.Call,
59+
Pin: info.Pin,
60+
NoPin: info.NoPin,
61+
62+
SipCallId: info.Call.LkCallId,
63+
CallingNumber: info.Call.From.User,
64+
CallingHost: info.Call.From.Host,
65+
CalledNumber: info.Call.To.User,
66+
CalledHost: info.Call.To.Host,
67+
SrcAddress: info.Call.SourceIp,
6368
})
6469

6570
if err != nil {

pkg/service/service.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"net"
2121
"net/http"
2222
"net/http/pprof"
23-
"net/netip"
2423
"sync/atomic"
2524
"time"
2625

@@ -171,8 +170,8 @@ func (s *Service) Run() error {
171170
}
172171
}
173172

174-
func (s *Service) GetAuthCredentials(ctx context.Context, callID, from, to, toHost string, srcAddress netip.Addr) (sip.AuthInfo, error) {
175-
return GetAuthCredentials(ctx, s.psrpcClient, callID, from, to, toHost, srcAddress)
173+
func (s *Service) GetAuthCredentials(ctx context.Context, call *rpc.SIPCall) (sip.AuthInfo, error) {
174+
return GetAuthCredentials(ctx, s.psrpcClient, call)
176175
}
177176

178177
func (s *Service) DispatchCall(ctx context.Context, info *sip.CallInfo) sip.CallDispatch {

pkg/sip/inbound.go

+40-22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package sip
1717
import (
1818
"context"
1919
"fmt"
20+
"github.com/livekit/protocol/rpc"
2021
"math"
2122
"net/netip"
2223
"slices"
@@ -184,7 +185,7 @@ func (s *Server) processInvite(req *sip.Request, tx sip.ServerTransaction) (retE
184185

185186
from, to := cc.From(), cc.To()
186187

187-
cmon := s.mon.NewCall(stats.Inbound, from.Host, cc.To().Host)
188+
cmon := s.mon.NewCall(stats.Inbound, from.Host, to.Host)
188189
cmon.InviteReq()
189190
defer cmon.SessionDur()()
190191
joinDur := cmon.JoinDur()
@@ -193,7 +194,25 @@ func (s *Server) processInvite(req *sip.Request, tx sip.ServerTransaction) (retE
193194
cc.Processing()
194195
}
195196

196-
r, err := s.handler.GetAuthCredentials(ctx, callID, from.User, to.User, to.Host, src.Addr())
197+
callInfo := &rpc.SIPCall{
198+
LkCallId: callID,
199+
SourceIp: src.Addr().String(),
200+
Address: ToSIPUri("", cc.Address()),
201+
From: ToSIPUri("", from),
202+
To: ToSIPUri("", to),
203+
}
204+
for _, h := range cc.RemoteHeaders() {
205+
switch h := h.(type) {
206+
case *sip.ViaHeader:
207+
callInfo.Via = append(callInfo.Via, &livekit.SIPUri{
208+
Host: h.Host,
209+
Port: uint32(h.Port),
210+
Transport: SIPTransportFrom(Transport(h.Transport)),
211+
})
212+
}
213+
}
214+
215+
r, err := s.handler.GetAuthCredentials(ctx, callInfo)
197216
if err != nil {
198217
cmon.InviteErrorShort("auth-error")
199218
log.Warnw("Rejecting inbound, auth check failed", err)
@@ -245,7 +264,7 @@ func (s *Server) processInvite(req *sip.Request, tx sip.ServerTransaction) (retE
245264
// ok
246265
}
247266

248-
call = s.newInboundCall(log, cmon, cc, src, state, nil)
267+
call = s.newInboundCall(log, cmon, cc, callInfo, state, nil)
249268
call.joinDur = joinDur
250269
return call.handleInvite(call.ctx, req, r.TrunkID, s.conf)
251270
}
@@ -316,7 +335,7 @@ type inboundCall struct {
316335
attrsToHdr map[string]string
317336
ctx context.Context
318337
cancel func()
319-
src netip.AddrPort
338+
call *rpc.SIPCall
320339
media *MediaPort
321340
dtmf chan dtmf.Event // buffered
322341
lkRoom *Room // LiveKit room; only active after correct pin is entered
@@ -331,7 +350,7 @@ func (s *Server) newInboundCall(
331350
log logger.Logger,
332351
mon *stats.CallMonitor,
333352
cc *sipInbound,
334-
src netip.AddrPort,
353+
call *rpc.SIPCall,
335354
state *CallState,
336355
extra map[string]string,
337356
) *inboundCall {
@@ -342,7 +361,7 @@ func (s *Server) newInboundCall(
342361
log: log,
343362
mon: mon,
344363
cc: cc,
345-
src: src,
364+
call: call,
346365
state: state,
347366
extraAttrs: extra,
348367
dtmf: make(chan dtmf.Event, 10),
@@ -366,14 +385,10 @@ func (c *inboundCall) handleInvite(ctx context.Context, req *sip.Request, trunkI
366385
// Send initial request. In the best case scenario, we will immediately get a room name to join.
367386
// Otherwise, we could even learn that this number is not allowed and reject the call, or ask for pin if required.
368387
disp := c.s.handler.DispatchCall(ctx, &CallInfo{
369-
TrunkID: trunkID,
370-
ID: string(c.cc.ID()),
371-
FromUser: c.cc.From().User,
372-
ToUser: c.cc.To().User,
373-
ToHost: c.cc.To().Host,
374-
SrcAddress: c.src.Addr(),
375-
Pin: "",
376-
NoPin: false,
388+
TrunkID: trunkID,
389+
Call: c.call,
390+
Pin: "",
391+
NoPin: false,
377392
})
378393
if disp.ProjectID != "" {
379394
c.log = c.log.WithValues("projectID", disp.ProjectID)
@@ -659,14 +674,10 @@ func (c *inboundCall) pinPrompt(ctx context.Context, trunkID string) (disp CallD
659674

660675
c.log.Infow("Checking Pin for SIP call", "pin", pin, "noPin", noPin)
661676
disp = c.s.handler.DispatchCall(ctx, &CallInfo{
662-
TrunkID: trunkID,
663-
ID: string(c.cc.ID()),
664-
FromUser: c.cc.From().User,
665-
ToUser: c.cc.To().User,
666-
ToHost: c.cc.To().Host,
667-
SrcAddress: c.src.Addr(),
668-
Pin: pin,
669-
NoPin: noPin,
677+
TrunkID: trunkID,
678+
Call: c.call,
679+
Pin: pin,
680+
NoPin: noPin,
670681
})
671682
if disp.ProjectID != "" {
672683
c.log = c.log.WithValues("projectID", disp.ProjectID)
@@ -1008,6 +1019,13 @@ func (c *sipInbound) RespondAndDrop(status sip.StatusCode, reason string) {
10081019
c.drop()
10091020
}
10101021

1022+
func (c *sipInbound) Address() sip.Uri {
1023+
if c.invite == nil {
1024+
return sip.Uri{}
1025+
}
1026+
return c.invite.Recipient
1027+
}
1028+
10111029
func (c *sipInbound) From() sip.Uri {
10121030
if c.from == nil {
10131031
return sip.Uri{}

pkg/sip/outbound.go

+9
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,15 @@ func (c *sipOutbound) To() sip.Uri {
649649
return c.to.Address
650650
}
651651

652+
func (c *sipOutbound) Address() sip.Uri {
653+
c.mu.RLock()
654+
defer c.mu.RUnlock()
655+
if c.invite == nil {
656+
return sip.Uri{}
657+
}
658+
return c.invite.Recipient
659+
}
660+
652661
func (c *sipOutbound) ID() LocalTag {
653662
return c.id
654663
}

pkg/sip/protocol.go

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/pkg/errors"
2727

28+
"github.com/livekit/protocol/livekit"
2829
"github.com/livekit/psrpc"
2930
"github.com/livekit/sipgo/sip"
3031

@@ -107,6 +108,7 @@ func statusName(status int) string {
107108
type setHeadersFunc func(headers map[string]string) map[string]string
108109

109110
type Signaling interface {
111+
Address() sip.Uri
110112
From() sip.Uri
111113
To() sip.Uri
112114
ID() LocalTag
@@ -363,3 +365,15 @@ func setCSeq(req *sip.Request, cseq uint32) {
363365
req.RemoveHeader(h.Name())
364366
req.AppendHeader(h)
365367
}
368+
369+
func ToSIPUri(ip string, u sip.Uri) *livekit.SIPUri {
370+
tr, _ := u.UriParams.Get("transport")
371+
url := &livekit.SIPUri{
372+
User: u.User,
373+
Host: u.Host,
374+
Ip: ip,
375+
Port: uint32(u.Port),
376+
Transport: SIPTransportFrom(Transport(tr)),
377+
}
378+
return url
379+
}

pkg/sip/server.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"crypto/tls"
2020
"errors"
2121
"fmt"
22+
"github.com/livekit/protocol/rpc"
2223
"io"
2324
"log/slog"
2425
"net"
@@ -50,14 +51,10 @@ var (
5051
)
5152

5253
type CallInfo struct {
53-
TrunkID string
54-
ID string
55-
FromUser string
56-
ToUser string
57-
ToHost string
58-
SrcAddress netip.Addr
59-
Pin string
60-
NoPin bool
54+
TrunkID string
55+
Call *rpc.SIPCall
56+
Pin string
57+
NoPin bool
6158
}
6259

6360
type AuthResult int
@@ -102,7 +99,7 @@ type CallDispatch struct {
10299
}
103100

104101
type Handler interface {
105-
GetAuthCredentials(ctx context.Context, callID, fromUser, toUser, toHost string, srcAddress netip.Addr) (AuthInfo, error)
102+
GetAuthCredentials(ctx context.Context, call *rpc.SIPCall) (AuthInfo, error)
106103
DispatchCall(ctx context.Context, info *CallInfo) CallDispatch
107104
GetMediaProcessor(features []livekit.SIPFeature) media.PCM16Processor
108105

pkg/sip/service_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"math/rand"
7-
"net/netip"
87
"testing"
98
"time"
109

@@ -54,12 +53,12 @@ func expectNoResponse(t *testing.T, tx sip.ClientTransaction) {
5453
}
5554

5655
type TestHandler struct {
57-
GetAuthCredentialsFunc func(ctx context.Context, callID, fromUser, toUser, toHost string, srcAddress netip.Addr) (AuthInfo, error)
56+
GetAuthCredentialsFunc func(ctx context.Context, call *rpc.SIPCall) (AuthInfo, error)
5857
DispatchCallFunc func(ctx context.Context, info *CallInfo) CallDispatch
5958
}
6059

61-
func (h TestHandler) GetAuthCredentials(ctx context.Context, callID, fromUser, toUser, toHost string, srcAddress netip.Addr) (AuthInfo, error) {
62-
return h.GetAuthCredentialsFunc(ctx, callID, fromUser, toUser, toHost, srcAddress)
60+
func (h TestHandler) GetAuthCredentials(ctx context.Context, call *rpc.SIPCall) (AuthInfo, error) {
61+
return h.GetAuthCredentialsFunc(ctx, call)
6362
}
6463

6564
func (h TestHandler) DispatchCall(ctx context.Context, info *CallInfo) CallDispatch {
@@ -132,9 +131,9 @@ func TestService_AuthFailure(t *testing.T) {
132131
expectedToUser = "bar"
133132
)
134133
h := &TestHandler{
135-
GetAuthCredentialsFunc: func(ctx context.Context, callID, fromUser, toUser, toHost string, srcAddress netip.Addr) (AuthInfo, error) {
136-
require.Equal(t, expectedFromUser, fromUser)
137-
require.Equal(t, expectedToUser, toUser)
134+
GetAuthCredentialsFunc: func(ctx context.Context, call *rpc.SIPCall) (AuthInfo, error) {
135+
require.Equal(t, expectedFromUser, call.From.User)
136+
require.Equal(t, expectedToUser, call.To.User)
138137
return AuthInfo{}, fmt.Errorf("Auth Failure")
139138
},
140139
}
@@ -153,9 +152,9 @@ func TestService_AuthDrop(t *testing.T) {
153152
expectedToUser = "bar"
154153
)
155154
h := &TestHandler{
156-
GetAuthCredentialsFunc: func(ctx context.Context, callID, fromUser, toUser, toHost string, srcAddress netip.Addr) (AuthInfo, error) {
157-
require.Equal(t, expectedFromUser, fromUser)
158-
require.Equal(t, expectedToUser, toUser)
155+
GetAuthCredentialsFunc: func(ctx context.Context, call *rpc.SIPCall) (AuthInfo, error) {
156+
require.Equal(t, expectedFromUser, call.From.User)
157+
require.Equal(t, expectedToUser, call.To.User)
159158
return AuthInfo{Result: AuthDrop}, nil
160159
},
161160
}

0 commit comments

Comments
 (0)