sockopt_posix.go 2.1 KB

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