control_pktinfo.go 701 B

123456789101112131415161718192021222324252627282930
  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. // +build darwin linux
  5. package ipv4
  6. import (
  7. "syscall"
  8. "unsafe"
  9. )
  10. func (cm *ControlMessage) marshalPacketInfo() (oob []byte) {
  11. if l := cm.oobLen(); l > 0 {
  12. oob = make([]byte, l)
  13. m := (*syscall.Cmsghdr)(unsafe.Pointer(&oob[0]))
  14. m.Level = ianaProtocolIP
  15. m.Type = sysIP_PKTINFO
  16. m.SetLen(syscall.CmsgLen(sysSizeofInetPktinfo))
  17. pi := (*sysInetPktinfo)(unsafe.Pointer(&oob[syscall.CmsgLen(0)]))
  18. if ip := cm.Src.To4(); ip != nil {
  19. copy(pi.Addr[:], ip)
  20. }
  21. if cm.IfIndex != 0 {
  22. pi.setIfindex(cm.IfIndex)
  23. }
  24. }
  25. return
  26. }