syslog-ng source
logproto-client.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2012 Balabit
3  * Copyright (c) 1998-2012 Balázs Scheidler
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  *
23  */
24 
25 #ifndef LOGPROTO_CLIENT_H_INCLUDED
26 #define LOGPROTO_CLIENT_H_INCLUDED
27 
28 #include "logproto.h"
29 #include "persist-state.h"
30 
31 typedef struct _LogProtoClient LogProtoClient;
32 
33 #define LOG_PROTO_CLIENT_OPTIONS_SIZE 128
34 
35 typedef struct _LogProtoClientOptions
36 {
37  gboolean drop_input;
40 
41 typedef union _LogProtoClientOptionsStorage
42 {
46 // _Static_assert() is a C11 feature, so we use a typedef trick to perform the static assertion
49 
50 typedef void (*LogProtoClientAckCallback)(gint num_msg_acked, gpointer user_data);
51 typedef void (*LogProtoClientRewindCallback)(gpointer user_data);
52 
53 typedef struct
54 {
57  gpointer user_data;
59 
63 
67 
69 {
72  LogTransportStack transport_stack;
73  gboolean (*poll_prepare)(LogProtoClient *s, GIOCondition *cond, GIOCondition *idle_cond, gint *timeout);
74  LogProtoStatus (*post)(LogProtoClient *s, LogMessage *logmsg, guchar *msg, gsize msg_len, gboolean *consumed);
75  LogProtoStatus (*process_in)(LogProtoClient *s);
76  LogProtoStatus (*flush)(LogProtoClient *s);
77  gboolean (*validate_options)(LogProtoClient *s);
78  LogProtoStatus (*handshake)(LogProtoClient *s, gboolean *handshake_finished);
79  gboolean (*restart_with_state)(LogProtoClient *s, PersistState *state, const gchar *persist_name);
80  void (*free_fn)(LogProtoClient *s);
82 };
83 
84 static inline void
85 log_proto_client_set_client_flow_control(LogProtoClient *self, LogProtoClientFlowControlFuncs *flow_control_funcs)
86 {
87  self->flow_control_funcs.ack_callback = flow_control_funcs->ack_callback;
88  self->flow_control_funcs.rewind_callback = flow_control_funcs->rewind_callback;
89  self->flow_control_funcs.user_data = flow_control_funcs->user_data;
90 }
91 static inline void
92 log_proto_client_msg_ack(LogProtoClient *self, gint num_msg_acked)
93 {
94  if (self->flow_control_funcs.ack_callback)
95  self->flow_control_funcs.ack_callback(num_msg_acked, self->flow_control_funcs.user_data);
96 }
97 
98 static inline void
99 log_proto_client_msg_rewind(LogProtoClient *self)
100 {
101  if (self->flow_control_funcs.rewind_callback)
102  self->flow_control_funcs.rewind_callback(self->flow_control_funcs.user_data);
103 }
104 
105 static inline void
106 log_proto_client_set_options(LogProtoClient *self, const LogProtoClientOptionsStorage *options)
107 {
108  self->options = options;
109 }
110 
111 static inline gboolean
112 log_proto_client_validate_options(LogProtoClient *self)
113 {
114  return self->validate_options(self);
115 }
116 
117 static inline LogProtoStatus
118 log_proto_client_handshake(LogProtoClient *s, gboolean *handshake_finished)
119 {
120  if (s->handshake)
121  {
122  return s->handshake(s, handshake_finished);
123  }
124  *handshake_finished = TRUE;
125  return LPS_SUCCESS;
126 }
127 
128 static inline gboolean
129 log_proto_client_poll_prepare(LogProtoClient *self, GIOCondition *cond, GIOCondition *idle_cond, gint *timeout)
130 {
131  GIOCondition transport_cond = 0;
132  gboolean result = TRUE;
133 
134  if (log_transport_stack_poll_prepare(&self->transport_stack, &transport_cond))
135  goto exit;
136 
137  result = self->poll_prepare(self, cond, idle_cond, timeout);
138 
139  if (!result && *timeout < 0)
140  *timeout = self->options->super.idle_timeout;
141 
142 exit:
143  /* transport I/O needs take precedence */
144  if (transport_cond != 0)
145  *cond = transport_cond;
146 
147  return result;
148 }
149 
150 static inline LogProtoStatus log_proto_client_process_in(LogProtoClient *s);
151 
152 static inline LogProtoStatus
153 log_proto_client_flush(LogProtoClient *self)
154 {
155  if (log_transport_stack_get_io_requirement(&self->transport_stack) == LTIO_READ_WANTS_WRITE)
156  return self->process_in(self);
157 
158  return self->flush(self);
159 }
160 
161 static inline LogProtoStatus
162 log_proto_client_process_in(LogProtoClient *self)
163 {
164  if (log_transport_stack_get_io_requirement(&self->transport_stack) == LTIO_WRITE_WANTS_READ)
165  return self->flush(self);
166 
167  return self->process_in(self);
168 }
169 
170 static inline LogProtoStatus
171 log_proto_client_post(LogProtoClient *s, LogMessage *logmsg, guchar *msg, gsize msg_len, gboolean *consumed)
172 {
173  return s->post(s, logmsg, msg, msg_len, consumed);
174 }
175 
176 static inline gint
177 log_proto_client_get_fd(LogProtoClient *s)
178 {
179  /* FIXME: Layering violation */
180  return s->transport_stack.fd;
181 }
182 
183 static inline void
184 log_proto_client_reset_error(LogProtoClient *s)
185 {
186  s->status = LPS_SUCCESS;
187 }
188 
189 static inline gboolean
190 log_proto_client_restart_with_state(LogProtoClient *s, PersistState *state, const gchar *persist_name)
191 {
192  if (s->restart_with_state)
193  return s->restart_with_state(s, state, persist_name);
194  return FALSE;
195 }
196 
197 gboolean log_proto_client_validate_options(LogProtoClient *self);
198 void log_proto_client_init(LogProtoClient *s, LogTransport *transport, const LogProtoClientOptionsStorage *options);
199 void log_proto_client_free(LogProtoClient *s);
200 void log_proto_client_free_method(LogProtoClient *s);
201 
202 #define DEFINE_LOG_PROTO_CLIENT(prefix, options...) \
203  static gpointer \
204  prefix ## _client_plugin_construct(Plugin *self) \
205  { \
206  static LogProtoClientFactory proto = { \
207  .construct = prefix ## _client_new, \
208  .stateful = FALSE, \
209  ##options \
210  }; \
211  return &proto; \
212  }
213 
214 #define LOG_PROTO_CLIENT_PLUGIN(prefix, __name) \
215  { \
216  .type = LL_CONTEXT_CLIENT_PROTO, \
217  .name = __name, \
218  .construct = prefix ## _client_plugin_construct, \
219  }
220 
221 #define LOG_PROTO_CLIENT_PLUGIN_WITH_GRAMMAR(__parser, __name) \
222  { \
223  .type = LL_CONTEXT_CLIENT_PROTO, \
224  .name = __name, \
225  .parser = &__parser, \
226  }
227 
228 typedef struct _LogProtoClientFactory LogProtoClientFactory;
229 
231 {
232  LogProtoClient *(*construct)(LogTransport *transport, const LogProtoClientOptionsStorage *options);
234  gboolean stateful;
235 };
236 
237 static inline LogProtoClient *
238 log_proto_client_factory_construct(LogProtoClientFactory *self, LogTransport *transport,
239  const LogProtoClientOptionsStorage *options)
240 {
241  return self->construct(transport, options);
242 }
243 
244 static inline gboolean
245 log_proto_client_factory_is_proto_stateful(LogProtoClientFactory *self)
246 {
247  return self->stateful;
248 }
249 
250 LogProtoClientFactory *log_proto_client_get_factory(PluginContext *context, const gchar *name);
251 
252 #endif
const gchar * name
Definition: debugger.c:265
void log_proto_client_free_method(LogProtoClient *s)
Definition: logproto-client.c:39
void(* LogProtoClientRewindCallback)(gpointer user_data)
Definition: logproto-client.h:51
gint log_proto_client_options_get_timeout(LogProtoClientOptionsStorage *options)
Definition: logproto-client.c:75
void log_proto_client_options_defaults(LogProtoClientOptionsStorage *options)
Definition: logproto-client.c:81
#define LOG_PROTO_CLIENT_OPTIONS_SIZE
Definition: logproto-client.h:33
void log_proto_client_options_destroy(LogProtoClientOptionsStorage *options)
Definition: logproto-client.c:93
void log_proto_client_options_init(LogProtoClientOptionsStorage *options, GlobalConfig *cfg)
Definition: logproto-client.c:88
void log_proto_client_options_set_timeout(LogProtoClientOptionsStorage *options, gint timeout)
Definition: logproto-client.c:69
void log_proto_client_options_set_drop_input(LogProtoClientOptionsStorage *options, gboolean drop_input)
Definition: logproto-client.c:63
LogProtoClientFactory * log_proto_client_get_factory(PluginContext *context, const gchar *name)
Definition: logproto-client.c:98
char static_assert_size_check_LogProtoClientOptions[LOG_PROTO_CLIENT_OPTIONS_SIZE >=sizeof(LogProtoClientOptions) ? 1 :-1]
Definition: logproto-client.h:48
void log_proto_client_init(LogProtoClient *s, LogTransport *transport, const LogProtoClientOptionsStorage *options)
Definition: logproto-client.c:54
void log_proto_client_free(LogProtoClient *s)
Definition: logproto-client.c:46
void(* LogProtoClientAckCallback)(gint num_msg_acked, gpointer user_data)
Definition: logproto-client.h:50
LogProtoStatus
Definition: logproto.h:33
@ LPS_SUCCESS
Definition: logproto.h:34
@ LTIO_WRITE_WANTS_READ
Definition: logtransport.h:37
@ LTIO_READ_WANTS_WRITE
Definition: logtransport.h:35
#define self
Definition: rcptid.c:38
Definition: logproto-client.h:54
gpointer user_data
Definition: logproto-client.h:57
LogProtoClientAckCallback ack_callback
Definition: logproto-client.h:55
LogProtoClientRewindCallback rewind_callback
Definition: logproto-client.h:56
Definition: logproto-client.h:36
gboolean drop_input
Definition: logproto-client.h:37
gint idle_timeout
Definition: logproto-client.h:38
Definition: logproto-client.h:231
gboolean stateful
Definition: logproto-client.h:234
gint default_inet_port
Definition: logproto-client.h:233
Definition: logproto-client.h:69
LogProtoStatus status
Definition: logproto-client.h:70
gboolean(* validate_options)(LogProtoClient *s)
Definition: logproto-client.h:77
LogProtoStatus(* flush)(LogProtoClient *s)
Definition: logproto-client.h:76
LogProtoStatus(* process_in)(LogProtoClient *s)
Definition: logproto-client.h:75
LogProtoStatus(* post)(LogProtoClient *s, LogMessage *logmsg, guchar *msg, gsize msg_len, gboolean *consumed)
Definition: logproto-client.h:74
const LogProtoClientOptionsStorage * options
Definition: logproto-client.h:71
void(* free_fn)(LogProtoClient *s)
Definition: logproto-client.h:80
gboolean(* restart_with_state)(LogProtoClient *s, PersistState *state, const gchar *persist_name)
Definition: logproto-client.h:79
gboolean(* poll_prepare)(LogProtoClient *s, GIOCondition *cond, GIOCondition *idle_cond, gint *timeout)
Definition: logproto-client.h:73
LogTransportStack transport_stack
Definition: logproto-client.h:72
LogProtoClientFlowControlFuncs flow_control_funcs
Definition: logproto-client.h:81
LogProtoStatus(* handshake)(LogProtoClient *s, gboolean *handshake_finished)
Definition: logproto-client.h:78
GlobalConfig * cfg
Definition: test_batched_ack_tracker.c:34
CSVScannerOptions options
Definition: test_csv_scanner.c:30
GString * result
Definition: test_lexer_block.c:34
LogMessage * msg
Definition: test_rename.c:35
struct @95 state
Definition: logproto-client.h:42
LogProtoClientOptions super
Definition: logproto-client.h:43