11 #include <netinet/in.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/nf_tables.h>
16 #include <libmnl/libmnl.h>
17 #include <libnftnl/set.h>
19 static int set_cb(
const struct nlmsghdr *nlh,
void *data)
23 uint32_t *type = data;
25 t = nftnl_set_alloc();
31 if (nftnl_set_elems_nlmsg_parse(nlh, t) < 0) {
32 perror(
"nftnl_set_nlmsg_parse");
36 nftnl_set_snprintf(buf,
sizeof(buf), t, *type, 0);
45 int main(
int argc,
char *argv[])
47 struct mnl_socket *nl;
48 char buf[MNL_SOCKET_BUFFER_SIZE];
50 uint32_t portid, seq, family;
51 uint32_t type = NFTNL_OUTPUT_DEFAULT;
52 struct nftnl_set *t = NULL;
55 if (argc < 4 || argc > 5) {
56 fprintf(stderr,
"%s <family> <table> <set>\n", argv[0]);
59 t = nftnl_set_alloc();
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],
"inet") == 0)
70 family = NFPROTO_INET;
71 else if (strcmp(argv[1],
"bridge") == 0)
72 family = NFPROTO_BRIDGE;
73 else if (strcmp(argv[1],
"arp") == 0)
76 fprintf(stderr,
"Unknown family: ip, ip6, inet, bridge, arp\n");
80 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETSETELEM, family,
81 NLM_F_DUMP | NLM_F_ACK, seq);
82 nftnl_set_set_str(t, NFTNL_SET_NAME, argv[3]);
83 nftnl_set_set_str(t, NFTNL_SET_TABLE, argv[2]);
84 nftnl_set_elems_nlmsg_build_payload(nlh, t);
87 nl = mnl_socket_open(NETLINK_NETFILTER);
89 perror(
"mnl_socket_open");
93 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
94 perror(
"mnl_socket_bind");
97 portid = mnl_socket_get_portid(nl);
99 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
100 perror(
"mnl_socket_send");
104 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
106 ret = mnl_cb_run(buf, ret, seq, portid, set_cb, &type);
109 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
115 mnl_socket_close(nl);