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",
66 if (strcmp(argv[1],
"ip") == 0)
67 family = NFPROTO_IPV4;
68 else if (strcmp(argv[1],
"ip6") == 0)
69 family = NFPROTO_IPV6;
70 else if (strcmp(argv[1],
"inet") == 0)
71 family = NFPROTO_INET;
72 else if (strcmp(argv[1],
"unspec") == 0)
73 family = NFPROTO_UNSPEC;
75 fprintf(stderr,
"Unknown family: ip, ip6, inet, unspec");
79 if (argc == 3 || argc == 4) {
80 t = nftnl_obj_alloc();
88 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_HELPER);
90 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
93 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
94 nftnl_obj_nlmsg_build_payload(nlh, t);
98 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
99 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
101 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
103 nftnl_obj_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, obj_cb, &type);
129 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
135 mnl_socket_close(nl);