nmathewson/libevent-book — pull request #9 by droe (2012-02-27 20:59:51+00:00)
Title: Make the SSL_shutdown() hack actually work

The proposed hack does not achieve the goal, because it will lead to
an unclean shutdown on the other end, which means that the other end
will mark the session unclean and drop it from the session cache,
leading to degraded performance exactly as if we just closed the
socket.

So instead of setting both SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN,
only set SSL_RECEIVED_SHUTDOWN and let OpenSSL send a close notify.
Because there is no distinction in the protocol between close notify
requests and close notify replies, this has the net effect of a clean
shutdown on both ends of the SSL connection.

Also note that SSL_shutdown() will not block on a non-blocking socket.
Instead of blocking, it will return -1 with one of SSL_ERROR_WANT_READ
or SSL_ERROR_WANT_WRITE, telling us to retry SSL_shutdown() after the
underlying socket has become readable or writable, respectively.