16#include <netinet/in.h>
18#include <linux/netfilter.h>
19#include <linux/netfilter/nf_tables.h>
21#include <libmnl/libmnl.h>
22#include <libnftnl/object.h>
24static int obj_cb(
const struct nlmsghdr *nlh,
void *data)
28 uint32_t *type = data;
30 t = nftnl_obj_alloc();
36 if (nftnl_obj_nlmsg_parse(nlh, t) < 0) {
37 perror(
"nftnl_obj_nlmsg_parse");
41 nftnl_obj_snprintf(buf,
sizeof(buf), t, *type, 0);
50int main(
int argc,
char *argv[])
52 struct mnl_socket *nl;
53 char buf[MNL_SOCKET_BUFFER_SIZE];
55 uint32_t portid, seq, family;
56 struct nftnl_obj *t = NULL;
58 uint32_t type = NFTNL_OUTPUT_DEFAULT;
60 if (argc < 3 || argc > 5) {
61 fprintf(stderr,
"%s <family> <table> [<obj>]\n", argv[0]);
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],
"unspec") == 0)
72 family = NFPROTO_UNSPEC;
74 fprintf(stderr,
"Unknown family: ip, ip6, inet, unspec");
78 if (argc == 3 || argc == 4) {
79 t = nftnl_obj_alloc();
87 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_TIMEOUT);
89 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
92 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
93 nftnl_obj_nlmsg_build_payload(nlh, t);
97 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
98 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
100 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
102 nftnl_obj_nlmsg_build_payload(nlh, t);
106 nl = mnl_socket_open(NETLINK_NETFILTER);
108 perror(
"mnl_socket_open");
112 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
113 perror(
"mnl_socket_bind");
116 portid = mnl_socket_get_portid(nl);
118 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
119 perror(
"mnl_socket_send");
123 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
125 ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type);
128 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
134 mnl_socket_close(nl);