-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpect.go
167 lines (145 loc) · 5.53 KB
/
expect.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package cgolmnfct
import (
mnl "github.com/chamaken/cgolmnl"
"reflect"
"syscall"
"unsafe"
)
/*
#cgo CFLAGS: -I./include
#cgo LDFLAGS: -lnetfilter_conntrack
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
*/
import "C"
type Expect C.struct_nf_expect // [0]byte
// struct nf_expect *nfexp_new(void)
func expectNew() (*Expect, error) {
ret, err := C.nfexp_new()
return (*Expect)(ret), err
}
// void nfexp_destroy(struct nf_expect *exp)
func expectDestroy(exp *Expect) {
C.nfexp_destroy((*C.struct_nf_expect)(exp))
}
// struct nf_expect *nfexp_clone(const struct nf_expect *exp)
func expectClone(exp *Expect) (*Expect, error) {
ret, err := C.nfexp_clone((*C.struct_nf_expect)(exp))
return (*Expect)(ret), err
}
// int nfexp_cmp(const struct nf_expect *exp1, const struct nf_expect *exp2,
// unsigned int flags)
func expectCmp(exp1, exp2 *Expect, flags int) int {
return int(C.nfexp_cmp((*C.struct_nf_expect)(exp1), (*C.struct_nf_expect)(exp2), C.uint(flags)))
}
// NO Library setup
// void nfexp_set_attr(struct nf_expect *exp,
// const enum nf_expect_attr type,
// const void *value)
func expectSetAttr(exp *Expect, attr_type ExpectAttr, value unsafe.Pointer) error {
if attr_type >= ATTR_EXP_MAX {
return syscall.EINVAL
}
C.nfexp_set_attr((*C.struct_nf_expect)(exp), C.enum_nf_expect_attr(attr_type), value)
return nil
}
func expectSetAttrPtr(exp *Expect, attr_type ExpectAttr, value interface{}) error {
v := reflect.ValueOf(value)
if v.Kind() != reflect.Ptr {
panic("pointer required for value")
}
return expectSetAttr(exp, attr_type, unsafe.Pointer(v.Pointer()))
}
// void nfexp_set_attr_u8(struct nf_expect *exp,
// const enum nf_expect_attr type,
// u_int8_t value)
func expectSetAttrU8(exp *Expect, attr_type ExpectAttr, value uint8) error {
if attr_type >= ATTR_EXP_MAX {
return syscall.EINVAL
}
C.nfexp_set_attr_u8((*C.struct_nf_expect)(exp), C.enum_nf_expect_attr(attr_type), (C.uint8_t)(value))
return nil
}
// void nfexp_set_attr_u16(struct nf_expect *exp,
// const enum nf_expect_attr type,
// u_int16_t value)
func expectSetAttrU16(exp *Expect, attr_type ExpectAttr, value uint16) error {
if attr_type >= ATTR_EXP_MAX {
return syscall.EINVAL
}
C.nfexp_set_attr_u16((*C.struct_nf_expect)(exp), C.enum_nf_expect_attr(attr_type), (C.uint16_t)(value))
return nil
}
// void nfexp_set_attr_u32(struct nf_expect *exp,
// const enum nf_expect_attr type,
// u_int32_t value)
func expectSetAttrU32(exp *Expect, attr_type ExpectAttr, value uint32) error {
if attr_type >= ATTR_EXP_MAX {
return syscall.EINVAL
}
C.nfexp_set_attr_u32((*C.struct_nf_expect)(exp), C.enum_nf_expect_attr(attr_type), (C.uint32_t)(value))
return nil
}
// const void *nfexp_get_attr(const struct nf_expect *exp,
// const enum nf_expect_attr type)
func expectGetAttr(exp *Expect, attr_type ExpectAttr) (unsafe.Pointer, error) {
ret, err := C.nfexp_get_attr((*C.struct_nf_expect)(exp), C.enum_nf_expect_attr(attr_type))
return ret, err
}
// u_int8_t nfexp_get_attr_u8(const struct nf_expect *exp,
// const enum nf_expect_attr type)
func expectGetAttrU8(exp *Expect, attr_type ExpectAttr) (uint8, error) {
ret, err := C.nfexp_get_attr_u8((*C.struct_nf_expect)(exp), C.enum_nf_expect_attr(attr_type))
return uint8(ret), err
}
// u_int16_t nfexp_get_attr_u16(const struct nf_expect *exp,
// const enum nf_expect_attr type)
func expectGetAttrU16(exp *Expect, attr_type ExpectAttr) (uint16, error) {
ret, err := C.nfexp_get_attr_u16((*C.struct_nf_expect)(exp), C.enum_nf_expect_attr(attr_type))
return uint16(ret), err
}
// u_int32_t nfexp_get_attr_u32(const struct nf_expect *exp,
// const enum nf_expect_attr type)
func expectGetAttrU32(exp *Expect, attr_type ExpectAttr) (uint32, error) {
ret, err := C.nfexp_get_attr_u32((*C.struct_nf_expect)(exp), C.enum_nf_expect_attr(attr_type))
return uint32(ret), err
}
// int nfexp_attr_is_set(const struct nf_expect *exp,
// const enum nf_expect_attr type)
func expectAttrIsSet(exp *Expect, attr_type ExpectAttr) (bool, error) {
ret, err := C.nfexp_attr_is_set((*C.struct_nf_expect)(exp), C.enum_nf_expect_attr(attr_type))
return ret > 0, err
}
// int nfexp_attr_unset(struct nf_expect *exp,
// const enum nf_expect_attr type)
func expectAttrUnset(exp *Expect, attr_type ExpectAttr) error {
_, err := C.nfexp_attr_unset((*C.struct_nf_expect)(exp), C.enum_nf_expect_attr(attr_type))
return err
}
// NO Low level object to Netlink message
// NO Send commands to kernel-space and receive replies
// int nfexp_snprintf(char *buf,
// unsigned int size,
// const struct nf_expect *exp,
// unsigned int msg_type,
// unsigned int out_type,
// unsigned int flags)
func expectSnprintf(buf []byte, exp *Expect, msg_type ConntrackMsgType, out_type, flags uint) (int, error) {
ret, err := C.nfexp_snprintf((*C.char)(unsafe.Pointer(&buf[0])), C.uint(len(buf)), (*C.struct_nf_expect)(exp),
C.uint(msg_type), C.uint(out_type), C.uint(flags))
return int(ret), err
}
// int
// nfexp_nlmsg_build(struct nlmsghdr *nlh, const struct nf_expect *exp)
func expectNlmsgBuild(nlh *mnl.Nlmsg, exp *Expect) (int, error) {
ret, err := C.nfexp_nlmsg_build(
(*C.struct_nlmsghdr)(unsafe.Pointer(nlh.Nlmsghdr)),
(*C.struct_nf_expect)(exp))
return int(ret), err
}
// int nfexp_nlmsg_parse(const struct nlmsghdr *nlh, struct nf_expect *exp)
func expectNlmsgParse(nlh *mnl.Nlmsg, exp *Expect) (int, error) {
ret, err := C.nfexp_nlmsg_parse(
(*C.struct_nlmsghdr)(unsafe.Pointer(nlh.Nlmsghdr)),
(*C.struct_nf_expect)(exp))
return int(ret), err
}