github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/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 //go:build darwin || linux || solaris 6 // +build darwin linux solaris 7 8 package ipv4 9 10 import ( 11 "net" 12 "unsafe" 13 14 "github.com/hxx258456/ccgo/net/internal/iana" 15 "github.com/hxx258456/ccgo/net/internal/socket" 16 17 "golang.org/x/sys/unix" 18 ) 19 20 func marshalPacketInfo(b []byte, cm *ControlMessage) []byte { 21 m := socket.ControlMessage(b) 22 m.MarshalHeader(iana.ProtocolIP, unix.IP_PKTINFO, sizeofInetPktinfo) 23 if cm != nil { 24 pi := (*inetPktinfo)(unsafe.Pointer(&m.Data(sizeofInetPktinfo)[0])) 25 if ip := cm.Src.To4(); ip != nil { 26 copy(pi.Spec_dst[:], ip) 27 } 28 if cm.IfIndex > 0 { 29 pi.setIfindex(cm.IfIndex) 30 } 31 } 32 return m.Next(sizeofInetPktinfo) 33 } 34 35 func parsePacketInfo(cm *ControlMessage, b []byte) { 36 pi := (*inetPktinfo)(unsafe.Pointer(&b[0])) 37 cm.IfIndex = int(pi.Ifindex) 38 if len(cm.Dst) < net.IPv4len { 39 cm.Dst = make(net.IP, net.IPv4len) 40 } 41 copy(cm.Dst, pi.Addr[:]) 42 }