libnftnl  1.0.6
nft-set-get.c
1 /*
2  * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
10  */
11 
12 #include <stdlib.h>
13 #include <time.h>
14 #include <string.h>
15 #include <netinet/in.h>
16 
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
19 
20 #include <libmnl/libmnl.h>
21 #include <libnftnl/set.h>
22 
23 static int set_cb(const struct nlmsghdr *nlh, void *data)
24 {
25  struct nftnl_set *t;
26  char buf[4096];
27  uint32_t *type = data;
28 
29  t = nftnl_set_alloc();
30  if (t == NULL) {
31  perror("OOM");
32  goto err;
33  }
34 
35  if (nftnl_set_nlmsg_parse(nlh, t) < 0) {
36  perror("nftnl_set_nlmsg_parse");
37  goto err_free;
38  }
39 
40  nftnl_set_snprintf(buf, sizeof(buf), t, *type, 0);
41  printf("%s\n", buf);
42 
43 err_free:
44  nftnl_set_free(t);
45 err:
46  return MNL_CB_OK;
47 }
48 
49 int main(int argc, char *argv[])
50 {
51  struct mnl_socket *nl;
52  char buf[MNL_SOCKET_BUFFER_SIZE];
53  struct nlmsghdr *nlh;
54  uint32_t portid, seq, family;
55  uint32_t type = NFTNL_OUTPUT_DEFAULT;
56  struct nftnl_set *t = NULL;
57  int ret;
58 
59  if (argc < 2 || argc > 3) {
60  fprintf(stderr, "%s <family> [<json|xml>]\n", argv[0]);
61  return EXIT_FAILURE;
62  }
63  t = nftnl_set_alloc();
64  if (t == NULL) {
65  perror("OOM");
66  exit(EXIT_FAILURE);
67  }
68  seq = time(NULL);
69  if (strcmp(argv[1], "ip") == 0)
70  family = NFPROTO_IPV4;
71  else if (strcmp(argv[1], "ip6") == 0)
72  family = NFPROTO_IPV6;
73  else if (strcmp(argv[1], "bridge") == 0)
74  family = NFPROTO_BRIDGE;
75  else if (strcmp(argv[1], "arp") == 0)
76  family = NFPROTO_ARP;
77  else if (strcmp(argv[1], "unspec") == 0)
78  family = NFPROTO_UNSPEC;
79  else {
80  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp, unspec\n");
81  exit(EXIT_FAILURE);
82  }
83 
84  if (argc == 3 && strcmp(argv[2], "json") == 0)
85  type = NFTNL_OUTPUT_JSON;
86  else if (argc == 3 && strcmp(argv[2], "xml") == 0)
87  type = NFTNL_OUTPUT_XML;
88 
89  nlh = nftnl_set_nlmsg_build_hdr(buf, NFT_MSG_GETSET, family,
90  NLM_F_DUMP|NLM_F_ACK, seq);
91  /* Use this below if you want to obtain sets per table */
92 /* nftnl_set_set(t, NFT_SET_TABLE, argv[2]); */
93  nftnl_set_nlmsg_build_payload(nlh, t);
94  nftnl_set_free(t);
95 
96  nl = mnl_socket_open(NETLINK_NETFILTER);
97  if (nl == NULL) {
98  perror("mnl_socket_open");
99  exit(EXIT_FAILURE);
100  }
101 
102  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
103  perror("mnl_socket_bind");
104  exit(EXIT_FAILURE);
105  }
106  portid = mnl_socket_get_portid(nl);
107 
108  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
109  perror("mnl_socket_send");
110  exit(EXIT_FAILURE);
111  }
112 
113  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
114  while (ret > 0) {
115  ret = mnl_cb_run(buf, ret, seq, portid, set_cb, &type);
116  if (ret <= 0)
117  break;
118  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
119  }
120  if (ret == -1) {
121  perror("error");
122  exit(EXIT_FAILURE);
123  }
124  mnl_socket_close(nl);
125 
126  return EXIT_SUCCESS;
127 }