ibnetfilter_queue

转载 2013年12月02日 17:54:52

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <linux/netfilter.h>		/* for NF_ACCEPT */
#include <errno.h>


#include <libnetfilter_queue/libnetfilter_queue.h>


/* returns packet id */
static u_int32_t print_pkt (struct nfq_data *tb)
{
	int id = 0;
	struct nfqnl_msg_packet_hdr *ph;
	struct nfqnl_msg_packet_hw *hwph;
	u_int32_t mark,ifi; 
	int ret;
	unsigned 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);
	}


	hwph = nfq_get_packet_hw(tb);
	if (hwph) {
		int i, hlen = ntohs(hwph->hw_addrlen);


		printf("hw_src_addr=");
		for (i = 0; i < hlen-1; i++)
			printf("%02x:", hwph->hw_addr[i]);
		printf("%02x ", hwph->hw_addr[hlen-1]);
	}


	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);
	ifi = nfq_get_physindev(tb);
	if (ifi)
		printf("physindev=%u ", ifi);


	ifi = nfq_get_physoutdev(tb);
	if (ifi)
		printf("physoutdev=%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] __attribute__ ((aligned));


	printf("opening library handle\n");
	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");
	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");
	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");
	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);
	}


	fd = nfq_fd(h);


	for (;;) {
		if ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0) {
			printf("pkt received\n");
			nfq_handle_packet(h, buf, rv);
			continue;
		}
		/* if your application is too slow to digest the packets that
		 * are sent from kernel-space, the socket buffer that we use
		 * to enqueue packets may fill up returning ENOBUFS. Depending
		 * on your application, this error may be ignored. Please, see
		 * the doxygen documentation of this library on how to improve
		 * this situation.
		 */
		if (rv < 0 && errno == ENOBUFS) {
			printf("losing packets!\n");
			continue;
		}
		perror("recv failed");
		break;
	}


	printf("unbinding from queue 0\n");
	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");
	nfq_unbind_pf(h, AF_INET);
#endif


	printf("closing library handle\n");
	nfq_close(h);


	exit(0);
}



相关文章推荐

netfilter-libnetfilter_queue

Linux netfilter提供了数据的队列机制,而libnetfilter_queue为用户空间的接收、处理内核队列里缓冲的数据包的函数库。 Libnetfilter_queue需要libnfne...

libnetfilter_queue快速入门

http://chyeh.logdown.com/posts/91924-libnefilter-queueqi-bu-zou 以上为原博客网址。 根据[netfilter的官方網站(http:/...

netfilter_queue

1. 官方资源 官方网址是:http://netfilter.org/projects/libnetfilter_queue/ 截止2017年2月16日,最新版本是1.0.2 , 这个版本的依赖...
  • prahs
  • prahs
  • 2017年02月16日 16:06
  • 499

libnetfilter_queue安装测试

libnetfilter_queue可以实现网络阻断功能,从网卡上直接截取数据包,对开发IPS工具有用处。 安装libnfnetlink、libnetfilter_queue,安装libnetfil...

用netfilter_queue在用户态实现NAT

偶尔在网上看到了>这篇文章, 并结合libnetfilter_queue-0.0.17.tar.bz2中的例子, 然后修改了一下tcp计算checksum部分,在linux2.6.24上用netf...

queue_template

  • 2015年06月01日 20:13
  • 2KB
  • 下载

Message Queue

  • 2015年01月16日 13:41
  • 6.12MB
  • 下载

Java队列Queue、双端队列Deque

http://uule.iteye.com/blog/2095650?utm_source=tuicool 注意:这都只是接口而已   1、Queue API 在ja...

Queue_linklist

  • 2014年11月16日 23:29
  • 2KB
  • 下载

1143.Printer Queue

  • 2013年01月18日 13:54
  • 1KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ibnetfilter_queue
举报原因:
原因补充:

(最多只允许输入30个字)