// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package dtls

import (
	

	
	
	
)

// Listen creates a DTLS listener
func ( string,  *net.UDPAddr,  *Config) (net.Listener, error) {
	if  := validateConfig();  != nil {
		return nil, 
	}

	 := udp.ListenConfig{
		AcceptFilter: func( []byte) bool {
			,  := recordlayer.UnpackDatagram()
			if  != nil || len() < 1 {
				return false
			}
			 := &recordlayer.Header{}
			if  := .Unmarshal([0]);  != nil {
				return false
			}
			return .ContentType == protocol.ContentTypeHandshake
		},
	}
	,  := .Listen(, )
	if  != nil {
		return nil, 
	}
	return &listener{
		config: ,
		parent: ,
	}, nil
}

// NewListener creates a DTLS listener which accepts connections from an inner Listener.
func ( net.Listener,  *Config) (net.Listener, error) {
	if  := validateConfig();  != nil {
		return nil, 
	}

	return &listener{
		config: ,
		parent: ,
	}, nil
}

// listener represents a DTLS listener
type listener struct {
	config *Config
	parent net.Listener
}

// 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 ( *listener) () (net.Conn, error) {
	,  := .parent.Accept()
	if  != nil {
		return nil, 
	}
	return Server(, .config)
}

// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
// Already Accepted connections are not closed.
func ( *listener) () error {
	return .parent.Close()
}

// Addr returns the listener's network address.
func ( *listener) () net.Addr {
	return .parent.Addr()
}