Skip to content

Commit

Permalink
refactor: split read buffer and parse packet
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Jun 30, 2024
1 parent 5bd35af commit e1da571
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/ua2f.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,43 @@ void signal_handler(const int signum) {
should_exit = true;
}

int parse_packet(const struct nf_queue *queue, struct nf_buffer *buf) {
struct nf_packet packet[1] = {0};

while (!should_exit) {
const __auto_type status = nfqueue_next(buf, packet);
switch (status) {
case IO_READY:
handle_packet(queue, packet);
break;
default:
return status;
}
}

return IO_ERROR;
}

int read_buffer(struct nf_queue *queue, struct nf_buffer *buf) {
const __auto_type buf_status = nfqueue_receive(queue, buf, 0);
switch (buf_status) {
case IO_READY:
return parse_packet(queue, buf);
default:
return buf_status;
}
}

void main_loop(struct nf_queue *queue) {
struct nf_buffer buf[1] = {0};

while (!should_exit) {
if (read_buffer(queue, buf) == IO_ERROR) {
break;
}
}
}

int main(const int argc, char *argv[]) {
openlog("UA2F", LOG_PID, LOG_SYSLOG);

Expand All @@ -44,7 +81,6 @@ int main(const int argc, char *argv[]) {
signal(SIGABRT, signal_handler);

struct nf_queue queue[1] = {0};
struct nf_buffer buf[1] = {0};

const __auto_type ret = nfqueue_open(queue, QUEUE_NUM, 0);
if (!ret) {
Expand Down

0 comments on commit e1da571

Please sign in to comment.