github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/ipv6/control_rfc2292_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 darwin
     6  // +build darwin
     7  
     8  package ipv6
     9  
    10  import (
    11  	"unsafe"
    12  
    13  	"github.com/hxx258456/ccgo/net/internal/iana"
    14  	"github.com/hxx258456/ccgo/net/internal/socket"
    15  
    16  	"golang.org/x/sys/unix"
    17  )
    18  
    19  func marshal2292HopLimit(b []byte, cm *ControlMessage) []byte {
    20  	m := socket.ControlMessage(b)
    21  	m.MarshalHeader(iana.ProtocolIPv6, unix.IPV6_2292HOPLIMIT, 4)
    22  	if cm != nil {
    23  		socket.NativeEndian.PutUint32(m.Data(4), uint32(cm.HopLimit))
    24  	}
    25  	return m.Next(4)
    26  }
    27  
    28  func marshal2292PacketInfo(b []byte, cm *ControlMessage) []byte {
    29  	m := socket.ControlMessage(b)
    30  	m.MarshalHeader(iana.ProtocolIPv6, unix.IPV6_2292PKTINFO, sizeofInet6Pktinfo)
    31  	if cm != nil {
    32  		pi := (*inet6Pktinfo)(unsafe.Pointer(&m.Data(sizeofInet6Pktinfo)[0]))
    33  		if ip := cm.Src.To16(); ip != nil && ip.To4() == nil {
    34  			copy(pi.Addr[:], ip)
    35  		}
    36  		if cm.IfIndex > 0 {
    37  			pi.setIfindex(cm.IfIndex)
    38  		}
    39  	}
    40  	return m.Next(sizeofInet6Pktinfo)
    41  }
    42  
    43  func marshal2292NextHop(b []byte, cm *ControlMessage) []byte {
    44  	m := socket.ControlMessage(b)
    45  	m.MarshalHeader(iana.ProtocolIPv6, unix.IPV6_2292NEXTHOP, sizeofSockaddrInet6)
    46  	if cm != nil {
    47  		sa := (*sockaddrInet6)(unsafe.Pointer(&m.Data(sizeofSockaddrInet6)[0]))
    48  		sa.setSockaddr(cm.NextHop, cm.IfIndex)
    49  	}
    50  	return m.Next(sizeofSockaddrInet6)
    51  }