Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 8b4dece

Browse files
authored
fix: queueID was taken wrong sporadically (#2)
* fix: queueID was taken wrong sporadically * fix: handler types
1 parent 3b85f77 commit 8b4dece

5 files changed

Lines changed: 21 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Each packet that is processed by a netfilter queue is encapsulated in the type `
5454
// Packet struct provides the packet data and methods to accept, drop or modify the packet.
5555
type Packet struct {
5656
Buffer []byte
57-
id C.uint32_t
57+
id uint32
5858
q *Queue
5959
}
6060
@@ -95,6 +95,7 @@ func NewQueue(id uint16) *Queue {
9595
}
9696
queueCfg := &nfqueue.QueueConfig{
9797
MaxPackets: 1000,
98+
BufferSize: 16 * 1024 * 1024,
9899
QueueFlags: []nfqueue.QueueFlag{nfqueue.FailOpen},
99100
}
100101
// Pass as packet handler the current instance because it implements nfqueue.PacketHandler interface

handler.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ import (
2323
)
2424

2525
//export handle
26-
func handle(id C.uint, buffer *C.uchar, len C.int, cbData *unsafe.Pointer) int {
27-
queueID := (*uint16)(unsafe.Pointer(cbData))
28-
q := queueRegistry.Get(*queueID)
26+
func handle(id uint32, buffer *C.uchar, len C.int, queueID int) int {
27+
q := queueRegistry.Get(uint16(queueID))
28+
if q == nil {
29+
return 0
30+
}
2931
packet := &Packet{
30-
id: uint32(id),
32+
id: id,
3133
Buffer: C.GoBytes(unsafe.Pointer(buffer), len),
3234
q: q,
3335
}

nfqueue.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,8 @@ func (q *Queue) Start() error {
127127
// It is not possible to pass the queue as callback data due to error:
128128
// runtime error: cgo argument has Go pointer to Go pointer
129129
// As a result, we have to pass the queue ID and use the registry to retrieve the queue.
130-
qid := q.ID
131-
cb := (*C.nfq_callback)(C.nfqueue_cb)
132-
if q.qh = C.nfq_create_queue(q.h, C.u_int16_t(q.ID), cb, unsafe.Pointer(&qid)); q.qh == nil {
133-
return errors.New("Error in nfq_create_queue")
130+
if q.qh = C.nfqueue_create_queue(q.h, C.u_int16_t(q.ID)); q.qh == nil {
131+
return errors.New("Error in nfqueue_create_queue")
134132
}
135133

136134
// Configure mode (packet copy) and the packet size. Note that this is not configurable on purpose.
@@ -156,8 +154,7 @@ func (q *Queue) Start() error {
156154
}
157155
}
158156

159-
q.fd = C.nfq_fd(q.h)
160-
if q.fd < 0 {
157+
if q.fd = C.nfq_fd(q.h); q.fd < 0 {
161158
return errors.New("Error in nfq_fd")
162159
}
163160

nfqueue.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@ const uint MAX_PACKET_SIZE = 65535;
3131
// - id (packet identifier)
3232
// - buffer (pointer to the packet data starting from IP layer)
3333
// - len (buffer length)
34-
// - cb_data (pointer to the queue identifier)
35-
extern int handle(uint32_t id, unsigned char* buffer, int len, void *cb_data);
34+
// - queue_id (queue identifier)
35+
extern int handle(uint32_t id, unsigned char* buffer, int len, int queue_id);
3636

3737
int nfqueue_cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *cb_data)
3838
{
3939
unsigned char *buffer = NULL;
4040
struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfa);
4141
uint32_t id = ntohl(ph->packet_id);
4242
int ret = nfq_get_payload(nfa, &buffer);
43+
return handle(id, buffer, ret, (intptr_t)cb_data);
44+
}
4345

44-
return handle(id, buffer, ret, cb_data);
46+
static struct nfq_q_handle *nfqueue_create_queue(struct nfq_handle *h, u_int16_t queue_id) {
47+
return nfq_create_queue(h, queue_id, &nfqueue_cb, (void *)(intptr_t)queue_id);
4548
}
4649

4750
static int nfqueue_loop(struct nfq_handle *h, int fd)

registry.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,8 @@ func (r *QueueRegistry) Unregister(queueID uint16) {
5555

5656
// Get returns a queue from the registry based on the queueID.
5757
func (r *QueueRegistry) Get(queueID uint16) *Queue {
58-
return r.queues[queueID]
58+
if len(r.queues) > int(queueID) {
59+
return r.queues[queueID]
60+
}
61+
return nil
5962
}

0 commit comments

Comments
 (0)