summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorÁlvaro Neira Ayuso <alvaroneay@gmail.com>2014-01-06 00:51:14 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2014-01-06 13:20:46 +0100
commite87d2f9ef8a4a298de5514b30ec2d43d3c90a644 (patch)
treeb1379f466db57d1a8bf8c2e6048f8a3933ef9639 /examples
parentb4a0d19dc1b16a614cdf0aa362f754e734486b38 (diff)
src: new error reporting approach for XML/JSON parsers
I have added a new structure for reporting some errors in parser that we can't cover with errno. In this patch, we have three errors that we can't cover with errno: NFT_PARSE_EBADINPUT : Bad XML/JSON format in the input NFT_PARSE_EMISSINGNODE : Missing node in our input NFT_PARSE_EBADTYPE : Wrong type value in a node Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'examples')
-rw-r--r--examples/nft-chain-json-add.c12
-rw-r--r--examples/nft-chain-xml-add.c12
-rw-r--r--examples/nft-rule-json-add.c12
-rw-r--r--examples/nft-rule-xml-add.c12
-rw-r--r--examples/nft-set-json-add.c12
-rw-r--r--examples/nft-table-json-add.c12
-rw-r--r--examples/nft-table-xml-add.c13
7 files changed, 71 insertions, 14 deletions
diff --git a/examples/nft-chain-json-add.c b/examples/nft-chain-json-add.c
index 50cb29f..1802fb0 100644
--- a/examples/nft-chain-json-add.c
+++ b/examples/nft-chain-json-add.c
@@ -39,6 +39,7 @@ int main(int argc, char *argv[])
uint16_t family;
char json[4096];
char reprint[4096];
+ struct nft_parse_err *err;
if (argc < 2) {
printf("Usage: %s <json-file>\n", argv[0]);
@@ -63,10 +64,16 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
+ err = nft_parse_err_alloc();
+ if (err == NULL) {
+ perror("error");
+ exit(EXIT_FAILURE);
+ }
+
close(fd);
- if (nft_chain_parse(c, NFT_PARSE_JSON, json) < 0) {
- printf("E: Unable to parse JSON file: %s\n", strerror(errno));
+ if (nft_chain_parse(c, NFT_PARSE_JSON, json, err) < 0) {
+ nft_parse_perror("Unable to parse JSON file", err);
exit(EXIT_FAILURE);
}
@@ -82,6 +89,7 @@ int main(int argc, char *argv[])
nft_chain_nlmsg_build_payload(nlh, c);
nft_chain_free(c);
+ nft_parse_err_free(err);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
diff --git a/examples/nft-chain-xml-add.c b/examples/nft-chain-xml-add.c
index 03a2950..79b1541 100644
--- a/examples/nft-chain-xml-add.c
+++ b/examples/nft-chain-xml-add.c
@@ -37,6 +37,7 @@ int main(int argc, char *argv[])
uint16_t family;
char xml[4096];
char reprint[4096];
+ struct nft_parse_err *err;
if (argc < 2) {
printf("Usage: %s <xml-file>\n", argv[0]);
@@ -49,6 +50,12 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
+ err = nft_parse_err_alloc();
+ if (err == NULL) {
+ perror("error");
+ exit(EXIT_FAILURE);
+ }
+
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("open");
@@ -63,8 +70,8 @@ int main(int argc, char *argv[])
close(fd);
- if (nft_chain_parse(c, NFT_PARSE_XML, xml) < 0) {
- printf("E: Unable to parse XML file: %s\n", strerror(errno));
+ if (nft_chain_parse(c, NFT_PARSE_XML, xml, err) < 0) {
+ nft_parse_perror("Unable to parse XML file", err);
exit(EXIT_FAILURE);
}
@@ -80,6 +87,7 @@ int main(int argc, char *argv[])
nft_chain_nlmsg_build_payload(nlh, c);
nft_chain_free(c);
+ nft_parse_err_free(err);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
diff --git a/examples/nft-rule-json-add.c b/examples/nft-rule-json-add.c
index 8659081..de1fb54 100644
--- a/examples/nft-rule-json-add.c
+++ b/examples/nft-rule-json-add.c
@@ -38,6 +38,7 @@ int main(int argc, char *argv[])
uint8_t family;
char json[4096];
char reprint[4096];
+ struct nft_parse_err *err;
if (argc < 2) {
printf("Usage: %s <json-file>\n", argv[0]);
@@ -63,8 +64,14 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- if (nft_rule_parse(r, NFT_PARSE_JSON, json) < 0) {
- printf("E: Unable to parse JSON file: %s\n", strerror(errno));
+ err = nft_parse_err_alloc();
+ if (err == NULL) {
+ perror("error");
+ exit(EXIT_FAILURE);
+ }
+
+ if (nft_rule_parse(r, NFT_PARSE_JSON, json, err) < 0) {
+ nft_parse_perror("Unable to parse JSON file", err);
exit(EXIT_FAILURE);
}
@@ -80,6 +87,7 @@ int main(int argc, char *argv[])
seq);
nft_rule_nlmsg_build_payload(nlh, r);
nft_rule_free(r);
+ nft_parse_err_free(err);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
diff --git a/examples/nft-rule-xml-add.c b/examples/nft-rule-xml-add.c
index ce33fe7..8a7685d 100644
--- a/examples/nft-rule-xml-add.c
+++ b/examples/nft-rule-xml-add.c
@@ -38,6 +38,7 @@ int main(int argc, char *argv[])
uint8_t family;
char xml[4096];
char reprint[4096];
+ struct nft_parse_err *err;
if (argc < 2) {
printf("Usage: %s <xml-file>\n", argv[0]);
@@ -63,8 +64,14 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- if (nft_rule_parse(r, NFT_PARSE_XML, xml) < 0) {
- printf("E: Unable to parse XML file: %s\n", strerror(errno));
+ err = nft_parse_err_alloc();
+ if (err == NULL) {
+ perror("error");
+ exit(EXIT_FAILURE);
+ }
+
+ if (nft_rule_parse(r, NFT_PARSE_XML, xml, err) < 0) {
+ nft_parse_perror("Unable to parse XML file", err);
exit(EXIT_FAILURE);
}
@@ -80,6 +87,7 @@ int main(int argc, char *argv[])
seq);
nft_rule_nlmsg_build_payload(nlh, r);
nft_rule_free(r);
+ nft_parse_err_free(err);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
diff --git a/examples/nft-set-json-add.c b/examples/nft-set-json-add.c
index 9a4aa48..9553ebf 100644
--- a/examples/nft-set-json-add.c
+++ b/examples/nft-set-json-add.c
@@ -38,6 +38,7 @@ int main(int argc, char *argv[])
uint16_t family;
char json[4096];
char reprint[4096];
+ struct nft_parse_err *err;
if (argc < 2) {
printf("Usage: %s <json-file>\n", argv[0]);
@@ -62,10 +63,16 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
+ err = nft_parse_err_alloc();
+ if (err == NULL) {
+ perror("error");
+ exit(EXIT_FAILURE);
+ }
+
close(fd);
- if (nft_set_parse(s, NFT_PARSE_JSON, json) < 0) {
- printf("E: Unable to parse JSON file: %s\n", strerror(errno));
+ if (nft_set_parse(s, NFT_PARSE_JSON, json, err) < 0) {
+ nft_parse_perror("Unable to parse JSON file", err);
exit(EXIT_FAILURE);
}
@@ -80,6 +87,7 @@ int main(int argc, char *argv[])
NLM_F_CREATE|NLM_F_ACK, seq);
nft_set_nlmsg_build_payload(nlh, s);
nft_set_free(s);
+ nft_parse_err_free(err);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
diff --git a/examples/nft-table-json-add.c b/examples/nft-table-json-add.c
index 6b16c7f..f3a57c0 100644
--- a/examples/nft-table-json-add.c
+++ b/examples/nft-table-json-add.c
@@ -39,6 +39,7 @@ int main(int argc, char *argv[])
uint16_t family;
char json[4096];
char reprint[4096];
+ struct nft_parse_err *err;
if (argc < 2) {
printf("Usage: %s <json-file>\n", argv[0]);
@@ -64,8 +65,14 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- if (nft_table_parse(t, NFT_PARSE_JSON, json) < 0) {
- printf("E: Unable to parse JSON file: %s\n", strerror(errno));
+ err = nft_parse_err_alloc();
+ if (err == NULL) {
+ perror("error");
+ exit(EXIT_FAILURE);
+ }
+
+ if (nft_table_parse(t, NFT_PARSE_JSON, json, err) < 0) {
+ nft_parse_perror("Unable to parse JSON file", err);
exit(EXIT_FAILURE);
}
@@ -80,6 +87,7 @@ int main(int argc, char *argv[])
NLM_F_CREATE|NLM_F_ACK, seq);
nft_table_nlmsg_build_payload(nlh, t);
nft_table_free(t);
+ nft_parse_err_free(err);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
diff --git a/examples/nft-table-xml-add.c b/examples/nft-table-xml-add.c
index 6a9163b..edef1d4 100644
--- a/examples/nft-table-xml-add.c
+++ b/examples/nft-table-xml-add.c
@@ -23,6 +23,7 @@
#include <libmnl/libmnl.h>
#include <libnftables/table.h>
+#include <libnftables/common.h>
int main(int argc, char *argv[])
{
@@ -35,6 +36,7 @@ int main(int argc, char *argv[])
uint16_t family;
char xml[4096];
char reprint[4096];
+ struct nft_parse_err *err;
if (argc < 2) {
printf("Usage: %s <xml-file>\n", argv[0]);
@@ -60,8 +62,14 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- if (nft_table_parse(t, NFT_PARSE_XML, xml) < 0) {
- printf("E: Unable to parse XML file: %s\n", strerror(errno));
+ err = nft_parse_err_alloc();
+ if (err == NULL) {
+ perror("error");
+ exit(EXIT_FAILURE);
+ }
+
+ if (nft_table_parse(t, NFT_PARSE_XML, xml, err) < 0) {
+ nft_parse_perror("Unable to parse XML file", err);
exit(EXIT_FAILURE);
}
@@ -76,6 +84,7 @@ int main(int argc, char *argv[])
NLM_F_CREATE|NLM_F_ACK, seq);
nft_table_nlmsg_build_payload(nlh, t);
nft_table_free(t);
+ nft_parse_err_free(err);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {