我一直在尝试以编程方式添加netfilter规则,而不用调用iptables二进制文件。除了libipt的头文件之外,最接近我发现的文档的是here。奇怪的是,文档中没有任何内容描述我想要做什么。
ipt_entry
结构底部的ip_tables.h中是以下代码:
/* The matches (if any),then the target. */
unsigned char elems[0];
但是,我还没有找到正确读取或修改elems
的任何方法。
我还尝试通过iptables iptables -A INPUT -p tcp --dest 192.168.1.10 --dport 8080 -j accEPT
添加规则,然后运行一个调用libipt的程序,如下所示:ipt_entry *rule = iptc_first_rule(chain,ipthandle);
。我在GDB中运行它,然后在下面的行中中断了
iptc_first_rule
。转储ipt_entry结构可以得到以下信息:
(gdb) p *rule
$4 = {
ip = {
src = {
s_addr = 0
},dst = {
s_addr = 167880896
},smsk = {
s_addr = 0
},dmsk = {
s_addr = 4294967295
},iniface = '\000' <repeats 15 times>,outiface = '\000' <repeats 15 times>,iniface_mask = '\000' <repeats 15 times>,outiface_mask = '\000' <repeats 15 times>,proto = 6,flags = 0 '\000',invflags = 0 '\000'
},nfcache = 0,target_offset = 160,next_offset = 200,comefrom = 2,counters = {
pcnt = 0,bcnt = 0
},elems = 0x77e85b58 "0"
}
我看到elems
成员中有某物,所以我也尝试将其转储:
(gdb) x /10s rule->elems
0x77e85b58: "0"
0x77e85b5a: "tcp"
0x77e85b5e: "%s"
0x77e85b61: ""
0x77e85b62: ""
0x77e85b63: ""
0x77e85b64: "%s(): set IP_HDRINC"
0x77e85b78: ""
0x77e85b79: ""
0x77e85b7a: "\377\377P"
最后,我尝试查看iptables的源代码。但是,我不得不停下来看看意大利面条汤,然后才将自己扔出最近的窗户。
您如何在此指定规则的目标端口?
编辑:我忘了提一下,目标平台是openWRT,它不提供任何较新的netfilter库。我坚持使用libipt / xtables。