github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/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  // +build darwin
     6  
     7  package ipv6
     8  
     9  import (
    10  	"syscall"
    11  	"unsafe"
    12  
    13  	"golang.org/x/net/internal/iana"
    14  )
    15  
    16  func marshal2292HopLimit(b []byte, cm *ControlMessage) []byte {
    17  	m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
    18  	m.Level = iana.ProtocolIPv6
    19  	m.Type = sysIPV6_2292HOPLIMIT
    20  	m.SetLen(syscall.CmsgLen(4))
    21  	if cm != nil {
    22  		data := b[syscall.CmsgLen(0):]
    23  		// TODO(mikio): fix potential misaligned memory access
    24  		*(*int32)(unsafe.Pointer(&data[:4][0])) = int32(cm.HopLimit)
    25  	}
    26  	return b[syscall.CmsgSpace(4):]
    27  }
    28  
    29  func marshal2292PacketInfo(b []byte, cm *ControlMessage) []byte {
    30  	m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
    31  	m.Level = iana.ProtocolIPv6
    32  	m.Type = sysIPV6_2292PKTINFO
    33  	m.SetLen(syscall.CmsgLen(sysSizeofInet6Pktinfo))
    34  	if cm != nil {
    35  		pi := (*sysInet6Pktinfo)(unsafe.Pointer(&b[syscall.CmsgLen(0)]))
    36  		if ip := cm.Src.To16(); ip != nil && ip.To4() == nil {
    37  			copy(pi.Addr[:], ip)
    38  		}
    39  		if cm.IfIndex > 0 {
    40  			pi.setIfindex(cm.IfIndex)
    41  		}
    42  	}
    43  	return b[syscall.CmsgSpace(sysSizeofInet6Pktinfo):]
    44  }
    45  
    46  func marshal2292NextHop(b []byte, cm *ControlMessage) []byte {
    47  	m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
    48  	m.Level = iana.ProtocolIPv6
    49  	m.Type = sysIPV6_2292NEXTHOP
    50  	m.SetLen(syscall.CmsgLen(sysSizeofSockaddrInet6))
    51  	if cm != nil {
    52  		sa := (*sysSockaddrInet6)(unsafe.Pointer(&b[syscall.CmsgLen(0)]))
    53  		sa.setSockaddr(cm.NextHop, cm.IfIndex)
    54  	}
    55  	return b[syscall.CmsgSpace(sysSizeofSockaddrInet6):]
    56  }