...

Source file src/github.com/mdlayher/socket/conn_linux.go

Documentation: github.com/mdlayher/socket

     1  //go:build linux
     2  // +build linux
     3  
     4  package socket
     5  
     6  import (
     7  	"context"
     8  	"os"
     9  	"unsafe"
    10  
    11  	"golang.org/x/net/bpf"
    12  	"golang.org/x/sys/unix"
    13  )
    14  
    15  // IoctlKCMClone wraps ioctl(2) for unix.KCMClone values, but returns a Conn
    16  // rather than a raw file descriptor.
    17  func (c *Conn) IoctlKCMClone() (*Conn, error) {
    18  	info, err := controlT(c, context.Background(), "ioctl", unix.IoctlKCMClone)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	// Successful clone, wrap in a Conn for use by the caller.
    24  	return New(int(info.Fd), c.name)
    25  }
    26  
    27  // IoctlKCMAttach wraps ioctl(2) for unix.KCMAttach values.
    28  func (c *Conn) IoctlKCMAttach(info unix.KCMAttach) error {
    29  	return c.control(context.Background(), "ioctl", func(fd int) error {
    30  		return unix.IoctlKCMAttach(fd, info)
    31  	})
    32  }
    33  
    34  // IoctlKCMUnattach wraps ioctl(2) for unix.KCMUnattach values.
    35  func (c *Conn) IoctlKCMUnattach(info unix.KCMUnattach) error {
    36  	return c.control(context.Background(), "ioctl", func(fd int) error {
    37  		return unix.IoctlKCMUnattach(fd, info)
    38  	})
    39  }
    40  
    41  // PidfdGetfd wraps pidfd_getfd(2) for a Conn which wraps a pidfd, but returns a
    42  // Conn rather than a raw file descriptor.
    43  func (c *Conn) PidfdGetfd(targetFD, flags int) (*Conn, error) {
    44  	outFD, err := controlT(c, context.Background(), "pidfd_getfd", func(fd int) (int, error) {
    45  		return unix.PidfdGetfd(fd, targetFD, flags)
    46  	})
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	// Successful getfd, wrap in a Conn for use by the caller.
    52  	return New(outFD, c.name)
    53  }
    54  
    55  // PidfdSendSignal wraps pidfd_send_signal(2) for a Conn which wraps a Linux
    56  // pidfd.
    57  func (c *Conn) PidfdSendSignal(sig unix.Signal, info *unix.Siginfo, flags int) error {
    58  	return c.control(context.Background(), "pidfd_send_signal", func(fd int) error {
    59  		return unix.PidfdSendSignal(fd, sig, info, flags)
    60  	})
    61  }
    62  
    63  // SetBPF attaches an assembled BPF program to a Conn.
    64  func (c *Conn) SetBPF(filter []bpf.RawInstruction) error {
    65  	// We can't point to the first instruction in the array if no instructions
    66  	// are present.
    67  	if len(filter) == 0 {
    68  		return os.NewSyscallError("setsockopt", unix.EINVAL)
    69  	}
    70  
    71  	prog := unix.SockFprog{
    72  		Len:    uint16(len(filter)),
    73  		Filter: (*unix.SockFilter)(unsafe.Pointer(&filter[0])),
    74  	}
    75  
    76  	return c.SetsockoptSockFprog(unix.SOL_SOCKET, unix.SO_ATTACH_FILTER, &prog)
    77  }
    78  
    79  // RemoveBPF removes a BPF filter from a Conn.
    80  func (c *Conn) RemoveBPF() error {
    81  	// 0 argument is ignored.
    82  	return c.SetsockoptInt(unix.SOL_SOCKET, unix.SO_DETACH_FILTER, 0)
    83  }
    84  
    85  // SetsockoptPacketMreq wraps setsockopt(2) for unix.PacketMreq values.
    86  func (c *Conn) SetsockoptPacketMreq(level, opt int, mreq *unix.PacketMreq) error {
    87  	return c.control(context.Background(), "setsockopt", func(fd int) error {
    88  		return unix.SetsockoptPacketMreq(fd, level, opt, mreq)
    89  	})
    90  }
    91  
    92  // SetsockoptSockFprog wraps setsockopt(2) for unix.SockFprog values.
    93  func (c *Conn) SetsockoptSockFprog(level, opt int, fprog *unix.SockFprog) error {
    94  	return c.control(context.Background(), "setsockopt", func(fd int) error {
    95  		return unix.SetsockoptSockFprog(fd, level, opt, fprog)
    96  	})
    97  }
    98  
    99  // GetsockoptTpacketStats wraps getsockopt(2) for unix.TpacketStats values.
   100  func (c *Conn) GetsockoptTpacketStats(level, name int) (*unix.TpacketStats, error) {
   101  	return controlT(c, context.Background(), "getsockopt", func(fd int) (*unix.TpacketStats, error) {
   102  		return unix.GetsockoptTpacketStats(fd, level, name)
   103  	})
   104  }
   105  
   106  // GetsockoptTpacketStatsV3 wraps getsockopt(2) for unix.TpacketStatsV3 values.
   107  func (c *Conn) GetsockoptTpacketStatsV3(level, name int) (*unix.TpacketStatsV3, error) {
   108  	return controlT(c, context.Background(), "getsockopt", func(fd int) (*unix.TpacketStatsV3, error) {
   109  		return unix.GetsockoptTpacketStatsV3(fd, level, name)
   110  	})
   111  }
   112  
   113  // Waitid wraps waitid(2).
   114  func (c *Conn) Waitid(idType int, info *unix.Siginfo, options int, rusage *unix.Rusage) error {
   115  	return c.read(context.Background(), "waitid", func(fd int) error {
   116  		return unix.Waitid(idType, fd, info, options, rusage)
   117  	})
   118  }
   119  

View as plain text