sys_darwin.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. package ipv6
  5. import (
  6. "net"
  7. "syscall"
  8. "unsafe"
  9. "golang.org/x/net/internal/iana"
  10. )
  11. type sysSockoptLen int32
  12. var (
  13. ctlOpts = [ctlMax]ctlOpt{
  14. ctlHopLimit: {sysIPV6_2292HOPLIMIT, 4, marshal2292HopLimit, parseHopLimit},
  15. ctlPacketInfo: {sysIPV6_2292PKTINFO, sysSizeofInet6Pktinfo, marshal2292PacketInfo, parsePacketInfo},
  16. }
  17. sockOpts = [ssoMax]sockOpt{
  18. ssoHopLimit: {iana.ProtocolIPv6, sysIPV6_UNICAST_HOPS, ssoTypeInt},
  19. ssoMulticastInterface: {iana.ProtocolIPv6, sysIPV6_MULTICAST_IF, ssoTypeInterface},
  20. ssoMulticastHopLimit: {iana.ProtocolIPv6, sysIPV6_MULTICAST_HOPS, ssoTypeInt},
  21. ssoMulticastLoopback: {iana.ProtocolIPv6, sysIPV6_MULTICAST_LOOP, ssoTypeInt},
  22. ssoReceiveHopLimit: {iana.ProtocolIPv6, sysIPV6_2292HOPLIMIT, ssoTypeInt},
  23. ssoReceivePacketInfo: {iana.ProtocolIPv6, sysIPV6_2292PKTINFO, ssoTypeInt},
  24. ssoChecksum: {iana.ProtocolIPv6, sysIPV6_CHECKSUM, ssoTypeInt},
  25. ssoICMPFilter: {iana.ProtocolIPv6ICMP, sysICMP6_FILTER, ssoTypeICMPFilter},
  26. ssoJoinGroup: {iana.ProtocolIPv6, sysIPV6_JOIN_GROUP, ssoTypeIPMreq},
  27. ssoLeaveGroup: {iana.ProtocolIPv6, sysIPV6_LEAVE_GROUP, ssoTypeIPMreq},
  28. }
  29. )
  30. func init() {
  31. // Seems like kern.osreldate is veiled on latest OS X. We use
  32. // kern.osrelease instead.
  33. osver, err := syscall.Sysctl("kern.osrelease")
  34. if err != nil {
  35. return
  36. }
  37. var i int
  38. for i = range osver {
  39. if osver[i] == '.' {
  40. break
  41. }
  42. }
  43. // The IP_PKTINFO and protocol-independent multicast API were
  44. // introduced in OS X 10.7 (Darwin 11.0.0). But it looks like
  45. // those features require OS X 10.8 (Darwin 12.0.0) and above.
  46. // See http://support.apple.com/kb/HT1633.
  47. if i > 2 || i == 2 && osver[0] >= '1' && osver[1] >= '2' {
  48. ctlOpts[ctlTrafficClass].name = sysIPV6_TCLASS
  49. ctlOpts[ctlTrafficClass].length = 4
  50. ctlOpts[ctlTrafficClass].marshal = marshalTrafficClass
  51. ctlOpts[ctlTrafficClass].parse = parseTrafficClass
  52. ctlOpts[ctlHopLimit].name = sysIPV6_HOPLIMIT
  53. ctlOpts[ctlHopLimit].marshal = marshalHopLimit
  54. ctlOpts[ctlPacketInfo].name = sysIPV6_PKTINFO
  55. ctlOpts[ctlPacketInfo].marshal = marshalPacketInfo
  56. ctlOpts[ctlNextHop].name = sysIPV6_NEXTHOP
  57. ctlOpts[ctlNextHop].length = sysSizeofSockaddrInet6
  58. ctlOpts[ctlNextHop].marshal = marshalNextHop
  59. ctlOpts[ctlNextHop].parse = parseNextHop
  60. ctlOpts[ctlPathMTU].name = sysIPV6_PATHMTU
  61. ctlOpts[ctlPathMTU].length = sysSizeofIPv6Mtuinfo
  62. ctlOpts[ctlPathMTU].marshal = marshalPathMTU
  63. ctlOpts[ctlPathMTU].parse = parsePathMTU
  64. sockOpts[ssoTrafficClass].level = iana.ProtocolIPv6
  65. sockOpts[ssoTrafficClass].name = sysIPV6_TCLASS
  66. sockOpts[ssoTrafficClass].typ = ssoTypeInt
  67. sockOpts[ssoReceiveTrafficClass].level = iana.ProtocolIPv6
  68. sockOpts[ssoReceiveTrafficClass].name = sysIPV6_RECVTCLASS
  69. sockOpts[ssoReceiveTrafficClass].typ = ssoTypeInt
  70. sockOpts[ssoReceiveHopLimit].name = sysIPV6_RECVHOPLIMIT
  71. sockOpts[ssoReceivePacketInfo].name = sysIPV6_RECVPKTINFO
  72. sockOpts[ssoReceivePathMTU].level = iana.ProtocolIPv6
  73. sockOpts[ssoReceivePathMTU].name = sysIPV6_RECVPATHMTU
  74. sockOpts[ssoReceivePathMTU].typ = ssoTypeInt
  75. sockOpts[ssoPathMTU].level = iana.ProtocolIPv6
  76. sockOpts[ssoPathMTU].name = sysIPV6_PATHMTU
  77. sockOpts[ssoPathMTU].typ = ssoTypeMTUInfo
  78. sockOpts[ssoJoinGroup].name = sysMCAST_JOIN_GROUP
  79. sockOpts[ssoJoinGroup].typ = ssoTypeGroupReq
  80. sockOpts[ssoLeaveGroup].name = sysMCAST_LEAVE_GROUP
  81. sockOpts[ssoLeaveGroup].typ = ssoTypeGroupReq
  82. sockOpts[ssoJoinSourceGroup].level = iana.ProtocolIPv6
  83. sockOpts[ssoJoinSourceGroup].name = sysMCAST_JOIN_SOURCE_GROUP
  84. sockOpts[ssoJoinSourceGroup].typ = ssoTypeGroupSourceReq
  85. sockOpts[ssoLeaveSourceGroup].level = iana.ProtocolIPv6
  86. sockOpts[ssoLeaveSourceGroup].name = sysMCAST_LEAVE_SOURCE_GROUP
  87. sockOpts[ssoLeaveSourceGroup].typ = ssoTypeGroupSourceReq
  88. sockOpts[ssoBlockSourceGroup].level = iana.ProtocolIPv6
  89. sockOpts[ssoBlockSourceGroup].name = sysMCAST_BLOCK_SOURCE
  90. sockOpts[ssoBlockSourceGroup].typ = ssoTypeGroupSourceReq
  91. sockOpts[ssoUnblockSourceGroup].level = iana.ProtocolIPv6
  92. sockOpts[ssoUnblockSourceGroup].name = sysMCAST_UNBLOCK_SOURCE
  93. sockOpts[ssoUnblockSourceGroup].typ = ssoTypeGroupSourceReq
  94. }
  95. }
  96. func (sa *sysSockaddrInet6) setSockaddr(ip net.IP, i int) {
  97. sa.Len = sysSizeofSockaddrInet6
  98. sa.Family = syscall.AF_INET6
  99. copy(sa.Addr[:], ip)
  100. sa.Scope_id = uint32(i)
  101. }
  102. func (pi *sysInet6Pktinfo) setIfindex(i int) {
  103. pi.Ifindex = uint32(i)
  104. }
  105. func (mreq *sysIPv6Mreq) setIfindex(i int) {
  106. mreq.Interface = uint32(i)
  107. }
  108. func (gr *sysGroupReq) setGroup(grp net.IP) {
  109. sa := (*sysSockaddrInet6)(unsafe.Pointer(&gr.Pad_cgo_0[0]))
  110. sa.Len = sysSizeofSockaddrInet6
  111. sa.Family = syscall.AF_INET6
  112. copy(sa.Addr[:], grp)
  113. }
  114. func (gsr *sysGroupSourceReq) setSourceGroup(grp, src net.IP) {
  115. sa := (*sysSockaddrInet6)(unsafe.Pointer(&gsr.Pad_cgo_0[0]))
  116. sa.Len = sysSizeofSockaddrInet6
  117. sa.Family = syscall.AF_INET6
  118. copy(sa.Addr[:], grp)
  119. sa = (*sysSockaddrInet6)(unsafe.Pointer(&gsr.Pad_cgo_1[0]))
  120. sa.Len = sysSizeofSockaddrInet6
  121. sa.Family = syscall.AF_INET6
  122. copy(sa.Addr[:], src)
  123. }