libnftnl  1.0.2
nft-rule-json-add.c
1 /*
2  * (C) 2013 by Álvaro Neira Ayuso <alvaroneay@gmail.com>
3  *
4  * Based on nft-rule-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 <sys/types.h>
18 #include <sys/stat.h>
19 #include <string.h>
20 #include <netinet/in.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/rule.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_rule *r = NULL;
37  int ret, fd;
38  uint8_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  fd = open(argv[1], O_RDONLY);
49  if (fd < 0) {
50  perror("open");
51  exit(EXIT_FAILURE);
52  }
53 
54  if (read(fd, json, sizeof(json)) < 0) {
55  perror("read");
56  close(fd);
57  exit(EXIT_FAILURE);
58  }
59  close(fd);
60 
61  r = nft_rule_alloc();
62  if (r == NULL) {
63  perror("OOM");
64  exit(EXIT_FAILURE);
65  }
66 
67  err = nft_parse_err_alloc();
68  if (err == NULL) {
69  perror("error");
70  exit(EXIT_FAILURE);
71  }
72 
73  if (nft_rule_parse(r, NFT_PARSE_JSON, json, err) < 0) {
74  nft_parse_perror("Unable to parse JSON file", err);
75  exit(EXIT_FAILURE);
76  }
77 
78  nft_rule_snprintf(reprint, sizeof(reprint), r, NFT_OUTPUT_JSON, 0);
79  printf("Parsed:\n%s\n", reprint);
80 
81  nft_rule_attr_unset(r, NFT_RULE_ATTR_HANDLE);
82  family = nft_rule_attr_get_u8(r, NFT_RULE_ATTR_FAMILY);
83 
84  seq = time(NULL);
85  nlh = nft_rule_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, family,
86  NLM_F_CREATE|NLM_F_APPEND|NLM_F_ACK,
87  seq);
88  nft_rule_nlmsg_build_payload(nlh, r);
89  nft_rule_free(r);
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  mnl_socket_close(nl);
121 
122  return EXIT_SUCCESS;
123 }
Definition: rule.c:34