Boost :: asio with SSL - problems after SSL error

I am using synchronous boost :: asio SSL sockets in my application. I initialize all the parameters and then connect to some hosts (one by one) and make a GET request for each host.

Everything works until I get a "404 - not found" error for one of the hosts. After this error, all new connections fail with some undefined SSL error.

Do I need to reset ssl :: stream? Is it possible to reinitialize the ssl :: stream after each connection?

In the following code snippets, I removed the error handling and all related events.

Main:

asio::io_service ioservice;
asio::ssl::context ctx(ioservice, asio::ssl::context::sslv23);
ctx.set_verify_mode(asio::ssl::context::verify_none);

Connector *con = new Connector(ioservice, ctx);

while (!iplist.empty())
{
    ...
    con->ssl_connect(ipaddress, port);
    ...
}

Connector:

Connector::Connector(asio::io_service& io_service, asio::ssl::context &ctx) 
    : sslSock(io_service, ctx)
{
}

Connector::ssl_connect(std::string ipAdr, std::string port)
{
    ...
    tcp::resolver resolver(ioserv);
    tcp::resolver::query query(ipAdr, port);
    endpoint_iterator = resolver.resolve(query);
    ...

    asio::error_code errorcode = asio::error::host_not_found;
    tcp::resolver::iterator end;

    // Establish connection
    while (errorcode && endpoint_iterator != end)
    {
        sslSock.lowest_layer().close();
        sslSock.lowest_layer().connect(*endpoint_iterator++, errorcode);
    }
    sslSock.handshake(asio::ssl::stream_base::client, errorcode);
    ...
    asio::write(...);
    ...
    asio::read(...);
    ...
    sslSock.lowest_layer().close();
    ...
    return;
}
+3
source share
3 answers

asio ( Marsh Ray). , asio:: ssl:: context. std::auto_ptr.

Connector.h:

std::auto_ptr<asio::ssl::stream<tcp::socket>> sslSock;

Connector.cpp:

asio::ssl::context ctx(ioserv, asio::ssl::context::sslv23);
ctx.set_verify_mode(asio::ssl::context::verify_none);
sslSock.reset(new asio::ssl::stream<tcp::socket>(ioserv, ctx));
+4

asio::ssl::context asio::ssl::stream.

+3

I saw the same exception because I ran curl_global_cleanup();before I ended up with curl in the application.

0
source

Source: https://habr.com/ru/post/1773536/


All Articles