golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/ipv6/control_rfc3542_unix.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
     6  
     7  package ipv6
     8  
     9  import (
    10  	"net"
    11  	"unsafe"
    12  
    13  	"golang.org/x/net/internal/iana"
    14  	"golang.org/x/net/internal/socket"
    15  
    16  	"golang.org/x/sys/unix"
    17  )
    18  
    19  func marshalTrafficClass(b []byte, cm *ControlMessage) []byte {
    20  	m := socket.ControlMessage(b)
    21  	m.MarshalHeader(iana.ProtocolIPv6, unix.IPV6_TCLASS, 4)
    22  	if cm != nil {
    23  		socket.NativeEndian.PutUint32(m.Data(4), uint32(cm.TrafficClass))
    24  	}
    25  	return m.Next(4)
    26  }
    27  
    28  func parseTrafficClass(cm *ControlMessage, b []byte) {
    29  	cm.TrafficClass = int(socket.NativeEndian.Uint32(b[:4]))
    30  }
    31  
    32  func marshalHopLimit(b []byte, cm *ControlMessage) []byte {
    33  	m := socket.ControlMessage(b)
    34  	m.MarshalHeader(iana.ProtocolIPv6, unix.IPV6_HOPLIMIT, 4)
    35  	if cm != nil {
    36  		socket.NativeEndian.PutUint32(m.Data(4), uint32(cm.HopLimit))
    37  	}
    38  	return m.Next(4)
    39  }
    40  
    41  func parseHopLimit(cm *ControlMessage, b []byte) {
    42  	cm.HopLimit = int(socket.NativeEndian.Uint32(b[:4]))
    43  }
    44  
    45  func marshalPacketInfo(b []byte, cm *ControlMessage) []byte {
    46  	m := socket.ControlMessage(b)
    47  	m.MarshalHeader(iana.ProtocolIPv6, unix.IPV6_PKTINFO, sizeofInet6Pktinfo)
    48  	if cm != nil {
    49  		pi := (*inet6Pktinfo)(unsafe.Pointer(&m.Data(sizeofInet6Pktinfo)[0]))
    50  		if ip := cm.Src.To16(); ip != nil && ip.To4() == nil {
    51  			copy(pi.Addr[:], ip)
    52  		}
    53  		if cm.IfIndex > 0 {
    54  			pi.setIfindex(cm.IfIndex)
    55  		}
    56  	}
    57  	return m.Next(sizeofInet6Pktinfo)
    58  }
    59  
    60  func parsePacketInfo(cm *ControlMessage, b []byte) {
    61  	pi := (*inet6Pktinfo)(unsafe.Pointer(&b[0]))
    62  	if len(cm.Dst) < net.IPv6len {
    63  		cm.Dst = make(net.IP, net.IPv6len)
    64  	}
    65  	copy(cm.Dst, pi.Addr[:])
    66  	cm.IfIndex = int(pi.Ifindex)
    67  }
    68  
    69  func marshalNextHop(b []byte, cm *ControlMessage) []byte {
    70  	m := socket.ControlMessage(b)
    71  	m.MarshalHeader(iana.ProtocolIPv6, unix.IPV6_NEXTHOP, sizeofSockaddrInet6)
    72  	if cm != nil {
    73  		sa := (*sockaddrInet6)(unsafe.Pointer(&m.Data(sizeofSockaddrInet6)[0]))
    74  		sa.setSockaddr(cm.NextHop, cm.IfIndex)
    75  	}
    76  	return m.Next(sizeofSockaddrInet6)
    77  }
    78  
    79  func parseNextHop(cm *ControlMessage, b []byte) {
    80  }
    81  
    82  func marshalPathMTU(b []byte, cm *ControlMessage) []byte {
    83  	m := socket.ControlMessage(b)
    84  	m.MarshalHeader(iana.ProtocolIPv6, unix.IPV6_PATHMTU, sizeofIPv6Mtuinfo)
    85  	return m.Next(sizeofIPv6Mtuinfo)
    86  }
    87  
    88  func parsePathMTU(cm *ControlMessage, b []byte) {
    89  	mi := (*ipv6Mtuinfo)(unsafe.Pointer(&b[0]))
    90  	if len(cm.Dst) < net.IPv6len {
    91  		cm.Dst = make(net.IP, net.IPv6len)
    92  	}
    93  	copy(cm.Dst, mi.Addr.Addr[:])
    94  	cm.IfIndex = int(mi.Addr.Scope_id)
    95  	cm.MTU = int(mi.Mtu)
    96  }