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.
5 // GNU/Linux socket filter
13 func LsfStmt(code, k int) *SockFilter {
14 return &SockFilter{Code: uint16(code), K: uint32(k)}
17 func LsfJump(code, k, jt, jf int) *SockFilter {
18 return &SockFilter{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)}
21 func LsfSocket(ifindex, proto int) (int, int) {
22 var lsall SockaddrLinklayer
23 s, e := Socket(AF_PACKET, SOCK_RAW, proto)
27 p := (*[2]byte)(unsafe.Pointer(&lsall.Protocol))
28 p[0] = byte(proto >> 8)
30 lsall.Ifindex = ifindex
44 func SetLsfPromisc(name string, m bool) int {
45 s, e := Socket(AF_INET, SOCK_DGRAM, 0)
51 copy(ifl.name[:], []byte(name))
52 _, _, ep := Syscall(SYS_IOCTL, uintptr(s), SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
53 if e := int(ep); e != 0 {
57 ifl.flags |= uint16(IFF_PROMISC)
59 ifl.flags &= ^uint16(IFF_PROMISC)
61 _, _, ep = Syscall(SYS_IOCTL, uintptr(s), SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
62 if e := int(ep); e != 0 {
68 func AttachLsf(fd int, i []SockFilter) int {
70 p.Len = uint16(len(i))
71 p.Filter = (*SockFilter)(unsafe.Pointer(&i[0]))
72 return setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, uintptr(unsafe.Pointer(&p)), unsafe.Sizeof(p))
75 func DetachLsf(fd int) int {
77 return setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, uintptr(unsafe.Pointer(&dummy)), unsafe.Sizeof(dummy))