Skip to content

Commit

Permalink
Set UDP routing if CID is enabled
Browse files Browse the repository at this point in the history
Sets UDP routing functions in the default DTLS listener if a connection
ID generator is provided. Also updates to accept a dtls
net.PacketListener when a caller wishes to provide their own.

Signed-off-by: Daniel Mangum <georgedanielmangum@gmail.com>
  • Loading branch information
hasheddan committed Aug 27, 2023
1 parent e663309 commit f5875c1
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package dtls
import (
"net"

"github.com/pion/dtls/v2/internal/util"
"github.com/pion/dtls/v2/internal/net/udp"
dtlsnet "github.com/pion/dtls/v2/pkg/net"
"github.com/pion/dtls/v2/pkg/protocol"
"github.com/pion/dtls/v2/pkg/protocol/recordlayer"
"github.com/pion/transport/v2/udp"
)

// Listen creates a DTLS listener
Expand All @@ -31,6 +31,12 @@ func Listen(network string, laddr *net.UDPAddr, config *Config) (net.Listener, e
return h.ContentType == protocol.ContentTypeHandshake
},
}
// If connection ID support is enabled, then they must be supported in
// routing.
if config.ConnectionIDGenerator != nil {
lc.DatagramRouter = cidDatagramRouter(len(config.ConnectionIDGenerator()))
lc.ConnectionIdentifier = cidConnIdentifier()
}
parent, err := lc.Listen(network, laddr)
if err != nil {
return nil, err
Expand All @@ -42,7 +48,7 @@ func Listen(network string, laddr *net.UDPAddr, config *Config) (net.Listener, e
}

// NewListener creates a DTLS listener which accepts connections from an inner Listener.
func NewListener(inner net.Listener, config *Config) (net.Listener, error) {
func NewListener(inner dtlsnet.PacketListener, config *Config) (net.Listener, error) {
if err := validateConfig(config); err != nil {
return nil, err
}
Expand All @@ -56,19 +62,19 @@ func NewListener(inner net.Listener, config *Config) (net.Listener, error) {
// listener represents a DTLS listener
type listener struct {
config *Config
parent net.Listener
parent dtlsnet.PacketListener
}

// Accept waits for and returns the next connection to the listener.
// You have to either close or read on all connection that are created.
// Connection handshake will timeout using ConnectContextMaker in the Config.
// If you want to specify the timeout duration, set ConnectContextMaker.
func (l *listener) Accept() (net.Conn, error) {
c, err := l.parent.Accept()
c, raddr, err := l.parent.Accept()
if err != nil {
return nil, err
}
return Server(util.FromConn(c), c.RemoteAddr(), l.config)
return Server(c, raddr, l.config)
}

// Close closes the listener.
Expand Down

0 comments on commit f5875c1

Please sign in to comment.