/src/fluent-bit/plugins/out_udp/udp_conf.c
Line | Count | Source (jump to first uncovered line) |
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_output_plugin.h> |
21 | | #include <fluent-bit/flb_utils.h> |
22 | | #include <fluent-bit/flb_sds.h> |
23 | | #include <fluent-bit/flb_pack.h> |
24 | | |
25 | | #include "udp.h" |
26 | | #include "udp_conf.h" |
27 | | |
28 | | struct flb_out_udp *flb_udp_conf_create(struct flb_output_instance *ins, |
29 | | struct flb_config *config) |
30 | 0 | { |
31 | 0 | int ret; |
32 | 0 | const char *tmp; |
33 | 0 | struct flb_out_udp *ctx = NULL; |
34 | | |
35 | | /* Allocate plugin context */ |
36 | 0 | ctx = flb_calloc(1, sizeof(struct flb_out_udp)); |
37 | 0 | if (!ctx) { |
38 | 0 | flb_errno(); |
39 | 0 | return NULL; |
40 | 0 | } |
41 | 0 | ctx->ins = ins; |
42 | |
|
43 | 0 | ret = flb_output_config_map_set(ins, (void *) ctx); |
44 | 0 | if (ret == -1) { |
45 | 0 | flb_free(ctx); |
46 | 0 | return NULL; |
47 | 0 | } |
48 | | |
49 | | /* Set default network configuration if not set */ |
50 | 0 | flb_output_net_default("127.0.0.1", 5170, ins); |
51 | | |
52 | | /* raw message key mode */ |
53 | 0 | if (ctx->raw_message_key) { |
54 | 0 | ctx->ra_raw_message_key = flb_ra_create(ctx->raw_message_key, FLB_TRUE); |
55 | 0 | if (!ctx->ra_raw_message_key) { |
56 | 0 | flb_plg_error(ctx->ins, "could not create record accessor for raw_message_key"); |
57 | 0 | flb_free(ctx); |
58 | 0 | return NULL; |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | /* Output format */ |
63 | 0 | ctx->out_format = FLB_PACK_JSON_FORMAT_NONE; |
64 | 0 | tmp = flb_output_get_property("format", ins); |
65 | 0 | if (tmp) { |
66 | 0 | ret = flb_pack_to_json_format_type(tmp); |
67 | 0 | if (ret == -1) { |
68 | 0 | flb_plg_error(ctx->ins, "unrecognized 'format' option '%s'. " |
69 | 0 | "Using 'msgpack'", tmp); |
70 | 0 | } |
71 | 0 | else { |
72 | 0 | ctx->out_format = ret; |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | | /* Date key */ |
77 | 0 | ctx->date_key = ctx->json_date_key; |
78 | 0 | tmp = flb_output_get_property("json_date_key", ins); |
79 | 0 | if (tmp) { |
80 | | /* Just check if we have to disable it */ |
81 | 0 | if (flb_utils_bool(tmp) == FLB_FALSE) { |
82 | 0 | ctx->date_key = NULL; |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | /* Date format for JSON output */ |
87 | 0 | ctx->json_date_format = FLB_PACK_JSON_DATE_DOUBLE; |
88 | 0 | tmp = flb_output_get_property("json_date_format", ins); |
89 | 0 | if (tmp) { |
90 | 0 | ret = flb_pack_to_json_date_type(tmp); |
91 | 0 | if (ret == -1) { |
92 | 0 | flb_plg_error(ctx->ins, "unrecognized 'json_date_format' option '%s'. " |
93 | 0 | "Using 'double'", tmp); |
94 | 0 | } |
95 | 0 | else { |
96 | 0 | ctx->json_date_format = ret; |
97 | 0 | } |
98 | 0 | } |
99 | |
|
100 | 0 | ctx->host = ins->host.name; |
101 | 0 | ctx->port = ins->host.port; |
102 | |
|
103 | 0 | ctx->endpoint_descriptor = flb_net_udp_connect(ins->host.name, |
104 | 0 | ins->host.port, |
105 | 0 | ins->net_setup.source_address); |
106 | |
|
107 | 0 | if (ctx->endpoint_descriptor < 0) { |
108 | 0 | flb_udp_conf_destroy(ctx); |
109 | |
|
110 | 0 | flb_plg_error(ctx->ins, "Error creating upstream socket"); |
111 | |
|
112 | 0 | ctx = NULL; |
113 | 0 | } |
114 | |
|
115 | 0 | return ctx; |
116 | 0 | } |
117 | | |
118 | | void flb_udp_conf_destroy(struct flb_out_udp *ctx) |
119 | 0 | { |
120 | 0 | if (!ctx) { |
121 | 0 | return; |
122 | 0 | } |
123 | | |
124 | 0 | if (ctx->ra_raw_message_key) { |
125 | 0 | flb_ra_destroy(ctx->ra_raw_message_key); |
126 | 0 | } |
127 | |
|
128 | 0 | if (ctx->endpoint_descriptor >= 0) { |
129 | 0 | flb_socket_close(ctx->endpoint_descriptor); |
130 | 0 | } |
131 | |
|
132 | 0 | flb_free(ctx); |
133 | |
|
134 | 0 | ctx = NULL; |
135 | 0 | } |