netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ken-ichirou MATSUZAWA <chamaken@gmail.com>
To: Eric Leblond <eric@regit.org>
Cc: The netfilter developer mailinglist <netfilter-devel@vger.kernel.org>
Subject: [ulogd PATCH 7/8] ipfix: print ipfix message
Date: Wed, 26 Mar 2014 21:28:31 +0900	[thread overview]
Message-ID: <20140326122830.GH24689@gmail.com> (raw)
In-Reply-To: <20140326121122.GA24689@gmail.com>

by messy ipfix_fprintf_ functions.

Signed-off-by Ken-ichirou MATSUZAWA <chamas@h4.dion.ne.jp>
---
 output/ulogd_output_IPFIX.c | 226 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 226 insertions(+)

diff --git a/output/ulogd_output_IPFIX.c b/output/ulogd_output_IPFIX.c
index 9e8f140..ffb8e6e 100644
--- a/output/ulogd_output_IPFIX.c
+++ b/output/ulogd_output_IPFIX.c
@@ -31,6 +31,7 @@
 #include <stdbool.h>
 #include <unistd.h>
 #include <string.h>
+#include <time.h>
 #include <errno.h>
 
 #include <sys/types.h>
@@ -152,6 +153,8 @@ struct ipfix_instance {
 #define ULOGD_IPFIX_TEMPL_BASE 1024
 static u_int16_t next_template_id = ULOGD_IPFIX_TEMPL_BASE;
 
+static int ipfix_fprintf_header(FILE *fd, const struct ipfix_msg_hdr *hdr);
+
 /* Build the IPFIX template from the input keys */
 struct ulogd_ipfix_template *
 build_template_for_bitmask(struct ulogd_pluginstance *upi,
@@ -377,6 +380,7 @@ static int output_ipfix(struct ulogd_pluginstance *upi)
 {
 	struct ipfix_instance *ii = (struct ipfix_instance *) &upi->private;
 	struct ulogd_ipfix_template *template;
+	struct ipfix_msg_hdr *ipfix_msg;
 	unsigned int i;
 	bool need_template = false;
 
@@ -419,6 +423,15 @@ static int output_ipfix(struct ulogd_pluginstance *upi)
 		template->until_template = template_per_ce(upi->config_kset).u.value;
 	}
 
+	ipfix_msg = build_ipfix_msg(upi, template, need_template);
+	if (ipfix_msg == NULL)
+		return ULOGD_IRET_ERR;
+
+	ipfix_msg->export_time = htonl((u_int32_t)(time(NULL)));
+	ipfix_fprintf_header(stdout, ipfix_msg);
+	fprintf(stdout, "\n");
+
+	free(ipfix_msg);
 	return ULOGD_IRET_OK;
 }
 
@@ -617,3 +630,216 @@ void init(void)
 {
 	ulogd_register_plugin(&ipfix_plugin);
 }
+
+static int ipfix_fprintf_ietf_field(FILE *fd, const struct ipfix_ietf_field *field, int len);
+static int ipfix_fprintf_vendor_field(FILE *fd, const struct ipfix_vendor_field *field, int len);
+
+static int ipfix_fprintf_ietf_field(FILE *fd, const struct ipfix_ietf_field *field,
+				    int len)
+{
+	int ret;
+	void *ptr;
+
+	if (len < (int)sizeof(*field)) {
+		fprintf(fd, "ERROR ietf field: too short buflen for IETF field: %d\n", len);
+		return -1;
+	}
+
+	fprintf(fd, "+--------------------------------+--------------------------------+\n");
+	fprintf(fd, "|0 Information Emement id: %5d |            Field Length: %5d |\n",
+		ntohs(field->type), ntohs(field->length));
+
+	len -= sizeof(*field);
+	if (len == 0)
+		return sizeof(*field);
+
+	ptr = (void *)field + sizeof(*field);
+	if (*(u_int8_t *)ptr & 0x80)
+		ret = ipfix_fprintf_vendor_field(fd, ptr, len);
+	else
+		ret = ipfix_fprintf_ietf_field(fd, ptr, len);
+
+	if (ret == -1)
+		return -1;
+	return ret + sizeof(*field);
+}
+
+static int ipfix_fprintf_vendor_field(FILE *fd, const struct ipfix_vendor_field *field,
+				      int len)
+{
+	int ret;
+	void *ptr;
+
+	if (len < (int)sizeof(*field)) {
+		fprintf(fd, "ERROR vendor field: too short buflen for vendor field: %d\n", len);
+		return -1;
+	}
+
+	fprintf(fd, "+--------------------------------+--------------------------------+\n");
+	fprintf(fd, "|1 Information Emement id: %5d |            Field Length: %5d |\n",
+		ntohs(field->type) & 0x7fff, ntohs(field->length));
+	fprintf(fd, "+--------------------------------+--------------------------------+\n");
+	fprintf(fd, "|               Enterprise Number: %10d                     |\n",
+		ntohl(field->enterprise_num));
+
+	len -= sizeof(*field);
+	if (len == 0)
+		return sizeof(*field);
+
+	ptr = (void *)field + sizeof(*field);
+	if (*(u_int8_t *)ptr & 0x80) /* vendor */
+		ret = ipfix_fprintf_vendor_field(fd, ptr, len);
+	else /* ietf */
+		ret = ipfix_fprintf_ietf_field(fd, ptr, len);
+
+	if (ret == -1)
+		return -1;
+	return ret + sizeof(*field);
+}
+
+static int ipfix_fprintf_data_records(FILE *fd, const void *data, int len)
+{
+	int i;
+
+	fprintf(fd, "+-----------------------------------------------------------------+\n");
+	/* don't say messy...*/
+	for (i = 0; i < len; i += 4) {
+		switch (len - i - 4) {
+		case -3:
+			fprintf(fd, "|          0x%02x                                                   |\n",
+				*(u_int8_t *)(data + i));
+			break;
+		case -2:
+			fprintf(fd, "|          0x%02x          0x%02x                                     |\n",
+				*(u_int8_t *)(data + i), *(u_int8_t *)(data + i + 1));
+			break;
+		case -1:
+			fprintf(fd, "|          0x%02x          0x%02x          0x%02x                       |\n",
+				*(u_int8_t *)(data + i), *(u_int8_t *)(data + i + 1), *(u_int8_t *)(data + i + 2));
+			break;
+		default:
+			fprintf(fd, "|          0x%02x          0x%02x          0x%02x          0x%02x         |\n",
+				*(u_int8_t *)(data + i), *(u_int8_t *)(data + i + 1),
+				*(u_int8_t *)(data + i + 2), *(u_int8_t *)(data + i + 3));
+			break;
+		}
+	}
+	return len;
+}
+
+static int ipfix_fprintf_template_records(FILE *fd, const struct ipfix_templ_rec_hdr *hdr,
+					  int len)
+{
+	int ret;
+	void *field;
+
+	if (len < (int)sizeof(*hdr)) {
+		fprintf(fd, "ERROR template records: too short buflen for template record: %d\n", len);
+		return -1;
+	}
+
+	fprintf(fd, "+--------------------------------+--------------------------------+\n");
+	fprintf(fd, "|             Template ID: %5d |             Field Count: %5d |\n",
+		ntohs(hdr->templ_id), ntohs(hdr->field_count));
+
+	len -= sizeof(*hdr);
+	if (len == 0)
+		return sizeof(*hdr);
+
+	field = (void *)hdr + sizeof(*hdr);
+	if (*(u_int8_t *)field & 0x80)
+		ret = ipfix_fprintf_vendor_field(fd, field, len);
+	else
+		ret = ipfix_fprintf_ietf_field(fd, field, len);
+
+	if (ret == -1)
+		return -1;
+	return ret + sizeof(*hdr);
+}
+
+static int ipfix_fprintf_set_header(FILE *fd, const struct ipfix_set_hdr *hdr, int len)
+{
+	int ret, setlen, total_len;
+	void *ptr;
+
+	if (len < (int)sizeof(*hdr)) {
+		fprintf(fd, "ERROR set header: too short buflen for set header: %d\n", len);
+		return -1;
+	}
+	setlen = ntohs(hdr->length);
+	if (len < setlen) {
+		fprintf(fd, "ERROR set header: buflen: %d is smaller than set length field: %d\n", len, setlen);
+		/* return -1; */
+	}
+	if (setlen < (int)sizeof(*hdr)) {
+		fprintf(fd, "ERROR set header: too short set length field: %d\n", setlen);
+		return -1;
+	}
+
+	fprintf(fd, "+--------------------------------+--------------------------------+\n");
+	fprintf(fd, "|                  Set ID: %5d |                  Length: %5d |\n",
+		ntohs(hdr->set_id), setlen);
+
+	setlen -= sizeof(*hdr);
+	ptr = (void *)hdr + sizeof(*hdr);
+	total_len = sizeof(*hdr);
+
+	switch (ntohs(hdr->set_id)) {
+	case 2:
+		ret = ipfix_fprintf_template_records(fd, ptr, setlen);
+		break;
+	case 3:
+		/* XXX: ret = ipfix_fprintf_options_template_records(fd, ptr, setlen); */
+		fprintf(fd, "ERROR: options template is not implemented yet, sorry");
+		ret = setlen;
+		break;
+	default:
+		ret = ipfix_fprintf_data_records(fd, ptr, setlen);
+		break;
+	}
+
+	if (ret == -1 || ret != setlen)
+		return -1;
+
+	fprintf(fd, "+-----------------------------------------------------------------+\n");
+	return total_len + ret;
+}
+
+static int ipfix_fprintf_header(FILE *fd, const struct ipfix_msg_hdr *hdr)
+{
+	int ret, len;
+	char outstr[20];
+	void *ptr;
+	time_t t = (time_t)(ntohl(hdr->export_time));
+	struct tm *tmp = localtime(&t);
+
+	/* XXX: tmp == NULL and strftime == 0 */
+	strftime(outstr, sizeof(outstr), "%F %T", tmp);
+
+	fprintf(fd, "+--------------------------------+--------------------------------+\n");
+	fprintf(fd, "|          Version Number: %5d |                  Length: %5d |\n",
+		ntohs(hdr->version), ntohs(hdr->length));
+	fprintf(fd, "+--------------------------------+--------------------------------+\n");
+	fprintf(fd, "|                     Exoprt Time: %10d                     |\t%s\n",
+		ntohl(hdr->export_time), outstr);
+	fprintf(fd, "+-----------------------------------------------------------------+\n");
+	fprintf(fd, "|                 Sequence Number: %10d                     |\n",
+		ntohl(hdr->seq));
+	fprintf(fd, "+-----------------------------------------------------------------+\n");
+	fprintf(fd, "|           Observation Domain ID: %10d                     |\n",
+		ntohl(hdr->domain_id));
+	fprintf(fd, "+-----------------------------------------------------------------+\n");
+
+	len = ntohs(hdr->length) - sizeof(*hdr);
+	ptr = (void *)hdr + sizeof(*hdr);
+
+	while (len > 0) {
+		ret = ipfix_fprintf_set_header(fd, ptr, len);
+		if (ret == -1)
+			return -1;
+		len -= ret;
+		ptr += ret;
+	}
+
+	return ntohs(hdr->length) - len;
+}
-- 
1.8.5.3


  parent reply	other threads:[~2014-03-26 12:28 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-08  1:03 [ulogd PATCH 0/8] make progress ulogd_output_IPFIX Ken-ichirou MATSUZAWA
2014-03-08  1:07 ` [PATCH 1/8] ipfix: use nfct_bitmask Ken-ichirou MATSUZAWA
2014-03-23 18:55   ` Eric Leblond
2014-03-08  1:09 ` [PATCH 2/8] ipfix: fix enterprise bit handling Ken-ichirou MATSUZAWA
2014-03-08  1:10 ` [PATCH 3/8] ipfix: some cleanups Ken-ichirou MATSUZAWA
2014-03-08  1:12 ` [PATCH 4/8] ipfix: add functions for ipfix dataset creation Ken-ichirou MATSUZAWA
2014-03-08  1:13 ` [PATCH 5/8] ipfix: add function for ipfix message creation Ken-ichirou MATSUZAWA
2014-03-23 20:06   ` Eric Leblond
2014-03-08  1:15 ` [PATCH 6/8] ipfix: decide whether prepending template by send times Ken-ichirou MATSUZAWA
2014-03-08  1:17 ` [PATCH 7/8] ipfix: print ipfix message Ken-ichirou MATSUZAWA
2014-03-08  1:19 ` [PATCH 8/8] ipfix: build headers with template Ken-ichirou MATSUZAWA
2014-03-08  1:24 ` [libnetfilter_conntrack PATCH] conntrack: introduce clear and equal functions for bitmask object Ken-ichirou MATSUZAWA
2014-03-08  9:25   ` Florian Westphal
2014-03-23 18:50 ` [ulogd PATCH 0/8] make progress ulogd_output_IPFIX Eric Leblond
2014-03-26 12:11   ` Ken-ichirou MATSUZAWA
2014-03-26 12:16     ` [ulogd PATCH 1/8] ipfix: use nfct_bitmask Ken-ichirou MATSUZAWA
2014-03-26 12:18     ` [ulogd PATCH 2/8] ipfix: fix enterprise bit handling Ken-ichirou MATSUZAWA
2014-03-26 12:19     ` [ulogd PATCH 3/8] ipfix: some cleanups Ken-ichirou MATSUZAWA
2014-03-26 12:23     ` [ulogd PATCH 4/8] ipfix: add functions for ipfix dataset creation Ken-ichirou MATSUZAWA
2014-03-26 12:25     ` [ulogd PATCH 5/8] ipfix: add function for ipfix message creation Ken-ichirou MATSUZAWA
2014-03-26 12:26     ` [ulogd PATCH 6/8] ipfix: decide whether prepending template by send times Ken-ichirou MATSUZAWA
2014-03-26 12:28     ` Ken-ichirou MATSUZAWA [this message]
2014-03-26 12:30     ` [ulogd PATCH 8/8] ipfix: build headers with template Ken-ichirou MATSUZAWA
2014-04-19 13:36     ` [ulogd PATCH 0/8] make progress ulogd_output_IPFIX Eric Leblond
2014-04-22 11:56       ` [ulogd PATCH 1/8 resend] ipfix: use nfct_bitmask Ken-ichirou MATSUZAWA
2014-04-22 12:03       ` [ulogd PATCH 0/8] make progress ulogd_output_IPFIX Ken-ichirou MATSUZAWA
2014-04-22 15:20         ` Eric Leblond
2014-04-28 11:39 ` [ulogd PATCH 0/13] " Ken-ichirou MATSUZAWA
2014-04-28 11:42   ` [libnetfilter_conntrack PATCH 1/13] conntrack: introduce clear and equal functions for bitmask object Ken-ichirou MATSUZAWA
2014-04-28 11:44   ` [ulogd PATCH 2/13] ipfix: use nfct_bitmask Ken-ichirou MATSUZAWA
2014-04-28 11:45   ` [ulogd PATCH 3/13] ipfix: fix enterprise bit handling Ken-ichirou MATSUZAWA
2014-04-28 11:46   ` [ulogd PATCH 4/13] ipfix: some cleanups Ken-ichirou MATSUZAWA
2014-04-28 11:48   ` [ulogd PATCH 5/13] ipfix: add functions for ipfix dataset creation Ken-ichirou MATSUZAWA
2014-04-28 11:49   ` [ulogd PATCH 6/13] ipfix: add function for ipfix message creation Ken-ichirou MATSUZAWA
2014-04-28 11:50   ` [ulogd PATCH 7/13] ipfix: decide whether prepending template by send times Ken-ichirou MATSUZAWA
2014-04-28 11:51   ` [ulogd PATCH 8/13] ipfix: print ipfix message Ken-ichirou MATSUZAWA
2014-04-28 11:52   ` [ulogd PATCH 9/13] ipfix: build headers with template Ken-ichirou MATSUZAWA
2014-04-28 11:53   ` [ulogd PATCH 10/13] nfct: fix ipfix field_id of flow.end.usec Ken-ichirou MATSUZAWA
2014-04-28 11:54   ` [ulogd PATCH 11/13] nfct/ipfix: introduce new vendor id Ken-ichirou MATSUZAWA
2014-04-28 11:56   ` [ulogd PATCH 12/13] nfct: introduce new out keys for ipfix timestamp Ken-ichirou MATSUZAWA
2014-06-01 10:28     ` Eric Leblond
2014-06-02  9:52       ` Pablo Neira Ayuso
2014-06-02 12:51         ` Ken-ichirou MATSUZAWA
2014-06-02 18:59           ` Eric Leblond
2014-04-28 11:57   ` [ulogd PATCH 13/13] ipfix: add debug symbol for yafscii Ken-ichirou MATSUZAWA

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140326122830.GH24689@gmail.com \
    --to=chamaken@gmail.com \
    --cc=eric@regit.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).