libgo: Update to Go 1.3 release.
[official-gcc.git] / libgo / go / syscall / lsf_linux.go
blob48ba191c6da928a8ace2ea6f014db72d081f3f54
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 // Linux socket filter
7 package syscall
9 import (
10 "unsafe"
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, error) {
22 var lsall SockaddrLinklayer
23 s, e := Socket(AF_PACKET, SOCK_RAW, proto)
24 if e != nil {
25 return 0, e
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 != nil {
33 Close(s)
34 return 0, e
36 return s, nil
39 type iflags struct {
40 name [IFNAMSIZ]byte
41 flags uint16
44 func SetLsfPromisc(name string, m bool) error {
45 s, e := Socket(AF_INET, SOCK_DGRAM, 0)
46 if e != nil {
47 return e
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 ep != 0 {
54 return Errno(ep)
56 if m {
57 ifl.flags |= uint16(IFF_PROMISC)
58 } else {
59 ifl.flags &= ^uint16(IFF_PROMISC)
61 _, _, ep = Syscall(SYS_IOCTL, uintptr(s), SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
62 if ep != 0 {
63 return Errno(ep)
65 return nil
68 func AttachLsf(fd int, i []SockFilter) error {
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, unsafe.Pointer(&p), Socklen_t(unsafe.Sizeof(p)))
75 func DetachLsf(fd int) error {
76 var dummy int
77 return setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, unsafe.Pointer(&dummy), Socklen_t(unsafe.Sizeof(dummy)))