//go:build !windows && !darwin
// +build !windows,!darwin

package dns

import (
	

	
	
)

// This is the required size of the OOB buffer to pass to ReadMsgUDP.
var udpOOBSize = func() int {
	// We can't know whether we'll get an IPv4 control message or an
	// IPv6 control message ahead of time. To get around this, we size
	// the buffer equal to the largest of the two.

	 := ipv4.NewControlMessage(ipv4.FlagDst | ipv4.FlagInterface)
	 := ipv6.NewControlMessage(ipv6.FlagDst | ipv6.FlagInterface)

	if len() > len() {
		return len()
	}

	return len()
}()

// SessionUDP holds the remote address and the associated
// out-of-band data.
type SessionUDP struct {
	raddr   *net.UDPAddr
	context []byte
}

// RemoteAddr returns the remote network address.
func ( *SessionUDP) () net.Addr { return .raddr }

// ReadFromSessionUDP acts just like net.UDPConn.ReadFrom(), but returns a session object instead of a
// net.UDPAddr.
func ( *net.UDPConn,  []byte) (int, *SessionUDP, error) {
	 := make([]byte, udpOOBSize)
	, , , ,  := .ReadMsgUDP(, )
	if  != nil {
		return , nil, 
	}
	return , &SessionUDP{, [:]}, 
}

// WriteToSessionUDP acts just like net.UDPConn.WriteTo(), but uses a *SessionUDP instead of a net.Addr.
func ( *net.UDPConn,  []byte,  *SessionUDP) (int, error) {
	 := correctSource(.context)
	, ,  := .WriteMsgUDP(, , .raddr)
	return , 
}

func setUDPSocketOptions( *net.UDPConn) error {
	// Try setting the flags for both families and ignore the errors unless they
	// both error.
	 := ipv6.NewPacketConn().SetControlMessage(ipv6.FlagDst|ipv6.FlagInterface, true)
	 := ipv4.NewPacketConn().SetControlMessage(ipv4.FlagDst|ipv4.FlagInterface, true)
	if  != nil &&  != nil {
		return 
	}
	return nil
}

// parseDstFromOOB takes oob data and returns the destination IP.
func parseDstFromOOB( []byte) net.IP {
	// Start with IPv6 and then fallback to IPv4
	// TODO(fastest963): Figure out a way to prefer one or the other. Looking at
	// the lvl of the header for a 0 or 41 isn't cross-platform.
	 := new(ipv6.ControlMessage)
	if .Parse() == nil && .Dst != nil {
		return .Dst
	}
	 := new(ipv4.ControlMessage)
	if .Parse() == nil && .Dst != nil {
		return .Dst
	}
	return nil
}

// correctSource takes oob data and returns new oob data with the Src equal to the Dst
func correctSource( []byte) []byte {
	 := parseDstFromOOB()
	if  == nil {
		return nil
	}
	// If the dst is definitely an IPv6, then use ipv6's ControlMessage to
	// respond otherwise use ipv4's because ipv6's marshal ignores ipv4
	// addresses.
	if .To4() == nil {
		 := new(ipv6.ControlMessage)
		.Src = 
		 = .Marshal()
	} else {
		 := new(ipv4.ControlMessage)
		.Src = 
		 = .Marshal()
	}
	return 
}