summaryrefslogtreecommitdiffstats
path: root/fluent-bit/plugins/in_syslog/syslog_conn.c
blob: 8785c1e869b13d022925febba2c5638c8275b78e (plain)
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_engine.h>
#include <fluent-bit/flb_network.h>
#include <fluent-bit/flb_downstream.h>

#include "syslog.h"
#include "syslog_conf.h"
#include "syslog_conn.h"
#include "syslog_prot.h"

/* Callback invoked every time an event is triggered for a connection */
int syslog_conn_event(void *data)
{
    struct flb_connection *connection;
    struct syslog_conn    *conn;
    struct flb_syslog     *ctx;

    connection = (struct flb_connection *) data;

    conn = connection->user_data;

    ctx = conn->ctx;

    if (ctx->dgram_mode_flag) {
        return syslog_dgram_conn_event(data);
    }

    return syslog_stream_conn_event(data);
}

int syslog_stream_conn_event(void *data)
{
    int ret;
    int bytes;
    int available;
    size_t size;
    char *tmp;
    struct mk_event *event;
    struct syslog_conn *conn;
    struct flb_syslog *ctx;
    struct flb_connection *connection;

    connection = (struct flb_connection *) data;

    conn = connection->user_data;

    ctx = conn->ctx;

    event = &connection->event;

    if (event->mask & MK_EVENT_READ) {
        available = (conn->buf_size - conn->buf_len) - 1;
        if (available < 1) {
            if (conn->buf_size + ctx->buffer_chunk_size > ctx->buffer_max_size) {
                flb_plg_debug(ctx->ins,
                              "fd=%i incoming data exceed limit (%zd bytes)",
                              event->fd, (ctx->buffer_max_size));
                syslog_conn_del(conn);
                return -1;
            }

            size = conn->buf_size + ctx->buffer_chunk_size;
            tmp = flb_realloc(conn->buf_data, size);
            if (!tmp) {
                flb_errno();
                return -1;
            }
            flb_plg_trace(ctx->ins, "fd=%i buffer realloc %zd -> %zd",
                          event->fd, conn->buf_size, size);

            conn->buf_data = tmp;
            conn->buf_size = size;
            available = (conn->buf_size - conn->buf_len) - 1;
        }

        bytes = flb_io_net_read(connection,
                                (void *) &conn->buf_data[conn->buf_len],
                                available);

        if (bytes > 0) {
            flb_plg_trace(ctx->ins, "read()=%i pre_len=%zu now_len=%zu",
                          bytes, conn->buf_len, conn->buf_len + bytes);
            conn->buf_len += bytes;
            conn->buf_data[conn->buf_len] = '\0';
            ret = syslog_prot_process(conn);
            if (ret == -1) {
                return -1;
            }
            return bytes;
        }
        else {
            flb_plg_trace(ctx->ins, "fd=%i closed connection", event->fd);
            syslog_conn_del(conn);
            return -1;
        }
    }

    if (event->mask & MK_EVENT_CLOSE) {
        flb_plg_trace(ctx->ins, "fd=%i hangup", event->fd);
        syslog_conn_del(conn);
        return -1;
    }
    return 0;
}

int syslog_dgram_conn_event(void *data)
{
    struct flb_connection *connection;
    int                    bytes;
    struct syslog_conn    *conn;

    connection = (struct flb_connection *) data;

    conn = connection->user_data;

    bytes = flb_io_net_read(connection,
                            (void *) &conn->buf_data[conn->buf_len],
                            conn->buf_size - 1);

    if (bytes > 0) {
        conn->buf_data[bytes] = '\0';
        conn->buf_len = bytes;

        syslog_prot_process_udp(conn);
    }
    else {
        flb_errno();
    }

    conn->buf_len = 0;

    return 0;
}

/* Create a new mqtt request instance */
struct syslog_conn *syslog_conn_add(struct flb_connection *connection,
                                    struct flb_syslog *ctx)
{
    int ret;
    struct syslog_conn *conn;

    conn = flb_malloc(sizeof(struct syslog_conn));
    if (!conn) {
        return NULL;
    }

    conn->connection = connection;

    /* Set data for the event-loop */
    MK_EVENT_NEW(&connection->event);

    connection->user_data     = conn;
    connection->event.type    = FLB_ENGINE_EV_CUSTOM;
    connection->event.handler = syslog_conn_event;

    /* Connection info */
    conn->ctx     = ctx;
    conn->ins     = ctx->ins;
    conn->buf_len = 0;
    conn->buf_parsed = 0;

    /* Allocate read buffer */
    conn->buf_data = flb_malloc(ctx->buffer_chunk_size);
    if (!conn->buf_data) {
        flb_errno();

        flb_free(conn);

        return NULL;
    }
    conn->buf_size = ctx->buffer_chunk_size;

    /* Register instance into the event loop if we're in
     * stream mode (UDP events are received through the collector)
     */
    if (!ctx->dgram_mode_flag) {
        ret = mk_event_add(flb_engine_evl_get(),
                           connection->fd,
                           FLB_ENGINE_EV_CUSTOM,
                           MK_EVENT_READ,
                           &connection->event);
        if (ret == -1) {
            flb_plg_error(ctx->ins, "could not register new connection");

            flb_free(conn->buf_data);
            flb_free(conn);

            return NULL;
        }
    }

    mk_list_add(&conn->_head, &ctx->connections);

    return conn;
}

int syslog_conn_del(struct syslog_conn *conn)
{
    /* The downstream unregisters the file descriptor from the event-loop
     * so there's nothing to be done by the plugin
     */
    if (!conn->ctx->dgram_mode_flag) {
        flb_downstream_conn_release(conn->connection);
    }

    /* Release resources */
    mk_list_del(&conn->_head);

    flb_free(conn->buf_data);
    flb_free(conn);

    return 0;
}

int syslog_conn_exit(struct flb_syslog *ctx)
{
    struct mk_list *tmp;
    struct mk_list *head;
    struct syslog_conn *conn;

    mk_list_foreach_safe(head, tmp, &ctx->connections) {
        conn = mk_list_entry(head, struct syslog_conn, _head);
        syslog_conn_del(conn);
    }

    return 0;
}