github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/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  // +build darwin dragonfly freebsd linux netbsd openbsd
     6  
     7  package ipv6
     8  
     9  import (
    10  	"syscall"
    11  	"unsafe"
    12  
    13  	"golang.org/x/net/internal/iana"
    14  )
    15  
    16  func marshalTrafficClass(b []byte, cm *ControlMessage) []byte {
    17  	m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
    18  	m.Level = iana.ProtocolIPv6
    19  	m.Type = sysIPV6_TCLASS
    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.TrafficClass)
    25  	}
    26  	return b[syscall.CmsgSpace(4):]
    27  }
    28  
    29  func parseTrafficClass(cm *ControlMessage, b []byte) {
    30  	// TODO(mikio): fix potential misaligned memory access
    31  	cm.TrafficClass = int(*(*int32)(unsafe.Pointer(&b[:4][0])))
    32  }
    33  
    34  func marshalHopLimit(b []byte, cm *ControlMessage) []byte {
    35  	m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
    36  	m.Level = iana.ProtocolIPv6
    37  	m.Type = sysIPV6_HOPLIMIT
    38  	m.SetLen(syscall.CmsgLen(4))
    39  	if cm != nil {
    40  		data := b[syscall.CmsgLen(0):]
    41  		// TODO(mikio): fix potential misaligned memory access
    42  		*(*int32)(unsafe.Pointer(&data[:4][0])) = int32(cm.HopLimit)
    43  	}
    44  	return b[syscall.CmsgSpace(4):]
    45  }
    46  
    47  func parseHopLimit(cm *ControlMessage, b []byte) {
    48  	// TODO(mikio): fix potential misaligned memory access
    49  	cm.HopLimit = int(*(*int32)(unsafe.Pointer(&b[:4][0])))
    50  }
    51  
    52  func marshalPacketInfo(b []byte, cm *ControlMessage) []byte {
    53  	m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
    54  	m.Level = iana.ProtocolIPv6
    55  	m.Type = sysIPV6_PKTINFO
    56  	m.SetLen(syscall.CmsgLen(sysSizeofInet6Pktinfo))
    57  	if cm != nil {
    58  		pi := (*sysInet6Pktinfo)(unsafe.Pointer(&b[syscall.CmsgLen(0)]))
    59  		if ip := cm.Src.To16(); ip != nil && ip.To4() == nil {
    60  			copy(pi.Addr[:], ip)
    61  		}
    62  		if cm.IfIndex > 0 {
    63  			pi.setIfindex(cm.IfIndex)
    64  		}
    65  	}
    66  	return b[syscall.CmsgSpace(sysSizeofInet6Pktinfo):]
    67  }
    68  
    69  func parsePacketInfo(cm *ControlMessage, b []byte) {
    70  	pi := (*sysInet6Pktinfo)(unsafe.Pointer(&b[0]))
    71  	cm.Dst = pi.Addr[:]
    72  	cm.IfIndex = int(pi.Ifindex)
    73  }
    74  
    75  func marshalNextHop(b []byte, cm *ControlMessage) []byte {
    76  	m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
    77  	m.Level = iana.ProtocolIPv6
    78  	m.Type = sysIPV6_NEXTHOP
    79  	m.SetLen(syscall.CmsgLen(sysSizeofSockaddrInet6))
    80  	if cm != nil {
    81  		sa := (*sysSockaddrInet6)(unsafe.Pointer(&b[syscall.CmsgLen(0)]))
    82  		sa.setSockaddr(cm.NextHop, cm.IfIndex)
    83  	}
    84  	return b[syscall.CmsgSpace(sysSizeofSockaddrInet6):]
    85  }
    86  
    87  func parseNextHop(cm *ControlMessage, b []byte) {
    88  }
    89  
    90  func marshalPathMTU(b []byte, cm *ControlMessage) []byte {
    91  	m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
    92  	m.Level = iana.ProtocolIPv6
    93  	m.Type = sysIPV6_PATHMTU
    94  	m.SetLen(syscall.CmsgLen(sysSizeofIPv6Mtuinfo))
    95  	return b[syscall.CmsgSpace(sysSizeofIPv6Mtuinfo):]
    96  }
    97  
    98  func parsePathMTU(cm *ControlMessage, b []byte) {
    99  	mi := (*sysIPv6Mtuinfo)(unsafe.Pointer(&b[0]))
   100  	cm.Dst = mi.Addr.Addr[:]
   101  	cm.IfIndex = int(mi.Addr.Scope_id)
   102  	cm.MTU = int(mi.Mtu)
   103  }