libnftnl  1.2.9
nft-set-elem-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/set.h>
18 
19 static int set_cb(const struct nlmsghdr *nlh, void *data)
20 {
21  struct nftnl_set *t;
22  char buf[4096];
23  uint32_t *type = data;
24 
25  t = nftnl_set_alloc();
26  if (t == NULL) {
27  perror("OOM");
28  goto err;
29  }
30 
31  if (nftnl_set_elems_nlmsg_parse(nlh, t) < 0) {
32  perror("nftnl_set_nlmsg_parse");
33  goto err_free;
34  }
35 
36  nftnl_set_snprintf(buf, sizeof(buf), t, *type, 0);
37  printf("%s\n", buf);
38 
39 err_free:
40  nftnl_set_free(t);
41 err:
42  return MNL_CB_OK;
43 }
44 
45 int 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  uint32_t type = NFTNL_OUTPUT_DEFAULT;
52  struct nftnl_set *t = NULL;
53  int ret;
54 
55  if (argc < 4 || argc > 5) {
56  fprintf(stderr, "%s <family> <table> <set>\n", argv[0]);
57  return EXIT_FAILURE;
58  }
59  t = nftnl_set_alloc();
60  if (t == NULL) {
61  perror("OOM");
62  exit(EXIT_FAILURE);
63  }
64  seq = time(NULL);
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], "bridge") == 0)
72  family = NFPROTO_BRIDGE;
73  else if (strcmp(argv[1], "arp") == 0)
74  family = NFPROTO_ARP;
75  else {
76  fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
77  exit(EXIT_FAILURE);
78  }
79 
80  nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETSETELEM, family,
81  NLM_F_DUMP | NLM_F_ACK, seq);
82  nftnl_set_set_str(t, NFTNL_SET_NAME, argv[3]);
83  nftnl_set_set_str(t, NFTNL_SET_TABLE, argv[2]);
84  nftnl_set_elems_nlmsg_build_payload(nlh, t);
85  nftnl_set_free(t);
86 
87  nl = mnl_socket_open(NETLINK_NETFILTER);
88  if (nl == NULL) {
89  perror("mnl_socket_open");
90  exit(EXIT_FAILURE);
91  }
92 
93  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
94  perror("mnl_socket_bind");
95  exit(EXIT_FAILURE);
96  }
97  portid = mnl_socket_get_portid(nl);
98 
99  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
100  perror("mnl_socket_send");
101  exit(EXIT_FAILURE);
102  }
103 
104  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
105  while (ret > 0) {
106  ret = mnl_cb_run(buf, ret, seq, portid, set_cb, &type);
107  if (ret <= 0)
108  break;
109  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
110  }
111  if (ret == -1) {
112  perror("error");
113  exit(EXIT_FAILURE);
114  }
115  mnl_socket_close(nl);
116 
117  return EXIT_SUCCESS;
118 }