-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_nfq.go
69 lines (54 loc) · 1.4 KB
/
go_nfq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (C) 2015 Martin Garton <[email protected]>
package nfq
/*
#cgo pkg-config: libnetfilter_queue
#cgo CFLAGS: -Wall -Werror -I/usr/include
#cgo LDFLAGS: -L/usr/lib64/
#include "go_nfq.h"
*/
import "C"
import (
"unsafe"
)
type Queue struct {
nfp C.struct_go_nfq_params
closed chan struct{}
callback Callback
}
type Callback func([]byte) Verdict
type Verdict C.uint
const (
NF_DROP Verdict = 0
NF_ACCEPT Verdict = 1
NF_STOLEN Verdict = 2
NF_QUEUE Verdict = 3
NF_REPEAT Verdict = 4
NF_STOP Verdict = 5
)
func NewDefaultQueue(queueId uint16, callback Callback) (*Queue, error) {
return NewQueue(queueId, 1 /* is 1 a reasonable default? */, 0xffff, callback)
}
func NewQueue(queueId uint16, maxPacketsInQueue uint32, packetSize uint32, callback Callback) (*Queue, error) {
var nfq = Queue{closed: make(chan struct{})}
nfq.callback = callback
var err error
var ret C.int
ret, err = C.go_nfq_init(&nfq.nfp, C.u_int16_t(queueId), unsafe.Pointer(&nfq), C.u_int32_t(maxPacketsInQueue), C.u_int(packetSize))
if err != nil || ret < 0 {
return nil, err
}
go func() {
C.go_nfq_run(&nfq.nfp)
close(nfq.closed)
}()
return &nfq, nil
}
func (nfq *Queue) Close() {
C.go_nfq_stop(&nfq.nfp)
<-nfq.closed
}
//export callback
func callback(queueId C.int, data *C.uchar, len C.int, nfqp unsafe.Pointer) Verdict {
nfq := (*Queue)(nfqp)
return nfq.callback(C.GoBytes(unsafe.Pointer(data), len))
}