<commit_before>
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <glib.h>

#include "cal.h"
#include "bip.h"




static pthread_t *bip_publisher_thread = NULL;

static int bip_publisher_fds_to_user[2];
static int bip_publisher_fds_from_user[2];

static int bip_listening_socket = -1;

static GPtrArray *clients;




static void bip_publisher_disconnect_peer(cal_peer_t *peer) {
    int r;
    cal_event_t *event;

    close(peer->as.ipv4.socket);
    peer->as.ipv4.socket = -1;

    g_ptr_array_remove_fast(clients, peer);

    event = cal_event_new(CAL_EVENT_DISCONNECT);
    if (event == NULL) {
        fprintf(stderr, "bip_disconnect_peer(): out of memory\n");
        return;
    }

    event->peer = peer;

    r = write(bip_publisher_fds_to_user[1], &event, sizeof(cal_event_t*));
    if (r < 0) {
        printf("bip_publisher_disconnect_peer(): error writing Disconnect event: %s\n", strerror(errno));
    } else if (r != sizeof(cal_event_t*)) {
        printf("bip_publisher_disconnect_peer(): short write of Disconnect event!\n");
    }
}


static void bip_publisher_read_from_user(void) {
    printf("bip: user has something to say\n");
}


static void bip_publisher_accept_connection(void) {
    int r;
    int socket;
    struct sockaddr_in sin;
    socklen_t sin_len;

    cal_event_t *event;


    sin_len = sizeof(struct sockaddr);
    socket = accept(bip_listening_socket, (struct sockaddr *)&sin, &sin_len);
    if (socket < 0) {
        fprintf(stderr, "bip_publisher_accept_connection(): error accepting a connection: %s\n", strerror(errno));
        return;
    }

    event = cal_event_new(CAL_EVENT_CONNECT);
    if (event == NULL) {
        // an error has been logged already
        close(socket);
        return;
    }

    event->peer = cal_peer_new(NULL);
    if (event->peer == NULL) {
        // an error has been logged already
        cal_event_free(event);
        close(socket);
        return;
    }

    cal_peer_set_addressing_scheme(event->peer, CAL_AS_IPv4);
    event->peer->as.ipv4.socket = socket;
    event->peer->as.ipv4.port = ntohs(sin.sin_port);

    event->peer->as.ipv4.hostname = strdup(inet_ntoa(sin.sin_addr));
    if (event->peer->as.ipv4.hostname == NULL) {
        fprintf(stderr, "bip_publisher_accept_connection(): out of memory\n");
        cal_event_free(event);
        close(socket);
        return;
    }

    g_ptr_array_add(clients, event->peer);

    r = write(bip_publisher_fds_to_user[1], &event, sizeof(cal_event_t*));
    if (r < 0) {
        printf("bip_publisher_accept_connection(): error writing Connect event: %s\n", strerror(errno));
    } else if (r != sizeof(cal_event_t*)) {
        printf("bip_publisher_accept_connection(): short write of Connect event!\n");
    }
}


static int bip_publisher_read_from_client(cal_peer_t *peer) {
    int r;
    cal_event_t *event;


    event = cal_event_new(CAL_EVENT_MESSAGE);
    if (event == NULL) {
        fprintf(stderr, "bip_publisher_read_from_client(): out of memory!\n");
        goto fail0;
    }

    event->msg.buffer = malloc(1024);
    if (event->msg.buffer == NULL) {
        fprintf(stderr, "bip_publisher_read_from_client(): out of memory!\n");
        goto fail1;
    }

    r = read(peer->as.ipv4.socket, event->msg.buffer, sizeof(event->msg.buffer));
    if (r < 0) {
        fprintf(stderr, "bip_publisher_read_from_client(): error reading from client %s (%s): %s\n", peer->name, cal_peer_address_to_str(peer), strerror(errno));
        goto fail1;
    } else if (r == 0) {
        fprintf(stderr, "bip_publisher_read_from_client(): client %s (%s) disconnects\n", peer->name, cal_peer_address_to_str(peer));
        goto fail1;
    }

    event->msg.size = r;
    event->peer = peer;

    r = write(bip_publisher_fds_to_user[1], &event, sizeof(cal_event_t*));
    if (r < 0) {
        printf("bip_publisher_read_from_client(): error writing Message event: %s\n", strerror(errno));
        goto fail1;
    } else if (r != sizeof(cal_event_t*)) {
        printf("bip_publisher_read_from_client(): short write of Message event!\n");
        goto fail1;
    }

    return 0;
<commit_msg>fix a silly buffer-size bug<commit_after>
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <glib.h>

#include "cal.h"
#include "bip.h"




static pthread_t *bip_publisher_thread = NULL;

static int bip_publisher_fds_to_user[2];
static int bip_publisher_fds_from_user[2];

static int bip_listening_socket = -1;

static GPtrArray *clients;




static void bip_publisher_disconnect_peer(cal_peer_t *peer) {
    int r;
    cal_event_t *event;

    close(peer->as.ipv4.socket);
    peer->as.ipv4.socket = -1;

    g_ptr_array_remove_fast(clients, peer);

    event = cal_event_new(CAL_EVENT_DISCONNECT);
    if (event == NULL) {
        fprintf(stderr, "bip_disconnect_peer(): out of memory\n");
        return;
    }

    event->peer = peer;

    r = write(bip_publisher_fds_to_user[1], &event, sizeof(cal_event_t*));
    if (r < 0) {
        printf("bip_publisher_disconnect_peer(): error writing Disconnect event: %s\n", strerror(errno));
    } else if (r != sizeof(cal_event_t*)) {
        printf("bip_publisher_disconnect_peer(): short write of Disconnect event!\n");
    }
}


static void bip_publisher_read_from_user(void) {
    printf("bip: user has something to say\n");
}


static void bip_publisher_accept_connection(void) {
    int r;
    int socket;
    struct sockaddr_in sin;
    socklen_t sin_len;

    cal_event_t *event;


    sin_len = sizeof(struct sockaddr);
    socket = accept(bip_listening_socket, (struct sockaddr *)&sin, &sin_len);
    if (socket < 0) {
        fprintf(stderr, "bip_publisher_accept_connection(): error accepting a connection: %s\n", strerror(errno));
        return;
    }

    event = cal_event_new(CAL_EVENT_CONNECT);
    if (event == NULL) {
        // an error has been logged already
        close(socket);
        return;
    }

    event->peer = cal_peer_new(NULL);
    if (event->peer == NULL) {
        // an error has been logged already
        cal_event_free(event);
        close(socket);
        return;
    }

    cal_peer_set_addressing_scheme(event->peer, CAL_AS_IPv4);
    event->peer->as.ipv4.socket = socket;
    event->peer->as.ipv4.port = ntohs(sin.sin_port);

    event->peer->as.ipv4.hostname = strdup(inet_ntoa(sin.sin_addr));
    if (event->peer->as.ipv4.hostname == NULL) {
        fprintf(stderr, "bip_publisher_accept_connection(): out of memory\n");
        cal_event_free(event);
        close(socket);
        return;
    }

    g_ptr_array_add(clients, event->peer);

    r = write(bip_publisher_fds_to_user[1], &event, sizeof(cal_event_t*));
    if (r < 0) {
        printf("bip_publisher_accept_connection(): error writing Connect event: %s\n", strerror(errno));
    } else if (r != sizeof(cal_event_t*)) {
        printf("bip_publisher_accept_connection(): short write of Connect event!\n");
    }
}


#define BIP_MSG_BUFFER_SIZE (1024)

static int bip_publisher_read_from_client(cal_peer_t *peer) {
    int r;
    cal_event_t *event;


    event = cal_event_new(CAL_EVENT_MESSAGE);
    if (event == NULL) {
        fprintf(stderr, "bip_publisher_read_from_client(): out of memory!\n");
        goto fail0;
    }

    event->msg.buffer = malloc(BIP_MSG_BUFFER_SIZE);
    if (event->msg.buffer == NULL) {
        fprintf(stderr, "bip_publisher_read_from_client(): out of memory!\n");
        goto fail1;
    }

    r = read(peer->as.ipv4.socket, event->msg.buffer, BIP_MSG_BUFFER_SIZE);
    if (r < 0) {
        fprintf(stderr, "bip_publisher_read_from_client(): error reading from client %s (%s): %s\n", peer->name, cal_peer_address_to_str(peer), strerror(errno));
        goto fail1;
    } else if (r == 0) {
        fprintf(stderr, "bip_publisher_read_from_client(): client %s (%s) disconnects\n", peer->name, cal_peer_address_to_str(peer));
        goto fail1;
    }

    event->msg.size = r;
    event->peer = peer;

    r = write(bip_publisher_fds_to_user[1], &event, sizeof(cal_event_t*));
    if (r < 0) {
        printf("bip_publisher_read_from_client(): error writing Message event: %s\n", strerror(errno));
        goto fail1;
    } else if (r != sizeof(cal_event_t*)) {
        printf("bip_publisher_read_from_client(): short write of Message event!\n");
        goto fail1;
    }

    return 0;

{'fraction_non_alphanumeric': 0.13562261533125217, 'fraction_numerical': 0.007399699387212394, 'mean_word_length': 3.338014042126379, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7186501', 'n_tokens_mistral': 3162, 'n_tokens_neox': 3095, 'n_words': 629}