sys_darwin.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2014 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 ipv4
  5. import (
  6. "net"
  7. "syscall"
  8. "unsafe"
  9. )
  10. var (
  11. ctlOpts = [ctlMax]ctlOpt{
  12. ctlTTL: {sysIP_RECVTTL, 1, marshalTTL, parseTTL},
  13. ctlDst: {sysIP_RECVDSTADDR, net.IPv4len, marshalDst, parseDst},
  14. ctlInterface: {sysIP_RECVIF, syscall.SizeofSockaddrDatalink, marshalInterface, parseInterface},
  15. }
  16. sockOpts = [ssoMax]sockOpt{
  17. ssoTOS: {sysIP_TOS, ssoTypeInt},
  18. ssoTTL: {sysIP_TTL, ssoTypeInt},
  19. ssoMulticastTTL: {sysIP_MULTICAST_TTL, ssoTypeByte},
  20. ssoMulticastInterface: {sysIP_MULTICAST_IF, ssoTypeInterface},
  21. ssoMulticastLoopback: {sysIP_MULTICAST_LOOP, ssoTypeInt},
  22. ssoReceiveTTL: {sysIP_RECVTTL, ssoTypeInt},
  23. ssoReceiveDst: {sysIP_RECVDSTADDR, ssoTypeInt},
  24. ssoReceiveInterface: {sysIP_RECVIF, ssoTypeInt},
  25. ssoHeaderPrepend: {sysIP_HDRINCL, ssoTypeInt},
  26. ssoStripHeader: {sysIP_STRIPHDR, ssoTypeInt},
  27. ssoJoinGroup: {sysIP_ADD_MEMBERSHIP, ssoTypeIPMreq},
  28. ssoLeaveGroup: {sysIP_DROP_MEMBERSHIP, ssoTypeIPMreq},
  29. }
  30. )
  31. func init() {
  32. // Seems like kern.osreldate is veiled on latest OS X. We use
  33. // kern.osrelease instead.
  34. osver, err := syscall.Sysctl("kern.osrelease")
  35. if err != nil {
  36. return
  37. }
  38. var i int
  39. for i = range osver {
  40. if osver[i] == '.' {
  41. break
  42. }
  43. }
  44. // The IP_PKTINFO and protocol-independent multicast API were
  45. // introduced in OS X 10.7 (Darwin 11.0.0). But it looks like
  46. // those features require OS X 10.8 (Darwin 12.0.0) and above.
  47. // See http://support.apple.com/kb/HT1633.
  48. if i > 2 || i == 2 && osver[0] >= '1' && osver[1] >= '2' {
  49. ctlOpts[ctlPacketInfo].name = sysIP_PKTINFO
  50. ctlOpts[ctlPacketInfo].length = sizeofInetPktinfo
  51. ctlOpts[ctlPacketInfo].marshal = marshalPacketInfo
  52. ctlOpts[ctlPacketInfo].parse = parsePacketInfo
  53. sockOpts[ssoPacketInfo].name = sysIP_RECVPKTINFO
  54. sockOpts[ssoPacketInfo].typ = ssoTypeInt
  55. sockOpts[ssoMulticastInterface].typ = ssoTypeIPMreqn
  56. sockOpts[ssoJoinGroup].name = sysMCAST_JOIN_GROUP
  57. sockOpts[ssoJoinGroup].typ = ssoTypeGroupReq
  58. sockOpts[ssoLeaveGroup].name = sysMCAST_LEAVE_GROUP
  59. sockOpts[ssoLeaveGroup].typ = ssoTypeGroupReq
  60. sockOpts[ssoJoinSourceGroup].name = sysMCAST_JOIN_SOURCE_GROUP
  61. sockOpts[ssoJoinSourceGroup].typ = ssoTypeGroupSourceReq
  62. sockOpts[ssoLeaveSourceGroup].name = sysMCAST_LEAVE_SOURCE_GROUP
  63. sockOpts[ssoLeaveSourceGroup].typ = ssoTypeGroupSourceReq
  64. sockOpts[ssoBlockSourceGroup].name = sysMCAST_BLOCK_SOURCE
  65. sockOpts[ssoBlockSourceGroup].typ = ssoTypeGroupSourceReq
  66. sockOpts[ssoUnblockSourceGroup].name = sysMCAST_UNBLOCK_SOURCE
  67. sockOpts[ssoUnblockSourceGroup].typ = ssoTypeGroupSourceReq
  68. }
  69. }
  70. func (pi *inetPktinfo) setIfindex(i int) {
  71. pi.Ifindex = uint32(i)
  72. }
  73. func (gr *groupReq) setGroup(grp net.IP) {
  74. sa := (*sockaddrInet)(unsafe.Pointer(uintptr(unsafe.Pointer(gr)) + 4))
  75. sa.Len = sizeofSockaddrInet
  76. sa.Family = syscall.AF_INET
  77. copy(sa.Addr[:], grp)
  78. }
  79. func (gsr *groupSourceReq) setSourceGroup(grp, src net.IP) {
  80. sa := (*sockaddrInet)(unsafe.Pointer(uintptr(unsafe.Pointer(gsr)) + 4))
  81. sa.Len = sizeofSockaddrInet
  82. sa.Family = syscall.AF_INET
  83. copy(sa.Addr[:], grp)
  84. sa = (*sockaddrInet)(unsafe.Pointer(uintptr(unsafe.Pointer(gsr)) + 132))
  85. sa.Len = sizeofSockaddrInet
  86. sa.Family = syscall.AF_INET
  87. copy(sa.Addr[:], src)
  88. }