/*------------------------------------------------------------------------------
 *
 * Copyright (c) 2011-2025, EURid vzw. All rights reserved.
 * The YADIFA TM software product is provided under the BSD 3-clause license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *        * Redistributions of source code must retain the above copyright
 *          notice, this list of conditions and the following disclaimer.
 *        * Redistributions in binary form must reproduce the above copyright
 *          notice, this list of conditions and the following disclaimer in the
 *          documentation and/or other materials provided with the distribution.
 *        * Neither the name of EURid nor the names of its contributors may be
 *          used to endorse or promote products derived from this software
 *          without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *----------------------------------------------------------------------------*/

/**-----------------------------------------------------------------------------
 * @defgroup streaming Streams
 * @ingroup dnscore
 * @brief
 *
 *
 *
 * @{
 *----------------------------------------------------------------------------*/
#pragma once

#include <dnscore/input_stream.h>
#include <dnscore/output_stream.h>

#ifdef __cplusplus
extern "C"
{
#endif

/**
 * SSL read helper.
 * Reads from an SSL connection.
 * Handles retries.
 * Can short-read.
 *
 * @param ssl_ the connection (SSL*)
 * @param buffer the buffer to read to
 * @param len the number of bytes to read
 * @return the number of bytes read or an error code
 */
ya_result ssl_read(void *ssl_, void *buffer, uint32_t len);

/**
 * SSL write helper.
 * Writes to an SSL connection.
 * Handles retries.
 * Can short-write.
 *
 * @param ssl_ the connection (SSL*)
 * @param buffer the buffer to write
 * @param len the number of bytes to write
 * @return the number of bytes written or an error code
 */
ya_result ssl_write(void *ssl_, const uint8_t *buffer, uint32_t len);

/**
 * SSL skip helper.
 * Skip bytes from an SSL connection.
 * Handles retries.
 * May skip less in case of connection closed or error.
 *
 * @param ssl_ the connection (SSL*)
 * @param len the number of bytes to skip
 * @return the number of bytes skipped or an error code
 */
ya_result ssl_skip(void *ssl_, uint32_t len);  // no need to skip fully as this one already does it

/**
 * SSL read helper.
 * Reads from an SSL connection.
 * Handles retries.
 * May short read in case of connection closed or error.
 *
 * @param ssl_ the connection (SSL*)
 * @param buffer the buffer to read to
 * @param len the number of bytes to read
 * @return the number of bytes read or an error code
 */
ya_result ssl_readfully(void *ssl_, void *buffer, uint32_t len);

/**
 * SSL write helper.
 * Writes to an SSL connection.
 * Handles retries.
 * May short write in case of connection closed or error.
 *
 * @param ssl_ the connection (SSL*)
 * @param buffer the buffer to write
 * @param len the number of bytes to write
 * @return the number of bytes written or an error code
 */
ya_result ssl_writefully(void *ssl_, const void *buffer, uint32_t len);

/**
 * Adds the SSL protocol with the 3dot ALPN on an open TCP connection.
 *
 * @param in_stream the SSL input stream
 * @param in_filtered the TCP input stream
 * @param out_stream the SSL output stream
 * @param out_filtered the TCP output stream
 * @param cert_pem the X509 certificate to use
 * @param key_pem the key of the X509 certificate
 *
 */

ya_result       ssl_input_output_stream_init(input_stream_t *in_stream, input_stream_t *in_filtered, output_stream_t *out_stream, output_stream_t *out_filtered, const char *cert_pem, const char *key_pem);

input_stream_t *ssl_input_stream_get_filtered(input_stream_t *bis);

/**
 * Returns true iff the input stream is a buffer input stream
 *
 * @param bos
 * @return
 */

bool is_ssl_input_stream(input_stream_t *bos);

#ifdef __cplusplus
}
#endif

/** @} */
