syslog-ng source
logtransport.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2013 Balabit
3  * Copyright (c) 1998-2013 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 LOGTRANSPORT_H_INCLUDED
26 #define LOGTRANSPORT_H_INCLUDED
27 
28 #include "syslog-ng.h"
30 
31 typedef enum _LogTransportIOCond
32 {
39 
40 /*
41  * LogTransport:
42  *
43  * This is an interface that a LogProto implementation can use to do I/O.
44  * There might be multiple LogTransport implementations alive for a specific
45  * connection: for instance we might do both plain text and SSL encrypted
46  * communication on the same socket, when the haproxy proxy protocol is in
47  * use and SSL is enabled. It might also make sense to instantiate a
48  * transport doing gzip compression transparently.
49  *
50  * The combination of interoperating LogTransport instances is called the
51  * LogTransportStack (see transport-stack.h)
52  *
53  * There's a circular, borrowed reference between the stack and the
54  * constituent LogTransport instances.
55  */
56 
57 typedef struct _LogTransport LogTransport;
58 typedef struct _LogTransportStack LogTransportStack;
60 {
61  gint fd;
63 
64  gssize (*read)(LogTransport *self, gpointer buf, gsize count, LogTransportAuxData *aux);
65  gssize (*write)(LogTransport *self, const gpointer buf, gsize count);
66  gssize (*writev)(LogTransport *self, struct iovec *iov, gint iov_count);
67  void (*shutdown)(LogTransport *self);
68  void (*free_fn)(LogTransport *self);
69 
70  /* read ahead */
71  struct
72  {
73  gchar buf[16];
74  gint buf_len;
75  gint pos;
76  } ra;
77  LogTransportStack *stack;
78  const gchar *name;
79 };
80 
81 static inline GIOCondition
82 _log_transport_io_cond(LogTransportIOCond c)
83 {
84  switch (c)
85  {
86  case LTIO_NOTHING:
87  return (GIOCondition) 0;
90  return G_IO_IN;
93  return G_IO_OUT;
94  default:
95  g_assert_not_reached();
96  }
97 
98  g_assert_not_reached();
99 }
100 
101 static inline gboolean
102 log_transport_poll_prepare(LogTransport *self, GIOCondition *cond)
103 {
104  *cond = _log_transport_io_cond(self->cond);
105 
106  if (self->ra.buf_len != self->ra.pos)
107  return TRUE;
108 
109  return FALSE;
110 }
111 
112 static inline LogTransportIOCond
113 log_transport_get_io_requirement(LogTransport *self)
114 {
115  return self->cond;
116 }
117 
118 static inline void
119 log_transport_assign_to_stack(LogTransport *self, LogTransportStack *stack)
120 {
121  self->stack = stack;
122 }
123 
124 static inline gssize
125 log_transport_write(LogTransport *self, const gpointer buf, gsize count)
126 {
127  return self->write(self, buf, count);
128 }
129 
130 static inline gssize
131 log_transport_writev(LogTransport *self, struct iovec *iov, gint iov_count)
132 {
133  return self->writev(self, iov, iov_count);
134 }
135 
136 static inline void
137 log_transport_shutdown(LogTransport *self)
138 {
139  if (self->shutdown)
140  return self->shutdown(self);
141 }
142 
143 gssize _log_transport_combined_read_with_read_ahead(LogTransport *self,
144  gpointer buf, gsize count,
146 
147 static inline gssize
148 log_transport_read(LogTransport *self, gpointer buf, gsize count, LogTransportAuxData *aux)
149 {
150  if (G_LIKELY(self->ra.buf_len == 0))
151  return self->read(self, buf, count, aux);
152 
153  return _log_transport_combined_read_with_read_ahead(self, buf, count, aux);
154 }
155 
156 gssize log_transport_read_ahead(LogTransport *self, gpointer buf, gsize count, gboolean *moved_forward);
157 
158 void log_transport_init_instance(LogTransport *s, const gchar *name, gint fd);
159 void log_transport_free_method(LogTransport *s);
160 void log_transport_free(LogTransport *s);
161 
162 #endif
const gchar * name
Definition: debugger.c:265
gssize _log_transport_combined_read_with_read_ahead(LogTransport *self, gpointer buf, gsize count, LogTransportAuxData *aux)
Definition: logtransport.c:31
void log_transport_free_method(LogTransport *s)
Definition: logtransport.c:124
LogTransportIOCond
Definition: logtransport.h:32
@ LTIO_WRITE_WANTS_WRITE
Definition: logtransport.h:36
@ LTIO_NOTHING
Definition: logtransport.h:33
@ LTIO_READ_WANTS_READ
Definition: logtransport.h:34
@ LTIO_WRITE_WANTS_READ
Definition: logtransport.h:37
@ LTIO_READ_WANTS_WRITE
Definition: logtransport.h:35
void log_transport_free(LogTransport *s)
Definition: logtransport.c:138
gssize log_transport_read_ahead(LogTransport *self, gpointer buf, gsize count, gboolean *moved_forward)
Definition: logtransport.c:83
void log_transport_init_instance(LogTransport *s, const gchar *name, gint fd)
Definition: logtransport.c:129
#define self
Definition: rcptid.c:38
Definition: transport-aux-data.h:30
Definition: transport-stack.h:103
Definition: logtransport.h:60
const gchar * name
Definition: logtransport.h:78
gssize(* read)(LogTransport *self, gpointer buf, gsize count, LogTransportAuxData *aux)
Definition: logtransport.h:64
gint pos
Definition: logtransport.h:75
gssize(* writev)(LogTransport *self, struct iovec *iov, gint iov_count)
Definition: logtransport.h:66
struct _LogTransport::@101 ra
gint fd
Definition: logtransport.h:61
gssize(* write)(LogTransport *self, const gpointer buf, gsize count)
Definition: logtransport.h:65
gint buf_len
Definition: logtransport.h:74
gchar buf[16]
Definition: logtransport.h:73
void(* shutdown)(LogTransport *self)
Definition: logtransport.h:67
LogTransportIOCond cond
Definition: logtransport.h:62
LogTransportStack * stack
Definition: logtransport.h:77
void(* free_fn)(LogTransport *self)
Definition: logtransport.h:68
LogTransportAuxData * aux
Definition: test_aux_data.c:28