~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/syscall/bpf_bsd.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// Berkeley packet filter for BSD variants
 
6
 
 
7
package syscall
 
8
 
 
9
import (
 
10
        "unsafe"
 
11
)
 
12
 
 
13
func BpfStmt(code, k int) *BpfInsn {
 
14
        return &BpfInsn{Code: uint16(code), K: uint32(k)}
 
15
}
 
16
 
 
17
func BpfJump(code, k, jt, jf int) *BpfInsn {
 
18
        return &BpfInsn{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)}
 
19
}
 
20
 
 
21
func BpfBuflen(fd int) (int, int) {
 
22
        var l int
 
23
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCGBLEN, uintptr(unsafe.Pointer(&l)))
 
24
        if e := int(ep); e != 0 {
 
25
                return 0, e
 
26
        }
 
27
        return l, 0
 
28
}
 
29
 
 
30
func SetBpfBuflen(fd, l int) (int, int) {
 
31
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCSBLEN, uintptr(unsafe.Pointer(&l)))
 
32
        if e := int(ep); e != 0 {
 
33
                return 0, e
 
34
        }
 
35
        return l, 0
 
36
}
 
37
 
 
38
func BpfDatalink(fd int) (int, int) {
 
39
        var t int
 
40
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCGDLT, uintptr(unsafe.Pointer(&t)))
 
41
        if e := int(ep); e != 0 {
 
42
                return 0, e
 
43
        }
 
44
        return t, 0
 
45
}
 
46
 
 
47
func SetBpfDatalink(fd, t int) (int, int) {
 
48
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCSDLT, uintptr(unsafe.Pointer(&t)))
 
49
        if e := int(ep); e != 0 {
 
50
                return 0, e
 
51
        }
 
52
        return t, 0
 
53
}
 
54
 
 
55
func SetBpfPromisc(fd, m int) int {
 
56
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCPROMISC, uintptr(unsafe.Pointer(&m)))
 
57
        if e := int(ep); e != 0 {
 
58
                return e
 
59
        }
 
60
        return 0
 
61
}
 
62
 
 
63
func FlushBpf(fd int) int {
 
64
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCFLUSH, 0)
 
65
        if e := int(ep); e != 0 {
 
66
                return e
 
67
        }
 
68
        return 0
 
69
}
 
70
 
 
71
type ivalue struct {
 
72
        name  [IFNAMSIZ]byte
 
73
        value int16
 
74
}
 
75
 
 
76
func BpfInterface(fd int, name string) (string, int) {
 
77
        var iv ivalue
 
78
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCGETIF, uintptr(unsafe.Pointer(&iv)))
 
79
        if e := int(ep); e != 0 {
 
80
                return "", e
 
81
        }
 
82
        return name, 0
 
83
}
 
84
 
 
85
func SetBpfInterface(fd int, name string) int {
 
86
        var iv ivalue
 
87
        copy(iv.name[:], []byte(name))
 
88
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETIF, uintptr(unsafe.Pointer(&iv)))
 
89
        if e := int(ep); e != 0 {
 
90
                return e
 
91
        }
 
92
        return 0
 
93
}
 
94
 
 
95
func BpfTimeout(fd int) (*Timeval, int) {
 
96
        var tv Timeval
 
97
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCGRTIMEOUT, uintptr(unsafe.Pointer(&tv)))
 
98
        if e := int(ep); e != 0 {
 
99
                return nil, e
 
100
        }
 
101
        return &tv, 0
 
102
}
 
103
 
 
104
func SetBpfTimeout(fd int, tv *Timeval) int {
 
105
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCSRTIMEOUT, uintptr(unsafe.Pointer(tv)))
 
106
        if e := int(ep); e != 0 {
 
107
                return e
 
108
        }
 
109
        return 0
 
110
}
 
111
 
 
112
func BpfStats(fd int) (*BpfStat, int) {
 
113
        var s BpfStat
 
114
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCGSTATS, uintptr(unsafe.Pointer(&s)))
 
115
        if e := int(ep); e != 0 {
 
116
                return nil, e
 
117
        }
 
118
        return &s, 0
 
119
}
 
120
 
 
121
func SetBpfImmediate(fd, m int) int {
 
122
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCIMMEDIATE, uintptr(unsafe.Pointer(&m)))
 
123
        if e := int(ep); e != 0 {
 
124
                return e
 
125
        }
 
126
        return 0
 
127
}
 
128
 
 
129
func SetBpf(fd int, i []BpfInsn) int {
 
130
        var p BpfProgram
 
131
        p.Len = uint32(len(i))
 
132
        p.Insns = (*BpfInsn)(unsafe.Pointer(&i[0]))
 
133
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCSETF, uintptr(unsafe.Pointer(&p)))
 
134
        if e := int(ep); e != 0 {
 
135
                return e
 
136
        }
 
137
        return 0
 
138
}
 
139
 
 
140
func CheckBpfVersion(fd int) int {
 
141
        var v BpfVersion
 
142
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCVERSION, uintptr(unsafe.Pointer(&v)))
 
143
        if e := int(ep); e != 0 {
 
144
                return e
 
145
        }
 
146
        if v.Major != BPF_MAJOR_VERSION || v.Minor != BPF_MINOR_VERSION {
 
147
                return EINVAL
 
148
        }
 
149
        return 0
 
150
}
 
151
 
 
152
func BpfHeadercmpl(fd int) (int, int) {
 
153
        var f int
 
154
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCGHDRCMPLT, uintptr(unsafe.Pointer(&f)))
 
155
        if e := int(ep); e != 0 {
 
156
                return 0, e
 
157
        }
 
158
        return f, 0
 
159
}
 
160
 
 
161
func SetBpfHeadercmpl(fd, f int) int {
 
162
        _, _, ep := Syscall(SYS_IOCTL, uintptr(fd), BIOCSHDRCMPLT, uintptr(unsafe.Pointer(&f)))
 
163
        if e := int(ep); e != 0 {
 
164
                return e
 
165
        }
 
166
        return 0
 
167
}