The shutdown procedure consists of 2 steps: the sending of the \*(L"close notify\*(R"
shutdown alert and the reception of the peer's \*(L"close notify\*(R" shutdown
alert. According to the \s-1TLS\s0 standard, it is acceptable for an application
to only send its shutdown alert and then close the underlying connection
without waiting for the peer's response (this way resources can be saved,
as the process can already terminate or serve another connection).
When the underlying connection shall be used for more communications, the
complete shutdown procedure (bidirectional \*(L"close notify\*(R" alerts) must be
performed, so that the peers stay synchronized.
.PP
\&\fISSL_shutdown()\fR supports both uni- and bidirectional shutdown by its 2 step
behaviour.
.ifn.Ip"When the application is the first party to send the """"closenotify""""alert,\fISSL_shutdown()\fRwillonlysendthealertandthesetthe\s-1SSL_SENT_SHUTDOWN\s0flag(sothatthesessionisconsideredgoodandwillbekeptincache).\fISSL_shutdown()\fRwillthenreturnwith0.Ifaunidirectionalshutdownisenough(theunderlyingconnectionshallbeclosedanyway),thisfirstcallto\fISSL_shutdown()\fRissufficient.Inordertocompletethebidirectionalshutdownhandshake,\fISSL_shutdown()\fRmustbecalledagain.Thesecondcallwillmake\fISSL_shutdown()\fRwaitforthepeer's""""closenotify""""shutdownalert.Onsuccess,thesecondcallto\fISSL_shutdown()\fRwillreturnwith1."4
.el.Ip"When the application is the first party to send the ``close notify'' alert, \fISSL_shutdown()\fR will only send the alert and the set the \s-1SSL_SENT_SHUTDOWN\s0 flag (so that the session is considered good and will be kept in cache). \fISSL_shutdown()\fR will then return with 0. If a unidirectional shutdown is enough (the underlying connection shall be closed anyway), this first call to \fISSL_shutdown()\fR is sufficient. In order to complete the bidirectional shutdown handshake, \fISSL_shutdown()\fR must be called again. The second call will make \fISSL_shutdown()\fR wait for the peer's ``close notify'' shutdown alert. On success, the second call to \fISSL_shutdown()\fR will return with 1."4
.IXItem"When the application is the first party to send the "closenotifyalert,SSL_shutdown()willonlysendthealertandthesettheSSL_SENT_SHUTDOWNflag(sothatthesessionisconsideredgoodandwillbekeptincache).SSL_shutdown()willthenreturnwith0.Ifaunidirectionalshutdownisenough(theunderlyingconnectionshallbeclosedanyway),thisfirstcalltoSSL_shutdown()issufficient.Inordertocompletethebidirectionalshutdownhandshake,SSL_shutdown()mustbecalledagain.ThesecondcallwillmakeSSL_shutdown()waitforthepeer'sclosenotifyshutdownalert.Onsuccess,thesecondcalltoSSL_shutdown()willreturnwith1."
.ifn.Ip"If the peer already sent the """"closenotify""""alert\fBand\fRitwasalreadyprocessedimplicitlyinsideanotherfunction(SSL_read(3)),the\s-1SSL_RECEIVED_SHUTDOWN\s0flagisset.\fISSL_shutdown()\fRwillsendthe""""closenotify""""alert,setthe\s-1SSL_SENT_SHUTDOWN\s0flagandwillimmediatelyreturnwith1.Whether\s-1SSL_RECEIVED_SHUTDOWN\s0isalreadysetcanbecheckedusingthe\fISSL_get_shutdown()\fR(seealsoSSL_set_shutdown(3)call."4
.el.Ip"If the peer already sent the ``close notify'' alert \fBand\fR it was already processed implicitly inside another function (SSL_read(3)), the \s-1SSL_RECEIVED_SHUTDOWN\s0 flag is set. \fISSL_shutdown()\fR will send the ``close notify'' alert, set the \s-1SSL_SENT_SHUTDOWN\s0 flag and will immediately return with 1. Whether \s-1SSL_RECEIVED_SHUTDOWN\s0 is already set can be checked using the \fISSL_get_shutdown()\fR (see also SSL_set_shutdown(3) call."4
.IXItem"If the peer already sent the "closenotifyalertanditwasalreadyprocessedimplicitlyinsideanotherfunction(SSL_read(3)),theSSL_RECEIVED_SHUTDOWNflagisset.SSL_shutdown()willsendtheclosenotifyalert,settheSSL_SENT_SHUTDOWNflagandwillimmediatelyreturnwith1.WhetherSSL_RECEIVED_SHUTDOWNisalreadysetcanbecheckedusingtheSSL_get_shutdown()(seealsoSSL_set_shutdown(3)call."
.PP
It is therefore recommended, to check the return value of \fISSL_shutdown()\fR
and call \fISSL_shutdown()\fR again, if the bidirectional shutdown is not yet
complete (return value of the first call is 0). As the shutdown is not
specially handled in the SSLv2 protocol, \fISSL_shutdown()\fR will succeed on
the first call.
.PP
The behaviour of \fISSL_shutdown()\fR additionally depends on the underlying \s-1BIO\s0.