sockopt_posix.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 aix darwin dragonfly freebsd linux netbsd openbsd solaris windows
  5. package ipv6
  6. import (
  7. "net"
  8. "runtime"
  9. "unsafe"
  10. "golang.org/x/net/bpf"
  11. "golang.org/x/net/internal/socket"
  12. )
  13. func (so *sockOpt) getMulticastInterface(c *socket.Conn) (*net.Interface, error) {
  14. n, err := so.GetInt(c)
  15. if err != nil {
  16. return nil, err
  17. }
  18. return net.InterfaceByIndex(n)
  19. }
  20. func (so *sockOpt) setMulticastInterface(c *socket.Conn, ifi *net.Interface) error {
  21. var n int
  22. if ifi != nil {
  23. n = ifi.Index
  24. }
  25. return so.SetInt(c, n)
  26. }
  27. func (so *sockOpt) getICMPFilter(c *socket.Conn) (*ICMPFilter, error) {
  28. b := make([]byte, so.Len)
  29. n, err := so.Get(c, b)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if n != sizeofICMPv6Filter {
  34. return nil, errNotImplemented
  35. }
  36. return (*ICMPFilter)(unsafe.Pointer(&b[0])), nil
  37. }
  38. func (so *sockOpt) setICMPFilter(c *socket.Conn, f *ICMPFilter) error {
  39. b := (*[sizeofICMPv6Filter]byte)(unsafe.Pointer(f))[:sizeofICMPv6Filter]
  40. return so.Set(c, b)
  41. }
  42. func (so *sockOpt) getMTUInfo(c *socket.Conn) (*net.Interface, int, error) {
  43. b := make([]byte, so.Len)
  44. n, err := so.Get(c, b)
  45. if err != nil {
  46. return nil, 0, err
  47. }
  48. if n != sizeofIPv6Mtuinfo {
  49. return nil, 0, errNotImplemented
  50. }
  51. mi := (*ipv6Mtuinfo)(unsafe.Pointer(&b[0]))
  52. if mi.Addr.Scope_id == 0 || runtime.GOOS == "aix" {
  53. // AIX kernel might return a wrong address.
  54. return nil, int(mi.Mtu), nil
  55. }
  56. ifi, err := net.InterfaceByIndex(int(mi.Addr.Scope_id))
  57. if err != nil {
  58. return nil, 0, err
  59. }
  60. return ifi, int(mi.Mtu), nil
  61. }
  62. func (so *sockOpt) setGroup(c *socket.Conn, ifi *net.Interface, grp net.IP) error {
  63. switch so.typ {
  64. case ssoTypeIPMreq:
  65. return so.setIPMreq(c, ifi, grp)
  66. case ssoTypeGroupReq:
  67. return so.setGroupReq(c, ifi, grp)
  68. default:
  69. return errNotImplemented
  70. }
  71. }
  72. func (so *sockOpt) setSourceGroup(c *socket.Conn, ifi *net.Interface, grp, src net.IP) error {
  73. return so.setGroupSourceReq(c, ifi, grp, src)
  74. }
  75. func (so *sockOpt) setBPF(c *socket.Conn, f []bpf.RawInstruction) error {
  76. return so.setAttachFilter(c, f)
  77. }