1

I have a client-server application where the connection is performed via SSL. and uses boost::asio.

The relevant client code to establish a connection is

Client::Client(boost::asio::io_service& io_service,
               boost::asio::ssl::context& context,
               boost::asio::ip::tcp::resolver::iterator endpoint_iterator) : AbstractNetworkOps(io_service, context)
{
    socket_.set_verify_mode(boost::asio::ssl::verify_peer);
    socket_.set_verify_callback(boost::bind(&Client::verify_certificate, this, _1, _2));

    this->endpoint_iterator = endpoint_iterator; 
}

Client::~Client() {}


void Client::startConnection() {
    Log("Start connecting...");

    boost::system::error_code ec;
    boost::asio::connect(socket_.lowest_layer(), this->endpoint_iterator, ec);

    handle_connect(ec);
}

After a certain amount of message exchanges, the client should close the connection for which I call the following method

void AbstractNetworkOps::saveCloseSocket() {
    boost::system::error_code ec;

    socket_.lowest_layer().cancel();
    socket_.shutdown(ec);

    if (ec) {
        stringstream ss;
        Log("Socket shutdown error: %s", ec.message());
    } else {
        socket_.lowest_layer().close();
    }
}

The problem here is, that the call to socket_.shutdown(ec) blocks somehow and does not return!? Is this not the correct way of doing it?

P.S. The application is quite large, that's why I cannot post the entire code here but please let me know if some important pieces are missing, so I can update it

wasp256
  • 5,943
  • 12
  • 72
  • 119
  • Is there a `recv` call outstanding? Call `shutdown` – stark Feb 26 '17 at 20:12
  • The client performs a `boost::asio::read` and after the received data has been processed, the `saveCloseSocket` method is called, so I'd say no, but is there a way to confirm that? – wasp256 Feb 26 '17 at 20:17
  • You should not shutdown an SSL socket. Just close it. You are making it impossible for the SSL close handshake to proceed. If you have the same error at the peer you could get a deadlock. – user207421 Feb 26 '17 at 21:26
  • If I just close the socket (uncommenting the `shutdown` from above), then on the server side I receive the error `Unknown socket error while reading occured!`; I assume this is because the server is still listening on this connection, but is this supposed to be terminated like this then? – wasp256 Feb 27 '17 at 06:53
  • Shutting down `SSL` sockets correctly is complicated. See [What is the proper way to securely disconnect an SSL scoket](http://stackoverflow.com/questions/32046034/what-is-the-proper-way-to-securely-disconnect-an-asio-ssl-socket) and Tanner Sainsbury's answer [here](http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error/25703699#25703699). – kenba Feb 27 '17 at 13:48

0 Answers0