Blame lib/system_override.c

Packit Service 4684c1
/*
Packit Service 4684c1
 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
Packit Service 4684c1
 *
Packit Service 4684c1
 * Author: Nikos Mavrogiannopoulos
Packit Service 4684c1
 *
Packit Service 4684c1
 * This file is part of GnuTLS.
Packit Service 4684c1
 *
Packit Service 4684c1
 * The GnuTLS is free software; you can redistribute it and/or
Packit Service 4684c1
 * modify it under the terms of the GNU Lesser General Public License
Packit Service 4684c1
 * as published by the Free Software Foundation; either version 2.1 of
Packit Service 4684c1
 * the License, or (at your option) any later version.
Packit Service 4684c1
 *
Packit Service 4684c1
 * This library is distributed in the hope that it will be useful, but
Packit Service 4684c1
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 4684c1
 * Lesser General Public License for more details.
Packit Service 4684c1
 *
Packit Service 4684c1
 * You should have received a copy of the GNU Lesser General Public License
Packit Service 4684c1
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit Service 4684c1
 *
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
/* This file contains function that will override the 
Packit Service 4684c1
 * default berkeley sockets API per session.
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
#include "gnutls_int.h"
Packit Service 4684c1
#include "errors.h"
Packit Service 4684c1
#include <num.h>
Packit Service 4684c1
#include <record.h>
Packit Service 4684c1
#include <buffers.h>
Packit Service 4684c1
#include <mbuffers.h>
Packit Service 4684c1
#include <state.h>
Packit Service 4684c1
#include <dtls.h>
Packit Service 4684c1
#include <system.h>
Packit Service 4684c1
Packit Service 4684c1
#include <errno.h>
Packit Service 4684c1
#ifdef _WIN32
Packit Service 4684c1
#include <windows.h>
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 * gnutls_transport_set_errno:
Packit Service 4684c1
 * @session: is a #gnutls_session_t type.
Packit Service 4684c1
 * @err: error value to store in session-specific errno variable.
Packit Service 4684c1
 *
Packit Service 4684c1
 * Store @err in the session-specific errno variable.  Useful values
Packit Service 4684c1
 * for @err are EINTR, EAGAIN and EMSGSIZE, other values are treated will be
Packit Service 4684c1
 * treated as real errors in the push/pull function.
Packit Service 4684c1
 *
Packit Service 4684c1
 * This function is useful in replacement push and pull functions set by
Packit Service 4684c1
 * gnutls_transport_set_push_function() and
Packit Service 4684c1
 * gnutls_transport_set_pull_function() under Windows, where the
Packit Service 4684c1
 * replacements may not have access to the same @errno
Packit Service 4684c1
 * variable that is used by GnuTLS (e.g., the application is linked to
Packit Service 4684c1
 * msvcr71.dll and gnutls is linked to msvcrt.dll).
Packit Service 4684c1
 *
Packit Service 4684c1
 * This function is unreliable if you are using the same
Packit Service 4684c1
 * @session in different threads for sending and receiving.
Packit Service 4684c1
 *
Packit Service 4684c1
 **/
Packit Service 4684c1
void gnutls_transport_set_errno(gnutls_session_t session, int err)
Packit Service 4684c1
{
Packit Service 4684c1
	session->internals.errnum = err;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 * gnutls_transport_set_pull_function:
Packit Service 4684c1
 * @session: is a #gnutls_session_t type.
Packit Service 4684c1
 * @pull_func: a callback function similar to read()
Packit Service 4684c1
 *
Packit Service 4684c1
 * This is the function where you set a function for gnutls to receive
Packit Service 4684c1
 * data.  Normally, if you use berkeley style sockets, do not need to
Packit Service 4684c1
 * use this function since the default recv(2) will probably be ok.
Packit Service 4684c1
 * The callback should return 0 on connection termination, a positive
Packit Service 4684c1
 * number indicating the number of bytes received, and -1 on error.
Packit Service 4684c1
 *
Packit Service 4684c1
 * @gnutls_pull_func is of the form,
Packit Service 4684c1
 * ssize_t (*gnutls_pull_func)(gnutls_transport_ptr_t, void*, size_t);
Packit Service 4684c1
 **/
Packit Service 4684c1
void
Packit Service 4684c1
gnutls_transport_set_pull_function(gnutls_session_t session,
Packit Service 4684c1
				   gnutls_pull_func pull_func)
Packit Service 4684c1
{
Packit Service 4684c1
	session->internals.pull_func = pull_func;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 * gnutls_transport_set_pull_timeout_function:
Packit Service 4684c1
 * @session: is a #gnutls_session_t type.
Packit Service 4684c1
 * @func: a callback function
Packit Service 4684c1
 *
Packit Service 4684c1
 * This is the function where you set a function for gnutls to know
Packit Service 4684c1
 * whether data are ready to be received. It should wait for data a
Packit Service 4684c1
 * given time frame in milliseconds. The callback should return 0 on 
Packit Service 4684c1
 * timeout, a positive number if data can be received, and -1 on error.
Packit Service 4684c1
 * You'll need to override this function if select() is not suitable
Packit Service 4684c1
 * for the provided transport calls.
Packit Service 4684c1
 *
Packit Service 4684c1
 * As with select(), if the timeout value is zero the callback should return
Packit Service 4684c1
 * zero if no data are immediately available. The special value
Packit Service 4684c1
 * %GNUTLS_INDEFINITE_TIMEOUT indicates that the callback should wait indefinitely
Packit Service 4684c1
 * for data.
Packit Service 4684c1
 *
Packit Service 4684c1
 * @gnutls_pull_timeout_func is of the form,
Packit Service 4684c1
 * int (*gnutls_pull_timeout_func)(gnutls_transport_ptr_t, unsigned int ms);
Packit Service 4684c1
 *
Packit Service 4684c1
 * This callback is necessary when gnutls_handshake_set_timeout() or 
Packit Service 4684c1
 * gnutls_record_set_timeout() are set, under TLS1.3 and for enforcing the DTLS
Packit Service 4684c1
 * mode timeouts when in blocking mode.
Packit Service 4684c1
 *
Packit Service 4684c1
 * For compatibility with future GnuTLS versions this callback must be set when
Packit Service 4684c1
 * a custom pull function is registered. The callback will not be used when the
Packit Service 4684c1
 * session is in TLS mode with non-blocking sockets. That is, when %GNUTLS_NONBLOCK
Packit Service 4684c1
 * is specified for a TLS session in gnutls_init().
Packit Service 4684c1
 *
Packit Service 4684c1
 * The helper function gnutls_system_recv_timeout() is provided to
Packit Service 4684c1
 * simplify writing callbacks. 
Packit Service 4684c1
 *
Packit Service 4684c1
 * Since: 3.0
Packit Service 4684c1
 **/
Packit Service 4684c1
void
Packit Service 4684c1
gnutls_transport_set_pull_timeout_function(gnutls_session_t session,
Packit Service 4684c1
					   gnutls_pull_timeout_func func)
Packit Service 4684c1
{
Packit Service 4684c1
	session->internals.pull_timeout_func = func;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 * gnutls_transport_set_push_function:
Packit Service 4684c1
 * @session: is a #gnutls_session_t type.
Packit Service 4684c1
 * @push_func: a callback function similar to write()
Packit Service 4684c1
 *
Packit Service 4684c1
 * This is the function where you set a push function for gnutls to
Packit Service 4684c1
 * use in order to send data.  If you are going to use berkeley style
Packit Service 4684c1
 * sockets, you do not need to use this function since the default
Packit Service 4684c1
 * send(2) will probably be ok.  Otherwise you should specify this
Packit Service 4684c1
 * function for gnutls to be able to send data.
Packit Service 4684c1
 * The callback should return a positive number indicating the
Packit Service 4684c1
 * bytes sent, and -1 on error.
Packit Service 4684c1
 *
Packit Service 4684c1
 * @push_func is of the form,
Packit Service 4684c1
 * ssize_t (*gnutls_push_func)(gnutls_transport_ptr_t, const void*, size_t);
Packit Service 4684c1
 *
Packit Service 4684c1
 **/
Packit Service 4684c1
void
Packit Service 4684c1
gnutls_transport_set_push_function(gnutls_session_t session,
Packit Service 4684c1
				   gnutls_push_func push_func)
Packit Service 4684c1
{
Packit Service 4684c1
	session->internals.push_func = push_func;
Packit Service 4684c1
	session->internals.vec_push_func = NULL;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 * gnutls_transport_set_vec_push_function:
Packit Service 4684c1
 * @session: is a #gnutls_session_t type.
Packit Service 4684c1
 * @vec_func: a callback function similar to writev()
Packit Service 4684c1
 *
Packit Service 4684c1
 * Using this function you can override the default writev(2)
Packit Service 4684c1
 * function for gnutls to send data. Setting this callback 
Packit Service 4684c1
 * instead of gnutls_transport_set_push_function() is recommended
Packit Service 4684c1
 * since it introduces less overhead in the TLS handshake process.
Packit Service 4684c1
 *
Packit Service 4684c1
 * @vec_func is of the form,
Packit Service 4684c1
 * ssize_t (*gnutls_vec_push_func) (gnutls_transport_ptr_t, const giovec_t * iov, int iovcnt);
Packit Service 4684c1
 *
Packit Service 4684c1
 * Since: 2.12.0
Packit Service 4684c1
 **/
Packit Service 4684c1
void
Packit Service 4684c1
gnutls_transport_set_vec_push_function(gnutls_session_t session,
Packit Service 4684c1
				       gnutls_vec_push_func vec_func)
Packit Service 4684c1
{
Packit Service 4684c1
	session->internals.push_func = NULL;
Packit Service 4684c1
	session->internals.vec_push_func = vec_func;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 * gnutls_transport_set_errno_function:
Packit Service 4684c1
 * @session: is a #gnutls_session_t type.
Packit Service 4684c1
 * @errno_func: a callback function similar to write()
Packit Service 4684c1
 *
Packit Service 4684c1
 * This is the function where you set a function to retrieve errno
Packit Service 4684c1
 * after a failed push or pull operation.
Packit Service 4684c1
 *
Packit Service 4684c1
 * @errno_func is of the form,
Packit Service 4684c1
 * int (*gnutls_errno_func)(gnutls_transport_ptr_t);
Packit Service 4684c1
 * and should return the errno.
Packit Service 4684c1
 *
Packit Service 4684c1
 * Since: 2.12.0
Packit Service 4684c1
 **/
Packit Service 4684c1
void
Packit Service 4684c1
gnutls_transport_set_errno_function(gnutls_session_t session,
Packit Service 4684c1
				    gnutls_errno_func errno_func)
Packit Service 4684c1
{
Packit Service 4684c1
	session->internals.errno_func = errno_func;
Packit Service 4684c1
}