libnftnl  1.0.8
nft-set-del.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 int main(int argc, char *argv[])
24 {
25  struct mnl_socket *nl;
26  char buf[MNL_SOCKET_BUFFER_SIZE];
27  struct nlmsghdr *nlh;
28  uint32_t portid, seq, family;
29  struct nftnl_set *t = NULL;
30  int ret;
31 
32  if (argc != 4) {
33  fprintf(stderr, "%s <family> <table> <set>\n", argv[0]);
34  exit(EXIT_FAILURE);
35  }
36 
37  t = nftnl_set_alloc();
38  if (t == NULL) {
39  perror("OOM");
40  exit(EXIT_FAILURE);
41  }
42 
43  seq = time(NULL);
44  if (strcmp(argv[1], "ip") == 0)
45  family = NFPROTO_IPV4;
46  else if (strcmp(argv[1], "ip6") == 0)
47  family = NFPROTO_IPV6;
48  else if (strcmp(argv[1], "bridge") == 0)
49  family = NFPROTO_BRIDGE;
50  else if (strcmp(argv[1], "arp") == 0)
51  family = NFPROTO_ARP;
52  else {
53  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
54  exit(EXIT_FAILURE);
55  }
56 
57  nlh = nftnl_set_nlmsg_build_hdr(buf, NFT_MSG_DELSET, family,
58  NLM_F_ACK, seq);
59  nftnl_set_set(t, NFTNL_SET_TABLE, argv[2]);
60  nftnl_set_set(t, NFTNL_SET_NAME, argv[3]);
61 
62  nftnl_set_nlmsg_build_payload(nlh, t);
63  nftnl_set_free(t);
64 
65  nl = mnl_socket_open(NETLINK_NETFILTER);
66  if (nl == NULL) {
67  perror("mnl_socket_open");
68  exit(EXIT_FAILURE);
69  }
70 
71  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
72  perror("mnl_socket_bind");
73  exit(EXIT_FAILURE);
74  }
75  portid = mnl_socket_get_portid(nl);
76 
77  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
78  perror("mnl_socket_send");
79  exit(EXIT_FAILURE);
80  }
81 
82  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
83  while (ret > 0) {
84  ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
85  if (ret <= 0)
86  break;
87  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
88  }
89  if (ret == -1) {
90  perror("error");
91  exit(EXIT_FAILURE);
92  }
93  mnl_socket_close(nl);
94 
95  return EXIT_SUCCESS;
96 }