From: Ken-ichirou MATSUZAWA <chamaken@gmail.com>
To: The netfilter developer mailinglist <netfilter-devel@vger.kernel.org>
Cc: Eric Leblond <eric@regit.org>
Subject: [PATCH v3 ulogd 07/12] ipfix: print ipfix message
Date: Tue, 3 Jun 2014 19:11:47 +0900 [thread overview]
Message-ID: <20140603101147.GH24668@gmail.com> (raw)
In-Reply-To: <20140603100130.GA24668@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 f9e23bb..0ff2efe 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,
@@ -364,6 +367,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;
@@ -406,6 +410,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;
}
@@ -604,3 +617,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.9.1
next prev parent reply other threads:[~2014-06-03 10:11 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-03 10:01 [PATCH v3 ulogd 0/12] make progress ulogd_output_IPFIX Ken-ichirou MATSUZAWA
2014-06-03 10:04 ` [PATCH v3 ulogd 01/12] ipfix: use nfct_bitmask Ken-ichirou MATSUZAWA
2014-06-03 10:05 ` [PATCH v3 ulogd 02/12] ipfix: fix enterprise bit handling Ken-ichirou MATSUZAWA
2014-06-03 10:07 ` [PATCH v3 ulogd 03/12] ipfix: some cleanups Ken-ichirou MATSUZAWA
2014-06-03 10:08 ` [PATCH v3 ulogd 04/12] ipfix: add functions for ipfix dataset creation Ken-ichirou MATSUZAWA
2014-06-03 10:09 ` [PATCH v3 ulogd 05/12] ipfix: add function for ipfix message creation Ken-ichirou MATSUZAWA
2014-06-03 10:10 ` [PATCH v3 ulogd 06/12] ipfix: decide whether prepending template by send times Ken-ichirou MATSUZAWA
2014-06-03 10:11 ` Ken-ichirou MATSUZAWA [this message]
2014-06-03 10:12 ` [PATCH 08/12] ipfix: build headers with template Ken-ichirou MATSUZAWA
2014-06-03 10:13 ` [PATCH v3 ulogd 09/12] nfct: fix ipfix field_id of flow.end.usec Ken-ichirou MATSUZAWA
2014-06-03 10:15 ` [PATCH v3 ulogd 10/12] nfct: fix icmp type and code output key size Ken-ichirou MATSUZAWA
2014-06-03 10:16 ` [PATCH v3 ulogd 11/12] nfct/ipfix: introduce new vendor id Ken-ichirou MATSUZAWA
2014-06-03 10:18 ` [PATCH v3 ulogd 12/12] 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=20140603101147.GH24668@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.