12#include <netinet/in.h>
14#include <linux/netfilter.h>
15#include <linux/netfilter/nf_tables.h>
17#include <libmnl/libmnl.h>
18#include <libnftnl/object.h>
20static int obj_cb(
const struct nlmsghdr *nlh,
void *data)
24 uint32_t *type = data;
26 t = nftnl_obj_alloc();
32 if (nftnl_obj_nlmsg_parse(nlh, t) < 0) {
33 perror(
"nftnl_obj_nlmsg_parse");
37 nftnl_obj_snprintf(buf,
sizeof(buf), t, *type, 0);
46int main(
int argc,
char *argv[])
48 struct mnl_socket *nl;
49 char buf[MNL_SOCKET_BUFFER_SIZE];
51 uint32_t portid, seq, family;
52 struct nftnl_obj *t = NULL;
54 uint32_t type = NFTNL_OUTPUT_DEFAULT;
56 if (argc < 3 || argc > 5) {
57 fprintf(stderr,
"%s <family> <table> [<obj>]\n",
62 if (strcmp(argv[1],
"ip") == 0)
63 family = NFPROTO_IPV4;
64 else if (strcmp(argv[1],
"ip6") == 0)
65 family = NFPROTO_IPV6;
66 else if (strcmp(argv[1],
"inet") == 0)
67 family = NFPROTO_INET;
68 else if (strcmp(argv[1],
"unspec") == 0)
69 family = NFPROTO_UNSPEC;
71 fprintf(stderr,
"Unknown family: ip, ip6, inet, unspec");
75 if (argc == 3 || argc == 4) {
76 t = nftnl_obj_alloc();
84 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_HELPER);
86 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
89 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
90 nftnl_obj_nlmsg_build_payload(nlh, t);
94 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
95 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
97 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
99 nftnl_obj_nlmsg_build_payload(nlh, t);
103 nl = mnl_socket_open(NETLINK_NETFILTER);
105 perror(
"mnl_socket_open");
109 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
110 perror(
"mnl_socket_bind");
113 portid = mnl_socket_get_portid(nl);
115 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
116 perror(
"mnl_socket_send");
120 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
122 ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type);
125 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
131 mnl_socket_close(nl);