sys_darwin.go 5.1 KB

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