lsf_linux.go 1.7 KB

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