在使用Boost :: asio之前从套接字读取之后是否可以执行async_handshake?

问题描述 投票:0回答:1

我有一个boost::asio::ssl::stream<boost::asio::ip::tcp::socket>类型的插座。当boost首先接受到这个套接字的连接时,我想查看一些字节。但是,偷看不是你可以正确/安全地做的事情。所以我读了我需要的字节并将它们放在缓冲区中。

typedef socket_type boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
void OnAccept(std::shared_ptr<socket_type> socket)
{
    boost::asio::mutable_buffers_1 sslBuffer(m_Buffer.data(), m_Buffer.size());
    // I'm going to read 4 bytes from the socket.
    boost::system::error_code ec;
    std::size_t readBytes = boost::asio::read(socket->next_layer(), boost::asio::buffer(sslBuffer, 4), ec);
    if(ec) { Stop(); return; } // pseudo

    // Check the bytes I read in the buffer

    socket->async_handshake(boost::asio::ssl::stream_base::server, sslBuffer, &handler);
}

此时,将调用async_handshake的处理程序,但它会告诉我它从ssl获得了unexpected message错误代码。这是有道理的:它正在进行握手的消息可能缺少前4个字节!

我该怎么做才能为async_handshake提供适当的缓冲区,告知它已经有4个字节有效?

c++ ssl boost
1个回答
0
投票

在研究了async_handshake的缓冲区重载方法的实现之后,似乎缓冲区必须已经读入了握手。

我尝试了但仍遇到问题,我不断收到错误代码,说SSL版本不正确。我知道这不是问题因为没有使用async_handshake的缓冲重载工作正常!

然后解决方案也限制缓冲参数的大小。

typedef socket_type boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
void OnAccept(std::shared_ptr<socket_type> socket)
{
    const uint bytesToPeek = 4;
    boost::asio::mutable_buffers_1 sslBuffer(m_Buffer.data(), m_Buffer.size());
    // I'm going to read 4 bytes from the socket.
    boost::system::error_code ec;
    std::size_t readBytes = boost::asio::read(socket->next_layer(), boost::asio::buffer(sslBuffer, bytesToPeek), ec);
    if(ec) { Stop(); return; } // pseudo

    // Check the bytes I read in the buffer

    // Read in the rest of the handshake.
    std::size_t bytesOfHandshake = socket->next_layer().read_some(boost::asio::buffer(sslBuffer+bytesToPeek, 4000));
    bytesOfHandshake += bytesToPeek;

    // Finish the handshake.
    socket->async_handshake(boost::asio::ssl::stream_base::server, boost::asio::buffer(sslBuffer, bytesOfHandshake), &handler);
}

请注意,readread_some中的调用也应该是async。我只是想在没有处理程序的情况下展示答案。

© www.soinside.com 2019 - 2024. All rights reserved.