libnftnl  1.0.2
nft-table-json-add.c
1 /*
2  * (C) 2013 by Álvaro Neira Ayuso <alvaroneay@gmail.com>
3  *
4  * Based on nft-table-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  * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
15  */
16 
17 #include <stdlib.h>
18 #include <time.h>
19 #include <string.h>
20 #include <netinet/in.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 
26 #include <linux/netfilter/nf_tables.h>
27 
28 #include <libmnl/libmnl.h>
29 #include <libnftnl/table.h>
30 
31 int main(int argc, char *argv[])
32 {
33  struct mnl_socket *nl;
34  char buf[MNL_SOCKET_BUFFER_SIZE];
35  struct nlmsghdr *nlh;
36  uint32_t portid, seq;
37  struct nft_table *t = NULL;
38  int ret, fd;
39  uint16_t family;
40  char json[4096];
41  char reprint[4096];
42  struct nft_parse_err *err;
43 
44  if (argc < 2) {
45  printf("Usage: %s <json-file>\n", argv[0]);
46  exit(EXIT_FAILURE);
47  }
48 
49  fd = open(argv[1], O_RDONLY);
50  if (fd < 0) {
51  perror("open");
52  exit(EXIT_FAILURE);
53  }
54 
55  if (read(fd, json, sizeof(json)) < 0) {
56  perror("read");
57  close(fd);
58  exit(EXIT_FAILURE);
59  }
60  close(fd);
61 
62  t = nft_table_alloc();
63  if (t == NULL) {
64  perror("OOM");
65  exit(EXIT_FAILURE);
66  }
67 
68  err = nft_parse_err_alloc();
69  if (err == NULL) {
70  perror("error");
71  exit(EXIT_FAILURE);
72  }
73 
74  if (nft_table_parse(t, NFT_PARSE_JSON, json, err) < 0) {
75  nft_parse_perror("Unable to parse JSON file", err);
76  exit(EXIT_FAILURE);
77  }
78 
79  nft_table_snprintf(reprint, sizeof(reprint), t, NFT_OUTPUT_JSON, 0);
80  printf("Parsed:\n%s\n", reprint);
81 
82  family = nft_table_attr_get_u32(t, NFT_TABLE_ATTR_FAMILY);
83 
84  seq = time(NULL);
85 
86  nlh = nft_table_nlmsg_build_hdr(buf, NFT_MSG_NEWTABLE, family,
87  NLM_F_CREATE|NLM_F_ACK, seq);
88  nft_table_nlmsg_build_payload(nlh, t);
89  nft_table_free(t);
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 }