Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"strconv"
"syscall"
"time"
)

Expand Down Expand Up @@ -170,6 +171,11 @@ type Config struct {
// An implementation of the Logger interface
Logger Logger

// ListenerControl is called on the raw socket file descriptor after
// gosrt's built-in options (SO_REUSEADDR, IP_TOS, IP_TTL) are set
// but before the socket is bound. Same signature as net.ListenConfig.Control.
ListenerControl func(network, address string, c syscall.RawConn) error

// if a new IP starts sending data on an existing socket id, allow it
AllowPeerIpChange bool
}
Expand Down
11 changes: 10 additions & 1 deletion listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/netip"
"os"
"sync"
"syscall"
"time"

srtnet "github.com/datarhei/gosrt/net"
Expand Down Expand Up @@ -175,7 +176,15 @@ func Listen(network, address string, config Config) (Listener, error) {
}

lc := net.ListenConfig{
Control: ListenControl(config),
Control: func(network, address string, c syscall.RawConn) error {
if err := ListenControl(config)(network, address, c); err != nil {
return err
}
if config.ListenerControl != nil {
return config.ListenerControl(network, address, c)
}
return nil
},
}

network = "udp"
Expand Down
17 changes: 17 additions & 0 deletions listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"strconv"
"sync"
"syscall"
"testing"
"time"

Expand All @@ -27,6 +28,22 @@ func TestListenReuse(t *testing.T) {
ln.Close()
}

func TestListenerControl(t *testing.T) {
called := false

config := DefaultConfig()
config.ListenerControl = func(network, address string, c syscall.RawConn) error {
called = true
return nil
}

ln, err := Listen("srt", "127.0.0.1:6003", config)
require.NoError(t, err)
defer ln.Close()

require.True(t, called, "ListenerControl callback was not invoked")
}

func TestListen(t *testing.T) {
ln, err := Listen("srt", "127.0.0.1:6003", DefaultConfig())
require.NoError(t, err)
Expand Down