sys_darwin.go 4.5 KB

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