github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/syscall/lsf_linux.go (about)

     1  // Copyright 2011 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  
     5  // Linux socket filter
     6  
     7  package syscall
     8  
     9  import (
    10  	"unsafe"
    11  )
    12  
    13  // Deprecated: Use golang.org/x/net/bpf instead.
    14  func LsfStmt(code, k int) *SockFilter {
    15  	return &SockFilter{Code: uint16(code), K: uint32(k)}
    16  }
    17  
    18  // Deprecated: Use golang.org/x/net/bpf instead.
    19  func LsfJump(code, k, jt, jf int) *SockFilter {
    20  	return &SockFilter{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)}
    21  }
    22  
    23  // Deprecated: Use golang.org/x/net/bpf instead.
    24  func LsfSocket(ifindex, proto int) (int, error) {
    25  	var lsall SockaddrLinklayer
    26  	s, e := Socket(AF_PACKET, SOCK_RAW, proto)
    27  	if e != nil {
    28  		return 0, e
    29  	}
    30  	p := (*[2]byte)(unsafe.Pointer(&lsall.Protocol))
    31  	p[0] = byte(proto >> 8)
    32  	p[1] = byte(proto)
    33  	lsall.Ifindex = ifindex
    34  	e = Bind(s, &lsall)
    35  	if e != nil {
    36  		Close(s)
    37  		return 0, e
    38  	}
    39  	return s, nil
    40  }
    41  
    42  type iflags struct {
    43  	name  [IFNAMSIZ]byte
    44  	flags uint16
    45  }
    46  
    47  // Deprecated: Use golang.org/x/net/bpf instead.
    48  func SetLsfPromisc(name string, m bool) error {
    49  	s, e := Socket(AF_INET, SOCK_DGRAM, 0)
    50  	if e != nil {
    51  		return e
    52  	}
    53  	defer Close(s)
    54  	var ifl iflags
    55  	copy(ifl.name[:], []byte(name))
    56  	_, _, ep := Syscall(SYS_IOCTL, uintptr(s), SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
    57  	if ep != 0 {
    58  		return Errno(ep)
    59  	}
    60  	if m {
    61  		ifl.flags |= uint16(IFF_PROMISC)
    62  	} else {
    63  		ifl.flags &^= uint16(IFF_PROMISC)
    64  	}
    65  	_, _, ep = Syscall(SYS_IOCTL, uintptr(s), SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
    66  	if ep != 0 {
    67  		return Errno(ep)
    68  	}
    69  	return nil
    70  }
    71  
    72  // Deprecated: Use golang.org/x/net/bpf instead.
    73  func AttachLsf(fd int, i []SockFilter) error {
    74  	var p SockFprog
    75  	p.Len = uint16(len(i))
    76  	p.Filter = (*SockFilter)(unsafe.Pointer(&i[0]))
    77  	return setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, unsafe.Pointer(&p), unsafe.Sizeof(p))
    78  }
    79  
    80  // Deprecated: Use golang.org/x/net/bpf instead.
    81  func DetachLsf(fd int) error {
    82  	var dummy int
    83  	return setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, unsafe.Pointer(&dummy), unsafe.Sizeof(dummy))
    84  }