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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
* 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 "logproto-text-server.h"
#include "messages.h"
#include <string.h>
LogProtoPrepareAction
log_proto_text_server_prepare_method(LogProtoServer *s, GIOCondition *cond, gint *timeout)
{
LogProtoTextServer *self = (LogProtoTextServer *) s;
gboolean avail;
LogProtoPrepareAction action = log_proto_buffered_server_prepare(s, cond, timeout);
if (action != LPPA_POLL_IO)
return action;
avail = (self->cached_eol_pos != 0);
return avail ? LPPA_FORCE_SCHEDULE_FETCH : LPPA_POLL_IO;
}
static gint
log_proto_text_server_accumulate_line(LogProtoTextServer *self, const guchar *msg, gsize msg_len,
gssize consumed_len)
{
if (self->multi_line)
{
const guchar *segment = &msg[consumed_len + 1];
gsize segment_len = msg_len - (segment - msg);
return multi_line_logic_accumulate_line(self->multi_line,
msg, consumed_len < 0 ? 0 : consumed_len,
segment, segment_len);
}
return MLL_CONSUME_SEGMENT | MLL_EXTRACTED;
}
static gboolean
log_proto_text_server_try_extract(LogProtoTextServer *self, LogProtoBufferedServerState *state,
const guchar *buffer_start, gsize buffer_bytes, const guchar *eol, const guchar **msg, gsize *msg_len)
{
gint verdict;
guint32 next_line_pos;
guint32 next_eol_pos = 0;
next_line_pos = eol + 1 - self->super.buffer;
if (state->pending_buffer_end != next_line_pos)
{
const guchar *eom;
/* we have some more data in the buffer, check if we have a
* subsequent EOL there. It indicates whether we need to
* read further data, or the buffer already contains a
* complete line */
eom = self->find_eom(self->super.buffer + next_line_pos, state->pending_buffer_end - next_line_pos);
if (eom)
next_eol_pos = eom - self->super.buffer;
}
*msg_len = eol - buffer_start;
*msg = buffer_start;
verdict = log_proto_text_server_accumulate_line(self, *msg, *msg_len, self->consumed_len);
if (verdict & MLL_EXTRACTED)
{
if (verdict & MLL_CONSUME_SEGMENT)
{
gint drop_length = (verdict & MLL_CONSUME_PARTIAL_AMOUNT_MASK) >> MLL_CONSUME_PARTIAL_AMOUNT_SHIFT;
state->pending_buffer_pos = next_line_pos;
self->cached_eol_pos = next_eol_pos;
if (drop_length)
*msg_len -= drop_length;
}
else if (verdict & MLL_REWIND_SEGMENT)
{
if (self->consumed_len >= 0)
*msg_len = self->consumed_len;
else
*msg_len = 0;
state->pending_buffer_pos = (buffer_start + self->consumed_len + 1) - self->super.buffer;
self->cached_eol_pos = eol - self->super.buffer;
}
else
g_assert_not_reached();
self->consumed_len = -1;
}
else if (verdict & MLL_WAITING)
{
*msg = NULL;
*msg_len = 0;
if (verdict & MLL_CONSUME_SEGMENT)
{
gint drop_length = (verdict & MLL_CONSUME_PARTIAL_AMOUNT_MASK) >> MLL_CONSUME_PARTIAL_AMOUNT_SHIFT;
/* NOTE: we can't partially consume a line at the middle of a
* multi-line construct, as that would mean we would have to copy
* stuff to a separate buffer, which we don't at the moment for
* performance reasons.
*
* No implementation of the MultiLineLogic interface does that and
* honestly I can't even see a use-case at the moment: we only use
* MLL_CONSUME_PARTIALLY() to truncate the end of the last line
* (when "garbage" is found). Should we ever want to add an
* implementation that extracts only parts of the incoming lines,
* even in the middle, we would probably have to check the
* interface, and validate at init() time that LogProtoTextBuffer
* is unable to work with something that does that.
*
* At the moment we will violently crash if this happens with a
* SIGABRT.
*/
g_assert(drop_length == 0);
self->cached_eol_pos = next_eol_pos;
self->consumed_len = eol - buffer_start;
}
else if (verdict & MLL_REWIND_SEGMENT)
{
/* when we are waiting for another line, the current one
* can't be rewinded, so MLL_REWIND_SEGMENT is not valid */
g_assert_not_reached();
}
else
{
g_assert_not_reached();
}
return FALSE;
}
else
g_assert_not_reached();
return TRUE;
}
static gboolean
log_proto_text_server_extract(LogProtoTextServer *self, LogProtoBufferedServerState *state, const guchar *buffer_start,
gsize buffer_bytes, const guchar *eol, const guchar **msg, gsize *msg_len)
{
do
{
if (log_proto_text_server_try_extract(self, state, buffer_start, buffer_bytes, eol, msg, msg_len))
return TRUE;
eol = self->super.buffer + self->cached_eol_pos;
}
while (self->cached_eol_pos > 0);
return FALSE;
}
static void
log_proto_text_server_remove_trailing_newline(const guchar **msg, gsize *msg_len)
{
const guchar *msg_start = (*msg);
const guchar *msg_end = msg_start + (*msg_len);
/* msg_end points at the newline character. A \r or \0 may precede
* this which should be removed from the message body */
while ((msg_end > msg_start) && (msg_end[-1] == '\r' || msg_end[-1] == '\n' || msg_end[-1] == 0))
msg_end--;
*msg_len = msg_end - msg_start;
}
static inline void
log_proto_text_server_yield_whole_buffer_as_message(LogProtoTextServer *self, LogProtoBufferedServerState *state,
const guchar *buffer_start, gsize buffer_bytes, const guchar **msg, gsize *msg_len)
{
/* no EOL, our buffer is full, no way to move forward, return
* everything we have in our buffer. */
*msg = buffer_start;
*msg_len = buffer_bytes;
self->consumed_len = -1;
state->pending_buffer_pos = (*msg) + (*msg_len) - self->super.buffer;
}
static inline const guchar *
log_proto_text_server_locate_next_eol(LogProtoTextServer *self, LogProtoBufferedServerState *state,
const guchar *buffer_start, gsize buffer_bytes)
{
const guchar *eol;
if (self->cached_eol_pos)
{
/* previous invocation was nice enough to save a cached EOL
* pointer, no need to look it up again */
eol = self->super.buffer + self->cached_eol_pos;
self->cached_eol_pos = 0;
}
else
{
eol = self->find_eom(buffer_start + self->consumed_len + 1, buffer_bytes - self->consumed_len - 1);
}
return eol;
}
static gboolean
log_proto_text_server_message_size_too_large(LogProtoTextServer *self, gsize buffer_bytes)
{
return buffer_bytes >= self->super.super.options->max_msg_size;
}
static inline gboolean
_fetch_msg_from_buffer(LogProtoTextServer *self, LogProtoBufferedServerState *state,
const guchar *buffer_start, gsize buffer_bytes,
const guchar **msg, gsize *msg_len)
{
const guchar *eol = log_proto_text_server_locate_next_eol(self, state, buffer_start, buffer_bytes);
if (!eol)
{
if (log_proto_text_server_message_size_too_large(self, buffer_bytes)
|| log_proto_buffered_server_is_input_closed(&self->super))
{
log_proto_text_server_yield_whole_buffer_as_message(self, state, buffer_start, buffer_bytes, msg, msg_len);
goto success;
}
return FALSE;
}
if (log_proto_text_server_extract(self, state, buffer_start, buffer_bytes, eol, msg, msg_len))
goto success;
if (log_proto_text_server_message_size_too_large(self, buffer_bytes))
{
log_proto_text_server_yield_whole_buffer_as_message(self, state, buffer_start, buffer_bytes, msg, msg_len);
goto success;
}
return FALSE;
success:
log_proto_text_server_remove_trailing_newline(msg, msg_len);
return TRUE;
}
static gboolean
log_proto_text_server_fetch_from_buffer(LogProtoBufferedServer *s, const guchar *buffer_start, gsize buffer_bytes,
const guchar **msg, gsize *msg_len)
{
LogProtoTextServer *self = (LogProtoTextServer *) s;
LogProtoBufferedServerState *state = log_proto_buffered_server_get_state(&self->super);
gboolean result = _fetch_msg_from_buffer(self, state, buffer_start, buffer_bytes, msg, msg_len);
log_proto_buffered_server_put_state(&self->super);
return result;
}
static void
log_proto_text_server_flush(LogProtoBufferedServer *s)
{
LogProtoTextServer *self = (LogProtoTextServer *) s;
self->consumed_len = -1;
self->cached_eol_pos = 0;
}
void
log_proto_text_server_set_multi_line(LogProtoServer *s, MultiLineLogic *multi_line)
{
LogProtoTextServer *self = (LogProtoTextServer *) s;
if (self->multi_line)
multi_line_logic_free(self->multi_line);
self->multi_line = multi_line;
}
void
log_proto_text_server_free(LogProtoServer *s)
{
LogProtoTextServer *self = (LogProtoTextServer *) s;
if (self->multi_line)
multi_line_logic_free(self->multi_line);
log_proto_buffered_server_free_method(&self->super.super);
}
void
log_proto_text_server_init(LogProtoTextServer *self, LogTransport *transport, const LogProtoServerOptions *options)
{
log_proto_buffered_server_init(&self->super, transport, options);
self->super.super.prepare = log_proto_text_server_prepare_method;
self->super.super.free_fn = log_proto_text_server_free;
self->super.fetch_from_buffer = log_proto_text_server_fetch_from_buffer;
self->super.flush = log_proto_text_server_flush;
self->find_eom = find_eom;
self->super.stream_based = TRUE;
self->consumed_len = -1;
}
LogProtoServer *
log_proto_text_server_new(LogTransport *transport, const LogProtoServerOptions *options)
{
LogProtoTextServer *self = g_new0(LogProtoTextServer, 1);
log_proto_text_server_init(self, transport, options);
return &self->super.super;
}
static const guchar *
_find_nl_as_eom(const guchar *s, gsize n)
{
return memchr(s, '\n', n);
}
LogProtoServer *
log_proto_text_with_nuls_server_new(LogTransport *transport, const LogProtoServerOptions *options)
{
LogProtoTextServer *self = g_new0(LogProtoTextServer, 1);
log_proto_text_server_init(self, transport, options);
self->find_eom = _find_nl_as_eom;
return &self->super.super;
}
|