control_rfc2292_unix.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. nativeEndian.PutUint32(data[:4], uint32(cm.HopLimit))
  19. }
  20. return b[syscall.CmsgSpace(4):]
  21. }
  22. func marshal2292PacketInfo(b []byte, cm *ControlMessage) []byte {
  23. m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
  24. m.Level = iana.ProtocolIPv6
  25. m.Type = sysIPV6_2292PKTINFO
  26. m.SetLen(syscall.CmsgLen(sizeofInet6Pktinfo))
  27. if cm != nil {
  28. pi := (*inet6Pktinfo)(unsafe.Pointer(&b[syscall.CmsgLen(0)]))
  29. if ip := cm.Src.To16(); ip != nil && ip.To4() == nil {
  30. copy(pi.Addr[:], ip)
  31. }
  32. if cm.IfIndex > 0 {
  33. pi.setIfindex(cm.IfIndex)
  34. }
  35. }
  36. return b[syscall.CmsgSpace(sizeofInet6Pktinfo):]
  37. }
  38. func marshal2292NextHop(b []byte, cm *ControlMessage) []byte {
  39. m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
  40. m.Level = iana.ProtocolIPv6
  41. m.Type = sysIPV6_2292NEXTHOP
  42. m.SetLen(syscall.CmsgLen(sizeofSockaddrInet6))
  43. if cm != nil {
  44. sa := (*sockaddrInet6)(unsafe.Pointer(&b[syscall.CmsgLen(0)]))
  45. sa.setSockaddr(cm.NextHop, cm.IfIndex)
  46. }
  47. return b[syscall.CmsgSpace(sizeofSockaddrInet6):]
  48. }