OSDN Git Service

0976688b9e713d084d1f2167876bcc1add34fc90
[pf3gnuchains/gcc-fork.git] / libgo / go / syscall / lsf_linux.go
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 // GNU/Linux socket filter
6
7 package syscall
8
9 import (
10         "unsafe"
11 )
12
13 func LsfStmt(code, k int) *SockFilter {
14         return &SockFilter{Code: uint16(code), K: uint32(k)}
15 }
16
17 func LsfJump(code, k, jt, jf int) *SockFilter {
18         return &SockFilter{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)}
19 }
20
21 func LsfSocket(ifindex, proto int) (int, int) {
22         var lsall SockaddrLinklayer
23         s, e := Socket(AF_PACKET, SOCK_RAW, proto)
24         if e != 0 {
25                 return 0, e
26         }
27         p := (*[2]byte)(unsafe.Pointer(&lsall.Protocol))
28         p[0] = byte(proto >> 8)
29         p[1] = byte(proto)
30         lsall.Ifindex = ifindex
31         e = Bind(s, &lsall)
32         if e != 0 {
33                 Close(s)
34                 return 0, e
35         }
36         return s, 0
37 }
38
39 type iflags struct {
40         name  [IFNAMSIZ]byte
41         flags uint16
42 }
43
44 func SetLsfPromisc(name string, m bool) int {
45         s, e := Socket(AF_INET, SOCK_DGRAM, 0)
46         if e != 0 {
47                 return e
48         }
49         defer Close(s)
50         var ifl iflags
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 {
54                 return e
55         }
56         if m {
57                 ifl.flags |= uint16(IFF_PROMISC)
58         } else {
59                 ifl.flags &= ^uint16(IFF_PROMISC)
60         }
61         _, _, ep = Syscall(SYS_IOCTL, uintptr(s), SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
62         if e := int(ep); e != 0 {
63                 return e
64         }
65         return 0
66 }
67
68 func AttachLsf(fd int, i []SockFilter) int {
69         var p SockFprog
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))
73 }
74
75 func DetachLsf(fd int) int {
76         var dummy int
77         return setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, uintptr(unsafe.Pointer(&dummy)), unsafe.Sizeof(dummy))
78 }