control_rfc3542_unix.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 aix darwin dragonfly freebsd linux netbsd openbsd solaris
  5. package ipv6
  6. import (
  7. "net"
  8. "unsafe"
  9. "golang.org/x/net/internal/iana"
  10. "golang.org/x/net/internal/socket"
  11. )
  12. func marshalTrafficClass(b []byte, cm *ControlMessage) []byte {
  13. m := socket.ControlMessage(b)
  14. m.MarshalHeader(iana.ProtocolIPv6, sysIPV6_TCLASS, 4)
  15. if cm != nil {
  16. socket.NativeEndian.PutUint32(m.Data(4), uint32(cm.TrafficClass))
  17. }
  18. return m.Next(4)
  19. }
  20. func parseTrafficClass(cm *ControlMessage, b []byte) {
  21. cm.TrafficClass = int(socket.NativeEndian.Uint32(b[:4]))
  22. }
  23. func marshalHopLimit(b []byte, cm *ControlMessage) []byte {
  24. m := socket.ControlMessage(b)
  25. m.MarshalHeader(iana.ProtocolIPv6, sysIPV6_HOPLIMIT, 4)
  26. if cm != nil {
  27. socket.NativeEndian.PutUint32(m.Data(4), uint32(cm.HopLimit))
  28. }
  29. return m.Next(4)
  30. }
  31. func parseHopLimit(cm *ControlMessage, b []byte) {
  32. cm.HopLimit = int(socket.NativeEndian.Uint32(b[:4]))
  33. }
  34. func marshalPacketInfo(b []byte, cm *ControlMessage) []byte {
  35. m := socket.ControlMessage(b)
  36. m.MarshalHeader(iana.ProtocolIPv6, sysIPV6_PKTINFO, sizeofInet6Pktinfo)
  37. if cm != nil {
  38. pi := (*inet6Pktinfo)(unsafe.Pointer(&m.Data(sizeofInet6Pktinfo)[0]))
  39. if ip := cm.Src.To16(); ip != nil && ip.To4() == nil {
  40. copy(pi.Addr[:], ip)
  41. }
  42. if cm.IfIndex > 0 {
  43. pi.setIfindex(cm.IfIndex)
  44. }
  45. }
  46. return m.Next(sizeofInet6Pktinfo)
  47. }
  48. func parsePacketInfo(cm *ControlMessage, b []byte) {
  49. pi := (*inet6Pktinfo)(unsafe.Pointer(&b[0]))
  50. if len(cm.Dst) < net.IPv6len {
  51. cm.Dst = make(net.IP, net.IPv6len)
  52. }
  53. copy(cm.Dst, pi.Addr[:])
  54. cm.IfIndex = int(pi.Ifindex)
  55. }
  56. func marshalNextHop(b []byte, cm *ControlMessage) []byte {
  57. m := socket.ControlMessage(b)
  58. m.MarshalHeader(iana.ProtocolIPv6, sysIPV6_NEXTHOP, sizeofSockaddrInet6)
  59. if cm != nil {
  60. sa := (*sockaddrInet6)(unsafe.Pointer(&m.Data(sizeofSockaddrInet6)[0]))
  61. sa.setSockaddr(cm.NextHop, cm.IfIndex)
  62. }
  63. return m.Next(sizeofSockaddrInet6)
  64. }
  65. func parseNextHop(cm *ControlMessage, b []byte) {
  66. }
  67. func marshalPathMTU(b []byte, cm *ControlMessage) []byte {
  68. m := socket.ControlMessage(b)
  69. m.MarshalHeader(iana.ProtocolIPv6, sysIPV6_PATHMTU, sizeofIPv6Mtuinfo)
  70. return m.Next(sizeofIPv6Mtuinfo)
  71. }
  72. func parsePathMTU(cm *ControlMessage, b []byte) {
  73. mi := (*ipv6Mtuinfo)(unsafe.Pointer(&b[0]))
  74. if len(cm.Dst) < net.IPv6len {
  75. cm.Dst = make(net.IP, net.IPv6len)
  76. }
  77. copy(cm.Dst, mi.Addr.Addr[:])
  78. cm.IfIndex = int(mi.Addr.Scope_id)
  79. cm.MTU = int(mi.Mtu)
  80. }