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/set.h>
23static int set_cb(
const struct nlmsghdr *nlh,
void *data)
27 uint32_t *type = data;
29 t = nftnl_set_alloc();
35 if (nftnl_set_nlmsg_parse(nlh, t) < 0) {
36 perror(
"nftnl_set_nlmsg_parse");
40 nftnl_set_snprintf(buf,
sizeof(buf), t, *type, 0);
49int main(
int argc,
char *argv[])
51 struct mnl_socket *nl;
52 char buf[MNL_SOCKET_BUFFER_SIZE];
54 uint32_t portid, seq, family;
55 uint32_t type = NFTNL_OUTPUT_DEFAULT;
56 struct nftnl_set *t = NULL;
59 if (argc < 2 || argc > 3) {
60 fprintf(stderr,
"%s <family>\n", argv[0]);
63 t = nftnl_set_alloc();
69 if (strcmp(argv[1],
"ip") == 0)
70 family = NFPROTO_IPV4;
71 else if (strcmp(argv[1],
"ip6") == 0)
72 family = NFPROTO_IPV6;
73 else if (strcmp(argv[1],
"inet") == 0)
74 family = NFPROTO_INET;
75 else if (strcmp(argv[1],
"bridge") == 0)
76 family = NFPROTO_BRIDGE;
77 else if (strcmp(argv[1],
"arp") == 0)
79 else if (strcmp(argv[1],
"unspec") == 0)
80 family = NFPROTO_UNSPEC;
82 fprintf(stderr,
"Unknown family: ip, ip6, inet, bridge, arp, unspec\n");
86 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETSET, family,
87 NLM_F_DUMP | NLM_F_ACK, seq);
90 nftnl_set_nlmsg_build_payload(nlh, t);
93 nl = mnl_socket_open(NETLINK_NETFILTER);
95 perror(
"mnl_socket_open");
99 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
100 perror(
"mnl_socket_bind");
103 portid = mnl_socket_get_portid(nl);
105 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
106 perror(
"mnl_socket_send");
110 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
112 ret = mnl_cb_run(buf, ret, seq, portid, set_cb, &type);
115 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
121 mnl_socket_close(nl);