Skip to content

Commit 020b1e8

Browse files
Addressing comment
1 parent ab3204d commit 020b1e8

4 files changed

Lines changed: 24 additions & 31 deletions

File tree

pkg/sip/client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"github.com/livekit/psrpc"
3939
"github.com/livekit/sipgo"
4040
"github.com/livekit/sipgo/sip"
41-
"github.com/livekit/sipgo/transport"
4241

4342
"github.com/livekit/sip/pkg/config"
4443
siperrors "github.com/livekit/sip/pkg/errors"
@@ -51,9 +50,9 @@ import (
5150
type SIPClient interface {
5251
TransactionRequest(req *sip.Request, options ...sipgo.ClientRequestOption) (sip.ClientTransaction, error)
5352
WriteRequest(req *sip.Request, options ...sipgo.ClientRequestOption) error
54-
// TransportLayer exposes sipgo's resolution and connection handling. It may
55-
// be nil for clients that do not own one.
56-
TransportLayer() *transport.Layer
53+
// ResolveTargets returns the addresses to try for a request URI, in order.
54+
// port is the port from the URI, or 0 when it carries none.
55+
ResolveTargets(ctx context.Context, network, host string, port int, sipScheme string) ([]netip.AddrPort, error)
5756
Close() error
5857
}
5958

pkg/sip/outbound.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ import (
3939
"github.com/livekit/protocol/utils/traceid"
4040
"github.com/livekit/psrpc"
4141
lksdk "github.com/livekit/server-sdk-go/v2"
42+
"github.com/livekit/sipgo"
4243
"github.com/livekit/sipgo/sip"
43-
"github.com/livekit/sipgo/transport"
4444

4545
"github.com/livekit/sip/pkg/config"
4646
"github.com/livekit/sip/pkg/stats"
@@ -1069,11 +1069,7 @@ func (c *sipOutbound) inviteDestinations(ctx context.Context, headers map[string
10691069
}
10701070
}
10711071

1072-
tpl := c.transportLayer()
1073-
if tpl == nil {
1074-
return unresolved
1075-
}
1076-
targets, err := tpl.ResolveTargets(ctx, uriTransport(c.uri), c.uri.Host, c.uri.Port, c.uri.Scheme)
1072+
targets, err := c.c.sipCli.ResolveTargets(ctx, uriTransport(c.uri), c.uri.Host, c.uri.Port, c.uri.Scheme)
10771073
if err != nil {
10781074
c.log.Warnw("could not resolve destination, falling back to transport resolution", err,
10791075
"host", c.uri.Host, "port", c.uri.Port)
@@ -1197,15 +1193,6 @@ authLoop:
11971193
return req, resp, nil
11981194
}
11991195

1200-
// transportLayer returns sipgo's transport layer, or nil when the SIP client is
1201-
// not the real one, as in tests that substitute their own.
1202-
func (c *sipOutbound) transportLayer() *transport.Layer {
1203-
if c.c.sipCli == nil {
1204-
return nil
1205-
}
1206-
return c.c.sipCli.TransportLayer()
1207-
}
1208-
12091196
// uriTransport returns the transport a request to uri will use, matching how
12101197
// sipgo derives it from the request.
12111198
func uriTransport(uri *sip.Uri) string {
@@ -1317,12 +1304,16 @@ func (c *sipOutbound) attemptInvite(ctx context.Context, dest string, callID sip
13171304

13181305
// Log the actual local port used for TCP connections from the DialPort range
13191306
if req.Transport() == "TCP" {
1320-
if tpl := c.transportLayer(); tpl != nil {
1321-
// The connection should be available after TransactionRequest creates it
1322-
if dest := req.Destination(); dest != "" {
1323-
if conn, err := tpl.GetConnection("tcp", dest); err == nil && conn != nil {
1324-
if tcpAddr, ok := conn.LocalAddr().(*net.TCPAddr); ok && tcpAddr != nil {
1325-
c.log.Debugw("TCP connection using port on cloud-sip side", "port", tcpAddr.Port)
1307+
// Type-assert to *sipgo.Client to access the embedded UserAgent
1308+
if sipClient, ok := c.c.sipCli.(*sipgo.Client); ok {
1309+
if tpl := sipClient.TransportLayer(); tpl != nil {
1310+
// Try to get the connection using the destination address
1311+
// The connection should be available after TransactionRequest creates it
1312+
if dest := req.Destination(); dest != "" {
1313+
if conn, err := tpl.GetConnection("tcp", dest); err == nil && conn != nil {
1314+
if tcpAddr, ok := conn.LocalAddr().(*net.TCPAddr); ok && tcpAddr != nil {
1315+
c.log.Debugw("TCP connection using port on cloud-sip side", "port", tcpAddr.Port)
1316+
}
13261317
}
13271318
}
13281319
}

pkg/sip/outbound_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ package sip
1717
import (
1818
"context"
1919
"encoding/binary"
20+
"errors"
2021
"fmt"
2122
"net"
23+
"net/netip"
2224
"strings"
2325
"sync"
2426
"testing"
@@ -32,7 +34,6 @@ import (
3234
"github.com/livekit/protocol/rpc"
3335
"github.com/livekit/sipgo"
3436
"github.com/livekit/sipgo/sip"
35-
"github.com/livekit/sipgo/transport"
3637
)
3738

3839
// recordingSIPClient is a SIPClient that records the requests written to it.
@@ -49,7 +50,9 @@ func (c *recordingSIPClient) WriteRequest(req *sip.Request, _ ...sipgo.ClientReq
4950
return nil
5051
}
5152

52-
func (c *recordingSIPClient) TransportLayer() *transport.Layer { return nil }
53+
func (c *recordingSIPClient) ResolveTargets(_ context.Context, _, _ string, _ int, _ string) ([]netip.AddrPort, error) {
54+
return nil, errors.New("not resolved in this test")
55+
}
5356

5457
func (c *recordingSIPClient) Close() error { return nil }
5558

pkg/sip/outbound_utilities_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121
"net"
22+
"net/netip"
2223
"sync/atomic"
2324
"testing"
2425
"time"
@@ -32,7 +33,6 @@ import (
3233
"github.com/livekit/psrpc"
3334
"github.com/livekit/sipgo"
3435
"github.com/livekit/sipgo/sip"
35-
"github.com/livekit/sipgo/transport"
3636

3737
msdk "github.com/livekit/media-sdk"
3838
"github.com/livekit/media-sdk/dtmf"
@@ -358,11 +358,11 @@ type testSIPClient struct {
358358
sequence uint64
359359
}
360360

361-
func (w *testSIPClient) TransportLayer() *transport.Layer {
361+
func (w *testSIPClient) ResolveTargets(ctx context.Context, network, host string, port int, sipScheme string) ([]netip.AddrPort, error) {
362362
if w.client == nil {
363-
return nil
363+
return nil, errors.New("no sip client")
364364
}
365-
return w.client.TransportLayer()
365+
return w.client.ResolveTargets(ctx, network, host, port, sipScheme)
366366
}
367367

368368
func (w *testSIPClient) FillRequestBlanks(req *sip.Request) {

0 commit comments

Comments
 (0)