sockopt_posix.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 darwin dragonfly freebsd linux netbsd openbsd solaris windows
  5. package ipv6
  6. import (
  7. "net"
  8. "os"
  9. "unsafe"
  10. )
  11. func getInt(s uintptr, opt *sockOpt) (int, error) {
  12. if opt.name < 1 || opt.typ != ssoTypeInt {
  13. return 0, errOpNoSupport
  14. }
  15. var i int32
  16. l := uint32(4)
  17. if err := getsockopt(s, opt.level, opt.name, unsafe.Pointer(&i), &l); err != nil {
  18. return 0, os.NewSyscallError("getsockopt", err)
  19. }
  20. return int(i), nil
  21. }
  22. func setInt(s uintptr, opt *sockOpt, v int) error {
  23. if opt.name < 1 || opt.typ != ssoTypeInt {
  24. return errOpNoSupport
  25. }
  26. i := int32(v)
  27. return os.NewSyscallError("setsockopt", setsockopt(s, opt.level, opt.name, unsafe.Pointer(&i), 4))
  28. }
  29. func getInterface(s uintptr, opt *sockOpt) (*net.Interface, error) {
  30. if opt.name < 1 || opt.typ != ssoTypeInterface {
  31. return nil, errOpNoSupport
  32. }
  33. var i int32
  34. l := uint32(4)
  35. if err := getsockopt(s, opt.level, opt.name, unsafe.Pointer(&i), &l); err != nil {
  36. return nil, os.NewSyscallError("getsockopt", err)
  37. }
  38. if i == 0 {
  39. return nil, nil
  40. }
  41. ifi, err := net.InterfaceByIndex(int(i))
  42. if err != nil {
  43. return nil, err
  44. }
  45. return ifi, nil
  46. }
  47. func setInterface(s uintptr, opt *sockOpt, ifi *net.Interface) error {
  48. if opt.name < 1 || opt.typ != ssoTypeInterface {
  49. return errOpNoSupport
  50. }
  51. var i int32
  52. if ifi != nil {
  53. i = int32(ifi.Index)
  54. }
  55. return os.NewSyscallError("setsockopt", setsockopt(s, opt.level, opt.name, unsafe.Pointer(&i), 4))
  56. }
  57. func getICMPFilter(s uintptr, opt *sockOpt) (*ICMPFilter, error) {
  58. if opt.name < 1 || opt.typ != ssoTypeICMPFilter {
  59. return nil, errOpNoSupport
  60. }
  61. var f ICMPFilter
  62. l := uint32(sizeofICMPv6Filter)
  63. if err := getsockopt(s, opt.level, opt.name, unsafe.Pointer(&f.icmpv6Filter), &l); err != nil {
  64. return nil, os.NewSyscallError("getsockopt", err)
  65. }
  66. return &f, nil
  67. }
  68. func setICMPFilter(s uintptr, opt *sockOpt, f *ICMPFilter) error {
  69. if opt.name < 1 || opt.typ != ssoTypeICMPFilter {
  70. return errOpNoSupport
  71. }
  72. return os.NewSyscallError("setsockopt", setsockopt(s, opt.level, opt.name, unsafe.Pointer(&f.icmpv6Filter), sizeofICMPv6Filter))
  73. }
  74. func getMTUInfo(s uintptr, opt *sockOpt) (*net.Interface, int, error) {
  75. if opt.name < 1 || opt.typ != ssoTypeMTUInfo {
  76. return nil, 0, errOpNoSupport
  77. }
  78. var mi ipv6Mtuinfo
  79. l := uint32(sizeofIPv6Mtuinfo)
  80. if err := getsockopt(s, opt.level, opt.name, unsafe.Pointer(&mi), &l); err != nil {
  81. return nil, 0, os.NewSyscallError("getsockopt", err)
  82. }
  83. if mi.Addr.Scope_id == 0 {
  84. return nil, int(mi.Mtu), nil
  85. }
  86. ifi, err := net.InterfaceByIndex(int(mi.Addr.Scope_id))
  87. if err != nil {
  88. return nil, 0, err
  89. }
  90. return ifi, int(mi.Mtu), nil
  91. }
  92. func setGroup(s uintptr, opt *sockOpt, ifi *net.Interface, grp net.IP) error {
  93. if opt.name < 1 {
  94. return errOpNoSupport
  95. }
  96. switch opt.typ {
  97. case ssoTypeIPMreq:
  98. return setsockoptIPMreq(s, opt, ifi, grp)
  99. case ssoTypeGroupReq:
  100. return setsockoptGroupReq(s, opt, ifi, grp)
  101. default:
  102. return errOpNoSupport
  103. }
  104. }
  105. func setSourceGroup(s uintptr, opt *sockOpt, ifi *net.Interface, grp, src net.IP) error {
  106. if opt.name < 1 || opt.typ != ssoTypeGroupSourceReq {
  107. return errOpNoSupport
  108. }
  109. return setsockoptGroupSourceReq(s, opt, ifi, grp, src)
  110. }