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/object.h>
23static int obj_cb(
const struct nlmsghdr *nlh,
void *data)
27 uint32_t *type = data;
29 t = nftnl_obj_alloc();
35 if (nftnl_obj_nlmsg_parse(nlh, t) < 0) {
36 perror(
"nftnl_obj_nlmsg_parse");
40 nftnl_obj_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 struct nftnl_obj *t = NULL;
57 uint32_t type = NFTNL_OUTPUT_DEFAULT;
59 if (argc < 2 || argc > 5) {
60 fprintf(stderr,
"%s <family> <table> [<obj>]\n", argv[0]);
64 if (strcmp(argv[1],
"ip") == 0)
65 family = NFPROTO_IPV4;
66 else if (strcmp(argv[1],
"ip6") == 0)
67 family = NFPROTO_IPV6;
68 else if (strcmp(argv[1],
"inet") == 0)
69 family = NFPROTO_INET;
70 else if (strcmp(argv[1],
"bridge") == 0)
71 family = NFPROTO_BRIDGE;
72 else if (strcmp(argv[1],
"arp") == 0)
74 else if (strcmp(argv[1],
"unspec") == 0)
75 family = NFPROTO_UNSPEC;
77 fprintf(stderr,
"Unknown family: ip, ip6, inet, bridge, arp, unspec\n");
81 if (argc == 3 || argc == 4) {
82 t = nftnl_obj_alloc();
91 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
94 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
95 nftnl_obj_nlmsg_build_payload(nlh, t);
99 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
100 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
101 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_COUNTER);
103 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
105 nftnl_obj_nlmsg_build_payload(nlh, t);
109 nl = mnl_socket_open(NETLINK_NETFILTER);
111 perror(
"mnl_socket_open");
115 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
116 perror(
"mnl_socket_bind");
119 portid = mnl_socket_get_portid(nl);
121 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
122 perror(
"mnl_socket_send");
126 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
128 ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type);
131 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
137 mnl_socket_close(nl);