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

package dtls

import (
	

	
	dtlsnet 
	
	
)

// 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
		},
	}
	// If connection ID support is enabled, then they must be supported in
	// routing.
	if .ConnectionIDGenerator != nil {
		.DatagramRouter = cidDatagramRouter(len(.ConnectionIDGenerator()))
		.ConnectionIdentifier = cidConnIdentifier()
	}
	,  := .Listen(, )
	if  != nil {
		return nil, 
	}

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

// NewListener creates a DTLS listener which accepts connections from an inner Listener.
func ( dtlsnet.PacketListener,  *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 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.
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()
}