sockopt_unix.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2012 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
  5. package ipv4
  6. import (
  7. "net"
  8. "os"
  9. "unsafe"
  10. "golang.org/x/net/internal/iana"
  11. )
  12. func getInt(fd int, opt *sockOpt) (int, error) {
  13. if opt.name < 1 || (opt.typ != ssoTypeByte && opt.typ != ssoTypeInt) {
  14. return 0, errOpNoSupport
  15. }
  16. var i int32
  17. var b byte
  18. p := unsafe.Pointer(&i)
  19. l := uint32(4)
  20. if opt.typ == ssoTypeByte {
  21. p = unsafe.Pointer(&b)
  22. l = 1
  23. }
  24. if err := getsockopt(fd, iana.ProtocolIP, opt.name, p, &l); err != nil {
  25. return 0, os.NewSyscallError("getsockopt", err)
  26. }
  27. if opt.typ == ssoTypeByte {
  28. return int(b), nil
  29. }
  30. return int(i), nil
  31. }
  32. func setInt(fd int, opt *sockOpt, v int) error {
  33. if opt.name < 1 || (opt.typ != ssoTypeByte && opt.typ != ssoTypeInt) {
  34. return errOpNoSupport
  35. }
  36. i := int32(v)
  37. var b byte
  38. p := unsafe.Pointer(&i)
  39. l := uint32(4)
  40. if opt.typ == ssoTypeByte {
  41. b = byte(v)
  42. p = unsafe.Pointer(&b)
  43. l = 1
  44. }
  45. return os.NewSyscallError("setsockopt", setsockopt(fd, iana.ProtocolIP, opt.name, p, l))
  46. }
  47. func getInterface(fd int, opt *sockOpt) (*net.Interface, error) {
  48. if opt.name < 1 {
  49. return nil, errOpNoSupport
  50. }
  51. switch opt.typ {
  52. case ssoTypeInterface:
  53. return getsockoptInterface(fd, opt.name)
  54. case ssoTypeIPMreqn:
  55. return getsockoptIPMreqn(fd, opt.name)
  56. default:
  57. return nil, errOpNoSupport
  58. }
  59. }
  60. func setInterface(fd int, opt *sockOpt, ifi *net.Interface) error {
  61. if opt.name < 1 {
  62. return errOpNoSupport
  63. }
  64. switch opt.typ {
  65. case ssoTypeInterface:
  66. return setsockoptInterface(fd, opt.name, ifi)
  67. case ssoTypeIPMreqn:
  68. return setsockoptIPMreqn(fd, opt.name, ifi, nil)
  69. default:
  70. return errOpNoSupport
  71. }
  72. }
  73. func getICMPFilter(fd int, opt *sockOpt) (*ICMPFilter, error) {
  74. if opt.name < 1 || opt.typ != ssoTypeICMPFilter {
  75. return nil, errOpNoSupport
  76. }
  77. var f ICMPFilter
  78. l := uint32(sysSizeofICMPFilter)
  79. if err := getsockopt(fd, iana.ProtocolReserved, opt.name, unsafe.Pointer(&f.sysICMPFilter), &l); err != nil {
  80. return nil, os.NewSyscallError("getsockopt", err)
  81. }
  82. return &f, nil
  83. }
  84. func setICMPFilter(fd int, opt *sockOpt, f *ICMPFilter) error {
  85. if opt.name < 1 || opt.typ != ssoTypeICMPFilter {
  86. return errOpNoSupport
  87. }
  88. return os.NewSyscallError("setsockopt", setsockopt(fd, iana.ProtocolReserved, opt.name, unsafe.Pointer(&f.sysICMPFilter), sysSizeofICMPFilter))
  89. }
  90. func setGroup(fd int, opt *sockOpt, ifi *net.Interface, grp net.IP) error {
  91. if opt.name < 1 {
  92. return errOpNoSupport
  93. }
  94. switch opt.typ {
  95. case ssoTypeIPMreq:
  96. return setsockoptIPMreq(fd, opt.name, ifi, grp)
  97. case ssoTypeIPMreqn:
  98. return setsockoptIPMreqn(fd, opt.name, ifi, grp)
  99. case ssoTypeGroupReq:
  100. return setsockoptGroupReq(fd, opt.name, ifi, grp)
  101. default:
  102. return errOpNoSupport
  103. }
  104. }
  105. func setSourceGroup(fd int, 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(fd, opt.name, ifi, grp, src)
  110. }