package quic

import (
	
	
	
	
	
	
	
	

	
	
	
)

type connCapabilities struct {
	// This connection has the Don't Fragment (DF) bit set.
	// This means it makes to run DPLPMTUD.
	DF bool
	// GSO (Generic Segmentation Offload) supported
	GSO bool
	// ECN (Explicit Congestion Notifications) supported
	ECN bool
}

// rawConn is a connection that allow reading of a receivedPackeh.
type rawConn interface {
	ReadPacket() (receivedPacket, error)
	// WritePacket writes a packet on the wire.
	// gsoSize is the size of a single packet, or 0 to disable GSO.
	// It is invalid to set gsoSize if capabilities.GSO is not set.
	WritePacket(b []byte, addr net.Addr, packetInfoOOB []byte, gsoSize uint16, ecn protocol.ECN) (int, error)
	LocalAddr() net.Addr
	SetReadDeadline(time.Time) error
	io.Closer

	capabilities() connCapabilities
}

// OOBCapablePacketConn is a connection that allows the reading of ECN bits from the IP header.
// If the PacketConn passed to the [Transport] satisfies this interface, quic-go will use it.
// In this case, ReadMsgUDP() will be used instead of ReadFrom() to read packets.
type OOBCapablePacketConn interface {
	net.PacketConn
	SyscallConn() (syscall.RawConn, error)
	SetReadBuffer(int) error
	ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error)
	WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
}

var _ OOBCapablePacketConn = &net.UDPConn{}

func wrapConn( net.PacketConn) (rawConn, error) {
	if  := setReceiveBuffer();  != nil {
		if !strings.Contains(.Error(), "use of closed network connection") {
			setBufferWarningOnce.Do(func() {
				if ,  := strconv.ParseBool(os.Getenv("QUIC_GO_DISABLE_RECEIVE_BUFFER_WARNING"));  {
					return
				}
				log.Printf("%s. See https://github.com/quic-go/quic-go/wiki/UDP-Buffer-Sizes for details.", )
			})
		}
	}
	if  := setSendBuffer();  != nil {
		if !strings.Contains(.Error(), "use of closed network connection") {
			setBufferWarningOnce.Do(func() {
				if ,  := strconv.ParseBool(os.Getenv("QUIC_GO_DISABLE_RECEIVE_BUFFER_WARNING"));  {
					return
				}
				log.Printf("%s. See https://github.com/quic-go/quic-go/wiki/UDP-Buffer-Sizes for details.", )
			})
		}
	}

	,  := .(interface {
		() (syscall.RawConn, error)
	})
	var  bool
	if  {
		,  := .()
		if  != nil {
			return nil, 
		}

		// only set DF on UDP sockets
		if ,  := .LocalAddr().(*net.UDPAddr);  {
			var  error
			,  = setDF()
			if  != nil {
				return nil, 
			}
		}
	}
	,  := .(OOBCapablePacketConn)
	if ! {
		utils.DefaultLogger.Infof("PacketConn is not a net.UDPConn. Disabling optimizations possible on UDP connections.")
		return &basicConn{PacketConn: , supportsDF: }, nil
	}
	return newConn(, )
}

// The basicConn is the most trivial implementation of a rawConn.
// It reads a single packet from the underlying net.PacketConn.
// It is used when
// * the net.PacketConn is not a OOBCapablePacketConn, and
// * when the OS doesn't support OOB.
type basicConn struct {
	net.PacketConn
	supportsDF bool
}

var _ rawConn = &basicConn{}

func ( *basicConn) () (receivedPacket, error) {
	 := getPacketBuffer()
	// The packet size should not exceed protocol.MaxPacketBufferSize bytes
	// If it does, we only read a truncated packet, which will then end up undecryptable
	.Data = .Data[:protocol.MaxPacketBufferSize]
	, ,  := .ReadFrom(.Data)
	if  != nil {
		return receivedPacket{}, 
	}
	return receivedPacket{
		remoteAddr: ,
		rcvTime:    monotime.Now(),
		data:       .Data[:],
		buffer:     ,
	}, nil
}

func ( *basicConn) ( []byte,  net.Addr,  []byte,  uint16,  protocol.ECN) ( int,  error) {
	if  != 0 {
		panic("cannot use GSO with a basicConn")
	}
	if  != protocol.ECNUnsupported {
		panic("cannot use ECN with a basicConn")
	}
	return .WriteTo(, )
}

func ( *basicConn) () connCapabilities { return connCapabilities{DF: .supportsDF} }