bpfopt_linux.go 750 B

12345678910111213141516171819202122232425262728
  1. // Copyright 2016 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. package ipv6
  5. import (
  6. "os"
  7. "unsafe"
  8. "golang.org/x/net/bpf"
  9. "golang.org/x/net/internal/netreflect"
  10. )
  11. // SetBPF attaches a BPF program to the connection.
  12. //
  13. // Only supported on Linux.
  14. func (c *dgramOpt) SetBPF(filter []bpf.RawInstruction) error {
  15. s, err := netreflect.PacketSocketOf(c.PacketConn)
  16. if err != nil {
  17. return err
  18. }
  19. prog := sockFProg{
  20. Len: uint16(len(filter)),
  21. Filter: (*sockFilter)(unsafe.Pointer(&filter[0])),
  22. }
  23. return os.NewSyscallError("setsockopt", setsockopt(s, sysSOL_SOCKET, sysSO_ATTACH_FILTER, unsafe.Pointer(&prog), uint32(unsafe.Sizeof(prog))))
  24. }