15 #include <netinet/in.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
20 #include <libmnl/libmnl.h>
21 #include <libnftnl/table.h>
23 static int table_cb(
const struct nlmsghdr *nlh,
void *data)
27 uint32_t *type = data;
29 t = nftnl_table_alloc();
35 if (nftnl_table_nlmsg_parse(nlh, t) < 0) {
36 perror(
"nftnl_table_nlmsg_parse");
40 nftnl_table_snprintf(buf,
sizeof(buf), t, *type, 0);
49 int main(
int argc,
char *argv[])
51 struct mnl_socket *nl;
52 char buf[MNL_SOCKET_BUFFER_SIZE];
54 uint32_t portid, seq, family;
57 uint32_t type = NFTNL_OUTPUT_DEFAULT;
59 if (argc < 2 || argc > 4) {
60 fprintf(stderr,
"%s <family> [<table>] [<default|json>]\n",
65 if (strcmp(argv[1],
"ip") == 0)
66 family = NFPROTO_IPV4;
67 else if (strcmp(argv[1],
"ip6") == 0)
68 family = NFPROTO_IPV6;
69 else if (strcmp(argv[1],
"bridge") == 0)
70 family = NFPROTO_BRIDGE;
71 else if (strcmp(argv[1],
"arp") == 0)
73 else if (strcmp(argv[1],
"unspec") == 0)
74 family = NFPROTO_UNSPEC;
76 fprintf(stderr,
"Unknown family: ip, ip6, bridge, arp, unspec\n");
80 if (strcmp(argv[argc-1],
"json") == 0) {
81 type = NFTNL_OUTPUT_JSON;
84 }
else if (strcmp(argv[argc - 1],
"default") == 0) {
89 t = nftnl_table_alloc();
98 nlh = nftnl_table_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family,
101 nlh = nftnl_table_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family,
103 nftnl_table_set(t, NFTNL_TABLE_NAME, argv[2]);
104 nftnl_table_nlmsg_build_payload(nlh, t);
108 nl = mnl_socket_open(NETLINK_NETFILTER);
110 perror(
"mnl_socket_open");
114 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
115 perror(
"mnl_socket_bind");
118 portid = mnl_socket_get_portid(nl);
120 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
121 perror(
"mnl_socket_send");
125 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
127 ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type);
130 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
136 mnl_socket_close(nl);