<commit_before>#ifndef _SOCKET_HELPER_H
#define _SOCKET_HELPER_H

#include <factory.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include "io.c"

#ifndef CHANNEL
#define CHANNEL REUSE_SOCKET
#endif

#ifndef HOST
#define HOST -1
#endif

#ifndef PORT
#define PORT 0
#endif

struct channel
{
    int rx;
    int tx;
};

enum channel_mode
{
    REUSE_SOCKET,
    CONNECT_BACK,
    TCP_LISTEN,
    USE_STDOUT,
    USE_STDERR,
};

static const unsigned long host_addr = HOST;
static const unsigned short host_port = PORT;

static inline
int _socket(int domain, int type, int protocol)
{
    return INTERNAL_SYSCALL(socket,, 3, domain, type, protocol);
}

static inline
int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
    return INTERNAL_SYSCALL(connect,, 3, sockfd, addr, addrlen);
}

static inline
int _listen(int socket, int backlog)
{
    return INTERNAL_SYSCALL(listen,, 2, socket, backlog);
}

static inline
int _bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
    return INTERNAL_SYSCALL(bind,, 3, sockfd, addr, addrlen);
}

static inline
int _accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
    return INTERNAL_SYSCALL(accept,, 3, sockfd, addr, addrlen);
}

static inline
int find_open_socket()
{
    for ( int fd = 0; fd != 0xFFFF; fd++ )
    {
        struct stat sb;

        INTERNAL_SYSCALL(fstat,, 2, fd, &sb);
        if ( S_ISSOCK(sb.st_mode) )
            return fd;
    }

    return -1;
}

static inline
int tcp_connect(const long addr, const short port)
{
    _Static_assert(CHANNEL != CONNECT_BACK || host_addr != -1, "Must specify an address to connect to.\n");
    _Static_assert(CHANNEL != CONNECT_BACK || host_port != 0, "Must specify a port to connect to.\n");

    struct sockaddr_in serv_addr;
    int sock = _socket(AF_INET, SOCK_STREAM, 0);

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = host_port;
    serv_addr.sin_addr.s_addr = host_addr;

    if ( _connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0 )
        return -1;

    return sock;
}

static inline
int tcp_listen(const long addr, const short port)
{
    _Static_assert(CHANNEL != TCP_LISTEN || host_addr != -1, "Must specify an address to listen to.\n");
    _Static_assert(CHANNEL != TCP_LISTEN || host_port != 0, "Must specify a port to listen to.\n");

    struct sockaddr_in serv_addr, client_addr;
    socklen_t client_len = sizeof(client_addr);
    int client_sock, listen_sock = _socket(AF_INET, SOCK_STREAM, 0);

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = host_port;
    serv_addr.sin_addr.s_addr = host_addr;    

    if ( _bind(listen_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) )
        return -1;

    _listen(listen_sock, 0);

    client_sock = _accept(listen_sock, (struct sockaddr *) &client_addr, &client_len);
    _close(listen_sock);

    return client_sock;
}<commit_msg>socket.c: avoid unnecessary global variables<commit_after>#ifndef _SOCKET_HELPER_H
#define _SOCKET_HELPER_H

#include <factory.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include "io.c"

#ifndef CHANNEL
#define CHANNEL REUSE_SOCKET
#endif

#ifndef HOST
#define HOST -1
#endif

#ifndef PORT
#define PORT 0
#endif

struct channel
{
    int rx;
    int tx;
};

enum channel_mode
{
    REUSE_SOCKET,
    CONNECT_BACK,
    TCP_LISTEN,
    USE_STDOUT,
    USE_STDERR,
};

static inline
int _socket(int domain, int type, int protocol)
{
    return INTERNAL_SYSCALL(socket,, 3, domain, type, protocol);
}

static inline
int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
    return INTERNAL_SYSCALL(connect,, 3, sockfd, addr, addrlen);
}

static inline
int _listen(int socket, int backlog)
{
    return INTERNAL_SYSCALL(listen,, 2, socket, backlog);
}

static inline
int _bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
    return INTERNAL_SYSCALL(bind,, 3, sockfd, addr, addrlen);
}

static inline
int _accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
    return INTERNAL_SYSCALL(accept,, 3, sockfd, addr, addrlen);
}

static inline
int find_open_socket()
{
    for ( int fd = 0; fd != 0xFFFF; fd++ )
    {
        struct stat sb;

        INTERNAL_SYSCALL(fstat,, 2, fd, &sb);
        if ( S_ISSOCK(sb.st_mode) )
            return fd;
    }

    return -1;
}

static inline
int tcp_connect(const long addr, const short port)
{
    const unsigned long host_addr = HOST;
    const unsigned short host_port = PORT;

    _Static_assert(CHANNEL != CONNECT_BACK || host_addr != -1, "Must specify an address to connect to.\n");
    _Static_assert(CHANNEL != CONNECT_BACK || host_port != 0, "Must specify a port to connect to.\n");

    struct sockaddr_in serv_addr;
    int sock = _socket(AF_INET, SOCK_STREAM, 0);

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = host_port;
    serv_addr.sin_addr.s_addr = host_addr;

    if ( _connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0 )
        return -1;

    return sock;
}

static inline
int tcp_listen(const long addr, const short port)
{
    const unsigned long host_addr = HOST;
    const unsigned short host_port = PORT;

    _Static_assert(CHANNEL != TCP_LISTEN || host_addr != -1, "Must specify an address to listen to.\n");
    _Static_assert(CHANNEL != TCP_LISTEN || host_port != 0, "Must specify a port to listen to.\n");

    struct sockaddr_in serv_addr, client_addr;
    socklen_t client_len = sizeof(client_addr);
    int client_sock, listen_sock = _socket(AF_INET, SOCK_STREAM, 0);

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = host_port;
    serv_addr.sin_addr.s_addr = host_addr;    

    if ( _bind(listen_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) )
        return -1;

    _listen(listen_sock, 0);

    client_sock = _accept(listen_sock, (struct sockaddr *) &client_addr, &client_len);
    _close(listen_sock);

    return client_sock;
}
{'fraction_non_alphanumeric': 0.10887030660764059, 'fraction_numerical': 0.006886374815543532, 'mean_word_length': 3.7918303220738414, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, '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': '93282', 'n_tokens_mistral': 2234, 'n_tokens_neox': 2142, 'n_words': 590}