libnftnl 1.2.9
nft-table-get.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
6 */
7
8#include <stdlib.h>
9#include <time.h>
10#include <string.h>
11#include <netinet/in.h>
12
13#include <linux/netfilter.h>
14#include <linux/netfilter/nf_tables.h>
15
16#include <libmnl/libmnl.h>
17#include <libnftnl/table.h>
18
19static int table_cb(const struct nlmsghdr *nlh, void *data)
20{
21 struct nftnl_table *t;
22 char buf[4096];
23 uint32_t *type = data;
24
25 t = nftnl_table_alloc();
26 if (t == NULL) {
27 perror("OOM");
28 goto err;
29 }
30
31 if (nftnl_table_nlmsg_parse(nlh, t) < 0) {
32 perror("nftnl_table_nlmsg_parse");
33 goto err_free;
34 }
35
36 nftnl_table_snprintf(buf, sizeof(buf), t, *type, 0);
37 printf("%s\n", buf);
38
39err_free:
40 nftnl_table_free(t);
41err:
42 return MNL_CB_OK;
43}
44
45int main(int argc, char *argv[])
46{
47 struct mnl_socket *nl;
48 char buf[MNL_SOCKET_BUFFER_SIZE];
49 struct nlmsghdr *nlh;
50 uint32_t portid, seq, family;
51 struct nftnl_table *t = NULL;
52 int ret;
53 uint32_t type = NFTNL_OUTPUT_DEFAULT;
54
55 if (argc < 2 || argc > 4) {
56 fprintf(stderr, "%s <family> [<table>]\n", argv[0]);
57 return EXIT_FAILURE;
58 }
59
60 if (strcmp(argv[1], "ip") == 0)
61 family = NFPROTO_IPV4;
62 else if (strcmp(argv[1], "ip6") == 0)
63 family = NFPROTO_IPV6;
64 else if (strcmp(argv[1], "inet") == 0)
65 family = NFPROTO_INET;
66 else if (strcmp(argv[1], "bridge") == 0)
67 family = NFPROTO_BRIDGE;
68 else if (strcmp(argv[1], "arp") == 0)
69 family = NFPROTO_ARP;
70 else if (strcmp(argv[1], "unspec") == 0)
71 family = NFPROTO_UNSPEC;
72 else {
73 fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp, unspec\n");
74 exit(EXIT_FAILURE);
75 }
76
77 if (argc == 3) {
78 t = nftnl_table_alloc();
79 if (t == NULL) {
80 perror("OOM");
81 exit(EXIT_FAILURE);
82 }
83 }
84
85 seq = time(NULL);
86 if (t == NULL) {
87 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family,
88 NLM_F_DUMP, seq);
89 } else {
90 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family,
91 NLM_F_ACK, seq);
92 nftnl_table_set_str(t, NFTNL_TABLE_NAME, argv[2]);
93 nftnl_table_nlmsg_build_payload(nlh, t);
94 nftnl_table_free(t);
95 }
96
97 nl = mnl_socket_open(NETLINK_NETFILTER);
98 if (nl == NULL) {
99 perror("mnl_socket_open");
100 exit(EXIT_FAILURE);
101 }
102
103 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
104 perror("mnl_socket_bind");
105 exit(EXIT_FAILURE);
106 }
107 portid = mnl_socket_get_portid(nl);
108
109 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
110 perror("mnl_socket_send");
111 exit(EXIT_FAILURE);
112 }
113
114 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
115 while (ret > 0) {
116 ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type);
117 if (ret <= 0)
118 break;
119 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
120 }
121 if (ret == -1) {
122 perror("error");
123 exit(EXIT_FAILURE);
124 }
125 mnl_socket_close(nl);
126
127 return EXIT_SUCCESS;
128}