fix build of utils against local libtool library
[platform/upstream/libnetfilter_queue.git] / utils / nfqnl_test.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <netinet/in.h>
6 #include <linux/netfilter.h>            /* for NF_ACCEPT */
7
8 #include <libnfnetlink_queue/libnfnetlink_queue.h>
9
10 /* returns packet id */
11 static u_int32_t print_pkt (struct nfattr *tb[])
12 {
13         int id = 0;
14         struct nfqnl_msg_packet_hdr *ph;
15         u_int32_t mark,ifi; 
16         int ret;
17         unsigned int datalength;
18         char * data;
19         
20         ph = nfqnl_get_msg_packet_hdr(tb);
21         if (ph){
22                 id = ntohl(ph->packet_id);
23                 printf("hw_protocol=0x%04x hook=%u id=%u ",
24                         ntohs(ph->hw_protocol), ph->hook, id);
25         }
26         
27         mark = nfqnl_get_nfmark(tb);
28         if (mark)
29                 printf("mark=%u ", mark);
30
31         ifi = nfqnl_get_indev(tb);
32         if (ifi)
33                 printf("indev=%u ", ifi);
34
35         ifi = nfqnl_get_outdev(tb);
36         if (ifi)
37                 printf("outdev=%u ", ifi);
38
39         ret = nfqnl_get_payload(tb, &data, &datalength);
40         if (ret)
41                 printf("payload_len=%d ", datalength);
42
43         fputc('\n', stdout);
44
45         return id;
46 }
47         
48
49 static int cb(struct nfqnl_q_handle *qh, struct nfgenmsg *nfmsg,
50               struct nfattr *nfa[], void *data)
51 {
52         u_int32_t id = print_pkt(nfa);
53         printf("entering callback\n");
54         return nfqnl_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
55 }
56
57 int main(int argc, char **argv)
58 {
59         struct nfqnl_handle *h;
60         struct nfqnl_q_handle *qh;
61         struct nfnl_handle *nh;
62         int fd;
63         int rv;
64         char buf[4096];
65
66         printf("opening library handle\n");
67         h = nfqnl_open();
68         if (!h) {
69                 fprintf(stderr, "error during nfqnl_open()\n");
70                 exit(1);
71         }
72
73         printf("unbinding existing nf_queue handler for AF_INET (if any)\n");
74         if (nfqnl_unbind_pf(h, AF_INET) < 0) {
75                 fprintf(stderr, "error during nfqnl_unbind_pf()\n");
76                 exit(1);
77         }
78
79         printf("binding nfnetlink_queue as nf_queue handler for AF_INET\n");
80         if (nfqnl_bind_pf(h, AF_INET) < 0) {
81                 fprintf(stderr, "error during nfqnl_bind_pf()\n");
82                 exit(1);
83         }
84
85         printf("binding this socket to queue '0'\n");
86         qh = nfqnl_create_queue(h,  0, &cb, NULL);
87         if (!qh) {
88                 fprintf(stderr, "error during nfqnl_create_queue()\n");
89                 exit(1);
90         }
91
92         printf("setting copy_packet mode\n");
93         if (nfqnl_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
94                 fprintf(stderr, "can't set packet_copy mode\n");
95                 exit(1);
96         }
97
98         nh = nfqnl_nfnlh(h);
99         fd = nfnl_fd(nh);
100
101         while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
102                 printf("pkt received\n");
103                 nfqnl_handle_packet(h, buf, rv);
104         }
105
106         printf("unbinding from queue 0\n");
107         nfqnl_destroy_queue(qh);
108
109 #ifdef INSANE
110         /* normally, applications SHOULD NOT issue this command, since
111          * it detaches other programs/sockets from AF_INET, too ! */
112         printf("unbinding from AF_INET\n");
113         nfqnl_unbind_pf(h, AF_INET);
114 #endif
115
116         printf("closing library handle\n");
117         nfqnl_close(h);
118
119         exit(0);
120 }