<commit_before>#include "stdafx.h"
#include "Socket.h"
#include <iostream>

namespace
{
	timeval gZeroTimeout = { 0, 0 };
}



Socket::Socket(SOCKET s, bool enabledStats) : m_socket(s), m_isAccepting(false), m_enabledStats(enabledStats), m_dataBytesSent(), m_sslBytesSent(), m_dataBytesReceived(), m_sslBytesReceived()
{
}
Socket::~Socket()
{
	Finalize();
}

#pragma pack(push)
#pragma pack(1)

struct socks4_request_header_t
{
    uint8_t vn;
    uint8_t cd;
    uint16_t dstport;
    uint32_t dstip;
};

struct socks4_response_header_t
{
    uint8_t vn;
    uint8_t cd;
    uint16_t dstport;
    uint32_t dstip;
};

struct socks5_method_request_header_t
{
    uint8_t ver;
    uint8_t methods;
    uint8_t method;
};

struct socks5_method_response_header_t
{
    uint8_t ver;
    uint8_t method;
};

struct socks5_logon_request_header_t
{
    uint8_t ver;
};

struct socks5_logon_response_header_t
{
    uint8_t ver;
    uint8_t status;
};

struct socks5_connect_request_header_t
{
    uint8_t ver;
    uint8_t cmd;
    uint8_t rsv;
    uint8_t atyp;
    uint32_t addr;
    uint16_t port;
};

struct socks5_connect_response_header_t
{
    uint8_t ver;
    uint8_t rep;
    uint8_t rsv;
    uint8_t atyp;
    uint32_t addr;
    uint16_t port;
};

#pragma pack(pop)

int Socket::Connect(ConnectType type, const sockaddr* address, int addressLen, const sockaddr* proxyAddress, int proxyAddressLen, const char* userName, const char* password)
{
    switch (type)
    {
    case ConnectType_Direct:
        return connectDirect(address, addressLen);

    case ConnectType_ProxySocks4:
        return connectProxySocks4(address, addressLen, proxyAddress, proxyAddressLen, userName);

    case ConnectType_ProxySocks5:
        return connectProxySocks5(address, addressLen, proxyAddress, proxyAddressLen, userName, password);

    default:
        return -1;
    }
}

int Socket::connectDirect(const sockaddr* address, int addressLen)
{
    return ::connect(m_socket, address, addressLen);
}

int Socket::connectProxySocks4(const sockaddr* address, int addressLen, const sockaddr* proxyAddress, int proxyAddressLen, const char* userName) 
{
    int result = ::connect(m_socket, proxyAddress, proxyAddressLen);

    if (result < 0)
        return result;

    size_t user_name_size = strlen(userName) + 1;
    size_t socks_request_size = sizeof(socks4_request_header_t) + user_name_size;

    socks4_request_header_t* socks_request_header = (socks4_request_header_t*)malloc(socks_request_size);<commit_msg>. . .<commit_after>#include "stdafx.h"
#include "Socket.h"
#include <iostream>

namespace
{
	timeval gZeroTimeout = { 0, 0 };
}



Socket::Socket(SOCKET s, bool enabledStats) : m_socket(s), m_isAccepting(false), m_enabledStats(enabledStats), m_dataBytesSent(), m_sslBytesSent(), m_dataBytesReceived(), m_sslBytesReceived()
{
}
Socket::~Socket()
{
	Finalize();
}

#pragma pack(push)
#pragma pack(1)

struct socks4_request_header_t
{
    uint8_t vn;
    uint8_t cd;
    uint16_t dstport;
    uint32_t dstip;
};

struct socks4_response_header_t
{
    uint8_t vn;
    uint8_t cd;
    uint16_t dstport;
    uint32_t dstip;
};

struct socks5_method_request_header_t
{
    uint8_t ver;
    uint8_t methods;
    uint8_t method;
};

struct socks5_method_response_header_t
{
    uint8_t ver;
    uint8_t method;
};

struct socks5_logon_request_header_t
{
    uint8_t ver;
};

struct socks5_logon_response_header_t
{
    uint8_t ver;
    uint8_t status;
};

struct socks5_connect_request_header_t
{
    uint8_t ver;
    uint8_t cmd;
    uint8_t rsv;
    uint8_t atyp;
    uint32_t addr;
    uint16_t port;
};

struct socks5_connect_response_header_t
{
    uint8_t ver;
    uint8_t rep;
    uint8_t rsv;
    uint8_t atyp;
    uint32_t addr;
    uint16_t port;
};

#pragma pack(pop)

int Socket::Connect(ConnectType type, const sockaddr* address, int addressLen, const sockaddr* proxyAddress, int proxyAddressLen, const char* userName, const char* password)
{
    switch (type)
    {
    case ConnectType_Direct:
        return connectDirect(address, addressLen);

    case ConnectType_ProxySocks4:
        return connectProxySocks4(address, addressLen, proxyAddress, proxyAddressLen, userName);

    case ConnectType_ProxySocks5:
        return connectProxySocks5(address, addressLen, proxyAddress, proxyAddressLen, userName, password);

    default:
        return -1;
    }
}


int Socket::connectDirect(const sockaddr* address, int addressLen)
{
    return ::connect(m_socket, address, addressLen);
}

int Socket::connectProxySocks4(const sockaddr* address, int addressLen, const sockaddr* proxyAddress, int proxyAddressLen, const char* userName) 
{
    int result = ::connect(m_socket, proxyAddress, proxyAddressLen);

    if (result < 0)
        return result;

    size_t user_name_size = strlen(userName) + 1;
    size_t socks_request_size = sizeof(socks4_request_header_t) + user_name_size;

    socks4_request_header_t* socks_request_header = (socks4_request_header_t*)malloc(socks_request_size);
{'fraction_non_alphanumeric': 0.08952533546965752, 'fraction_numerical': 0.02323252553575005, 'mean_word_length': 4.329775880469584, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '1410884', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1767, 'n_words': 386}