From patchwork Thu Jul 25 20:52:32 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alvaro Neira X-Patchwork-Id: 261958 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 9C1012C00BB for ; Fri, 26 Jul 2013 06:53:12 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754784Ab3GYUwh (ORCPT ); Thu, 25 Jul 2013 16:52:37 -0400 Received: from mail-wg0-f52.google.com ([74.125.82.52]:46926 "EHLO mail-wg0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753048Ab3GYUwg (ORCPT ); Thu, 25 Jul 2013 16:52:36 -0400 Received: by mail-wg0-f52.google.com with SMTP id b13so2133763wgh.7 for ; Thu, 25 Jul 2013 13:52:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:from:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-type:content-transfer-encoding; bh=JOWG35NAXkDQxjI8EipsdvXam+/NRGEV2lnmuzyTX+E=; b=nXFxVVVRakdryNo7zialBaTJLoUA/Vwje8Hr4dKqVBP6Qsq5x32FWsPsmlUzgNRlaB t7Rnn2uMncbm1OBl6mY2+IHdpGyt2S6gif2nZ6fs4nm+tH+SeM5+Va+bvsPkJKaKRmFx QvP+el9wYLyK9SE9JzIQ8VBedv6dthtQ/uT7E3g0Si81BrDZGJFiIv9895n4QDTUCuCv 8hHZ9gJ+TS5WYC2M998QZF6jLtnO0oDw/uYUkxkRCriCrYxuaq5TL5M3FDj0Qa6oE+aE LQ80txthTy1IvmoqbMoTmPkmikVdGV7+dnuYZnb0phjdwcW75pMY84hIBOOD0lBjFguZ tsjA== X-Received: by 10.180.37.20 with SMTP id u20mr3453583wij.21.1374785554830; Thu, 25 Jul 2013 13:52:34 -0700 (PDT) Received: from [127.0.1.1] (4.151.76.188.dynamic.jazztel.es. [188.76.151.4]) by mx.google.com with ESMTPSA id w4sm376634wia.9.2013.07.25.13.52.33 for (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 25 Jul 2013 13:52:34 -0700 (PDT) Subject: [libnftables PATCH 3/6] examples: Add nft-table-json-add To: netfilter-devel@vger.kernel.org From: Alvaro Neira Cc: eric@regit.org Date: Thu, 25 Jul 2013 22:52:32 +0200 Message-ID: <20130725205232.26223.56829.stgit@Ph0enix> In-Reply-To: <20130725205215.26223.77001.stgit@Ph0enix> References: <20130725205215.26223.77001.stgit@Ph0enix> User-Agent: StGit/0.15 MIME-Version: 1.0 Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org From: Álvaro Neira Ayuso Signed-off-by: Alvaro Neira Ayuso --- examples/Makefile.am | 4 + examples/nft-table-json-add.c | 116 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 examples/nft-table-json-add.c -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/examples/Makefile.am b/examples/Makefile.am index bf28bae..722ce0b 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -2,6 +2,7 @@ include $(top_srcdir)/Make_global.am check_PROGRAMS = nft-table-add \ nft-table-xml-add \ + nft-table-json-add \ nft-table-upd \ nft-table-del \ nft-table-get \ @@ -29,6 +30,9 @@ nft_table_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} nft_table_xml_add_SOURCES = nft-table-xml-add.c nft_table_xml_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} +nft_table_json_add_SOURCES = nft-table-json-add.c +nft_table_json_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} + nft_table_upd_SOURCES = nft-table-upd.c nft_table_upd_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} diff --git a/examples/nft-table-json-add.c b/examples/nft-table-json-add.c new file mode 100644 index 0000000..5526c91 --- /dev/null +++ b/examples/nft-table-json-add.c @@ -0,0 +1,116 @@ +/* + * (C) 2013 by Álvaro Neira Ayuso + * + * Based on nft-table-xml-add from: + * + * (C) 2013 by Pablo Neira Ayuso + * (C) 2013 by Arturo Borrero Gonzalez + * + * 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 + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +int main(int argc, char *argv[]) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + uint32_t portid, seq; + struct nft_table *t = NULL; + int ret, fd; + uint16_t family; + char json[4096]; + char reprint[4096]; + + if (argc < 2) { + printf("Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + fd = open(argv[1], O_RDONLY); + if (fd < 0) { + perror("open"); + exit(EXIT_FAILURE); + } + + if (read(fd, json, sizeof(json)) < 0) { + perror("read"); + close(fd); + exit(EXIT_FAILURE); + } + close(fd); + + t = nft_table_alloc(); + if (t == NULL) { + perror("OOM"); + exit(EXIT_FAILURE); + } + + if (nft_table_parse(t, NFT_TABLE_PARSE_JSON, json) < 0) { + printf("E: Unable to parse JSON file: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + + nft_table_snprintf(reprint, sizeof(reprint), t, NFT_TABLE_O_JSON, 0); + printf("Parsed:\n%s\n", reprint); + + 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_CREATE|NLM_F_ACK, seq); + nft_table_nlmsg_build_payload(nlh, t); + nft_table_free(t); + + nl = mnl_socket_open(NETLINK_NETFILTER); + if (nl == NULL) { + perror("mnl_socket_open"); + exit(EXIT_FAILURE); + } + + if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { + perror("mnl_socket_bind"); + exit(EXIT_FAILURE); + } + portid = mnl_socket_get_portid(nl); + + if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { + perror("mnl_socket_send"); + exit(EXIT_FAILURE); + } + + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + while (ret > 0) { + ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL); + if (ret <= 0) + break; + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + } + if (ret == -1) { + perror("error"); + exit(EXIT_FAILURE); + } + + mnl_socket_close(nl); + + return EXIT_SUCCESS; +}