control_rfc2292_unix.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2013 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
  5. package ipv6
  6. import (
  7. "syscall"
  8. "unsafe"
  9. )
  10. func marshal2292HopLimit(b []byte, cm *ControlMessage) []byte {
  11. m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
  12. m.Level = ianaProtocolIPv6
  13. m.Type = sysIPV6_2292HOPLIMIT
  14. m.SetLen(syscall.CmsgLen(4))
  15. if cm != nil {
  16. data := b[syscall.CmsgLen(0):]
  17. *(*int32)(unsafe.Pointer(&data[:4][0])) = int32(cm.HopLimit)
  18. }
  19. return b[syscall.CmsgSpace(4):]
  20. }
  21. func marshal2292PacketInfo(b []byte, cm *ControlMessage) []byte {
  22. m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
  23. m.Level = ianaProtocolIPv6
  24. m.Type = sysIPV6_2292PKTINFO
  25. m.SetLen(syscall.CmsgLen(sysSizeofInet6Pktinfo))
  26. if cm != nil {
  27. pi := (*sysInet6Pktinfo)(unsafe.Pointer(&b[syscall.CmsgLen(0)]))
  28. if ip := cm.Src.To16(); ip != nil && ip.To4() == nil {
  29. copy(pi.Addr[:], ip)
  30. }
  31. if cm.IfIndex > 0 {
  32. pi.setIfindex(cm.IfIndex)
  33. }
  34. }
  35. return b[syscall.CmsgSpace(sysSizeofInet6Pktinfo):]
  36. }
  37. func marshal2292NextHop(b []byte, cm *ControlMessage) []byte {
  38. m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
  39. m.Level = ianaProtocolIPv6
  40. m.Type = sysIPV6_2292NEXTHOP
  41. m.SetLen(syscall.CmsgLen(sysSizeofSockaddrInet6))
  42. if cm != nil {
  43. sa := (*sysSockaddrInet6)(unsafe.Pointer(&b[syscall.CmsgLen(0)]))
  44. sa.setSockaddr(cm.NextHop, cm.IfIndex)
  45. }
  46. return b[syscall.CmsgSpace(sysSizeofSockaddrInet6):]
  47. }