]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
examples: nft-{table,chain,rule}-xml-add: fix missing NLM_F_CREATE
authorPablo Neira Ayuso <pablo@netfilter.org>
Wed, 10 Jul 2013 16:34:57 +0000 (18:34 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 10 Jul 2013 16:51:11 +0000 (18:51 +0200)
Thus, automodule loading was not working.

While at it, apply not so relevant comestic cleanups and fix some
inconsistencies between examples.

* Fix copyright header, this is code heavily based on existing
  nft-*-add examples.
* Remove unrequired extern struct nft_table definition.
* Make sure we close file descriptor once we don't need it anymore.
* Remove unrequired casting.
* Remove comment that provides nothing interesting.

I considered a patch to address each on those was too much burden.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
examples/nft-chain-xml-add.c
examples/nft-rule-xml-add.c
examples/nft-table-xml-add.c

index 80a513db73fc6e2fa4a3c45e61edd6134fa303ce..2e2fe113b647e6a5f74d70e19175535929f940d8 100644 (file)
@@ -1,10 +1,13 @@
 /*
- * 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
+ * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
+ * (C) 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
+ *
+ * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
  */
 
 #include <stdlib.h>
@@ -30,8 +33,7 @@ int main(int argc, char *argv[])
        struct nlmsghdr *nlh;
        uint32_t portid, seq;
        struct nft_chain *c = NULL;
-       int ret;
-       int fd;
+       int ret, fd;
        uint16_t family;
        char xml[4096];
        char reprint[4096];
@@ -70,11 +72,11 @@ int main(int argc, char *argv[])
        printf("Parsed:\n%s\n", reprint);
 
        nft_chain_attr_unset(c, NFT_CHAIN_ATTR_HANDLE);
-       family = (uint16_t)nft_chain_attr_get_u32(c, NFT_CHAIN_ATTR_FAMILY);
+       family = nft_chain_attr_get_u32(c, NFT_CHAIN_ATTR_FAMILY);
 
        seq = time(NULL);
        nlh = nft_chain_nlmsg_build_hdr(buf, NFT_MSG_NEWCHAIN, family,
-                                       NLM_F_ACK, seq);
+                                       NLM_F_CREATE|NLM_F_ACK, seq);
        nft_chain_nlmsg_build_payload(nlh, c);
 
        nft_chain_free(c);
index 7bd44ffcc2e2863ae9b4f1971515e0dab453f329..4de7a6943f8b38924830dc0fd05b700b13a6e2ef 100644 (file)
@@ -1,10 +1,13 @@
 /*
- * 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
+ * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
+ * (C) 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
+ *
+ * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
  */
 
 #include <stdlib.h>
@@ -52,6 +55,7 @@ int main(int argc, char *argv[])
                close(fd);
                exit(EXIT_FAILURE);
        }
+       close(fd);
 
        r = nft_rule_alloc();
        if (r == NULL) {
@@ -72,7 +76,8 @@ int main(int argc, char *argv[])
 
        seq = time(NULL);
        nlh = nft_rule_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, family,
-                                       NLM_F_APPEND|NLM_F_ACK, seq);
+                                      NLM_F_CREATE|NLM_F_APPEND|NLM_F_ACK,
+                                      seq);
        nft_rule_nlmsg_build_payload(nlh, r);
        nft_rule_free(r);
 
index 41df5b4d297290cc3fcb22fc7a16b9531c25dc65..6de0855d56fa1499f687e80bc89f9d516eeb2ae7 100644 (file)
@@ -1,10 +1,13 @@
 /*
- * 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
+ * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
+ * (C) 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
+ *
+ * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
  */
 
 #include <stdlib.h>
@@ -21,8 +24,6 @@
 #include <libmnl/libmnl.h>
 #include <libnftables/table.h>
 
-extern struct nft_table nft_table;
-
 int main(int argc, char *argv[])
 {
        struct mnl_socket *nl;
@@ -30,8 +31,7 @@ int main(int argc, char *argv[])
        struct nlmsghdr *nlh;
        uint32_t portid, seq;
        struct nft_table *t = NULL;
-       int ret;
-       int fd;
+       int ret, fd;
        uint16_t family;
        char xml[4096];
        char reprint[4096];
@@ -52,33 +52,28 @@ int main(int argc, char *argv[])
                close(fd);
                exit(EXIT_FAILURE);
        }
+       close(fd);
 
        t = nft_table_alloc();
        if (t == NULL) {
                perror("OOM");
-               close(fd);
                exit(EXIT_FAILURE);
        }
 
-       /* Parsing XML now */
        if (nft_table_parse(t, NFT_TABLE_PARSE_XML, xml) < 0) {
                printf("E: Unable to parse XML file: %s\n", strerror(errno));
-               close(fd);
                exit(EXIT_FAILURE);
        }
 
-       close(fd);
-
        nft_table_snprintf(reprint, sizeof(reprint), t, NFT_TABLE_O_XML, 0);
        printf("Parsed:\n%s\n", reprint);
 
-
-       family = (uint16_t)nft_table_attr_get_u32(t, NFT_TABLE_ATTR_FAMILY);
+       family = nft_table_attr_get_u32(t, NFT_TABLE_ATTR_FAMILY);
 
        seq = time(NULL);
 
        nlh = nft_table_nlmsg_build_hdr(buf, NFT_MSG_NEWTABLE, family,
-                                                       NLM_F_ACK, seq);
+                                       NLM_F_CREATE|NLM_F_ACK, seq);
        nft_table_nlmsg_build_payload(nlh, t);
        nft_table_free(t);