#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/netfilter.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
static u_int32_t print_pkt (struct nfq_data *tb)
{
int id = 0;
struct nfqnl_msg_packet_hdr *ph;
u_int32_t mark,ifi;
int ret;
char *data;
ph = nfq_get_msg_packet_hdr(tb);
if (ph){
id = ntohl(ph->packet_id);
printf("hw_protocol=0x%04x hook=%u id=%u ",
ntohs(ph->hw_protocol), ph->hook, id);
}
mark = nfq_get_nfmark(tb);
if (mark)
printf("mark=%u ", mark);
ifi = nfq_get_indev(tb);
if (ifi)
printf("indev=%u ", ifi);
ifi = nfq_get_outdev(tb);
if (ifi)
printf("outdev=%u ", ifi);
ret = nfq_get_payload(tb, &data);
if (ret >= 0)
printf("payload_len=%d ", ret);
fputc('/n', stdout);
return id;
}
static int cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
struct nfq_data *nfa, void *data)
{
// 数据包处理
u_int32_t id = print_pkt(nfa);
printf("entering callback/n");
// 设置裁定
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}
int main(int argc, char **argv)
{
struct nfq_handle *h;
struct nfq_q_handle *qh;
struct nfnl_handle *nh;
int fd;
int rv;
char buf[4096];
printf("opening library handle/n");
// 打开nfq_handle
h = nfq_open();
if (!h) {
fprintf(stderr, "error during nfq_open()/n");
exit(1);
}
printf("unbinding existing nf_queue handler for AF_INET (if any)/n");
// 先解开和AF_INET的绑定
if (nfq_unbind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_unbind_pf()/n");
exit(1);
}
printf("binding nfnetlink_queue as nf_queue handler for AF_INET/n");
// 绑定到AF_INET
if (nfq_bind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_bind_pf()/n");
exit(1);
}
printf("binding this socket to queue '0'/n");
// 建立nfq_q_handle, 号码是0, 回调函数是cb
// 可建立多个queue,用不同的号码区分即可
qh = nfq_create_queue(h, 0, &cb, NULL);
if (!qh) {
fprintf(stderr, "error during nfq_create_queue()/n");
exit(1);
}
printf("setting copy_packet mode/n");
// 设置数据拷贝模式, 全包拷贝
if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
fprintf(stderr, "can't set packet_copy mode/n");
exit(1);
}
nh = nfq_nfnlh(h);
fd = nfnl_fd(nh);
while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
printf("pkt received/n");
nfq_handle_packet(h, buf, rv);
}
printf("unbinding from queue 0/n");
// 释放队列
nfq_destroy_queue(qh);
#ifdef INSANE
printf("unbinding from AF_INET/n");
nfq_unbind_pf(h, AF_INET);
#endif
printf("closing library handle/n");
// 关闭nfq_handle
nfq_close(h);
exit(0);
}
上面这段经典的代码是在哪一段捕捉到外部的packet请求的?虽然对于任何请求,似乎都给予了接受的裁定。。钱不多,发发善心好不好。。。
Netfilter-queue-library, 下面这段经典的代码是在哪一段捕捉到外部的packet请求的?
答案:1 悬赏:60 手机版
解决时间 2021-02-28 22:22
- 提问者网友:我们很暧昧
- 2021-02-28 18:33
最佳答案
- 五星知识达人网友:想偏头吻你
- 2021-02-28 19:39
while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0)收包保存在buf了;
在nfq_handle_packet函数实现会调用qh = nfq_create_queue(h, 0, &cb, NULL) 设置的回调函数cb,最后在回调函数处理nfq_data数据。
在nfq_handle_packet函数实现会调用qh = nfq_create_queue(h, 0, &cb, NULL) 设置的回调函数cb,最后在回调函数处理nfq_data数据。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯