control_rfc2292_unix.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. "golang.org/x/net/internal/iana"
  10. )
  11. func marshal2292HopLimit(b []byte, cm *ControlMessage) []byte {
  12. m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
  13. m.Level = iana.ProtocolIPv6
  14. m.Type = sysIPV6_2292HOPLIMIT
  15. m.SetLen(syscall.CmsgLen(4))
  16. if cm != nil {
  17. data := b[syscall.CmsgLen(0):]
  18. // TODO(mikio): fix potential misaligned memory access
  19. *(*int32)(unsafe.Pointer(&data[:4][0])) = int32(cm.HopLimit)
  20. }
  21. return b[syscall.CmsgSpace(4):]
  22. }
  23. func marshal2292PacketInfo(b []byte, cm *ControlMessage) []byte {
  24. m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
  25. m.Level = iana.ProtocolIPv6
  26. m.Type = sysIPV6_2292PKTINFO
  27. m.SetLen(syscall.CmsgLen(sysSizeofInet6Pktinfo))
  28. if cm != nil {
  29. pi := (*sysInet6Pktinfo)(unsafe.Pointer(&b[syscall.CmsgLen(0)]))
  30. if ip := cm.Src.To16(); ip != nil && ip.To4() == nil {
  31. copy(pi.Addr[:], ip)
  32. }
  33. if cm.IfIndex > 0 {
  34. pi.setIfindex(cm.IfIndex)
  35. }
  36. }
  37. return b[syscall.CmsgSpace(sysSizeofInet6Pktinfo):]
  38. }
  39. func marshal2292NextHop(b []byte, cm *ControlMessage) []byte {
  40. m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
  41. m.Level = iana.ProtocolIPv6
  42. m.Type = sysIPV6_2292NEXTHOP
  43. m.SetLen(syscall.CmsgLen(sysSizeofSockaddrInet6))
  44. if cm != nil {
  45. sa := (*sysSockaddrInet6)(unsafe.Pointer(&b[syscall.CmsgLen(0)]))
  46. sa.setSockaddr(cm.NextHop, cm.IfIndex)
  47. }
  48. return b[syscall.CmsgSpace(sysSizeofSockaddrInet6):]
  49. }