20 #include <netinet/in.h>
21 #include <sys/types.h>
26 #include <linux/netfilter/nf_tables.h>
28 #include <libmnl/libmnl.h>
29 #include <libnftnl/table.h>
31 int main(
int argc,
char *argv[])
33 struct mnl_socket *nl;
34 char buf[MNL_SOCKET_BUFFER_SIZE];
37 struct nft_table *t = NULL;
42 struct nft_parse_err *err;
45 printf(
"Usage: %s <json-file>\n", argv[0]);
49 fd = open(argv[1], O_RDONLY);
55 if (read(fd, json,
sizeof(json)) < 0) {
62 t = nft_table_alloc();
68 err = nft_parse_err_alloc();
74 if (nft_table_parse(t, NFT_PARSE_JSON, json, err) < 0) {
75 nft_parse_perror(
"Unable to parse JSON file", err);
79 nft_table_snprintf(reprint,
sizeof(reprint), t, NFT_OUTPUT_JSON, 0);
80 printf(
"Parsed:\n%s\n", reprint);
82 family = nft_table_attr_get_u32(t, NFT_TABLE_ATTR_FAMILY);
86 nlh = nft_table_nlmsg_build_hdr(buf, NFT_MSG_NEWTABLE, family,
87 NLM_F_CREATE|NLM_F_ACK, seq);
88 nft_table_nlmsg_build_payload(nlh, t);
90 nft_parse_err_free(err);
92 nl = mnl_socket_open(NETLINK_NETFILTER);
94 perror(
"mnl_socket_open");
98 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
99 perror(
"mnl_socket_bind");
102 portid = mnl_socket_get_portid(nl);
104 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
105 perror(
"mnl_socket_send");
109 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
111 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
114 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
121 mnl_socket_close(nl);