github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/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 aix darwin dragonfly freebsd linux netbsd openbsd solaris 6 7 package ipv6 8 9 import ( 10 "net" 11 "unsafe" 12 13 "github.com/Andyfoo/golang/x/net/internal/iana" 14 "github.com/Andyfoo/golang/x/net/internal/socket" 15 ) 16 17 func marshalTrafficClass(b []byte, cm *ControlMessage) []byte { 18 m := socket.ControlMessage(b) 19 m.MarshalHeader(iana.ProtocolIPv6, sysIPV6_TCLASS, 4) 20 if cm != nil { 21 socket.NativeEndian.PutUint32(m.Data(4), uint32(cm.TrafficClass)) 22 } 23 return m.Next(4) 24 } 25 26 func parseTrafficClass(cm *ControlMessage, b []byte) { 27 cm.TrafficClass = int(socket.NativeEndian.Uint32(b[:4])) 28 } 29 30 func marshalHopLimit(b []byte, cm *ControlMessage) []byte { 31 m := socket.ControlMessage(b) 32 m.MarshalHeader(iana.ProtocolIPv6, sysIPV6_HOPLIMIT, 4) 33 if cm != nil { 34 socket.NativeEndian.PutUint32(m.Data(4), uint32(cm.HopLimit)) 35 } 36 return m.Next(4) 37 } 38 39 func parseHopLimit(cm *ControlMessage, b []byte) { 40 cm.HopLimit = int(socket.NativeEndian.Uint32(b[:4])) 41 } 42 43 func marshalPacketInfo(b []byte, cm *ControlMessage) []byte { 44 m := socket.ControlMessage(b) 45 m.MarshalHeader(iana.ProtocolIPv6, sysIPV6_PKTINFO, sizeofInet6Pktinfo) 46 if cm != nil { 47 pi := (*inet6Pktinfo)(unsafe.Pointer(&m.Data(sizeofInet6Pktinfo)[0])) 48 if ip := cm.Src.To16(); ip != nil && ip.To4() == nil { 49 copy(pi.Addr[:], ip) 50 } 51 if cm.IfIndex > 0 { 52 pi.setIfindex(cm.IfIndex) 53 } 54 } 55 return m.Next(sizeofInet6Pktinfo) 56 } 57 58 func parsePacketInfo(cm *ControlMessage, b []byte) { 59 pi := (*inet6Pktinfo)(unsafe.Pointer(&b[0])) 60 if len(cm.Dst) < net.IPv6len { 61 cm.Dst = make(net.IP, net.IPv6len) 62 } 63 copy(cm.Dst, pi.Addr[:]) 64 cm.IfIndex = int(pi.Ifindex) 65 } 66 67 func marshalNextHop(b []byte, cm *ControlMessage) []byte { 68 m := socket.ControlMessage(b) 69 m.MarshalHeader(iana.ProtocolIPv6, sysIPV6_NEXTHOP, sizeofSockaddrInet6) 70 if cm != nil { 71 sa := (*sockaddrInet6)(unsafe.Pointer(&m.Data(sizeofSockaddrInet6)[0])) 72 sa.setSockaddr(cm.NextHop, cm.IfIndex) 73 } 74 return m.Next(sizeofSockaddrInet6) 75 } 76 77 func parseNextHop(cm *ControlMessage, b []byte) { 78 } 79 80 func marshalPathMTU(b []byte, cm *ControlMessage) []byte { 81 m := socket.ControlMessage(b) 82 m.MarshalHeader(iana.ProtocolIPv6, sysIPV6_PATHMTU, sizeofIPv6Mtuinfo) 83 return m.Next(sizeofIPv6Mtuinfo) 84 } 85 86 func parsePathMTU(cm *ControlMessage, b []byte) { 87 mi := (*ipv6Mtuinfo)(unsafe.Pointer(&b[0])) 88 if len(cm.Dst) < net.IPv6len { 89 cm.Dst = make(net.IP, net.IPv6len) 90 } 91 copy(cm.Dst, mi.Addr.Addr[:]) 92 cm.IfIndex = int(mi.Addr.Scope_id) 93 cm.MTU = int(mi.Mtu) 94 }