<commit_before>#ifndef _WIN32

#include "Socket.h"
#include "System/Log.h"
#include "System/Common.h"
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

unsigned long iftonl(const char* interface);

Socket::Socket()
{
    fd = -1;
    listening = false;
}

bool Socket::Create()
{
    if (fd >= 0)
    {
        Log_Trace("Called Create() on existing socket");
        return false;
    }
    
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0)
    {
        Log_Errno();
        return false;
    }
        
    listening = false;
    
//    SetReceiveBufferSize(64*KiB);
    
    return true;
}

bool Socket::SetNonblocking()
{
    int ret;
    
    if (fd < 0)
    {
        Log_Trace("SetNonblocking on invalid file descriptor");
        return false;
    }
    
    ret = fcntl(fd, F_GETFL, 0);
    if (ret < 0)
    {
        Log_Errno();
        return false;
    }

    ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
    if (ret < 0)
    {
        Log_Errno();
        return false;
    }
    
    return true;
}

bool Socket::SetNodelay()
{
    int     ret;
    int     sockopt;

    if (fd < 0)
    {
        Log_Trace("SetNodelay on invalid file descriptor");
        return false;
    }
    
    sockopt = 1;
    ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &sockopt, sizeof(sockopt));
    if (ret < 0)
    {
        Log_Errno();
        return false;
    }
    
    return true;
}

bool Socket::SetReceiveBufferSize(unsigned size)
{
    int     ret;

    if (fd < 0)
    {
        Log_Trace("SetRecieveBufferSize on invalid file descriptor");
        return false;
    }
    
    ret = setsockopt(fd, IPPROTO_TCP, SO_RCVBUF, &size, sizeof(size));
    if (ret < 0)
    {
        Log_Errno();
        return false;
    }
    
    return true;
}

bool Socket::Bind(int port)
{
    int                 ret;
    struct sockaddr_in  sa;
    int                 sockopt;

    sockopt = 1;
    ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt));
    if (ret < 0)
    {
        Log_Errno();
        Close();
        return false;
    }
    
    memset(&sa, 0, sizeof(sa));
    sa.sin_port = htons((uint16_t)port);
    sa.sin_addr.s_addr = htonl(INADDR_ANY);
    
    ret = bind(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in));
    if (ret < 0)<commit_msg>Fixed unspecified socket family (AF_UNSPEC) bug in Posix socket implementation.<commit_after>#ifndef _WIN32

#include "Socket.h"
#include "System/Log.h"
#include "System/Common.h"
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

unsigned long iftonl(const char* interface);

Socket::Socket()
{
    fd = -1;
    listening = false;
}

bool Socket::Create()
{
    if (fd >= 0)
    {
        Log_Trace("Called Create() on existing socket");
        return false;
    }
    
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0)
    {
        Log_Errno();
        return false;
    }
        
    listening = false;
    
//    SetReceiveBufferSize(64*KiB);
    
    return true;
}

bool Socket::SetNonblocking()
{
    int ret;
    
    if (fd < 0)
    {
        Log_Trace("SetNonblocking on invalid file descriptor");
        return false;
    }
    
    ret = fcntl(fd, F_GETFL, 0);
    if (ret < 0)
    {
        Log_Errno();
        return false;
    }

    ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
    if (ret < 0)
    {
        Log_Errno();
        return false;
    }
    
    return true;
}

bool Socket::SetNodelay()
{
    int     ret;
    int     sockopt;

    if (fd < 0)
    {
        Log_Trace("SetNodelay on invalid file descriptor");
        return false;
    }
    
    sockopt = 1;
    ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &sockopt, sizeof(sockopt));
    if (ret < 0)
    {
        Log_Errno();
        return false;
    }
    
    return true;
}

bool Socket::SetReceiveBufferSize(unsigned size)
{
    int     ret;

    if (fd < 0)
    {
        Log_Trace("SetRecieveBufferSize on invalid file descriptor");
        return false;
    }
    
    ret = setsockopt(fd, IPPROTO_TCP, SO_RCVBUF, &size, sizeof(size));
    if (ret < 0)
    {
        Log_Errno();
        return false;
    }
    
    return true;
}

bool Socket::Bind(int port)
{
    int                 ret;
    struct sockaddr_in  sa;
    int                 sockopt;

    sockopt = 1;
    ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt));
    if (ret < 0)
    {
        Log_Errno();
        Close();
        return false;
    }
    
    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons((uint16_t)port);
    sa.sin_addr.s_addr = htonl(INADDR_ANY);
    
    ret = bind(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in));
    if (ret < 0)
{'fraction_non_alphanumeric': 0.12572340850129715, 'fraction_numerical': 0.009578926362003592, 'mean_word_length': 2.057962172056132, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '3843060', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1807, 'n_words': 420}