//go:build unix

package socket

import (
	
	

	
)

// Sizes of struct in_pktinfo and struct in6_pktinfo, laid out the same on
// every platform that has them.
const (
	sizeofInet4Pktinfo = 12 // uint32 ifindex, in_addr spec_dst, in_addr addr
	sizeofInet6Pktinfo = 20 // in6_addr addr, uint32 ifindex
)

// parsePacketInfo extracts the destination address of a received datagram
// from its control messages without allocating. Both families are
// recognized: the level the kernel uses depends on the socket and the
// traffic, not on the bind address alone.
func parsePacketInfo( []byte) (localAddr, bool) {
	for len() > 0 {
		, , ,  := unix.ParseOneSocketControlMessage()
		if  != nil {
			return localAddr{}, false
		}
		switch {
		case .Level == unix.IPPROTO_IP && .Type == sysIPPktinfo && len() >= sizeofInet4Pktinfo:
			return localAddr{
				addr:    netip.AddrFrom4([4]byte([8:12])),
				ifIndex: int(binary.NativeEndian.Uint32([0:4])),
			}, true
		case .Level == unix.IPPROTO_IPV6 && .Type == unix.IPV6_PKTINFO && len() >= sizeofInet6Pktinfo:
			 := netip.AddrFrom16([16]byte([0:16]))
			 := 0
			if .IsLinkLocalUnicast() {
				 = int(binary.NativeEndian.Uint32([16:20]))
			}
			return localAddr{addr: .Unmap(), ifIndex: }, true
		}
		 = 
	}
	return localAddr{}, false
}