<commit_before>
#include "libprotossl.h"
#include "bufprintf.h"
#include <sstream>
#include <mbedtls/oid.h>

using namespace ProtoSSL;

ProtoSSLConnClient :: ProtoSSLConnClient(ProtoSSLMsgs * _msgs,
                                         mbedtls_net_context new_netctx,
                                         bool _use_tcp,
                                         unsigned char *client_ip,
                                         size_t cliip_len)
    : msgs(_msgs), use_tcp(_use_tcp)
{
    _ok = false;
    send_close_notify = false;
    ssl_initialized = false;
    netctx = new_netctx;
    net_initialized = true;
    WaitUtil::Lock lock(&ssl_lock);
    _ok = init_common(client_ip, cliip_len);
    msgs->registerClient(this);
}

ProtoSSLConnClient :: ProtoSSLConnClient(ProtoSSLMsgs * _msgs,
                                         const std::string &remoteHost,
                                         int remotePort,
                                         bool _use_tcp)
    : msgs(_msgs), use_tcp(_use_tcp)
{
    _ok = false;
    send_close_notify = false;
    ssl_initialized = false;
    net_initialized = false;

    WaitUtil::Lock lock(&ssl_lock);
    Bufprintf<20> portString;
    portString.print("%d", remotePort);
    mbedtls_net_init(&netctx);
    int ret = mbedtls_net_connect(&netctx, remoteHost.c_str(),
                                  portString.getBuf(),
                                  use_tcp ?
                                  MBEDTLS_NET_PROTO_TCP :
                                  MBEDTLS_NET_PROTO_UDP );
    if (ret != 0)
    {
        char strbuf[200];
        mbedtls_strerror( ret, strbuf, sizeof(strbuf));
        fprintf(stderr, "net bind returned 0x%x: %s\n", -ret, strbuf);
        return;
    }
    msgs->registerClient(this);

    // net_connect has a bug -- if connection fails, it closes
    // netctx.fd but does not set it to -1. then if you net_free,
    // you end up double-closing the fd (leading to a possible race
    // with other threads reusing that fd#).
    net_initialized = true;
    _ok = init_common(NULL, 0);
}

// this function requires ssl_lock to be locked.
bool
ProtoSSLConnClient :: init_common(unsigned char *client_ip, size_t cliip_len)
{
    int ret;

    mbedtls_ssl_init( &sslctx );
    ssl_initialized = true;
    mbedtls_ssl_setup( &sslctx, &msgs->sslcfg );
    mbedtls_ssl_set_hs_authmode( &sslctx, MBEDTLS_SSL_VERIFY_REQUIRED );
    mbedtls_ssl_set_bio( &sslctx, &netctx,
                 &mbedtls_net_send, &mbedtls_net_recv,
                 &mbedtls_net_recv_timeout);

    if (use_tcp == false && client_ip != NULL)
    {
        int ret;
        /* For HelloVerifyRequest cookies */<commit_msg>ssl client register with msgs timing change to avoid dll3 error<commit_after>
#include "libprotossl.h"
#include "bufprintf.h"
#include <sstream>
#include <mbedtls/oid.h>

using namespace ProtoSSL;

ProtoSSLConnClient :: ProtoSSLConnClient(ProtoSSLMsgs * _msgs,
                                         mbedtls_net_context new_netctx,
                                         bool _use_tcp,
                                         unsigned char *client_ip,
                                         size_t cliip_len)
    : msgs(_msgs), use_tcp(_use_tcp)
{
    _ok = false;
    send_close_notify = false;
    ssl_initialized = false;
    netctx = new_netctx;
    net_initialized = true;
    WaitUtil::Lock lock(&ssl_lock);
    _ok = init_common(client_ip, cliip_len);
    msgs->registerClient(this);
}

ProtoSSLConnClient :: ProtoSSLConnClient(ProtoSSLMsgs * _msgs,
                                         const std::string &remoteHost,
                                         int remotePort,
                                         bool _use_tcp)
    : msgs(_msgs), use_tcp(_use_tcp)
{
    _ok = false;
    send_close_notify = false;
    ssl_initialized = false;
    net_initialized = false;

    WaitUtil::Lock lock(&ssl_lock);
    msgs->registerClient(this);
    Bufprintf<20> portString;
    portString.print("%d", remotePort);
    mbedtls_net_init(&netctx);
    int ret = mbedtls_net_connect(&netctx, remoteHost.c_str(),
                                  portString.getBuf(),
                                  use_tcp ?
                                  MBEDTLS_NET_PROTO_TCP :
                                  MBEDTLS_NET_PROTO_UDP );
    if (ret != 0)
    {
        char strbuf[200];
        mbedtls_strerror( ret, strbuf, sizeof(strbuf));
        fprintf(stderr, "net bind returned 0x%x: %s\n", -ret, strbuf);
        return;
    }

    // net_connect has a bug -- if connection fails, it closes
    // netctx.fd but does not set it to -1. then if you net_free,
    // you end up double-closing the fd (leading to a possible race
    // with other threads reusing that fd#).
    net_initialized = true;
    _ok = init_common(NULL, 0);
}

// this function requires ssl_lock to be locked.
bool
ProtoSSLConnClient :: init_common(unsigned char *client_ip, size_t cliip_len)
{
    int ret;

    mbedtls_ssl_init( &sslctx );
    ssl_initialized = true;
    mbedtls_ssl_setup( &sslctx, &msgs->sslcfg );
    mbedtls_ssl_set_hs_authmode( &sslctx, MBEDTLS_SSL_VERIFY_REQUIRED );
    mbedtls_ssl_set_bio( &sslctx, &netctx,
                 &mbedtls_net_send, &mbedtls_net_recv,
                 &mbedtls_net_recv_timeout);

    if (use_tcp == false && client_ip != NULL)
    {
        int ret;
        /* For HelloVerifyRequest cookies */
{'fraction_non_alphanumeric': 0.09070796460176991, 'fraction_numerical': 0.0035029498525073746, 'mean_word_length': 1.930848190167477, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '343952', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1649, 'n_words': 372}