Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need to call ssl::stream::shutdown when closing boost asio ssl socket?

My code is as follows:

declaration: boost::asio::ssl::stream<boost::asio::ip::tcp::socket> m_remote_socket;

m_remote_socket.shutdown(ec);
if (ec)
{      
    cdbug<<"id: "<<m_id<<", error when ssl shutdown: "    <<boost::system::system_category().message(ec.value()).c_str(); 
}
m_remote_socket.lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
if (ec)
{
    cdbug<<"id: "<<m_id<<", error when tcp shutdown: "<<boost::system::system_category().message(ec.value()).c_str(); 
}

Each time I call m_remote_socket.shutdown, it will get an error. Such kind of unknown error with a really big error value.

But it is ok to call m_remote_socket.lowest_layer().shutdown() directly without calling m_remote_socket.shutdown.

Could anybody tell me how to close a ssl streaming socket?

like image 806
Dafan Avatar asked Mar 09 '13 15:03

Dafan


3 Answers

It is cleanest to make shutdown() calls on both the ssl::stream and its lowest_layer(). The first ends the SSL connection and the second ends the TCP connection. If you're getting an error on the SSL shutdown, it may be that the other side is not being as graceful in ending the connection.

like image 119
rhashimoto Avatar answered Nov 09 '22 05:11

rhashimoto


I do highly recommend that you should not use shutdown method and don't respect SSL layer + TCP layer (lowest_layer). be in safe side and close the tcp lowest_layer as

m_remote_socket.lowest_layer().close(ec);

The problem i faced that the time you respect the SSL or TCP, the application resources(socket handler) will stock in memory till server side send close session ack.

like image 3
Ahmed Avatar answered Nov 09 '22 06:11

Ahmed


Just call close(). It isn't legal to shutdown SSL sockets: there is no such thing as a half-close in SSL. See RFC 2246, discussion of close_notify.

like image 1
user207421 Avatar answered Nov 09 '22 07:11

user207421