github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/ipv4/control_pktinfo.go (about) 1 // Copyright 2014 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 linux solaris 6 7 package ipv4 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 marshalPacketInfo(b []byte, cm *ControlMessage) []byte { 18 m := socket.ControlMessage(b) 19 m.MarshalHeader(iana.ProtocolIP, sysIP_PKTINFO, sizeofInetPktinfo) 20 if cm != nil { 21 pi := (*inetPktinfo)(unsafe.Pointer(&m.Data(sizeofInetPktinfo)[0])) 22 if ip := cm.Src.To4(); ip != nil { 23 copy(pi.Spec_dst[:], ip) 24 } 25 if cm.IfIndex > 0 { 26 pi.setIfindex(cm.IfIndex) 27 } 28 } 29 return m.Next(sizeofInetPktinfo) 30 } 31 32 func parsePacketInfo(cm *ControlMessage, b []byte) { 33 pi := (*inetPktinfo)(unsafe.Pointer(&b[0])) 34 cm.IfIndex = int(pi.Ifindex) 35 if len(cm.Dst) < net.IPv4len { 36 cm.Dst = make(net.IP, net.IPv4len) 37 } 38 copy(cm.Dst, pi.Addr[:]) 39 }