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
|
/*
* 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-client.h"
#include "messages.h"
#include <errno.h>
static gboolean
log_proto_text_client_prepare(LogProtoClient *s, gint *fd, GIOCondition *cond, gint *timeout)
{
LogProtoTextClient *self = (LogProtoTextClient *) s;
*fd = self->super.transport->fd;
*cond = self->super.transport->cond;
/* if there's no pending I/O in the transport layer, then we want to do a write */
if (*cond == 0)
*cond = G_IO_OUT;
const gboolean pending_write = self->partial != NULL;
if (!pending_write && s->options->timeout > 0)
*timeout = s->options->timeout;
return pending_write;
}
static LogProtoStatus
log_proto_text_client_drop_input(LogProtoClient *s)
{
LogProtoTextClient *self = (LogProtoTextClient *) s;
guchar buf[1024];
gint rc = -1;
do
{
rc = log_transport_read(self->super.transport, buf, sizeof(buf), NULL);
}
while (rc > 0);
if (rc == -1 && errno != EAGAIN)
{
msg_error("Error reading data", evt_tag_int("fd", self->super.transport->fd), evt_tag_error("error"));
return LPS_ERROR;
}
else if (rc == 0)
{
msg_error("EOF occurred while idle", evt_tag_int("fd", log_proto_client_get_fd(&self->super)));
return LPS_ERROR;
}
return LPS_SUCCESS;
}
static LogProtoStatus
log_proto_text_client_flush(LogProtoClient *s)
{
LogProtoTextClient *self = (LogProtoTextClient *) s;
gint rc;
if (!self->partial)
{
return LPS_SUCCESS;
}
/* attempt to flush previously buffered data */
gint len = self->partial_len - self->partial_pos;
rc = log_transport_write(self->super.transport, &self->partial[self->partial_pos], len);
if (rc < 0)
{
if (errno != EAGAIN && errno != EINTR)
{
msg_error("I/O error occurred while writing",
evt_tag_int("fd", self->super.transport->fd),
evt_tag_error(EVT_TAG_OSERROR));
return LPS_ERROR;
}
return LPS_SUCCESS;
}
if (rc != len)
{
self->partial_pos += rc;
return LPS_PARTIAL;
}
if (self->partial_free)
self->partial_free(self->partial);
self->partial = NULL;
if (self->next_state >= 0)
{
self->state = self->next_state;
self->next_state = -1;
}
log_proto_client_msg_ack(&self->super, 1);
/* NOTE: we return here to give a chance to the framed protocol to send the frame header. */
return LPS_SUCCESS;
}
LogProtoStatus
log_proto_text_client_submit_write(LogProtoClient *s, guchar *msg, gsize msg_len, GDestroyNotify msg_free,
gint next_state)
{
LogProtoTextClient *self = (LogProtoTextClient *) s;
g_assert(self->partial == NULL);
self->partial = msg;
self->partial_len = msg_len;
self->partial_pos = 0;
self->partial_free = msg_free;
self->next_state = next_state;
return log_proto_text_client_flush(s);
}
/*
* log_proto_text_client_post:
* @msg: formatted log message to send (this might be consumed by this function)
* @msg_len: length of @msg
* @consumed: pointer to a gboolean that gets set if the message was consumed by this function
* @error: error information, if any
*
* This function posts a message to the log transport, performing buffering
* of partially sent data if needed. The return value indicates whether we
* successfully sent this message, or if it should be resent by the caller.
**/
static LogProtoStatus
log_proto_text_client_post(LogProtoClient *s, LogMessage *logmsg, guchar *msg, gsize msg_len, gboolean *consumed)
{
LogProtoTextClient *self = (LogProtoTextClient *) s;
/* try to flush already buffered data */
*consumed = FALSE;
const LogProtoStatus status = log_proto_text_client_flush(s);
if (status == LPS_ERROR)
{
/* log_proto_flush() already logs in the case of an error */
return status;
}
if (self->partial || LPS_PARTIAL == status)
{
/* NOTE: the partial buffer has not been emptied yet even with the
* flush above, we shouldn't attempt to write again.
*
* Otherwise: with the framed protocol this could case the frame
* header to be split, and interleaved with message payload, as in:
*
* First bytes of frame header || payload || tail of frame header.
*
* This obviously would cause the framing to break. Also libssl
* returns an error in this case, which is how this was discovered.
*/
return LPS_PARTIAL;
}
*consumed = TRUE;
return log_proto_text_client_submit_write(s, msg, msg_len, (GDestroyNotify) g_free, -1);
}
void
log_proto_text_client_free(LogProtoClient *s)
{
LogProtoTextClient *self = (LogProtoTextClient *)s;
if (self->partial_free)
self->partial_free(self->partial);
self->partial = NULL;
log_proto_client_free_method(s);
};
void
log_proto_text_client_init(LogProtoTextClient *self, LogTransport *transport, const LogProtoClientOptions *options)
{
log_proto_client_init(&self->super, transport, options);
self->super.prepare = log_proto_text_client_prepare;
self->super.flush = log_proto_text_client_flush;
if (options->drop_input)
self->super.process_in = log_proto_text_client_drop_input;
self->super.post = log_proto_text_client_post;
self->super.free_fn = log_proto_text_client_free;
self->super.transport = transport;
self->next_state = -1;
}
LogProtoClient *
log_proto_text_client_new(LogTransport *transport, const LogProtoClientOptions *options)
{
LogProtoTextClient *self = g_new0(LogProtoTextClient, 1);
log_proto_text_client_init(self, transport, options);
return &self->super;
}
|