sockopt_posix.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 aix darwin dragonfly freebsd linux netbsd openbsd solaris windows
  5. package ipv4
  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. switch so.typ {
  14. case ssoTypeIPMreqn:
  15. return so.getIPMreqn(c)
  16. default:
  17. return so.getMulticastIf(c)
  18. }
  19. }
  20. func (so *sockOpt) setMulticastInterface(c *socket.Conn, ifi *net.Interface) error {
  21. switch so.typ {
  22. case ssoTypeIPMreqn:
  23. return so.setIPMreqn(c, ifi, nil)
  24. default:
  25. return so.setMulticastIf(c, ifi)
  26. }
  27. }
  28. func (so *sockOpt) getICMPFilter(c *socket.Conn) (*ICMPFilter, error) {
  29. b := make([]byte, so.Len)
  30. n, err := so.Get(c, b)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if n != sizeofICMPFilter {
  35. return nil, errNotImplemented
  36. }
  37. return (*ICMPFilter)(unsafe.Pointer(&b[0])), nil
  38. }
  39. func (so *sockOpt) setICMPFilter(c *socket.Conn, f *ICMPFilter) error {
  40. b := (*[sizeofICMPFilter]byte)(unsafe.Pointer(f))[:sizeofICMPFilter]
  41. return so.Set(c, b)
  42. }
  43. func (so *sockOpt) setGroup(c *socket.Conn, ifi *net.Interface, grp net.IP) error {
  44. switch so.typ {
  45. case ssoTypeIPMreq:
  46. return so.setIPMreq(c, ifi, grp)
  47. case ssoTypeIPMreqn:
  48. return so.setIPMreqn(c, ifi, grp)
  49. case ssoTypeGroupReq:
  50. return so.setGroupReq(c, ifi, grp)
  51. default:
  52. return errNotImplemented
  53. }
  54. }
  55. func (so *sockOpt) setSourceGroup(c *socket.Conn, ifi *net.Interface, grp, src net.IP) error {
  56. return so.setGroupSourceReq(c, ifi, grp, src)
  57. }
  58. func (so *sockOpt) setBPF(c *socket.Conn, f []bpf.RawInstruction) error {
  59. return so.setAttachFilter(c, f)
  60. }