]> git.ipfire.org Git - thirdparty/ulogd2.git/commitdiff
finish port of OPRINT target
authorlaforge <laforge>
Fri, 4 Nov 2005 22:55:38 +0000 (22:55 +0000)
committerlaforge <laforge>
Fri, 4 Nov 2005 22:55:38 +0000 (22:55 +0000)
output/Makefile.am
output/ulogd_output_OPRINT.c

index 73cc2b4eb22e198a73f573ca11759358a7420dde..7e9212f8713c1ece838a5f0042e06037c1c3b8c2 100644 (file)
@@ -4,7 +4,7 @@ SUBDIRS = pcap
 INCLUDES = $(all_includes) -I$(top_srcdir)/include
 LIBS=
 
-pkglib_LTLIBRARIES = ulogd_output_LOGEMU.la ulogd_output_SYSLOG.la ulogd_output_OPRINT.la
+pkglib_LTLIBRARIES = ulogd_output_LOGEMU.la ulogd_output_SYSLOG.la ulogd_output_OPRINT.la
 
 ulogd_output_LOGEMU_la_SOURCES = ulogd_output_LOGEMU.c
 ulogd_output_LOGEMU_la_LDFLAGS = -module
@@ -12,7 +12,7 @@ ulogd_output_LOGEMU_la_LDFLAGS = -module
 ulogd_output_SYSLOG_la_SOURCES = ulogd_output_SYSLOG.c
 ulogd_output_SYSLOG_la_LDFLAGS = -module
 
-#ulogd_OPRINT_la_SOURCES = ulogd_output_OPRINT.c
-#ulogd_OPRINT_la_LDFLAGS = -module
+ulogd_output_OPRINT_la_SOURCES = ulogd_output_OPRINT.c
+ulogd_output_OPRINT_la_LDFLAGS = -module
 
 
index d0d8181ea60b0b2c09ca7c5b56dff01cc8d20cfb..f44c8a0d6f8baaeb5db6e1967030377b1059f0c6 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include <ulogd/ulogd.h>
 #include <ulogd/conffile.h>
 
         ((unsigned char *)&addr)[0]
 
 struct oprint_priv {
-       static FILE *of = NULL;
+       FILE *of;
 };
 
-static int oprint_interp(struct ulogd_pluginstance *instance)
+static int oprint_interp(struct ulogd_pluginstance *upi)
 {
-       ulog_iret_t *ret = instance->input.keys;
+       struct oprint_priv *opi = (struct oprint_priv *) &upi->private;
+       unsigned int i;
        
-       fprintf(of, "===>PACKET BOUNDARY\n");
-       for (ret = res; ret; ret = ret->cur_next) {
-               fprintf(of,"%s=", ret->key);
+       fprintf(opi->of, "===>PACKET BOUNDARY\n");
+       for (i = 0; i < upi->plugin->input.num_keys; i++) {
+               struct ulogd_key *ret = upi->input[i].u.source;
+
+               if (!IS_VALID(*ret))
+                       continue;
+
+               fprintf(opi->of,"%s=", ret->name);
                switch (ret->type) {
                        case ULOGD_RET_STRING:
-                               fprintf(of, "%s\n", (char *) ret->value.ptr);
+                               fprintf(opi->of, "%s\n",
+                                       (char *) ret->u.value.ptr);
                                break;
                        case ULOGD_RET_BOOL:
                        case ULOGD_RET_INT8:
                        case ULOGD_RET_INT16:
                        case ULOGD_RET_INT32:
-                               fprintf(of, "%d\n", ret->value.i32);
+                               fprintf(opi->of, "%d\n", ret->u.value.i32);
                                break;
                        case ULOGD_RET_UINT8:
                        case ULOGD_RET_UINT16:
                        case ULOGD_RET_UINT32:
-                               fprintf(of, "%u\n", ret->value.ui32);
+                               fprintf(opi->of, "%u\n", ret->u.value.ui32);
                                break;
                        case ULOGD_RET_IPADDR:
-                               fprintf(of, "%u.%u.%u.%u\n", 
-                                       HIPQUAD(ret->value.ui32));
+                               fprintf(opi->of, "%u.%u.%u.%u\n", 
+                                       HIPQUAD(ret->u.value.ui32));
                                break;
                        case ULOGD_RET_NONE:
-                               fprintf(of, "<none>");
+                               fprintf(opi->of, "<none>");
                                break;
                }
        }
        return 0;
 }
 
-static struct config_entry outf_ce = { 
-       .key = "file", 
-       .type = CONFIG_TYPE_STRING, 
-       .options = CONFIG_OPT_NONE,
-       .u.string = ULOGD_OPRINT_DEFAULT
+static struct config_keyset oprint_kset = {
+       .num_ces = 1,
+       .ces = {
+               {
+               .key = "file", 
+               .type = CONFIG_TYPE_STRING, 
+               .options = CONFIG_OPT_NONE,
+               .u = {.string = ULOGD_OPRINT_DEFAULT },
+               },
+       },
 };
 
-static void sighup_handler_print(int signal)
+static void sighup_handler_print(struct ulogd_pluginstance *upi, int signal)
 {
+       struct oprint_priv *oi = (struct oprint_priv *) &upi->private;
 
        switch (signal) {
        case SIGHUP:
-               ulogd_log(ULOGD_NOTICE, "PKTLOG: reopening logfile\n");
-               fclose(of);
-               of = fopen(outf_ce.u.string, "a");
-               if (!of) {
-                       ulogd_log(ULOGD_FATAL, "can't open PKTLOG: %s\n",
+               ulogd_log(ULOGD_NOTICE, "OPRINT: reopening logfile\n");
+               fclose(oi->of);
+               oi->of = fopen(upi->config_kset->ces[0].u.string, "a");
+               if (!oi->of) {
+                       ulogd_log(ULOGD_ERROR, "can't open PKTLOG: %s\n",
                                strerror(errno));
-                       exit(2);
                }
                break;
        default:
@@ -107,61 +120,92 @@ static void sighup_handler_print(int signal)
        }
 }
 
-static struct ulogd_pluginstance *oprint_init(struct ulogd_plugin *pl)
+static int oprint_configure(struct ulogd_pluginstance *upi,
+                           struct ulogd_pluginstance_stack *stack)
 {
-       struct oprint_priv *op;
-       struct ulogd_pluginstance *opi = malloc(sizeof(*opi)+sizeof(*op));
+       struct ulogd_pluginstance *pi_cur;
+       unsigned int num_keys = 0;
+       unsigned int index = 0;
+
+       /* ok, this is a bit tricky, and probably requires some documentation.
+        * Since we are a output plugin (SINK), we can only be the last one
+        * in the stack.  Therefore, all other (input/filter) plugins, area
+        * already linked into the stack.  This means, we can iterate over them,
+        * get a list of all the keys, and create one input key for every output
+        * key that any of the upstream plugins provide.  By the time we resolve
+        * the inter-key pointers, everything will work as expected. */
+
+       /* first pass: count keys */
+       list_for_each_entry(pi_cur, &stack->list, list) {
+               ulogd_log(ULOGD_DEBUG, "iterating over pluginstance '%s'\n",
+                         pi_cur->id);
+               num_keys += pi_cur->plugin->output.num_keys;
+       }
+
+       ulogd_log(ULOGD_DEBUG, "allocating %u input keys\n", num_keys);
+       upi->input = malloc(sizeof(struct ulogd_key) * num_keys);
+       if (!upi->input)
+               return -ENOMEM;
+
+       /* second pass: copy key names */
+       list_for_each_entry(pi_cur, &stack->list, list) {
+               struct ulogd_key *cur;
+               int i;
+
+               for (i = 0; i < pi_cur->plugin->output.num_keys; i++)
+                       upi->input[index++] = pi_cur->output[i];
+       }
 
-       if (!opi)
-               return NULL;
+       config_parse_file(upi->id, upi->config_kset);
 
-       op = (struct oprint_priv *) opi->private;
-       opi->plugin = pl;
-       /* FIXME: opi->input */
-       opi->output = NULL;
+       /* FIXME: the count needs to be per-instance */
+       upi->plugin->input.num_keys = num_keys;
 
-#ifdef DEBUG
-       op->of = stdout;
-#else
-       config_parse_file("OPRINT", &outf_ce);
+       return 0;
+}
+
+static int oprint_init(struct ulogd_pluginstance *upi)
+{
+       struct oprint_priv *op = (struct oprint_priv *) &upi->private;
 
-       op->of = fopen(outf_ce.u.string, "a");
+       op->of = fopen(upi->config_kset->ces[0].u.string, "a");
        if (!op->of) {
                ulogd_log(ULOGD_FATAL, "can't open PKTLOG: %s\n", 
                        strerror(errno));
-               exit(2);
+               return -1;
        }               
-#endif
-       return opi;
+       return 0;
 }
 
 static int oprint_fini(struct ulogd_pluginstance *pi)
 {
-       struct oprint_priv *op = (struct oprint_priv *) pi->priv;
+       struct oprint_priv *op = (struct oprint_priv *) &pi->private;
 
        if (op->of != stdout)
                fclose(op->of);
 
-       return 1;
+       return 0;
 }
 
 static struct ulogd_plugin oprint_plugin = {
        .name = "OPRINT", 
        .input = {
-                       .type = ULOGD_DTYPE_PKT,
+                       .type = ULOGD_DTYPE_PACKET,
                },
        .output = {
                        .type = ULOGD_DTYPE_SINK,
                },
-       .interp = &oprint_interp,
-       .constructor = &oprint_init,
-       .destructor = &oprint_fini,
+       .configure = &oprint_configure,
+       .interp = &oprint_interp,
+       .start  = &oprint_init,
+       .stop   = &oprint_fini,
        .signal = &sighup_handler_print,
-       .configs = &outf_ce,
-       .num_configs = 1,
+       .config_kset = &oprint_kset,
 };
 
-void _init(void)
+void __attribute__ ((constructor)) init(void);
+
+void init(void)
 {
-       ulogd_register_output(&oprint_plugin);
+       ulogd_register_plugin(&oprint_plugin);
 }