
#use wml::openssl-macros area=docs page=SSL_write

<title>Documents, SSL_write(3)</title>

<h1>SSL_write(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#NOTES">NOTES</A>
	<LI><A HREF="#WARNING">WARNING</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
SSL_write - write bytes to a TLS/SSL connection.

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/ssl.h&gt;
</PRE>
<PRE> int SSL_write(SSL *ssl, const void *buf, int num);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>SSL_write()</CODE> writes <STRONG>num</STRONG> bytes from the buffer <STRONG>buf</STRONG> into the specified
<STRONG>ssl</STRONG> connection.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
If necessary, <CODE>SSL_write()</CODE> will negotiate a TLS/SSL session, if
not already explicitly performed by <A HREF="../ssl/SSL_connect.html#">SSL_connect(3)</A> or
<A HREF="../ssl/SSL_accept.html#">SSL_accept(3)</A>. If the peer requests a re-negotiation, it will be performed transparently
during the <CODE>SSL_write()</CODE> operation. The behaviour of
<CODE>SSL_write()</CODE> depends on the underlying BIO. 

</P>
<P>
For the transparent negotiation to succeed, the <STRONG>ssl</STRONG> must have been initialized to client or server mode. This is being done by
calling
<A HREF="../ssl/SSL_set_connect_state.html#">SSL_set_connect_state(3)</A> or <CODE>SSL_set_accept_state()</CODE> before the first call to an <A HREF="../ssl/SSL_read.html#">SSL_read(3)</A> or <CODE>SSL_write()</CODE> function.

</P>
<P>
If the underlying BIO is <STRONG>blocking</STRONG>, <CODE>SSL_write()</CODE> will only return, once the write operation has
been finished or an error occurred, except when a renegotiation take place,
in which case a SSL_ERROR_WANT_READ may occur. This behaviour can be
controlled with the SSL_MODE_AUTO_RETRY flag of the
<A HREF="../ssl/SSL_CTX_set_mode.html#">SSL_CTX_set_mode(3)</A> call.

</P>
<P>
If the underlying BIO is <STRONG>non-blocking</STRONG>, <CODE>SSL_write()</CODE> will also return, when the underlying BIO could
not satisfy the needs of <CODE>SSL_write()</CODE> to continue the
operation. In this case a call to
<A HREF="../ssl/SSL_get_error.html#">SSL_get_error(3)</A> with the return value of <CODE>SSL_write()</CODE> will yield <STRONG>SSL_ERROR_WANT_READ</STRONG> or
<STRONG>SSL_ERROR_WANT_WRITE</STRONG>. As at any time a re-negotiation is possible, a call to
<CODE>SSL_write()</CODE> can also cause read operations! The calling
process then must repeat the call after taking appropriate action to
satisfy the needs of <CODE>SSL_write().</CODE> The action depends on the
underlying BIO. When using a non-blocking socket, nothing is to be done,
but <CODE>select()</CODE> can be used to check for the required condition.
When using a buffering BIO, like a BIO pair, data must be written into or
retrieved out of the BIO before being able to continue.

</P>
<P>
<CODE>SSL_write()</CODE> will only return with success, when the complete
contents of <STRONG>buf</STRONG> of length <STRONG>num</STRONG> has been written. This default behaviour can be changed with the
SSL_MODE_ENABLE_PARTIAL_WRITE option of
<A HREF="../ssl/SSL_CTX_set_mode.html#">SSL_CTX_set_mode(3)</A>. When this flag is set, <CODE>SSL_write()</CODE> will also return with
success, when a partial write has been successfully completed. In this case
the <CODE>SSL_write()</CODE> operation is considered completed. The bytes
are sent and a new <CODE>SSL_write()</CODE> operation with a new buffer
(with the already sent bytes removed) must be started. A partial write is
performed with the size of a message block, which is 16kB for SSLv3/TLSv1.

</P>
<P>
<HR>
<H1><A NAME="WARNING">WARNING</A></H1>
<P>
When an <CODE>SSL_write()</CODE> operation has to be repeated because of
<STRONG>SSL_ERROR_WANT_READ</STRONG> or <STRONG>SSL_ERROR_WANT_WRITE</STRONG>, it must be repeated with the same arguments.

</P>
<P>
When calling <CODE>SSL_write()</CODE> with num=0 bytes to be sent the
behaviour is undefined.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
The following return values can occur:

</P>
<DL>
<DT><STRONG><A NAME="item_gt0">&gt;0</A></STRONG><DD>
<P>
The write operation was successful, the return value is the number of bytes
actually written to the TLS/SSL connection.

</P>
<DT><STRONG><A NAME="item_0">0</A></STRONG><DD>
<P>
The write operation was not successful. Probably the underlying connection
was closed. Call <CODE>SSL_get_error()</CODE> with the return value <STRONG>ret</STRONG> to find out, whether an error occurred or the connection was shut down
cleanly (SSL_ERROR_ZERO_RETURN).

</P>
<P>
SSLv2 (deprecated) does not support a shutdown alert protocol, so it can
only be detected, whether the underlying connection was closed. It cannot
be checked, why the closure happened.

</P>
<DT><STRONG><A NAME="item_lt0">&lt;0</A></STRONG><DD>
<P>
The write operation was not successful, because either an error occurred or
action must be taken by the calling process. Call
<CODE>SSL_get_error()</CODE> with the return value <STRONG>ret</STRONG> to find out the reason.

</P>
</DL>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../ssl/SSL_get_error.html#">SSL_get_error(3)</A>, <A HREF="../ssl/SSL_read.html#">SSL_read(3)</A>,
<A HREF="../ssl/SSL_CTX_set_mode.html#">SSL_CTX_set_mode(3)</A>, <A HREF="../ssl/SSL_CTX_new.html#">SSL_CTX_new(3)</A>,
<A HREF="../ssl/SSL_connect.html#">SSL_connect(3)</A>, <A HREF="../ssl/SSL_accept.html#">SSL_accept(3)</A>

<A HREF="../ssl/SSL_set_connect_state.html#">SSL_set_connect_state(3)</A>,
<A HREF="../ssl/ssl.html#">ssl(3)</A>, <A HREF="../crypto/bio.html#">bio(3)</A>



</P>
:}


