github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/ipv4/control_bsd.go (about) 1 // Copyright 2012 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 aix || darwin || dragonfly || freebsd || netbsd || openbsd 6 // +build aix darwin dragonfly freebsd netbsd openbsd 7 8 package ipv4 9 10 import ( 11 "net" 12 "syscall" 13 "unsafe" 14 15 "github.com/hxx258456/ccgo/net/internal/iana" 16 "github.com/hxx258456/ccgo/net/internal/socket" 17 18 "golang.org/x/sys/unix" 19 ) 20 21 func marshalDst(b []byte, cm *ControlMessage) []byte { 22 m := socket.ControlMessage(b) 23 m.MarshalHeader(iana.ProtocolIP, unix.IP_RECVDSTADDR, net.IPv4len) 24 return m.Next(net.IPv4len) 25 } 26 27 func parseDst(cm *ControlMessage, b []byte) { 28 if len(cm.Dst) < net.IPv4len { 29 cm.Dst = make(net.IP, net.IPv4len) 30 } 31 copy(cm.Dst, b[:net.IPv4len]) 32 } 33 34 func marshalInterface(b []byte, cm *ControlMessage) []byte { 35 m := socket.ControlMessage(b) 36 m.MarshalHeader(iana.ProtocolIP, sockoptReceiveInterface, syscall.SizeofSockaddrDatalink) 37 return m.Next(syscall.SizeofSockaddrDatalink) 38 } 39 40 func parseInterface(cm *ControlMessage, b []byte) { 41 var sadl syscall.SockaddrDatalink 42 copy((*[unsafe.Sizeof(sadl)]byte)(unsafe.Pointer(&sadl))[:], b) 43 cm.IfIndex = int(sadl.Index) 44 }