简体   繁体   English

使用SSL的boost :: asio - SSL错误后的问题

[英]boost::asio with SSL - problems after SSL error

I use synchronous boost::asio SSL sockets in my application. 我在我的应用程序中使用同步boost :: asio SSL套接字。 I initialize all parameters and then connect to some hosts (one after another) and do a GET request for each host. 我初始化所有参数然后连接到一些主机(一个接一个)并为每个主机执行GET请求。

Everything works until I get a "404 - Not Found" error for one of the hosts. 一切正常,直到我得到一个主机的“404 - Not Found”错误。 After this error, all new connections fail with some unspecified SSL error. 发生此错误后,所有新连接都会因某些未指定的SSL错误而失败。

Do I have to reset the ssl::stream somehow? 我是否必须以某种方式重置ssl :: stream? Is it possible to re-initialize the ssl::stream after each connection? 是否可以在每次连接后重新初始化ssl :: stream?

In the following code snippets I removed error handling and all non asio related things. 在下面的代码片段中,我删除了错误处理和所有非asio相关的事情。

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

I got the answer from the asio mailing list (many thanks to Marsh Ray). 我从asio邮件列表中得到了答案(非常感谢Marsh Ray)。 Sam Miller was correct in that the asio::ssl::context has to be created each time. Sam Miller是正确的,因为每次都必须创建asio :: ssl :: context。 To achieve this, std::auto_ptr is used. 为此,使用std::auto_ptr

Connector.h: Connector.h:

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

Connector.cpp: 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));

您可以尝试在每次创建asio::ssl::stream时重新创建asio::ssl::context

I saw the same exception because I ran curl_global_cleanup(); 我看到了同样的异常,因为我运行了curl_global_cleanup(); before I was done with curl in the application. 在我完成应用程序中的curl之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM