bpfopt_linux.go 688 B

123456789101112131415161718192021222324252627
  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. )
  10. // SetBPF attaches a BPF program to the connection.
  11. //
  12. // Only supported on Linux.
  13. func (c *dgramOpt) SetBPF(filter []bpf.RawInstruction) error {
  14. fd, err := c.sysfd()
  15. if err != nil {
  16. return err
  17. }
  18. prog := sysSockFProg{
  19. Len: uint16(len(filter)),
  20. Filter: (*sysSockFilter)(unsafe.Pointer(&filter[0])),
  21. }
  22. return os.NewSyscallError("setsockopt", setsockopt(fd, sysSOL_SOCKET, sysSO_ATTACH_FILTER, unsafe.Pointer(&prog), uint32(unsafe.Sizeof(prog))))
  23. }