libnftnl  1.0.2
nft-set-json-add.c
1 /*
2  * (C) 2013 by Álvaro Neira Ayuso <alvaroneay@gmail.com>
3  *
4  * Based on nft-set-xml-add from:
5  *
6  * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
7  * (C) 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  */
14 
15 #include <stdlib.h>
16 #include <time.h>
17 #include <string.h>
18 #include <netinet/in.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 
24 #include <linux/netfilter.h>
25 #include <linux/netfilter/nf_tables.h>
26 
27 #include <libmnl/libmnl.h>
28 #include <libnftnl/set.h>
29 
30 int main(int argc, char *argv[])
31 {
32  struct mnl_socket *nl;
33  char buf[MNL_SOCKET_BUFFER_SIZE];
34  struct nlmsghdr *nlh;
35  uint32_t portid, seq;
36  struct nft_set *s;
37  int ret, fd;
38  uint16_t family;
39  char json[4096];
40  char reprint[4096];
41  struct nft_parse_err *err;
42 
43  if (argc < 2) {
44  printf("Usage: %s <json-file>\n", argv[0]);
45  exit(EXIT_FAILURE);
46  }
47 
48  s = nft_set_alloc();
49  if (s == NULL) {
50  perror("OOM");
51  exit(EXIT_FAILURE);
52  }
53 
54  fd = open(argv[1], O_RDONLY);
55  if (fd < 0) {
56  perror("open");
57  exit(EXIT_FAILURE);
58  }
59 
60  if (read(fd, json, sizeof(json)) < 0) {
61  perror("read");
62  close(fd);
63  exit(EXIT_FAILURE);
64  }
65 
66  err = nft_parse_err_alloc();
67  if (err == NULL) {
68  perror("error");
69  exit(EXIT_FAILURE);
70  }
71 
72  close(fd);
73 
74  if (nft_set_parse(s, NFT_PARSE_JSON, json, err) < 0) {
75  nft_parse_perror("Unable to parse JSON file", err);
76  exit(EXIT_FAILURE);
77  }
78 
79  nft_set_snprintf(reprint, sizeof(reprint), s, NFT_OUTPUT_JSON, 0);
80  printf("Parsed:\n%s\n", reprint);
81 
82  family = nft_set_attr_get_u32(s, NFT_SET_ATTR_FAMILY);
83 
84  seq = time(NULL);
85 
86  nlh = nft_set_nlmsg_build_hdr(buf, NFT_MSG_NEWSET, family,
87  NLM_F_CREATE|NLM_F_ACK, seq);
88  nft_set_nlmsg_build_payload(nlh, s);
89  nft_set_free(s);
90  nft_parse_err_free(err);
91 
92  nl = mnl_socket_open(NETLINK_NETFILTER);
93  if (nl == NULL) {
94  perror("mnl_socket_open");
95  exit(EXIT_FAILURE);
96  }
97 
98  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
99  perror("mnl_socket_bind");
100  exit(EXIT_FAILURE);
101  }
102  portid = mnl_socket_get_portid(nl);
103 
104  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
105  perror("mnl_socket_send");
106  exit(EXIT_FAILURE);
107  }
108 
109  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
110  while (ret > 0) {
111  ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
112  if (ret <= 0)
113  break;
114  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
115  }
116  if (ret == -1) {
117  perror("error");
118  exit(EXIT_FAILURE);
119  }
120 
121  mnl_socket_close(nl);
122 
123  return EXIT_SUCCESS;
124 }