@@ -5486,6 +5486,21 @@ int wolfSSL_CTX_get_read_ahead(WOLFSSL_CTX* ctx);
54865486 \ingroup Setup
54875487
54885488 \brief This function sets the read ahead flag in the WOLFSSL_CTX structure.
5489+ When enabled, the record-header read pulls in up to a full TLS record in a
5490+ single recv(), so the body (and any following buffered records) is obtained
5491+ without a second syscall. The flag only changes I/O behaviour when the
5492+ library is built with read-ahead support (--enable-readahead /
5493+ WOLFSSL_TLS_READ_AHEAD); otherwise it is stored but inert.
5494+
5495+ \note With read-ahead enabled, undecrypted data can remain buffered
5496+ internally while the socket has no more data to read. wolfSSL_has_pending()
5497+ reports whether any such data is buffered, but a non-zero return does not
5498+ guarantee a full record is available: wolfSSL_read() may still return
5499+ WANT_READ when only a partial record is buffered. Event-driven applications
5500+ should therefore drain the connection by calling wolfSSL_read() until it
5501+ returns WANT_READ (or an error) before returning to select()/poll(), rather
5502+ than looping on wolfSSL_has_pending() alone; otherwise buffered data could
5503+ be missed or the loop could spin.
54895504
54905505 \return SSL_SUCCESS If ctx read ahead flag set.
54915506 \return SSL_FAILURE If ctx is NULL then SSL_FAILURE is returned.
@@ -5506,9 +5521,147 @@ int wolfSSL_CTX_get_read_ahead(WOLFSSL_CTX* ctx);
55065521 \sa wolfSSL_CTX_new
55075522 \sa wolfSSL_CTX_free
55085523 \sa wolfSSL_CTX_get_read_ahead
5524+ \sa wolfSSL_has_pending
55095525*/
55105526int wolfSSL_CTX_set_read_ahead(WOLFSSL_CTX* ctx, int v);
55115527
5528+ /*!
5529+ \ingroup Setup
5530+
5531+ \brief This function sets the read-ahead receive window size for contexts
5532+ created from this WOLFSSL_CTX. When read-ahead is enabled
5533+ (wolfSSL_CTX_set_read_ahead()), a single recv() pulls in up to \p len bytes:
5534+ - \p len of 0 resets the window to the one-record default. This is also
5535+ the window a freshly created context already carries, so the record body
5536+ is read together with its header without a second syscall.
5537+ - A \p len larger than one record lets a single recv() coalesce several
5538+ back-to-back records, one syscall instead of one per record.
5539+ - A \p len smaller than one record caps the receive buffer's footprint
5540+ when the peer's records are known to be small (e.g. 4 KB), saving heap
5541+ versus the one-record default.
5542+ \p len is only a speculative read window, not a hard limit: a record larger
5543+ than \p len is still received correctly, with the input buffer grown to the
5544+ record's actual size on demand (costing an extra syscall and reallocation
5545+ for that record) and then reallocated back down to the window once the
5546+ oversized record is consumed, so the retained footprint stays bounded by
5547+ \p len rather than by the largest record seen. The setting only affects I/O
5548+ when the library is built with read-ahead support (--enable-readahead /
5549+ WOLFSSL_TLS_READ_AHEAD); otherwise it is stored but inert. This is the
5550+ wolfSSL equivalent of OpenSSL's SSL_CTX_set_default_read_buffer_len(); unlike
5551+ OpenSSL it returns a status code (callers that ignore the return remain
5552+ source-compatible) and it honours sizes below one record, whereas OpenSSL
5553+ only ever enlarges the buffer. A \p len above WOLFSSL_MAX_READ_AHEAD_SZ
5554+ (16 MB) is clamped to that maximum. Because 0 and oversized values are both
5555+ normalised, wolfSSL_CTX_get_default_read_buffer_len() reports the effective
5556+ window (the one-record default rather than 0 when unset), not the raw
5557+ argument.
5558+
5559+ \note This is a memory-vs-syscall trade-off. While read-ahead is enabled the
5560+ input buffer is retained (bounded to \p len) for the connection's lifetime,
5561+ so a large \p len multiplied by many concurrent connections is persistent
5562+ memory, while a small \p len bounds per-connection footprint at the cost of
5563+ more syscalls for records that exceed it.
5564+
5565+ \return SSL_SUCCESS If the buffer length was set.
5566+ \return SSL_FAILURE If ctx is NULL.
5567+
5568+ \param ctx WOLFSSL_CTX structure to set the read-ahead buffer length on.
5569+ \param len read-ahead coalescing buffer size in bytes (0 = one record).
5570+
5571+ _Example_
5572+ \code
5573+ WOLFSSL_CTX* ctx;
5574+ // setup ctx
5575+ wolfSSL_CTX_set_read_ahead(ctx, 1);
5576+ // coalesce up to four max-size records per recv()
5577+ wolfSSL_CTX_set_default_read_buffer_len(ctx, 4 * 16384);
5578+ \endcode
5579+
5580+ \sa wolfSSL_CTX_set_read_ahead
5581+ \sa wolfSSL_set_default_read_buffer_len
5582+ \sa wolfSSL_has_pending
5583+ */
5584+ int wolfSSL_CTX_set_default_read_buffer_len(WOLFSSL_CTX* ctx, size_t len);
5585+
5586+ /*!
5587+ \ingroup Setup
5588+
5589+ \brief This function sets the read-ahead coalescing buffer size on a single
5590+ WOLFSSL session, overriding the value inherited from its WOLFSSL_CTX. See
5591+ wolfSSL_CTX_set_default_read_buffer_len() for the full description, the
5592+ one-record default, and the memory-vs-syscall trade-off.
5593+
5594+ \return SSL_SUCCESS If the buffer length was set.
5595+ \return SSL_FAILURE If ssl is NULL.
5596+
5597+ \param ssl WOLFSSL structure to set the read-ahead buffer length on.
5598+ \param len read-ahead coalescing buffer size in bytes (0 = one record).
5599+
5600+ \sa wolfSSL_CTX_set_default_read_buffer_len
5601+ \sa wolfSSL_set_read_ahead
5602+ \sa wolfSSL_has_pending
5603+ */
5604+ int wolfSSL_set_default_read_buffer_len(WOLFSSL* ssl, size_t len);
5605+
5606+ /*!
5607+ \ingroup Setup
5608+
5609+ \brief This function returns the read-ahead coalescing buffer size
5610+ configured on a WOLFSSL_CTX by wolfSSL_CTX_set_default_read_buffer_len().
5611+ Because a length of 0 and oversized values are normalised when set, the
5612+ value reported is the effective window actually in use (the one-record
5613+ default rather than 0 when the length was never changed), not the raw
5614+ argument last passed.
5615+
5616+ \return len On success returns the read-ahead buffer length in bytes.
5617+ \return SSL_FAILURE If ctx is NULL.
5618+
5619+ \param ctx WOLFSSL_CTX structure to get the read-ahead buffer length from.
5620+
5621+ _Example_
5622+ \code
5623+ WOLFSSL_CTX* ctx;
5624+ long len;
5625+ // setup ctx
5626+ len = wolfSSL_CTX_get_default_read_buffer_len(ctx);
5627+ // check len
5628+ \endcode
5629+
5630+ \sa wolfSSL_CTX_set_default_read_buffer_len
5631+ \sa wolfSSL_get_default_read_buffer_len
5632+ \sa wolfSSL_CTX_set_read_ahead
5633+ */
5634+ long wolfSSL_CTX_get_default_read_buffer_len(WOLFSSL_CTX* ctx);
5635+
5636+ /*!
5637+ \ingroup Setup
5638+
5639+ \brief This function returns the read-ahead coalescing buffer size in
5640+ effect for a single WOLFSSL session, whether inherited from its
5641+ WOLFSSL_CTX or overridden by wolfSSL_set_default_read_buffer_len(). As with
5642+ the WOLFSSL_CTX getter, the value reported is the effective window in use,
5643+ not the raw argument last passed.
5644+
5645+ \return len On success returns the read-ahead buffer length in bytes.
5646+ \return SSL_FAILURE If ssl is NULL.
5647+
5648+ \param ssl WOLFSSL structure to get the read-ahead buffer length from.
5649+
5650+ _Example_
5651+ \code
5652+ WOLFSSL* ssl;
5653+ long len;
5654+ // setup ssl
5655+ len = wolfSSL_get_default_read_buffer_len(ssl);
5656+ // check len
5657+ \endcode
5658+
5659+ \sa wolfSSL_set_default_read_buffer_len
5660+ \sa wolfSSL_CTX_get_default_read_buffer_len
5661+ \sa wolfSSL_set_read_ahead
5662+ */
5663+ long wolfSSL_get_default_read_buffer_len(const WOLFSSL* ssl);
5664+
55125665/*!
55135666 \ingroup Setup
55145667
0 commit comments