<commit_before>/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2016 Treasure Data Inc.
 *
 *  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 <msgpack.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_network.h>

#include "tcp.h"
#include "tcp_conn.h"
#include "tcp_config.h"

static void *in_tcp_flush(void *in_context, size_t *size)
{
    char *buf;
    msgpack_sbuffer *sbuf;
    struct flb_in_tcp_config *ctx = in_context;

    if (ctx->buffer_id == 0|| sbuf->size <= 0) {
        return NULL;
    }

    sbuf = &ctx->mp_sbuf;
    *size = sbuf->size;
    buf = flb_malloc(sbuf->size);
    if (!buf) {
        return NULL;
    }

    /* set a new buffer and re-initialize our MessagePack context */
    memcpy(buf, sbuf->data, sbuf->size);
    msgpack_sbuffer_destroy(&ctx->mp_sbuf);
    msgpack_sbuffer_init(&ctx->mp_sbuf);
    msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write);

    ctx->buffer_id = 0;

    return buf;
}

/*
 * For a server event, the collection event means a new client have arrived, we
 * accept the connection and create a new TCP instance which will wait for
 * JSON map messages.
 */
static int in_tcp_collect(struct flb_config *config, void *in_context)
{
    int fd;
    struct flb_in_tcp_config *ctx = in_context;
    struct tcp_conn *conn;

    /* Accept the new connection */
    fd = flb_net_accept(ctx->server_fd);
    if (fd == -1) {
        flb_error("[in_tcp] could not accept new connection");
        return -1;
    }

    flb_trace("[in_tcp] new TCP connection arrived FD=%i", fd);
    conn = tcp_conn_add(fd, ctx);
    if (!conn) {
        return -1;
    }
    return 0;
}

/* Initialize plugin */
static int in_tcp_init(struct flb_input_instance *in,
                      struct flb_config *config, void *data)
{
    int ret;
    struct flb_in_tcp_config *ctx;
    (void) data;

    /* Allocate space for the configuration */
    ctx = tcp_config_init(in);
    if (!ctx) {
        return -1;
    }
    ctx->in = in;
    mk_list_init(&ctx->connections);

    /* Set the context */
    flb_input_set_context(in, ctx);

    /* Create TCP server */
    ctx->server_fd = flb_net_server(ctx->tcp_port, ctx->listen);
    if (ctx->server_fd > 0) {
        flb_info("[in_tcp] binding %s:%s", ctx->listen, ctx->tcp_port);
    }
    else {
        flb_error("[in_tcp] could not bind address %s:%s. Aborting",
                  ctx->listen, ctx->tcp_port);
        return -1;
    }
    flb_net_socket_nonblocking(ctx->server_fd);

    ctx->evl = config->evl;
    ctx->buffer_id = 0;

    /* Initialize MessagePack buffers */
    msgpack_sbuffer_init(&ctx->mp_sbuf);
    msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write);

    /* Collect upon data available on the standard input */
    ret = flb_input_set_collector_socket(in,
                                        in_tcp_collect,
                                        ctx->server_fd,
                                        config);
    if (ret == -1) {
        flb_utils_error_c("Could not set collector for IN_TCP input plugin");
    }<commit_msg>in_tcp: release flb_in_tcp_config when binding fails<commit_after>/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2016 Treasure Data Inc.
 *
 *  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 <msgpack.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_network.h>

#include "tcp.h"
#include "tcp_conn.h"
#include "tcp_config.h"

static void *in_tcp_flush(void *in_context, size_t *size)
{
    char *buf;
    msgpack_sbuffer *sbuf;
    struct flb_in_tcp_config *ctx = in_context;

    if (ctx->buffer_id == 0|| sbuf->size <= 0) {
        return NULL;
    }

    sbuf = &ctx->mp_sbuf;
    *size = sbuf->size;
    buf = flb_malloc(sbuf->size);
    if (!buf) {
        return NULL;
    }

    /* set a new buffer and re-initialize our MessagePack context */
    memcpy(buf, sbuf->data, sbuf->size);
    msgpack_sbuffer_destroy(&ctx->mp_sbuf);
    msgpack_sbuffer_init(&ctx->mp_sbuf);
    msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write);

    ctx->buffer_id = 0;

    return buf;
}

/*
 * For a server event, the collection event means a new client have arrived, we
 * accept the connection and create a new TCP instance which will wait for
 * JSON map messages.
 */
static int in_tcp_collect(struct flb_config *config, void *in_context)
{
    int fd;
    struct flb_in_tcp_config *ctx = in_context;
    struct tcp_conn *conn;

    /* Accept the new connection */
    fd = flb_net_accept(ctx->server_fd);
    if (fd == -1) {
        flb_error("[in_tcp] could not accept new connection");
        return -1;
    }

    flb_trace("[in_tcp] new TCP connection arrived FD=%i", fd);
    conn = tcp_conn_add(fd, ctx);
    if (!conn) {
        return -1;
    }
    return 0;
}

/* Initialize plugin */
static int in_tcp_init(struct flb_input_instance *in,
                      struct flb_config *config, void *data)
{
    int ret;
    struct flb_in_tcp_config *ctx;
    (void) data;

    /* Allocate space for the configuration */
    ctx = tcp_config_init(in);
    if (!ctx) {
        return -1;
    }
    ctx->in = in;
    mk_list_init(&ctx->connections);

    /* Set the context */
    flb_input_set_context(in, ctx);

    /* Create TCP server */
    ctx->server_fd = flb_net_server(ctx->tcp_port, ctx->listen);
    if (ctx->server_fd > 0) {
        flb_info("[in_tcp] binding %s:%s", ctx->listen, ctx->tcp_port);
    }
    else {
        flb_error("[in_tcp] could not bind address %s:%s. Aborting",
                  ctx->listen, ctx->tcp_port);
        tcp_config_destroy(ctx);
        return -1;
    }
    flb_net_socket_nonblocking(ctx->server_fd);

    ctx->evl = config->evl;
    ctx->buffer_id = 0;

    /* Initialize MessagePack buffers */
    msgpack_sbuffer_init(&ctx->mp_sbuf);
    msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write);

    /* Collect upon data available on the standard input */
    ret = flb_input_set_collector_socket(in,
                                        in_tcp_collect,
                                        ctx->server_fd,
                                        config);
    if (ret == -1) {
        flb_utils_error_c("Could not set collector for IN_TCP input plugin");
    }
{'fraction_non_alphanumeric': 0.12036055143160128, 'fraction_numerical': 0.006892895015906681, 'mean_word_length': 2.6152371825586966, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1316243', 'n_tokens_mistral': 2536, 'n_tokens_neox': 2470, 'n_words': 686}