...
1
2
3
4
5
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 }
42
View as plain text