2017-06-04 14:07:03 +02:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-06-04 14:47:24 +02:00
|
|
|
// +build darwin linux solaris
|
2017-06-04 14:07:03 +02:00
|
|
|
|
|
|
|
package ipv4
|
|
|
|
|
|
|
|
import (
|
2017-06-04 14:47:24 +02:00
|
|
|
"net"
|
2017-06-04 14:07:03 +02:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"golang.org/x/net/internal/iana"
|
2017-06-04 14:47:24 +02:00
|
|
|
"golang.org/x/net/internal/socket"
|
2017-06-04 14:07:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func marshalPacketInfo(b []byte, cm *ControlMessage) []byte {
|
2017-06-04 14:47:24 +02:00
|
|
|
m := socket.ControlMessage(b)
|
|
|
|
m.MarshalHeader(iana.ProtocolIP, sysIP_PKTINFO, sizeofInetPktinfo)
|
2017-06-04 14:07:03 +02:00
|
|
|
if cm != nil {
|
2017-06-04 14:47:24 +02:00
|
|
|
pi := (*inetPktinfo)(unsafe.Pointer(&m.Data(sizeofInetPktinfo)[0]))
|
2017-06-04 14:07:03 +02:00
|
|
|
if ip := cm.Src.To4(); ip != nil {
|
|
|
|
copy(pi.Spec_dst[:], ip)
|
|
|
|
}
|
|
|
|
if cm.IfIndex > 0 {
|
|
|
|
pi.setIfindex(cm.IfIndex)
|
|
|
|
}
|
|
|
|
}
|
2017-06-04 14:47:24 +02:00
|
|
|
return m.Next(sizeofInetPktinfo)
|
2017-06-04 14:07:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func parsePacketInfo(cm *ControlMessage, b []byte) {
|
2017-06-04 14:47:24 +02:00
|
|
|
pi := (*inetPktinfo)(unsafe.Pointer(&b[0]))
|
2017-06-04 14:07:03 +02:00
|
|
|
cm.IfIndex = int(pi.Ifindex)
|
2017-06-04 14:47:24 +02:00
|
|
|
if len(cm.Dst) < net.IPv4len {
|
|
|
|
cm.Dst = make(net.IP, net.IPv4len)
|
|
|
|
}
|
|
|
|
copy(cm.Dst, pi.Addr[:])
|
2017-06-04 14:07:03 +02:00
|
|
|
}
|