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