libnftnl  1.0.2
nft-chain-xml-add.c
1 /*
2  * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
3  * (C) 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
11  */
12 
13 #include <stdlib.h>
14 #include <time.h>
15 #include <string.h>
16 #include <netinet/in.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <errno.h>
21 
22 #include <linux/netfilter.h>
23 #include <linux/netfilter/nf_tables.h>
24 
25 #include <libmnl/libmnl.h>
26 #include <libnftnl/chain.h>
27 #include <libnftnl/rule.h>
28 
29 int main(int argc, char *argv[])
30 {
31  struct mnl_socket *nl;
32  char buf[MNL_SOCKET_BUFFER_SIZE];
33  struct nlmsghdr *nlh;
34  uint32_t portid, seq;
35  struct nft_chain *c = NULL;
36  int ret, fd;
37  uint16_t family;
38  char xml[4096];
39  char reprint[4096];
40  struct nft_parse_err *err;
41 
42  if (argc < 2) {
43  printf("Usage: %s <xml-file>\n", argv[0]);
44  exit(EXIT_FAILURE);
45  }
46 
47  c = nft_chain_alloc();
48  if (c == NULL) {
49  perror("OOM");
50  exit(EXIT_FAILURE);
51  }
52 
53  err = nft_parse_err_alloc();
54  if (err == NULL) {
55  perror("error");
56  exit(EXIT_FAILURE);
57  }
58 
59  fd = open(argv[1], O_RDONLY);
60  if (fd < 0) {
61  perror("open");
62  exit(EXIT_FAILURE);
63  }
64 
65  if (read(fd, xml, sizeof(xml)) < 0) {
66  perror("read");
67  close(fd);
68  exit(EXIT_FAILURE);
69  }
70 
71  close(fd);
72 
73  if (nft_chain_parse(c, NFT_PARSE_XML, xml, err) < 0) {
74  nft_parse_perror("Unable to parse XML file", err);
75  exit(EXIT_FAILURE);
76  }
77 
78  nft_chain_snprintf(reprint, sizeof(reprint), c, NFT_OUTPUT_XML, 0);
79  printf("Parsed:\n%s\n", reprint);
80 
81  nft_chain_attr_unset(c, NFT_CHAIN_ATTR_HANDLE);
82  family = nft_chain_attr_get_u32(c, NFT_CHAIN_ATTR_FAMILY);
83 
84  seq = time(NULL);
85  nlh = nft_chain_nlmsg_build_hdr(buf, NFT_MSG_NEWCHAIN, family,
86  NLM_F_CREATE|NLM_F_ACK, seq);
87  nft_chain_nlmsg_build_payload(nlh, c);
88 
89  nft_chain_free(c);
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 
103  portid = mnl_socket_get_portid(nl);
104 
105  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
106  perror("mnl_socket_send");
107  exit(EXIT_FAILURE);
108  }
109 
110  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
111  while (ret > 0) {
112  ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
113  if (ret <= 0)
114  break;
115  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
116 
117  }
118  if (ret == -1) {
119  perror("error");
120  exit(EXIT_FAILURE);
121  }
122 
123 
124  mnl_socket_close(nl);
125  return EXIT_SUCCESS;
126 }