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>
23 static int set_cb(
const struct nlmsghdr *nlh,
void *data)
27 uint32_t *type = data;
29 t = nftnl_set_alloc();
35 if (nftnl_set_elems_nlmsg_parse(nlh, t) < 0) {
36 perror(
"nftnl_set_nlmsg_parse");
40 nftnl_set_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;
55 uint32_t type = NFTNL_OUTPUT_DEFAULT;
56 struct nftnl_set *t = NULL;
57 struct nftnl_set_elem *e;
61 if (argc < 4 || argc > 5) {
62 fprintf(stderr,
"%s <family> <table> <set> [<json>]\n",
66 t = nftnl_set_alloc();
72 if (strcmp(argv[1],
"ip") == 0)
73 family = NFPROTO_IPV4;
74 else if (strcmp(argv[1],
"ip6") == 0)
75 family = NFPROTO_IPV6;
76 else if (strcmp(argv[1],
"bridge") == 0)
77 family = NFPROTO_BRIDGE;
78 else if (strcmp(argv[1],
"arp") == 0)
81 fprintf(stderr,
"Unknown family: ip, ip6, bridge, arp\n");
85 if (argc == 5 && strcmp(argv[4],
"json") == 0 )
86 type = NFTNL_OUTPUT_JSON;
88 nlh = nftnl_set_nlmsg_build_hdr(buf, NFT_MSG_GETSETELEM, family,
90 nftnl_set_set(t, NFTNL_SET_NAME, argv[3]);
91 nftnl_set_set(t, NFTNL_SET_TABLE, argv[2]);
94 e = nftnl_set_elem_alloc();
101 nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &data,
sizeof(data));
102 nftnl_set_elem_add(t, e);
104 nftnl_set_elems_nlmsg_build_payload(nlh, t);
107 nl = mnl_socket_open(NETLINK_NETFILTER);
109 perror(
"mnl_socket_open");
113 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
114 perror(
"mnl_socket_bind");
117 portid = mnl_socket_get_portid(nl);
119 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
120 perror(
"mnl_socket_send");
124 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
126 ret = mnl_cb_run(buf, ret, seq, portid, set_cb, &type);
129 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
135 mnl_socket_close(nl);