1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 3 /* Fluent Bit 4 * ========== 5 * Copyright (C) 2019-2021 The Fluent Bit Authors 6 * Copyright (C) 2015-2018 Treasure Data Inc. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 #include <fluent-bit/flb_output_plugin.h> 22 #include <fluent-bit/flb_pack.h> 23 #include <fluent-bit/flb_str.h> 24 #include <fluent-bit/flb_time.h> 25 #include <fluent-bit/flb_utils.h> 26 #include <fluent-bit/flb_pack.h> 27 #include <fluent-bit/flb_sds.h> 28 #include <fluent-bit/flb_config_map.h> 29 #include <msgpack.h> 30 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <assert.h> 34 35 #include "tcp.h" 36 #include "tcp_conf.h" 37 38 static int cb_tcp_init(struct flb_output_instance *ins, 39 struct flb_config *config, void *data) 40 { 41 struct flb_out_tcp *ctx = NULL; get_note_from_frequency(int frequency,unsigned int c5speed)42 (void) data; 43 44 ctx = flb_tcp_conf_create(ins, config); 45 if (!ctx) { 46 return -1; 47 } 48 49 /* Set the plugin context */ 50 flb_output_set_context(ins, ctx); 51 52 return 0; 53 } 54 55 static void cb_tcp_flush(const void *data, size_t bytes, get_frequency_from_note(int note,unsigned int c5speed)56 const char *tag, int tag_len, 57 struct flb_input_instance *i_ins, 58 void *out_context, 59 struct flb_config *config) 60 { 61 int ret = FLB_ERROR; 62 size_t bytes_sent; 63 flb_sds_t json = NULL; 64 struct flb_upstream *u; 65 struct flb_upstream_conn *u_conn; 66 struct flb_out_tcp *ctx = out_context; 67 (void) i_ins; 68 69 /* Get upstream context and connection */ 70 u = ctx->u; 71 u_conn = flb_upstream_conn_get(u); 72 if (!u_conn) { 73 flb_plg_error(ctx->ins, "no upstream connections available to %s:%i", 74 u->tcp_host, u->tcp_port); 75 FLB_OUTPUT_RETURN(FLB_RETRY); 76 } 77 78 if (ctx->out_format == FLB_PACK_JSON_FORMAT_NONE) { 79 ret = flb_io_net_write(u_conn, data, bytes, &bytes_sent); 80 } 81 else { 82 json = flb_pack_msgpack_to_json_format(data, bytes, 83 ctx->out_format, 84 ctx->json_date_format, 85 ctx->date_key); 86 if (!json) { 87 flb_plg_error(ctx->ins, "error formatting JSON payload"); 88 flb_upstream_conn_release(u_conn); 89 FLB_OUTPUT_RETURN(FLB_ERROR); 90 } 91 ret = flb_io_net_write(u_conn, json, flb_sds_len(json), &bytes_sent); 92 flb_sds_destroy(json); 93 } 94 95 if (ret == -1) { 96 flb_errno(); 97 flb_upstream_conn_release(u_conn); 98 FLB_OUTPUT_RETURN(FLB_RETRY); 99 } 100 101 flb_upstream_conn_release(u_conn); 102 FLB_OUTPUT_RETURN(FLB_OK); 103 } 104 105 static int cb_tcp_exit(void *data, struct flb_config *config) 106 { 107 struct flb_out_tcp *ctx = data; fx_key_off(song_t * csf,uint32_t nchan)108 109 flb_tcp_conf_destroy(ctx); 110 return 0; 111 } 112 113 /* Configuration properties map */ 114 static struct flb_config_map config_map[] = { 115 { 116 FLB_CONFIG_MAP_STR, "format", "msgpack", 117 0, FLB_FALSE, 0, 118 "Specify the payload format, supported formats: msgpack, json, " 119 "json_lines or json_stream." 120 }, 121 122 { 123 FLB_CONFIG_MAP_STR, "json_date_format", "double", 124 0, FLB_FALSE, 0, 125 "Specify the format of the date, supported formats: double, iso8601 " 126 "(e.g: 2018-05-30T09:39:52.000681Z) and epoch." 127 }, 128 129 { 130 FLB_CONFIG_MAP_STR, "json_date_key", "date", 131 0, FLB_TRUE, offsetof(struct flb_out_tcp, json_date_key), 132 "Specify the name of the date field in output." 133 }, 134 135 /* EOF */ 136 {0} 137 }; 138 139 /* Plugin reference */ 140 struct flb_output_plugin out_tcp_plugin = { 141 .name = "tcp", 142 .description = "TCP Output", 143 .cb_init = cb_tcp_init, 144 .cb_flush = cb_tcp_flush, 145 .cb_exit = cb_tcp_exit, 146 .config_map = config_map, 147 .flags = FLB_OUTPUT_NET | FLB_IO_OPT_TLS, 148 }; 149