ibnetfilter_queue

15 篇文章 0 订阅
6 篇文章 0 订阅


#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);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
cpu_sys_in_millis cpu_user_in_millis merge_threads merge_queue merge_active merge_rejected merge_largest merge_completed bulk_threads bulk_queue bulk_active bulk_rejected bulk_largest bulk_completed warmer_threads warmer_queue warmer_active warmer_rejected warmer_largest warmer_completed get_largest get_completed get_threads get_queue get_active get_rejected index_threads index_queue index_active index_rejected index_largest index_completed suggest_threads suggest_queue suggest_active suggest_rejected suggest_largest suggest_completed fetch_shard_store_queue fetch_shard_store_active fetch_shard_store_rejected fetch_shard_store_largest fetch_shard_store_completed fetch_shard_store_threads management_threads management_queue management_active management_rejected management_largest management_completed percolate_queue percolate_active percolate_rejected percolate_largest percolate_completed percolate_threads listener_active listener_rejected listener_largest listener_completed listener_threads listener_queue search_rejected search_largest search_completed search_threads search_queue search_active fetch_shard_started_threads fetch_shard_started_queue fetch_shard_started_active fetch_shard_started_rejected fetch_shard_started_largest fetch_shard_started_completed refresh_rejected refresh_largest refresh_completed refresh_threads refresh_queue refresh_active optimize_threads optimize_queue optimize_active optimize_rejected optimize_largest optimize_completed snapshot_largest snapshot_completed snapshot_threads snapshot_queue snapshot_active snapshot_rejected generic_threads generic_queue generic_active generic_rejected generic_largest generic_completed flush_threads flush_queue flush_active flush_rejected flush_largest flush_completed server_open rx_count rx_size_in_bytes tx_count tx_size_in_bytes
最新发布
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值