tshark现在的数据包获取方式有两种,分别是读文件、网口监听(af-packet原始套接字)。两种方式在包获取上,都是通过读文件的形式;存在文件io操作,在专门处理大流量的情境下, 我们复用wireshark去做功能开发是不适合的,在文件io这部分很消耗性能。
此前什么iptables和转发nat需要配置
准备工作
../build/CMakeFiles/tshark.dir/link.txt 链接-lnetfilter_queue库
通过iptables提供数据包,跳过文件读写的过程,可以大大提升性能(没有进行性能测试)
一、将iptables加到收包方式中
在原始代码中,real_main接口为tshark工具的入口函数,前半部分都是参数解析(我们不关注),在中间部分找到,收包代码;
其中第一个条件if (cf_name)这里是读文件的形式,使用是通过-r *.pcap判断的,第二个else位置,里面是capture用来监听网口通过-I eth0使用。
追加第三个iptables收包方式。
直接固定通过iptables收包。
cf_init_iptables接口完成初始化工作,cf是全局的数据,数据包信息,数据包状态相关,比如处理多少,丢掉多少,提供的解码工具等外来数据。里面还加了一个wtap空间,这里是关联到每个数据包的,保存数据包内容。
cf_status_t cf_init_iptables(capture_file *cf,unsigned int type, gboolean is_tempfile, int *err) { wtap *wth; gchar *err_info; wth = iptables_get_wtap_init();
/* The open succeeded. Fill in the information for this file. */ /* Create new epan session for dissection. */ epan_free(cf->epan); cf->epan = tshark_epan_new(cf); cf->provider.wth = wth; cf->f_datalen = 0; /* not used, but set it anyway */ /* Set the file name because we need it to set the follow stream filter. XXX - is that |