开发者

boost::asio with SSL - problems after SSL error

I use synchronous boost::asio SSL sockets in my application. I initialize all parameters and then connect to some hosts (one after another) and do 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 unspecified SSL error.

Do I have to reset the ssl::stream somehow? Is it possible to re-initialize the ssl::stream after each connection?

In the following code snippets I removed error handling and all non asio related things.

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)
开发者_JS百科{
}

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;
}


I got the answer from the asio mailing list (many thanks to Marsh Ray). Sam Miller was correct in that the asio::ssl::context has to be created each time. To achieve this, std::auto_ptr is used.

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));


you might try recreating the asio::ssl::context each time you create a asio::ssl::stream.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜