libnftnl  1.1.8
nft-table-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/table.h>
22 
23 static int table_cb(const struct nlmsghdr *nlh, void *data)
24 {
25  struct nftnl_table *t;
26  char buf[4096];
27  uint32_t *type = data;
28 
29  t = nftnl_table_alloc();
30  if (t == NULL) {
31  perror("OOM");
32  goto err;
33  }
34 
35  if (nftnl_table_nlmsg_parse(nlh, t) < 0) {
36  perror("nftnl_table_nlmsg_parse");
37  goto err_free;
38  }
39 
40  nftnl_table_snprintf(buf, sizeof(buf), t, *type, 0);
41  printf("%s\n", buf);
42 
43 err_free:
44  nftnl_table_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  struct nftnl_table *t = NULL;
56  int ret;
57  uint32_t type = NFTNL_OUTPUT_DEFAULT;
58 
59  if (argc < 2 || argc > 4) {
60  fprintf(stderr, "%s <family> [<table>]\n", argv[0]);
61  return EXIT_FAILURE;
62  }
63 
64  if (strcmp(argv[1], "ip") == 0)
65  family = NFPROTO_IPV4;
66  else if (strcmp(argv[1], "ip6") == 0)
67  family = NFPROTO_IPV6;
68  else if (strcmp(argv[1], "inet") == 0)
69  family = NFPROTO_INET;
70  else if (strcmp(argv[1], "bridge") == 0)
71  family = NFPROTO_BRIDGE;
72  else if (strcmp(argv[1], "arp") == 0)
73  family = NFPROTO_ARP;
74  else if (strcmp(argv[1], "unspec") == 0)
75  family = NFPROTO_UNSPEC;
76  else {
77  fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp, unspec\n");
78  exit(EXIT_FAILURE);
79  }
80 
81  if (argc == 3) {
82  t = nftnl_table_alloc();
83  if (t == NULL) {
84  perror("OOM");
85  exit(EXIT_FAILURE);
86  }
87  }
88 
89  seq = time(NULL);
90  if (t == NULL) {
91  nlh = nftnl_table_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family,
92  NLM_F_DUMP, seq);
93  } else {
94  nlh = nftnl_table_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family,
95  NLM_F_ACK, seq);
96  nftnl_table_set_str(t, NFTNL_TABLE_NAME, argv[2]);
97  nftnl_table_nlmsg_build_payload(nlh, t);
98  nftnl_table_free(t);
99  }
100 
101  nl = mnl_socket_open(NETLINK_NETFILTER);
102  if (nl == NULL) {
103  perror("mnl_socket_open");
104  exit(EXIT_FAILURE);
105  }
106 
107  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
108  perror("mnl_socket_bind");
109  exit(EXIT_FAILURE);
110  }
111  portid = mnl_socket_get_portid(nl);
112 
113  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
114  perror("mnl_socket_send");
115  exit(EXIT_FAILURE);
116  }
117 
118  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
119  while (ret > 0) {
120  ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type);
121  if (ret <= 0)
122  break;
123  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
124  }
125  if (ret == -1) {
126  perror("error");
127  exit(EXIT_FAILURE);
128  }
129  mnl_socket_close(nl);
130 
131  return EXIT_SUCCESS;
132 }
nftnl_table
Definition: table.c:28