1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/*
* Copyright (c) 2002-2012 Balabit
* Copyright (c) 1998-2012 Balázs Scheidler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "logmpx.h"
#include "cfg-walker.h"
void
log_multiplexer_add_next_hop(LogMultiplexer *self, LogPipe *next_hop)
{
g_ptr_array_add(self->next_hops, next_hop);
}
static gboolean
log_multiplexer_init(LogPipe *s)
{
LogMultiplexer *self = (LogMultiplexer *) s;
gint i;
for (i = 0; i < self->next_hops->len; i++)
{
LogPipe *branch_head = g_ptr_array_index(self->next_hops, i);
LogPipe *p;
for (p = branch_head; p; p = p->pipe_next)
{
branch_head->flags |= (p->flags & PIF_BRANCH_PROPERTIES);
}
if (branch_head->flags & PIF_BRANCH_FALLBACK)
{
self->fallback_exists = TRUE;
}
}
return TRUE;
}
static gboolean
log_multiplexer_deinit(LogPipe *self)
{
return TRUE;
}
static void
log_multiplexer_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_options)
{
LogMultiplexer *self = (LogMultiplexer *) s;
gint i;
LogPathOptions local_options = *path_options;
gboolean matched;
gboolean delivered = FALSE;
gint fallback;
local_options.matched = &matched;
if (self->next_hops->len > 1)
{
log_msg_write_protect(msg);
}
for (fallback = 0; (fallback == 0) || (fallback == 1 && self->fallback_exists && !delivered); fallback++)
{
for (i = 0; i < self->next_hops->len; i++)
{
LogPipe *next_hop = g_ptr_array_index(self->next_hops, i);
if (G_UNLIKELY(fallback == 0 && (next_hop->flags & PIF_BRANCH_FALLBACK) != 0))
{
continue;
}
else if (G_UNLIKELY(fallback && (next_hop->flags & PIF_BRANCH_FALLBACK) == 0))
{
continue;
}
matched = TRUE;
log_msg_add_ack(msg, &local_options);
log_pipe_queue(next_hop, log_msg_ref(msg), &local_options);
if (matched)
{
delivered = TRUE;
if (G_UNLIKELY(next_hop->flags & PIF_BRANCH_FINAL))
break;
}
}
}
if (self->next_hops->len > 1)
{
log_msg_write_unprotect(msg);
}
/* NOTE: non of our multiplexed destinations delivered this message, let's
* propagate this result. But only if we don't have a "next". If we do,
* that would be responsible for doing the same, for instance if it is a
* filter.
*
* In case where this matters most (e.g. the multiplexer attached to the
* source LogPipe), "next" will always be NULL. I am not sure if there's
* ever a case, where a LogMpx has both "next" set, and have branches as
* well.
*
* If there's such a case, then from a conceptual point of view, this Mpx
* instance should transfer the responsibility for setting "matched" to
* the next pipeline element, which is what we do here.
*/
if (!s->pipe_next && !delivered && path_options->matched)
*path_options->matched = FALSE;
log_pipe_forward_msg(s, msg, path_options);
}
static void
log_multiplexer_free(LogPipe *s)
{
LogMultiplexer *self = (LogMultiplexer *) s;
g_ptr_array_free(self->next_hops, TRUE);
log_pipe_free_method(s);
}
static void
_append(LogPipe *to, gpointer *user_data)
{
LogPipe *from = user_data[0];
GList **list = user_data[1];
Arc *arc = arc_new(from, to, ARC_TYPE_NEXT_HOP);
*list = g_list_append(*list, arc);
}
static GList *
_arcs(LogPipe *s)
{
LogMultiplexer *self = (LogMultiplexer *)s;
GList *list = NULL;
g_ptr_array_foreach(self->next_hops, (GFunc)_append, (gpointer[2])
{
self, &list
});
if (s->pipe_next)
list = g_list_append(list, arc_new((LogPipe *)self, s->pipe_next, ARC_TYPE_PIPE_NEXT));
return list;
};
LogMultiplexer *
log_multiplexer_new(GlobalConfig *cfg)
{
LogMultiplexer *self = g_new0(LogMultiplexer, 1);
log_pipe_init_instance(&self->super, cfg);
self->super.init = log_multiplexer_init;
self->super.deinit = log_multiplexer_deinit;
self->super.queue = log_multiplexer_queue;
self->super.free_fn = log_multiplexer_free;
self->next_hops = g_ptr_array_new();
self->super.arcs = _arcs;
log_pipe_add_info(&self->super, "multiplexer");
return self;
}
|