<commit_before>/*
 * An example server for the logging protocol.  It prints the messages
 * to stdout.
 *
 * author: <NAME> <<EMAIL>>
 */

#include "log-server.h"
#include "fd_util.h"

#include <socket/resolver.h>

#include <glib.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>

static int
open_udp(const char *host, int default_port)
{
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_socktype = SOCK_DGRAM;

    struct addrinfo *ai;
    int ret = socket_resolve_host_port(host, default_port, &hints, &ai);
    if (ret != 0) {
        fprintf(stderr, "Failed to resolve '%s': %s\n",
                host, gai_strerror(ret));
        return -1;
    }

    int fd = socket_cloexec_nonblock(ai->ai_family, ai->ai_socktype,
                                     ai->ai_protocol);
    if (fd < 0) {
        fprintf(stderr, "socket() failed: %s\n", strerror(errno));
        freeaddrinfo(ai);
        return -1;
    }

    ret = connect(fd, ai->ai_addr, ai->ai_addrlen);
    if (ret < 0) {
        fprintf(stderr, "connect() failed: %s\n", strerror(errno));
        close(fd);
        freeaddrinfo(ai);
        return -1;
    }

    freeaddrinfo(ai);
    return fd;
}

struct destination {
    int fd;
    bool failed;
};

int main(int argc, char **argv)
{
    if (argc < 2) {
        fprintf(stderr, "Usage: log-forward HOST ...\n");
        return EXIT_FAILURE;
    }

    static struct destination destinations[256];
    unsigned num_destinations = argc - 1;

    if (num_destinations > G_N_ELEMENTS(destinations)) {
        fprintf(stderr, "Too many hosts\n");
        return EXIT_FAILURE;
    }

    for (unsigned i = 0; i < num_destinations; ++i) {
        destinations[i].fd = open_udp(argv[1 + i], 5479);
        if (destinations[i].fd < 0)
            return EXIT_FAILURE;
    }

    static char buffer[16384];
    ssize_t nbytes;<commit_msg>log-forward: try all addresses for a host name if there are failures<commit_after>/*
 * An example server for the logging protocol.  It prints the messages
 * to stdout.
 *
 * author: <NAME> <<EMAIL>>
 */

#include "log-server.h"
#include "fd_util.h"

#include <socket/resolver.h>

#include <glib.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>

static int
open_udp(const char *host, int default_port)
{
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_socktype = SOCK_DGRAM;

    struct addrinfo *ai;
    int ret = socket_resolve_host_port(host, default_port, &hints, &ai);
    if (ret != 0) {
        fprintf(stderr, "Failed to resolve '%s': %s\n",
                host, gai_strerror(ret));
        return -1;
    }

    for (const struct addrinfo *i = ai; i != NULL; i = i->ai_next) {
        int fd = socket_cloexec_nonblock(i->ai_family, i->ai_socktype,
                                         i->ai_protocol);
        if (fd < 0) {
            fprintf(stderr, "socket() failed: %s\n", strerror(errno));
            continue;
        }

        ret = connect(fd, i->ai_addr, i->ai_addrlen);
        if (ret < 0) {
            fprintf(stderr, "connect() failed: %s\n", strerror(errno));
            close(fd);
            continue;
        }

        freeaddrinfo(ai);
        return fd;
    }

    freeaddrinfo(ai);
    return -1;
}

struct destination {
    int fd;
    bool failed;
};

int main(int argc, char **argv)
{
    if (argc < 2) {
        fprintf(stderr, "Usage: log-forward HOST ...\n");
        return EXIT_FAILURE;
    }

    static struct destination destinations[256];
    unsigned num_destinations = argc - 1;

    if (num_destinations > G_N_ELEMENTS(destinations)) {
        fprintf(stderr, "Too many hosts\n");
        return EXIT_FAILURE;
    }

    for (unsigned i = 0; i < num_destinations; ++i) {
        destinations[i].fd = open_udp(argv[1 + i], 5479);
        if (destinations[i].fd < 0)
            return EXIT_FAILURE;
    }

    static char buffer[16384];
    ssize_t nbytes;
{'fraction_non_alphanumeric': 0.13878743608473337, 'fraction_numerical': 0.011443876308741174, 'mean_word_length': 2.4119601328903655, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '64836', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1416, 'n_words': 359}