쓰레드 프로그램 구현중인데 질문드립니다.

wonjnlee의 이미지

안녕하세요.

netfilter queue를 쓰레드로 구현하는 프로그램을 만들어보고 있습니다.
(pc를 브릿지로 구성해서 client1에서 보내는 패킷을 받아서 client2로 전달(forward)해주고 있습니다)
그런데 잘 안되네요..ㅠㅠ
어디에 문제가 있을 것 같은지 혹시 확인해주시면 감사하겠습니다.

소스코드 밑에 첨부합니다.
(작성 중에 발생한 오류들은 주석으로 달아놓았습니다)

동작시켰을 때 다음과 같은 오류가 있었습니다.
1. 잘 동작하다가 어느순간 segmentation fault(core dump)가 발생했습니다. 제 예상으로는 dequeue에서 destroy_node를 호출하지 않아서 그런 것 같습니다. 그런데 dequeue안에서 generate_packet함수 호출 이후에 다 사용한 node를 destroy_node로 free하면 프로그램이 아예 동작하지 않더라구요. 언제 node를 free 할 수 있는지 모르겠습니다..

2. dequeue함수에서 print("empty") 부분을 하지 않으면 아예 프로그램이 동작하지 않습니다.. 왜그럴까요?ㅠ

도와주십시오 부탁드립니다.

// threadtest.c

QUEUE* packetpool;
struct nfq_handle* h = NULL;
int fd = -1;

//================ FUNCTIONS =============================================

void* recv_packet()
{
int rv;
char buf[MAX_BUF]; // __attribute__ ((aligned));
while (1) {
if((rv = recv(fd, buf, sizeof(buf), 0)) >= 0)
{
nfq_handle_packet(h, buf, rv);
}
if(rv < 0)
{
continue;
}
}
}

// Previous cb(callback) function
void generate_packet(NODE* popped) {
...
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}

static int cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data) {
enqueue(packetpool, create_node(qh, nfmsg, nfa, data));
return 1;
}

void create_queue(QUEUE** queue) {
(*queue) = (QUEUE*)malloc(sizeof(QUEUE));
(*queue)->front = NULL;
(*queue)->rear = NULL;
(*queue)->count = 0;
}

void destroy_queue(QUEUE* queue) {
while (!is_empty(queue))
{
NODE* popped = dequeue(queue);
destroy_node(popped);
}
free(queue);
}

NODE* create_node(struct nfq_q_handle* qh_data, struct nfgenmsg* nfmsg_data, struct nfq_data* nfa_data, void* data_data) {
NODE* node = (NODE*)malloc(sizeof(NODE));
node->node_qh = qh_data;
node->node_nfmsg = nfmsg_data;
node->node_nfa = nfa_data;
node->node_data = data_data;
return node;
}

void destroy_node(NODE* node) {
free(node);
node = NULL;
}

void enqueue(QUEUE* queue, NODE* node) {
if (queue->front == NULL)
{
queue->front = node;
queue->rear = node;
queue->count++;
}
else
{
queue->rear->next = node;
queue->rear = node;
queue->count++;
}
}

void* dequeue() {
NODE* packet;
while (1)
{
if (packetpool->count > 0)
{
packet = packetpool->front;
if (packetpool->front->next == NULL)
{
packetpool->front = NULL;
packetpool->rear = NULL;
}
else
{
packetpool->front = packetpool->front->next;
}
packetpool->count--;
generate_packet(packet);
//destroy_node(packet); <- 얘를 활성화시키면 프로그램이 동작하지 않습니다.. packet 변수는 어차피 generate_packet 함수 이후에 더이상 사용 안하는데 왜 그럴까요?
}
else
{
//printf("empty\n"); <- 얘를 지우면 아예 동작도 안하더라구요.

}
}
}

int is_empty(QUEUE* queue) {
return (queue->front == NULL);
}

//================ MAIN ==================================================
int main(int argc, char **argv) {
struct nfq_q_handle* qh = NULL;

pthread_t recv_thread;
pthread_t dequeue_thread;

#ifdef LICENCE
system("mount -n -o remount,rw /");
#endif
init_xmsed();
#ifdef LICENCE
if(licence_flag == true) system("mount -n -o remount,ro /");
else printf("licence failed\n");
#endif

h = nfq_open();
if (!h) {
fprintf(stderr, "error during nfq_open()\n");
goto end;
}
if (nfq_unbind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_unbind_pf()\n");
goto end;
}
if (nfq_bind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_bind_pf()\n");
goto end;
}
qh = nfq_create_queue(h, 0, &cb, NULL);
if (!qh) {
fprintf(stderr, "error during nfq_create_queue()\n");
goto end;
}
if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
fprintf(stderr, "can't set packet_copy mode\n");
goto end;
}
fd = nfq_fd(h);

pthread_create(&dequeue_thread, NULL, dequeue, NULL);
pthread_create(&recv_thread, NULL, recv_packet, NULL);

end:
pthread_join(dequeue_thread, NULL);
pthread_join(recv_thread, NULL);
destroy_queue(packetpool);

if (qh) {
nfq_destroy_queue(qh);
}

#ifdef INSANE
/* normally, applications SHOULD NOT issue this command, since
* it detaches other programs/sockets from AF_INET, too ! */
printf("unbinding from AF_INET\n");
if(h)
{
nfq_unbind_pf(h, AF_INET);
}
#endif

if (h) {
nfq_close(h);
}
return 0;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.