Coverage Report

Created: 2025-10-14 08:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fluent-bit/plugins/in_udp/udp_config.c
Line
Count
Source
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*  Fluent Bit
4
 *  ==========
5
 *  Copyright (C) 2015-2024 The Fluent Bit Authors
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License");
8
 *  you may not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *      http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS,
15
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 */
19
20
#include <fluent-bit/flb_input_plugin.h>
21
#include <fluent-bit/flb_utils.h>
22
#include <fluent-bit/flb_unescape.h>
23
24
#include "udp.h"
25
#include "udp_conn.h"
26
#include "udp_config.h"
27
28
#include <stdlib.h>
29
30
struct flb_in_udp_config *udp_config_init(struct flb_input_instance *ins)
31
0
{
32
0
    int ret;
33
0
    int len;
34
0
    char port[16];
35
0
    char *out;
36
0
    struct flb_in_udp_config *ctx;
37
38
    /* Allocate plugin context */
39
0
    ctx = flb_calloc(1, sizeof(struct flb_in_udp_config));
40
0
    if (!ctx) {
41
0
        flb_errno();
42
0
        return NULL;
43
0
    }
44
0
    ctx->ins = ins;
45
0
    ctx->format = FLB_UDP_FMT_JSON;
46
47
    /* Load the config map */
48
0
    ret = flb_input_config_map_set(ins, (void *)ctx);
49
0
    if (ret == -1) {
50
0
        flb_plg_error(ins, "unable to load configuration");
51
0
        flb_free(ctx);
52
0
        return NULL;
53
0
    }
54
55
    /* Data format (expected payload) */
56
0
    if (ctx->format_name) {
57
0
        if (strcasecmp(ctx->format_name, "json") == 0) {
58
0
            ctx->format = FLB_UDP_FMT_JSON;
59
0
        }
60
0
        else if (strcasecmp(ctx->format_name, "none") == 0) {
61
0
            ctx->format = FLB_UDP_FMT_NONE;
62
0
        }
63
0
        else {
64
0
            flb_plg_error(ctx->ins, "unrecognized format value '%s'", ctx->format_name);
65
0
            flb_free(ctx);
66
0
            return NULL;
67
0
        }
68
0
    }
69
70
    /* String separator used to split records when using 'format none' */
71
0
    if (ctx->raw_separator) {
72
0
        len = strlen(ctx->raw_separator);
73
0
        out = flb_malloc(len + 1);
74
0
        if (!out) {
75
0
            flb_errno();
76
0
            flb_free(ctx);
77
0
            return NULL;
78
0
        }
79
0
        ret = flb_unescape_string(ctx->raw_separator, len, &out);
80
0
        if (ret <= 0) {
81
0
            flb_plg_error(ctx->ins, "invalid separator");
82
0
            flb_free(out);
83
0
            flb_free(ctx);
84
0
            return NULL;
85
0
        }
86
87
0
        ctx->separator = flb_sds_create_len(out, ret);
88
0
        if (!ctx->separator) {
89
0
            flb_free(out);
90
0
            flb_free(ctx);
91
0
            return NULL;
92
0
        }
93
0
        flb_free(out);
94
0
    }
95
0
    if (!ctx->separator) {
96
0
        ctx->separator = flb_sds_create_len("\n", 1);
97
0
    }
98
99
    /* Listen interface (if not set, defaults to 0.0.0.0:5170) */
100
0
    flb_input_net_default_listener("0.0.0.0", 5170, ins);
101
0
    ctx->listen = ins->host.listen;
102
0
    snprintf(port, sizeof(port) - 1, "%d", ins->host.port);
103
0
    ctx->port = flb_strdup(port);
104
105
    /* Chunk size */
106
0
    if (ctx->chunk_size_str) {
107
        /* Convert KB unit to Bytes */
108
0
        ctx->chunk_size  = (atoi(ctx->chunk_size_str) * 1024);
109
0
    } else {
110
0
        ctx->chunk_size  = atoi(FLB_IN_UDP_CHUNK);
111
0
    }
112
113
    /* Buffer size */
114
0
    if (!ctx->buffer_size_str) {
115
0
        ctx->buffer_size = ctx->chunk_size;
116
0
    }
117
0
    else {
118
        /* Convert KB unit to Bytes */
119
0
        ctx->buffer_size  = (atoi(ctx->buffer_size_str) * 1024);
120
0
    }
121
122
0
    ctx->log_encoder = flb_log_event_encoder_create(FLB_LOG_EVENT_FORMAT_DEFAULT);
123
124
0
    if (ctx->log_encoder == NULL) {
125
0
        flb_plg_error(ctx->ins, "could not initialize event encoder");
126
0
        udp_config_destroy(ctx);
127
128
0
        ctx = NULL;
129
0
    }
130
131
0
    return ctx;
132
0
}
133
134
int udp_config_destroy(struct flb_in_udp_config *ctx)
135
0
{
136
0
    if (ctx->log_encoder != NULL) {
137
0
        flb_log_event_encoder_destroy(ctx->log_encoder);
138
0
    }
139
140
0
    if (ctx->collector_id != -1) {
141
0
        flb_input_collector_delete(ctx->collector_id, ctx->ins);
142
143
0
        ctx->collector_id = -1;
144
0
    }
145
146
0
    if (ctx->downstream != NULL) {
147
0
        flb_downstream_destroy(ctx->downstream);
148
0
    }
149
150
0
    flb_sds_destroy(ctx->separator);
151
0
    flb_free(ctx->port);
152
0
    flb_free(ctx);
153
154
0
    return 0;
155
0
}