首页 > 解决方案 > 使用 C netfilter lib 列出自定义链

问题描述

我正在尝试在 netfilter 中列出自定义链,但是当在 Debian 11 上运行时,以下代码只给了我过滤表中的默认链。在 Ubuntu 20.04 上,它也列出了其他链。谁能告诉我我做错了什么?先感谢您。

外壳:Debian 11,内核 5.10.0-8-amd64,iptables 1.8.7-1

iptables -N TEST
./listchains
Chain name is INPUT
Chain name is FORWARD
Chain name is OUTPUT

Ubuntu 20.04,内核 5.4.0-40-generic,iptables 1.8.4-3ubuntu2

iptables -N TEST
./listchains
Chain name is INPUT
Chain name is FORWARD
Chain name is OUTPUT
Chain name is TEST

C代码:

// compile with gcc -Wall -o listchains listchains.c -lip4tc
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/errno.h>
#include <libiptc/libiptc.h>

int main(void) {
 struct xtc_handle *h;
 const char *chain = NULL;
 const char *tablename = "filter";

 h = iptc_init(tablename);
  if (!h) {
   printf("Error initializing: %s\n", iptc_strerror(errno));
   exit(errno);
 }

  chain = iptc_first_chain(h);
  while (chain) {
   printf("Chain name is %s\n", chain);
   chain = iptc_next_chain(h);
 }

}

标签: cnetfilter

解决方案


推荐阅读