forked from ZhangYi111/practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nflog_test.c
executable file
·167 lines (138 loc) · 4.1 KB
/
nflog_test.c
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
/*
源码里面给的例子,加了个数据包打印
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <libnetfilter_log/libnetfilter_log.h>
void lprint_pkt(unsigned char *pkt, int len, char *title)
{
char hex[64] = {0};
char assic[64] = {0};
char *p_hex = hex, *p_assic = assic;
int i = 0;
printf("*******************%s start*****************\n", title);
while(len--)
{
if(i != 0 && !(i%16))
{
printf("%s %s\n", hex, assic);
memset(hex, 0x0, sizeof(hex));
memset(assic, 0x0, sizeof(assic));
p_hex = hex;
p_assic = assic;
i = 0;
}
sprintf(p_hex, "%02x ", *pkt);
p_hex += 3;
sprintf(p_assic, "%c", *pkt < 32 || *pkt > 127? '.' : *pkt);
p_assic += 1;
i++;
pkt++;
if(!(i%8))
{
sprintf(p_hex, " ");
p_hex += 2;
sprintf(p_assic, " ");
p_assic += 2;
}
}
printf("%s %s\n", hex, assic);
printf("\n\n*******************%s end*****************\n", title);
}
static int print_pkt(struct nflog_data *ldata)
{
struct nfulnl_msg_packet_hdr *ph = nflog_get_msg_packet_hdr(ldata);
u_int32_t mark = nflog_get_nfmark(ldata);
u_int32_t indev = nflog_get_indev(ldata);
u_int32_t outdev = nflog_get_outdev(ldata);
char *prefix = nflog_get_prefix(ldata);
char *payload;
int payload_len = nflog_get_payload(ldata, &payload);
if (ph) {
printf("hw_protocol=0x%04x hook=%u ",
ntohs(ph->hw_protocol), ph->hook);
}
printf("mark=%u ", mark);
if (indev > 0)
printf("indev=%u ", indev);
if (outdev > 0)
printf("outdev=%u ", outdev);
if (prefix) {
printf("prefix=\"%s\" ", prefix);
}
if (payload_len >= 0)
printf("payload_len=%d ", payload_len);
fputc('\n', stdout);
lprint_pkt(payload, payload_len, "payload");
return 0;
}
static int cb(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg,
struct nflog_data *nfa, void *data)
{
print_pkt(nfa);
return 0;
}
int main(int argc, char **argv)
{
struct nflog_handle *h;
struct nflog_g_handle *qh;
struct nflog_g_handle *qh100;
int rv, fd;
char buf[4096];
h = nflog_open();
if (!h) {
fprintf(stderr, "error during nflog_open()\n");
exit(1);
}
printf("unbinding existing nf_log handler for AF_INET (if any)\n");
if (nflog_unbind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error nflog_unbind_pf()\n");
exit(1);
}
printf("binding nfnetlink_log to AF_INET\n");
if (nflog_bind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nflog_bind_pf()\n");
exit(1);
}
printf("binding this socket to group 1\n");
qh = nflog_bind_group(h, 1);
if (!qh) {
fprintf(stderr, "no handle for grup 1\n");
exit(1);
}
printf("binding this socket to group 100\n");
qh100 = nflog_bind_group(h, 100);
if (!qh100) {
fprintf(stderr, "no handle for group 100\n");
exit(1);
}
printf("setting copy_packet mode\n");
if (nflog_set_mode(qh, NFULNL_COPY_PACKET, 0xffff) < 0) {
fprintf(stderr, "can't set packet copy mode\n");
exit(1);
}
fd = nflog_fd(h);
printf("registering callback for group 0\n");
nflog_callback_register(qh, &cb, NULL);
printf("going into main loop\n");
while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
printf("pkt received (len=%u)\n", rv);
/* handle messages in just-received packet */
nflog_handle_packet(h, buf, rv);
}
printf("unbinding from group 100\n");
nflog_unbind_group(qh100);
printf("unbinding from group 0\n");
nflog_unbind_group(qh);
#ifdef INSANE
/* norally, applications SHOULD NOT issue this command,
* since it detaches other programs/sockets from AF_INET, too ! */
printf("unbinding from AF_INET\n");
nflog_unbind_pf(h, AF_INET);
#endif
printf("closing handle\n");
nflog_close(h);
return EXIT_SUCCESS;
}